Merge imported repository permissions with existing repository permissions (#1520)

This commit is contained in:
Eduard Heimbuch
2021-02-05 12:20:55 +01:00
committed by GitHub
parent ac5d145266
commit 877713574a
7 changed files with 218 additions and 6 deletions

View File

@@ -36,6 +36,7 @@ import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryPermission;
import sonia.scm.repository.RepositoryTestData;
import sonia.scm.repository.api.ImportFailedException;
import sonia.scm.repository.api.RepositoryService;
@@ -47,13 +48,13 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Collection;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -105,9 +106,13 @@ class FullScmRepositoryImporterTest {
void shouldImportScmRepositoryArchive() throws IOException {
when(compatibilityChecker.check(any())).thenReturn(true);
when(repositoryManager.create(eq(REPOSITORY), any())).thenReturn(REPOSITORY);
Collection<RepositoryPermission> existingPermissions = REPOSITORY.getPermissions();
Repository repository = fullImporter.importFromStream(REPOSITORY, Resources.getResource("sonia/scm/repository/import/scm-import.tar.gz").openStream());
assertThat(repository).isEqualTo(REPOSITORY);
verify(storeImporter).importFromTarArchive(eq(REPOSITORY), any(InputStream.class));
verify(repositoryManager).modify(REPOSITORY);
Collection<RepositoryPermission> updatedPermissions = REPOSITORY.getPermissions();
assertThat(updatedPermissions).isNotEqualTo(existingPermissions);
}
}

View File

@@ -0,0 +1,125 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.importexport;
import com.google.common.collect.ImmutableSet;
import org.junit.jupiter.api.Test;
import sonia.scm.repository.RepositoryPermission;
import java.util.Collection;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
class RepositoryImportPermissionMergerTest {
private final RepositoryImportPermissionMerger merger = new RepositoryImportPermissionMerger();
@Test
void shouldReturnExistingPermissionsIfNoImportedPermissions() {
RepositoryPermission owner = new RepositoryPermission("owner", ImmutableSet.of("*"), false);
Collection<RepositoryPermission> existing = ImmutableSet.of(owner);
Collection<RepositoryPermission> imported = Collections.emptySet();
Collection<RepositoryPermission> result = merger.merge(existing, imported);
assertThat(result)
.hasSize(1)
.contains(owner);
}
@Test
void shouldReturnMergedPermissions() {
RepositoryPermission owner = new RepositoryPermission("owner", ImmutableSet.of("*"), false);
RepositoryPermission trillian = new RepositoryPermission("trillian", ImmutableSet.of("read"), false);
Collection<RepositoryPermission> existing = ImmutableSet.of(owner);
Collection<RepositoryPermission> imported = ImmutableSet.of(trillian);
Collection<RepositoryPermission> result = merger.merge(existing, imported);
assertThat(result)
.hasSize(2)
.contains(owner)
.contains(trillian);
}
@Test
void shouldReturnOnlyMergePermissionIfNotExistYet() {
RepositoryPermission owner = new RepositoryPermission("owner", ImmutableSet.of("*"), false);
RepositoryPermission trillian = new RepositoryPermission("trillian", ImmutableSet.of("read", "write"), false);
RepositoryPermission importedOwner = new RepositoryPermission("owner", ImmutableSet.of("read, write"), false);
RepositoryPermission importedTrillian = new RepositoryPermission("trillian", ImmutableSet.of("read"), false);
Collection<RepositoryPermission> existing = ImmutableSet.of(owner, trillian);
Collection<RepositoryPermission> imported = ImmutableSet.of(importedOwner, importedTrillian);
Collection<RepositoryPermission> result = merger.merge(existing, imported);
assertThat(result)
.hasSize(2)
.contains(owner)
.contains(trillian)
.doesNotContain(importedOwner)
.doesNotContain(importedTrillian);
}
@Test
void shouldAddPermissionWithSameNameIfOneIsGroupPermission() {
RepositoryPermission owner = new RepositoryPermission("owner", ImmutableSet.of("*"), false);
RepositoryPermission trillian = new RepositoryPermission("trillian", ImmutableSet.of("read", "write"), false);
RepositoryPermission importedOwner = new RepositoryPermission("owner", ImmutableSet.of("read, write"), true);
RepositoryPermission importedTrillian = new RepositoryPermission("trillian", ImmutableSet.of("read"), true);
Collection<RepositoryPermission> existing = ImmutableSet.of(owner, trillian);
Collection<RepositoryPermission> imported = ImmutableSet.of(importedOwner, importedTrillian);
Collection<RepositoryPermission> result = merger.merge(existing, imported);
assertThat(result)
.hasSize(4)
.contains(owner)
.contains(trillian)
.contains(importedOwner)
.contains(importedTrillian);
}
@Test
void shouldNotAddPermissionMultipleTimes() {
RepositoryPermission owner = new RepositoryPermission("owner", ImmutableSet.of("*"), false);
RepositoryPermission importedTrillian1 = new RepositoryPermission("trillian", ImmutableSet.of("read, write"), false);
RepositoryPermission importedTrillian2 = new RepositoryPermission("trillian", ImmutableSet.of("read"), false);
Collection<RepositoryPermission> existing = ImmutableSet.of(owner);
Collection<RepositoryPermission> imported = ImmutableSet.of(importedTrillian1, importedTrillian2);
Collection<RepositoryPermission> result = merger.merge(existing, imported);
assertThat(result)
.hasSize(2)
.contains(owner)
.contains(importedTrillian1)
.doesNotContain(importedTrillian2);
}
}