Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_post_should_not_login_if_empty_request(self):
data = {}
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'],
[djoser.constants.INVALID_CREDENTIALS_ERROR]
)
class LogoutViewTest(restframework.APIViewTestCase,
assertions.StatusCodeAssertionsMixin):
view_class = djoser.views.LogoutView
def setUp(self):
self.signal_sent = False
def signal_receiver(self, *args, **kwargs):
self.signal_sent = True
def test_post_should_logout_logged_in_user(self):
user = create_user()
user_logged_out.connect(self.signal_receiver)
request = self.factory.post(user=user)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
'username': 'john',
'email': 'john@beatles.com',
'password': 'secret',
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.data, [djoser.constants.CANNOT_CREATE_USER_ERROR]
)
class LoginViewTest(restframework.APIViewTestCase,
assertions.StatusCodeAssertionsMixin,
assertions.InstanceAssertionsMixin):
view_class = djoser.views.LoginView
def setUp(self):
self.signal_sent = False
def signal_receiver(self, *args, **kwargs):
self.signal_sent = True
def test_post_should_login_user(self):
user = create_user()
data = {
'username': user.username,
'password': user.raw_password,
}
user_logged_in.connect(self.signal_receiver)
data = {
'new_password': 'new password',
'current_password': 'secret',
}
request = self.factory.post(user=user, data=data)
djoser.utils.login_user(request, user)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
is_logged = authtoken.models.Token.objects.filter(user=user).exists()
self.assertTrue(is_logged)
class SetUsernameViewTest(restframework.APIViewTestCase,
assertions.StatusCodeAssertionsMixin):
view_class = djoser.views.SetUsernameView
def test_post_should_set_new_username(self):
user = create_user()
data = {
'new_username': 'ringo',
'current_password': 'secret',
}
request = self.factory.post(user=user, data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
user = utils.refresh(user)
self.assertEqual(data['new_username'], user.username)
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data['non_field_errors'], [djoser.constants.INVALID_CREDENTIALS_ERROR])
self.assertTrue(self.signal_sent)
def test_post_should_not_login_if_empty_request(self):
data = {}
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'], [djoser.constants.INVALID_CREDENTIALS_ERROR])
class LogoutViewTest(restframework.APIViewTestCase,
assertions.StatusCodeAssertionsMixin):
view_class = djoser.views.LogoutView
def setUp(self):
self.signal_sent = False
def signal_receiver(self, *args, **kwargs):
self.signal_sent = True
def test_post_should_logout_logged_in_user(self):
user = create_user()
user_logged_out.connect(self.signal_receiver)
request = self.factory.post(user=user)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
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)
**{'PASSWORD_RESET_SHOW_EMAIL_NOT_FOUND': True}))
def test_post_should_return_bad_request_if_user_does_not_exist(self):
data = {
'email': 'john@beatles.com',
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data['email'][0],
djoser.constants.EMAIL_NOT_FOUND)
class PasswordResetConfirmViewTest(restframework.APIViewTestCase,
assertions.StatusCodeAssertionsMixin):
view_class = djoser.views.PasswordResetConfirmView
def test_post_should_set_new_password(self):
user = create_user()
data = {
'uid': djoser.utils.encode_uid(user.pk),
'token': default_token_generator.make_token(user),
'new_password': 'new password',
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
user = utils.refresh(user)
assertions.StatusCodeAssertionsMixin):
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)
**{'PASSWORD_RESET_SHOW_EMAIL_NOT_FOUND': True}))
def test_post_should_return_bad_request_if_user_does_not_exist(self):
data = {
'email': 'john@beatles.com',
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data['email'][0],
djoser.constants.EMAIL_NOT_FOUND)
class PasswordResetConfirmViewTest(restframework.APIViewTestCase,
assertions.StatusCodeAssertionsMixin):
view_class = djoser.views.PasswordResetConfirmView
def test_post_should_set_new_password(self):
user = create_user()
data = {
'uid': djoser.utils.encode_uid(user.pk),
'token': default_token_generator.make_token(user),
'new_password': 'new password',
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
user = utils.refresh(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