Skip to content

Commit 0d91eb9

Browse files
committed
Coverage for import and migrate providers
1 parent 4fab819 commit 0d91eb9

File tree

7 files changed

+284
-20
lines changed

7 files changed

+284
-20
lines changed

server/src/main/java/access/api/ApplicationController.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@
6060
import static access.SwaggerOpenIdConfig.API_TOKENS_SCHEME_NAME;
6161
import static access.SwaggerOpenIdConfig.OPEN_ID_SCHEME_NAME;
6262
import static access.api.Results.deleteResult;
63-
import static access.manage.ManageData.getData;
64-
import static access.manage.ManageData.getMetaDataFields;
63+
import static access.manage.ManageData.*;
6564

6665
@RestController
6766
@RequestMapping(value = {"/api/v1/applications"}, produces = MediaType.APPLICATION_JSON_VALUE)
@@ -296,6 +295,7 @@ public ResponseEntity<List<Map<String, Object>>> identityProvidersByAllowedConne
296295
}
297296

298297
@PutMapping({"/migrate"})
298+
@Transactional
299299
public ResponseEntity<Map<String, Object>> migrate(User user, @Validated @RequestBody MigrateApplicationRequest migrateApplicationRequest) {
300300
LOG.debug("/migrate application by " + user.getEmail());
301301

@@ -307,7 +307,10 @@ public ResponseEntity<Map<String, Object>> migrate(User user, @Validated @Reques
307307
.orElseThrow(() -> new NotFoundException("Organization not found"));
308308
application.setOrganization(organization);
309309
applicationRepository.save(application);
310-
application.getConnections().forEach(connection -> {
310+
application.getConnections()
311+
.stream()
312+
.filter(connection -> !isEmpty(connection.getManageIdentifier()))
313+
.forEach(connection -> {
311314
Map<String, Object> provider = manage.providerByConnection(connection);
312315
Map<String, Object> metaDataFields = getMetaDataFields(getData(provider));
313316
metaDataFields.put("OrganizationName:en", organization.getName());

server/src/main/java/access/api/ConnectionController.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646

4747
import java.time.Instant;
4848
import java.util.Collections;
49-
import java.util.HashMap;
5049
import java.util.List;
5150
import java.util.Map;
5251
import java.util.Optional;
@@ -335,15 +334,15 @@ private Connection productionReadyChangeRequests(Connection connection, User use
335334
newPathUpdates.forEach((key, value) -> {
336335
if (key.equals("arp") && existingPathUpdates.containsKey(key)) {
337336
//three way merge on the attributes and profile, motivation from the latest change
338-
Map<String, Object> attributes = (Map<String, Object>) ((Map<String, Object>)value).get("attributes");
337+
Map<String, Object> attributes = (Map<String, Object>) ((Map<String, Object>) value).getOrDefault("attributes", Map.of());
339338
List<String> attibuteNames = attributes.keySet().stream().toList();
340339

341-
Map<String, Object> arpPath = (Map<String, Object>) existingPathUpdates.get("arp");
342-
Map<String, Object> pathAttributes = (Map<String, Object>) arpPath.get("attributes");
340+
Map<String, Object> arpPath = (Map<String, Object>) existingPathUpdates.getOrDefault("arp", Map.of());
341+
Map<String, Object> pathAttributes = (Map<String, Object>) arpPath.getOrDefault("attributes", Map.of());
343342
List<String> pathValues = pathAttributes.keySet().stream().toList();
344343

345-
Map<String, Object> baseArp= (Map<String, Object>) getData(provider).get("arp");
346-
Map<String, Object> baseAttributes = (Map<String, Object>) baseArp.get("attributes");
344+
Map<String, Object> baseArp = (Map<String, Object>) getData(provider).getOrDefault("arp", Map.of());
345+
Map<String, Object> baseAttributes = (Map<String, Object>) baseArp.getOrDefault("attributes", Map.of());
347346
List<String> baseValues = baseAttributes.keySet().stream().toList();
348347

349348
List<String> newValues = ListMerger.threeWayMerge(baseValues, pathValues, attibuteNames);
@@ -352,7 +351,7 @@ private Connection productionReadyChangeRequests(Connection connection, User use
352351
attrName -> attrName,
353352
attrName -> attributes.getOrDefault(attrName, pathAttributes.getOrDefault(attrName, baseAttributes.get(attrName)))));
354353
arpPath.put("attributes", newAttributes);
355-
} else if (value instanceof List && existingPathUpdates.containsKey(key)) {
354+
} else if (value instanceof List && existingPathUpdates.containsKey(key)) {
356355
//three way merge
357356
List<String> pathUpdateValue = (List<String>) existingPathUpdates.get(key);
358357
List<String> base = (List<String>) getMetaDataFields(getData(provider)).getOrDefault(key.substring(key.indexOf(".") + 1), List.of());

server/src/main/resources/manage/oidc10_rp.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"allowedall": true,
8181
"arp": {
8282
"enabled": true,
83+
"profile": "personalized",
8384
"attributes": {
8485
"urn:mace:dir:attribute-def:cn": [
8586
{

server/src/test/java/access/AbstractTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,15 @@ protected void stubForServiceProviders() {
573573
.willReturn(aResponse().withHeader("Content-Type", "application/json")
574574
.withBody(body)
575575
.withStatus(200)));
576+
}
576577

578+
protected Connection connection(EntityType entityType, String manageIdentifier) {
579+
Connection connection = new Connection();
580+
connection.setManageIdentifier(manageIdentifier);
581+
connection.setProtocol(entityType);
582+
connection.setEnvironment(Environment.PROD);
583+
connection.setState(State.prodaccepted);
584+
return connection;
577585
}
578586

579587
private void doSeed() {

server/src/test/java/access/api/ApplicationControllerTest.java

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
import access.model.ConnectionStatus;
1010
import access.model.EntityType;
1111
import access.model.Environment;
12+
import access.model.ImportEntityRequest;
13+
import access.model.MigrateApplicationRequest;
1214
import access.model.Organization;
15+
import com.fasterxml.jackson.core.JsonProcessingException;
1316
import com.fasterxml.jackson.core.type.TypeReference;
1417
import io.restassured.common.mapper.TypeRef;
1518
import io.restassured.http.ContentType;
@@ -239,4 +242,170 @@ void delete() {
239242
Optional<Application> optionalApplication = applicationRepository.findById(applicationId);
240243
assertFalse(optionalApplication.isPresent());
241244
}
245+
246+
@Test
247+
void findAll() {
248+
AccessCookieFilter accessCookieFilter = mockLoginFlow(SUPER_SUB);
249+
List<Map<String, Object>> applications = given()
250+
.when()
251+
.filter(accessCookieFilter.cookieFilter())
252+
.header(csrfHeader(accessCookieFilter))
253+
.accept(ContentType.JSON)
254+
.contentType(ContentType.JSON)
255+
.get("/api/v1/applications/all")
256+
.as(new TypeRef<>() {
257+
});
258+
259+
assertEquals(3, applications.size());
260+
}
261+
262+
@Test
263+
void allLightByOrganization() {
264+
AccessCookieFilter accessCookieFilter = mockLoginFlow(SUPER_SUB);
265+
Long organizationId = seedIdentifiers.get(SHARE_LOGICS);
266+
List<Map<String, Object>> applications = given()
267+
.when()
268+
.filter(accessCookieFilter.cookieFilter())
269+
.header(csrfHeader(accessCookieFilter))
270+
.accept(ContentType.JSON)
271+
.contentType(ContentType.JSON)
272+
.pathParam("organizationId", organizationId)
273+
.get("/api/v1/applications/all/light/{organizationId}")
274+
.as(new TypeRef<>() {
275+
});
276+
277+
assertEquals(2, applications.size());
278+
}
279+
280+
@Test
281+
void identityProvidersByAllowedConnections() throws JsonProcessingException {
282+
AccessCookieFilter accessCookieFilter = mockLoginFlow(SUPER_SUB);
283+
Long applicationId = seedIdentifiers.get(BUDDY_CHECK);
284+
285+
List<Connection> connections = List.of(
286+
connection(EntityType.saml20_sp, "4"),
287+
connection(EntityType.oidc10_rp, "5")
288+
);
289+
List<Map<String, Object>> identityProviders = localManage.identityProvidersByAllowedConnections(connections);
290+
String body = objectMapper.writeValueAsString(identityProviders);
291+
stubFor(post(urlEqualTo("/manage/api/internal/delete-consequences")).willReturn(aResponse()
292+
.withHeader("Content-Type", "application/json")
293+
.withBody(body)));
294+
295+
List<Map<String, Object>> providers = given()
296+
.when()
297+
.filter(accessCookieFilter.cookieFilter())
298+
.header(csrfHeader(accessCookieFilter))
299+
.accept(ContentType.JSON)
300+
.contentType(ContentType.JSON)
301+
.pathParam("applicationId", applicationId)
302+
.get("/api/v1/applications/identity-providers-allowed-connections/{applicationId}")
303+
.as(new TypeRef<>() {
304+
});
305+
306+
assertEquals(2, providers.size());
307+
}
308+
309+
@Test
310+
void identityProvidersByAllowedConnectionsTestConnection() {
311+
AccessCookieFilter accessCookieFilter = mockLoginFlow(SUPER_SUB);
312+
Long applicationId = seedIdentifiers.get(NITRO_MAP);
313+
List<Map<String, Object>> providers = given()
314+
.when()
315+
.filter(accessCookieFilter.cookieFilter())
316+
.header(csrfHeader(accessCookieFilter))
317+
.accept(ContentType.JSON)
318+
.contentType(ContentType.JSON)
319+
.pathParam("applicationId", applicationId)
320+
.get("/api/v1/applications/identity-providers-allowed-connections/{applicationId}")
321+
.as(new TypeRef<>() {
322+
});
323+
324+
assertEquals(0, providers.size());
325+
}
326+
327+
@Test
328+
void migrate() {
329+
AccessCookieFilter accessCookieFilter = mockLoginFlow(SUPER_SUB);
330+
Application applicationBuddyCheck = applicationRepository.findDetailsById(seedIdentifiers.get(BUDDY_CHECK)).get();
331+
assertEquals(SHARE_LOGICS, applicationBuddyCheck.getOrganization().getName());
332+
333+
MigrateApplicationRequest migrateApplicationRequest = new MigrateApplicationRequest(
334+
seedIdentifiers.get(BUDDY_CHECK),
335+
seedIdentifiers.get(FAR_WIND)
336+
);
337+
stubForGetProvider(EntityType.oidc10_rp, MANAGE_IDENTIFIER, Environment.PROD, "5");
338+
Connection connectionProd = connectionRepository.findById(seedIdentifiers.get(BUDDY_CHECK_PROD)).get();
339+
connectionProd.setManageIdentifier("5");
340+
super.stubForSaveProvider(connectionProd);
341+
342+
given()
343+
.when()
344+
.filter(accessCookieFilter.cookieFilter())
345+
.header(csrfHeader(accessCookieFilter))
346+
.accept(ContentType.JSON)
347+
.contentType(ContentType.JSON)
348+
.body(migrateApplicationRequest)
349+
.put("/api/v1/applications/migrate")
350+
.then()
351+
.statusCode(HttpStatus.OK.value());
352+
353+
applicationBuddyCheck = applicationRepository.findDetailsById(seedIdentifiers.get(BUDDY_CHECK)).get();
354+
assertEquals(FAR_WIND, applicationBuddyCheck.getOrganization().getName());
355+
356+
}
357+
358+
@Test
359+
void importEntity() {
360+
AccessCookieFilter accessCookieFilter = mockLoginFlow(SUPER_SUB);
361+
362+
Map<String, Object> provider = localManage.providerByManageIdentifier(EntityType.oidc10_rp, "10", Environment.PROD);
363+
ImportEntityRequest importEntityRequest = new ImportEntityRequest(
364+
seedIdentifiers.get(FAR_WIND),
365+
null,
366+
provider
367+
);
368+
super.stubForSaveProvider(connection(EntityType.oidc10_rp, "5"));
369+
given()
370+
.when()
371+
.filter(accessCookieFilter.cookieFilter())
372+
.header(csrfHeader(accessCookieFilter))
373+
.accept(ContentType.JSON)
374+
.contentType(ContentType.JSON)
375+
.body(importEntityRequest)
376+
.post("/api/v1/applications/import")
377+
.then()
378+
.statusCode(HttpStatus.OK.value());
379+
380+
Organization organization = organizationRepository.findApplicationsDetailsOrganizationById(seedIdentifiers.get(FAR_WIND)).get();
381+
//See src/main/resources/manage/oidc10_rp.json id="10"
382+
Application application = organization.getApplications().stream()
383+
.filter(app -> app.getName().equals("OIDC Playground Client")).findFirst().get();
384+
assertEquals(1, application.getConnections().size());
385+
}
386+
387+
@Test
388+
void importEntityExistingApplication() {
389+
AccessCookieFilter accessCookieFilter = mockLoginFlow(SUPER_SUB);
390+
391+
Map<String, Object> provider = localManage.providerByManageIdentifier(EntityType.oidc10_rp, "10", Environment.PROD);
392+
ImportEntityRequest importEntityRequest = new ImportEntityRequest(
393+
seedIdentifiers.get(FAR_WIND),
394+
seedIdentifiers.get(NITRO_MAP),
395+
provider
396+
);
397+
super.stubForSaveProvider(connection(EntityType.oidc10_rp, "10"));
398+
given()
399+
.when()
400+
.filter(accessCookieFilter.cookieFilter())
401+
.header(csrfHeader(accessCookieFilter))
402+
.accept(ContentType.JSON)
403+
.contentType(ContentType.JSON)
404+
.body(importEntityRequest)
405+
.post("/api/v1/applications/import")
406+
.then()
407+
.statusCode(HttpStatus.OK.value());
408+
Application application = applicationRepository.findDetailsById(seedIdentifiers.get(NITRO_MAP)).get();
409+
assertEquals(1, application.getConnections().size());
410+
}
242411
}

0 commit comments

Comments
 (0)