use of models.ApiKey in project civiform by seattle-uat.
the class ApiKeyServiceTest method retireApiKey_keyAlreadyRetired_throws.
@Test
public void retireApiKey_keyAlreadyRetired_throws() {
ApiKey apiKey = apiKeyService.createApiKey(buildForm(ImmutableMap.of("keyName", "test key 1", "expiration", "2020-01-30", "subnet", "0.0.0.1/32")), adminProfile).getApiKey();
apiKeyService.retireApiKey(apiKey.id, adminProfile);
NotChangeableException exception = assertThrows(NotChangeableException.class, () -> apiKeyService.retireApiKey(apiKey.id, adminProfile));
assertThat(exception).hasMessage(String.format("ApiKey %s is already retired", apiKey));
}
use of models.ApiKey in project civiform by seattle-uat.
the class ApiKeyServiceTest method retireApiKey_retiresAnApiKey.
@Test
public void retireApiKey_retiresAnApiKey() {
ApiKey apiKey = apiKeyService.createApiKey(buildForm(ImmutableMap.of("keyName", "test key 1", "expiration", "2020-01-30", "subnet", "0.0.0.1/32")), adminProfile).getApiKey();
apiKeyService.retireApiKey(apiKey.id, adminProfile);
apiKey.refresh();
assertThat(apiKey.isRetired()).isTrue();
assertThat(apiKey.getRetiredBy().get()).isEqualTo(adminProfile.getAuthorityId().join());
assertThat(apiKey.getRetiredTime()).isPresent();
}
use of models.ApiKey in project civiform by seattle-uat.
the class ApiKeyRepositoryTest method insert_persistsANewKey.
@Test
public void insert_persistsANewKey() {
ApiKey foundKey;
ApiKeyGrants grants = new ApiKeyGrants();
grants.grantProgramPermission("program-a", ApiKeyGrants.Permission.READ);
ApiKey apiKey = new ApiKey(grants);
apiKey.setName("key name").setKeyId("key-id").setCreatedBy("test@example.com").setSaltedKeySecret("secret").setSubnet("0.0.0.0/32").setExpiration(Instant.ofEpochSecond(100));
ApiKeyRepository repo = instanceOf(ApiKeyRepository.class);
repo.insert(apiKey).toCompletableFuture().join();
long id = apiKey.id;
foundKey = repo.lookupApiKey(id).toCompletableFuture().join().get();
assertThat(foundKey.id).isEqualTo(id);
foundKey = repo.lookupApiKey("key-id").toCompletableFuture().join().get();
assertThat(foundKey.id).isEqualTo(id);
assertThat(foundKey.getName()).isEqualTo("key name");
assertThat(foundKey.getKeyId()).isEqualTo("key-id");
assertThat(foundKey.getCreatedBy()).isEqualTo("test@example.com");
assertThat(foundKey.getSaltedKeySecret()).isEqualTo("secret");
assertThat(foundKey.getSubnet()).isEqualTo("0.0.0.0/32");
assertThat(foundKey.getExpiration()).isEqualTo(Instant.ofEpochSecond(100));
assertThat(foundKey.getGrants().hasProgramPermission("program-a", ApiKeyGrants.Permission.READ)).isTrue();
}
use of models.ApiKey in project civiform by seattle-uat.
the class ApiKeyServiceTest method createApiKey_createsAnApiKey.
@Test
public void createApiKey_createsAnApiKey() {
resourceCreator.insertActiveProgram("test program");
DynamicForm form = buildForm(ImmutableMap.of("keyName", "test key", "expiration", "2020-01-30", "subnet", "0.0.0.1/32", "grant-program-read[test-program]", "true"));
ApiKeyCreationResult apiKeyCreationResult = apiKeyService.createApiKey(form, adminProfile);
assertThat(apiKeyCreationResult.isSuccessful()).isTrue();
String credentialString = apiKeyCreationResult.getCredentials();
byte[] keyIdBytes = Base64.getDecoder().decode(credentialString);
String keyId = Iterables.get(Splitter.on(':').split(new String(keyIdBytes, StandardCharsets.UTF_8)), 0);
ApiKey apiKey = apiKeyRepository.lookupApiKey(keyId).toCompletableFuture().join().get();
assertThat(apiKey.getName()).isEqualTo("test key");
assertThat(apiKey.getSubnet()).isEqualTo("0.0.0.1/32");
assertThat(apiKey.getExpiration()).isEqualTo(dateConverter.parseIso8601DateToStartOfDateInstant("2020-01-30"));
assertThat(apiKey.getGrants().hasProgramPermission("test-program", Permission.READ)).isTrue();
}
use of models.ApiKey in project civiform by seattle-uat.
the class ApiKeyIndexView method render.
public Content render(Http.Request request, PaginationResult<ApiKey> apiKeyPaginationResult, ImmutableSet<String> allProgramNames) {
String title = "API Keys";
ContainerTag headerDiv = div().withClasses(Styles.FLEX, Styles.PLACE_CONTENT_BETWEEN, Styles.MY_8).with(h1(title).withClasses(Styles.MY_4), new LinkElement().setHref(controllers.admin.routes.AdminApiKeysController.newOne().url()).setId("new-api-key-button").setText("New API Key").asButton());
ContainerTag contentDiv = div().withClasses(Styles.PX_20).with(headerDiv);
for (ApiKey apiKey : apiKeyPaginationResult.getPageContents()) {
contentDiv.with(renderApiKey(request, apiKey, buildProgramSlugToName(allProgramNames)));
}
HtmlBundle htmlBundle = layout.getBundle().setTitle(title).addMainContent(contentDiv);
return layout.renderCentered(htmlBundle);
}
Aggregations