Skip to content

Commit

Permalink
Test create serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
SalmanAsh committed Aug 8, 2024
1 parent 18cf92e commit d448e69
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 45 deletions.
25 changes: 13 additions & 12 deletions api/auth/backends/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.contrib.auth.backends import BaseBackend

from ...models import Contributor
from ...serializers import ContributorSerializer


class GithubBackend(BaseBackend):
Expand Down Expand Up @@ -56,24 +57,24 @@ def authenticate( # type: ignore[override]
timeout=5,
)

# if not response.ok:
# return None

try:
contributor_data = response.json()
contributor = Contributor.objects.create(
id=contributor_data["id"],
email=contributor_data["email"],
name=contributor_data["name"],
location=contributor_data["location"],
html_url=contributor_data["html_url"],
avatar_url=contributor_data["avatar_url"],
serializer = ContributorSerializer(
data={
"id": contributor_data["id"],
"email": contributor_data["email"],
"name": contributor_data["name"],
"location": contributor_data["location"],
"html_url": contributor_data["html_url"],
"avatar_url": contributor_data["avatar_url"],
}
)

if serializer.is_valid():
return serializer.save()
except KeyError:
return None

return contributor if contributor else None
return None

def get_user(self, user_id: int):
try:
Expand Down
80 changes: 47 additions & 33 deletions api/views/session_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,6 @@ def test_login__invalid_user(self):
format="json",
)

requests_post.assert_called_once_with(
url="https://github.com/login/oauth/access_token",
headers={"Accept": "application/json"},
params={
"client_id": settings.GH_CLIENT_ID,
"client_secret": settings.GH_CLIENT_SECRET,
"code": code,
},
timeout=5,
)

assert response.status_code == status.HTTP_400_BAD_REQUEST

self._assert_request_github_access_token(requests_post, code)
Expand Down Expand Up @@ -155,18 +144,6 @@ def test_login__new_contributor(self):
data={"code": code},
format="json",
)

requests_post.assert_called_once_with(
url="https://github.com/login/oauth/access_token",
headers={"Accept": "application/json"},
params={
"client_id": settings.GH_CLIENT_ID,
"client_secret": settings.GH_CLIENT_SECRET,
"code": code,
},
timeout=5,
)

assert response.status_code == status.HTTP_200_OK

self._assert_request_github_access_token(requests_post, code)
Expand Down Expand Up @@ -215,18 +192,55 @@ def test_login__existing_contributor(self):
format="json",
)

requests_post.assert_called_once_with(
url="https://github.com/login/oauth/access_token",
headers={"Accept": "application/json"},
params={
"client_id": settings.GH_CLIENT_ID,
"client_secret": settings.GH_CLIENT_SECRET,
"code": code,
},
timeout=5,
assert response.status_code == status.HTTP_200_OK

self._assert_request_github_access_token(requests_post, code)
self._assert_request_github_user(requests_get, "Bearer 123254")

def test_login__invalid_contributor_data(self):
"""
Login a returning user with their existing github account and
sync any updated information for this contributor.
"""

code = "7f06468085765cdc1578"

response_post = requests.Response()
response_post.status_code = status.HTTP_200_OK
response_post.encoding = "utf-8"
# pylint: disable-next=protected-access
response_post._content = json.dumps(
{"access_token": "123254", "token_type": "Bearer"}
).encode("utf-8")

response_get = requests.Response()
response_get.status_code = status.HTTP_200_OK
response_get.encoding = "utf-8"
# pylint: disable-next=protected-access
response_get._content = json.dumps(
{
"id": 1,
"email": "1234",
"name": "contributor one",
"location": "London",
"html_url": "https://github.com/contributor1",
"avatar_url": "https://contributor1.github.io/",
}
).encode("utf-8")

with patch.object(
requests, "post", return_value=response_post
) as requests_post:
with patch.object(
requests, "get", return_value=response_get
) as requests_get:
response = self.client.post(
reverse("session-login"),
data={"code": code},
format="json",
)

assert response.status_code == status.HTTP_200_OK
assert response.status_code == status.HTTP_400_BAD_REQUEST

self._assert_request_github_access_token(requests_post, code)
self._assert_request_github_user(requests_get, "Bearer 123254")

0 comments on commit d448e69

Please sign in to comment.