|
@@ -79,10 +79,28 @@ class GatewayTests(TestCase):
|
|
|
self.assertEqual(user_json['id'], user.id)
|
|
|
self.assertEqual(user_json['username'], user.username)
|
|
|
|
|
|
- def test_submit_empty(self):
|
|
|
+ def test_submit_no_data(self):
|
|
|
"""login api errors for no body"""
|
|
|
response = self.client.post('/api/auth/')
|
|
|
- self.assertContains(response, 'empty_data', status_code=400)
|
|
|
+
|
|
|
+ self.assertEqual(response.status_code, 400)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'username': ['This field is required.'],
|
|
|
+ 'password': ['This field is required.'],
|
|
|
+ })
|
|
|
+
|
|
|
+ def test_submit_empty(self):
|
|
|
+ """login api errors for empty fields"""
|
|
|
+ response = self.client.post('/api/auth/', data={
|
|
|
+ 'username': '',
|
|
|
+ 'password': '',
|
|
|
+ })
|
|
|
+
|
|
|
+ self.assertEqual(response.status_code, 400)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'username': ['This field may not be blank.'],
|
|
|
+ 'password': ['This field may not be blank.'],
|
|
|
+ })
|
|
|
|
|
|
def test_submit_invalid(self):
|
|
|
"""login api errors for invalid data"""
|
|
@@ -91,6 +109,7 @@ class GatewayTests(TestCase):
|
|
|
'false',
|
|
|
content_type="application/json",
|
|
|
)
|
|
|
+
|
|
|
self.assertContains(response, "Invalid data.", status_code=400)
|
|
|
|
|
|
def test_login_banned(self):
|
|
@@ -110,15 +129,14 @@ class GatewayTests(TestCase):
|
|
|
'password': 'Pass.123',
|
|
|
},
|
|
|
)
|
|
|
- self.assertEqual(response.status_code, 400)
|
|
|
-
|
|
|
- response_json = response.json()
|
|
|
-
|
|
|
- self.assertEqual(response_json['detail']['plain'], ban.user_message)
|
|
|
- self.assertEqual(
|
|
|
- response_json['detail']['html'], '<p>%s</p>' % ban.user_message
|
|
|
- )
|
|
|
- self.assertIn('expires_on', response_json)
|
|
|
+ self.assertEqual(response.status_code, 403)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'detail': {
|
|
|
+ 'html': '<p>%s</p>' % ban.user_message,
|
|
|
+ 'plain': ban.user_message,
|
|
|
+ },
|
|
|
+ 'expires_on': None,
|
|
|
+ })
|
|
|
|
|
|
response = self.client.get('/api/auth/')
|
|
|
self.assertEqual(response.status_code, 200)
|
|
@@ -157,7 +175,7 @@ class GatewayTests(TestCase):
|
|
|
|
|
|
def test_login_inactive_admin(self):
|
|
|
"""login api fails to sign admin-activated user in"""
|
|
|
- UserModel.objects.create_user('Bob', 'bob@test.com', 'Pass.123', requires_activation=1)
|
|
|
+ UserModel.objects.create_user('Bob', 'bob@test.com', 'Pass.123', requires_activation=2)
|
|
|
|
|
|
response = self.client.post(
|
|
|
'/api/auth/',
|
|
@@ -167,9 +185,11 @@ class GatewayTests(TestCase):
|
|
|
},
|
|
|
)
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
-
|
|
|
- response_json = response.json()
|
|
|
- self.assertEqual(response_json['code'], 'inactive_user')
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'non_field_errors': [
|
|
|
+ "Your account has to be activated by Administrator before you will be able to sign in.",
|
|
|
+ ],
|
|
|
+ })
|
|
|
|
|
|
response = self.client.get('/api/auth/')
|
|
|
self.assertEqual(response.status_code, 200)
|
|
@@ -179,7 +199,7 @@ class GatewayTests(TestCase):
|
|
|
|
|
|
def test_login_inactive_user(self):
|
|
|
"""login api fails to sign user-activated user in"""
|
|
|
- UserModel.objects.create_user('Bob', 'bob@test.com', 'Pass.123', requires_activation=2)
|
|
|
+ UserModel.objects.create_user('Bob', 'bob@test.com', 'Pass.123', requires_activation=1)
|
|
|
|
|
|
response = self.client.post(
|
|
|
'/api/auth/',
|
|
@@ -189,9 +209,11 @@ class GatewayTests(TestCase):
|
|
|
},
|
|
|
)
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
-
|
|
|
- response_json = response.json()
|
|
|
- self.assertEqual(response_json['code'], 'inactive_admin')
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'non_field_errors': [
|
|
|
+ "You have to activate your account before you will be able to sign in.",
|
|
|
+ ],
|
|
|
+ })
|
|
|
|
|
|
response = self.client.get('/api/auth/')
|
|
|
self.assertEqual(response.status_code, 200)
|
|
@@ -222,10 +244,10 @@ class GatewayTests(TestCase):
|
|
|
self.assertIsNone(user_json['id'])
|
|
|
|
|
|
|
|
|
-class UserCredentialsTests(TestCase):
|
|
|
+class UserRequirementsTests(TestCase):
|
|
|
def test_edge_returns_response(self):
|
|
|
"""api edge has no showstoppers"""
|
|
|
- response = self.client.get('/api/auth/criteria/')
|
|
|
+ response = self.client.get('/api/auth/requirements/')
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
@@ -250,8 +272,8 @@ class SendActivationAPITests(TestCase):
|
|
|
self.assertIn('Activate Bob', mail.outbox[0].subject)
|
|
|
|
|
|
def test_submit_banned(self):
|
|
|
- """request activation link api passes for banned users"""
|
|
|
- Ban.objects.create(
|
|
|
+ """request activation link api errors for banned users"""
|
|
|
+ ban = Ban.objects.create(
|
|
|
check_type=Ban.USERNAME,
|
|
|
banned_value=self.user.username,
|
|
|
user_message='Nope!',
|
|
@@ -263,9 +285,16 @@ class SendActivationAPITests(TestCase):
|
|
|
'email': self.user.email,
|
|
|
},
|
|
|
)
|
|
|
- self.assertEqual(response.status_code, 200)
|
|
|
+ self.assertEqual(response.status_code, 403)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'detail': {
|
|
|
+ 'html': '<p>%s</p>' % ban.user_message,
|
|
|
+ 'plain': ban.user_message,
|
|
|
+ },
|
|
|
+ 'expires_on': None,
|
|
|
+ })
|
|
|
|
|
|
- self.assertIn('Activate Bob', mail.outbox[0].subject)
|
|
|
+ self.assertTrue(not mail.outbox)
|
|
|
|
|
|
def test_submit_disabled(self):
|
|
|
"""request activation link api fails disabled users"""
|
|
@@ -278,14 +307,15 @@ class SendActivationAPITests(TestCase):
|
|
|
'email': self.user.email,
|
|
|
},
|
|
|
)
|
|
|
- self.assertContains(response, 'not_found', status_code=400)
|
|
|
+ self.assertContains(response, "No user with this e-mail exists.", status_code=400)
|
|
|
|
|
|
self.assertTrue(not mail.outbox)
|
|
|
|
|
|
def test_submit_empty(self):
|
|
|
"""request activation link api errors for no body"""
|
|
|
response = self.client.post(self.link)
|
|
|
- self.assertContains(response, 'empty_email', status_code=400)
|
|
|
+ self.assertEqual(response.status_code, 400)
|
|
|
+ self.assertEqual(response.json(), {'email': ["This field is required."]})
|
|
|
|
|
|
self.assertTrue(not mail.outbox)
|
|
|
|
|
@@ -306,7 +336,7 @@ class SendActivationAPITests(TestCase):
|
|
|
'email': 'fake@mail.com',
|
|
|
},
|
|
|
)
|
|
|
- self.assertContains(response, 'not_found', status_code=400)
|
|
|
+ self.assertContains(response, "No user with this e-mail exists.", status_code=400)
|
|
|
|
|
|
self.assertTrue(not mail.outbox)
|
|
|
|
|
@@ -321,7 +351,7 @@ class SendActivationAPITests(TestCase):
|
|
|
'email': self.user.email,
|
|
|
},
|
|
|
)
|
|
|
- self.assertContains(response, 'Bob, your account is already active.', status_code=400)
|
|
|
+ self.assertContains(response, "Bob, your account is already active.", status_code=400)
|
|
|
|
|
|
def test_submit_inactive_user(self):
|
|
|
"""request activation link api errors for admin-activated users"""
|
|
@@ -334,7 +364,7 @@ class SendActivationAPITests(TestCase):
|
|
|
'email': self.user.email,
|
|
|
},
|
|
|
)
|
|
|
- self.assertContains(response, 'inactive_admin', status_code=400)
|
|
|
+ self.assertContains(response, "only administrator may activate your account", status_code=400)
|
|
|
|
|
|
self.assertTrue(not mail.outbox)
|
|
|
|
|
@@ -347,7 +377,10 @@ class SendActivationAPITests(TestCase):
|
|
|
'email': self.user.email,
|
|
|
}
|
|
|
)
|
|
|
- self.assertEqual(response.status_code, 200)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'username': self.user.username,
|
|
|
+ 'email': self.user.email,
|
|
|
+ })
|
|
|
|
|
|
self.assertTrue(mail.outbox)
|
|
|
|
|
@@ -371,8 +404,8 @@ class SendPasswordFormAPITests(TestCase):
|
|
|
self.assertIn('Change Bob password', mail.outbox[0].subject)
|
|
|
|
|
|
def test_submit_banned(self):
|
|
|
- """request change password form link api sends reset link mail"""
|
|
|
- Ban.objects.create(
|
|
|
+ """request change password form link api errors for banned users"""
|
|
|
+ ban = Ban.objects.create(
|
|
|
check_type=Ban.USERNAME,
|
|
|
banned_value=self.user.username,
|
|
|
user_message='Nope!',
|
|
@@ -384,9 +417,16 @@ class SendPasswordFormAPITests(TestCase):
|
|
|
'email': self.user.email,
|
|
|
},
|
|
|
)
|
|
|
- self.assertEqual(response.status_code, 200)
|
|
|
+ self.assertEqual(response.status_code, 403)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'detail': {
|
|
|
+ 'html': '<p>%s</p>' % ban.user_message,
|
|
|
+ 'plain': ban.user_message,
|
|
|
+ },
|
|
|
+ 'expires_on': None,
|
|
|
+ })
|
|
|
|
|
|
- self.assertIn('Change Bob password', mail.outbox[0].subject)
|
|
|
+ self.assertTrue(not mail.outbox)
|
|
|
|
|
|
def test_submit_disabled(self):
|
|
|
"""request change password form api fails disabled users"""
|
|
@@ -399,14 +439,15 @@ class SendPasswordFormAPITests(TestCase):
|
|
|
'email': self.user.email,
|
|
|
},
|
|
|
)
|
|
|
- self.assertContains(response, 'not_found', status_code=400)
|
|
|
+ self.assertContains(response, "No user with this e-mail exists.", status_code=400)
|
|
|
|
|
|
self.assertTrue(not mail.outbox)
|
|
|
|
|
|
def test_submit_empty(self):
|
|
|
"""request change password form link api errors for no body"""
|
|
|
response = self.client.post(self.link)
|
|
|
- self.assertContains(response, 'empty_email', status_code=400)
|
|
|
+ self.assertEqual(response.status_code, 400)
|
|
|
+ self.assertEqual(response.json(), {'email': ["This field is required."]})
|
|
|
|
|
|
self.assertTrue(not mail.outbox)
|
|
|
|
|
@@ -418,7 +459,7 @@ class SendPasswordFormAPITests(TestCase):
|
|
|
'email': 'fake@mail.com',
|
|
|
},
|
|
|
)
|
|
|
- self.assertContains(response, 'not_found', status_code=400)
|
|
|
+ self.assertContains(response, "No user with this e-mail exists.", status_code=400)
|
|
|
|
|
|
self.assertTrue(not mail.outbox)
|
|
|
|
|
@@ -442,7 +483,8 @@ class SendPasswordFormAPITests(TestCase):
|
|
|
'email': self.user.email,
|
|
|
},
|
|
|
)
|
|
|
- self.assertContains(response, 'inactive_user', status_code=400)
|
|
|
+ self.assertContains(response, "You have to activate your account", status_code=400)
|
|
|
+ self.assertTrue(not mail.outbox)
|
|
|
|
|
|
self.user.requires_activation = 2
|
|
|
self.user.save()
|
|
@@ -453,23 +495,22 @@ class SendPasswordFormAPITests(TestCase):
|
|
|
'email': self.user.email,
|
|
|
},
|
|
|
)
|
|
|
- self.assertContains(response, 'inactive_admin', status_code=400)
|
|
|
-
|
|
|
+ self.assertContains(response, "Administrator has to activate your account", status_code=400)
|
|
|
self.assertTrue(not mail.outbox)
|
|
|
|
|
|
|
|
|
class ChangePasswordAPITests(TestCase):
|
|
|
def setUp(self):
|
|
|
self.user = UserModel.objects.create_user('Bob', 'bob@test.com', 'Pass.123')
|
|
|
-
|
|
|
- self.link = '/api/auth/change-password/%s/%s/'
|
|
|
+ self.link = '/api/auth/change-password/%s/'
|
|
|
|
|
|
def test_submit_valid(self):
|
|
|
"""submit change password form api changes password"""
|
|
|
response = self.client.post(
|
|
|
- self.link % (self.user.pk, make_password_change_token(self.user)),
|
|
|
+ self.link % self.user.pk,
|
|
|
data={
|
|
|
'password': 'n3wp4ss!',
|
|
|
+ 'token': make_password_change_token(self.user),
|
|
|
},
|
|
|
)
|
|
|
self.assertEqual(response.status_code, 200)
|
|
@@ -480,9 +521,10 @@ class ChangePasswordAPITests(TestCase):
|
|
|
def test_submit_with_whitespaces(self):
|
|
|
"""submit change password form api changes password with whitespaces"""
|
|
|
response = self.client.post(
|
|
|
- self.link % (self.user.pk, make_password_change_token(self.user)),
|
|
|
+ self.link % self.user.pk,
|
|
|
data={
|
|
|
'password': ' n3wp4ss! ',
|
|
|
+ 'token': make_password_change_token(self.user),
|
|
|
},
|
|
|
)
|
|
|
self.assertEqual(response.status_code, 200)
|
|
@@ -493,30 +535,46 @@ class ChangePasswordAPITests(TestCase):
|
|
|
def test_submit_invalid_data(self):
|
|
|
"""login api errors for invalid data"""
|
|
|
response = self.client.post(
|
|
|
- self.link % (self.user.pk, make_password_change_token(self.user)),
|
|
|
+ self.link % self.user.pk,
|
|
|
'false',
|
|
|
content_type="application/json",
|
|
|
)
|
|
|
self.assertContains(response, "Invalid data.", status_code=400)
|
|
|
|
|
|
- def test_invalid_token_link(self):
|
|
|
+ def test_invalid_token(self):
|
|
|
"""api errors on invalid user id link"""
|
|
|
- response = self.client.post(self.link % (self.user.pk, 'asda7ad89sa7d9s789as'))
|
|
|
-
|
|
|
- self.assertContains(response, "Form link is invalid.", status_code=400)
|
|
|
+ response = self.client.post(
|
|
|
+ self.link % self.user.pk,
|
|
|
+ data={
|
|
|
+ 'password': 'n3wp4ss!',
|
|
|
+ 'token': 'invalid!',
|
|
|
+ },
|
|
|
+ )
|
|
|
+ self.assertContains(response, "Form link is invalid or expired.", status_code=400)
|
|
|
|
|
|
def test_banned_user_link(self):
|
|
|
"""request errors because user is banned"""
|
|
|
- Ban.objects.create(
|
|
|
+ ban = Ban.objects.create(
|
|
|
check_type=Ban.USERNAME,
|
|
|
banned_value=self.user.username,
|
|
|
user_message='Nope!',
|
|
|
)
|
|
|
|
|
|
response = self.client.post(
|
|
|
- self.link % (self.user.pk, make_password_change_token(self.user))
|
|
|
+ self.link % self.user.pk,
|
|
|
+ data={
|
|
|
+ 'password': 'n3wp4ss!',
|
|
|
+ 'token': make_password_change_token(self.user),
|
|
|
+ },
|
|
|
)
|
|
|
- self.assertContains(response, "Your link has expired.", status_code=400)
|
|
|
+ self.assertEqual(response.status_code, 403)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'detail': {
|
|
|
+ 'html': '<p>%s</p>' % ban.user_message,
|
|
|
+ 'plain': ban.user_message,
|
|
|
+ },
|
|
|
+ 'expires_on': None,
|
|
|
+ })
|
|
|
|
|
|
def test_inactive_user(self):
|
|
|
"""change password api errors for inactive users"""
|
|
@@ -524,31 +582,35 @@ class ChangePasswordAPITests(TestCase):
|
|
|
self.user.save()
|
|
|
|
|
|
response = self.client.post(
|
|
|
- self.link % (self.user.pk, make_password_change_token(self.user))
|
|
|
+ self.link % self.user.pk,
|
|
|
+ data={
|
|
|
+ 'password': 'n3wp4ss!',
|
|
|
+ 'token': make_password_change_token(self.user),
|
|
|
+ },
|
|
|
)
|
|
|
- self.assertContains(response, "Your link has expired.", status_code=400)
|
|
|
+ self.assertContains(response, "You have to activate your account", status_code=400)
|
|
|
|
|
|
self.user.requires_activation = 2
|
|
|
self.user.save()
|
|
|
|
|
|
response = self.client.post(
|
|
|
- self.link % (self.user.pk, make_password_change_token(self.user))
|
|
|
+ self.link % self.user.pk,
|
|
|
+ data={
|
|
|
+ 'password': 'n3wp4ss!',
|
|
|
+ 'token': make_password_change_token(self.user),
|
|
|
+ },
|
|
|
)
|
|
|
- self.assertContains(response, "Your link has expired.", status_code=400)
|
|
|
+ self.assertContains(response, "Administrator has to activate your account", status_code=400)
|
|
|
|
|
|
def test_disabled_user(self):
|
|
|
"""change password api errors for disabled users"""
|
|
|
self.user.is_active = False
|
|
|
self.user.save()
|
|
|
|
|
|
- response = self.client.post(
|
|
|
- self.link % (self.user.pk, make_password_change_token(self.user))
|
|
|
- )
|
|
|
- self.assertContains(response, "Form link is invalid.", status_code=400)
|
|
|
+ response = self.client.post(self.link % self.user.pk)
|
|
|
+ self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
def test_submit_empty(self):
|
|
|
"""change password api errors for empty body"""
|
|
|
- response = self.client.post(
|
|
|
- self.link % (self.user.pk, make_password_change_token(self.user))
|
|
|
- )
|
|
|
- self.assertContains(response, "This password is too shor", status_code=400)
|
|
|
+ response = self.client.post(self.link % self.user.pk)
|
|
|
+ self.assertContains(response, "This field is required.", status_code=400)
|