2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2020-12-16 10:58:29 +01:00
|
|
|
|
2019-01-23 12:22:06 +01:00
|
|
|
package sonia.scm.security;
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import sonia.scm.plugin.PluginLoader;
|
|
|
|
|
import sonia.scm.repository.RepositoryPermissions;
|
2019-05-03 14:19:33 +02:00
|
|
|
import sonia.scm.repository.RepositoryRole;
|
2019-01-23 12:22:06 +01:00
|
|
|
import sonia.scm.util.ClassLoaders;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.util.Arrays;
|
2019-02-18 10:31:12 +01:00
|
|
|
import java.util.Collection;
|
2019-01-23 12:22:06 +01:00
|
|
|
|
2020-12-16 10:58:29 +01:00
|
|
|
import static java.util.Arrays.asList;
|
2019-01-23 12:22:06 +01:00
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.fail;
|
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
2019-05-03 14:19:33 +02:00
|
|
|
class SystemRepositoryPermissionProviderTest {
|
2019-01-23 12:22:06 +01:00
|
|
|
|
2019-05-03 14:19:33 +02:00
|
|
|
private SystemRepositoryPermissionProvider repositoryPermissionProvider;
|
2019-01-23 12:22:06 +01:00
|
|
|
private String[] allVerbsFromRepositoryClass;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
|
void init() {
|
|
|
|
|
PluginLoader pluginLoader = mock(PluginLoader.class);
|
|
|
|
|
when(pluginLoader.getUberClassLoader()).thenReturn(ClassLoaders.getContextClassLoader(DefaultSecuritySystem.class));
|
2019-05-03 14:19:33 +02:00
|
|
|
repositoryPermissionProvider = new SystemRepositoryPermissionProvider(pluginLoader);
|
2019-01-23 12:22:06 +01:00
|
|
|
allVerbsFromRepositoryClass = Arrays.stream(RepositoryPermissions.class.getDeclaredFields())
|
|
|
|
|
.filter(field -> field.getName().startsWith("ACTION_"))
|
2019-02-18 10:17:35 +01:00
|
|
|
.filter(field -> !field.getName().equals("ACTION_HEALTHCHECK"))
|
2019-01-23 12:22:06 +01:00
|
|
|
.map(this::getString)
|
2020-12-16 10:58:29 +01:00
|
|
|
.filter(verb -> !asList("create", "archive").contains(verb))
|
2019-01-23 12:22:06 +01:00
|
|
|
.toArray(String[]::new);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldReadAvailableRoles() {
|
|
|
|
|
assertThat(repositoryPermissionProvider.availableRoles()).isNotEmpty();
|
2019-01-23 12:46:08 +01:00
|
|
|
assertThat(repositoryPermissionProvider.availableRoles()).allSatisfy(this::containsOnlyAvailableVerbs);
|
2019-01-23 12:22:06 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-23 12:46:08 +01:00
|
|
|
private void containsOnlyAvailableVerbs(RepositoryRole role) {
|
|
|
|
|
assertThat(role.getVerbs()).isSubsetOf(repositoryPermissionProvider.availableVerbs());
|
2019-01-23 12:22:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldReadAvailableVerbsFromRepository() {
|
|
|
|
|
assertThat(repositoryPermissionProvider.availableVerbs()).contains(allVerbsFromRepositoryClass);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-18 10:31:12 +01:00
|
|
|
@Test
|
|
|
|
|
void shouldMergeRepositoryRoles() {
|
|
|
|
|
Collection<String> verbsInMergedRole = repositoryPermissionProvider
|
|
|
|
|
.availableRoles()
|
|
|
|
|
.stream()
|
|
|
|
|
.filter(r -> "READ".equals(r.getName()))
|
|
|
|
|
.findFirst()
|
|
|
|
|
.get()
|
|
|
|
|
.getVerbs();
|
|
|
|
|
assertThat(verbsInMergedRole).contains("read", "pull", "test");
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-23 12:22:06 +01:00
|
|
|
private String getString(Field field) {
|
|
|
|
|
try {
|
|
|
|
|
return (String) field.get(null);
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
fail(e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|