Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pr: 1901
changes:
- section: "Other Changes"
description: "Authorization: Improve testability by removing dependency on CommandContext.ServiceProvider in ExecuteAsync"
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

namespace Azure.Mcp.Tools.Authorization.Commands;

public sealed class RoleAssignmentListCommand(ILogger<RoleAssignmentListCommand> logger) : SubscriptionCommand<RoleAssignmentListOptions>
public sealed class RoleAssignmentListCommand(ILogger<RoleAssignmentListCommand> logger, IAuthorizationService authorizationService) : SubscriptionCommand<RoleAssignmentListOptions>
{
private const string _commandTitle = "List Role Assignments";
private readonly ILogger<RoleAssignmentListCommand> _logger = logger;
private readonly IAuthorizationService _authorizationService = authorizationService;

public override string Id => "1dfbef45-4014-4575-a9ba-2242bc792e54";

Expand Down Expand Up @@ -64,8 +65,7 @@ public override async Task<CommandResponse> ExecuteAsync(CommandContext context,

try
{
var authService = context.GetService<IAuthorizationService>();
var assignments = await authService.ListRoleAssignmentsAsync(
var assignments = await _authorizationService.ListRoleAssignmentsAsync(
options.Subscription!,
options.Scope!,
options.Tenant,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.CommandLine;
using System.Net;
using System.Text.Json;
using Azure.Mcp.Core.Options;
Expand All @@ -22,16 +23,20 @@ public class RoleAssignmentListCommandTests
private readonly IServiceProvider _serviceProvider;
private readonly IAuthorizationService _authorizationService;
private readonly ILogger<RoleAssignmentListCommand> _logger;
private readonly RoleAssignmentListCommand _command;
private readonly CommandContext _context;
private readonly Command _commandDefinition;

public RoleAssignmentListCommandTests()
{
_authorizationService = Substitute.For<IAuthorizationService>();
_logger = Substitute.For<ILogger<RoleAssignmentListCommand>>();

var collection = new ServiceCollection();
collection.AddSingleton(_authorizationService);

_serviceProvider = collection.BuildServiceProvider();
_command = new(_logger, _authorizationService);
_commandDefinition = _command.GetCommand();
_serviceProvider = new ServiceCollection()
.BuildServiceProvider();
_context = new(_serviceProvider);
}

[Fact]
Expand Down Expand Up @@ -74,15 +79,13 @@ public async Task ExecuteAsync_ReturnsRoleAssignments_WhenRoleAssignmentsExist()
Arg.Any<RetryPolicyOptions>(),
Arg.Any<CancellationToken>())
.Returns(expectedRoleAssignments);
var command = new RoleAssignmentListCommand(_logger);
var args = command.GetCommand().Parse([
var args = _commandDefinition.Parse([
"--subscription", subscriptionId,
"--scope", scope,
]);
var context = new CommandContext(_serviceProvider);

// Act
var response = await command.ExecuteAsync(context, args, TestContext.Current.CancellationToken);
var response = await _command.ExecuteAsync(_context, args, TestContext.Current.CancellationToken);

// Assert
Assert.NotNull(response);
Expand All @@ -104,15 +107,13 @@ public async Task ExecuteAsync_ReturnsEmpty_WhenNoRoleAssignments()
_authorizationService.ListRoleAssignmentsAsync(subscriptionId, scope, null, null, TestContext.Current.CancellationToken)
.Returns(new ResourceQueryResults<RoleAssignment>([], false));

var command = new RoleAssignmentListCommand(_logger);
var args = command.GetCommand().Parse([
var args = _commandDefinition.Parse([
"--subscription", subscriptionId,
"--scope", scope
]);
var context = new CommandContext(_serviceProvider);

// Act
var response = await command.ExecuteAsync(context, args, TestContext.Current.CancellationToken);
var response = await _command.ExecuteAsync(_context, args, TestContext.Current.CancellationToken);

// Assert
Assert.NotNull(response);
Expand All @@ -136,15 +137,13 @@ public async Task ExecuteAsync_HandlesException()
_authorizationService.ListRoleAssignmentsAsync(subscriptionId, scope, null, null, TestContext.Current.CancellationToken)
.ThrowsAsync(new Exception(expectedError));

var command = new RoleAssignmentListCommand(_logger);
var args = command.GetCommand().Parse([
var args = _commandDefinition.Parse([
"--subscription", subscriptionId,
"--scope", scope
]);
var context = new CommandContext(_serviceProvider);

// Act
var response = await command.ExecuteAsync(context, args, TestContext.Current.CancellationToken);
var response = await _command.ExecuteAsync(_context, args, TestContext.Current.CancellationToken);

// Assert
Assert.NotNull(response);
Expand Down
Loading