From 26aa96ae379510996a49704c0b56b0538fd4cb93 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 11 Nov 2024 11:12:51 +0100 Subject: [PATCH] Fixed test for non active contexts --- .../contexts/ContextStateTest.java | 169 ++++++++++++++++-- 1 file changed, 158 insertions(+), 11 deletions(-) diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/contexts/ContextStateTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/contexts/ContextStateTest.java index 9ae9dc8..e0c05b7 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/contexts/ContextStateTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/contexts/ContextStateTest.java @@ -13,9 +13,13 @@ import org.gcube.informationsystem.serialization.ElementMapper; import org.gcube.informationsystem.tree.Tree; import org.gcube.resourcemanagement.model.reference.entities.resources.EService; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ContextStateTest extends ContextManagementTest { + private static Logger logger = LoggerFactory.getLogger(ContextManagementTest.class); + public ResourceManagement getResourceManagement(R r) throws Exception { ResourceManagement resourceManagement = new ResourceManagement(); resourceManagement.setElementType(r.getTypeName()); @@ -26,7 +30,7 @@ public class ContextStateTest extends ContextManagementTest { return resourceManagement; } - @Test(expected = ForbiddenException.class) + @Test() public void testNonActiveContext() throws Exception { ContextTest.setContextByName(GCUBE); @@ -46,23 +50,166 @@ public class ContextStateTest extends ContextManagementTest { return; } - FakeSecretUtility.setFakeSecret("noroles.json", rootContext+"/"+contextA1.getName()); + String newContext = rootContext+"/"+contextA1.getName(); - EService eService = ERManagementTest.instantiateValidEService(); - ResourceManagement resourceManagement = getResourceManagement(eService); + boolean resourceCreated = false; + EService createdEservice = null; - String json = null; try { - json = resourceManagement.create(); - }catch (Exception e) { - throw e; - } finally { - if(json!=null) { - resourceManagement.delete(); + FakeSecretUtility.setFakeSecret("noroles.json", newContext); + + EService eService = ERManagementTest.instantiateValidEService(); + ResourceManagement resourceManagement = getResourceManagement(eService); + + String json = null; + try { + json = resourceManagement.create(); + } catch (ForbiddenException e) { + logger.info("As expected the no role user cannot operate in non active context", e); + } catch (Exception e) { + throw e; + } finally { + if(json!=null) { + resourceManagement.delete(); + } + } + + FakeSecretUtility.setFakeSecret("infrastructure-manager.json", newContext); + + try { + json = null; + resourceManagement = getResourceManagement(eService); + json = resourceManagement.create(); + resourceCreated = true; + } catch (Exception e) { + if(json != null) { + resourceManagement.delete(); + } + throw e; + } + + createdEservice = ElementMapper.unmarshal(EService.class, json); + + try { + resourceManagement = getResourceManagement(createdEservice); + json = resourceManagement.read(); + } catch (Exception e) { + throw e; + } + + try { + resourceManagement = getResourceManagement(createdEservice); + json = resourceManagement.update(); + } catch (Exception e) { + throw e; + } + + FakeSecretUtility.setFakeSecret("noroles.json", newContext); + + try { + resourceManagement = getResourceManagement(createdEservice); + json = resourceManagement.read(); + } catch (ForbiddenException e) { + logger.info("As expected the no role user cannot operate in non active context", e); + } catch (Exception e) { + throw e; + } + + try { + resourceManagement = getResourceManagement(createdEservice); + json = resourceManagement.update(); + } catch (ForbiddenException e) { + logger.info("As expected the no role user cannot operate in non active context", e); + } catch (Exception e) { + throw e; + } + + try { + resourceManagement = getResourceManagement(createdEservice); + resourceManagement.delete(); + } catch (ForbiddenException e) { + logger.info("As expected the no role user cannot operate in non active context", e); + } catch (Exception e) { + throw e; + } + + FakeSecretUtility.setFakeSecret("is-manager.json", newContext); + + try { + resourceManagement = getResourceManagement(createdEservice); + json = resourceManagement.read(); + } catch (Exception e) { + throw e; + } + + try { + resourceManagement = getResourceManagement(createdEservice); + json = resourceManagement.update(); + } catch (Exception e) { + throw e; + } + + + FakeSecretUtility.setFakeSecret("noroles.json", newContext); + + try { + resourceManagement = getResourceManagement(createdEservice); + json = resourceManagement.read(); + } catch (ForbiddenException e) { + logger.info("As expected the no role user cannot operate in non active context", e); + } catch (Exception e) { + throw e; + } + + try { + resourceManagement = getResourceManagement(createdEservice); + json = resourceManagement.update(); + } catch (ForbiddenException e) { + logger.info("As expected the no role user cannot operate in non active context", e); + } catch (Exception e) { + throw e; + } + + try { + resourceManagement = getResourceManagement(createdEservice); + resourceManagement.delete(); + } catch (ForbiddenException e) { + logger.info("As expected the no role user cannot operate in non active context", e); + } catch (Exception e) { + throw e; + } + + + FakeSecretUtility.setFakeSecret("is-manager.json", newContext); + + try { + resourceManagement = getResourceManagement(createdEservice); + resourceManagement.delete(); + resourceCreated = false; + } catch (ForbiddenException e) { + logger.info("As expected the no role user cannot operate in non active context", e); + } catch (Exception e) { + throw e; + } + + } finally { + ContextTest.setContextByName(newContext); + + if(resourceCreated) { + ResourceManagement resourceManagement = getResourceManagement(createdEservice); + try { + resourceManagement.delete(); + }catch (Exception e) { + logger.error("Unable to delete resource {}", createdEservice); + } + } + ContextTest.setContextByName(rootContext); + delete(contextA1); } + }