package sonia.scm.it.utils; import io.restassured.response.ValidatableResponse; import org.apache.http.HttpStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sonia.scm.web.VndMediaType; import javax.json.Json; import javax.json.JsonObjectBuilder; import java.net.URI; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import static java.util.Arrays.asList; import static sonia.scm.it.utils.RestUtil.createResourceUrl; import static sonia.scm.it.utils.RestUtil.given; import static sonia.scm.it.utils.ScmTypes.availableScmTypes; public class TestData { private static final Logger LOG = LoggerFactory.getLogger(TestData.class); public static final String USER_SCM_ADMIN = "scmadmin"; public static final String USER_ANONYMOUS = "anonymous"; public static final Collection READ = asList("read", "pull"); public static final Collection WRITE = asList("read", "write", "pull", "push"); public static final Collection OWNER = asList("*"); private static final List PROTECTED_USERS = asList(USER_SCM_ADMIN, USER_ANONYMOUS); private static Map DEFAULT_REPOSITORIES = new HashMap<>(); public static final JsonObjectBuilder JSON_BUILDER = NullAwareJsonObjectBuilder.wrap(Json.createObjectBuilder()); public static void createDefault() { cleanup(); createDefaultRepositories(); } public static void cleanup() { LOG.info("start to clean up to integration tests"); cleanupRepositories(); cleanupGroups(); cleanupUsers(); } public static String getDefaultRepositoryUrl(String repositoryType) { return DEFAULT_REPOSITORIES.get(repositoryType); } public static void createNotAdminUser(String username, String password) { createUser(username, password, false, "xml", "user1@scm-manager.org"); } public static void createUser(String username, String password, boolean isAdmin, String type, final String email) { LOG.info("create user with username: {}", username); String admin = isAdmin ? "true" : "false"; given(VndMediaType.USER) .when() .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") .append(" \"mail\": \"" + email + "\",\n") .append(" \"name\": \"").append(username).append("\",\n") .append(" \"password\": \"").append(password).append("\",\n") .append(" \"type\": \"").append(type).append("\"\n") .append(" }").toString()) .post(getUsersUrl()) .then() .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); } private static URI getPermissionUrl(String username) { return RestUtil.createResourceUrl(String.format("users/%s/permissions", username)); } 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) ; } public static void createUserPermission(String username, Collection verbs, String repositoryType) { String defaultPermissionUrl = TestData.getDefaultPermissionUrl(USER_SCM_ADMIN, USER_SCM_ADMIN, repositoryType); LOG.info("create permission with name {} and verbs {} using the endpoint: {}", username, verbs, defaultPermissionUrl); given(VndMediaType.REPOSITORY_PERMISSION) .when() .content("{\n" + "\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" + "\t\"groupPermission\": false\n" + "\t\n" + "}") .post(defaultPermissionUrl) .then() .statusCode(HttpStatus.SC_CREATED) ; } public static List getUserPermissions(String username, String password, String repositoryType) { return callUserPermissions(username, password, repositoryType, HttpStatus.SC_OK) .extract() .body().jsonPath().getList("_embedded.permissions"); } public static ValidatableResponse callUserPermissions(String username, String password, String repositoryType, int expectedStatusCode) { return given(VndMediaType.REPOSITORY_PERMISSION, username, password) .when() .get(TestData.getDefaultPermissionUrl(username, password, repositoryType)) .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); } 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"); } private static void cleanupRepositories() { LOG.info("clean up repository"); List 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); DEFAULT_REPOSITORIES.clear(); } private static void cleanupGroups() { List 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); } private static void cleanupUsers() { List 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); } private static void delete(String url) { given(VndMediaType.REPOSITORY) .when() .delete(url) .then() .statusCode(HttpStatus.SC_NO_CONTENT); LOG.info("deleted {}", url); } private static void createDefaultRepositories() { LOG.info("create default repositories"); for (String repositoryType : availableScmTypes()) { String url = given(VndMediaType.REPOSITORY) .body(repositoryJson(repositoryType)) .when() .post(createResourceUrl("repositories")) .then() .statusCode(HttpStatus.SC_CREATED) .extract() .header("location"); LOG.info("a {} repository is created: {}", repositoryType, url); DEFAULT_REPOSITORIES.put(repositoryType, url); } } public static String repositoryJson(String repositoryType) { return JSON_BUILDER .add("contact", "zaphod.beeblebrox@hitchhiker.com") .add("description", "Heart of Gold") .add("name", getDefaultRepoName(repositoryType)) .add("archived", false) .add("type", repositoryType) .build().toString(); } public static String getDefaultRepoName(String repositoryType) { return "HeartOfGold-" + repositoryType; } 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/"); } 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(); } public static void main(String[] args) { cleanup(); } }