2018-09-19 15:54:24 +02:00
|
|
|
package sonia.scm.it.utils;
|
2018-08-03 09:38:13 +02:00
|
|
|
|
2018-08-22 11:19:19 +02:00
|
|
|
import io.restassured.response.ValidatableResponse;
|
2018-08-03 09:38:13 +02:00
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import sonia.scm.web.VndMediaType;
|
|
|
|
|
|
2018-08-06 16:16:19 +02:00
|
|
|
import javax.json.Json;
|
2018-09-19 15:54:24 +02:00
|
|
|
import javax.json.JsonObjectBuilder;
|
|
|
|
|
import java.net.URI;
|
2019-01-23 09:47:38 +01:00
|
|
|
import java.util.Collection;
|
2018-08-06 16:16:19 +02:00
|
|
|
import java.util.HashMap;
|
2018-08-03 09:38:13 +02:00
|
|
|
import java.util.List;
|
2018-08-06 16:16:19 +02:00
|
|
|
import java.util.Map;
|
2019-01-23 09:47:38 +01:00
|
|
|
import java.util.stream.Collectors;
|
2018-08-03 09:38:13 +02:00
|
|
|
|
|
|
|
|
import static java.util.Arrays.asList;
|
2018-09-19 15:54:24 +02:00
|
|
|
import static sonia.scm.it.utils.RestUtil.createResourceUrl;
|
|
|
|
|
import static sonia.scm.it.utils.RestUtil.given;
|
|
|
|
|
import static sonia.scm.it.utils.ScmTypes.availableScmTypes;
|
2018-08-03 09:38:13 +02:00
|
|
|
|
|
|
|
|
public class TestData {
|
|
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(TestData.class);
|
|
|
|
|
|
2018-08-22 09:18:17 +02:00
|
|
|
public static final String USER_SCM_ADMIN = "scmadmin";
|
|
|
|
|
public static final String USER_ANONYMOUS = "anonymous";
|
2019-01-23 09:47:38 +01:00
|
|
|
|
|
|
|
|
public static final Collection<String> READ = asList("read", "pull");
|
|
|
|
|
public static final Collection<String> WRITE = asList("read", "write", "pull", "push");
|
|
|
|
|
public static final Collection<String> OWNER = asList("*");
|
|
|
|
|
|
2018-08-22 11:19:19 +02:00
|
|
|
private static final List<String> PROTECTED_USERS = asList(USER_SCM_ADMIN, USER_ANONYMOUS);
|
2018-08-03 09:38:13 +02:00
|
|
|
|
2018-08-06 16:16:19 +02:00
|
|
|
private static Map<String, String> DEFAULT_REPOSITORIES = new HashMap<>();
|
2018-09-19 15:54:24 +02:00
|
|
|
public static final JsonObjectBuilder JSON_BUILDER = NullAwareJsonObjectBuilder.wrap(Json.createObjectBuilder());
|
2018-08-06 16:16:19 +02:00
|
|
|
|
|
|
|
|
public static void createDefault() {
|
|
|
|
|
cleanup();
|
|
|
|
|
createDefaultRepositories();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 09:38:13 +02:00
|
|
|
public static void cleanup() {
|
2018-09-05 10:44:03 +02:00
|
|
|
LOG.info("start to clean up to integration tests");
|
2018-08-03 09:38:13 +02:00
|
|
|
cleanupRepositories();
|
|
|
|
|
cleanupGroups();
|
|
|
|
|
cleanupUsers();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-06 16:16:19 +02:00
|
|
|
public static String getDefaultRepositoryUrl(String repositoryType) {
|
|
|
|
|
return DEFAULT_REPOSITORIES.get(repositoryType);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:06:53 +02:00
|
|
|
public static void createNotAdminUser(String username, String password) {
|
|
|
|
|
createUser(username, password, false, "xml", "user1@scm-manager.org");
|
2018-09-19 15:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:06:53 +02:00
|
|
|
public static void createUser(String username, String password, boolean isAdmin, String type, final String email) {
|
2018-09-05 10:44:03 +02:00
|
|
|
LOG.info("create user with username: {}", username);
|
2018-09-19 15:54:24 +02:00
|
|
|
String admin = isAdmin ? "true" : "false";
|
2018-08-22 09:18:17 +02:00
|
|
|
given(VndMediaType.USER)
|
|
|
|
|
.when()
|
2018-09-19 15:54:24 +02:00
|
|
|
.content(new StringBuilder()
|
|
|
|
|
.append(" {\n")
|
|
|
|
|
.append(" \"active\": true,\n")
|
|
|
|
|
.append(" \"admin\": ").append(admin).append(",\n")
|
|
|
|
|
.append(" \"creationDate\": \"2018-08-21T12:26:46.084Z\",\n")
|
|
|
|
|
.append(" \"displayName\": \"").append(username).append("\",\n")
|
2018-10-12 11:06:53 +02:00
|
|
|
.append(" \"mail\": \"" + email + "\",\n")
|
2018-09-19 15:54:24 +02:00
|
|
|
.append(" \"name\": \"").append(username).append("\",\n")
|
|
|
|
|
.append(" \"password\": \"").append(password).append("\",\n")
|
|
|
|
|
.append(" \"type\": \"").append(type).append("\"\n")
|
|
|
|
|
.append(" }").toString())
|
|
|
|
|
.post(getUsersUrl())
|
2018-08-22 09:18:17 +02:00
|
|
|
.then()
|
2019-03-13 15:56:40 +01:00
|
|
|
.statusCode(HttpStatus.SC_CREATED);
|
|
|
|
|
|
|
|
|
|
if (isAdmin) {
|
|
|
|
|
assignAdminPermissions(username);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void assignAdminPermissions(String username) {
|
|
|
|
|
LOG.info("assign admin permissions to user {}", username);
|
|
|
|
|
given(VndMediaType.PERMISSION_COLLECTION)
|
|
|
|
|
.when()
|
|
|
|
|
.body("{'permissions': ['*']}".replaceAll("'", "\""))
|
|
|
|
|
.put(getPermissionUrl(username))
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_NO_CONTENT);
|
2018-08-22 09:18:17 +02:00
|
|
|
}
|
2019-03-13 15:56:40 +01:00
|
|
|
|
|
|
|
|
private static URI getPermissionUrl(String username) {
|
|
|
|
|
return RestUtil.createResourceUrl(String.format("users/%s/permissions", username));
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:06:53 +02:00
|
|
|
public static void createGroup(String groupName, String desc) {
|
|
|
|
|
LOG.info("create group with group name: {} and description {}", groupName, desc);
|
|
|
|
|
given(VndMediaType.GROUP)
|
|
|
|
|
.when()
|
|
|
|
|
.content(getGroupJson(groupName,desc))
|
|
|
|
|
.post(getGroupsUrl())
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_CREATED)
|
|
|
|
|
;
|
|
|
|
|
}
|
2018-08-22 09:18:17 +02:00
|
|
|
|
2019-05-07 09:20:19 +02:00
|
|
|
public static void createUserPermission(String username, Collection<String> verbs, String repositoryType) {
|
2019-01-23 09:47:38 +01:00
|
|
|
String defaultPermissionUrl = TestData.getDefaultPermissionUrl(USER_SCM_ADMIN, USER_SCM_ADMIN, repositoryType);
|
2019-05-07 09:20:19 +02:00
|
|
|
LOG.info("create permission with name {} and verbs {} using the endpoint: {}", username, verbs, defaultPermissionUrl);
|
2019-01-23 11:19:55 +01:00
|
|
|
given(VndMediaType.REPOSITORY_PERMISSION)
|
2019-01-23 09:47:38 +01:00
|
|
|
.when()
|
|
|
|
|
.content("{\n" +
|
2019-05-07 09:20:19 +02:00
|
|
|
"\t\"verbs\": " + verbs.stream().collect(Collectors.joining("\",\"", "[\"", "\"]")) + ",\n" +
|
|
|
|
|
"\t\"name\": \"" + username + "\",\n" +
|
|
|
|
|
"\t\"groupPermission\": false\n" +
|
|
|
|
|
"\t\n" +
|
|
|
|
|
"}")
|
|
|
|
|
.post(defaultPermissionUrl)
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_CREATED)
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void createUserPermission(String username, String roleName, String repositoryType) {
|
|
|
|
|
String defaultPermissionUrl = TestData.getDefaultPermissionUrl(USER_SCM_ADMIN, USER_SCM_ADMIN, repositoryType);
|
|
|
|
|
LOG.info("create permission with name {} and role {} using the endpoint: {}", username, roleName, defaultPermissionUrl);
|
|
|
|
|
given(VndMediaType.REPOSITORY_PERMISSION)
|
|
|
|
|
.when()
|
|
|
|
|
.content("{\n" +
|
|
|
|
|
"\t\"role\": " + roleName + ",\n" +
|
|
|
|
|
"\t\"name\": \"" + username + "\",\n" +
|
2019-01-23 09:47:38 +01:00
|
|
|
"\t\"groupPermission\": false\n" +
|
|
|
|
|
"\t\n" +
|
|
|
|
|
"}")
|
|
|
|
|
.post(defaultPermissionUrl)
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_CREATED)
|
|
|
|
|
;
|
|
|
|
|
}
|
2018-08-22 09:18:17 +02:00
|
|
|
|
2018-11-09 10:13:36 +01:00
|
|
|
public static List<Map> getUserPermissions(String username, String password, String repositoryType) {
|
2018-08-22 11:19:19 +02:00
|
|
|
return callUserPermissions(username, password, repositoryType, HttpStatus.SC_OK)
|
|
|
|
|
.extract()
|
2018-11-09 10:13:36 +01:00
|
|
|
.body().jsonPath().<Map>getList("_embedded.permissions");
|
2018-08-22 11:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ValidatableResponse callUserPermissions(String username, String password, String repositoryType, int expectedStatusCode) {
|
2019-01-23 11:19:55 +01:00
|
|
|
return given(VndMediaType.REPOSITORY_PERMISSION, username, password)
|
2018-08-22 11:19:19 +02:00
|
|
|
.when()
|
2018-08-22 12:20:09 +02:00
|
|
|
.get(TestData.getDefaultPermissionUrl(username, password, repositoryType))
|
2018-08-22 11:19:19 +02:00
|
|
|
.then()
|
|
|
|
|
.statusCode(expectedStatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ValidatableResponse callRepository(String username, String password, String repositoryType, int expectedStatusCode) {
|
|
|
|
|
return given(VndMediaType.REPOSITORY, username, password)
|
|
|
|
|
|
|
|
|
|
.when()
|
|
|
|
|
.get(getDefaultRepositoryUrl(repositoryType))
|
|
|
|
|
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(expectedStatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 12:20:09 +02:00
|
|
|
public static String getDefaultPermissionUrl(String username, String password, String repositoryType) {
|
|
|
|
|
return given(VndMediaType.REPOSITORY, username, password)
|
|
|
|
|
.when()
|
|
|
|
|
.get(getDefaultRepositoryUrl(repositoryType))
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_OK)
|
|
|
|
|
.extract()
|
|
|
|
|
.body().jsonPath().getString("_links.permissions.href");
|
2018-08-22 09:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-08-06 16:16:19 +02:00
|
|
|
private static void cleanupRepositories() {
|
2018-09-05 10:44:03 +02:00
|
|
|
LOG.info("clean up repository");
|
2018-08-03 09:38:13 +02:00
|
|
|
List<String> repositories = given(VndMediaType.REPOSITORY_COLLECTION)
|
|
|
|
|
.when()
|
|
|
|
|
.get(createResourceUrl("repositories"))
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_OK)
|
|
|
|
|
.extract()
|
|
|
|
|
.body().jsonPath().getList("_embedded.repositories._links.self.href");
|
|
|
|
|
LOG.info("about to delete {} repositories", repositories.size());
|
|
|
|
|
repositories.forEach(TestData::delete);
|
2018-08-06 16:16:19 +02:00
|
|
|
DEFAULT_REPOSITORIES.clear();
|
2018-08-03 09:38:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-06 16:16:19 +02:00
|
|
|
private static void cleanupGroups() {
|
2018-08-03 09:38:13 +02:00
|
|
|
List<String> groups = given(VndMediaType.GROUP_COLLECTION)
|
|
|
|
|
.when()
|
|
|
|
|
.get(createResourceUrl("groups"))
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_OK)
|
|
|
|
|
.extract()
|
|
|
|
|
.body().jsonPath().getList("_embedded.groups._links.self.href");
|
|
|
|
|
LOG.info("about to delete {} groups", groups.size());
|
|
|
|
|
groups.forEach(TestData::delete);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-06 16:16:19 +02:00
|
|
|
private static void cleanupUsers() {
|
2018-08-03 09:38:13 +02:00
|
|
|
List<String> users = given(VndMediaType.USER_COLLECTION)
|
|
|
|
|
.when()
|
|
|
|
|
.get(createResourceUrl("users"))
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_OK)
|
|
|
|
|
.extract()
|
|
|
|
|
.body().jsonPath().getList("_embedded.users._links.self.href");
|
|
|
|
|
LOG.info("about to delete {} users", users.size());
|
|
|
|
|
users.stream().filter(url -> PROTECTED_USERS.stream().noneMatch(url::contains)).forEach(TestData::delete);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-06 16:16:19 +02:00
|
|
|
private static void delete(String url) {
|
2018-08-03 09:38:13 +02:00
|
|
|
given(VndMediaType.REPOSITORY)
|
|
|
|
|
.when()
|
|
|
|
|
.delete(url)
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_NO_CONTENT);
|
|
|
|
|
LOG.info("deleted {}", url);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-06 16:16:19 +02:00
|
|
|
private static void createDefaultRepositories() {
|
2018-09-05 10:44:03 +02:00
|
|
|
LOG.info("create default repositories");
|
2018-08-06 16:16:19 +02:00
|
|
|
for (String repositoryType : availableScmTypes()) {
|
|
|
|
|
String url = given(VndMediaType.REPOSITORY)
|
|
|
|
|
.body(repositoryJson(repositoryType))
|
|
|
|
|
|
|
|
|
|
.when()
|
|
|
|
|
.post(createResourceUrl("repositories"))
|
|
|
|
|
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_CREATED)
|
|
|
|
|
.extract()
|
|
|
|
|
.header("location");
|
2018-09-05 10:44:03 +02:00
|
|
|
LOG.info("a {} repository is created: {}", repositoryType, url);
|
2018-08-06 16:16:19 +02:00
|
|
|
DEFAULT_REPOSITORIES.put(repositoryType, url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String repositoryJson(String repositoryType) {
|
2018-09-19 15:54:24 +02:00
|
|
|
return JSON_BUILDER
|
2018-08-06 16:16:19 +02:00
|
|
|
.add("contact", "zaphod.beeblebrox@hitchhiker.com")
|
|
|
|
|
.add("description", "Heart of Gold")
|
2018-10-15 18:35:45 +02:00
|
|
|
.add("name", getDefaultRepoName(repositoryType))
|
2018-08-06 16:16:19 +02:00
|
|
|
.add("archived", false)
|
|
|
|
|
.add("type", repositoryType)
|
|
|
|
|
.build().toString();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-15 18:35:45 +02:00
|
|
|
public static String getDefaultRepoName(String repositoryType) {
|
|
|
|
|
return "HeartOfGold-" + repositoryType;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:06:53 +02:00
|
|
|
public static String getGroupJson(String groupname , String desc) {
|
|
|
|
|
return JSON_BUILDER
|
|
|
|
|
.add("name", groupname)
|
|
|
|
|
.add("description", desc)
|
|
|
|
|
.build().toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static URI getGroupsUrl() {
|
|
|
|
|
return RestUtil.createResourceUrl("groups/");
|
2018-09-19 15:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static URI getUsersUrl() {
|
|
|
|
|
return RestUtil.createResourceUrl("users/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String createPasswordChangeJson(String oldPassword, String newPassword) {
|
|
|
|
|
return JSON_BUILDER
|
|
|
|
|
.add("oldPassword", oldPassword)
|
|
|
|
|
.add("newPassword", newPassword)
|
|
|
|
|
.build().toString();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 09:38:13 +02:00
|
|
|
public static void main(String[] args) {
|
|
|
|
|
cleanup();
|
|
|
|
|
}
|
2018-10-12 11:06:53 +02:00
|
|
|
|
2018-08-03 09:38:13 +02:00
|
|
|
}
|