Django 单元测试中出现未经授权的用户错误
Unauthorized User Error in Django Unit test
您好!我正在为网站编写单元测试用例,该网站具有地址更新的基本功能,地址更新需要真实的用户登录,因此我在查询参数中为此目的使用临时令牌(作为网站的一部分功能)
from django.test import TestCase
from accounts.models import Address, User, Business, Employment
from accounts.controllers import AddressController
class Test_Cases_AddressController(TestCase):
user= User.objects.create_user( birth_year=1996, birth_month= 8, birth_day = 15, marital_status= "married", \
reset_password_token = "xxxxy", confirmation_token = "xxxx", password_reset_at= "2022-01-12 13:12:13", \
confirmed_at= "2022-01-12 13:12:13", email= "testuser@example.com", username = "testuser123")
def test_update_address(self):
address1 = Address.objects.create(street_1="abc abc abc", city="lahore", state="punjab", zip="54000", type="abcdef")
API_LOGIN_URL = '/addresses/1/?_t=xxxx/'
response= self.client.put(API_LOGIN_URL, {"street_1": "xyz xyz xyz"}, content_type='application/json' )
self.assertEquals(response.status_code, 201)
def test_get_address(self):
API_LOGIN_URL = '/addresses/1/?_t=xxxx/'
response= self.client.get(API_LOGIN_URL)
self.assertEquals(response.status_code, 200)
但我仍然遇到同样的未授权错误
PS C:\Users\Lenovo\web> docker-compose run --rm api sh -c "python manage.py test accounts"
Creating web_api_run ... done
Creating test database for alias 'default'...
System check identified some issues:
WARNINGS:
accounts.User.privacy: (postgres.E003) JSONField default should be a callable instead of an instance so that it's not shared between all field instances.
HINT: Use a callable instead, e.g., use `dict` instead of `{}`.
main.NonProfit.website: (fields.W340) null has no effect on ManyToManyField.
System check identified 2 issues (0 silenced).
.:::token:::
xxxx/
============token_attribute===confirmation_token
=============xxxx/
===========confirmed_at
error :::
:::token:::
xxxx/
============token_attribute===reset_password_token
=============xxxx/
===========password_reset_at
error :::
WARNING:django.request:Unauthorized: /addresses/1/
F:::token:::
xxxx/
============token_attribute===confirmation_token
=============xxxx/
===========confirmed_at
error :::
:::token:::
xxxx/
============token_attribute===reset_password_token
=============xxxx/
===========password_reset_at
error :::
WARNING:django.request:Unauthorized: /addresses/1/
F
======================================================================
FAIL: test_get_address (accounts.tests.Test_Cases_AddressController)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/src/app/web/accounts/tests.py", line 26, in test_get_address
self.assertEquals(response.status_code, 200)
AssertionError: 401 != 200
======================================================================
FAIL: test_update_address (accounts.tests.Test_Cases_AddressController)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/src/app/web/accounts/tests.py", line 21, in test_update_address
self.assertEquals(response.status_code, 201)
AssertionError: 401 != 201
----------------------------------------------------------------------
Ran 3 tests in 0.147s
FAILED (failures=2)
Destroying test database for alias 'default'...
ERROR: 1
这里是用户模型
class User(AbstractUser, BaseModelMixin):
birth_year = models.IntegerField(default=1900)
birth_month = models.IntegerField(default=1)
birth_day = models.IntegerField(default=1)
marital_status = models.CharField(max_length=255, blank=True, default="")
reset_password_token = models.CharField(
max_length=32, blank=True, default="")
password_reset_at = models.DateTimeField(null=True, blank=True)
reset_password_sent_at = models.DateTimeField(null=True, blank=True)
confirmation_token = models.CharField(
max_length=32, blank=True, default="")
confirmed_at = models.DateTimeField(null=True, blank=True)
confirmation_sent_at = models.DateTimeField(null=True, blank=True)
employers = models.ManyToManyField(Business, through="Employment")
address = models.ForeignKey(
Address, null=True, blank=True, on_delete=models.CASCADE)
email = models.EmailField("Email Address", blank=True, unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
这是Url
url(r'^addresses/(?P<address>[^/]+)/?$', AddressController()),
非常感谢您的帮助。提前致谢
我认为你应该在 test force 中创建一个测试用户来验证你的请求
self.client.force_authenticate(user=self.your_created_user)
您好!我正在为网站编写单元测试用例,该网站具有地址更新的基本功能,地址更新需要真实的用户登录,因此我在查询参数中为此目的使用临时令牌(作为网站的一部分功能)
from django.test import TestCase
from accounts.models import Address, User, Business, Employment
from accounts.controllers import AddressController
class Test_Cases_AddressController(TestCase):
user= User.objects.create_user( birth_year=1996, birth_month= 8, birth_day = 15, marital_status= "married", \
reset_password_token = "xxxxy", confirmation_token = "xxxx", password_reset_at= "2022-01-12 13:12:13", \
confirmed_at= "2022-01-12 13:12:13", email= "testuser@example.com", username = "testuser123")
def test_update_address(self):
address1 = Address.objects.create(street_1="abc abc abc", city="lahore", state="punjab", zip="54000", type="abcdef")
API_LOGIN_URL = '/addresses/1/?_t=xxxx/'
response= self.client.put(API_LOGIN_URL, {"street_1": "xyz xyz xyz"}, content_type='application/json' )
self.assertEquals(response.status_code, 201)
def test_get_address(self):
API_LOGIN_URL = '/addresses/1/?_t=xxxx/'
response= self.client.get(API_LOGIN_URL)
self.assertEquals(response.status_code, 200)
但我仍然遇到同样的未授权错误
PS C:\Users\Lenovo\web> docker-compose run --rm api sh -c "python manage.py test accounts"
Creating web_api_run ... done
Creating test database for alias 'default'...
System check identified some issues:
WARNINGS:
accounts.User.privacy: (postgres.E003) JSONField default should be a callable instead of an instance so that it's not shared between all field instances.
HINT: Use a callable instead, e.g., use `dict` instead of `{}`.
main.NonProfit.website: (fields.W340) null has no effect on ManyToManyField.
System check identified 2 issues (0 silenced).
.:::token:::
xxxx/
============token_attribute===confirmation_token
=============xxxx/
===========confirmed_at
error :::
:::token:::
xxxx/
============token_attribute===reset_password_token
=============xxxx/
===========password_reset_at
error :::
WARNING:django.request:Unauthorized: /addresses/1/
F:::token:::
xxxx/
============token_attribute===confirmation_token
=============xxxx/
===========confirmed_at
error :::
:::token:::
xxxx/
============token_attribute===reset_password_token
=============xxxx/
===========password_reset_at
error :::
WARNING:django.request:Unauthorized: /addresses/1/
F
======================================================================
FAIL: test_get_address (accounts.tests.Test_Cases_AddressController)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/src/app/web/accounts/tests.py", line 26, in test_get_address
self.assertEquals(response.status_code, 200)
AssertionError: 401 != 200
======================================================================
FAIL: test_update_address (accounts.tests.Test_Cases_AddressController)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/src/app/web/accounts/tests.py", line 21, in test_update_address
self.assertEquals(response.status_code, 201)
AssertionError: 401 != 201
----------------------------------------------------------------------
Ran 3 tests in 0.147s
FAILED (failures=2)
Destroying test database for alias 'default'...
ERROR: 1
这里是用户模型
class User(AbstractUser, BaseModelMixin):
birth_year = models.IntegerField(default=1900)
birth_month = models.IntegerField(default=1)
birth_day = models.IntegerField(default=1)
marital_status = models.CharField(max_length=255, blank=True, default="")
reset_password_token = models.CharField(
max_length=32, blank=True, default="")
password_reset_at = models.DateTimeField(null=True, blank=True)
reset_password_sent_at = models.DateTimeField(null=True, blank=True)
confirmation_token = models.CharField(
max_length=32, blank=True, default="")
confirmed_at = models.DateTimeField(null=True, blank=True)
confirmation_sent_at = models.DateTimeField(null=True, blank=True)
employers = models.ManyToManyField(Business, through="Employment")
address = models.ForeignKey(
Address, null=True, blank=True, on_delete=models.CASCADE)
email = models.EmailField("Email Address", blank=True, unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
这是Url
url(r'^addresses/(?P<address>[^/]+)/?$', AddressController()),
非常感谢您的帮助。提前致谢
我认为你应该在 test force 中创建一个测试用户来验证你的请求
self.client.force_authenticate(user=self.your_created_user)