-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Baylee Feore
committed
Mar 23, 2016
1 parent
5496cdb
commit e814f88
Showing
4 changed files
with
31 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,13 @@ | |
User = get_user_model() | ||
|
||
|
||
def encode_string(str): | ||
""" | ||
Encode a unicode string as base 64 bytes, then decode back to unicode for use as a string | ||
""" | ||
return base64.encodebytes(bytes(str, 'utf8')).decode() | ||
|
||
|
||
class UserTests(SchemaTestCase): | ||
def setUp(self): | ||
super(UserTests, self).setUp() | ||
|
@@ -63,7 +70,7 @@ def test_get_logged_in_user(self): | |
|
||
def test_user_can_sign_up(self): | ||
url = reverse("sign_up") | ||
password = base64.encodebytes(b"testtest").decode() | ||
password = encode_string("testtest") | ||
data = { | ||
"fullname": "tester", | ||
"username": "tester", | ||
|
@@ -78,7 +85,7 @@ def test_user_can_sign_up(self): | |
|
||
def test_password_min_length(self): | ||
url = reverse("sign_up") | ||
password = base64.encodebytes(b"test").decode() | ||
password = encode_string("test") | ||
data = { | ||
"fullname": "tester2", | ||
"username": "tester2", | ||
|
@@ -92,14 +99,14 @@ def test_user_can_log_in(self): | |
url = reverse("login") | ||
|
||
# With the correct username and password, a user can log in with basic auth | ||
auth_string = base64.encodebytes(b"tester1:password").decode() | ||
auth_string = encode_string("tester1:password") | ||
self.client.credentials(HTTP_AUTHORIZATION='Basic ' + auth_string) | ||
response = self.client.get(url) | ||
self.assertValidJSONResponse(response) | ||
self.check_response_data(response, "$loginResponse") | ||
|
||
# Incorrect credentials return unauthorized | ||
auth_string = base64.encodebytes(b"tester1:WRONGPASSWORD").decode() | ||
auth_string = encode_string("tester1:WRONGPASSWORD") | ||
self.client.credentials(HTTP_AUTHORIZATION='Basic ' + auth_string) | ||
response = self.client.get(url) | ||
self.assertHttpUnauthorized(response) | ||
|
@@ -132,15 +139,15 @@ def test_inexact_signup(self): | |
data = { | ||
'username': 'useD', | ||
'email': '[email protected]', | ||
'password': base64.encodebytes(b"password").decode() | ||
'password': encode_string("password") | ||
} | ||
response = self.client.post(url, data, format="json") | ||
self.assertHttpBadRequest(response) | ||
|
||
data = { | ||
'username': "new_username", | ||
'email': '[email protected]', | ||
'password': base64.encodebytes(b"password").decode() | ||
'password': encode_string("password") | ||
} | ||
response = self.client.post(url, data, format="json") | ||
self.assertHttpBadRequest(response) | ||
|
@@ -149,7 +156,7 @@ def test_inexact_login(self): | |
url = reverse("login") | ||
|
||
# username is case-insensitive for login | ||
auth_string = base64.encodebytes(b"Tester1:password").decode() | ||
auth_string = encode_string("Tester1:password") | ||
self.client.credentials(HTTP_AUTHORIZATION='Basic ' + auth_string) | ||
response = self.client.get(url) | ||
self.assertValidJSONResponse(response) | ||
|
@@ -218,29 +225,29 @@ def test_user_can_change_password(self): | |
url = reverse("password_change") | ||
|
||
data = { | ||
"old_password": base64.encodebytes(b"password").decode(), | ||
"password": base64.encodebytes(b"felicia").decode(), | ||
"confirm_password": base64.encodebytes(b"felicia").decode() | ||
"old_password": encode_string("password"), | ||
"password": encode_string("felicia"), | ||
"confirm_password": encode_string("felicia") | ||
} | ||
# Unauthenticated user can't change password | ||
self.assertSchemaPatch(url, "$changePasswordRequest", "$changePasswordResponse", data, None, unauthorized=True) | ||
self.assertFalse(User.objects.get(pk=felicia.pk).check_password("felicia")) | ||
|
||
# User can't change password if the old / current password is incorrect | ||
bad_data = { | ||
"old_password": base64.encodebytes(b"wrong_password").decode(), | ||
"password": base64.encodebytes(b"felicia").decode(), | ||
"confirm_password": base64.encodebytes(b"felicia").decode() | ||
"old_password": encode_string("wrong_password"), | ||
"password": encode_string("felicia"), | ||
"confirm_password": encode_string("felicia") | ||
} | ||
self.assertSchemaPatch(url, "$changePasswordRequest", "$changePasswordResponse", bad_data, felicia, | ||
unauthorized=True) | ||
self.assertFalse(User.objects.get(pk=felicia.pk).check_password("felicia")) | ||
|
||
# User can't change password if the two new passwords don't match | ||
mismatch_password_data = { | ||
"old_password": base64.encodebytes(b"password").decode(), | ||
"password": base64.encodebytes(b"felicia").decode(), | ||
"confirm_password": base64.encodebytes(b"FELICIA").decode() | ||
"old_password": encode_string("password"), | ||
"password": encode_string("felicia"), | ||
"confirm_password": encode_string("FELICIA") | ||
} | ||
self.add_credentials(felicia) | ||
response = self.client.patch(url, mismatch_password_data, format='json') | ||
|
@@ -269,8 +276,8 @@ def test_user_can_reset_password(self): | |
mismatch_password_data = { | ||
"uid": urlsafe_base64_encode(force_bytes(beverly.pk)).decode(), | ||
"token": default_token_generator.make_token(beverly), | ||
"password": base64.encodebytes(b"wesley").decode(), | ||
"confirm_password": base64.encodebytes(b"WESLEY").decode() | ||
"password": encode_string("wesley"), | ||
"confirm_password": encode_string("WESLEY") | ||
} | ||
response = self.client.post(url, mismatch_password_data, format='json') | ||
self.assertEqual(response.status_code, 400) | ||
|
@@ -279,8 +286,8 @@ def test_user_can_reset_password(self): | |
bad_uid_data = { | ||
"uid": urlsafe_base64_encode(force_bytes(UserFactory().pk)).decode(), | ||
"token": default_token_generator.make_token(beverly), | ||
"password": base64.encodebytes(b"wesley").decode(), | ||
"confirm_password": base64.encodebytes(b"wesley").decode() | ||
"password": encode_string("wesley"), | ||
"confirm_password": encode_string("wesley") | ||
} | ||
response = self.client.post(url, bad_uid_data, format='json') | ||
self.assertEqual(response.status_code, 400) | ||
|
@@ -289,8 +296,8 @@ def test_user_can_reset_password(self): | |
good_data = { | ||
"uid": urlsafe_base64_encode(force_bytes(beverly.pk)).decode(), | ||
"token": default_token_generator.make_token(beverly), | ||
"password": base64.encodebytes(b"wesley").decode(), | ||
"confirm_password": base64.encodebytes(b"wesley").decode() | ||
"password": encode_string("wesley"), | ||
"confirm_password": encode_string("wesley") | ||
} | ||
self.assertSchemaPost(url, "$setPasswordRequest", "$userResponse", good_data, None, status_OK=True) | ||
self.assertTrue(User.objects.get(username='beverly').check_password('wesley')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters