1010using NETCore . Keycloak . Client . Models . Common ;
1111using NETCore . Keycloak . Client . Models . Groups ;
1212using NETCore . Keycloak . Client . Models . KcEnum ;
13+ using NETCore . Keycloak . Client . Models . Organizations ;
1314using NETCore . Keycloak . Client . Models . Roles ;
1415using NETCore . Keycloak . Client . Models . Tokens ;
1516using NETCore . Keycloak . Client . Models . Users ;
@@ -495,6 +496,62 @@ protected async Task<KcGroup> CreateAndGetGroupAsync(string context)
495496 return listGroupsResponse . Response . First ( ) ;
496497 }
497498
499+ /// <summary>
500+ /// Creates a new organization in the Keycloak realm and retrieves its details.
501+ /// This method generates a mock organization, creates it via the Keycloak Admin API,
502+ /// then retrieves the created organization by listing organizations with a matching name filter.
503+ /// </summary>
504+ /// <param name="context">The context name used for managing environment variables.</param>
505+ /// <returns>A task representing the asynchronous operation, with a result of <see cref="KcOrganization"/>.</returns>
506+ protected async Task < KcOrganization > CreateAndGetOrganizationAsync ( string context )
507+ {
508+ // Retrieve an access token for the realm admin to perform the organization creation.
509+ var accessToken = await GetRealmAdminTokenAsync ( context ) . ConfigureAwait ( false ) ;
510+ Assert . IsNotNull ( accessToken ) ;
511+
512+ // Create a faker instance for generating random data.
513+ var faker = new Faker ( ) ;
514+
515+ // Generate a mock organization.
516+ var kcOrganization = KcOrganizationMocks . Generate ( faker ) ;
517+
518+ // Execute the operation to create the organization.
519+ var createOrganizationResponse = await KeycloakRestClient . Organizations . CreateAsync (
520+ TestEnvironment . TestingRealm . Name ,
521+ accessToken . AccessToken ,
522+ kcOrganization ) . ConfigureAwait ( false ) ;
523+
524+ // Validate the response from the organization creation operation.
525+ Assert . IsNotNull ( createOrganizationResponse ) ;
526+ Assert . IsFalse ( createOrganizationResponse . IsError ) ;
527+
528+ // Validate the monitoring metrics for the successful organization creation request.
529+ KcCommonAssertion . AssertResponseMonitoringMetrics ( createOrganizationResponse . MonitoringMetrics ,
530+ HttpStatusCode . Created , HttpMethod . Post ) ;
531+
532+ // Execute the operation to list organizations matching the specified filter criteria.
533+ var listOrganizationsResponse = await KeycloakRestClient . Organizations
534+ . ListAsync ( TestEnvironment . TestingRealm . Name , accessToken . AccessToken , new KcOrganizationFilter
535+ {
536+ Exact = true ,
537+ Search = kcOrganization . Name
538+ } ) . ConfigureAwait ( false ) ;
539+
540+ // Validate the response from the organization listing operation.
541+ Assert . IsNotNull ( listOrganizationsResponse ) ;
542+ Assert . IsFalse ( listOrganizationsResponse . IsError ) ;
543+ Assert . IsNotNull ( listOrganizationsResponse . Response ) ;
544+
545+ // Ensure that the test organization is included in the results.
546+ Assert . IsTrue ( listOrganizationsResponse . Response . Any ( org => org . Name == kcOrganization . Name ) ) ;
547+
548+ // Validate the monitoring metrics for the successful organization listing request.
549+ KcCommonAssertion . AssertResponseMonitoringMetrics ( listOrganizationsResponse . MonitoringMetrics ,
550+ HttpStatusCode . OK , HttpMethod . Get ) ;
551+
552+ return listOrganizationsResponse . Response . First ( ) ;
553+ }
554+
498555 /// <summary>
499556 /// Loads the test environment configuration from the `Assets/testing_environment.json` file.
500557 /// The loaded configuration is deserialized into the <see cref="KcTestEnvironment"/> object.
@@ -508,4 +565,19 @@ protected void LoadConfiguration()
508565
509566 Assert . IsNotNull ( TestEnvironment , "The test environment configuration must not be null." ) ;
510567 }
568+
569+ /// <summary>
570+ /// Gets the major version of the Keycloak server from the test environment configuration.
571+ /// </summary>
572+ /// <returns>The major version number, or 0 if the version is not set.</returns>
573+ protected int GetKcMajorVersion ( )
574+ {
575+ if ( string . IsNullOrWhiteSpace ( TestEnvironment ? . KcVersion ) )
576+ {
577+ return 0 ;
578+ }
579+
580+ var parts = TestEnvironment . KcVersion . Split ( '.' ) ;
581+ return int . TryParse ( parts [ 0 ] , out var major ) ? major : 0 ;
582+ }
511583}
0 commit comments