diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/authorization/FakeSecretUtility.java b/src/test/java/org/gcube/informationsystem/resourceregistry/authorization/FakeSecretUtility.java new file mode 100644 index 0000000..e9a1d41 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/authorization/FakeSecretUtility.java @@ -0,0 +1,35 @@ +package org.gcube.informationsystem.resourceregistry.authorization; + +import java.io.File; +import java.net.URL; + +import org.gcube.com.fasterxml.jackson.databind.JsonNode; +import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; +import org.gcube.common.authorization.utils.secret.Secret; +import org.gcube.informationsystem.resourceregistry.ContextTest; +import org.gcube.informationsystem.resourceregistry.queries.JsonQueryTest; + +public class FakeSecretUtility { + + public static File getFakeUsersDirectory() throws Exception { + URL logbackFileURL = JsonQueryTest.class.getClassLoader().getResource("logback-test.xml"); + File logbackFile = new File(logbackFileURL.toURI()); + File resourcesDirectory = logbackFile.getParentFile(); + return new File(resourcesDirectory, "fake-users"); + } + + public static Secret getSecret(String filename, String context) throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + File file = new File(getFakeUsersDirectory(), filename); + JsonNode jsonNode = objectMapper.readTree(file); + String json = objectMapper.writeValueAsString(jsonNode); + FakeSecret secret = new FakeSecret(json, context); + return secret; + } + + public static void setFakeSecret(String filename, String context) throws Exception { + Secret secret = getSecret(filename, context); + ContextTest.set(secret); + } + +} diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/contexts/ContextManagementTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/contexts/ContextManagementTest.java index c6f3c89..5413c80 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/contexts/ContextManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/contexts/ContextManagementTest.java @@ -482,7 +482,7 @@ public class ContextManagementTest extends ContextTest { // List contexts = ServerContextCache.getInstance().getContexts(); List contexts = ServerContextCache.getInstance().getFullInfoContexts(); for(Context context : contexts) { - logger.info("{}", context.getName()); + logger.info("{} {}", context.getName(), context.getID()); List childrenIsParentOf = context.getChildren(); for(IsParentOf childIsParentOf : childrenIsParentOf) { Assert.assertTrue(childIsParentOf.getSource() == context); @@ -502,10 +502,10 @@ public class ContextManagementTest extends ContextTest { logger.debug("{}", context); } - @Ignore - // @Test + //@Ignore + @Test public void deleteContext() throws ResourceRegistryException, IOException { - Context context = read(UUID.fromString("")); + Context context = read(UUID.fromString("5866f58d-fff2-4ae6-ae1a-a16727d44407")); delete(context); } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/contexts/ContextStateTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/contexts/ContextStateTest.java new file mode 100644 index 0000000..9ae9dc8 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/contexts/ContextStateTest.java @@ -0,0 +1,69 @@ +package org.gcube.informationsystem.resourceregistry.contexts; + +import javax.ws.rs.ForbiddenException; + +import org.gcube.informationsystem.contexts.impl.entities.ContextImpl; +import org.gcube.informationsystem.contexts.reference.entities.Context; +import org.gcube.informationsystem.model.reference.entities.Resource; +import org.gcube.informationsystem.resourceregistry.ContextTest; +import org.gcube.informationsystem.resourceregistry.authorization.FakeSecretUtility; +import org.gcube.informationsystem.resourceregistry.instances.ERManagementTest; +import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; +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; + +public class ContextStateTest extends ContextManagementTest { + + public ResourceManagement getResourceManagement(R r) throws Exception { + ResourceManagement resourceManagement = new ResourceManagement(); + resourceManagement.setElementType(r.getTypeName()); + resourceManagement.setJson(ElementMapper.marshal(r)); + if (r.getID() != null) { + resourceManagement.setUUID(r.getID()); + } + return resourceManagement; + } + + @Test(expected = ForbiddenException.class) + public void testNonActiveContext() throws Exception { + ContextTest.setContextByName(GCUBE); + + Tree tree = ServerContextCache.getInstance().getContextsTree(); + Context root = tree.getRootNode().getNodeElement(); + String rootContext = "/" + root.getName(); + ContextTest.setContextByName(rootContext); + + Context contextA1 = new ContextImpl(CTX_NAME_A); + contextA1.setParent(root); + + try { + contextA1 = create(contextA1); + }catch (Exception e) { + ContextTest.setContextByName(rootContext); + delete(contextA1); + return; + } + + FakeSecretUtility.setFakeSecret("noroles.json", rootContext+"/"+contextA1.getName()); + + EService eService = ERManagementTest.instantiateValidEService(); + ResourceManagement resourceManagement = getResourceManagement(eService); + + String json = null; + try { + json = resourceManagement.create(); + }catch (Exception e) { + throw e; + } finally { + if(json!=null) { + resourceManagement.delete(); + } + ContextTest.setContextByName(rootContext); + delete(contextA1); + } + } + + +}