Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
view_class = djoser.views.RootView
def test_get_should_return_urls_mapping(self):
request = self.factory.get()
view_object = self.create_view_object(request)
response = view_object.dispatch(request)
self.assert_status_equal(response, status.HTTP_200_OK)
for key in view_object.get_urls_mapping().keys():
self.assertIn(key, response.data)
class RegistrationViewTest(restframework.APIViewTestCase,
assertions.StatusCodeAssertionsMixin,
assertions.EmailAssertionsMixin,
assertions.InstanceAssertionsMixin):
view_class = djoser.views.RegistrationView
def test_post_should_create_user_without_login(self):
data = {
'username': 'john',
'password': 'secret',
'csrftoken': 'asdf',
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_201_CREATED)
self.assertTrue('password' not in response.data)
self.assert_instance_exists(get_user_model(), username=data['username'])
response = self.view(request)
self.assert_status_equal(response, status.HTTP_403_FORBIDDEN)
def test_options(self):
user = create_user()
request = self.factory.options(user=user)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_200_OK)
class PasswordResetViewTest(restframework.APIViewTestCase,
assertions.StatusCodeAssertionsMixin,
assertions.EmailAssertionsMixin):
view_class = djoser.views.PasswordResetView
def test_post_should_send_email_to_user_with_password_rest_link(self):
user = create_user()
data = {
'email': user.email,
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
self.assert_emails_in_mailbox(1)
self.assert_email_exists(to=[user.email])
site = djoser.utils.get_current_site(request)
self.assertIn(site.domain, mail.outbox[0].body)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_401_UNAUTHORIZED)
def test_options(self):
user = create_user()
request = self.factory.options(user=user)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_200_OK)
class PasswordResetViewTest(restframework.APIViewTestCase,
assertions.StatusCodeAssertionsMixin,
assertions.EmailAssertionsMixin):
view_class = djoser.views.PasswordResetView
def test_post_should_send_email_to_user_with_password_rest_link(self):
user = create_user()
data = {
'email': user.email,
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
self.assert_emails_in_mailbox(1)
self.assert_email_exists(to=[user.email])
site = djoser.utils.get_current_site(request)
self.assertIn(site.domain, mail.outbox[0].body)
user = create_user()
data = {
'uid': djoser.utils.encode_uid(user.pk),
'token': default_token_generator.make_token(user),
'new_password': '666',
're_new_password': 'isokpassword',
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data, {'non_field_errors': ['两个密码字段不一致。']})
class ActivationViewTest(restframework.APIViewTestCase,
assertions.EmailAssertionsMixin,
assertions.StatusCodeAssertionsMixin):
view_class = djoser.views.ActivationView
def setUp(self):
self.signal_sent = False
def signal_receiver(self, *args, **kwargs):
self.signal_sent = True
def test_post_should_activate_user_and_not_login(self):
user = create_user()
user.is_active = False
user.save()
data = {
'uid': djoser.utils.encode_uid(user.pk),
'token': default_token_generator.make_token(user),
'uid': djoser.utils.encode_uid(user.pk),
'token': default_token_generator.make_token(user),
'new_password': '666',
're_new_password': 'isokpassword',
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.data, {'new_password': ['Password 666 is not allowed.']}
)
class ActivationViewTest(restframework.APIViewTestCase,
assertions.EmailAssertionsMixin,
assertions.StatusCodeAssertionsMixin):
view_class = djoser.views.ActivationView
def setUp(self):
self.signal_sent = False
def signal_receiver(self, *args, **kwargs):
self.signal_sent = True
def test_post_should_activate_user_and_not_login(self):
user = create_user()
user.is_active = False
user.save()
data = {
'uid': djoser.utils.encode_uid(user.pk),
'token': default_token_generator.make_token(user),
def create_user(**kwargs):
data = {
'username': 'john',
'password': 'secret',
'email': 'john@beatles.com',
}
data.update(kwargs)
user = get_user_model().objects.create_user(**data)
user.raw_password = data['password']
return user
class RegistrationViewTest(restframework.APIViewTestCase,
assertions.StatusCodeAssertionsMixin,
assertions.EmailAssertionsMixin,
assertions.InstanceAssertionsMixin):
view_class = djoser.views.RegistrationView
def test_post_should_create_user_without_login(self):
data = {
'username': 'john',
'password': 'secret',
'csrftoken': 'asdf',
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_201_CREATED)
self.assertTrue('password' not in response.data)
self.assert_instance_exists(get_user_model(), username=data['username'])
user = create_user()
data = {
'new_username': '$ wrong username #',
'current_password': 'secret',
}
request = self.factory.post(user=user, data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
user = utils.refresh(user)
self.assertNotEqual(user.username, data['new_username'])
class UserViewTest(restframework.APIViewTestCase,
assertions.EmailAssertionsMixin,
assertions.StatusCodeAssertionsMixin):
view_class = djoser.views.UserView
def test_get_should_return_user(self):
user = create_user()
request = self.factory.get(user=user)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_200_OK)
self.assertEqual(set(response.data.keys()), set(
[get_user_model().USERNAME_FIELD, get_user_model()._meta.pk.name] + get_user_model().REQUIRED_FIELDS
))
def test_put_should_update_user(self):
user = create_user()
For example::
>>> with self.assert_instance_deleted(Article, slug='lorem-ipsum'):
... Article.objects.get(slug='lorem-ipsum').delete()
"""
return _InstanceContext(
self.assert_instance_exists,
self.assert_instance_does_not_exist,
model_class,
**kwargs
)
class CompleteAssertionsMixin(
StatusCodeAssertionsMixin,
EmailAssertionsMixin,
MessagesAssertionsMixin,
InstanceAssertionsMixin,
):
pass