mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 07:55:47 +01:00
Move tests to test classes
This commit is contained in:
@@ -41,13 +41,17 @@ import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import sonia.scm.repository.PermissionType;
|
||||
import sonia.scm.repository.client.api.RepositoryClient;
|
||||
import sonia.scm.repository.client.api.RepositoryClientException;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static sonia.scm.it.RepositoryUtil.addAndCommitRandomFile;
|
||||
import static sonia.scm.it.RestUtil.given;
|
||||
import static sonia.scm.it.ScmTypes.availableScmTypes;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
@@ -106,34 +110,56 @@ public class PermissionsITCase {
|
||||
assertEquals(1, Objects.requireNonNull(client.getWorkingCopy().list()).length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userWithREADPermissionShouldBeNotAuthorizedToCommit() throws IOException {
|
||||
assertFalse(RepositoryUtil.canUserCommit(USER_READ, USER_PASS, repositoryType, temporaryFolder));
|
||||
@Test(expected = RepositoryClientException.class)
|
||||
public void userWithReadPermissionShouldBeNotAuthorizedToCommit() throws IOException {
|
||||
createAndCommit(USER_READ);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userWithOwnerPermissionShouldBeAuthorizedToCommit() throws IOException {
|
||||
assertTrue(RepositoryUtil.canUserCommit(USER_OWNER, USER_PASS, repositoryType, temporaryFolder));
|
||||
createAndCommit(USER_OWNER);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userWithWritePermissionShouldBeAuthorizedToCommit() throws IOException {
|
||||
assertTrue(RepositoryUtil.canUserCommit(USER_WRITE, USER_PASS, repositoryType, temporaryFolder));
|
||||
createAndCommit(USER_WRITE);
|
||||
}
|
||||
|
||||
private void createAndCommit(String username) throws IOException {
|
||||
RepositoryClient client = RepositoryUtil.createRepositoryClient(repositoryType, temporaryFolder.newFolder(), username, PermissionsITCase.USER_PASS);
|
||||
addAndCommitRandomFile(client, username);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userWithWOwnerPermissionShouldBeAuthorizedToDeleteRepository(){
|
||||
RepositoryUtil.assertDeleteRepositoryOperation(USER_OWNER, HttpStatus.SC_NO_CONTENT, HttpStatus.SC_NOT_FOUND, USER_PASS, repositoryType);
|
||||
public void userWithOwnerPermissionShouldBeAuthorizedToDeleteRepository(){
|
||||
assertDeleteRepositoryOperation(HttpStatus.SC_NO_CONTENT, HttpStatus.SC_NOT_FOUND, USER_OWNER, repositoryType);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userWithWReadPermissionShouldNotBeAuthorizedToDeleteRepository(){
|
||||
RepositoryUtil.assertDeleteRepositoryOperation(USER_READ, HttpStatus.SC_FORBIDDEN, HttpStatus.SC_OK, USER_PASS, repositoryType);
|
||||
public void userWithReadPermissionShouldNotBeAuthorizedToDeleteRepository(){
|
||||
assertDeleteRepositoryOperation(HttpStatus.SC_FORBIDDEN, HttpStatus.SC_OK, USER_READ, repositoryType);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userWithWWritePermissionShouldNotBeAuthorizedToDeleteRepository(){
|
||||
RepositoryUtil.assertDeleteRepositoryOperation(USER_WRITE, HttpStatus.SC_FORBIDDEN, HttpStatus.SC_OK, USER_PASS, repositoryType);
|
||||
public void userWithWritePermissionShouldNotBeAuthorizedToDeleteRepository(){
|
||||
assertDeleteRepositoryOperation(HttpStatus.SC_FORBIDDEN, HttpStatus.SC_OK, USER_WRITE, repositoryType);
|
||||
}
|
||||
|
||||
private void assertDeleteRepositoryOperation(int expectedDeleteStatus, int expectedGetStatus, String user, String repositoryType) {
|
||||
given(VndMediaType.REPOSITORY, user, PermissionsITCase.USER_PASS)
|
||||
|
||||
.when()
|
||||
.delete(TestData.getDefaultRepositoryUrl(repositoryType))
|
||||
|
||||
.then()
|
||||
.statusCode(expectedDeleteStatus);
|
||||
|
||||
given(VndMediaType.REPOSITORY, user, PermissionsITCase.USER_PASS)
|
||||
|
||||
.when()
|
||||
.get(TestData.getDefaultRepositoryUrl(repositoryType))
|
||||
|
||||
.then()
|
||||
.statusCode(expectedGetStatus);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ package sonia.scm.it;
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -52,7 +53,6 @@ import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static sonia.scm.it.RegExMatcher.matchesPattern;
|
||||
import static sonia.scm.it.RestUtil.createResourceUrl;
|
||||
import static sonia.scm.it.RestUtil.given;
|
||||
@@ -142,7 +142,10 @@ public class RepositoriesITCase {
|
||||
|
||||
@Test
|
||||
public void shouldCommitFiles() throws IOException {
|
||||
assertTrue(RepositoryUtil.canScmAdminCommit(repositoryType,temporaryFolder));
|
||||
RepositoryClient client = RepositoryUtil.createRepositoryClient(repositoryType, temporaryFolder.newFolder(), "scmadmin", "scmadmin");
|
||||
String name = RepositoryUtil.addAndCommitRandomFile(client, "scmadmin");
|
||||
RepositoryClient checkClient = RepositoryUtil.createRepositoryClient(repositoryType, temporaryFolder.newFolder(), "scmadmin", "scmadmin");
|
||||
Assertions.assertThat(checkClient.getWorkingCopy().list()).contains(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ public class RepositoryAccessITCase {
|
||||
assumeFalse("There are no branches for SVN", repositoryType.equals("svn"));
|
||||
|
||||
RepositoryClient repositoryClient = RepositoryUtil.createRepositoryClient(repositoryType, folder);
|
||||
RepositoryUtil.createAndCommitFile(folder, repositoryClient, "scmadmin", "a.txt", "a");
|
||||
RepositoryUtil.createAndCommitFile(repositoryClient, "scmadmin", "a.txt", "a");
|
||||
|
||||
String branchesUrl = given()
|
||||
.when()
|
||||
|
||||
@@ -3,17 +3,14 @@ package sonia.scm.it;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.Files;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import sonia.scm.repository.Changeset;
|
||||
import sonia.scm.repository.Person;
|
||||
import sonia.scm.repository.client.api.ClientCommand;
|
||||
import sonia.scm.repository.client.api.RepositoryClient;
|
||||
import sonia.scm.repository.client.api.RepositoryClientException;
|
||||
import sonia.scm.repository.client.api.RepositoryClientFactory;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -23,35 +20,6 @@ public class RepositoryUtil {
|
||||
|
||||
private static final RepositoryClientFactory REPOSITORY_CLIENT_FACTORY = new RepositoryClientFactory();
|
||||
|
||||
static void addRandomFileToRepository(RepositoryClient client) throws IOException {
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
String name = "file-" + uuid + ".uuid";
|
||||
|
||||
File file = new File(client.getWorkingCopy(), name);
|
||||
try (FileOutputStream out = new FileOutputStream(file)) {
|
||||
out.write(uuid.getBytes());
|
||||
}
|
||||
client.getAddCommand().add(name);
|
||||
}
|
||||
|
||||
static boolean canScmAdminCommit(String repositoryType, TemporaryFolder temporaryFolder) throws IOException {
|
||||
return canUserCommit("scmadmin", "scmadmin", repositoryType, temporaryFolder);
|
||||
}
|
||||
|
||||
static boolean canUserCommit(String username, String password, String repositoryType, TemporaryFolder temporaryFolder) throws IOException {
|
||||
RepositoryClient client = RepositoryUtil.createRepositoryClient(repositoryType, temporaryFolder.newFolder(), username, password);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
addRandomFileToRepository(client);
|
||||
}
|
||||
try{
|
||||
commit(client, username, "commit");
|
||||
}catch (RepositoryClientException e){
|
||||
return false;
|
||||
}
|
||||
RepositoryClient checkClient = RepositoryUtil.createRepositoryClient(repositoryType, temporaryFolder.newFolder(), username, password);
|
||||
return checkClient.getWorkingCopy().list().length == 6;
|
||||
}
|
||||
|
||||
static RepositoryClient createRepositoryClient(String repositoryType, File folder) throws IOException {
|
||||
return createRepositoryClient(repositoryType, folder, "scmadmin", "scmadmin");
|
||||
}
|
||||
@@ -69,25 +37,16 @@ public class RepositoryUtil {
|
||||
|
||||
return REPOSITORY_CLIENT_FACTORY.create(repositoryType, httpProtocolUrl, username, password, folder);
|
||||
}
|
||||
static void assertDeleteRepositoryOperation(String user, int deleteStatus, int getStatus, String password, String repositoryType) {
|
||||
given(VndMediaType.REPOSITORY, user, password)
|
||||
|
||||
.when()
|
||||
.delete(TestData.getDefaultRepositoryUrl(repositoryType))
|
||||
|
||||
.then()
|
||||
.statusCode(deleteStatus);
|
||||
|
||||
given(VndMediaType.REPOSITORY, user, password)
|
||||
|
||||
.when()
|
||||
.get(TestData.getDefaultRepositoryUrl(repositoryType))
|
||||
|
||||
.then()
|
||||
.statusCode(getStatus);
|
||||
static String addAndCommitRandomFile(RepositoryClient client, String username) throws IOException {
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
String name = "file-" + uuid + ".uuid";
|
||||
createAndCommitFile(client, username, name, uuid);
|
||||
return name;
|
||||
}
|
||||
static void createAndCommitFile(File folder, RepositoryClient repositoryClient, String username, String fileName, String content) throws IOException {
|
||||
Files.write(content, new File(folder, fileName), Charsets.UTF_8);
|
||||
|
||||
static void createAndCommitFile(RepositoryClient repositoryClient, String username, String fileName, String content) throws IOException {
|
||||
Files.write(content, new File(repositoryClient.getWorkingCopy(), fileName), Charsets.UTF_8);
|
||||
repositoryClient.getAddCommand().add(fileName);
|
||||
commit(repositoryClient, username, "added " + fileName);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user