@@ -24,67 +24,71 @@ public class RoleAssignmentListCommandTests
2424 private readonly IServiceProvider _serviceProvider ;
2525 private readonly IAuthorizationService _authorizationService ;
2626 private readonly ILogger < RoleAssignmentListCommand > _logger ;
27+ private readonly RoleAssignmentListCommand _command ;
28+ private readonly CommandContext _context ;
29+ private readonly Parser _parser ;
30+ private readonly string _knownSubscriptionId = "00000000-0000-0000-0000-000000000001" ;
31+ private readonly string _knownScope = "/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1" ;
2732
2833 public RoleAssignmentListCommandTests ( )
2934 {
3035 _authorizationService = Substitute . For < IAuthorizationService > ( ) ;
3136 _logger = Substitute . For < ILogger < RoleAssignmentListCommand > > ( ) ;
3237
33- var collection = new ServiceCollection ( ) ;
34- collection . AddSingleton ( _authorizationService ) ;
38+ var collection = new ServiceCollection ( ) . AddSingleton ( _authorizationService ) ;
3539
3640 _serviceProvider = collection . BuildServiceProvider ( ) ;
41+ _command = new ( _logger ) ;
42+ _context = new ( _serviceProvider ) ;
43+ _parser = new ( _command . GetCommand ( ) ) ;
3744 }
3845
3946 [ Fact ]
4047 public async Task ExecuteAsync_ReturnsRoleAssignments_WhenRoleAssignmentsExist ( )
4148 {
4249 // Arrange
43- var subscriptionId = "00000000-0000-0000-0000-000000000001" ;
44- var scope = $ "/subscriptions/{ subscriptionId } /resourceGroups/rg1";
4550 var id1 = "00000000-0000-0000-0000-000000000001" ;
4651 var id2 = "00000000-0000-0000-0000-000000000002" ;
4752 var expectedRoleAssignments = new List < RoleAssignment >
4853 {
4954 new RoleAssignment
5055 {
51- Id = $ "/subscriptions/{ subscriptionId } /resourcegroups/azure-mcp/providers/Microsoft.Authorization/roleAssignments/{ id1 } ",
56+ Id = $ "/subscriptions/{ _knownSubscriptionId } /resourcegroups/azure-mcp/providers/Microsoft.Authorization/roleAssignments/{ id1 } ",
5257 Name = "Test role definition 1" ,
5358 PrincipalId = new Guid ( id1 ) ,
5459 PrincipalType = "User" ,
55- RoleDefinitionId = $ "/subscriptions/{ subscriptionId } /providers/Microsoft.Authorization/roleDefinitions/{ id1 } ",
56- Scope = scope ,
60+ RoleDefinitionId = $ "/subscriptions/{ _knownSubscriptionId } /providers/Microsoft.Authorization/roleDefinitions/{ id1 } ",
61+ Scope = _knownScope ,
5762 Description = "Role assignment for azmcp test 1" ,
5863 DelegatedManagedIdentityResourceId = string . Empty ,
5964 Condition = string . Empty
6065 } ,
6166 new RoleAssignment
6267 {
63- Id = $ "/subscriptions/{ subscriptionId } /resourcegroups/azure-mcp/providers/Microsoft.Authorization/roleAssignments/{ id2 } ",
68+ Id = $ "/subscriptions/{ _knownSubscriptionId } /resourcegroups/azure-mcp/providers/Microsoft.Authorization/roleAssignments/{ id2 } ",
6469 Name = "Test role definition 2" ,
6570 PrincipalId = new Guid ( id2 ) ,
6671 PrincipalType = "User" ,
67- RoleDefinitionId = $ "/subscriptions/{ subscriptionId } /providers/Microsoft.Authorization/roleDefinitions/{ id2 } ",
68- Scope = scope ,
72+ RoleDefinitionId = $ "/subscriptions/{ _knownSubscriptionId } /providers/Microsoft.Authorization/roleDefinitions/{ id2 } ",
73+ Scope = _knownScope ,
6974 Description = "Role assignment for azmcp test 2" ,
7075 DelegatedManagedIdentityResourceId = string . Empty ,
7176 Condition = "ActionMatches{'Microsoft.Authorization/roleAssignments/write'}"
7277 }
7378 } ;
7479 _authorizationService . ListRoleAssignments (
75- Arg . Is ( scope ) ,
80+ Arg . Is ( _knownScope ) ,
7681 Arg . Any < string > ( ) ,
7782 Arg . Any < RetryPolicyOptions > ( ) )
7883 . Returns ( expectedRoleAssignments ) ;
79- var command = new RoleAssignmentListCommand ( _logger ) ;
80- var args = command . GetCommand ( ) . Parse ( [
81- "--subscription" , subscriptionId ,
82- "--scope" , scope ,
84+
85+ var args = _parser . Parse ( [
86+ "--subscription" , _knownSubscriptionId ,
87+ "--scope" , _knownScope ,
8388 ] ) ;
84- var context = new CommandContext ( _serviceProvider ) ;
8589
8690 // Act
87- var response = await command . ExecuteAsync ( context , args ) ;
91+ var response = await _command . ExecuteAsync ( _context , args ) ;
8892
8993 // Assert
9094 Assert . NotNull ( response ) ;
@@ -98,49 +102,48 @@ public async Task ExecuteAsync_ReturnsRoleAssignments_WhenRoleAssignmentsExist()
98102 }
99103
100104 [ Fact ]
101- public async Task ExecuteAsync_ReturnsNull_WhenNoRoleAssignments ( )
105+ public async Task ExecuteAsync_ReturnsEmptyList_WhenNoRoleAssignments ( )
102106 {
103107 // Arrange
104- var subscriptionId = "00000000-0000-0000-0000-000000000001" ;
105- var scope = $ "/subscriptions/{ subscriptionId } /resourceGroups/rg1";
106- _authorizationService . ListRoleAssignments ( scope , null , null )
108+ _authorizationService . ListRoleAssignments ( Arg . Is ( _knownScope ) , Arg . Any < string > ( ) , Arg . Any < RetryPolicyOptions > ( ) )
107109 . Returns ( [ ] ) ;
108110
109- var command = new RoleAssignmentListCommand ( _logger ) ;
110- var args = command . GetCommand ( ) . Parse ( [
111- "--subscription" , subscriptionId ,
112- "--scope" , scope
111+ var args = _parser . Parse ( [
112+ "--subscription" , _knownSubscriptionId ,
113+ "--scope" , _knownScope
113114 ] ) ;
114- var context = new CommandContext ( _serviceProvider ) ;
115115
116116 // Act
117- var response = await command . ExecuteAsync ( context , args ) ;
117+ var response = await _command . ExecuteAsync ( _context , args ) ;
118118
119119 // Assert
120120 Assert . NotNull ( response ) ;
121- Assert . Null ( response . Results ) ;
121+ Assert . NotNull ( response . Results ) ;
122+
123+ var json = JsonSerializer . Serialize ( response . Results ) ;
124+ var result = JsonSerializer . Deserialize < RoleAssignmentListResult > ( json ) ;
125+
126+ Assert . NotNull ( result ) ;
127+ Assert . NotNull ( result . Assignments ) ;
128+ Assert . Empty ( result . Assignments ) ;
122129 }
123130
124131 [ Fact ]
125132 public async Task ExecuteAsync_HandlesException ( )
126133 {
127134 // Arrange
128135 var expectedError = "Test error" ;
129- var subscriptionId = "00000000-0000-0000-0000-000000000001" ;
130- var scope = $ "/subscriptions/{ subscriptionId } /resourceGroups/rg1";
131136
132- _authorizationService . ListRoleAssignments ( scope , null , Arg . Any < RetryPolicyOptions > ( ) )
137+ _authorizationService . ListRoleAssignments ( Arg . Is ( _knownScope ) , Arg . Any < string > ( ) , Arg . Any < RetryPolicyOptions > ( ) )
133138 . ThrowsAsync ( new Exception ( expectedError ) ) ;
134139
135- var command = new RoleAssignmentListCommand ( _logger ) ;
136- var args = command . GetCommand ( ) . Parse ( [
137- "--subscription" , subscriptionId ,
138- "--scope" , scope
140+ var args = _parser . Parse ( [
141+ "--subscription" , _knownSubscriptionId ,
142+ "--scope" , _knownScope
139143 ] ) ;
140- var context = new CommandContext ( _serviceProvider ) ;
141144
142145 // Act
143- var response = await command . ExecuteAsync ( context , args ) ;
146+ var response = await _command . ExecuteAsync ( _context , args ) ;
144147
145148 // Assert
146149 Assert . NotNull ( response ) ;
0 commit comments