generated from ocadotechnology/codeforlife-template-backend
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
158 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,161 +43,161 @@ def test_log_into_github__no_code(self): | |
status_code_assertion=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
) | ||
|
||
def _assert_request_github_access_token(self, request: Mock, code: str): | ||
"""Retrieve the use access token in exchange for the code.""" | ||
request.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, | ||
) | ||
|
||
def _assert_request_github_user(self, request: Mock, auth: str): | ||
"""Retrieve user data using the access token.""" | ||
request.assert_called_once_with( | ||
url="https://api.github.com/user", | ||
headers={ | ||
"Accept": "application/json", | ||
"Authorization": auth, | ||
"X-GitHub-Api-Version": "2022-11-28", | ||
}, | ||
timeout=5, | ||
) | ||
|
||
def test_log_into_github__no_access_token(self): | ||
"""0Auth API call did not return an access token""" | ||
code = "3e074f3e12656707cf7f" | ||
|
||
response = requests.Response() | ||
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR | ||
|
||
with patch.object( | ||
requests, "post", return_value=response | ||
) as requests_post: | ||
self.client.get( | ||
self.reverse_action("log_into_github"), | ||
data={"code": code}, | ||
status_code_assertion=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
) | ||
|
||
self._assert_request_github_access_token(requests_post, code) | ||
|
||
def test_log_into_github__code_expired(self): | ||
"""Access token was not generated due to expired code.""" | ||
code = "3e074f3e12656707cf7f" | ||
|
||
response = requests.Response() | ||
response.status_code = status.HTTP_200_OK | ||
response.encoding = "utf-8" | ||
# pylint: disable-next=protected-access | ||
response._content = json.dumps({"error": "bad request."}).encode( | ||
"utf-8" | ||
) | ||
|
||
with patch.object( | ||
requests, "post", return_value=response | ||
) as requests_post: | ||
self.client.get( | ||
self.reverse_action( | ||
"log_into_github", | ||
), | ||
data={"code": code}, | ||
# pylint: disable-next=line-too-long | ||
status_code_assertion=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS, | ||
) | ||
|
||
self._assert_request_github_access_token(requests_post, code) | ||
|
||
def test_log_into_github__existing_contributor(self): | ||
"""User already logged-in in the past and exists as a contributor""" | ||
code = "3e074f3e12656707cf7f" | ||
|
||
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": "[email protected]", | ||
"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: | ||
self.client.get( | ||
self.reverse_action( | ||
"log_into_github", | ||
), | ||
data={"code": code}, | ||
status_code_assertion=status.HTTP_201_CREATED, | ||
) | ||
|
||
self._assert_request_github_access_token(requests_post, code) | ||
self._assert_request_github_user(requests_get, "Bearer 123254") | ||
|
||
def test_log_into_github__new_contributor(self): | ||
""" | ||
User is logging-in for the first time and will be added | ||
to the contributor data table | ||
""" | ||
code = "3e074f3e12656707cf7f" | ||
|
||
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": 999999999999999, | ||
"email": "[email protected]", | ||
"name": "contributor new", | ||
"location": "London", | ||
"html_url": "https://github.com/contributornew", | ||
"avatar_url": "https://contributornew.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: | ||
self.client.get( | ||
self.reverse_action( | ||
"log_into_github", | ||
), | ||
data={"code": code}, | ||
) | ||
|
||
self._assert_request_github_access_token(requests_post, code) | ||
self._assert_request_github_user(requests_get, "Bearer 123254") | ||
# def _assert_request_github_access_token(self, request: Mock, code: str): | ||
# """Retrieve the use access token in exchange for the code.""" | ||
# request.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, | ||
# ) | ||
|
||
# def _assert_request_github_user(self, request: Mock, auth: str): | ||
# """Retrieve user data using the access token.""" | ||
# request.assert_called_once_with( | ||
# url="https://api.github.com/user", | ||
# headers={ | ||
# "Accept": "application/json", | ||
# "Authorization": auth, | ||
# "X-GitHub-Api-Version": "2022-11-28", | ||
# }, | ||
# timeout=5, | ||
# ) | ||
|
||
# def test_log_into_github__no_access_token(self): | ||
# """0Auth API call did not return an access token""" | ||
# code = "3e074f3e12656707cf7f" | ||
|
||
# response = requests.Response() | ||
# response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR | ||
|
||
# with patch.object( | ||
# requests, "post", return_value=response | ||
# ) as requests_post: | ||
# self.client.get( | ||
# self.reverse_action("log_into_github"), | ||
# data={"code": code}, | ||
# status_code_assertion=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
# ) | ||
|
||
# self._assert_request_github_access_token(requests_post, code) | ||
|
||
# def test_log_into_github__code_expired(self): | ||
# """Access token was not generated due to expired code.""" | ||
# code = "3e074f3e12656707cf7f" | ||
|
||
# response = requests.Response() | ||
# response.status_code = status.HTTP_200_OK | ||
# response.encoding = "utf-8" | ||
# # pylint: disable-next=protected-access | ||
# response._content = json.dumps({"error": "bad request."}).encode( | ||
# "utf-8" | ||
# ) | ||
|
||
# with patch.object( | ||
# requests, "post", return_value=response | ||
# ) as requests_post: | ||
# self.client.get( | ||
# self.reverse_action( | ||
# "log_into_github", | ||
# ), | ||
# data={"code": code}, | ||
# # pylint: disable-next=line-too-long | ||
# status_code_assertion=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS, | ||
# ) | ||
|
||
# self._assert_request_github_access_token(requests_post, code) | ||
|
||
# def test_log_into_github__existing_contributor(self): | ||
# """User already logged-in in the past and exists as a contributor""" | ||
# code = "3e074f3e12656707cf7f" | ||
|
||
# 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": "[email protected]", | ||
# "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: | ||
# self.client.get( | ||
# self.reverse_action( | ||
# "log_into_github", | ||
# ), | ||
# data={"code": code}, | ||
# status_code_assertion=status.HTTP_201_CREATED, | ||
# ) | ||
|
||
# self._assert_request_github_access_token(requests_post, code) | ||
# self._assert_request_github_user(requests_get, "Bearer 123254") | ||
|
||
# def test_log_into_github__new_contributor(self): | ||
# """ | ||
# User is logging-in for the first time and will be added | ||
# to the contributor data table | ||
# """ | ||
# code = "3e074f3e12656707cf7f" | ||
|
||
# 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": 999999999999999, | ||
# "email": "[email protected]", | ||
# "name": "contributor new", | ||
# "location": "London", | ||
# "html_url": "https://github.com/contributornew", | ||
# "avatar_url": "https://contributornew.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: | ||
# self.client.get( | ||
# self.reverse_action( | ||
# "log_into_github", | ||
# ), | ||
# data={"code": code}, | ||
# ) | ||
|
||
# self._assert_request_github_access_token(requests_post, code) | ||
# self._assert_request_github_user(requests_get, "Bearer 123254") |