@@ -476,165 +476,182 @@ public async Task GrantAppRoleAssignmentAsync_WhenResourceSpNotFound_ReturnsFals
476476 {
477477 // Arrange
478478 var ( service , handler ) = CreateServiceWithFakeHandler ( ) ;
479- // SP lookup returns empty value array
480- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
479+ using ( handler )
481480 {
482- Content = new StringContent ( "{\" value\" :[]}" )
483- } ) ;
481+ // SP lookup returns empty value array
482+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
483+ {
484+ Content = new StringContent ( "{\" value\" :[]}" )
485+ } ) ;
484486
485- // Act
486- var result = await service . GrantAppRoleAssignmentAsync (
487- "tenant-id" , "blueprint-sp-id" , "resource-app-id" ,
488- new [ ] { "Agent365.Observability.OtelWrite" } ) ;
487+ // Act
488+ var result = await service . GrantAppRoleAssignmentAsync (
489+ "tenant-id" , "blueprint-sp-id" , "resource-app-id" ,
490+ new [ ] { "Agent365.Observability.OtelWrite" } ) ;
489491
490- // Assert
491- result . Should ( ) . BeFalse ( ) ;
492+ // Assert
493+ result . Should ( ) . BeFalse ( ) ;
494+ }
492495 }
493496
494497 [ Fact ]
495498 public async Task GrantAppRoleAssignmentAsync_WhenRoleNotFoundOnResourceSp_ReturnsFalse ( )
496499 {
497500 // Arrange
498501 var ( service , handler ) = CreateServiceWithFakeHandler ( ) ;
499- // SP lookup succeeds
500- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
501- {
502- Content = new StringContent ( "{\" value\" :[{\" id\" :\" resource-sp-id\" }]}" )
503- } ) ;
504- // Resource SP has no matching app roles
505- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
506- {
507- Content = new StringContent ( "{\" appRoles\" :[]}" )
508- } ) ;
509- // Existing assignments
510- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
502+ using ( handler )
511503 {
512- Content = new StringContent ( "{\" value\" :[]}" )
513- } ) ;
504+ // SP lookup succeeds
505+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
506+ {
507+ Content = new StringContent ( "{\" value\" :[{\" id\" :\" resource-sp-id\" }]}" )
508+ } ) ;
509+ // Resource SP has no matching app roles
510+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
511+ {
512+ Content = new StringContent ( "{\" appRoles\" :[]}" )
513+ } ) ;
514+ // Existing assignments
515+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
516+ {
517+ Content = new StringContent ( "{\" value\" :[]}" )
518+ } ) ;
514519
515- // Act
516- var result = await service . GrantAppRoleAssignmentAsync (
517- "tenant-id" , "blueprint-sp-id" , "resource-app-id" ,
518- new [ ] { "Agent365.Observability.OtelWrite" } ) ;
520+ // Act
521+ var result = await service . GrantAppRoleAssignmentAsync (
522+ "tenant-id" , "blueprint-sp-id" , "resource-app-id" ,
523+ new [ ] { "Agent365.Observability.OtelWrite" } ) ;
519524
520- // Assert
521- result . Should ( ) . BeFalse ( ) ;
525+ // Assert
526+ result . Should ( ) . BeFalse ( ) ;
527+ }
522528 }
523529
524530 [ Fact ]
525531 public async Task GrantAppRoleAssignmentAsync_WhenRoleAlreadyAssigned_ReturnsTrueWithoutPost ( )
526532 {
527533 // Arrange
528534 var ( service , handler ) = CreateServiceWithFakeHandler ( ) ;
529- // SP lookup
530- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
531- {
532- Content = new StringContent ( "{\" value\" :[{\" id\" :\" resource-sp-id\" }]}" )
533- } ) ;
534- // Resource SP app roles
535- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
536- {
537- Content = new StringContent ( "{\" appRoles\" :[{\" value\" :\" Agent365.Observability.OtelWrite\" ,\" id\" :\" role-id-1\" }]}" )
538- } ) ;
539- // Existing assignments — role already assigned
540- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
535+ using ( handler )
541536 {
542- Content = new StringContent ( "{\" value\" :[{\" resourceId\" :\" resource-sp-id\" ,\" appRoleId\" :\" role-id-1\" }]}" )
543- } ) ;
544- // No POST should be queued — if the handler is called a 4th time it returns 404
537+ // SP lookup
538+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
539+ {
540+ Content = new StringContent ( "{\" value\" :[{\" id\" :\" resource-sp-id\" }]}" )
541+ } ) ;
542+ // Resource SP app roles
543+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
544+ {
545+ Content = new StringContent ( "{\" appRoles\" :[{\" value\" :\" Agent365.Observability.OtelWrite\" ,\" id\" :\" role-id-1\" }]}" )
546+ } ) ;
547+ // Existing assignments — role already assigned
548+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
549+ {
550+ Content = new StringContent ( "{\" value\" :[{\" resourceId\" :\" resource-sp-id\" ,\" appRoleId\" :\" role-id-1\" }]}" )
551+ } ) ;
552+ // No POST should be queued — if the handler is called a 4th time it returns 404
545553
546- // Act
547- var result = await service . GrantAppRoleAssignmentAsync (
548- "tenant-id" , "blueprint-sp-id" , "resource-app-id" ,
549- new [ ] { "Agent365.Observability.OtelWrite" } ) ;
554+ // Act
555+ var result = await service . GrantAppRoleAssignmentAsync (
556+ "tenant-id" , "blueprint-sp-id" , "resource-app-id" ,
557+ new [ ] { "Agent365.Observability.OtelWrite" } ) ;
550558
551- // Assert
552- result . Should ( ) . BeTrue ( ) ;
559+ // Assert
560+ result . Should ( ) . BeTrue ( ) ;
561+ }
553562 }
554563
555564 [ Fact ]
556565 public async Task GrantAppRoleAssignmentAsync_WhenPostSucceeds_ReturnsTrue ( )
557566 {
558567 // Arrange
559568 var ( service , handler ) = CreateServiceWithFakeHandler ( ) ;
560- // SP lookup
561- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
562- {
563- Content = new StringContent ( "{\" value\" :[{\" id\" :\" resource-sp-id\" }]}" )
564- } ) ;
565- // Resource SP app roles
566- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
567- {
568- Content = new StringContent ( "{\" appRoles\" :[{\" value\" :\" Agent365.Observability.OtelWrite\" ,\" id\" :\" role-id-1\" }]}" )
569- } ) ;
570- // Existing assignments — none
571- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
572- {
573- Content = new StringContent ( "{\" value\" :[]}" )
574- } ) ;
575- // POST succeeds
576- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . Created )
569+ using ( handler )
577570 {
578- Content = new StringContent ( "{\" id\" :\" assignment-id\" }" )
579- } ) ;
571+ // SP lookup
572+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
573+ {
574+ Content = new StringContent ( "{\" value\" :[{\" id\" :\" resource-sp-id\" }]}" )
575+ } ) ;
576+ // Resource SP app roles
577+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
578+ {
579+ Content = new StringContent ( "{\" appRoles\" :[{\" value\" :\" Agent365.Observability.OtelWrite\" ,\" id\" :\" role-id-1\" }]}" )
580+ } ) ;
581+ // Existing assignments — none
582+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
583+ {
584+ Content = new StringContent ( "{\" value\" :[]}" )
585+ } ) ;
586+ // POST succeeds
587+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . Created )
588+ {
589+ Content = new StringContent ( "{\" id\" :\" assignment-id\" }" )
590+ } ) ;
580591
581- // Act
582- var result = await service . GrantAppRoleAssignmentAsync (
583- "tenant-id" , "blueprint-sp-id" , "resource-app-id" ,
584- new [ ] { "Agent365.Observability.OtelWrite" } ) ;
592+ // Act
593+ var result = await service . GrantAppRoleAssignmentAsync (
594+ "tenant-id" , "blueprint-sp-id" , "resource-app-id" ,
595+ new [ ] { "Agent365.Observability.OtelWrite" } ) ;
585596
586- // Assert
587- result . Should ( ) . BeTrue ( ) ;
597+ // Assert
598+ result . Should ( ) . BeTrue ( ) ;
599+ }
588600 }
589601
590602 [ Fact ]
591603 public async Task GrantAppRoleAssignmentAsync_WhenPostFails_ReturnsFalse ( )
592604 {
593605 // Arrange
594606 var ( service , handler ) = CreateServiceWithFakeHandler ( ) ;
595- // SP lookup
596- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
597- {
598- Content = new StringContent ( "{\" value\" :[{\" id\" :\" resource-sp-id\" }]}" )
599- } ) ;
600- // Resource SP app roles
601- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
602- {
603- Content = new StringContent ( "{\" appRoles\" :[{\" value\" :\" Agent365.Observability.OtelWrite\" ,\" id\" :\" role-id-1\" }]}" )
604- } ) ;
605- // Existing assignments — none
606- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
607- {
608- Content = new StringContent ( "{\" value\" :[]}" )
609- } ) ;
610- // POST fails
611- handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . Forbidden )
607+ using ( handler )
612608 {
613- Content = new StringContent ( "{\" error\" :{\" code\" :\" Authorization_RequestDenied\" }}" )
614- } ) ;
609+ // SP lookup
610+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
611+ {
612+ Content = new StringContent ( "{\" value\" :[{\" id\" :\" resource-sp-id\" }]}" )
613+ } ) ;
614+ // Resource SP app roles
615+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
616+ {
617+ Content = new StringContent ( "{\" appRoles\" :[{\" value\" :\" Agent365.Observability.OtelWrite\" ,\" id\" :\" role-id-1\" }]}" )
618+ } ) ;
619+ // Existing assignments — none
620+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . OK )
621+ {
622+ Content = new StringContent ( "{\" value\" :[]}" )
623+ } ) ;
624+ // POST fails
625+ handler . QueueResponse ( new HttpResponseMessage ( HttpStatusCode . Forbidden )
626+ {
627+ Content = new StringContent ( "{\" error\" :{\" code\" :\" Authorization_RequestDenied\" }}" )
628+ } ) ;
615629
616- // Act
617- var result = await service . GrantAppRoleAssignmentAsync (
618- "tenant-id" , "blueprint-sp-id" , "resource-app-id" ,
619- new [ ] { "Agent365.Observability.OtelWrite" } ) ;
630+ // Act
631+ var result = await service . GrantAppRoleAssignmentAsync (
632+ "tenant-id" , "blueprint-sp-id" , "resource-app-id" ,
633+ new [ ] { "Agent365.Observability.OtelWrite" } ) ;
620634
621- // Assert
622- result . Should ( ) . BeFalse ( ) ;
635+ // Assert
636+ result . Should ( ) . BeFalse ( ) ;
637+ }
623638 }
624639
625640 [ Fact ]
626641 public async Task GrantAppRoleAssignmentAsync_WithEmptyRoleNames_ReturnsTrue ( )
627642 {
628643 // Arrange
629644 var ( service , handler ) = CreateServiceWithFakeHandler ( ) ;
645+ using ( handler )
646+ {
647+ // Act — no HTTP calls should be made for an empty role list
648+ var result = await service . GrantAppRoleAssignmentAsync (
649+ "tenant-id" , "blueprint-sp-id" , "resource-app-id" ,
650+ Array . Empty < string > ( ) ) ;
630651
631- // Act — no HTTP calls should be made for an empty role list
632- var result = await service . GrantAppRoleAssignmentAsync (
633- "tenant-id" , "blueprint-sp-id" , "resource-app-id" ,
634- Array . Empty < string > ( ) ) ;
635-
636- // Assert
637- result . Should ( ) . BeTrue ( ) ;
652+ // Assert
653+ result . Should ( ) . BeTrue ( ) ;
654+ }
638655 }
639656}
640657
0 commit comments