Skip to content

Conversation

pamelafox
Copy link
Collaborator

@pamelafox pamelafox commented Sep 18, 2025

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.

[ ] Yes
[X] No

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.

[ ] Yes
[X] No

Type of change

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Documentation content changes
[X] Other... Please describe:

Code quality checklist

See CONTRIBUTING.md for more details.

  • The current tests all pass (python -m pytest).
  • I added tests that prove my fix is effective or that my feature works
  • I ran python -m pytest --cov to verify 100% coverage of added lines
  • I ran python -m mypy to check for type errors
  • I either used the pre-commit hooks or ran ruff and black manually on my code.

@pamelafox pamelafox requested a review from Copilot September 18, 2025 18:08
Copy link
Contributor

@Copilot Copilot AI left a 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.

Comment on lines 115 to 118
@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
Copy link
Preview

Copilot AI Sep 18, 2025

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.

Suggested change
@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.

Comment on lines 210 to 213
def test_client_app_validation_errors():
# Server app without api
server_app = server_app_initial(1)
server_app.api = None # type: ignore
Copy link
Preview

Copilot AI Sep 18, 2025

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.

Suggested change
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.

Comment on lines 218 to 222
# 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)
Copy link
Preview

Copilot AI Sep 18, 2025

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.

Suggested change
# 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.

@pamelafox pamelafox requested a review from Copilot September 18, 2025 18:22
Copy link
Contributor

@Copilot Copilot AI left a 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.

Comment on lines 37 to 38
created_ids = {"object_id": "OBJ123", "client_id": "APP123"}
secret_text_value = {"value": "SECRET_VALUE"}
Copy link
Preview

Copilot AI Sep 18, 2025

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.

Comment on lines 42 to 46
method = (
request_info.http_method.value
if hasattr(request_info.http_method, "value")
else str(request_info.http_method)
)
Copy link
Preview

Copilot AI Sep 18, 2025

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.

@pamelafox pamelafox requested a review from Copilot September 18, 2025 18:54
Copy link
Contributor

@Copilot Copilot AI left a 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.

@pamelafox pamelafox merged commit 4b32f3c into Azure-Samples:main Sep 29, 2025
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants