mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-02-22 06:26:56 +01:00
Create rest endpoint to create new api keys
This commit is contained in:
@@ -119,7 +119,7 @@ public class MeResourceTest {
|
||||
when(userManager.isTypeDefault(userCaptor.capture())).thenCallRealMethod();
|
||||
when(userManager.getDefaultType()).thenReturn("xml");
|
||||
ApiKeyCollectionToDtoMapper apiKeyCollectionMapper = new ApiKeyCollectionToDtoMapper(apiKeyMapper, resourceLinks);
|
||||
ApiKeyResource apiKeyResource = new ApiKeyResource(apiKeyService, apiKeyCollectionMapper, apiKeyMapper);
|
||||
ApiKeyResource apiKeyResource = new ApiKeyResource(apiKeyService, apiKeyCollectionMapper, apiKeyMapper, resourceLinks);
|
||||
MeResource meResource = new MeResource(meDtoFactory, userManager, passwordService, of(apiKeyResource));
|
||||
when(uriInfo.getApiRestUri()).thenReturn(URI.create("/"));
|
||||
when(scmPathInfoStore.get()).thenReturn(uriInfo);
|
||||
@@ -238,6 +238,7 @@ public class MeResourceTest {
|
||||
assertThat(response.getContentAsString()).contains("\"displayName\":\"key 1\",\"role\":\"READ\"");
|
||||
assertThat(response.getContentAsString()).contains("\"displayName\":\"key 2\",\"role\":\"WRITE\"");
|
||||
assertThat(response.getContentAsString()).contains("\"self\":{\"href\":\"/v2/me/apiKeys\"}");
|
||||
assertThat(response.getContentAsString()).contains("\"create\":{\"href\":\"/v2/me/apiKeys\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -254,6 +255,22 @@ public class MeResourceTest {
|
||||
assertThat(response.getContentAsString()).contains("\"self\":{\"href\":\"/v2/me/apiKeys/1\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateNewApiKey() throws URISyntaxException, UnsupportedEncodingException {
|
||||
when(apiKeyService.createNewKey("guide", "READ")).thenReturn(new ApiKeyService.CreationResult("abc", "1"));
|
||||
|
||||
final MockHttpRequest request = MockHttpRequest
|
||||
.post("/" + MeResource.ME_PATH_V2 + "apiKeys/")
|
||||
.contentType(VndMediaType.API_KEY)
|
||||
.content("{\"displayName\":\"guide\",\"role\":\"READ\"}".getBytes());
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(201);
|
||||
assertThat(response.getContentAsString()).isEqualTo("abc");
|
||||
assertThat(response.getOutputHeaders().get("Location")).containsExactly(URI.create("/v2/me/apiKeys/1"));
|
||||
}
|
||||
|
||||
private User createDummyUser(String name) {
|
||||
User user = new User();
|
||||
user.setName(name);
|
||||
|
||||
@@ -101,7 +101,7 @@ class ApiKeyServiceTest {
|
||||
|
||||
@Test
|
||||
void shouldReturnRoleForKey() {
|
||||
String newKey = service.createNewKey("1", "READ");
|
||||
String newKey = service.createNewKey("1", "READ").getToken();
|
||||
|
||||
Optional<String> role = service.check(newKey);
|
||||
|
||||
@@ -119,23 +119,24 @@ class ApiKeyServiceTest {
|
||||
|
||||
@Test
|
||||
void shouldAddSecondKey() {
|
||||
String firstKey = service.createNewKey("1", "READ");
|
||||
String secondKey = service.createNewKey("2", "WRITE");
|
||||
ApiKeyService.CreationResult firstKey = service.createNewKey("1", "READ");
|
||||
ApiKeyService.CreationResult secondKey = service.createNewKey("2", "WRITE");
|
||||
|
||||
ApiKeyCollection apiKeys = store.get("dent");
|
||||
|
||||
assertThat(apiKeys.getKeys()).hasSize(2);
|
||||
|
||||
assertThat(service.check(firstKey)).contains("READ");
|
||||
assertThat(service.check(secondKey)).contains("WRITE");
|
||||
assertThat(service.check(firstKey.getToken())).contains("READ");
|
||||
assertThat(service.check(secondKey.getToken())).contains("WRITE");
|
||||
|
||||
assertThat(service.getKeys()).extracting("id").contains("1", "2");
|
||||
assertThat(service.getKeys()).extracting("id")
|
||||
.contains(firstKey.getId(), secondKey.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRemoveKey() {
|
||||
String firstKey = service.createNewKey("1", "READ");
|
||||
String secondKey = service.createNewKey("2", "WRITE");
|
||||
String firstKey = service.createNewKey("1", "READ").getToken();
|
||||
String secondKey = service.createNewKey("2", "WRITE").getToken();
|
||||
|
||||
service.remove("1");
|
||||
|
||||
@@ -145,7 +146,7 @@ class ApiKeyServiceTest {
|
||||
|
||||
@Test
|
||||
void shouldFailWhenAddingSameNameTwice() {
|
||||
String firstKey = service.createNewKey("1", "READ");
|
||||
String firstKey = service.createNewKey("1", "READ").getToken();
|
||||
|
||||
assertThrows(AlreadyExistsException.class, () -> service.createNewKey("1", "WRITE"));
|
||||
|
||||
@@ -154,7 +155,7 @@ class ApiKeyServiceTest {
|
||||
|
||||
@Test
|
||||
void shouldIgnoreCorrectPassphraseWithWrongName() {
|
||||
String firstKey = service.createNewKey("1", "READ");
|
||||
String firstKey = service.createNewKey("1", "READ").getToken();
|
||||
|
||||
assertThat(service.check("dent", "other", firstKey)).isEmpty();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user