refactor(be-auth): rename auth module to identity to avoid collision with django.contrib.auth

This commit is contained in:
Marc 2024-01-02 00:34:23 -05:00
parent cd9499a66b
commit db46e80bd7
Signed by: marc
GPG key ID: 048E042F22B5DC79
13 changed files with 36 additions and 30 deletions

View file

@ -1,9 +0,0 @@
import django.urls
import auth.views
urlpatterns = [
django.urls.path(
"session/", auth.views.SessionListView.as_view(), name="auth-session-list"
),
django.urls.path("user/", auth.views.UserListView.as_view(), name="auth-user-list"),
]

View file

@ -31,6 +31,7 @@ INSTALLED_APPS = [
"rest_framework",
"corsheaders",
"files",
"identity",
]
MIDDLEWARE = [
@ -39,7 +40,7 @@ MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"auth.middleware.JwtMiddleware",
"identity.middleware.JwtMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
@ -47,8 +48,8 @@ MIDDLEWARE = [
ROOT_URLCONF = "base.urls"
CORS_ALLOWED_ORIGINS = ["http://localhost:1234"]
CSRF_TRUSTED_ORIGINS = ["http://localhost:1234"]
CORS_ALLOWED_ORIGINS = ["https://localhost:1234"]
CSRF_TRUSTED_ORIGINS = ["https://localhost:1234"]
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",

View file

@ -18,10 +18,10 @@ from django.contrib import admin
import django.urls as django_urls
import files.urls as files_urls
import auth.urls
import identity.urls
urlpatterns = [
django_urls.path("admin/", admin.site.urls),
django_urls.path("", django_urls.include(files_urls.urlpatterns)),
django_urls.path("auth/", django_urls.include(auth.urls.urlpatterns)),
django_urls.path("auth/", django_urls.include(identity.urls.urlpatterns)),
]

View file

@ -3,4 +3,4 @@ from django.apps import AppConfig
class AuthConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "auth"
name = "identity"

View file

@ -2,17 +2,17 @@ import pytest
import freezegun
import jwt
import auth.jwt
import identity.jwt
@freezegun.freeze_time("2012-01-01")
def test_generates_and_decodes_token_token():
MOCK_USER_ID = 1
token = auth.jwt.generate_token_for_user(MOCK_USER_ID)
token = identity.jwt.generate_token_for_user(MOCK_USER_ID)
assert token is not None
token_data = auth.jwt.decode_token(token)
token_data = identity.jwt.decode_token(token)
assert token_data["user_id"] == MOCK_USER_ID
@ -21,9 +21,9 @@ def test_token_decode_fails_if_expired():
MOCK_USER_ID = 1
with freezegun.freeze_time("2012-01-01"):
token = auth.jwt.generate_token_for_user(MOCK_USER_ID)
token = identity.jwt.generate_token_for_user(MOCK_USER_ID)
assert token is not None
with pytest.raises(jwt.ExpiredSignatureError):
auth.jwt.decode_token(token)
identity.jwt.decode_token(token)

View file

@ -3,7 +3,7 @@ import logging
import django.http
import django.contrib.auth
import auth.jwt
import identity.jwt
logger = logging.getLogger(__name__)
@ -34,7 +34,7 @@ class JwtMiddleware:
if jwt_cookie is not None:
try:
decoded_token = auth.jwt.decode_token(jwt_cookie)
decoded_token = identity.jwt.decode_token(jwt_cookie)
logger.info("Token: %s\nDecoded token: %s", jwt_cookie, decoded_token)

View file

@ -1,8 +1,8 @@
import pytest
import django.http
import django.contrib.auth
import auth.middleware
import auth.jwt
import identity.middleware
import identity.jwt
AuthUser = django.contrib.auth.get_user_model()
@ -18,7 +18,7 @@ def fixture_jwt_middleware():
def _noop(_: django.http.HttpRequest):
return django.http.HttpResponse()
return auth.middleware.JwtMiddleware(_noop)
return identity.middleware.JwtMiddleware(_noop)
def test_middleware_does_not_append_user_details_to_request_if_invalid_credentials(
@ -40,7 +40,7 @@ def test_middleware_adds_user_to_request_in_if_valid_token(
"""If authorization headers are present and contain a valid JWT, sets user on request."""
mock_request = HttpRequestWithUser()
test_user = AuthUser.objects.get(username=test_user_credentials["username"])
token = auth.jwt.generate_token_for_user(test_user.id)
token = identity.jwt.generate_token_for_user(test_user.id)
mock_request.COOKIES["jwt"] = token
jwt_middleware(mock_request)

View file

@ -0,0 +1,11 @@
import django.urls
import identity.views
urlpatterns = [
django.urls.path(
"session/", identity.views.SessionListView.as_view(), name="auth-session-list"
),
django.urls.path(
"user/", identity.views.UserListView.as_view(), name="auth-user-list"
),
]

View file

@ -1,4 +1,4 @@
import auth.jwt
import identity.jwt
import pytest
@ -69,6 +69,6 @@ def test_user_login_returns_valid_token_on_success(create_user_request, login_re
assert "jwt" in login_response.cookies
decoded_token = auth.jwt.decode_token(login_response.cookies["jwt"].value)
decoded_token = identity.jwt.decode_token(login_response.cookies["jwt"].value)
assert decoded_token["user_id"] == create_user_data["id"]

View file

@ -1,11 +1,12 @@
import logging
import uuid
import django.http
import django.contrib.auth
import rest_framework.views
import rest_framework.status
import auth.jwt
import identity.jwt
AuthUser = django.contrib.auth.get_user_model()
@ -45,7 +46,8 @@ class SessionListView(rest_framework.views.APIView):
if user is not None:
django.contrib.auth.login(request, user)
token = auth.jwt.generate_token_for_user(user_id=user.id)
token = identity.jwt.generate_token_for_user(user_id=user.id)
response = django.http.HttpResponse(status=201)
response.set_cookie(

View file

@ -24,4 +24,5 @@ docker run \
--name $APP_CONTAINER_NAME \
--env-file ../backend.env \
--network rotini-local \
-v ./rotini:/app/rotini \
rotini:dev