-
Notifications
You must be signed in to change notification settings - Fork 5k
Add tests for auth_init.py #2741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds comprehensive test coverage for the auth_init.py script with mocked network requests to the Microsoft Graph SDK. The tests validate Azure AD application creation, secret management, and service principal operations without making actual HTTP calls.
- Adds 9 test functions covering application creation, secret addition, and validation scenarios
- Updates MockAzureCredential to match msgraph-sdk API expectations
- Upgrades msgraph-sdk from version 1.26.0 to 1.45.0
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
tests/test_auth_init.py | New comprehensive test suite for auth_init.py functions with GraphServiceClient mocking |
tests/mocks.py | Updates MockAzureCredential get_token method signature to accept *scopes and **kwargs |
app/backend/requirements.txt | Upgrades msgraph-sdk from 1.26.0 to 1.45.0 and removes rich from typing-extensions dependencies |
AGENTS.md | Adds documentation for mocking preferences and dependency upgrade procedures |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
tests/test_auth_init.py
Outdated
@pytest.mark.asyncio | ||
async def test_add_client_secret_missing_secret(graph_client): | ||
graph = graph_client | ||
graph._test_secret_text_value["value"] = None # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Direct mutation of test fixture attributes makes the test brittle and unclear. Consider creating a separate fixture or using parametrization to test this scenario instead of modifying the shared fixture state.
@pytest.mark.asyncio | |
async def test_add_client_secret_missing_secret(graph_client): | |
graph = graph_client | |
graph._test_secret_text_value["value"] = None # type: ignore | |
@pytest.fixture | |
def graph_client_with_missing_secret(graph_client): | |
graph_client._test_secret_text_value["value"] = None # type: ignore | |
return graph_client | |
@pytest.mark.asyncio | |
async def test_add_client_secret_missing_secret(graph_client_with_missing_secret): | |
graph = graph_client_with_missing_secret |
Copilot uses AI. Check for mistakes.
tests/test_auth_init.py
Outdated
def test_client_app_validation_errors(): | ||
# Server app without api | ||
server_app = server_app_initial(1) | ||
server_app.api = None # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting attributes to None and suppressing type checking makes the test unclear. Consider creating a proper test fixture or using a builder pattern to create invalid server app instances.
def test_client_app_validation_errors(): | |
# Server app without api | |
server_app = server_app_initial(1) | |
server_app.api = None # type: ignore | |
def server_app_with_no_api(): | |
# Create a server app instance with api=None | |
app = server_app_initial(1) | |
app.api = None | |
return app | |
def test_client_app_validation_errors(): | |
# Server app without api | |
server_app = server_app_with_no_api() |
Copilot uses AI. Check for mistakes.
# attach empty api | ||
server_app_permission = server_app_permission_setup("server_app") | ||
server_app_permission.api.oauth2_permission_scopes = [] # type: ignore | ||
with pytest.raises(ValueError): | ||
client_app("server_app_id", server_app_permission, 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Direct mutation with type ignore comments reduces test clarity. Consider creating a dedicated fixture or helper function that returns a server app with empty scopes for this test scenario.
# attach empty api | |
server_app_permission = server_app_permission_setup("server_app") | |
server_app_permission.api.oauth2_permission_scopes = [] # type: ignore | |
with pytest.raises(ValueError): | |
client_app("server_app_id", server_app_permission, 2) | |
# use a helper to create a server app with empty scopes | |
def server_app_with_empty_scopes(): | |
app = server_app_permission_setup("server_app") | |
app.api.oauth2_permission_scopes = [] | |
return app | |
with pytest.raises(ValueError): | |
client_app("server_app_id", server_app_with_empty_scopes(), 2) |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
tests/test_auth_init.py
Outdated
created_ids = {"object_id": "OBJ123", "client_id": "APP123"} | ||
secret_text_value = {"value": "SECRET_VALUE"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] These test data values are magic strings that could be made more maintainable by using constants or more descriptive variable names like MOCK_OBJECT_ID
and MOCK_CLIENT_ID
.
Copilot uses AI. Check for mistakes.
tests/test_auth_init.py
Outdated
method = ( | ||
request_info.http_method.value | ||
if hasattr(request_info.http_method, "value") | ||
else str(request_info.http_method) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] This method extraction logic is duplicated in multiple functions (lines 42-46 and 93-97). Consider extracting this into a helper function to reduce code duplication.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Purpose
This PR adds tests for the auth_init.py script, with mocks for the network requests to the Graph SDK.
Does this introduce a breaking change?
When developers merge from main and run the server, azd up, or azd deploy, will this produce an error?
If you're not sure, try it out on an old environment.
Does this require changes to learn.microsoft.com docs?
This repository is referenced by this tutorial
which includes deployment, settings and usage instructions. If text or screenshot need to change in the tutorial,
check the box below and notify the tutorial author. A Microsoft employee can do this for you if you're an external contributor.
Type of change
Code quality checklist
See CONTRIBUTING.md for more details.
python -m pytest
).python -m pytest --cov
to verify 100% coverage of added linespython -m mypy
to check for type errorsruff
andblack
manually on my code.