|
5 | 5 | "errors" |
6 | 6 | "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" |
7 | 7 | "github.com/Azure/azure-sdk-for-go/sdk/azcore/tracing" |
| 8 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization" |
8 | 9 | "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription" |
9 | 10 | mock_providers "github.com/Azure/draft/pkg/providers/mock" |
10 | 11 | "go.uber.org/mock/gomock" |
@@ -243,3 +244,66 @@ func TestGetAppObjectId_EmptyAppIdFromGraphClient(t *testing.T) { |
243 | 244 | t.Errorf("Expected error '%v', got '%v'", expectedError, err) |
244 | 245 | } |
245 | 246 | } |
| 247 | + |
| 248 | +var principalId = "mockPrincipalID" |
| 249 | +var roleDefId = "mockRoleDefinitionID" |
| 250 | +var Id = "mockID" |
| 251 | +var name = "mockName" |
| 252 | +var Idtype = "mocktype" |
| 253 | + |
| 254 | +func TestAssignSpRole(t *testing.T) { |
| 255 | + tests := []struct { |
| 256 | + name string |
| 257 | + expectedError error |
| 258 | + mockResponse armauthorization.RoleAssignmentsClientCreateByIDResponse |
| 259 | + }{ |
| 260 | + { |
| 261 | + name: "Success", |
| 262 | + expectedError: nil, |
| 263 | + mockResponse: armauthorization.RoleAssignmentsClientCreateByIDResponse{ |
| 264 | + RoleAssignment: armauthorization.RoleAssignment{ |
| 265 | + Properties: &armauthorization.RoleAssignmentPropertiesWithScope{ |
| 266 | + PrincipalID: &principalId, |
| 267 | + RoleDefinitionID: &roleDefId, |
| 268 | + }, |
| 269 | + ID: &Id, |
| 270 | + Name: &name, |
| 271 | + Type: &Idtype, |
| 272 | + }, |
| 273 | + }, |
| 274 | + }, |
| 275 | + { |
| 276 | + name: "Error", |
| 277 | + expectedError: errors.New("error"), |
| 278 | + mockResponse: armauthorization.RoleAssignmentsClientCreateByIDResponse{}, |
| 279 | + }, |
| 280 | + { |
| 281 | + name: "ErrorDuringRoleAssignment", |
| 282 | + expectedError: errors.New("error during role assignment"), |
| 283 | + mockResponse: armauthorization.RoleAssignmentsClientCreateByIDResponse{}, |
| 284 | + }, |
| 285 | + } |
| 286 | + |
| 287 | + for _, tt := range tests { |
| 288 | + t.Run(tt.name, func(t *testing.T) { |
| 289 | + ctrl := gomock.NewController(t) |
| 290 | + defer ctrl.Finish() |
| 291 | + |
| 292 | + mockRoleAssignClient := mock_providers.NewMockRoleAssignClient(ctrl) |
| 293 | + |
| 294 | + mockRoleAssignClient.EXPECT().CreateByID(gomock.Any(), "contributor", gomock.Any(), gomock.Any()).Return(tt.mockResponse, tt.expectedError) |
| 295 | + |
| 296 | + sc := &SetUpCmd{ |
| 297 | + AzClient: AzClient{ |
| 298 | + RoleAssignClient: mockRoleAssignClient, |
| 299 | + }, |
| 300 | + spObjectId: "testObjectId", |
| 301 | + } |
| 302 | + |
| 303 | + err := sc.assignSpRole(context.Background()) |
| 304 | + if !errors.Is(err, tt.expectedError) { |
| 305 | + t.Errorf("Expected error: %v, got: %v", tt.expectedError, err) |
| 306 | + } |
| 307 | + }) |
| 308 | + } |
| 309 | +} |
0 commit comments