2018-08-22 09:18:17 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010, Sebastian Sdorra
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
* 3. Neither the name of SCM-Manager; nor the names of its
|
|
|
|
|
* contributors may be used to endorse or promote products derived from this
|
|
|
|
|
* software without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*
|
|
|
|
|
* http://bitbucket.org/sdorra/scm-manager
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package sonia.scm.it;
|
|
|
|
|
|
|
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Rule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.rules.TemporaryFolder;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
import org.junit.runners.Parameterized;
|
|
|
|
|
import org.junit.runners.Parameterized.Parameters;
|
2018-09-19 15:54:24 +02:00
|
|
|
import sonia.scm.it.utils.RepositoryUtil;
|
|
|
|
|
import sonia.scm.it.utils.TestData;
|
2018-08-22 09:18:17 +02:00
|
|
|
import sonia.scm.repository.PermissionType;
|
|
|
|
|
import sonia.scm.repository.client.api.RepositoryClient;
|
2018-08-22 10:59:46 +02:00
|
|
|
import sonia.scm.repository.client.api.RepositoryClientException;
|
|
|
|
|
import sonia.scm.web.VndMediaType;
|
2018-08-22 09:18:17 +02:00
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
2018-08-22 10:59:46 +02:00
|
|
|
import static org.junit.Assert.assertEquals;
|
2018-08-22 12:20:09 +02:00
|
|
|
import static org.junit.Assert.assertNull;
|
2018-09-19 15:54:24 +02:00
|
|
|
import static sonia.scm.it.utils.RepositoryUtil.addAndCommitRandomFile;
|
|
|
|
|
import static sonia.scm.it.utils.RestUtil.given;
|
|
|
|
|
import static sonia.scm.it.utils.ScmTypes.availableScmTypes;
|
|
|
|
|
import static sonia.scm.it.utils.TestData.USER_SCM_ADMIN;
|
|
|
|
|
import static sonia.scm.it.utils.TestData.callRepository;
|
2018-08-22 09:18:17 +02:00
|
|
|
|
|
|
|
|
@RunWith(Parameterized.class)
|
|
|
|
|
public class PermissionsITCase {
|
|
|
|
|
|
|
|
|
|
public static final String USER_READ = "user_read";
|
|
|
|
|
public static final String USER_PASS = "pass";
|
|
|
|
|
private static final String USER_WRITE = "user_write";
|
|
|
|
|
private static final String USER_OWNER = "user_owner";
|
2018-08-22 11:19:19 +02:00
|
|
|
private static final String USER_OTHER = "user_other";
|
2018-08-22 09:18:17 +02:00
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
|
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
|
|
|
|
|
|
|
|
|
private final String repositoryType;
|
|
|
|
|
private int createdPermissions;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public PermissionsITCase(String repositoryType) {
|
|
|
|
|
this.repositoryType = repositoryType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Parameters(name = "{0}")
|
|
|
|
|
public static Collection<String> createParameters() {
|
|
|
|
|
return availableScmTypes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void prepareEnvironment() {
|
|
|
|
|
TestData.createDefault();
|
2018-10-12 11:06:53 +02:00
|
|
|
TestData.createNotAdminUser(USER_READ, USER_PASS);
|
2018-08-22 09:18:17 +02:00
|
|
|
TestData.createUserPermission(USER_READ, PermissionType.READ, repositoryType);
|
2018-10-12 11:06:53 +02:00
|
|
|
TestData.createNotAdminUser(USER_WRITE, USER_PASS);
|
2018-08-22 09:18:17 +02:00
|
|
|
TestData.createUserPermission(USER_WRITE, PermissionType.WRITE, repositoryType);
|
2018-10-12 11:06:53 +02:00
|
|
|
TestData.createNotAdminUser(USER_OWNER, USER_PASS);
|
2018-08-22 09:18:17 +02:00
|
|
|
TestData.createUserPermission(USER_OWNER, PermissionType.OWNER, repositoryType);
|
2018-10-12 11:06:53 +02:00
|
|
|
TestData.createNotAdminUser(USER_OTHER, USER_PASS);
|
2018-08-22 09:18:17 +02:00
|
|
|
createdPermissions = 3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2018-08-22 12:20:09 +02:00
|
|
|
public void readUserShouldNotSeePermissions() {
|
2018-08-27 13:04:05 +02:00
|
|
|
assertNull(callRepository(USER_READ, USER_PASS, repositoryType, HttpStatus.SC_OK)
|
2018-08-22 12:20:09 +02:00
|
|
|
.extract()
|
|
|
|
|
.body().jsonPath().getString("_links.permissions.href"));
|
2018-08-22 11:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2018-08-22 12:20:09 +02:00
|
|
|
public void readUserShouldNotSeeBruteForcePermissions() {
|
|
|
|
|
given(VndMediaType.PERMISSION, USER_READ, USER_PASS)
|
|
|
|
|
.when()
|
|
|
|
|
.get(TestData.getDefaultPermissionUrl(USER_SCM_ADMIN, USER_SCM_ADMIN, repositoryType))
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_FORBIDDEN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void writeUserShouldNotSeePermissions() {
|
|
|
|
|
assertNull(callRepository(USER_WRITE, USER_PASS, repositoryType, HttpStatus.SC_OK)
|
|
|
|
|
.extract()
|
|
|
|
|
.body().jsonPath().getString("_links.permissions.href"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void writeUserShouldNotSeeBruteForcePermissions() {
|
|
|
|
|
given(VndMediaType.PERMISSION, USER_WRITE, USER_PASS)
|
|
|
|
|
.when()
|
|
|
|
|
.get(TestData.getDefaultPermissionUrl(USER_SCM_ADMIN, USER_SCM_ADMIN, repositoryType))
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_FORBIDDEN);
|
2018-08-22 11:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void ownerShouldSeePermissions() {
|
|
|
|
|
List<Object> userPermissions = TestData.getUserPermissions(USER_OWNER, USER_PASS, repositoryType);
|
2018-08-22 09:18:17 +02:00
|
|
|
assertEquals(userPermissions.size(), createdPermissions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2018-08-22 12:20:09 +02:00
|
|
|
public void otherUserShouldNotSeeRepository() {
|
|
|
|
|
callRepository(USER_OTHER, USER_PASS, repositoryType, HttpStatus.SC_FORBIDDEN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void otherUserShouldNotSeeBruteForcePermissions() {
|
|
|
|
|
given(VndMediaType.PERMISSION, USER_OTHER, USER_PASS)
|
|
|
|
|
.when()
|
|
|
|
|
.get(TestData.getDefaultPermissionUrl(USER_SCM_ADMIN, USER_SCM_ADMIN, repositoryType))
|
|
|
|
|
.then()
|
|
|
|
|
.statusCode(HttpStatus.SC_FORBIDDEN);
|
2018-08-22 11:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void readUserShouldCloneRepository() throws IOException {
|
2018-08-22 09:18:17 +02:00
|
|
|
RepositoryClient client = RepositoryUtil.createRepositoryClient(repositoryType, temporaryFolder.newFolder(), USER_READ, USER_PASS);
|
|
|
|
|
assertEquals(1, Objects.requireNonNull(client.getWorkingCopy().list()).length);
|
2018-08-22 11:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void writeUserShouldCloneRepository() throws IOException {
|
|
|
|
|
RepositoryClient client = RepositoryUtil.createRepositoryClient(repositoryType, temporaryFolder.newFolder(), USER_WRITE, USER_PASS);
|
2018-08-22 09:18:17 +02:00
|
|
|
assertEquals(1, Objects.requireNonNull(client.getWorkingCopy().list()).length);
|
2018-08-22 11:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void ownerShouldCloneRepository() throws IOException {
|
|
|
|
|
RepositoryClient client = RepositoryUtil.createRepositoryClient(repositoryType, temporaryFolder.newFolder(), USER_OWNER, USER_PASS);
|
2018-08-22 09:18:17 +02:00
|
|
|
assertEquals(1, Objects.requireNonNull(client.getWorkingCopy().list()).length);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 11:19:19 +02:00
|
|
|
@Test
|
|
|
|
|
public void otherUserShouldNotCloneRepository() {
|
|
|
|
|
TestData.callRepository(USER_OTHER, USER_PASS, repositoryType, HttpStatus.SC_FORBIDDEN);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 10:59:46 +02:00
|
|
|
@Test(expected = RepositoryClientException.class)
|
|
|
|
|
public void userWithReadPermissionShouldBeNotAuthorizedToCommit() throws IOException {
|
|
|
|
|
createAndCommit(USER_READ);
|
2018-08-22 09:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void userWithOwnerPermissionShouldBeAuthorizedToCommit() throws IOException {
|
2018-08-22 10:59:46 +02:00
|
|
|
createAndCommit(USER_OWNER);
|
2018-08-22 09:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void userWithWritePermissionShouldBeAuthorizedToCommit() throws IOException {
|
2018-08-22 10:59:46 +02:00
|
|
|
createAndCommit(USER_WRITE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createAndCommit(String username) throws IOException {
|
|
|
|
|
RepositoryClient client = RepositoryUtil.createRepositoryClient(repositoryType, temporaryFolder.newFolder(), username, PermissionsITCase.USER_PASS);
|
|
|
|
|
addAndCommitRandomFile(client, username);
|
2018-08-22 09:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2018-08-22 10:59:46 +02:00
|
|
|
public void userWithOwnerPermissionShouldBeAuthorizedToDeleteRepository(){
|
|
|
|
|
assertDeleteRepositoryOperation(HttpStatus.SC_NO_CONTENT, HttpStatus.SC_NOT_FOUND, USER_OWNER, repositoryType);
|
2018-08-22 09:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2018-08-22 10:59:46 +02:00
|
|
|
public void userWithReadPermissionShouldNotBeAuthorizedToDeleteRepository(){
|
|
|
|
|
assertDeleteRepositoryOperation(HttpStatus.SC_FORBIDDEN, HttpStatus.SC_OK, USER_READ, repositoryType);
|
2018-08-22 09:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2018-08-22 10:59:46 +02:00
|
|
|
public void userWithWritePermissionShouldNotBeAuthorizedToDeleteRepository(){
|
|
|
|
|
assertDeleteRepositoryOperation(HttpStatus.SC_FORBIDDEN, HttpStatus.SC_OK, USER_WRITE, repositoryType);
|
2018-08-22 09:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-22 10:59:46 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2018-08-22 09:18:17 +02:00
|
|
|
}
|