Skip to content

Commit 6abffed

Browse files
feature(#20): this commit fixes the logic of the validation test where clients/realms cannot create permissions reserved by the system
1 parent 9204baf commit 6abffed

1 file changed

Lines changed: 8 additions & 65 deletions

File tree

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

Lines changed: 8 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public async Task WhenPostPermissionsWithReservedSystemNameInNonMasterRealm_Shou
133133

134134
/* arrange: create a new realm */
135135
var realmPayload = _fixture.Build<RealmCreationScheme>()
136-
.With(realm => realm.Name, $"test-realm-{Guid.NewGuid()}")
136+
.With(realm => realm.Name, $"realm-{Guid.NewGuid()}")
137137
.Create();
138138

139139
var realmResponse = await masterClient.PostAsJsonAsync("api/v1/realms", realmPayload);
@@ -142,79 +142,22 @@ public async Task WhenPostPermissionsWithReservedSystemNameInNonMasterRealm_Shou
142142
Assert.NotNull(realm);
143143
Assert.Equal(HttpStatusCode.Created, realmResponse.StatusCode);
144144

145-
/* arrange: create a client scoped to the new realm */
146-
var clientCollection = factory.Services.GetRequiredService<IClientCollection>();
147-
var realmAdminClient = factory.HttpClient
148-
.WithRealmHeader(realm.Name)
145+
/* arrange: use an authenticated identity in the target realm */
146+
var realmClient = factory.HttpClient.WithRealmHeader(realm.Name)
149147
.WithAuthorization(masterAuthenticationResult.AccessToken);
150148

151-
var clientPayload = _fixture.Build<ClientCreationScheme>()
152-
.With(client => client.Name, "nubank")
153-
.With(client => client.Flows, [Grant.ClientCredentials])
154-
.With(client => client.RedirectUris, [])
155-
.Create();
156-
157-
var clientResponse = await realmAdminClient.PostAsJsonAsync("api/v1/clients", clientPayload);
158-
159-
Assert.NotNull(clientResponse);
160-
Assert.Equal(HttpStatusCode.Created, clientResponse.StatusCode);
161-
162-
var clientFilters = ClientFilters.WithSpecifications()
163-
.WithName(clientPayload.Name)
164-
.Build();
165-
166-
var clients = await clientCollection.GetClientsAsync(clientFilters);
167-
var client = clients.FirstOrDefault();
168-
169-
Assert.NotEmpty(clients);
170-
Assert.NotNull(client);
171-
172-
/* arrange: assign CreatePermission to the client using the master-scoped admin client */
173-
var assignPayload = _fixture.Build<AssignClientPermissionScheme>()
174-
.With(assignment => assignment.PermissionName, Permissions.CreatePermission)
175-
.Create();
176-
177-
var assignment = await realmAdminClient.PostAsJsonAsync($"api/v1/clients/{client.Id}/permissions", assignPayload);
178-
179-
Assert.NotNull(assignment);
180-
Assert.Equal(HttpStatusCode.OK, assignment.StatusCode);
181-
182-
/* arrange: authenticate via OAuth 2.0 client_credentials using the created client */
183-
var oauthCredentials = new Dictionary<string, string>
149+
/* act: attempt to create a permission using a reserved system name */
150+
var payload = new PermissionCreationScheme
184151
{
185-
{ "grant_type", "client_credentials" },
186-
{ "client_id", client.ClientId },
187-
{ "client_secret", client.Secret }
152+
Name = Permissions.ViewRealms
188153
};
189154

190-
var oauthContent = new FormUrlEncodedContent(oauthCredentials);
191-
var connectClient = factory.HttpClient;
192-
193-
var oauthResponse = await connectClient.PostAsync("api/v1/protocol/open-id/connect/token", oauthContent);
194-
var oauthResult = await oauthResponse.Content.ReadFromJsonAsync<ClientAuthenticationResult>();
195-
196-
Assert.Equal(HttpStatusCode.OK, oauthResponse.StatusCode);
197-
198-
Assert.NotNull(oauthResult);
199-
Assert.NotEmpty(oauthResult.AccessToken);
200-
201-
var realmClient = factory.HttpClient.WithRealmHeader(realm.Name);
202-
203-
realmClient.WithAuthorization(oauthResult.AccessToken);
204-
205-
/* act: attempt to create a permission using a reserved system name */
206-
var payload = _fixture.Build<PermissionCreationScheme>()
207-
.With(permission => permission.Name, Permissions.ViewRealms)
208-
.Create();
209-
210155
var response = await realmClient.PostAsJsonAsync("api/v1/permissions", payload);
211-
212-
/* assert: response should be 409 Conflict */
213-
Assert.Equal(HttpStatusCode.Conflict, response.StatusCode);
214-
215156
var error = await response.Content.ReadFromJsonAsync<Error>();
216157

158+
/* assert: response should be 409 Conflict */
217159
Assert.NotNull(error);
160+
218161
Assert.Equal(HttpStatusCode.Conflict, response.StatusCode);
219162
Assert.Equal(PermissionErrors.PermissionNameIsReserved, error);
220163
}

0 commit comments

Comments
 (0)