mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-01-30 11:19:08 +01:00
first functional snapshot with unit test, no frontend changes
This commit is contained in:
@@ -81,7 +81,7 @@ class PublicKeyMapperTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotAppendDeleteLink() throws IOException {
|
||||
void shouldNotAppendDeleteLinkIfPermissionMissing() throws IOException {
|
||||
String raw = GPGTestHelper.readResourceAsString("single.asc");
|
||||
RawGpgKey key = new RawGpgKey("1", "key_42", "trillian", raw, Collections.emptySet(), Instant.now());
|
||||
|
||||
@@ -89,4 +89,16 @@ class PublicKeyMapperTest {
|
||||
|
||||
assertThat(dto.getLinks().getLinkBy("delete")).isNotPresent();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotAppendDeleteLinkIfReadonly() throws IOException {
|
||||
when(subject.isPermitted("user:changePublicKeys:trillian")).thenReturn(true);
|
||||
|
||||
String raw = GPGTestHelper.readResourceAsString("single.asc");
|
||||
RawGpgKey key = new RawGpgKey("1", "key_42", "trillian", raw, Collections.emptySet(), Instant.now(), true);
|
||||
|
||||
RawGpgKeyDto dto = mapper.map(key);
|
||||
|
||||
assertThat(dto.getLinks().getLinkBy("delete")).isNotPresent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,11 +24,6 @@
|
||||
|
||||
package sonia.scm.security.gpg;
|
||||
|
||||
import de.otto.edison.hal.HalRepresentation;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
@@ -36,18 +31,10 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriBuilder;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@@ -56,87 +43,19 @@ class PublicKeyResourceTest {
|
||||
@Mock
|
||||
private PublicKeyStore store;
|
||||
|
||||
@Mock
|
||||
private PublicKeyCollectionMapper collectionMapper;
|
||||
|
||||
@Mock
|
||||
private PublicKeyMapper mapper;
|
||||
|
||||
@InjectMocks
|
||||
private PublicKeyResource resource;
|
||||
|
||||
@Mock
|
||||
private Subject subject;
|
||||
|
||||
@BeforeEach
|
||||
void setUpSubject() {
|
||||
ThreadContext.bind(subject);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void clearSubject() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindAll() {
|
||||
List<RawGpgKey> keys = new ArrayList<>();
|
||||
when(store.findByUsername("trillian")).thenReturn(keys);
|
||||
|
||||
HalRepresentation collection = new HalRepresentation();
|
||||
when(collectionMapper.map("trillian", keys)).thenReturn(collection);
|
||||
|
||||
HalRepresentation result = resource.findAll("trillian");
|
||||
assertThat(result).isSameAs(collection);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindById() {
|
||||
RawGpgKey key = new RawGpgKey("42");
|
||||
when(store.findById("42")).thenReturn(Optional.of(key));
|
||||
RawGpgKeyDto dto = new RawGpgKeyDto();
|
||||
when(mapper.map(key)).thenReturn(dto);
|
||||
|
||||
Response response = resource.findByIdJson("42");
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
assertThat(response.getEntity()).isSameAs(dto);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturn404IfIdDoesNotExists() {
|
||||
when(store.findById("42")).thenReturn(Optional.empty());
|
||||
|
||||
Response response = resource.findByIdJson("42");
|
||||
assertThat(response.getStatus()).isEqualTo(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAddToStore() throws URISyntaxException, IOException {
|
||||
void shouldFindByIdGpg() throws IOException {
|
||||
String raw = GPGTestHelper.readResourceAsString("single.asc");
|
||||
RawGpgKey key = new RawGpgKey("42", raw);
|
||||
when(store.findById("42")).thenReturn(Optional.of(key));
|
||||
|
||||
UriInfo uriInfo = mock(UriInfo.class);
|
||||
UriBuilder builder = mock(UriBuilder.class);
|
||||
when(uriInfo.getAbsolutePathBuilder()).thenReturn(builder);
|
||||
when(builder.path("42")).thenReturn(builder);
|
||||
when(builder.build()).thenReturn(new URI("/v2/public_keys/42"));
|
||||
|
||||
RawGpgKey key = new RawGpgKey("42");
|
||||
RawGpgKeyDto dto = new RawGpgKeyDto();
|
||||
dto.setDisplayName("key_42");
|
||||
dto.setRaw(raw);
|
||||
when(store.add(dto.getDisplayName(), "trillian", dto.getRaw())).thenReturn(key);
|
||||
|
||||
Response response = resource.create(uriInfo, "trillian", dto);
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(201);
|
||||
assertThat(response.getLocation().toASCIIString()).isEqualTo("/v2/public_keys/42");
|
||||
Response response = resource.findByIdGpg("42");
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
assertThat(response.getEntity()).isSameAs(raw);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDeleteFromStore() {
|
||||
Response response = resource.deleteById("42");
|
||||
assertThat(response.getStatus()).isEqualTo(204);
|
||||
verify(store).delete("42");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,10 +27,12 @@ package sonia.scm.security.gpg;
|
||||
import org.apache.shiro.authz.AuthorizationException;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.junit.Rule;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
@@ -50,6 +52,7 @@ 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.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@@ -105,6 +108,24 @@ class PublicKeyStoreTest {
|
||||
assertThat(key.getOwner()).isEqualTo("trillian");
|
||||
assertThat(key.getCreated()).isAfterOrEqualTo(now);
|
||||
assertThat(key.getRaw()).isEqualTo(rawKey);
|
||||
assertThat(key.isReadonly()).isFalse();
|
||||
assertThat(key.getContacts()).contains(Person.toPerson("SCM Packages (signing key for packages.scm-manager.org) <scm-team@cloudogu.com>"));
|
||||
|
||||
verify(eventBus).post(any(PublicKeyCreatedEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnReadonlyStoredKey() throws IOException {
|
||||
String rawKey = GPGTestHelper.readResourceAsString("single.asc");
|
||||
Instant now = Instant.now();
|
||||
|
||||
RawGpgKey key = keyStore.add("SCM Package Key", "trillian", rawKey, true);
|
||||
assertThat(key.getId()).isEqualTo("0x975922F193B07D6E");
|
||||
assertThat(key.getDisplayName()).isEqualTo("SCM Package Key");
|
||||
assertThat(key.getOwner()).isEqualTo("trillian");
|
||||
assertThat(key.getCreated()).isAfterOrEqualTo(now);
|
||||
assertThat(key.getRaw()).isEqualTo(rawKey);
|
||||
assertThat(key.isReadonly()).isTrue();
|
||||
assertThat(key.getContacts()).contains(Person.toPerson("SCM Packages (signing key for packages.scm-manager.org) <scm-team@cloudogu.com>"));
|
||||
|
||||
verify(eventBus).post(any(PublicKeyCreatedEvent.class));
|
||||
@@ -134,6 +155,22 @@ class PublicKeyStoreTest {
|
||||
verify(eventBus).post(any(PublicKeyDeletedEvent.class));
|
||||
}
|
||||
|
||||
@Test()
|
||||
void shouldThrowOnDeletingReadonlyKey() throws IOException {
|
||||
String rawKey = GPGTestHelper.readResourceAsString("single.asc");
|
||||
keyStore.add("SCM Package Key", "trillian", rawKey, true);
|
||||
Optional<RawGpgKey> key = keyStore.findById("0x975922F193B07D6E");
|
||||
|
||||
assertThat(key).isPresent();
|
||||
|
||||
assertThrows(PublicKeyStore.DeletingReadonlyKeyNotAllowedException.class, () -> keyStore.delete("0x975922F193B07D6E"));
|
||||
key = keyStore.findById("0x975922F193B07D6E");
|
||||
|
||||
assertThat(key).isPresent();
|
||||
|
||||
verify(eventBus, never()).post(any(PublicKeyDeletedEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnEmptyListIfNoKeysAvailable() {
|
||||
List<RawGpgKey> keys = keyStore.findByUsername("zaphod");
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* 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.security.gpg;
|
||||
|
||||
import de.otto.edison.hal.HalRepresentation;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriBuilder;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
|
||||
class UserPublicKeyResourceTest {
|
||||
|
||||
@Mock
|
||||
private PublicKeyStore store;
|
||||
|
||||
@Mock
|
||||
private PublicKeyCollectionMapper collectionMapper;
|
||||
|
||||
@Mock
|
||||
private PublicKeyMapper mapper;
|
||||
|
||||
@InjectMocks
|
||||
private UserPublicKeyResource resource;
|
||||
|
||||
@Mock
|
||||
private Subject subject;
|
||||
|
||||
@BeforeEach
|
||||
void setUpSubject() {
|
||||
ThreadContext.bind(subject);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void clearSubject() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindAll() {
|
||||
List<RawGpgKey> keys = new ArrayList<>();
|
||||
when(store.findByUsername("trillian")).thenReturn(keys);
|
||||
|
||||
HalRepresentation collection = new HalRepresentation();
|
||||
when(collectionMapper.map("trillian", keys)).thenReturn(collection);
|
||||
|
||||
HalRepresentation result = resource.findAll("trillian");
|
||||
assertThat(result).isSameAs(collection);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindByIdJson() {
|
||||
RawGpgKey key = new RawGpgKey("42");
|
||||
when(store.findById("42")).thenReturn(Optional.of(key));
|
||||
RawGpgKeyDto dto = new RawGpgKeyDto();
|
||||
when(mapper.map(key)).thenReturn(dto);
|
||||
|
||||
Response response = resource.findByIdJson("42");
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
assertThat(response.getEntity()).isSameAs(dto);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturn404IfIdDoesNotExists() {
|
||||
when(store.findById("42")).thenReturn(Optional.empty());
|
||||
|
||||
Response response = resource.findByIdJson("42");
|
||||
assertThat(response.getStatus()).isEqualTo(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAddToStore() throws URISyntaxException, IOException {
|
||||
String raw = GPGTestHelper.readResourceAsString("single.asc");
|
||||
|
||||
UriInfo uriInfo = mock(UriInfo.class);
|
||||
UriBuilder builder = mock(UriBuilder.class);
|
||||
when(uriInfo.getAbsolutePathBuilder()).thenReturn(builder);
|
||||
when(builder.path("42")).thenReturn(builder);
|
||||
when(builder.build()).thenReturn(new URI("/v2/public_keys/42"));
|
||||
|
||||
RawGpgKey key = new RawGpgKey("42");
|
||||
RawGpgKeyDto dto = new RawGpgKeyDto();
|
||||
dto.setDisplayName("key_42");
|
||||
dto.setRaw(raw);
|
||||
when(store.add(dto.getDisplayName(), "trillian", dto.getRaw())).thenReturn(key);
|
||||
|
||||
Response response = resource.create(uriInfo, "trillian", dto);
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(201);
|
||||
assertThat(response.getLocation().toASCIIString()).isEqualTo("/v2/public_keys/42");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDeleteFromStore() {
|
||||
Response response = resource.deleteById("42");
|
||||
assertThat(response.getStatus()).isEqualTo(204);
|
||||
verify(store).delete("42");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user