Skip to content

Commit 8e18f8e

Browse files
feature(#22): this commit includes four integration tests for the realm secrets endpoints, covering both success and error scenarios. It also adds a global using statement for Payloads.Secret in `Us
1 parent df8d15e commit 8e18f8e

2 files changed

Lines changed: 167 additions & 0 deletions

File tree

Applications/Backend/Tests/Integration/Endpoints/RealmEndpointTests.cs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,170 @@ public async Task WhenDeleteRealmPermissionWithNonExistentPermission_ShouldRetur
447447
Assert.Equal(HttpStatusCode.NotFound, httpResponse.StatusCode);
448448
Assert.Equal(PermissionErrors.PermissionDoesNotExist, error);
449449
}
450+
451+
[Fact(DisplayName = "[e2e] - when GET /realms/{id}/secrets should return realm's active secrets")]
452+
public async Task WhenGetRealmSecrets_ShouldReturnActiveSecrets()
453+
{
454+
/* arrange: authenticate user and get access token */
455+
var httpClient = factory.HttpClient.WithRealmHeader("master");
456+
var credentials = new AuthenticationCredentials
457+
{
458+
Username = "federation.testing.user",
459+
Password = "federation.testing.password"
460+
};
461+
462+
var authenticationResponse = await httpClient.PostAsJsonAsync("api/v1/identity/authenticate", credentials);
463+
var authenticationResult = await authenticationResponse.Content.ReadFromJsonAsync<AuthenticationResult>();
464+
465+
Assert.NotNull(authenticationResult);
466+
Assert.NotEmpty(authenticationResult.AccessToken);
467+
468+
httpClient.WithAuthorization(authenticationResult.AccessToken);
469+
470+
/* arrange: create a new realm */
471+
var realmPayload = _fixture.Build<RealmCreationScheme>()
472+
.With(realm => realm.Name, $"test-realm-{Guid.NewGuid()}")
473+
.Create();
474+
475+
var realmResponse = await httpClient.PostAsJsonAsync("api/v1/realms", realmPayload);
476+
var realm = await realmResponse.Content.ReadFromJsonAsync<RealmDetailsScheme>();
477+
478+
Assert.NotNull(realm);
479+
Assert.Equal(HttpStatusCode.Created, realmResponse.StatusCode);
480+
481+
/* act: send GET request to retrieve realm's secrets */
482+
var getResponse = await httpClient.GetAsync($"api/v1/realms/{realm.Id}/secrets");
483+
var secrets = await getResponse.Content.ReadFromJsonAsync<IReadOnlyCollection<SecretScheme>>();
484+
485+
/* assert: response should be 200 OK */
486+
Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
487+
Assert.NotNull(secrets);
488+
489+
/* assert: should have at least one active secret */
490+
Assert.NotEmpty(secrets);
491+
492+
/* assert: verify secret structure (no private/public key values) */
493+
foreach (var secret in secrets)
494+
{
495+
Assert.NotNull(secret.Id);
496+
Assert.True(secret.CreatedAt != default);
497+
}
498+
}
499+
500+
[Fact(DisplayName = "[e2e] - when GET /realms/{id}/secrets with non-existent realm should return 404 #ERROR-2FB9A")]
501+
public async Task WhenGetRealmSecretsWithNonExistentRealm_ShouldReturnNotFound()
502+
{
503+
/* arrange: authenticate user and get access token */
504+
var httpClient = factory.HttpClient.WithRealmHeader("master");
505+
var credentials = new AuthenticationCredentials
506+
{
507+
Username = "federation.testing.user",
508+
Password = "federation.testing.password"
509+
};
510+
511+
var authenticationResponse = await httpClient.PostAsJsonAsync("api/v1/identity/authenticate", credentials);
512+
var authenticationResult = await authenticationResponse.Content.ReadFromJsonAsync<AuthenticationResult>();
513+
514+
Assert.NotNull(authenticationResult);
515+
Assert.NotEmpty(authenticationResult.AccessToken);
516+
517+
httpClient.WithAuthorization(authenticationResult.AccessToken);
518+
519+
/* arrange: prepare request with a non-existent realm ID */
520+
var nonExistentRealmId = Guid.NewGuid().ToString();
521+
522+
/* act: send GET request for non-existent realm's secrets */
523+
var response = await httpClient.GetAsync($"api/v1/realms/{nonExistentRealmId}/secrets");
524+
var error = await response.Content.ReadFromJsonAsync<Error>();
525+
526+
/* assert: response should be 404 Not Found */
527+
Assert.NotNull(error);
528+
529+
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
530+
Assert.Equal(RealmErrors.RealmDoesNotExist.Code, error.Code);
531+
}
532+
533+
[Fact(DisplayName = "[e2e] - when POST /realms/{id}/secrets/rotate should rotate secrets successfully")]
534+
public async Task WhenPostRealmSecretsRotate_ShouldRotateSecretsSuccessfully()
535+
{
536+
/* arrange: authenticate user and get access token */
537+
var httpClient = factory.HttpClient.WithRealmHeader("master");
538+
var credentials = new AuthenticationCredentials
539+
{
540+
Username = "federation.testing.user",
541+
Password = "federation.testing.password"
542+
};
543+
544+
var authenticationResponse = await httpClient.PostAsJsonAsync("api/v1/identity/authenticate", credentials);
545+
var authenticationResult = await authenticationResponse.Content.ReadFromJsonAsync<AuthenticationResult>();
546+
547+
Assert.NotNull(authenticationResult);
548+
Assert.NotEmpty(authenticationResult.AccessToken);
549+
550+
httpClient.WithAuthorization(authenticationResult.AccessToken);
551+
552+
/* arrange: create a new realm */
553+
var realmPayload = _fixture.Build<RealmCreationScheme>()
554+
.With(realm => realm.Name, $"test-realm-{Guid.NewGuid()}")
555+
.Create();
556+
557+
var realmResponse = await httpClient.PostAsJsonAsync("api/v1/realms", realmPayload);
558+
var realm = await realmResponse.Content.ReadFromJsonAsync<RealmDetailsScheme>();
559+
560+
Assert.NotNull(realm);
561+
Assert.Equal(HttpStatusCode.Created, realmResponse.StatusCode);
562+
563+
/* arrange: get secrets before rotation */
564+
var getBeforeResponse = await httpClient.GetAsync($"api/v1/realms/{realm.Id}/secrets");
565+
var secretsBefore = await getBeforeResponse.Content.ReadFromJsonAsync<IReadOnlyCollection<SecretScheme>>();
566+
567+
Assert.NotNull(secretsBefore);
568+
var initialSecretCount = secretsBefore.Count;
569+
570+
/* act: send POST request to rotate secrets */
571+
var rotateResponse = await httpClient.PostAsJsonAsync($"api/v1/realms/{realm.Id}/secrets/rotate", new { });
572+
573+
/* assert: response should be 204 No Content */
574+
Assert.Equal(HttpStatusCode.NoContent, rotateResponse.StatusCode);
575+
576+
/* assert: verify new secret was created */
577+
var getAfterResponse = await httpClient.GetAsync($"api/v1/realms/{realm.Id}/secrets");
578+
var secretsAfter = await getAfterResponse.Content.ReadFromJsonAsync<IReadOnlyCollection<SecretScheme>>();
579+
580+
Assert.NotNull(secretsAfter);
581+
Assert.True(secretsAfter.Count >= initialSecretCount, "New secret should be created after rotation");
582+
}
583+
584+
[Fact(DisplayName = "[e2e] - when POST /realms/{id}/secrets/rotate with non-existent realm should return 404 #ERROR-2FB9A")]
585+
public async Task WhenPostRealmSecretsRotateWithNonExistentRealm_ShouldReturnNotFound()
586+
{
587+
/* arrange: authenticate user and get access token */
588+
var httpClient = factory.HttpClient.WithRealmHeader("master");
589+
var credentials = new AuthenticationCredentials
590+
{
591+
Username = "federation.testing.user",
592+
Password = "federation.testing.password"
593+
};
594+
595+
var authenticationResponse = await httpClient.PostAsJsonAsync("api/v1/identity/authenticate", credentials);
596+
var authenticationResult = await authenticationResponse.Content.ReadFromJsonAsync<AuthenticationResult>();
597+
598+
Assert.NotNull(authenticationResult);
599+
Assert.NotEmpty(authenticationResult.AccessToken);
600+
601+
httpClient.WithAuthorization(authenticationResult.AccessToken);
602+
603+
/* arrange: prepare request with a non-existent realm ID */
604+
var nonExistentRealmId = Guid.NewGuid().ToString();
605+
606+
/* act: send POST request to rotate secrets for non-existent realm */
607+
var response = await httpClient.PostAsJsonAsync($"api/v1/realms/{nonExistentRealmId}/secrets/rotate", new { });
608+
var error = await response.Content.ReadFromJsonAsync<Error>();
609+
610+
/* assert: response should be 404 Not Found */
611+
Assert.NotNull(error);
612+
613+
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
614+
Assert.Equal(RealmErrors.RealmDoesNotExist, error);
615+
}
450616
}

Applications/Backend/Tests/Usings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
global using HttpsRichardy.Federation.Application.Payloads.Realm;
3030
global using HttpsRichardy.Federation.Application.Payloads.Permission;
3131
global using HttpsRichardy.Federation.Application.Payloads.Group;
32+
global using HttpsRichardy.Federation.Application.Payloads.Secret;
3233
global using HttpsRichardy.Federation.Application.Payloads.Common;
3334
global using HttpsRichardy.Federation.Application.Payloads.Connect;
3435
global using HttpsRichardy.Federation.Application.Payloads.Client;

0 commit comments

Comments
 (0)