Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
SalmanAsh committed Jul 25, 2024
1 parent a66fe7d commit eb23967
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
10 changes: 6 additions & 4 deletions api/views/contributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand 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
Expand All @@ -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()
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
55 changes: 22 additions & 33 deletions api/views/contributor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
)

Expand All @@ -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"
Expand All @@ -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,
)
Expand All @@ -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"
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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"
Expand Down Expand Up @@ -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,
)

Expand All @@ -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,
)
Expand All @@ -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"
Expand Down Expand Up @@ -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(
Expand All @@ -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,
)
Expand All @@ -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"
Expand Down Expand Up @@ -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,
)
Expand All @@ -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,
)
Expand Down
1 change: 0 additions & 1 deletion api/views/login.txt

This file was deleted.

0 comments on commit eb23967

Please sign in to comment.