-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
4ad0194
commit 78dfb30
Showing
5 changed files
with
104 additions
and
34 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[run] | ||
omit = | ||
*/test*/* | ||
*/helpers/* | ||
*/database_gen/* | ||
*/dataset_service/* | ||
*/feeds_operations_gen/* | ||
|
||
[report] | ||
exclude_lines = | ||
if __name__ == .__main__.: |
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
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
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
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 |
---|---|---|
|
@@ -24,7 +24,7 @@ | |
@pytest.fixture | ||
def scope(): | ||
def _scope(token): | ||
return { | ||
result = { | ||
"type": "http", | ||
"headers": [ | ||
(b"host", b"example.com"), | ||
|
@@ -33,12 +33,15 @@ def _scope(token): | |
(b"user-agent", b"test-agent"), | ||
(b"x-goog-iap-jwt-assertion", b"test-assertion"), | ||
(b"x-cloud-trace-context", b"trace-id/span-id;o=1"), | ||
(b"authorization", f"Bearer {token}".encode("utf-8")), | ||
], | ||
"client": ("192.168.1.1", 12345), | ||
"server": ("127.0.0.1", 8000), | ||
"scheme": "https", | ||
} | ||
if token is not None: | ||
if token is not None: | ||
result["headers"].append((b"authorization", f"Bearer {token}".encode())) | ||
return result | ||
|
||
return _scope | ||
|
||
|
@@ -70,9 +73,69 @@ def test_request_context_initialization( | |
assert request_context.trace_id == "trace-id" | ||
assert request_context.span_id == "span-id" | ||
assert request_context.trace_sampled is True | ||
assert ( | ||
request_context.user_email == "[email protected]" | ||
) # Mock the email extraction | ||
assert request_context.user_email == "[email protected]" | ||
|
||
|
||
@patch("middleware.request_context_oauth2.get_tokeninfo_response") | ||
def test_request_context_invalid_audience( | ||
mock_get_tokeninfo_response, scope, monkeypatch | ||
): | ||
monkeypatch.setenv("GOOGLE_CLIENT_ID", "test-client-id_audience") | ||
monkeypatch.setenv("LOCAL_ENV", "true") | ||
|
||
mock_get_tokeninfo_response.return_value.status_code = 200 | ||
mock_get_tokeninfo_response.return_value.json.return_value = { | ||
"email": "[email protected]", | ||
"audience": "not-test-client-id", | ||
"email_verified": True, | ||
"expires_in": 3600, | ||
} | ||
|
||
mocked_scope = scope("test_request_context_invalid_audience") | ||
|
||
with pytest.raises(HTTPException) as exc_info: | ||
RequestContext(mocked_scope) | ||
assert exc_info.value.status_code == 401 | ||
assert exc_info.value.detail == "Invalid token audience" | ||
|
||
|
||
@patch("middleware.request_context_oauth2.get_tokeninfo_response") | ||
def test_request_context_email_not_found( | ||
mock_get_tokeninfo_response, scope, monkeypatch | ||
): | ||
monkeypatch.setenv("GOOGLE_CLIENT_ID", "test-client-id") | ||
monkeypatch.setenv("LOCAL_ENV", "true") | ||
|
||
mock_get_tokeninfo_response.return_value.status_code = 200 | ||
mock_get_tokeninfo_response.return_value.json.return_value = { | ||
"audience": "test-client-id", | ||
"email_verified": True, | ||
"expires_in": 3600, | ||
} | ||
|
||
mocked_scope = scope("test_request_context_email_not_found") | ||
|
||
with pytest.raises(HTTPException) as exc_info: | ||
RequestContext(mocked_scope) | ||
assert exc_info.value.status_code == 400 | ||
assert exc_info.value.detail == "Email not found in token" | ||
|
||
|
||
@patch("middleware.request_context_oauth2.get_tokeninfo_response") | ||
def test_request_context_invalid_tokeninfo_exception( | ||
mock_get_tokeninfo_response, scope, monkeypatch | ||
): | ||
monkeypatch.setenv("GOOGLE_CLIENT_ID", "test-client-id") | ||
monkeypatch.setenv("LOCAL_ENV", "true") | ||
|
||
mock_get_tokeninfo_response.side_effect = Exception("Test exception") | ||
|
||
mocked_scope = scope("test_request_context_invalid_tokeninfo_exception") | ||
|
||
with pytest.raises(HTTPException) as exc_info: | ||
RequestContext(mocked_scope) | ||
assert exc_info.value.status_code == 500 | ||
assert exc_info.value.detail == "Token validation failed" | ||
|
||
|
||
def test_request_context_missing_authorization(scope, monkeypatch): | ||
|
@@ -101,3 +164,16 @@ def test_request_context_invalid_token(mock_get_tokeninfo_response, scope, monke | |
RequestContext(scope("test_token_test_request_context_invalid_token")) | ||
assert exc_info.value.status_code == 401 | ||
assert exc_info.value.detail == "Invalid access token" | ||
|
||
|
||
@patch("middleware.request_context_oauth2.get_tokeninfo_response") | ||
def test_request_context_no_token(mock_get_tokeninfo_response, scope, monkeypatch): | ||
monkeypatch.setenv("GOOGLE_CLIENT_ID", "test-client-id") | ||
monkeypatch.setenv("LOCAL_ENV", "False") | ||
|
||
mock_get_tokeninfo_response.return_value.status_code = 400 | ||
|
||
with pytest.raises(HTTPException) as exc_info: | ||
RequestContext(scope(token=None)) | ||
assert exc_info.value.status_code == 401 | ||
assert exc_info.value.detail == "Authorization header not found" |