Fakultas Ilmu Komputer UI

Commit 684020df authored by Jonathan Christopher Jakub's avatar Jonathan Christopher Jakub
Browse files

Merge branch 'bugfix/login-force-lowercase' into 'staging'

Lowercase username on login

See merge request !53
parents d3b86dda 853d4dbb
Pipeline #48255 failed with stages
in 18 seconds
from django.apps import AppConfig
class AuthConfig(AppConfig):
name = 'custom_auth'
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase
from django.contrib.auth.models import User
class CustomAuthTest(APITestCase):
@classmethod
def setUpTestData(cls):
cls.URL = "/auth/token/"
user = User.objects.create(username="jonathan")
user.set_password("justpass")
user.save()
cls.user = user
def _test_success(self, data):
response = self.client.post(path=self.URL, data=data, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(Token.objects.filter(user=self.user).exists())
def _test_fails(self, data):
response = self.client.post(path=self.URL, data=data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_login_success_with_case_insensitive_username(self):
data = {
"username": "JoNaThAn",
"password": "justpass",
}
self._test_success(data)
def test_login_success_with_exact_username(self):
data = {
"username": "jonathan",
"password": "justpass",
}
self._test_success(data)
def test_login_fails_with_incomplete_credentials(self):
data = {
"username": "JoNaThAn",
}
self._test_fails(data)
def test_login_fails_with_wrong_credentials(self):
data = {
"username": "jonathanjojo",
"password": "cantpass",
}
self._test_fails(data)
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from rest_framework import status
class CustomAuthToken(ObtainAuthToken):
def post(self, request, *args, **kwargs):
request_data = request.data
request_data["username"] = request_data["username"].lower()
serializer = self.serializer_class(
data=request_data,
context={"request": request}
)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data["user"]
token, _ = Token.objects.get_or_create(user=user)
return Response(data={ "token": token.key }, status=status.HTTP_200_OK)
......@@ -54,6 +54,7 @@ INSTALLED_APPS = [
"apps.cases",
"apps.exportables",
"apps.logs",
"apps.custom_auth",
]
MIDDLEWARE = [
......
from django.contrib import admin
from django.urls import path, include
from rest_framework.authtoken.views import obtain_auth_token
from apps.custom_auth.views import CustomAuthToken
urlpatterns = [
path("admin/", admin.site.urls),
path("accounts/", include("apps.accounts.urls")),
path("cases/", include("apps.cases.urls")),
path("logs/", include("apps.logs.urls")),
path("auth/token/", obtain_auth_token, name="api_token_auth"),
path("auth/token/", CustomAuthToken.as_view(), name="api_token_auth"),
path("auth/", include("django.contrib.auth.urls")),
path("exportables/", include("apps.exportables.urls")),
]
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