Files
SCM-Manager/scm-webapp/src/test/java/sonia/scm/security/ApiKeyServiceTest.java

163 lines
5.6 KiB
Java
Raw Normal View History

2020-09-28 18:11:07 +02: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.
*/
package sonia.scm.security;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.subject.PrincipalCollection;
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.Nested;
import org.junit.jupiter.api.Test;
import sonia.scm.AlreadyExistsException;
import sonia.scm.store.ConfigurationEntryStore;
import sonia.scm.store.ConfigurationEntryStoreFactory;
import sonia.scm.store.InMemoryConfigurationEntryStoreFactory;
import java.util.Optional;
import java.util.function.Supplier;
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.mock;
import static org.mockito.Mockito.when;
class ApiKeyServiceTest {
int nextKey = 1;
2020-09-29 11:08:36 +02:00
int nextId = 1;
2020-09-28 18:11:07 +02:00
PasswordService passwordService = mock(PasswordService.class);
2020-09-29 11:08:36 +02:00
Supplier<String> passphraseGenerator = () -> Integer.toString(nextKey++);
KeyGenerator keyGenerator = () -> Integer.toString(nextId++);
2020-09-29 13:54:21 +02:00
ApiKeyTokenHandler tokenHandler = new ApiKeyTokenHandler();
2020-09-28 18:11:07 +02:00
ConfigurationEntryStoreFactory storeFactory = new InMemoryConfigurationEntryStoreFactory();
ConfigurationEntryStore<ApiKeyCollection> store = storeFactory.withType(ApiKeyCollection.class).withName("apiKeys").build();
2020-09-29 13:54:21 +02:00
ApiKeyService service = new ApiKeyService(storeFactory, passwordService, keyGenerator, tokenHandler, passphraseGenerator);
2020-09-28 18:11:07 +02:00
@BeforeEach
void mockPasswordService() {
when(passwordService.encryptPassword(any()))
.thenAnswer(invocationOnMock -> invocationOnMock.getArgument(0) + "-hashed");
when(passwordService.passwordsMatch(any(), any()))
.thenAnswer(invocationOnMock -> invocationOnMock.getArgument(1, String.class).startsWith(invocationOnMock.getArgument(0)));
}
@Nested
class WithLoggedInUser {
@BeforeEach
void mockUser() {
final Subject subject = mock(Subject.class);
ThreadContext.bind(subject);
final PrincipalCollection principalCollection = mock(PrincipalCollection.class);
when(subject.getPrincipals()).thenReturn(principalCollection);
when(principalCollection.getPrimaryPrincipal()).thenReturn("dent");
}
@AfterEach
void unbindSubject() {
ThreadContext.unbindSubject();
}
@Test
void shouldCreateNewKeyAndStoreItHashed() {
2020-09-29 13:54:21 +02:00
service.createNewKey("1", "READ");
2020-09-28 18:11:07 +02:00
ApiKeyCollection apiKeys = store.get("dent");
assertThat(apiKeys.getKeys()).hasSize(1);
ApiKeyWithPassphrase key = apiKeys.getKeys().iterator().next();
assertThat(key.getRole()).isEqualTo("READ");
assertThat(key.getPassphrase()).isEqualTo("1-hashed");
Optional<String> role = service.check("dent", "1", "1-hashed");
assertThat(role).contains("READ");
}
@Test
void shouldReturnRoleForKey() {
String newKey = service.createNewKey("1", "READ");
2020-09-29 13:54:21 +02:00
Optional<String> role = service.check(newKey);
2020-09-28 18:11:07 +02:00
assertThat(role).contains("READ");
}
@Test
void shouldNotReturnAnythingWithWrongKey() {
service.createNewKey("1", "READ");
Optional<String> role = service.check("dent", "1", "wrong");
assertThat(role).isEmpty();
}
@Test
void shouldAddSecondKey() {
String firstKey = service.createNewKey("1", "READ");
String secondKey = service.createNewKey("2", "WRITE");
ApiKeyCollection apiKeys = store.get("dent");
assertThat(apiKeys.getKeys()).hasSize(2);
2020-09-29 13:54:21 +02:00
assertThat(service.check(firstKey)).contains("READ");
assertThat(service.check(secondKey)).contains("WRITE");
2020-09-28 18:11:07 +02:00
2020-09-29 11:08:36 +02:00
assertThat(service.getKeys()).extracting("id").contains("1", "2");
2020-09-28 18:11:07 +02:00
}
@Test
void shouldRemoveKey() {
String firstKey = service.createNewKey("1", "READ");
String secondKey = service.createNewKey("2", "WRITE");
service.remove("1");
2020-09-29 13:54:21 +02:00
assertThat(service.check(firstKey)).isEmpty();
assertThat(service.check(secondKey)).contains("WRITE");
2020-09-28 18:11:07 +02:00
}
@Test
void shouldFailWhenAddingSameNameTwice() {
String firstKey = service.createNewKey("1", "READ");
assertThrows(AlreadyExistsException.class, () -> service.createNewKey("1", "WRITE"));
2020-09-29 13:54:21 +02:00
assertThat(service.check(firstKey)).contains("READ");
2020-09-28 18:11:07 +02:00
}
@Test
void shouldIgnoreCorrectPassphraseWithWrongName() {
String firstKey = service.createNewKey("1", "READ");
assertThat(service.check("dent", "other", firstKey)).isEmpty();
}
}
}