Fakultas Ilmu Komputer UI

Commit 68e3ce5f authored by Jonathan Christopher Jakub's avatar Jonathan Christopher Jakub
Browse files

PBI-8: Bugfix - Implement password validator and standardize all username to lowercase

parent 4a72c2de
from django.contrib.auth import password_validation
from rest_framework import serializers
from apps.accounts.models import Account
......@@ -41,3 +43,7 @@ class AccountRegisterSerializer(serializers.ModelSerializer):
"area",
"is_admin",
]
def validate_password(self, value):
password_validation.validate_password(value)
return value
......@@ -150,19 +150,37 @@ class AccountViewTest(APITestCase):
'"action_type":"{}"'.format(ACTIVITY_TYPE_CREATE), response_string
)
def test_create_new_account_fails_with_no_auth_info(self):
def test_username_lowercased(self):
url = self.BASE_URL
data = {
"name": self.faker.name(),
"username": "aBcDeFgH",
"password": "justpass",
"email": self.faker.email(),
"phone_number": self.faker.phone_number(),
"area": self.faker.city(),
"is_admin": False,
}
self.client.post(
path=url, data=data, format="json",
)
self.assertTrue(Account.objects.filter(user__username="abcdefgh").exists())
def test_create_new_account_fails_with_poor_password(self):
url = self.BASE_URL
_account_id = self.faker.email()
data = {
"name": self.faker.name(),
"email": _account_id,
"username": _account_id,
"password": "12345678",
"phone_number": self.faker.phone_number(),
"area": self.faker.city(),
"is_admin": False,
}
response = self.client.post(path=url, data=data, format="json",)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
......
......@@ -51,7 +51,7 @@ class AccountViewSet(viewsets.ViewSet):
serializer = AccountRegisterSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
username = serializer.validated_data.pop("username")
username = serializer.validated_data.pop("username").lower()
password = serializer.validated_data.pop("password")
user = User.objects.create_user(username=username, password=password)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment