From 524e059b2f12f5ef2589ff3b37fa6318bbae150b Mon Sep 17 00:00:00 2001 From: Matias Vallejos Date: Tue, 28 May 2024 18:38:12 -0300 Subject: [PATCH] Update requirements.txt. Add github support for social authentication --- auth_api/urls.py | 3 ++- auth_api/views.py | 13 ++++++++---- requirements.dev.txt | 8 ++++---- requirements.txt | 4 +++- setup.cfg | 3 ++- tests/todo_api/test_list_api.py | 14 +++++++++++-- todo_api/views.py | 7 +++++++ todo_project/settings.py | 36 +++++++++++++++++++++------------ todo_project/urls.py | 3 ++- 9 files changed, 64 insertions(+), 27 deletions(-) diff --git a/auth_api/urls.py b/auth_api/urls.py index e95cb70..497e463 100644 --- a/auth_api/urls.py +++ b/auth_api/urls.py @@ -1,8 +1,9 @@ from django.urls import path -from .views import GoogleLoginView, ConnectionView +from .views import GoogleLoginView, ConnectionView, GitHubLoginView app_name = "auth_api" urlpatterns = [ path("verify/", ConnectionView.as_view(), name="connection"), + path("social/login/github/", GitHubLoginView.as_view(), name="login-github"), path("social/login/google/", GoogleLoginView.as_view(), name="login-google"), ] diff --git a/auth_api/views.py b/auth_api/views.py index b932531..9310add 100644 --- a/auth_api/views.py +++ b/auth_api/views.py @@ -4,22 +4,27 @@ import os from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter +from allauth.socialaccount.providers.github.views import GitHubOAuth2Adapter from allauth.socialaccount.providers.oauth2.client import OAuth2Client from dj_rest_auth.registration.views import SocialLoginView from rest_framework.response import Response -class GoogleLoginView(SocialLoginView): # Authorization Code grant +class GoogleLoginView(SocialLoginView): adapter_class = GoogleOAuth2Adapter callback_url = f"{os.environ.get('BASE_URL', 'http://localhost:3000')}/api/auth/callback/google" client_class = OAuth2Client +class GitHubLoginView(SocialLoginView): + adapter_class = GitHubOAuth2Adapter + callback_url = f"{os.environ.get('BASE_URL', 'http://localhost:3000')}/api/auth/callback/github" + client_class = OAuth2Client + + class ConnectionView(APIView): permission_classes = (permissions.IsAuthenticated,) def post(self, request, *args, **kwargs): - return Response({ - "message": "Connection successfully!" - }) + return Response({"message": "Connection successfully!"}) diff --git a/requirements.dev.txt b/requirements.dev.txt index 0592324..8181312 100644 --- a/requirements.dev.txt +++ b/requirements.dev.txt @@ -1,4 +1,4 @@ -flake8 -pytest -pytest-django -model_bakery \ No newline at end of file +flake8==7.0.0 +pytest==8.2.1 +pytest-django==4.8.0 +model_bakery==1.0.0 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 6b5c099..998cd24 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,4 +22,6 @@ PyJWT==2.7.0 dj-database-url==1.0.0 whitenoise==6.2.0 -gunicorn==20.1.0 \ No newline at end of file +gunicorn==20.1.0 + +groq=0.8.0 \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 98a56d3..ad54197 100644 --- a/setup.cfg +++ b/setup.cfg @@ -7,4 +7,5 @@ filterwarnings = ignore::UserWarning markers = unit: unit tests - e2e: end-to-end tests \ No newline at end of file + e2e: end-to-end tests + ai: artificial intelligence tests \ No newline at end of file diff --git a/tests/todo_api/test_list_api.py b/tests/todo_api/test_list_api.py index f550859..8576d9f 100644 --- a/tests/todo_api/test_list_api.py +++ b/tests/todo_api/test_list_api.py @@ -55,10 +55,20 @@ def test_retrieve_list_limited_to_user(api_client_with_credentials, user2): assert res.status_code == status.HTTP_404_NOT_FOUND -def test_retrieve_inbox_list_not_found(api_client_with_credentials, user): +def test_retrieve_list_inbox(api_client_with_credentials, user): baker.make(TaskList, created_by=user, name="inbox") res = api_client_with_credentials.get(tasks_detail_url("inbox")) - assert res.status_code == status.HTTP_404_NOT_FOUND + assert res.status_code == status.HTTP_200_OK + assert res.data["list_uuid"] == "inbox" + assert res.data["name"] == "inbox" + + +def test_retrieve_list_upcoming(api_client_with_credentials, user): + baker.make(TaskList, created_by=user, name="upcoming") + res = api_client_with_credentials.get(tasks_detail_url("upcoming")) + assert res.status_code == status.HTTP_200_OK + assert res.data["list_uuid"] == "upcoming" + assert res.data["name"] == "upcoming" def test_create_list(api_client_with_credentials, user): diff --git a/todo_api/views.py b/todo_api/views.py index 42749d6..d9476e3 100644 --- a/todo_api/views.py +++ b/todo_api/views.py @@ -124,6 +124,13 @@ def retrieve(self, request, list_uuid=None, *args, **kwargs): Retrieve a task list object by list_uuid. """ list_uuid = list_uuid.lower() + if list_uuid in ["inbox", "upcoming"]: + data = { + "list_uuid": list_uuid, + "name": list_uuid, + } + serializer = self.get_serializer(data) + return Response(serializer.data) queryset = self.get_queryset() try: diff --git a/todo_project/settings.py b/todo_project/settings.py index 527cb8b..1b93ff7 100644 --- a/todo_project/settings.py +++ b/todo_project/settings.py @@ -42,6 +42,7 @@ "core", "todo_api", "user_api", + "ai_api", "auth_api", "django.contrib.sites", "dj_rest_auth", @@ -50,6 +51,7 @@ "allauth.account", "allauth.socialaccount", "allauth.socialaccount.providers.google", + "allauth.socialaccount.providers.github", ] MIDDLEWARE = [ @@ -171,7 +173,7 @@ SITE_ID = 1 # https://dj-rest-auth.readthedocs.io/en/latest/installation.html#registration-optional REST_USE_JWT = True # use JSON Web -# # Social authentication configuration +# All auth social providers configuration SOCIALACCOUNT_PROVIDERS = { "google": { "SCOPE": [ @@ -183,12 +185,30 @@ "access_type": "online", }, "VERIFIED_EMAIL": True, - } + "APP": { + "client_id": os.environ.get("GOOGLE_CLIENT_ID"), + "secret": os.environ.get("GOOGLE_CLIENT_SECRET"), + }, + }, + "github": { + "SCOPE": [ + "user", + "repo", + ], + "AUTH_PARAMS": { + "access_type": "online", + }, + "VERIFIED_EMAIL": True, + "APP": { + "client_id": os.environ.get("GITHUB_CLIENT_ID"), + "secret": os.environ.get("GITHUB_CLIENT_SECRET"), + }, + }, } # JWT Settings SIMPLE_JWT = { - "ACCESS_TOKEN_LIFETIME": timedelta(minutes=30), + "ACCESS_TOKEN_LIFETIME": timedelta(minutes=480), "REFRESH_TOKEN_LIFETIME": timedelta(days=7), "ROTATE_REFRESH_TOKENS": True, "BLACKLIST_AFTER_ROTATION": True, @@ -197,13 +217,3 @@ "USER_ID_CLAIM": "user_id", "SIGNING_KEY": os.getenv("JWT_SECRET_KEY"), } - -# All auth social providers configuration -SOCIALACCOUNT_PROVIDERS = { - "google": { - "APP": { - "client_id": os.environ.get("GOOGLE_CLIENT_ID"), - "secret": os.environ.get("GOOGLE_CLIENT_SECRET"), - } - } -} diff --git a/todo_project/urls.py b/todo_project/urls.py index f096884..d7a41ea 100644 --- a/todo_project/urls.py +++ b/todo_project/urls.py @@ -7,8 +7,9 @@ path("admin/", admin.site.urls), path("api/", include("todo_api.urls"), name="task"), path("api/user/", include("user_api.urls"), name="user"), - path("api/rest/auth/", include("dj_rest_auth.urls"), name="rest-auth"), path("api/auth/", include("auth_api.urls"), name="auth"), + path("api/ai/", include("ai_api.urls"), name="ai"), + path("accounts/", include("allauth.urls")), ] urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)