unit test

This commit is contained in:
Alfredo Oliviero 2024-05-21 15:19:02 +02:00
parent 11bb16c98d
commit 3a6d2169da
9 changed files with 203 additions and 137 deletions

View File

@ -160,4 +160,10 @@ public class DefaultUsersClient extends AbstractIdmClient implements IdmUsersCli
listType);
return resp.getResult();
}
@Override
public org.gcube.idm.client.model.UserInfo getUser(String user_id) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getUser'");
}
}

View File

@ -2,6 +2,7 @@ package org.gcube.idm.client;
import java.util.List;
import org.gcube.idm.client.model.UserInfo;
import org.gcube.idm.client.model.UserProfile;
import org.gcube.idm.common.models.IdmFullUser;
import org.gcube.idm.common.models.IdmUser;
@ -30,4 +31,6 @@ public interface IdmUsersClient {
public List<String> searchUsernames(Integer first, Integer max, SearchUsersParams params) throws NotAuthorizedException, BadRequestException;
public UserInfo getUser(String user_id);
}

View File

@ -11,6 +11,11 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.gcube.com.fasterxml.jackson.core.type.TypeReference;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.common.gxrest.request.GXHTTPStringRequest;
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
import org.gcube.common.keycloak.KeycloakClientException;
import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.KeycloakClientHelper;
import org.gcube.common.keycloak.model.TokenResponse;
@ -22,19 +27,11 @@ import org.gcube.idm.client.IdmMeClient;
import org.gcube.idm.client.IdmUsersClient;
import org.gcube.idm.client.clients.IdmRestClient;
import org.gcube.idm.common.is.IsServerConfig;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.ws.rs.BadRequestException;
import org.gcube.com.fasterxml.jackson.core.type.TypeReference;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.common.gxrest.request.GXHTTPStringRequest;
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
/**
* @author Luca Frosini (ISTI - CNR)
*
@ -46,7 +43,9 @@ public abstract class ClientContextTest {
protected static final String CONFIG_INI_FILENAME = "test.ini";
public static final String DEFAULT_TEST_SCOPE;
public static Secret current_secret;
protected static Secret current_secret;
public IsServerConfig getMokedIsConfig() {
String baseurl = properties.getProperty("SERVICE_URL"); // "http://146.48.85.179:9999/idm-service/";
@ -67,7 +66,7 @@ public abstract class ClientContextTest {
return client;
}
public IdmUsersClient getUserClient() throws ServerException {
public IdmUsersClient getUserClient() throws IOException {
String token = getServiceToken();
return getUserClient(token);
}
@ -80,9 +79,14 @@ public abstract class ClientContextTest {
return client;
}
public String getServiceToken() {
String token = properties.getProperty("TOKEN_SERVICE");
return token;
private String service_token;
public String getServiceToken() throws IOException {
if (service_token == null) {
service_token = loginService();
}
return service_token;
}
private String user_token;
@ -109,16 +113,11 @@ public abstract class ClientContextTest {
protected static final Properties properties;
public static final String TYPE_PROPERTY_KEY = "type";
public static final String USERNAME_PROPERTY_KEY = "username";
public static final String PASSWORD_PROPERTY_KEY = "password";
public static final String CLIENT_ID_PROPERTY_KEY = "clientId";
static {
try {
properties = readProperties(CONFIG_INI_FILENAME);
VRE = properties.getProperty("context");
VRE = properties.getProperty("CONTEXT");
DEFAULT_TEST_SCOPE = VRE;
} catch (IOException e) {
@ -140,20 +139,20 @@ public abstract class ClientContextTest {
USER, CLIENT_ID
};
public static void set(Secret secret) throws Exception {
public static void set(Secret secret) {
SecretManagerProvider.reset();
SecretManagerProvider.set(secret);
current_secret = secret;
}
public static void setContextByName(String fullContextName) throws Exception {
public static void setContextByName(String fullContextName) throws ServerException {
logger.debug("Going to set credentials for context {}", fullContextName);
Secret secret = getSecretByContextName(fullContextName);
set(secret);
}
private static TokenResponse getJWTAccessToken(String context) throws Exception {
Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString());
private static TokenResponse getJWTAccessToken(String context) throws ServerException, KeycloakClientException {
Type type = Type.valueOf(properties.get("CLIENT_TYPE").toString());
TokenResponse tr = null;
@ -162,16 +161,23 @@ public abstract class ClientContextTest {
switch (type) {
case CLIENT_ID:
String clientId = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
String clientId = properties.getProperty("CLIENT_ID");
String clientSecret = properties.getProperty(root);
tr = KeycloakClientFactory.newInstance().queryUMAToken(context, clientId, clientSecret, context, null);
try {
tr = KeycloakClientFactory.newInstance().queryUMAToken(context, clientId, clientSecret, context,
null);
} catch (KeycloakClientException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ServerException(e.getMessage());
}
break;
case USER:
default:
String username = properties.getProperty(USERNAME_PROPERTY_KEY);
String password = properties.getProperty(PASSWORD_PROPERTY_KEY);
String username = properties.getProperty("LOGIN_USER_USERNAME");
String password = properties.getProperty("LOGIN_USER_PASS");
switch (root) {
case "/gcube":
@ -198,18 +204,25 @@ public abstract class ClientContextTest {
}
public static Secret getSecretByContextName(String context) throws Exception {
TokenResponse tr = getJWTAccessToken(context);
public static Secret getSecretByContextName(String context) throws ServerException {
TokenResponse tr;
try {
tr = getJWTAccessToken(context);
} catch (KeycloakClientException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ServerException(e.getMessage());
}
Secret secret = new AccessTokenSecret(tr.getAccessToken(), context);
return secret;
}
public static void setContext(String token) throws Exception {
public static void setContext(String token) {
Secret secret = getSecret(token);
set(secret);
}
private static Secret getSecret(String token) throws Exception {
private static Secret getSecret(String token) {
// TODO: verificare classe (AccessTokenSecret anziche JWTToken) e context(VRE)
Secret secret = new AccessTokenSecret(token, VRE);
return secret;
@ -225,14 +238,51 @@ public abstract class ClientContextTest {
return user;
}
@BeforeClass
public static void beforeClass() throws Exception {
setContextByName(DEFAULT_TEST_SCOPE);
public String loginService() throws IOException {
String context = properties.getProperty("LOGIN_CONTEXT");
return loginService(context);
}
@AfterClass
public static void afterClass() throws Exception {
SecretManagerProvider.reset();
public String loginService(String context) throws IOException {
URL login_url = null;
try {
login_url = new URL(properties.getProperty("LOGIN_URL"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String clientId = properties.getProperty("CLIENT_ID");
String clientSecret = properties.getProperty("CLIENT_SECRET");
String encoded_context = context.replace("/", "%2F");
Map<String, String> headers = new HashMap<String, String>();
Map<String, List<String>> params = new HashMap<String, List<String>>();
headers.put("Content-Type", "application/x-www-form-urlencoded");
headers.put("X-D4Science-Context", encoded_context);
params.put("client_id", Collections.singletonList(clientId));
params.put("client_secret", Collections.singletonList(clientSecret));
params.put("grant_type", Collections.singletonList("client_credentials"));
GXHTTPStringRequest request = IdmRestClient.preparePostRequest(login_url, headers, params);
GXInboundResponse response;
try {
response = request.post();
} catch (Exception e) {
throw new BadRequestException("Cannot send request correctly", e);
}
ObjectMapper om = new ObjectMapper();
TypeReference<Map<String, String>> typeRef = new TypeReference<Map<String, String>>() {
};
String jsonstring = response.getStreamedContentAsString();
HashMap<String, String> result = om.readValue(jsonstring, typeRef);
return result.get("access_token");
// return properties.getProperty("TOKEN_USER");
}
public String loginUser() throws IOException {
@ -248,11 +298,10 @@ public abstract class ClientContextTest {
String context = properties.getProperty("LOGIN_CONTEXT");
String encoded_context = context.replace("/", "%2F");
Map<String, String> headers = new HashMap<String, String>();
Map<String, List<String>> params = new HashMap<String, List<String>>();
headers.put("Content-Type", "application/x-www-form-urlencoded");
headers.put("X-D4Science-Context", encoded_context);
headers.put("X-D4Science-Context", encoded_context);
params.put("client_id", Collections.singletonList(properties.getProperty("LOGIN_USER_CLIENT_ID")));
params.put("username", Collections.singletonList(properties.getProperty("LOGIN_USER_USERNAME")));
@ -268,7 +317,8 @@ public abstract class ClientContextTest {
throw new BadRequestException("Cannot send request correctly", e);
}
ObjectMapper om = new ObjectMapper();
TypeReference<Map<String,String>> typeRef = new TypeReference<Map<String,String>>() {};
TypeReference<Map<String, String>> typeRef = new TypeReference<Map<String, String>>() {
};
String jsonstring = response.getStreamedContentAsString();
HashMap<String, String> result = om.readValue(jsonstring, typeRef);

View File

@ -8,7 +8,7 @@ import org.junit.Test;
public class IdmJwtTest extends ClientContextTest{
@Test
// @Test
public void decodeJWT() throws IOException {
fail("Unimplemented");
// IdmMeClient client = getMeClient();
@ -18,7 +18,7 @@ public class IdmJwtTest extends ClientContextTest{
}
@Test
// @Test
public void checkAuth() throws IOException {
fail("Unimplemented");
// IdmMeClient client = getMeClient();

View File

@ -8,7 +8,7 @@ import org.junit.Test;
public class IdmRolesTest extends ClientContextTest {
@Test
// @Test
public void getRoles() throws IOException {
fail("Unimplemented");
// IdmMeClient client = getMeClient();
@ -17,7 +17,7 @@ public class IdmRolesTest extends ClientContextTest {
// assertEquals(expected, email);
}
@Test
// @Test
public void getRoleByName() throws IOException {
fail("Unimplemented");
@ -27,7 +27,7 @@ public class IdmRolesTest extends ClientContextTest {
// assertEquals(expected, email);
}
@Test
// @Test
public void getRoleMembers() throws IOException {
fail("Unimplemented");
@ -37,7 +37,7 @@ public class IdmRolesTest extends ClientContextTest {
// assertEquals(expected, email);
}
@Test
// @Test
public void getUsersForRole() throws IOException {
fail("Unimplemented");

View File

@ -5,6 +5,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.rmi.ServerException;
import java.util.List;
@ -18,7 +19,7 @@ import jakarta.ws.rs.NotAuthorizedException;
public class IdmSocialTest extends ClientContextTest {
@Test
// @Test
public void getPepoleProfile() throws ServerException {
fail("Unimplemented");
@ -32,73 +33,81 @@ public class IdmSocialTest extends ClientContextTest {
// public TokenInfo getUserOwner(String user_id);
// public UserProfile getUserProfile(String user_id);
@Test
// @Test
public void getUserProfile() throws ServerException {
fail("Unimplemented");
}
@Test
// @Test
public void getUser() throws IOException {
// String user_id = properties.getProperty("USER_ID");
// IdmUsersClient client = getUserClient();
// UserInfo resp = client.getUser(user_id);
// assertNotNull(resp);
// String expected = "andrea.rossi";
// assertEquals(expected, resp.user.getUsername());
// assertEquals(expected, resp.owner.getId());
// assertEquals(expected, resp.profile.getUsername());
}
// @Test
public void getUserEmail() throws ServerException {
fail("Unimplemented");
}
@Test
// @Test
public void getUserFullname() throws ServerException {
fail("Unimplemented");
}
@Test
// @Test
public void getAllUsernames() throws ServerException {
fail("Unimplemented");
}
@Test
// @Test
public void getAllFullnamesUsernames() throws ServerException {
fail("Unimplemented");
}
@Test
// @Test
public void getUsernamesByRole() throws ServerException {
fail("Unimplemented");
}
@Test
// @Test
public void checkUserExists() throws ServerException {
fail("Unimplemented");
}
@Test
// @Test
public void getCustomAttribute() throws ServerException {
fail("Unimplemented");
}
@Test
// @Test
public void getOAuthProfile() throws ServerException {
fail("Unimplemented");
}
@Test
// @Test
public void getUsernamesByGlobalRole() throws ServerException {
fail("Unimplemented");
}
@Test
public void searchUsers() throws ServerException {
public void searchUsers() throws IOException {
IdmUsersClient client = getUserClient();
List<IdmUser> users = client.searchUsers(null, 3, null);
assertNotNull("expected to receive users", users);
assertEquals("size expected 3", 3, users.size());
}
@Test
public void searchFullUsers() throws ServerException {
// @Test
public void searchFullUsers() throws Exception {
IdmUsersClient client = getUserClient();
List<IdmFullUser> users = client.searchFullUsers(null, 3, null);
assertNotNull("expected to receive users", users);
@ -106,7 +115,7 @@ public class IdmSocialTest extends ClientContextTest {
}
@Test
public void searchFilteredFullUsers() throws ServerException {
public void searchFilteredFullUsers() throws IOException {
IdmUsersClient client = getUserClient();
SearchUsersParams params = new SearchUsersParams();
params.email = "alfredo.oliviero";

View File

@ -3,7 +3,6 @@ package org.gcube.idm.client.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.rmi.ServerException;
@ -18,69 +17,69 @@ import org.junit.Test;
import jakarta.ws.rs.NotAuthorizedException;
public class IdmUsersClientTest extends ClientContextTest {
@Test
public void getUser() throws IOException {
fail("Unimplemented");
}
// @Test
// public void getUser() throws IOException {
// fail("Unimplemented");
// }
// @Test
// public void getUserInspect() throws IOException {
// fail("Unimplemented");
// }
// @Test
// public void getUserProfile() throws IOException {
// fail("Unimplemented");
// }
@Test
public void getUserInspect() throws IOException {
fail("Unimplemented");
}
@Test
public void getUserProfile() throws IOException {
fail("Unimplemented");
}
@Test
public void getUserEmail() throws ServerException {
public void getUserEmail() throws IOException {
IdmUsersClient client = getUserClient();
String email = client.getUserEmail("alfredo.oliviero");
String expected = "alfredo.oliviero@isti.cnr.it";
assertEquals(expected, email);
}
@Test
public void getUserRolesRealm() throws IOException {
fail("Unimplemented");
}
// @Test
// public void getUserRolesRealm() throws IOException {
// fail("Unimplemented");
// }
@Test
public void getUserRolesClient() throws IOException {
fail("Unimplemented");
}
// @Test
// public void getUserRolesClient() throws IOException {
// fail("Unimplemented");
// }
@Test
public void getUserGroups() throws IOException {
fail("Unimplemented");
}
// @Test
// public void getUserGroups() throws IOException {
// fail("Unimplemented");
// }
@Test
public void getUserId() throws IOException {
fail("Unimplemented");
}
// @Test
// public void getUserId() throws IOException {
// fail("Unimplemented");
// }
@Test
public void getUserUsername() throws IOException {
fail("Unimplemented");
}
// @Test
// public void getUserUsername() throws IOException {
// fail("Unimplemented");
// }
@Test
public void getUserName() throws IOException {
fail("Unimplemented");
}
// @Test
// public void getUserName() throws IOException {
// fail("Unimplemented");
// }
@Test
public void getUserAttributes() throws IOException {
fail("Unimplemented");
}
// @Test
// public void getUserAttributes() throws IOException {
// fail("Unimplemented");
// }
@Test
public void getUserUser() throws IOException {
fail("Unimplemented");
}
// @Test
// public void getUserUser() throws IOException {
// fail("Unimplemented");
// }
// public String getUserEmail(String user_id);
@ -89,7 +88,7 @@ public class IdmUsersClientTest extends ClientContextTest {
// public UserProfile getUserProfile(String user_id);
@Test
public void searchUsernames() throws ServerException {
public void searchUsernames() throws IOException {
IdmUsersClient client = getUserClient();
List<String> usernames = client.searchUsernames(null, 3, null);
assertNotNull("expected to receive username", usernames);
@ -97,7 +96,7 @@ public class IdmUsersClientTest extends ClientContextTest {
}
@Test
public void searchUsers() throws ServerException {
public void searchUsers() throws IOException {
IdmUsersClient client = getUserClient();
List<IdmUser> users = client.searchUsers(null, 3, null);
assertNotNull("expected to receive users", users);
@ -105,7 +104,7 @@ public class IdmUsersClientTest extends ClientContextTest {
}
@Test
public void searchFullUsers() throws ServerException {
public void searchFullUsers() throws IOException {
IdmUsersClient client = getUserClient();
List<IdmFullUser> users = client.searchFullUsers(null, 3, null);
assertNotNull("expected to receive users", users);
@ -113,7 +112,7 @@ public class IdmUsersClientTest extends ClientContextTest {
}
@Test
public void searchFilteredFullUsers() throws ServerException {
public void searchFilteredFullUsers() throws IOException {
IdmUsersClient client = getUserClient();
SearchUsersParams params = new SearchUsersParams();
params.email = "alfredo.oliviero";

View File

@ -78,20 +78,20 @@ public class IdmUsersMeClientTest extends ClientContextTest {
assertEquals(expected, email);
}
@Test
public void getMeRolesRealm() throws IOException {
fail("Unimplemented");
}
// @Test
// public void getMeRolesRealm() throws IOException {
// fail("Unimplemented");
// }
@Test
public void getMeRolesClient() throws IOException {
fail("Unimplemented");
}
// @Test
// public void getMeRolesClient() throws IOException {
// fail("Unimplemented");
// }
@Test
public void getMeGroups() throws IOException {
fail("Unimplemented");
}
// @Test
// public void getMeGroups() throws IOException {
// fail("Unimplemented");
// }
@Test
public void getMeId() throws IOException {

View File

@ -1,8 +1,7 @@
context=/gcube
type=CLIENT_ID
clientId=id.d4science.org
/gcube=09c26f24-3c65-4039-9fa0-e5cc4f4032cd
CONTEXT=/gcube
CLIENT_TYPE=CLIENT_ID
CLIENT_ID=id.d4science.org
CLIENT_SECRET=09c26f24-3c65-4039-9fa0-e5cc4f4032cd
SERVICE_URL_PROCACCINI = http://146.48.85.179:9999/idm-service/
SERVICE_URL_LOCAL = http://localhost:8080/idm-service/