Migrating tests to new SecretManagerProvider
This commit is contained in:
parent
a5eda58293
commit
3359295adf
5
pom.xml
5
pom.xml
|
@ -50,6 +50,11 @@
|
|||
<artifactId>information-system-model</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>authorization-utils</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.orientechnologies</groupId>
|
||||
<!--
|
||||
|
|
|
@ -7,14 +7,11 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.gcube.common.authorization.client.Constants;
|
||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
||||
import org.gcube.common.authorization.library.provider.ClientInfo;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.authorization.library.utils.Caller;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.common.authorization.utils.manager.SecretManager;
|
||||
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
||||
import org.gcube.common.authorization.utils.secret.Secret;
|
||||
import org.gcube.common.authorization.utils.secret.SecretUtility;
|
||||
import org.gcube.informationsystem.model.reference.properties.Header;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -34,8 +31,6 @@ public class ContextTest {
|
|||
public static final String DEFAULT_TEST_SCOPE;
|
||||
public static final String ALTERNATIVE_TEST_SCOPE;
|
||||
|
||||
public static final String DEFAULT_TEST_SCOPE_ANOTHER_USER;
|
||||
|
||||
public static final String GCUBE;
|
||||
public static final String DEVNEXT;
|
||||
public static final String NEXTNEXT;
|
||||
|
@ -67,40 +62,43 @@ public class ContextTest {
|
|||
DEFAULT_TEST_SCOPE = PARENT_DEFAULT_TEST_SCOPE + "/devNext";
|
||||
ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE + "/NextNext";
|
||||
|
||||
DEFAULT_TEST_SCOPE_ANOTHER_USER = "lucio.lelii_" + DEFAULT_TEST_SCOPE;
|
||||
|
||||
try {
|
||||
setContextByName(DEFAULT_TEST_SCOPE);
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getCurrentContextFullName() throws ObjectNotFound, Exception {
|
||||
return getContextFullNameByToken(SecurityTokenProvider.instance.get());
|
||||
public static void set(Secret secret) throws Exception {
|
||||
SecretManagerProvider.instance.reset();
|
||||
SecretManager secretManager = SecretManagerProvider.instance.get();
|
||||
secretManager.addSecret(secret);
|
||||
secretManager.set();
|
||||
}
|
||||
|
||||
public static String getContextFullNameByToken(String token) throws ObjectNotFound, Exception {
|
||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
||||
String context = authorizationEntry.getContext();
|
||||
logger.info("Context of token {} is {}", token, context);
|
||||
return context;
|
||||
public static void setContext(String token) throws Exception {
|
||||
Secret secret = getSecret(token);
|
||||
set(secret);
|
||||
}
|
||||
|
||||
public static void setContextByName(String fullContextName) throws Exception {
|
||||
Secret secret = getSecretByContextName(fullContextName);
|
||||
set(secret);
|
||||
}
|
||||
|
||||
public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception {
|
||||
private static Secret getSecret(String token) throws Exception {
|
||||
Secret secret = SecretUtility.getSecretByTokenString(token);
|
||||
return secret;
|
||||
}
|
||||
|
||||
private static Secret getSecretByContextName(String fullContextName) throws Exception {
|
||||
String token = ContextTest.properties.getProperty(fullContextName);
|
||||
setContext(token);
|
||||
return getSecret(token);
|
||||
}
|
||||
|
||||
private static void setContext(String token) throws ObjectNotFound, Exception {
|
||||
SecurityTokenProvider.instance.set(token);
|
||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
||||
ClientInfo clientInfo = authorizationEntry.getClientInfo();
|
||||
logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name());
|
||||
String qualifier = authorizationEntry.getQualifier();
|
||||
Caller caller = new Caller(clientInfo, qualifier);
|
||||
AuthorizationProvider.instance.set(caller);
|
||||
ScopeProvider.instance.set(getContextFullNameByToken(token));
|
||||
public static String getUser() {
|
||||
String user = Header.UNKNOWN_USER;
|
||||
try {
|
||||
user = SecretManagerProvider.instance.get().getUser().getUsername();
|
||||
} catch(Exception e) {
|
||||
logger.error("Unable to retrieve user. {} will be used", user);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
|
@ -110,8 +108,7 @@ public class ContextTest {
|
|||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
SecurityTokenProvider.instance.reset();
|
||||
ScopeProvider.instance.reset();
|
||||
SecretManagerProvider.instance.reset();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,7 +12,6 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
||||
import org.gcube.informationsystem.base.reference.Element;
|
||||
import org.gcube.informationsystem.base.reference.IdentifiableElement;
|
||||
import org.gcube.informationsystem.model.impl.properties.EncryptedImpl;
|
||||
|
@ -525,7 +524,7 @@ public class ERManagementTest extends ContextTest {
|
|||
Assert.assertTrue(((String) updatedCpuFacet.getAdditionalProperty(additionPropertyKey))
|
||||
.compareTo((String) readCpuFacet.getAdditionalProperty(additionPropertyKey)) == 0);
|
||||
Assert.assertTrue(uuid.compareTo(updatedCpuFacet.getHeader().getUUID()) == 0);
|
||||
String user = AuthorizationProvider.instance.get().getClient().getId();
|
||||
String user = ContextTest.getUser();
|
||||
Assert.assertTrue(updatedCpuFacet.getHeader().getLastUpdateBy().compareTo(user) == 0);
|
||||
|
||||
facetManagement = new FacetManagement();
|
||||
|
|
|
@ -9,6 +9,7 @@ import java.util.TreeSet;
|
|||
import java.util.UUID;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
||||
import org.gcube.informationsystem.base.reference.Element;
|
||||
import org.gcube.informationsystem.contexts.reference.entities.Context;
|
||||
import org.gcube.informationsystem.model.reference.entities.Facet;
|
||||
|
@ -229,7 +230,7 @@ public class MultiContextTest extends ERManagementTest {
|
|||
// checkAffectedInstances(expectedInstances, affectedInstances);
|
||||
//
|
||||
if(!dryRun) {
|
||||
String currentContext = ContextTest.getCurrentContextFullName();
|
||||
String currentContext = SecretManagerProvider.instance.get().getContext();
|
||||
ContextTest.setContextByName(targetContextFullName);
|
||||
resourceManagement = ERManagementTest.getResourceManagement(r);
|
||||
String json = resourceManagement.read();
|
||||
|
|
|
@ -10,10 +10,6 @@ import org.gcube.com.fasterxml.jackson.databind.JavaType;
|
|||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.gcube.common.authorization.client.Constants;
|
||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||
import org.gcube.common.authorization.library.provider.ClientInfo;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.informationsystem.base.reference.IdentifiableElement;
|
||||
import org.gcube.informationsystem.model.reference.properties.Header;
|
||||
import org.gcube.informationsystem.queries.templates.impl.entities.QueryTemplateImpl;
|
||||
|
@ -41,28 +37,6 @@ public class QueryTemplateManagementTest extends ContextTest {
|
|||
public static final String NAME_VARIABLE_NAME = "$name";
|
||||
public static final String GROUP_VARIABLE_NAME = "$group";
|
||||
|
||||
public static String getUser() {
|
||||
String user = Header.UNKNOWN_USER;
|
||||
try {
|
||||
String token = SecurityTokenProvider.instance.get();
|
||||
if(token != null) {
|
||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
||||
if(authorizationEntry != null) {
|
||||
ClientInfo clientInfo = authorizationEntry.getClientInfo();
|
||||
String clientId = clientInfo.getId();
|
||||
if(clientId != null && clientId.compareTo("") != 0) {
|
||||
user = clientId;
|
||||
} else {
|
||||
throw new Exception("Username null or empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {
|
||||
logger.error("Unable to retrieve user. {} will be used", user);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
public static void checkHeader(IdentifiableElement previous, IdentifiableElement got) {
|
||||
Header gotHeader = got.getHeader();
|
||||
Header previousHeader = previous.getHeader();
|
||||
|
@ -70,7 +44,7 @@ public class QueryTemplateManagementTest extends ContextTest {
|
|||
Assert.assertTrue(gotHeader != null);
|
||||
Assert.assertTrue(gotHeader.getUUID() != null);
|
||||
|
||||
String user = getUser();
|
||||
String user = ContextTest.getUser();
|
||||
Assert.assertTrue(gotHeader.getLastUpdateBy().compareTo(user) == 0);
|
||||
|
||||
if(previousHeader != null) {
|
||||
|
|
Loading…
Reference in New Issue