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_set_new_username_if_invalid(self):
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'])
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)
def test_post_should_not_set_new_username_if_exists(self):
username = 'tom'
create_user(username=username)
user = create_user(username='john')
data = {
'new_username': 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, username)
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),
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
user = utils.refresh(user)
self.assertTrue(user.is_active)
def test_post_should_not_set_new_username_if_exists(self):
username = 'tom'
create_user(username=username)
user = create_user(username='john')
data = {
'new_username': 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, username)
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)
self.assertTrue(user.check_password(data['new_password']))
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),
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
user = utils.refresh(user)
self.assertTrue(user.is_active)
def test_post_should_logout_logged_in_user(self):
user = create_user()
token = models.Token.objects.create(user=user, client='my-device')
request = self.factory.post(user=user, token=token)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_200_OK)
self.assertEqual(response.data, None)
with self.assertRaises(models.Token.DoesNotExist):
utils.refresh(token)
def test_post_should_not_set_new_password_if_wrong_token(self):
user = create_user()
data = {
'uid': djoser.utils.encode_uid(user.pk),
'token': 'wrong-token',
'new_password': 'new password',
}
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_TOKEN_ERROR])
user = utils.refresh(user)
self.assertFalse(user.check_password(data['new_password']))
def test_post_should_not_set_new_password_if_mismatch(self):
user = create_user()
data = {
'uid': djoser.utils.encode_uid(user.pk),
'token': default_token_generator.make_token(user),
'new_password': 'new password',
're_new_password': 'wrong',
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
user = utils.refresh(user)
self.assertFalse(user.check_password(data['new_password']))