|
6 | 6 | from pyosmeta.github_api import GitHubAPI |
7 | 7 |
|
8 | 8 |
|
| 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 | + |
9 | 24 | @pytest.fixture |
10 | 25 | def mock_github_token(monkeypatch): |
11 | 26 | """Fixture to create a mock token - i don't believe this |
@@ -51,3 +66,31 @@ def test_api_endpoint(github_api): |
51 | 66 | "issues?labels=label1,label2&state=all&per_page=100" |
52 | 67 | ) |
53 | 68 | 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