Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Baylee Feore committed Mar 23, 2016
1 parent 5496cdb commit e814f88
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
7 changes: 0 additions & 7 deletions test_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,6 @@

CACHE_MIDDLEWARE_KEY_PREFIX = "YAK"

# CACHES = {
# "default": {
# "BACKEND": "django.core.cache.backends.dummy.DummyCache",
# "LOCATION": "127.0.0.1:11211",
# }
# }


##################
# LOCAL SETTINGS #
Expand Down
51 changes: 29 additions & 22 deletions test_project/test_app/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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'))
2 changes: 1 addition & 1 deletion yak/rest_notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def message(self, location):

configured_template_name = "{}.html".format(self.notification_type.slug)
template_name = self.template_override if self.template_override else configured_template_name
return str(render_to_string("notifications/{}/{}".format(location, template_name), data))
return render_to_string("notifications/{}/{}".format(location, template_name), data)

def email_message(self):
return self.message(Notification.EMAIL)
Expand Down
2 changes: 1 addition & 1 deletion yak/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class YAKAPISettings(APISettings):
"""

def __getattr__(self, attr):
if attr not in list(self.defaults.keys()):
if attr not in self.defaults:
raise AttributeError("Invalid API setting: '%s'" % attr)

try:
Expand Down

0 comments on commit e814f88

Please sign in to comment.