feat(account): implement GetAccountInformation and PutAccountName (#8725)

This commit is contained in:
Yang Hao authored and GitHub committed 2026-11-17 06:58:24 -04:00
1 parent 3e7ce122e7
commit 93c1d39348
21 files changed
+698 -50

No files matched your search

@@ -10,9 +10,9 @@ import org.junit.jupiter.api.Test;
import java.util.concurrent.atomic.AtomicInteger;
import static io.floci.aws.services.account.AccountRequests.account;
import static io.floci.aws.services.account.ContactInformationRequests.contactInformationRequest;
import static io.floci.aws.services.account.ContactInformationRequests.contactOf;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
@@ -95,11 +95,11 @@ class AccountContactInformationIntegrationTest {
@Test
void nonObjectRequestBodyReturnsSerializationException() {
String account = newAccount();
put(account, contactInformationRequest()).statusCode(200);
String caller = newAccount();
put(caller, contactInformationRequest()).statusCode(200);
get(account, "[]").statusCode(400).body("__type", equalTo("SerializationException"));
post(account, "/putContactInformation", "[]")
get(caller, "[]").statusCode(400).body("__type", equalTo("SerializationException"));
account(caller, "/putContactInformation", "[]")
.statusCode(400).body("__type", equalTo("SerializationException"));
}
@@ -154,20 +154,11 @@ class AccountContactInformationIntegrationTest {
return String.format("7310008259%02d", ACCOUNTS.incrementAndGet());
}
private static ValidatableResponse put(String account, ObjectNode request) {
return post(account, "/putContactInformation", request.toString());
private static ValidatableResponse put(String caller, ObjectNode request) {
return account(caller, "/putContactInformation", request.toString());
}
private static ValidatableResponse get(String account, String body) {
return post(account, "/getContactInformation", body);
}
private static ValidatableResponse post(String account, String path, String body) {
return given().contentType("application/json").header("Authorization", authorization(account))
.body(body).post(path).then();
}
private static String authorization(String account) {
return "AWS4-HMAC-SHA256 Credential=" + account + "/20261114/us-east-1/account/aws4_request";
private static ValidatableResponse get(String caller, String body) {
return account(caller, "/getContactInformation", body);
}
}
@@ -0,0 +1,85 @@
package io.floci.aws.services.account;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.ValidatableResponse;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.concurrent.atomic.AtomicInteger;
import static io.floci.aws.services.account.AccountNameRequests.accountNameRequest;
import static io.floci.aws.services.account.AccountRequests.account;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
/** Accounts in no organization; {@code AccountOrganizationsContextIntegrationTest} covers the others. */
@QuarkusTest
class AccountInformationIntegrationTest {
private static final AtomicInteger ACCOUNTS = new AtomicInteger();
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void accountIsActiveAndUnnamedUntilPutAccountName() {
String account = newAccount();
get(account, "{}").statusCode(200)
.body("AccountId", equalTo(account))
.body("AccountState", equalTo("ACTIVE"))
.body("$", not(hasKey("AccountName")))
.body("$", not(hasKey("AccountCreatedDate")));
put(account, accountNameRequest("Dev Sandbox").toString()).statusCode(200).body(equalTo(""));
get(account, "{}").statusCode(200)
.body("AccountId", equalTo(account))
.body("AccountName", equalTo("Dev Sandbox"))
.body("AccountState", equalTo("ACTIVE"));
get(newAccount(), "{}").statusCode(200).body("$", not(hasKey("AccountName")));
}
@Test
void putAccountNameRejectsANameOutsideTheModel() {
String account = newAccount();
put(account, accountNameRequest("Dev Sandbox").toString()).statusCode(200);
put(account, accountNameRequest("<Dev Sandbox>").toString()).statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", startsWith("Invalid parameter at 'AccountName'"));
put(account, "{}").statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", startsWith("Invalid parameter at 'AccountName'"));
put(account, "{\"AccountName\":42}").statusCode(400).body("__type", equalTo("SerializationException"));
get(account, "{}").statusCode(200).body("AccountName", equalTo("Dev Sandbox"));
}
@Test
void anotherAccountIsRefusedOutsideAnOrganization() {
String account = newAccount();
put(account, accountNameRequest("111122223333", "Member").toString())
.statusCode(403).body("__type", equalTo("AccessDeniedException"));
get(account, "{\"AccountId\":\"111122223333\"}")
.statusCode(403).body("__type", equalTo("AccessDeniedException"));
}
/** Names cannot be deleted, so every test acts on an account of its own. */
private static String newAccount() {
return String.format("7310009000%02d", ACCOUNTS.incrementAndGet());
}
private static ValidatableResponse put(String caller, String body) {
return account(caller, "/putAccountName", body);
}
private static ValidatableResponse get(String caller, String body) {
return account(caller, "/getAccountInformation", body);
}
}
@@ -0,0 +1,23 @@
package io.floci.aws.services.account;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
/** The {@code PutAccountName} request the account information tests send. */
final class AccountNameRequests {
private static final ObjectMapper MAPPER = new ObjectMapper();
private AccountNameRequests() {
}
static ObjectNode accountNameRequest(String name) {
ObjectNode request = MAPPER.createObjectNode();
request.put("AccountName", name);
return request;
}
static ObjectNode accountNameRequest(String accountId, String name) {
return accountNameRequest(name).put("AccountId", accountId);
}
}
@@ -1,6 +1,7 @@
package io.floci.aws.services.account;
import io.floci.aws.testing.EnforcementFixtures;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.testing.OrganizationsRequests;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
@@ -9,17 +10,23 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import static io.floci.aws.services.account.AccountNameRequests.accountNameRequest;
import static io.floci.aws.services.account.AccountRequests.account;
import static io.floci.aws.services.account.AlternateContactRequests.alternateContactRequest;
import static io.floci.aws.services.account.ContactInformationRequests.contactInformationRequest;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
/** Requests that name an {@code AccountId}, sent by the accounts of one organization. */
@QuarkusTest
class AccountOrganizationsContextIntegrationTest {
/** The management account of an organization of this class's own, which no other test uses. */
private static final String MANAGEMENT = "731000826800";
private static final ObjectMapper MAPPER = new ObjectMapper();
private static String delegatedAdministrator;
private static String member;
@@ -62,6 +69,9 @@ class AccountOrganizationsContextIntegrationTest {
refused(account(delegatedAdministrator, "/putContactInformation",
contactInformationRequest().put("AccountId", MANAGEMENT).toString()));
refused(account(delegatedAdministrator, "/getContactInformation", "{\"AccountId\":\"" + MANAGEMENT + "\"}"));
refused(account(delegatedAdministrator, "/putAccountName",
accountNameRequest(MANAGEMENT, "Management").toString()));
refused(account(delegatedAdministrator, "/getAccountInformation", "{\"AccountId\":\"" + MANAGEMENT + "\"}"));
}
@Test
@@ -69,6 +79,99 @@ class AccountOrganizationsContextIntegrationTest {
refused(account(MANAGEMENT, "/putAlternateContact", alternateContact(MANAGEMENT)));
}
@Test
void memberAccountIsDescribedByItsOrganizationRecord() throws Exception {
JsonNode described = describeAccount(member);
account(delegatedAdministrator, "/getAccountInformation", "{\"AccountId\":\"" + member + "\"}")
.statusCode(200)
.body("AccountId", equalTo(member))
.body("AccountName", equalTo("account-member"))
.body("AccountCreatedDate",
equalTo(Instant.ofEpochSecond(described.path("JoinedTimestamp").longValue()).toString()))
.body("AccountState", equalTo("ACTIVE"));
}
@Test
void managementAccountHasNoCreationDate() throws Exception {
account(MANAGEMENT, "/getAccountInformation", "{}").statusCode(200)
.body("AccountId", equalTo(MANAGEMENT))
.body("AccountName", equalTo(describeAccount(MANAGEMENT).path("Name").textValue()))
.body("AccountState", equalTo("ACTIVE"))
.body("$", not(hasKey("AccountCreatedDate")));
}
@Test
void renamingAMemberAccountShowsInDescribeAccount() {
String renamed = createAccount("account-to-rename");
account(MANAGEMENT, "/putAccountName", accountNameRequest(renamed, "Renamed Member").toString())
.statusCode(200).body(equalTo(""));
organizations("DescribeAccount", "{\"AccountId\":\"" + renamed + "\"}")
.body("Account.Name", equalTo("Renamed Member"));
organizations("ListAccounts", "{}")
.body("Accounts.find { it.Id == '" + renamed + "' }.Name", equalTo("Renamed Member"));
account(renamed, "/getAccountInformation", "{}").statusCode(200)
.body("AccountName", equalTo("Renamed Member"));
}
@Test
void nameOutlivesTheOrganization() {
String leaving = createAccount("account-to-remove");
account(delegatedAdministrator, "/putAccountName", accountNameRequest(leaving, "Former Member").toString())
.statusCode(200);
organizations("RemoveAccountFromOrganization", "{\"AccountId\":\"" + leaving + "\"}");
account(leaving, "/getAccountInformation", "{}").statusCode(200)
.body("AccountName", equalTo("Former Member"))
.body("AccountState", equalTo("ACTIVE"))
.body("$", not(hasKey("AccountCreatedDate")));
}
@Test
void removedAccountKeepsTheNameItWasCreatedWith() {
String removed = createAccount("account-removed-unnamed");
organizations("RemoveAccountFromOrganization", "{\"AccountId\":\"" + removed + "\"}");
account(removed, "/getAccountInformation", "{}").statusCode(200)
.body("AccountName", equalTo("account-removed-unnamed"));
}
@Test
void accountThatLeavesKeepsItsName() {
String leaving = createAccount("account-that-leaves");
OrganizationsRequests.request(leaving, "LeaveOrganization", "{}").when().post("/").then().statusCode(200);
account(leaving, "/getAccountInformation", "{}").statusCode(200)
.body("AccountName", equalTo("account-that-leaves"));
}
@Test
void managementAccountRenamesItselfWithoutAnAccountId() {
account(MANAGEMENT, "/putAccountName", accountNameRequest("Management Account").toString())
.statusCode(200);
organizations("DescribeAccount", "{\"AccountId\":\"" + MANAGEMENT + "\"}")
.body("Account.Name", equalTo("Management Account"));
}
@Test
void accountPendingClosureIsStillActive() {
String closing = createAccount("account-to-close");
organizations("CloseAccount", "{\"AccountId\":\"" + closing + "\"}");
account(closing, "/getAccountInformation", "{}").statusCode(200).body("AccountState", equalTo("ACTIVE"));
}
private static JsonNode describeAccount(String accountId) throws Exception {
String body = organizations("DescribeAccount", "{\"AccountId\":\"" + accountId + "\"}").extract().asString();
return MAPPER.readTree(body).path("Account");
}
private static void refused(ValidatableResponse response) {
response.statusCode(403).body("__type", equalTo("AccessDeniedException"))
.body("message", containsString("management account of the organization"));
@@ -88,10 +191,4 @@ class AccountOrganizationsContextIntegrationTest {
private static ValidatableResponse organizations(String action, String body) {
return OrganizationsRequests.request(MANAGEMENT, action, body).when().post("/").then().statusCode(200);
}
private static ValidatableResponse account(String caller, String path, String body) {
return given().contentType("application/json")
.header("Authorization", EnforcementFixtures.auth(caller, "account"))
.body(body).post(path).then();
}
}
@@ -0,0 +1,19 @@
package io.floci.aws.services.account;
import io.floci.aws.testing.EnforcementFixtures;
import io.restassured.response.ValidatableResponse;
import static io.restassured.RestAssured.given;
/** An Account Management request sent by a given caller account, for the integration tests. */
final class AccountRequests {
private AccountRequests() {
}
static ValidatableResponse account(String caller, String path, String body) {
return given().contentType("application/json")
.header("Authorization", EnforcementFixtures.auth(caller, "account"))
.body(body).post(path).then();
}
}
@@ -7,6 +7,7 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.ServicePrincipals;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.account.model.AccountInformation;
import io.floci.aws.services.account.model.AlternateContact;
import io.floci.aws.services.account.model.ContactInformation;
import io.floci.aws.services.organizations.OrganizationsService;
@@ -25,6 +26,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import static io.floci.aws.services.account.AccountNameRequests.accountNameRequest;
import static io.floci.aws.services.account.AlternateContactRequests.alternateContactRequest;
import static io.floci.aws.services.account.ContactInformationRequests.contactInformationRequest;
import static io.floci.aws.services.account.ContactInformationRequests.contactOf;
@@ -35,6 +37,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class AccountServiceTest {
@@ -54,6 +57,9 @@ class AccountServiceTest {
AccountAwareStorageBackend<ContactInformation> primaryStore = AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
when(storageFactory.create(eq("account"), eq("account-contact-information.json"), any(TypeReference.class)))
.thenReturn((AccountAwareStorageBackend) primaryStore);
AccountAwareStorageBackend<String> nameStore = AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
when(storageFactory.create(eq("account"), eq("account-names.json"), any(TypeReference.class)))
.thenReturn((AccountAwareStorageBackend) nameStore);
organizations = mock(OrganizationsService.class);
service = new AccountService(storageFactory, organizations);
}
@@ -105,7 +111,7 @@ class AccountServiceTest {
void putAlternateContactRejectsAMissingRequiredMember(String member) {
ObjectNode request = alternateContactRequest();
request.remove(member);
assertAlternateContactRejected(() -> service.putAlternateContact(ACCOUNT_ID, request), member);
assertInvalidParameter(() -> service.putAlternateContact(ACCOUNT_ID, request), member);
}
@ParameterizedTest
@@ -135,7 +141,7 @@ class AccountServiceTest {
service.putAlternateContact(ACCOUNT_ID, request);
request.put(member, "1".repeat(maximum + 1));
assertAlternateContactRejected(() -> service.putAlternateContact(ACCOUNT_ID, request), member);
assertInvalidParameter(() -> service.putAlternateContact(ACCOUNT_ID, request), member);
}
@Test
@@ -145,7 +151,7 @@ class AccountServiceTest {
service.putAlternateContact(ACCOUNT_ID, request);
request.put("EmailAddress", "a".repeat(243) + "@example.com");
assertAlternateContactRejected(() -> service.putAlternateContact(ACCOUNT_ID, request), "EmailAddress");
assertInvalidParameter(() -> service.putAlternateContact(ACCOUNT_ID, request), "EmailAddress");
}
@ParameterizedTest
@@ -154,7 +160,7 @@ class AccountServiceTest {
void putAlternateContactRejectsAMemberOutsideItsConstraints(String member, String value) {
ObjectNode request = alternateContactRequest();
request.put(member, value);
assertAlternateContactRejected(() -> service.putAlternateContact(ACCOUNT_ID, request), member);
assertInvalidParameter(() -> service.putAlternateContact(ACCOUNT_ID, request), member);
}
@Test
@@ -173,8 +179,8 @@ class AccountServiceTest {
void getAndDeleteAlternateContactRequireAKnownContactType(String type) {
ObjectNode request = mapper.createObjectNode().put("AlternateContactType", type);
assertAlternateContactRejected(() -> service.getAlternateContact(ACCOUNT_ID, request), "AlternateContactType");
assertAlternateContactRejected(() -> service.deleteAlternateContact(ACCOUNT_ID, request),
assertInvalidParameter(() -> service.getAlternateContact(ACCOUNT_ID, request), "AlternateContactType");
assertInvalidParameter(() -> service.deleteAlternateContact(ACCOUNT_ID, request),
"AlternateContactType");
}
@@ -186,7 +192,7 @@ class AccountServiceTest {
request.put("AccountId", "111122223333");
request.put("AlternateContactType", "OTHER");
assertAlternateContactRejected(() -> service.getAlternateContact(ACCOUNT_ID, request), "AlternateContactType");
assertInvalidParameter(() -> service.getAlternateContact(ACCOUNT_ID, request), "AlternateContactType");
}
@Test
@@ -194,9 +200,9 @@ class AccountServiceTest {
ObjectNode request = alternateContactRequest();
request.put("AccountId", "1234");
assertAlternateContactRejected(() -> service.putAlternateContact(ACCOUNT_ID, request), "AccountId");
assertAlternateContactRejected(() -> service.getAlternateContact(ACCOUNT_ID, request), "AccountId");
assertAlternateContactRejected(() -> service.deleteAlternateContact(ACCOUNT_ID, request), "AccountId");
assertInvalidParameter(() -> service.putAlternateContact(ACCOUNT_ID, request), "AccountId");
assertInvalidParameter(() -> service.getAlternateContact(ACCOUNT_ID, request), "AccountId");
assertInvalidParameter(() -> service.deleteAlternateContact(ACCOUNT_ID, request), "AccountId");
}
@Test
@@ -450,6 +456,10 @@ class AccountServiceTest {
assertManagementAccountRefused(() -> service.deleteAlternateContact(ACCOUNT_ID, alternateContact));
assertManagementAccountRefused(() -> service.putContactInformation(ACCOUNT_ID, contactInformation));
assertManagementAccountRefused(() -> service.getContactInformation(ACCOUNT_ID, contactInformation));
assertManagementAccountRefused(() -> service.putAccountName(ACCOUNT_ID,
accountNameRequest(MANAGEMENT_ACCOUNT_ID, "Management")));
assertManagementAccountRefused(() -> service.getAccountInformation(ACCOUNT_ID,
mapper.createObjectNode().put("AccountId", MANAGEMENT_ACCOUNT_ID)));
}
@Test
@@ -482,6 +492,167 @@ class AccountServiceTest {
assertEquals("ResourceNotFoundException", error.getErrorCode());
}
@Test
void accountOutsideAnOrganizationIsActiveAndUnnamedUntilPutAccountName() {
assertEquals(new AccountInformation(ACCOUNT_ID, null, null, "ACTIVE"),
service.getAccountInformation(ACCOUNT_ID, mapper.createObjectNode()));
service.putAccountName(ACCOUNT_ID, accountNameRequest("Dev Sandbox"));
service.putAccountName(ACCOUNT_ID, accountNameRequest("Dev Sandbox 2"));
assertEquals(new AccountInformation(ACCOUNT_ID, "Dev Sandbox 2", null, "ACTIVE"),
service.getAccountInformation(ACCOUNT_ID, mapper.createObjectNode()));
assertNull(service.getAccountInformation("111122223333", mapper.createObjectNode()).accountName());
}
@Test
void accountInAnOrganizationIsDescribedByItsOrganizationRecord() {
Instant joined = Instant.parse("2026-11-17T08:00:00Z");
when(organizations.findAccount(ACCOUNT_ID))
.thenReturn(Optional.of(organizationAccount("CREATED", "ACTIVE", joined)));
assertEquals(new AccountInformation(ACCOUNT_ID, "Team Account", joined, "ACTIVE"),
service.getAccountInformation(ACCOUNT_ID, mapper.createObjectNode()));
}
@Test
void invitedAccountHasNoCreationDate() {
when(organizations.findAccount(ACCOUNT_ID))
.thenReturn(Optional.of(organizationAccount("INVITED", "ACTIVE", Instant.EPOCH)));
assertNull(service.getAccountInformation(ACCOUNT_ID, mapper.createObjectNode()).accountCreatedDate());
}
@Test
void accountPendingClosureIsStillActive() {
when(organizations.findAccount(ACCOUNT_ID))
.thenReturn(Optional.of(organizationAccount("CREATED", "PENDING_CLOSURE", Instant.EPOCH)));
assertEquals("ACTIVE", service.getAccountInformation(ACCOUNT_ID, mapper.createObjectNode()).accountState());
}
@Test
void putAccountNameRenamesTheOrganizationRecordAndKeepsTheNameForTheAccount() {
service.putAccountName(ACCOUNT_ID, accountNameRequest("Team Account"));
verify(organizations).renameAccount(ACCOUNT_ID, "Team Account");
// Once no organization holds the account, the name kept for it answers.
assertEquals("Team Account",
service.getAccountInformation(ACCOUNT_ID, mapper.createObjectNode()).accountName());
}
@Test
void managementAccountRenamesAMemberAccount() {
String member = "111122223333";
callerIsInAnOrganizationManagedBy(ACCOUNT_ID, new OrganizationAccount());
service.putAccountName(ACCOUNT_ID, accountNameRequest(member, "Member"));
verify(organizations).renameAccount(member, "Member");
}
@Test
void accountInformationForAnotherAccountNeedsAnOrganization() {
when(organizations.describeOrganization(ACCOUNT_ID))
.thenThrow(new AwsException("AWSOrganizationsNotInUseException", "no organization", 400));
AwsException putError = assertThrows(AwsException.class,
() -> service.putAccountName(ACCOUNT_ID, accountNameRequest("111122223333", "Member")));
assertEquals("AccessDeniedException", putError.getErrorCode());
AwsException getError = assertThrows(AwsException.class, () -> service.getAccountInformation(ACCOUNT_ID,
mapper.createObjectNode().put("AccountId", "111122223333")));
assertEquals("AccessDeniedException", getError.getErrorCode());
}
@Test
void accountInformationRejectsAMalformedAccountId() {
assertInvalidParameter(() -> service.putAccountName(ACCOUNT_ID, accountNameRequest("1234", "Member")),
"AccountId");
assertInvalidParameter(() -> service.getAccountInformation(ACCOUNT_ID,
mapper.createObjectNode().put("AccountId", "1234")), "AccountId");
}
@ParameterizedTest
@ValueSource(strings = {"A", "Dev Sandbox", "a!\"#$%&'()*+,-./0:;=?@[\\]^_`{|}~"})
void putAccountNameAcceptsPrintableAsciiOtherThanAngleBrackets(String name) {
service.putAccountName(ACCOUNT_ID, accountNameRequest(name));
assertEquals(name, service.getAccountInformation(ACCOUNT_ID, mapper.createObjectNode()).accountName());
}
@ParameterizedTest
@ValueSource(strings = {"", "<script>", "a>b", "Caf\u00e9", "tab\there"})
void putAccountNameRejectsANameOutsideTheModel(String name) {
assertInvalidParameter(() -> service.putAccountName(ACCOUNT_ID, accountNameRequest(name)), "AccountName");
}
@Test
void putAccountNameLimitsTheNameToFiftyCharacters() {
service.putAccountName(ACCOUNT_ID, accountNameRequest("a".repeat(50)));
assertInvalidParameter(() -> service.putAccountName(ACCOUNT_ID, accountNameRequest("a".repeat(51))),
"AccountName");
}
@Test
void putAccountNameRequiresAName() {
assertInvalidParameter(() -> service.putAccountName(ACCOUNT_ID, mapper.createObjectNode()),
"AccountName");
}
@Test
void putAccountNameWithANonStringNameThrowsSerialization() {
ObjectNode request = mapper.createObjectNode();
request.put("AccountName", 42);
AwsException error = assertThrows(AwsException.class, () -> service.putAccountName(ACCOUNT_ID, request));
assertEquals("SerializationException", error.getErrorCode());
assertEquals("AccountName must be a string.", error.getMessage());
}
@Test
void clearRemovesAccountNames() {
service.putAccountName(ACCOUNT_ID, accountNameRequest("Dev Sandbox"));
service.clear();
assertNull(service.getAccountInformation(ACCOUNT_ID, mapper.createObjectNode()).accountName());
}
@Test
void accountKeepsItsOrganizationNameOnceItLeaves() {
when(organizations.findAccount(ACCOUNT_ID))
.thenReturn(Optional.of(organizationAccount("CREATED", "ACTIVE", Instant.EPOCH)));
service.accountPlacementChanged(MANAGEMENT_ACCOUNT_ID, ACCOUNT_ID);
when(organizations.findAccount(ACCOUNT_ID)).thenReturn(Optional.empty());
assertEquals("Team Account",
service.getAccountInformation(ACCOUNT_ID, mapper.createObjectNode()).accountName());
}
@Test
void joiningAnOrganizationKeepsTheNameTheAccountAlreadyHas() {
service.putAccountName(ACCOUNT_ID, accountNameRequest("Own Name"));
when(organizations.findAccount(ACCOUNT_ID))
.thenReturn(Optional.of(organizationAccount("INVITED", "ACTIVE", Instant.EPOCH)));
service.accountPlacementChanged(MANAGEMENT_ACCOUNT_ID, ACCOUNT_ID);
when(organizations.findAccount(ACCOUNT_ID)).thenReturn(Optional.empty());
assertEquals("Own Name",
service.getAccountInformation(ACCOUNT_ID, mapper.createObjectNode()).accountName());
}
private static OrganizationAccount organizationAccount(String joinedMethod, String status, Instant joined) {
OrganizationAccount account = new OrganizationAccount();
account.setId(ACCOUNT_ID);
account.setName("Team Account");
account.setJoinedMethod(joinedMethod);
account.setStatus(status);
account.setJoinedTimestamp(joined);
return account;
}
private void assertContactInformationRejected(String member, String value) {
ObjectNode request = contactInformationRequest();
contactOf(request).put(member, value);
@@ -492,7 +663,7 @@ class AccountServiceTest {
assertTrue(error.getMessage().contains(member), error.getMessage());
}
private static void assertAlternateContactRejected(Executable call, String member) {
private static void assertInvalidParameter(Executable call, String member) {
AwsException error = assertThrows(AwsException.class, call, member);
assertEquals("ValidationException", error.getErrorCode());
assertTrue(error.getMessage().startsWith("Invalid parameter at '" + member + "'"), error.getMessage());
@@ -984,6 +984,8 @@ class IamRestRouteEnforcementIntegrationTest {
"account:GetAlternateContact");
checkedAs(user, "account", "POST", "/getContactInformation", "{}", "account:GetContactInformation");
checkedAs(user, "account", "POST", "/putContactInformation", "{}", "account:PutContactInformation");
checkedAs(user, "account", "POST", "/getAccountInformation", "{}", "account:GetAccountInformation");
checkedAs(user, "account", "POST", "/putAccountName", "{}", "account:PutAccountName");
checkedAs(user, "controlcatalog", "POST", "/list-controls", "{}", "controlcatalog:ListControls");
checkedAs(user, "fis", "GET", "/experimentTemplates", null, "fis:ListExperimentTemplates");
checkedAs(user, "oam", "POST", "/ListSinks", "{}", "oam:ListSinks");
@@ -91,6 +91,29 @@ class OrganizationsServiceTest {
service.listTagsForResource(MANAGEMENT_ACCOUNT, status.getAccountId()));
}
@Test
void renameAccountRenamesAMemberAccountAndTheManagementAccount() {
service.createOrganization(MANAGEMENT_ACCOUNT, "ALL");
String member = service.createAccount(MANAGEMENT_ACCOUNT, "member@example.com", "Member", null, false)
.getAccountId();
service.renameAccount(member, "Renamed Member");
service.renameAccount(MANAGEMENT_ACCOUNT, "Renamed Management");
assertEquals("Renamed Member", service.describeAccount(MANAGEMENT_ACCOUNT, member).getName());
assertEquals("Renamed Management", service.describeAccount(MANAGEMENT_ACCOUNT, MANAGEMENT_ACCOUNT).getName());
assertEquals(Set.of("Renamed Member", "Renamed Management"), Set.copyOf(service.listAccounts(MANAGEMENT_ACCOUNT)
.stream().map(OrganizationAccount::getName).toList()));
assertEquals("Renamed Member", service.findAccount(member).orElseThrow().getName());
}
@Test
void accountInNoOrganizationIsNeitherFoundNorRenamed() {
service.renameAccount(OUTSIDER_ACCOUNT, "Outsider");
assertTrue(service.findAccount(OUTSIDER_ACCOUNT).isEmpty());
}
/** The (member account, role name, management account) triples a provisioner received. */
private static final class RecordedProvisions implements AccountAccessRoleProvisioner {
final List<String> calls = new ArrayList<>();
@@ -59,7 +59,7 @@ class SsoPortalServiceTest {
Instant.now().plusSeconds(3600).getEpochSecond(), Instant.now().plusSeconds(7200).getEpochSecond(), principalId));
when(ssoAdminService.portalAssignmentsForUser(principalId)).thenReturn(List.of(
new Assignment(accountId, permissionSetArn, principalId, "USER")));
when(organizationsService.findAccountForPortal(accountId)).thenReturn(Optional.empty());
when(organizationsService.findAccount(accountId)).thenReturn(Optional.empty());
when(ssoAdminService.permissionSetForPortal(permissionSetArn)).thenReturn(new PermissionSet(
permissionSetArn, "PlatformAdmins", null, "PT1H", Map.of(), Map.of(), null, null, Map.of(), null));