Add namespace cli commands (#2093)

Adds the CLI commands that are available to handle repository permissions on repositories for namespaces.

Co-authored-by: Eduard Heimbuch <eduard.heimbuch@cloudogu.com>
This commit is contained in:
René Pfeuffer
2022-07-26 09:58:00 +02:00
committed by GitHub
parent c190821a5e
commit 67a9dce7e6
38 changed files with 1599 additions and 403 deletions

View File

@@ -0,0 +1,204 @@
/*
* 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.repository.cli;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.cli.CommandValidator;
import sonia.scm.cli.PermissionDescriptionResolver;
import sonia.scm.repository.Namespace;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.NamespaceManager;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryPermission;
import sonia.scm.repository.RepositoryRole;
import sonia.scm.repository.RepositoryRoleManager;
import sonia.scm.repository.RepositoryTestData;
import java.util.Collection;
import java.util.List;
import static java.util.Optional.of;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class NamespacePermissionsListCommandTest {
@Mock
private RepositoryTemplateRenderer templateRenderer;
@Mock
private CommandValidator validator;
@Mock
private NamespaceManager manager;
@Mock
private RepositoryRoleManager roleManager;
@Mock
private PermissionDescriptionResolver permissionDescriptionResolver;
@InjectMocks
private RepositoryPermissionBeanMapper beanMapper;
private NamespacePermissionsListCommand command;
@BeforeEach
void initCommand() {
command = new NamespacePermissionsListCommand(templateRenderer, validator, manager, beanMapper);
}
@Test
void shouldPrintNotFoundErrorForUnknownRepository() {
command.setNamespace("hg2g");
command.run();
verify(templateRenderer).renderNotFoundError();
}
@Nested
class ForExistingRepository {
@Captor
private ArgumentCaptor<Collection<RepositoryPermissionBean>> permissionsCaptor;
private final Namespace namespace = new Namespace("hitchhiker");
@BeforeEach
void mockRepository() {
when(manager.get("hitchhiker"))
.thenReturn(of(namespace));
command.setNamespace("hitchhiker");
}
@Nested
class WithoutVerboseFlag {
@BeforeEach
void setUpRenderer() {
doNothing().when(templateRenderer).render(permissionsCaptor.capture());
}
@Test
void shouldRenderEmptyTableWithoutPermissions() {
command.run();
Collection<RepositoryPermissionBean> beans = permissionsCaptor.getValue();
assertThat(beans).isEmpty();
}
@Test
void shouldListCustomUserPermission() {
RepositoryPermission permission = new RepositoryPermission("trillian", List.of("read", "write"), false);
namespace.setPermissions(List.of(permission));
when(permissionDescriptionResolver.getDescription("read"))
.thenReturn(of("read repository"));
when(permissionDescriptionResolver.getDescription("write"))
.thenReturn(of("write repository"));
command.run();
Collection<RepositoryPermissionBean> beans = permissionsCaptor.getValue();
assertThat(beans).extracting("groupPermission", "name", "role")
.containsExactly(tuple(false, "trillian", "CUSTOM"));
}
}
@Nested
class WithVerboseFlag {
@BeforeEach
void setUpRenderer() {
doNothing().when(templateRenderer).renderVerbose(permissionsCaptor.capture());
}
@BeforeEach
void setVerbose() {
command.setVerbose(true);
}
@Test
void shouldListUserPermissionWithVerbs() {
RepositoryPermission permission = new RepositoryPermission("trillian", List.of("read", "write"), false);
namespace.setPermissions(List.of(permission));
when(permissionDescriptionResolver.getDescription("read"))
.thenReturn(of("read repository"));
when(permissionDescriptionResolver.getDescription("write"))
.thenReturn(of("write repository"));
command.run();
Collection<RepositoryPermissionBean> beans = permissionsCaptor.getValue();
assertThat(beans).extracting("groupPermission", "name", "role", "verbs")
.containsExactly(tuple(false, "trillian", "CUSTOM", List.of("read repository", "write repository")));
}
@Test
void shouldListUserPermissionWithRole() {
RepositoryPermission permission = new RepositoryPermission("trillian", "READ", false);
namespace.setPermissions(List.of(permission));
when(roleManager.get("READ"))
.thenReturn(new RepositoryRole("READ", List.of("read", "pull"), ""));
when(permissionDescriptionResolver.getDescription("read"))
.thenReturn(of("read repository"));
when(permissionDescriptionResolver.getDescription("pull"))
.thenReturn(of("clone/checkout repository"));
command.run();
Collection<RepositoryPermissionBean> beans = permissionsCaptor.getValue();
assertThat(beans).extracting("groupPermission", "name", "verbs")
.containsExactly(tuple(false, "trillian", List.of("read repository", "clone/checkout repository")));
}
@Test
void shouldListUserPermissionWithVerbsAsKeys() {
RepositoryPermission permission = new RepositoryPermission("trillian", List.of("read", "write"), false);
namespace.setPermissions(List.of(permission));
command.setKeys(true);
command.run();
Collection<RepositoryPermissionBean> beans = permissionsCaptor.getValue();
assertThat(beans).extracting("groupPermission", "name", "role")
.containsExactly(tuple(false, "trillian", "CUSTOM"));
assertThat(beans).extracting("verbs")
.map(c -> ((Collection) c).stream().collect(toList())) // to satisfy equal in the comparison, we have to use this form
.containsExactly(List.of("read", "write"));
}
}
}
}

View File

@@ -43,7 +43,6 @@ import sonia.scm.repository.RepositoryTestData;
import java.util.List;
import java.util.Set;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
@@ -91,7 +90,7 @@ class RepositoryPermissionsAddCommandTest {
@Test
void shouldSetMultipleVerbsForNewUser() {
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("trillian");
command.setVerbs("read", "pull", "push");
@@ -112,7 +111,7 @@ class RepositoryPermissionsAddCommandTest {
)
);
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("trillian");
command.setVerbs("write");
@@ -133,7 +132,7 @@ class RepositoryPermissionsAddCommandTest {
)
);
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("hog");
command.setVerbs("write");
command.setForGroup(true);
@@ -157,7 +156,7 @@ class RepositoryPermissionsAddCommandTest {
when(roleManager.get("READ"))
.thenReturn(new RepositoryRole("READ", List.of("read", "pull"), ""));
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("trillian");
command.setVerbs("write");
@@ -180,7 +179,7 @@ class RepositoryPermissionsAddCommandTest {
when(roleManager.get("READ"))
.thenReturn(new RepositoryRole("READ", List.of("read", "pull"), ""));
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("trillian");
command.setVerbs("read");
@@ -192,19 +191,19 @@ class RepositoryPermissionsAddCommandTest {
@Test
void shouldHandleMissingVerb() {
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("trillian");
command.setVerbs("make-party");
command.run();
verify(templateRenderer).renderVerbNotFoundError();
verify(templateRenderer).renderVerbNotFoundError("make-party");
}
}
@Test
void shouldHandleIllegalNamespaceNameParameter() {
command.setRepositoryName("illegal name");
command.setRepositoryNamespaceAndName("illegal name");
command.setName("trillian");
command.setVerbs("write");
@@ -215,7 +214,7 @@ class RepositoryPermissionsAddCommandTest {
@Test
void shouldHandleNotExistingRepository() {
command.setRepositoryName("no/repository");
command.setRepositoryNamespaceAndName("no/repository");
command.setName("trillian");
command.setVerbs("write");

View File

@@ -74,7 +74,7 @@ class RepositoryPermissionsClearCommandTest {
)
);
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("trillian");
command.run();
@@ -93,7 +93,7 @@ class RepositoryPermissionsClearCommandTest {
)
);
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("hog");
command.setForGroup(true);
@@ -108,7 +108,7 @@ class RepositoryPermissionsClearCommandTest {
@Test
void shouldHandleIllegalNamespaceNameParameter() {
command.setRepositoryName("illegal name");
command.setRepositoryNamespaceAndName("illegal name");
command.setName("trillian");
command.run();
@@ -118,7 +118,7 @@ class RepositoryPermissionsClearCommandTest {
@Test
void shouldHandleNotExistingRepository() {
command.setRepositoryName("no/repository");
command.setRepositoryNamespaceAndName("no/repository");
command.setName("trillian");
command.run();

View File

@@ -69,8 +69,15 @@ class RepositoryPermissionsListCommandTest {
private PermissionDescriptionResolver permissionDescriptionResolver;
@InjectMocks
private RepositoryPermissionBeanMapper beanMapper;
private RepositoryPermissionsListCommand command;
@BeforeEach
void initCommand() {
command = new RepositoryPermissionsListCommand(templateRenderer, validator, manager, beanMapper);
}
@Test
void shouldPrintNotFoundErrorForUnknownRepository() {
command.setRepository("hg2g/hog");

View File

@@ -82,7 +82,7 @@ class RepositoryPermissionsRemoveCommandTest {
)
);
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("dent");
command.setVerbs("write", "push", "pull");
@@ -103,7 +103,7 @@ class RepositoryPermissionsRemoveCommandTest {
)
);
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("hog");
command.setVerbs("write");
command.setForGroup(true);
@@ -127,7 +127,7 @@ class RepositoryPermissionsRemoveCommandTest {
when(roleManager.get("READ"))
.thenReturn(new RepositoryRole("READ", List.of("read", "pull"), ""));
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("dent");
command.setVerbs("pull");
@@ -148,7 +148,7 @@ class RepositoryPermissionsRemoveCommandTest {
)
);
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("dent");
command.setVerbs("push", "pull");
@@ -160,7 +160,7 @@ class RepositoryPermissionsRemoveCommandTest {
@Test
void shouldHandleIllegalNamespaceNameParameter() {
command.setRepositoryName("illegal name");
command.setRepositoryNamespaceAndName("illegal name");
command.setName("trillian");
command.setVerbs("write");
@@ -171,7 +171,7 @@ class RepositoryPermissionsRemoveCommandTest {
@Test
void shouldHandleNotExistingRepository() {
command.setRepositoryName("no/repository");
command.setRepositoryNamespaceAndName("no/repository");
command.setName("trillian");
command.setVerbs("write");

View File

@@ -85,7 +85,7 @@ class RepositoryPermissionsSetRoleCommandTest {
@Test
void shouldSetRoleForUser() {
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("trillian");
command.setRole("OWNER");
@@ -100,7 +100,7 @@ class RepositoryPermissionsSetRoleCommandTest {
@Test
void shouldSetRoleForGroup() {
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("crew");
command.setRole("READ");
command.setForGroup(true);
@@ -122,7 +122,7 @@ class RepositoryPermissionsSetRoleCommandTest {
)
);
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("trillian");
command.setRole("OWNER");
@@ -143,7 +143,7 @@ class RepositoryPermissionsSetRoleCommandTest {
)
);
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("trillian");
command.setRole("OWNER");
command.setForGroup(true);
@@ -160,7 +160,7 @@ class RepositoryPermissionsSetRoleCommandTest {
@Test
void shouldHandleMissingRole() {
command.setRepositoryName("hitchhiker/HeartOfGold");
command.setRepositoryNamespaceAndName("hitchhiker/HeartOfGold");
command.setName("trillian");
command.setRole("FUNNY");
@@ -172,7 +172,7 @@ class RepositoryPermissionsSetRoleCommandTest {
@Test
void shouldHandleIllegalNamespaceNameParameter() {
command.setRepositoryName("illegal name");
command.setRepositoryNamespaceAndName("illegal name");
command.setName("trillian");
command.setRole("READ");
@@ -184,7 +184,7 @@ class RepositoryPermissionsSetRoleCommandTest {
@Test
void shouldHandleNotExistingRepository() {
command.setRepositoryName("no/repository");
command.setRepositoryNamespaceAndName("no/repository");
command.setName("trillian");
command.setRole("READ");