@@ -345,4 +345,167 @@ public async Task WhenGetClientPermissions_ShouldReturnAssignedPermissions()
345345 Assert . Contains ( permissions , permission => permission . Name == permission1 . Name ) ;
346346 Assert . Contains ( permissions , permission => permission . Name == permission2 . Name ) ;
347347 }
348+
349+ [ Fact ( DisplayName = "[e2e] - when POST /clients/{id}/permissions with valid permission should assign permission successfully" ) ]
350+ public async Task WhenPostClientPermissionsWithValidPermission_ShouldAssignPermissionSuccessfully ( )
351+ {
352+ /* arrange: resolve required dependencies */
353+ var clientCollection = factory . Services . GetRequiredService < IClientCollection > ( ) ;
354+ var permissionCollection = factory . Services . GetRequiredService < IPermissionCollection > ( ) ;
355+
356+ /* arrange: authenticate user and get access token */
357+ var httpClient = factory . HttpClient . WithRealmHeader ( "master" ) ;
358+ var credentials = new AuthenticationCredentials
359+ {
360+ Username = "federation.testing.user" ,
361+ Password = "federation.testing.password"
362+ } ;
363+
364+ var authenticationResponse = await httpClient . PostAsJsonAsync ( "api/v1/identity/authenticate" , credentials ) ;
365+ var authenticationResult = await authenticationResponse . Content . ReadFromJsonAsync < AuthenticationResult > ( ) ;
366+
367+ Assert . NotNull ( authenticationResult ) ;
368+ Assert . NotEmpty ( authenticationResult . AccessToken ) ;
369+
370+ httpClient . WithAuthorization ( authenticationResult . AccessToken ) ;
371+
372+ /* arrange: create a new client */
373+ var clientPayload = _fixture . Build < ClientCreationScheme > ( )
374+ . With ( client => client . Name , $ "test-client-{ Guid . NewGuid ( ) } ")
375+ . With ( client => client . Flows , [ Grant . ClientCredentials ] )
376+ . With ( client => client . RedirectUris , [ ] )
377+ . Create ( ) ;
378+
379+ var clientResponse = await httpClient . PostAsJsonAsync ( "api/v1/clients" , clientPayload ) ;
380+
381+ Assert . Equal ( HttpStatusCode . Created , clientResponse . StatusCode ) ;
382+
383+ var clientFilters = ClientFilters . WithSpecifications ( )
384+ . WithName ( clientPayload . Name )
385+ . Build ( ) ;
386+
387+ var clients = await clientCollection . GetClientsAsync ( clientFilters , CancellationToken . None ) ;
388+ var client = clients . FirstOrDefault ( ) ;
389+
390+ Assert . NotEmpty ( clients ) ;
391+ Assert . NotNull ( client ) ;
392+
393+ /* arrange: create a new permission */
394+ var permissionPayload = _fixture . Build < PermissionCreationScheme > ( )
395+ . With ( permission => permission . Name , $ "test.permission.{ Guid . NewGuid ( ) } ")
396+ . Create ( ) ;
397+
398+ var permissionResponse = await httpClient . PostAsJsonAsync ( "api/v1/permissions" , permissionPayload ) ;
399+
400+ Assert . Equal ( HttpStatusCode . Created , permissionResponse . StatusCode ) ;
401+
402+ var permissionFilters = PermissionFilters . WithSpecifications ( )
403+ . WithName ( permissionPayload . Name )
404+ . Build ( ) ;
405+
406+ var permissions = await permissionCollection . GetPermissionsAsync ( permissionFilters , CancellationToken . None ) ;
407+ var permission = permissions . FirstOrDefault ( ) ;
408+
409+ Assert . NotEmpty ( permissions ) ;
410+ Assert . NotNull ( permission ) ;
411+
412+ /* arrange: prepare request to assign permission to client */
413+ var payload = _fixture . Build < AssignClientPermissionScheme > ( )
414+ . With ( assignment => assignment . PermissionName , permission . Name )
415+ . Create ( ) ;
416+
417+ /* act: send POST request to assign permission to client */
418+ var response = await httpClient . PostAsJsonAsync ( $ "api/v1/clients/{ client . Id } /permissions", payload ) ;
419+ var assignedPermissions = await response . Content . ReadFromJsonAsync < IReadOnlyCollection < PermissionDetailsScheme > > ( ) ;
420+
421+ /* assert: response should be 200 OK and permissions list should be returned */
422+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
423+ Assert . NotNull ( assignedPermissions ) ;
424+
425+ /* assert: the assigned permission should be in the returned list */
426+ Assert . Contains ( assignedPermissions , current => current . Name == permission . Name ) ;
427+ }
428+
429+ [ Fact ( DisplayName = "[e2e] - when POST /clients/{id}/permissions with duplicate permission should return 409 #ERROR-8D71B" ) ]
430+ public async Task WhenPostClientPermissionsWithDuplicatePermission_ShouldReturnConflict ( )
431+ {
432+ /* arrange: resolve required dependencies */
433+ var clientCollection = factory . Services . GetRequiredService < IClientCollection > ( ) ;
434+ var permissionCollection = factory . Services . GetRequiredService < IPermissionCollection > ( ) ;
435+
436+ /* arrange: authenticate user and get access token */
437+ var httpClient = factory . HttpClient . WithRealmHeader ( "master" ) ;
438+ var credentials = new AuthenticationCredentials
439+ {
440+ Username = "federation.testing.user" ,
441+ Password = "federation.testing.password"
442+ } ;
443+
444+ var authenticationResponse = await httpClient . PostAsJsonAsync ( "api/v1/identity/authenticate" , credentials ) ;
445+ var authenticationResult = await authenticationResponse . Content . ReadFromJsonAsync < AuthenticationResult > ( ) ;
446+
447+ Assert . NotNull ( authenticationResult ) ;
448+ Assert . NotEmpty ( authenticationResult . AccessToken ) ;
449+
450+ httpClient . WithAuthorization ( authenticationResult . AccessToken ) ;
451+
452+ /* arrange: create a new client */
453+ var clientPayload = _fixture . Build < ClientCreationScheme > ( )
454+ . With ( client => client . Name , $ "test-client-{ Guid . NewGuid ( ) } ")
455+ . With ( client => client . Flows , [ Grant . ClientCredentials ] )
456+ . With ( client => client . RedirectUris , [ ] )
457+ . Create ( ) ;
458+
459+ var clientResponse = await httpClient . PostAsJsonAsync ( "api/v1/clients" , clientPayload ) ;
460+
461+ Assert . Equal ( HttpStatusCode . Created , clientResponse . StatusCode ) ;
462+
463+ var clientFilters = ClientFilters . WithSpecifications ( )
464+ . WithName ( clientPayload . Name )
465+ . Build ( ) ;
466+
467+ var clients = await clientCollection . GetClientsAsync ( clientFilters , CancellationToken . None ) ;
468+ var client = clients . FirstOrDefault ( ) ;
469+
470+ Assert . NotEmpty ( clients ) ;
471+ Assert . NotNull ( client ) ;
472+
473+ /* arrange: create a new permission */
474+ var permissionPayload = _fixture . Build < PermissionCreationScheme > ( )
475+ . With ( permission => permission . Name , $ "test.permission.{ Guid . NewGuid ( ) } ")
476+ . Create ( ) ;
477+
478+ var permissionResponse = await httpClient . PostAsJsonAsync ( "api/v1/permissions" , permissionPayload ) ;
479+
480+ Assert . Equal ( HttpStatusCode . Created , permissionResponse . StatusCode ) ;
481+
482+ var permissionFilters = PermissionFilters . WithSpecifications ( )
483+ . WithName ( permissionPayload . Name )
484+ . Build ( ) ;
485+
486+ var permissions = await permissionCollection . GetPermissionsAsync ( permissionFilters , CancellationToken . None ) ;
487+ var permission = permissions . FirstOrDefault ( ) ;
488+
489+ Assert . NotEmpty ( permissions ) ;
490+ Assert . NotNull ( permission ) ;
491+
492+ /* arrange: assign permission to client first time */
493+ var payload = _fixture . Build < AssignClientPermissionScheme > ( )
494+ . With ( assignment => assignment . PermissionName , permission . Name )
495+ . Create ( ) ;
496+
497+ var firstResponse = await httpClient . PostAsJsonAsync ( $ "api/v1/clients/{ client . Id } /permissions", payload ) ;
498+
499+ Assert . Equal ( HttpStatusCode . OK , firstResponse . StatusCode ) ;
500+
501+ /* act: attempt to assign the same permission again */
502+ var secondResponse = await httpClient . PostAsJsonAsync ( $ "api/v1/clients/{ client . Id } /permissions", payload ) ;
503+ var error = await secondResponse . Content . ReadFromJsonAsync < Error > ( ) ;
504+
505+ /* assert: response should be 409 Conflict */
506+ Assert . NotNull ( error ) ;
507+
508+ Assert . Equal ( HttpStatusCode . Conflict , secondResponse . StatusCode ) ;
509+ Assert . Equal ( ClientErrors . ClientAlreadyHasPermission , error ) ;
510+ }
348511}
0 commit comments