-
Notifications
You must be signed in to change notification settings - Fork 310
Refactor sub fallback #1228
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
Draft
xiangyan99
wants to merge
2
commits into
main
Choose a base branch
from
refactor_sub_fallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Refactor sub fallback #1228
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
124 changes: 124 additions & 0 deletions
124
...re/tests/Azure.Mcp.Core.UnitTests/Services/Azure/Subscription/SubscriptionServiceTests.cs
This file contains hidden or 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,124 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Azure.Mcp.Core.Helpers; | ||
| using Azure.Mcp.Core.Options; | ||
| using Azure.Mcp.Core.Services.Azure.Subscription; | ||
| using Azure.Mcp.Core.Services.Azure.Tenant; | ||
| using Azure.Mcp.Core.Services.Caching; | ||
| using NSubstitute; | ||
| using Xunit; | ||
|
|
||
| namespace Azure.Mcp.Core.UnitTests.Services.Azure.Subscription; | ||
|
|
||
| public class SubscriptionServiceTests : IDisposable | ||
| { | ||
| private const string TestSubscriptionId = "12345678-1234-1234-1234-123456789012"; | ||
| private const string TestSubscriptionName = "Test Subscription"; | ||
| private const string EnvVarSubscriptionId = "87654321-4321-4321-4321-210987654321"; | ||
|
|
||
| private readonly ICacheService _cacheService; | ||
| private readonly ITenantService _tenantService; | ||
| private readonly SubscriptionService _subscriptionService; | ||
| private readonly string? _originalEnvValue; | ||
|
|
||
| public SubscriptionServiceTests() | ||
| { | ||
| // Save original environment variable | ||
| _originalEnvValue = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID"); | ||
|
|
||
| _cacheService = Substitute.For<ICacheService>(); | ||
| _tenantService = Substitute.For<ITenantService>(); | ||
| _subscriptionService = new SubscriptionService(_cacheService, _tenantService); | ||
|
|
||
| // Setup default cache behavior to return null (cache miss) | ||
| _cacheService.GetAsync<object>(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<TimeSpan>()) | ||
| .Returns((object?)null); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| // Restore original environment variable | ||
| if (_originalEnvValue != null) | ||
| { | ||
| Environment.SetEnvironmentVariable("AZURE_SUBSCRIPTION_ID", _originalEnvValue); | ||
| } | ||
| else | ||
| { | ||
| Environment.SetEnvironmentVariable("AZURE_SUBSCRIPTION_ID", null); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsSubscriptionId_ValidGuid_ReturnsTrue() | ||
| { | ||
| // Act | ||
| var result = _subscriptionService.IsSubscriptionId(TestSubscriptionId); | ||
|
|
||
| // Assert | ||
| Assert.True(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsSubscriptionId_InvalidGuid_ReturnsFalse() | ||
| { | ||
| // Act | ||
| var result = _subscriptionService.IsSubscriptionId("not-a-guid"); | ||
|
|
||
| // Assert | ||
| Assert.False(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsSubscriptionId_SubscriptionName_ReturnsFalse() | ||
| { | ||
| // Act | ||
| var result = _subscriptionService.IsSubscriptionId(TestSubscriptionName); | ||
|
|
||
| // Assert | ||
| Assert.False(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EnvironmentVariableFallback_IsAvailable() | ||
| { | ||
| // Arrange | ||
| Environment.SetEnvironmentVariable("AZURE_SUBSCRIPTION_ID", EnvVarSubscriptionId); | ||
|
|
||
| // Act | ||
| var envSubscription = EnvironmentHelpers.GetAzureSubscriptionId(); | ||
|
|
||
| // Assert | ||
| Assert.Equal(EnvVarSubscriptionId, envSubscription); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EnvironmentVariableFallback_WhenNotSet_ReturnsNull() | ||
| { | ||
| // Arrange | ||
| Environment.SetEnvironmentVariable("AZURE_SUBSCRIPTION_ID", null); | ||
|
|
||
| // Act | ||
| var envSubscription = EnvironmentHelpers.GetAzureSubscriptionId(); | ||
|
|
||
| // Assert | ||
| Assert.Null(envSubscription); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("12345678-1234-1234-1234-123456789012", true)] | ||
| [InlineData("00000000-0000-0000-0000-000000000000", true)] | ||
| [InlineData("subscription", false)] | ||
| [InlineData("default", false)] | ||
| [InlineData("My Subscription Name", false)] | ||
| [InlineData("test-subscription", false)] | ||
| public void IsSubscriptionId_VariousInputs_ReturnsExpected(string input, bool expected) | ||
| { | ||
| // Act | ||
| var result = _subscriptionService.IsSubscriptionId(input); | ||
|
|
||
| // Assert | ||
| Assert.Equal(expected, result); | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
By the time we reach this point, didn't we already go through the logic in
CommandHelperwhich checks for--subscriptionbeing passed, and if it wasn't falling back to the environment variableAZURE_SUBSCRIPTION_ID? So, wouldn't this be more confusing as if I pass--subscription invalidbut haveAZURE_SUBSCRIPTION_ID=validI now have a complicated to understand flow with my--subscriptioninput being ignored and--subscription validwas used.