2018-08-03 09:38:13 +02:00
|
|
|
package sonia.scm.it;
|
|
|
|
|
|
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;
|
2018-08-22 09:18:17 +02:00
|
|
|
import sonia.scm.repository.PermissionType;
|
2018-08-03 09:38:13 +02:00
|
|
|
import sonia.scm.web.VndMediaType;
|
|
|
|
|
|
2018-08-06 16:16:19 +02:00
|
|
|
import javax.json.Json;
|
|
|
|
|
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;
|
2018-08-03 09:38:13 +02:00
|
|
|
|
|
|
|
|
import static java.util.Arrays.asList;
|
|
|
|
|
import static sonia.scm.it.RestUtil.createResourceUrl;
|
|
|
|
|
import static sonia.scm.it.RestUtil.given;
|
2018-08-06 16:16:19 +02:00
|
|
|
import static sonia.scm.it.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";
|
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<>();
|
|
|
|
|
|
|
|
|
|
public static void createDefault() {
|
|
|
|
|
cleanup();
|
|
|
|
|
createDefaultRepositories();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 09:38:13 +02:00
|
|
|
public static void cleanup() {
|
|
|
|
|
cleanupRepositories();
|
|
|
|
|
cleanupGroups();
|
|
|
|
|
cleanupUsers();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-06 16:16:19 +02:00
|
|
|
public static String getDefaultRepositoryUrl(String repositoryType) {
|
|
|
|
|
return DEFAULT_REPOSITORIES.get(repositoryType);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 09:18:17 +02:00
|
|
|
public static void createUser(String username, String password) {
|
|
|
|
|
given(VndMediaType.USER)
|
|
|
|
|
.when()
|
|
|
|
|
.content(" {\n" +
|
|
|
|
|
" \"active\": true,\n" +
|
|
|
|
|
" \"admin\": false,\n" +
|
|
|
|
|
" \"creationDate\": \"2018-08-21T12:26:46.084Z\",\n" +
|
2018-08-22 11:19:19 +02:00
|
|
|
" \"displayName\": \"" + username + "\",\n" +
|
2018-08-22 09:18:17 +02:00
|
|
|
" \"mail\": \"user1@scm-manager.org\",\n" +
|
|
|
|
|
" \"name\": \"" + username + "\",\n" +
|
|
|
|
|
" \"password\": \"" + password + "\",\n" +
|
|
|
|
|
" \"type\": \"xml\"\n" +
|
|
|
|
|
" \n" +
|
|
|
|
|
" }")
|
|
|
|
|
.post(createResourceUrl("users"))
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_CREATED)
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void createUserPermission(String name, PermissionType permissionType, String repositoryType) {
|
|
|
|
|
given(VndMediaType.PERMISSION)
|
|
|
|
|
.when()
|
|
|
|
|
.content("{\n" +
|
2018-08-22 11:19:19 +02:00
|
|
|
"\t\"type\": \"" + permissionType.name() + "\",\n" +
|
|
|
|
|
"\t\"name\": \"" + name + "\",\n" +
|
2018-08-22 09:18:17 +02:00
|
|
|
"\t\"groupPermission\": false\n" +
|
|
|
|
|
"\t\n" +
|
|
|
|
|
"}")
|
2018-08-22 12:20:09 +02:00
|
|
|
.post(TestData.getDefaultPermissionUrl(USER_SCM_ADMIN, USER_SCM_ADMIN, repositoryType))
|
2018-08-22 09:18:17 +02:00
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_CREATED)
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 11:19:19 +02:00
|
|
|
public static List<Object> getUserPermissions(String username, String password, String repositoryType) {
|
|
|
|
|
return callUserPermissions(username, password, repositoryType, HttpStatus.SC_OK)
|
|
|
|
|
.extract()
|
|
|
|
|
.body().jsonPath().getList("");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ValidatableResponse callUserPermissions(String username, String password, String repositoryType, int expectedStatusCode) {
|
|
|
|
|
return given(VndMediaType.PERMISSION, username, password)
|
|
|
|
|
.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-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() {
|
|
|
|
|
for (String repositoryType : availableScmTypes()) {
|
|
|
|
|
String url = given(VndMediaType.REPOSITORY)
|
|
|
|
|
.body(repositoryJson(repositoryType))
|
|
|
|
|
|
|
|
|
|
.when()
|
|
|
|
|
.post(createResourceUrl("repositories"))
|
|
|
|
|
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_CREATED)
|
|
|
|
|
.extract()
|
|
|
|
|
.header("location");
|
|
|
|
|
DEFAULT_REPOSITORIES.put(repositoryType, url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String repositoryJson(String repositoryType) {
|
|
|
|
|
return Json.createObjectBuilder()
|
|
|
|
|
.add("contact", "zaphod.beeblebrox@hitchhiker.com")
|
|
|
|
|
.add("description", "Heart of Gold")
|
|
|
|
|
.add("name", "HeartOfGold-" + repositoryType)
|
|
|
|
|
.add("archived", false)
|
|
|
|
|
.add("type", repositoryType)
|
|
|
|
|
.build().toString();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 09:38:13 +02:00
|
|
|
public static void main(String[] args) {
|
|
|
|
|
cleanup();
|
|
|
|
|
}
|
|
|
|
|
}
|