forked from All-Hands-AI/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_github_service.py
81 lines (63 loc) · 2.98 KB
/
test_github_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from unittest.mock import AsyncMock, Mock, patch
import httpx
import pytest
from pydantic import SecretStr
from openhands.integrations.github.github_service import GitHubService
from openhands.integrations.github.github_types import GhAuthenticationError
@pytest.mark.asyncio
async def test_github_service_token_handling():
# Test initialization with SecretStr token
token = SecretStr('test-token')
service = GitHubService(user_id=None, token=token)
assert service.token == token
assert service.token.get_secret_value() == 'test-token'
# Test headers contain the token correctly
headers = await service._get_github_headers()
assert headers['Authorization'] == 'Bearer test-token'
assert headers['Accept'] == 'application/vnd.github.v3+json'
# Test initialization without token
service = GitHubService(user_id='test-user')
assert service.token == SecretStr('')
@pytest.mark.asyncio
async def test_github_service_token_refresh():
# Test that token refresh is only attempted when refresh=True
token = SecretStr('test-token')
service = GitHubService(user_id=None, token=token)
assert not service.refresh
# Test token expiry detection
assert service._has_token_expired(401)
assert not service._has_token_expired(200)
assert not service._has_token_expired(404)
# Test get_latest_token returns a copy of the current token
latest_token = await service.get_latest_token()
assert isinstance(latest_token, SecretStr)
assert latest_token.get_secret_value() == 'test-token' # Compare with known value
@pytest.mark.asyncio
async def test_github_service_fetch_data():
# Mock httpx.AsyncClient for testing API calls
mock_response = AsyncMock()
mock_response.status_code = 200
mock_response.json.return_value = {'login': 'test-user'}
mock_response.raise_for_status = Mock()
mock_client = AsyncMock()
mock_client.get.return_value = mock_response
mock_client.__aenter__.return_value = mock_client
mock_client.__aexit__.return_value = None
with patch('httpx.AsyncClient', return_value=mock_client):
service = GitHubService(user_id=None, token=SecretStr('test-token'))
_ = await service._fetch_data('https://api.github.com/user')
# Verify the request was made with correct headers
mock_client.get.assert_called_once()
call_args = mock_client.get.call_args
headers = call_args[1]['headers']
assert headers['Authorization'] == 'Bearer test-token'
# Test error handling with 401 status code
mock_response.status_code = 401
mock_response.raise_for_status.side_effect = httpx.HTTPStatusError(
message='401 Unauthorized', request=Mock(), response=mock_response
)
# Reset the mock to test error handling
mock_client.get.reset_mock()
mock_client.get.return_value = mock_response
with pytest.raises(GhAuthenticationError):
_ = await service._fetch_data('https://api.github.com/user')