Skip to content

Commit 734269d

Browse files
feature(#6): this commit introduces new tests to the test suite to ensure that reserved permissions cannot be created
1 parent a4dd64e commit 734269d

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

Tests/Integration/Endpoints/PermissionEndpointTests.cs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,76 @@ public async Task WhenPostPermissionsWithDuplicateName_ShouldReturnConflict()
112112
Assert.Equal(PermissionErrors.PermissionAlreadyExists, error);
113113
}
114114

115+
[Fact(DisplayName = "[e2e] - when POST /permissions in a non-master realm with reserved system name should return 409 #ERROR-7B1E2")]
116+
public async Task WhenPostPermissionsWithReservedSystemNameInNonMasterRealm_ShouldReturnConflict()
117+
{
118+
/* arrange: authenticate in master realm */
119+
var masterClient = factory.HttpClient.WithRealmHeader("master");
120+
var masterCredentials = new AuthenticationCredentials
121+
{
122+
Username = "federation.testing.user",
123+
Password = "federation.testing.password"
124+
};
125+
126+
var masterAuthenticationResponse = await masterClient.PostAsJsonAsync("api/v1/identity/authenticate", masterCredentials);
127+
var masterAuthenticationResult = await masterAuthenticationResponse.Content.ReadFromJsonAsync<AuthenticationResult>();
128+
129+
Assert.NotNull(masterAuthenticationResult);
130+
Assert.NotEmpty(masterAuthenticationResult.AccessToken);
131+
132+
masterClient.WithAuthorization(masterAuthenticationResult.AccessToken);
133+
134+
/* arrange: create a new realm */
135+
var realmPayload = _fixture.Build<RealmCreationScheme>()
136+
.With(realm => realm.Name, $"test-realm-{Guid.NewGuid()}")
137+
.Create();
138+
139+
var realmResponse = await masterClient.PostAsJsonAsync("api/v1/realms", realmPayload);
140+
var realm = await realmResponse.Content.ReadFromJsonAsync<RealmDetailsScheme>();
141+
142+
Assert.NotNull(realm);
143+
Assert.Equal(HttpStatusCode.Created, realmResponse.StatusCode);
144+
145+
/* arrange: authenticate realm via OAuth 2.0 client_credentials */
146+
var oauthCredentials = new Dictionary<string, string>
147+
{
148+
{ "grant_type", "client_credentials" },
149+
{ "client_id", realm.ClientId },
150+
{ "client_secret", realm.ClientSecret }
151+
};
152+
153+
var oauthContent = new FormUrlEncodedContent(oauthCredentials);
154+
var connectClient = factory.HttpClient;
155+
156+
var oauthResponse = await connectClient.PostAsync("api/v1/protocol/open-id/connect/token", oauthContent);
157+
var oauthResult = await oauthResponse.Content.ReadFromJsonAsync<ClientAuthenticationResult>();
158+
159+
Assert.Equal(HttpStatusCode.OK, oauthResponse.StatusCode);
160+
161+
Assert.NotNull(oauthResult);
162+
Assert.NotEmpty(oauthResult.AccessToken);
163+
164+
var realmClient = factory.HttpClient.WithRealmHeader(realm.Name);
165+
166+
realmClient.WithAuthorization(oauthResult.AccessToken);
167+
168+
/* act: attempt to create a permission using a reserved system name */
169+
var payload = _fixture.Build<PermissionCreationScheme>()
170+
.With(permission => permission.Name, Permissions.ViewRealms)
171+
.Create();
172+
173+
var response = await realmClient.PostAsJsonAsync("api/v1/permissions", payload);
174+
175+
/* assert: response should be 409 Conflict */
176+
Assert.Equal(HttpStatusCode.Conflict, response.StatusCode);
177+
178+
var error = await response.Content.ReadFromJsonAsync<Error>();
179+
180+
Assert.NotNull(error);
181+
Assert.Equal(HttpStatusCode.Conflict, response.StatusCode);
182+
Assert.Equal(PermissionErrors.PermissionNameIsReserved, error);
183+
}
184+
115185
[Fact(DisplayName = "[e2e] - when PUT /permissions/{id} with valid data should update permission successfully")]
116186
public async Task WhenPutPermissionsWithValidData_ShouldUpdatePermissionSuccessfully()
117187
{
@@ -160,6 +230,51 @@ public async Task WhenPutPermissionsWithValidData_ShouldUpdatePermissionSuccessf
160230
Assert.Equal(updatePayload.Name, updatedPermission.Name);
161231
}
162232

233+
[Fact(DisplayName = "[e2e] - when PUT /permissions/{id} with reserved system name should return 409 #ERROR-7B1E2")]
234+
public async Task WhenPutPermissionsWithReservedSystemName_ShouldReturnConflict()
235+
{
236+
/* arrange: authenticate user and get access token */
237+
var httpClient = factory.HttpClient.WithRealmHeader("master");
238+
var credentials = new AuthenticationCredentials
239+
{
240+
Username = "federation.testing.user",
241+
Password = "federation.testing.password"
242+
};
243+
244+
var authenticationResponse = await httpClient.PostAsJsonAsync("api/v1/identity/authenticate", credentials);
245+
var authenticationResult = await authenticationResponse.Content.ReadFromJsonAsync<AuthenticationResult>();
246+
247+
Assert.NotNull(authenticationResult);
248+
Assert.NotEmpty(authenticationResult.AccessToken);
249+
250+
httpClient.WithAuthorization(authenticationResult.AccessToken);
251+
252+
/* arrange: create a custom permission */
253+
var createPayload = _fixture.Build<PermissionCreationScheme>()
254+
.With(permission => permission.Name, $"test.permission.{Guid.NewGuid()}")
255+
.Create();
256+
257+
var createResponse = await httpClient.PostAsJsonAsync("api/v1/permissions", createPayload);
258+
var permission = await createResponse.Content.ReadFromJsonAsync<PermissionDetailsScheme>();
259+
260+
Assert.NotNull(permission);
261+
Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);
262+
263+
/* act: attempt to rename it to a reserved system permission */
264+
var updatePayload = _fixture.Build<PermissionUpdateScheme>()
265+
.With(update => update.Name, Permissions.ViewRealms)
266+
.Create();
267+
268+
var response = await httpClient.PutAsJsonAsync($"api/v1/permissions/{permission.Id}", updatePayload);
269+
var error = await response.Content.ReadFromJsonAsync<Error>();
270+
271+
/* assert: response should be 409 Conflict */
272+
Assert.NotNull(error);
273+
274+
Assert.Equal(HttpStatusCode.Conflict, response.StatusCode);
275+
Assert.Equal(PermissionErrors.PermissionNameIsReserved, error);
276+
}
277+
163278
[Fact(DisplayName = "[e2e] - when PUT /permissions/{id} with non-existent permission should return 404 #ERROR-93697")]
164279
public async Task WhenPutPermissionsWithNonExistentPermission_ShouldReturnNotFound()
165280
{

0 commit comments

Comments
 (0)