Skip to content

Commit fb9323a

Browse files
committed
Fix: add tests for get user
1 parent 1ff95de commit fb9323a

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ version.source = "vcs"
6969
build.hooks.vcs.version-file = "src/pyosmeta/_version.py"
7070

7171
[tool.hatch.envs.test]
72-
dependencies = ["pytest", "pytest-cov", "coverage[toml]"]
72+
dependencies = ["pytest", "pytest-cov", "coverage[toml]", "pytest-mock"]
7373

7474
[tool.hatch.envs.test.scripts]
7575
run-coverage = "pytest --cov-config=pyproject.toml --cov=pyosmeta --cov=tests/*"

src/pyosmeta/github_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,6 @@ def get_user_info(
252252

253253
if response.status_code == 401:
254254
raise ValueError(
255-
"Bad credentials. Please check your authentication token."
255+
"Oops, I couldn't authenticate. Please check your token."
256256
)
257257
return response.json()

tests/unit/test_github_api.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@
66
from pyosmeta.github_api import GitHubAPI
77

88

9+
@pytest.fixture
10+
def expected_ghuser_response():
11+
"""I took a valid response and changed the username to create this object"""
12+
expected_response = {
13+
"login": "chayadecacao",
14+
"id": 578543,
15+
"node_id": "MDQ6VXNlcjU3ODU0Mw==",
16+
"avatar_url": "https://avatars.githubusercontent.com/u/578543?v=4",
17+
"gravatar_id": "",
18+
"url": "https://api.github.com/users/webknjaz",
19+
"html_url": "https://github.com/webknjaz",
20+
}
21+
return expected_response
22+
23+
924
@pytest.fixture
1025
def mock_github_token(monkeypatch):
1126
"""Fixture to create a mock token - i don't believe this
@@ -51,3 +66,31 @@ def test_api_endpoint(github_api):
5166
"issues?labels=label1,label2&state=all&per_page=100"
5267
)
5368
assert github_api.api_endpoint == expected_endpoint
69+
70+
71+
def test_get_user_info_successful(mocker, expected_ghuser_response):
72+
"""Test that an expected response returns properly"""
73+
74+
expected_response = expected_ghuser_response
75+
mock_response = mocker.Mock()
76+
mock_response.status_code = 200
77+
mock_response.json.return_value = expected_response
78+
mocker.patch("requests.get", return_value=mock_response)
79+
80+
github_api_instance = GitHubAPI()
81+
user_info = github_api_instance.get_user_info("example_user")
82+
83+
assert user_info == expected_response
84+
85+
86+
def test_get_user_info_bad_credentials(mocker):
87+
"""Test that a value error is raised when the GH token is not
88+
valid."""
89+
mock_response = mocker.Mock()
90+
mock_response.status_code = 401
91+
mocker.patch("requests.get", return_value=mock_response)
92+
93+
github_api = GitHubAPI()
94+
95+
with pytest.raises(ValueError, match="Oops, I couldn't authenticate"):
96+
github_api.get_user_info("example_user")

0 commit comments

Comments
 (0)