-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: adds tests and fixes issues raised by them (#83)
* fix: passwords are now properly validated so they are not too similar to other account attributes. * refactor: modifies VerifyAccountView to satisfy the proper separation of concerns. Serializer validation is just for to make sure entry data is in the expected format. * chore: add tests for some endpoints * chore: remove unused ignore comments for mypy * chore: just for Eugene, you fuck. Here, it is under your comment now * chore: add first party modules to isort config * chore: add the dumb linter changes * chore: review suggestions
- Loading branch information
Showing
15 changed files
with
407 additions
and
27 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
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from django.contrib.auth.password_validation import UserAttributeSimilarityValidator | ||
from django.core.exceptions import ValidationError | ||
from django.db.models import Model | ||
|
||
from accounts.models import Account | ||
|
||
|
||
class CustomUserAttributeSimilarityValidator(UserAttributeSimilarityValidator): | ||
def validate(self, password: str, user: Model | None = None): | ||
if user is None: | ||
return | ||
|
||
if not isinstance(user, Account): | ||
raise Exception( | ||
f"User is not an instance of Account: {user}. Check if the right user model is being set at settings.py." | ||
) | ||
|
||
custom_attributes = [ | ||
user.username, | ||
user.email, | ||
user.unique_identifier, | ||
] | ||
|
||
for attribute in custom_attributes: | ||
if not attribute or not password: | ||
continue | ||
|
||
password = password.lower() | ||
attribute = attribute.lower() | ||
|
||
if password in attribute or attribute in password: | ||
raise ValidationError( | ||
"The password is too similar to the %(verbose_name)s.", | ||
code="password_too_similar", | ||
params={"verbose_name": attribute}, | ||
) |
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
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
Empty file.
Empty file.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
from accounts.models import Account | ||
|
||
|
||
class LoginCredentialsTest(APITestCase): | ||
def setUp(self): | ||
self.url = reverse("account:login-credentials") | ||
self.valid_account = Account.objects.create_user( | ||
username="validUser", | ||
email="[email protected]", | ||
unique_identifier="validUser", | ||
) | ||
|
||
self.valid_account.set_password("aValidPss963") | ||
self.valid_account.is_confirmed = True | ||
self.valid_account.save() | ||
self.valid_login_data = { | ||
"email": "[email protected]", | ||
"password": "aValidPss963", | ||
} | ||
|
||
def test_login_with_valid_credentials(self): | ||
response = self.client.post(self.url, self.valid_login_data, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_login_with_invalid_credentials(self): | ||
data = { | ||
"email": "[email protected]", | ||
"password": "wrongPassword", | ||
} | ||
response = self.client.post(self.url, data, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) | ||
|
||
def test_login_with_unconfirmed_account(self): | ||
self.valid_account.is_confirmed = False | ||
self.valid_account.save() | ||
|
||
response = self.client.post(self.url, self.valid_login_data, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) | ||
|
||
def test_login_with_inactive_account(self): | ||
self.valid_account.is_active = False | ||
self.valid_account.save() | ||
|
||
response = self.client.post(self.url, self.valid_login_data, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) | ||
|
||
def test_login_with_invalid_email(self): | ||
data = { | ||
"email": "invalidEmail", | ||
"password": "aValidPss963", | ||
} | ||
response = self.client.post(self.url, data, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
from accounts.models import Account | ||
|
||
|
||
class LoginTokenTest(APITestCase): | ||
def setUp(self): | ||
self.url = reverse("account:login-token") | ||
self.valid_account = Account.objects.create_user( | ||
username="validUser", | ||
email="[email protected]", | ||
unique_identifier="validUser", | ||
) | ||
|
||
self.valid_account.set_password("aValidPss963") | ||
self.valid_account.is_confirmed = True | ||
self.valid_account.save() | ||
self.valid_login_data = { | ||
"email": "[email protected]", | ||
"password": "aValidPss963", | ||
} | ||
|
||
# needs to log in with credentials to get the token | ||
response = self.client.post(reverse("account:login-credentials"), self.valid_login_data, format="json") | ||
self.token = response.data["token"] | ||
|
||
def test_login_with_valid_token(self): | ||
self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.token}") | ||
response = self.client.post(self.url, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_login_with_invalid_token(self): | ||
self.client.credentials(HTTP_AUTHORIZATION="Token invalidToken") | ||
response = self.client.post(self.url, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) | ||
|
||
def test_login_without_token(self): | ||
response = self.client.post(self.url, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
Oops, something went wrong.