|
@@ -29,6 +29,11 @@ class UserCreateTests(UserTestCase):
|
|
|
"""empty request errors with code 400"""
|
|
|
response = self.client.post(self.api_link)
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'username': ['This field is required.'],
|
|
|
+ 'email': ['This field is required.'],
|
|
|
+ 'password': ['This field is required.'],
|
|
|
+ })
|
|
|
|
|
|
def test_invalid_data(self):
|
|
|
"""invalid request data errors with code 400"""
|
|
@@ -38,19 +43,30 @@ class UserCreateTests(UserTestCase):
|
|
|
content_type="application/json",
|
|
|
)
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'username': ['This field is required.'],
|
|
|
+ 'email': ['This field is required.'],
|
|
|
+ 'password': ['This field is required.'],
|
|
|
+ })
|
|
|
|
|
|
def test_authenticated_request(self):
|
|
|
"""authentiated user request errors with code 403"""
|
|
|
self.login_user(self.get_authenticated_user())
|
|
|
response = self.client.post(self.api_link)
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ "detail": "This action is not available to signed in users."
|
|
|
+ })
|
|
|
|
|
|
def test_registration_off_request(self):
|
|
|
"""registrations off request errors with code 403"""
|
|
|
settings.override_setting('account_activation', 'closed')
|
|
|
|
|
|
response = self.client.post(self.api_link)
|
|
|
- self.assertContains(response, 'closed', status_code=403)
|
|
|
+ self.assertEqual(response.status_code, 403)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ "detail": "New users registrations are currently closed."
|
|
|
+ })
|
|
|
|
|
|
def test_registration_validates_ip_ban(self):
|
|
|
"""api validates ip ban"""
|
|
@@ -234,8 +250,10 @@ class UserCreateTests(UserTestCase):
|
|
|
'password': '',
|
|
|
},
|
|
|
)
|
|
|
-
|
|
|
- self.assertContains(response, "This field is required", status_code=400)
|
|
|
+ self.assertEqual(response.status_code, 400)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ "password": ["This field is required."],
|
|
|
+ })
|
|
|
|
|
|
def test_registration_validates_password(self):
|
|
|
"""api uses django's validate_password to validate registrations"""
|
|
@@ -247,10 +265,14 @@ class UserCreateTests(UserTestCase):
|
|
|
'password': '123',
|
|
|
},
|
|
|
)
|
|
|
-
|
|
|
- self.assertContains(response, "password is too short", status_code=400)
|
|
|
- self.assertContains(response, "password is entirely numeric", status_code=400)
|
|
|
- self.assertContains(response, "email is not allowed", status_code=400)
|
|
|
+ self.assertEqual(response.status_code, 400)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ "email": ["This email is not allowed."],
|
|
|
+ "password": [
|
|
|
+ "This password is too short. It must contain at least 7 characters.",
|
|
|
+ "This password is entirely numeric.",
|
|
|
+ ],
|
|
|
+ })
|
|
|
|
|
|
def test_registration_validates_password_similiarity(self):
|
|
|
"""api uses validate_password to validate registrations"""
|
|
@@ -262,8 +284,11 @@ class UserCreateTests(UserTestCase):
|
|
|
'password': 'BobBoberson',
|
|
|
},
|
|
|
)
|
|
|
-
|
|
|
- self.assertContains(response, "password is too similar to the username", status_code=400)
|
|
|
+ self.assertEqual(response.status_code, 400)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ "email": ["This email is not allowed."],
|
|
|
+ "password": ["The password is too similar to the username."],
|
|
|
+ })
|
|
|
|
|
|
@override_settings(captcha_type='qa', qa_question='Test', qa_answers='Lorem\nIpsum')
|
|
|
def test_registration_validates_captcha(self):
|
|
@@ -391,8 +416,11 @@ class UserCreateTests(UserTestCase):
|
|
|
'password': 'pas123',
|
|
|
},
|
|
|
)
|
|
|
-
|
|
|
- self.assertContains(response, "email is not allowed", status_code=400)
|
|
|
+ self.assertEqual(response.status_code, 400)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ "email": ["This email is not allowed."],
|
|
|
+ "password": ["This password is too short. It must contain at least 7 characters."],
|
|
|
+ })
|
|
|
|
|
|
def test_registration_creates_active_user(self):
|
|
|
"""api creates active and signed in user on POST"""
|
|
@@ -406,10 +434,12 @@ class UserCreateTests(UserTestCase):
|
|
|
'password': 'pass123',
|
|
|
},
|
|
|
)
|
|
|
-
|
|
|
- self.assertContains(response, 'active')
|
|
|
- self.assertContains(response, 'Bob')
|
|
|
- self.assertContains(response, 'bob@bob.com')
|
|
|
+ self.assertEqual(response.status_code, 200)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'activation': 'active',
|
|
|
+ 'username': 'Bob',
|
|
|
+ 'email': 'bob@bob.com',
|
|
|
+ })
|
|
|
|
|
|
UserModel.objects.get_by_username('Bob')
|
|
|
|
|
@@ -438,10 +468,12 @@ class UserCreateTests(UserTestCase):
|
|
|
'password': 'pass123',
|
|
|
},
|
|
|
)
|
|
|
-
|
|
|
- self.assertContains(response, 'user')
|
|
|
- self.assertContains(response, 'Bob')
|
|
|
- self.assertContains(response, 'bob@bob.com')
|
|
|
+ self.assertEqual(response.status_code, 200)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'activation': 'user',
|
|
|
+ 'username': 'Bob',
|
|
|
+ 'email': 'bob@bob.com',
|
|
|
+ })
|
|
|
|
|
|
auth_json = self.client.get(reverse('misago:api:auth')).json()
|
|
|
self.assertFalse(auth_json['is_authenticated'])
|
|
@@ -463,10 +495,12 @@ class UserCreateTests(UserTestCase):
|
|
|
'password': 'pass123',
|
|
|
},
|
|
|
)
|
|
|
-
|
|
|
- self.assertContains(response, 'admin')
|
|
|
- self.assertContains(response, 'Bob')
|
|
|
- self.assertContains(response, 'bob@bob.com')
|
|
|
+ self.assertEqual(response.status_code, 200)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'activation': 'admin',
|
|
|
+ 'username': 'Bob',
|
|
|
+ 'email': 'bob@bob.com',
|
|
|
+ })
|
|
|
|
|
|
auth_json = self.client.get(reverse('misago:api:auth')).json()
|
|
|
self.assertFalse(auth_json['is_authenticated'])
|
|
@@ -488,16 +522,17 @@ class UserCreateTests(UserTestCase):
|
|
|
'password': ' pass123 ',
|
|
|
},
|
|
|
)
|
|
|
-
|
|
|
- self.assertContains(response, 'active')
|
|
|
- self.assertContains(response, 'Bob')
|
|
|
- self.assertContains(response, 'bob@bob.com')
|
|
|
+ self.assertEqual(response.status_code, 200)
|
|
|
+ self.assertEqual(response.json(), {
|
|
|
+ 'activation': 'active',
|
|
|
+ 'username': 'Bob',
|
|
|
+ 'email': 'bob@bob.com',
|
|
|
+ })
|
|
|
|
|
|
UserModel.objects.get_by_username('Bob')
|
|
|
|
|
|
test_user = UserModel.objects.get_by_email('bob@bob.com')
|
|
|
self.assertEqual(Online.objects.filter(user=test_user).count(), 1)
|
|
|
-
|
|
|
self.assertTrue(test_user.check_password(' pass123 '))
|
|
|
|
|
|
self.assertIn('Welcome', mail.outbox[0].subject)
|