Skip to content

Commit

Permalink
proposed fixes of style
Browse files Browse the repository at this point in the history
  • Loading branch information
tledkov committed Feb 10, 2023
1 parent cab7301 commit 1497215
Showing 1 changed file with 20 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;

import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

/**
* <p>Integration tests for the leases (i.e. "sys/leases") Vault API operations.</p>
Expand All @@ -38,6 +39,10 @@ public class LeasesTests {

private Vault vault;

private DatabaseResponse credsResponse;

private String prefix;

@BeforeClass
public static void setupClass() throws IOException, InterruptedException {
container.initAndUnsealVault();
Expand All @@ -47,88 +52,43 @@ public static void setupClass() throws IOException, InterruptedException {
@Before
public void setup() throws VaultException {
vault = container.getRootVault();
}

@Test(expected = VaultException.class)
public void testRevoke() throws VaultException {
List<String> creationStatements = new ArrayList<>();
creationStatements.add(
"CREATE USER \"{{name}}\" WITH PASSWORD '{{password}}'; GRANT ALL PRIVILEGES ON DATABASE \"postgres\" to \"{{name}}\";");

DatabaseResponse databaseResponse = vault.database().createOrUpdateRole("new-role",
new DatabaseRoleOptions().dbName("postgres")
.creationStatements(creationStatements));
TestCase.assertEquals(204, databaseResponse.getRestResponse().getStatus());
assertEquals(204, databaseResponse.getRestResponse().getStatus());

DatabaseResponse credsResponse = vault.database().creds("new-role");
TestCase.assertEquals(200, credsResponse.getRestResponse().getStatus());
credsResponse = vault.database().creds("new-role");
assertEquals(200, credsResponse.getRestResponse().getStatus());

TestCase.assertTrue(credsResponse.getCredential().getUsername().contains("new-role"));
assertTrue(credsResponse.getCredential().getUsername().contains("new-role"));

final VaultResponse response = vault.leases().revoke(credsResponse.getLeaseId());
assertEquals(204, response.getRestResponse().getStatus());
prefix = Arrays.stream(credsResponse.getLeaseId().split("([^/]+)$"))
.map(str -> str.substring(0, str.length() - 1)).findFirst().get();
}

@Test(expected = VaultException.class)
public void testRevokePrefix() throws VaultException {
List<String> creationStatements = new ArrayList<>();
creationStatements.add(
"CREATE USER \"{{name}}\" WITH PASSWORD '{{password}}'; GRANT ALL PRIVILEGES ON DATABASE \"postgres\" to \"{{name}}\";");

DatabaseResponse databaseResponse = vault.database().createOrUpdateRole("new-role",
new DatabaseRoleOptions().dbName("postgres")
.creationStatements(creationStatements));
TestCase.assertEquals(204, databaseResponse.getRestResponse().getStatus());

DatabaseResponse credsResponse = vault.database().creds("new-role");
TestCase.assertEquals(200, credsResponse.getRestResponse().getStatus());

TestCase.assertTrue(credsResponse.getCredential().getUsername().contains("new-role"));
String prefix = Arrays.stream(credsResponse.getLeaseId().split("([^/]+)$"))
.map(str -> str.substring(0, str.length() - 1)).findFirst().get();
@Test
public void testRevoke() {
assertThrows(VaultException.class, () -> vault.leases().revoke(credsResponse.getLeaseId()));
}

final VaultResponse response = vault.leases().revokePrefix(prefix);
assertEquals(204, response.getRestResponse().getStatus());
@Test
public void testRevokePrefix() {
assertThrows(VaultException.class, () -> vault.leases().revokePrefix(prefix));
}

@Test
public void testRevokeForce() throws VaultException {
List<String> creationStatements = new ArrayList<>();
creationStatements.add(
"CREATE USER \"{{name}}\" WITH PASSWORD '{{password}}'; GRANT ALL PRIVILEGES ON DATABASE \"postgres\" to \"{{name}}\";");

DatabaseResponse databaseResponse = vault.database().createOrUpdateRole("new-role",
new DatabaseRoleOptions().dbName("postgres")
.creationStatements(creationStatements));
TestCase.assertEquals(204, databaseResponse.getRestResponse().getStatus());

DatabaseResponse credsResponse = vault.database().creds("new-role");
TestCase.assertEquals(200, credsResponse.getRestResponse().getStatus());

TestCase.assertTrue(credsResponse.getCredential().getUsername().contains("new-role"));
String prefix = Arrays.stream(credsResponse.getLeaseId().split("([^/]+)$"))
.map(str -> str.substring(0, str.length() - 1)).findFirst().get();

final VaultResponse response = vault.leases().revokeForce(prefix);
assertEquals(204, response.getRestResponse().getStatus());
}

@Test
public void testRenew() throws VaultException {
List<String> creationStatements = new ArrayList<>();
creationStatements.add(
"CREATE USER \"{{name}}\" WITH PASSWORD '{{password}}'; GRANT ALL PRIVILEGES ON DATABASE \"postgres\" to \"{{name}}\";");

DatabaseResponse databaseResponse = vault.database().createOrUpdateRole("new-role",
new DatabaseRoleOptions().dbName("postgres")
.creationStatements(creationStatements));
TestCase.assertEquals(204, databaseResponse.getRestResponse().getStatus());

DatabaseResponse credsResponse = vault.database().creds("new-role");
TestCase.assertEquals(200, credsResponse.getRestResponse().getStatus());

TestCase.assertTrue(credsResponse.getCredential().getUsername().contains("new-role"));

final VaultResponse response = vault.leases().renew(credsResponse.getLeaseId(),
credsResponse.getLeaseDuration());
assertEquals(200, response.getRestResponse().getStatus());
Expand Down

0 comments on commit 1497215

Please sign in to comment.