Skip to content

Commit

Permalink
delete duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
SalmanAsh committed Aug 8, 2024
1 parent 48ba003 commit 60361ca
Showing 1 changed file with 158 additions and 158 deletions.
316 changes: 158 additions & 158 deletions api/views/contributor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 60361ca

Please sign in to comment.