From eb239670077693c44dbec85186762b344bd1198f Mon Sep 17 00:00:00 2001 From: Salman Ashraf Date: Thu, 25 Jul 2024 09:35:34 +0000 Subject: [PATCH] Refactor code --- api/views/contributor.py | 10 ++++--- api/views/contributor_test.py | 55 ++++++++++++++--------------------- api/views/login.txt | 1 - 3 files changed, 28 insertions(+), 38 deletions(-) delete mode 100644 api/views/login.txt diff --git a/api/views/contributor.py b/api/views/contributor.py index 489f85c..1be6920 100644 --- a/api/views/contributor.py +++ b/api/views/contributor.py @@ -19,7 +19,7 @@ # pylint: disable-next=missing-class-docstring,too-many-ancestors class ContributorViewSet(ModelViewSet[User, Contributor]): - http_method_names = ["get", "post"] + http_method_names = ["get", "post", "delete"] permission_classes = [AllowAny] serializer_class = ContributorSerializer queryset = Contributor.objects.all() @@ -28,8 +28,7 @@ class ContributorViewSet(ModelViewSet[User, Contributor]): def log_into_github(self, request: Request): """Users can login using their existing github account""" # Get code from login request - code = request.GET.get("code") - if not code: + if not request.GET.get("code"): return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR) # Get user access Token @@ -43,6 +42,7 @@ def log_into_github(self, request: Request): }, timeout=5, ) + if not access_token_request.ok: return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR) auth_data = access_token_request.json() @@ -73,7 +73,7 @@ def log_into_github(self, request: Request): status=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS, ) - # Check if user is already a contributor (TODO: Update user info) + # Check if user is already a contributor gh_id = user_data["id"] contributor_data = { "id": gh_id, @@ -83,7 +83,9 @@ def log_into_github(self, request: Request): "html_url": user_data["html_url"], "avatar_url": user_data["avatar_url"], } + try: + # Update an existing contributor contributor = Contributor.objects.get(pk=gh_id) serializer = ContributorSerializer( contributor, data=contributor_data diff --git a/api/views/contributor_test.py b/api/views/contributor_test.py index aa72438..639b659 100644 --- a/api/views/contributor_test.py +++ b/api/views/contributor_test.py @@ -51,19 +51,16 @@ def test_create(self): def test_log_into_github__no_code(self): """Login API call does not return a code.""" - code = "" self.client.get( self.reverse_action( "log_into_github", ), - {"code": code}, + {"code": ""}, status_code_assertion=status.HTTP_500_INTERNAL_SERVER_ERROR, ) def test_log_into_github__no_access_token(self): - """POST API call did not return an access token""" - code = "3e074f3e12656707cf7f" - + """0Auth API call did not return an access token""" response = requests.Response() response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR @@ -74,7 +71,7 @@ def test_log_into_github__no_access_token(self): self.reverse_action( "log_into_github", ), - {"code": code}, + {"code": "3e074f3e12656707cf7f"}, status_code_assertion=status.HTTP_500_INTERNAL_SERVER_ERROR, ) @@ -84,15 +81,13 @@ def test_log_into_github__no_access_token(self): params={ "client_id": settings.GITHUB_CLIENT_ID, "client_secret": settings.GITHUB_CLIENT_SECRET, - "code": code, + "code": "3e074f3e12656707cf7f", }, timeout=5, ) def test_log_into_github__code_expired(self): - """POST API call failed due to expired code.""" - code = "3e074f3e12656707cf7f" - + """Access token was not generated due to expired code.""" response = requests.Response() response.status_code = status.HTTP_200_OK response.encoding = "utf-8" @@ -106,7 +101,7 @@ def test_log_into_github__code_expired(self): self.reverse_action( "log_into_github", ), - {"code": code}, + {"code": "3e074f3e12656707cf7f"}, # pylint: disable-next=line-too-long status_code_assertion=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS, ) @@ -117,15 +112,13 @@ def test_log_into_github__code_expired(self): params={ "client_id": settings.GITHUB_CLIENT_ID, "client_secret": settings.GITHUB_CLIENT_SECRET, - "code": code, + "code": "3e074f3e12656707cf7f", }, timeout=5, ) def test_log_into_github__null_email(self): - """User must have their email public on github""" - code = "3e074f3e12656707cf7f" - + """Users must have their email as PUBLIC on github""" response_post = requests.Response() response_post.status_code = status.HTTP_200_OK response_post.encoding = "utf-8" @@ -150,7 +143,7 @@ def test_log_into_github__null_email(self): self.reverse_action( "log_into_github", ), - {"code": code}, + {"code": "3e074f3e12656707cf7f"}, # pylint: disable-next=line-too-long status_code_assertion=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS, ) @@ -161,7 +154,7 @@ def test_log_into_github__null_email(self): params={ "client_id": settings.GITHUB_CLIENT_ID, "client_secret": settings.GITHUB_CLIENT_SECRET, - "code": code, + "code": "3e074f3e12656707cf7f", }, timeout=5, ) @@ -175,10 +168,7 @@ def test_log_into_github__null_email(self): ) def test_log_into_github__existing_contributor(self): - """User already exists as a contributor""" - # TODO: update the user info - code = "3e074f3e12656707cf7f" - + """User already logged-in in the past and exists as a contributor""" response_post = requests.Response() response_post.status_code = status.HTTP_200_OK response_post.encoding = "utf-8" @@ -212,7 +202,7 @@ def test_log_into_github__existing_contributor(self): self.reverse_action( "log_into_github", ), - {"code": code}, + {"code": "3e074f3e12656707cf7f"}, status_code_assertion=status.HTTP_200_OK, ) @@ -222,7 +212,7 @@ def test_log_into_github__existing_contributor(self): params={ "client_id": settings.GITHUB_CLIENT_ID, "client_secret": settings.GITHUB_CLIENT_SECRET, - "code": code, + "code": "3e074f3e12656707cf7f", }, timeout=5, ) @@ -236,9 +226,10 @@ def test_log_into_github__existing_contributor(self): ) def test_log_into_github__new_contributor(self): - """Add user to the contributor data table""" - code = "3e074f3e12656707cf7f" - + """ + User is logging-in for the first time and will be added + to the contributor data table + """ response_post = requests.Response() response_post.status_code = status.HTTP_200_OK response_post.encoding = "utf-8" @@ -272,7 +263,7 @@ def test_log_into_github__new_contributor(self): self.reverse_action( "log_into_github", ), - {"code": code}, + {"code": "3e074f3e12656707cf7f"}, ) requests_post.assert_called_once_with( @@ -281,7 +272,7 @@ def test_log_into_github__new_contributor(self): params={ "client_id": settings.GITHUB_CLIENT_ID, "client_secret": settings.GITHUB_CLIENT_SECRET, - "code": code, + "code": "3e074f3e12656707cf7f", }, timeout=5, ) @@ -295,9 +286,7 @@ def test_log_into_github__new_contributor(self): ) def test_log_into_github__invalid_serializer(self): - """User data from Github is not in the valid format.""" - code = "3e074f3e12656707cf7f" - + """User data retrieved from Github is not in the valid format.""" response_post = requests.Response() response_post.status_code = status.HTTP_200_OK response_post.encoding = "utf-8" @@ -331,7 +320,7 @@ def test_log_into_github__invalid_serializer(self): self.reverse_action( "log_into_github", ), - {"code": code}, + {"code": "3e074f3e12656707cf7f"}, # pylint: disable-next=line-too-long status_code_assertion=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS, ) @@ -342,7 +331,7 @@ def test_log_into_github__invalid_serializer(self): params={ "client_id": settings.GITHUB_CLIENT_ID, "client_secret": settings.GITHUB_CLIENT_SECRET, - "code": code, + "code": "3e074f3e12656707cf7f", }, timeout=5, ) diff --git a/api/views/login.txt b/api/views/login.txt deleted file mode 100644 index 73fd118..0000000 --- a/api/views/login.txt +++ /dev/null @@ -1 +0,0 @@ -https://github.com/login/oauth/authorize?client_id=Ov23liBErSabQFqROeMg \ No newline at end of file