feat(account): implement GetContactInformation and PutContactInformation (#8262)

This commit is contained in:
Yang Hao authored and GitHub committed 2026-11-14 01:04:49 -04:00
1 parent 16ff5a2c74
commit 5f03ff975d
15 files changed
+945 -4

No files matched your search

@@ -0,0 +1,173 @@
package io.floci.aws.services.account;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
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.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;
@QuarkusTest
class AccountContactInformationIntegrationTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final AtomicInteger ACCOUNTS = new AtomicInteger();
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void putThenGetReturnsTheContactAndItsVerificationStatus() {
String account = newAccount();
ObjectNode request = contactInformationRequest();
contactOf(request).put("CompanyName", "Example Corp");
put(account, request).statusCode(200).body(equalTo(""));
get(account, "{}").statusCode(200)
.body("ContactInformation.FullName", equalTo("Jane Doe"))
.body("ContactInformation.AddressLine1", equalTo("1 Main St"))
.body("ContactInformation.City", equalTo("Seattle"))
.body("ContactInformation.StateOrRegion", equalTo("WA"))
.body("ContactInformation.PostalCode", equalTo("98101"))
.body("ContactInformation.CountryCode", equalTo("US"))
.body("ContactInformation.PhoneNumber", equalTo("+12065550100"))
.body("ContactInformation.CompanyName", equalTo("Example Corp"))
.body("ContactInformation", not(hasKey("AddressLine2")))
.body("ContactInformation", not(hasKey("WebsiteUrl")))
.body("VerificationStatus", equalTo("NOT_SUPPORTED"));
}
@Test
void putReplacesTheStoredContact() {
String account = newAccount();
ObjectNode first = contactInformationRequest();
contactOf(first).put("CompanyName", "Example Corp");
put(account, first).statusCode(200);
ObjectNode second = contactInformationRequest();
contactOf(second).put("FullName", "John Roe");
put(account, second).statusCode(200);
get(account, "{}").statusCode(200)
.body("ContactInformation.FullName", equalTo("John Roe"))
.body("ContactInformation", not(hasKey("CompanyName")));
}
@Test
void getBeforePutReturnsResourceNotFound() {
get(newAccount(), "{}").statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
}
@Test
void contactsAreIsolatedByCallerAccount() {
String owner = newAccount();
put(owner, contactInformationRequest()).statusCode(200);
get(owner, "{}").statusCode(200);
get(newAccount(), "{}").statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
}
@Test
void putWithoutContactInformationReturnsValidationException() {
put(newAccount(), MAPPER.createObjectNode())
.statusCode(400).body("__type", equalTo("ValidationException"));
}
@Test
void putWithNonStructureContactInformationReturnsSerializationException() {
ObjectNode request = MAPPER.createObjectNode();
request.put("ContactInformation", "Jane Doe");
put(newAccount(), request).statusCode(400).body("__type", equalTo("SerializationException"));
}
@Test
void nonObjectRequestBodyReturnsSerializationException() {
String account = newAccount();
put(account, contactInformationRequest()).statusCode(200);
get(account, "[]").statusCode(400).body("__type", equalTo("SerializationException"));
post(account, "/putContactInformation", "[]")
.statusCode(400).body("__type", equalTo("SerializationException"));
}
@Test
void rejectedPutKeepsThePreviousContact() {
String account = newAccount();
put(account, contactInformationRequest()).statusCode(200);
ObjectNode invalid = contactInformationRequest();
contactOf(invalid).put("FullName", "John Roe");
contactOf(invalid).put("PhoneNumber", "12065550100");
put(account, invalid).statusCode(400).body("__type", equalTo("ValidationException"));
get(account, "{}").statusCode(200).body("ContactInformation.FullName", equalTo("Jane Doe"));
}
@Test
void putRequiresStateOrRegionForTheUnitedStates() {
ObjectNode request = contactInformationRequest();
contactOf(request).remove("StateOrRegion");
put(newAccount(), request).statusCode(400).body("__type", equalTo("ValidationException"));
}
@Test
void nonStringAccountIdReturnsSerializationException() {
String account = newAccount();
ObjectNode request = contactInformationRequest();
request.put("AccountId", 123456789012L);
put(account, request).statusCode(400).body("__type", equalTo("SerializationException"));
get(account, "{\"AccountId\":123456789012}")
.statusCode(400).body("__type", equalTo("SerializationException"));
}
@Test
void anotherAccountIsRefusedOutsideAnOrganization() {
String account = newAccount();
ObjectNode request = contactInformationRequest();
request.put("AccountId", "111122223333");
put(account, request).statusCode(403).body("__type", equalTo("AccessDeniedException"));
get(account, "{\"AccountId\":\"111122223333\"}")
.statusCode(403).body("__type", equalTo("AccessDeniedException"));
}
/**
* Contact information cannot be deleted, so every test acts on an account of its own. A 12-digit access key
* ID is the account ID a request acts as.
*/
private static String newAccount() {
return String.format("7310008259%02d", ACCOUNTS.incrementAndGet());
}
private static ValidatableResponse put(String account, ObjectNode request) {
return post(account, "/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";
}
}
@@ -4,19 +4,29 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
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.AlternateContact;
import io.floci.aws.services.account.model.ContactInformation;
import io.floci.aws.services.organizations.OrganizationsService;
import io.floci.aws.services.organizations.model.Organization;
import io.floci.aws.services.organizations.model.OrganizationAccount;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static io.floci.aws.services.account.ContactInformationRequests.contactInformationRequest;
import static io.floci.aws.services.account.ContactInformationRequests.contactOf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
@@ -37,6 +47,9 @@ class AccountServiceTest {
AccountAwareStorageBackend<AlternateContact> store = AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
when(storageFactory.create(eq("account"), eq("account-alternate-contacts.json"), any(TypeReference.class)))
.thenReturn((AccountAwareStorageBackend) store);
AccountAwareStorageBackend<ContactInformation> primaryStore = AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
when(storageFactory.create(eq("account"), eq("account-contact-information.json"), any(TypeReference.class)))
.thenReturn((AccountAwareStorageBackend) primaryStore);
organizations = mock(OrganizationsService.class);
service = new AccountService(storageFactory, organizations);
}
@@ -106,6 +119,244 @@ class AccountServiceTest {
assertEquals(Optional.empty(), service.find("accountInOrganization", List.of("111122223333"), "us-east-1"));
}
@Test
void contactInformationPutAndGetRoundTrip() {
ObjectNode request = contactInformationRequest();
ObjectNode contact = contactOf(request);
contact.put("AddressLine2", "Suite 500");
contact.put("CompanyName", "Example Corp");
contact.put("WebsiteUrl", "https://example.com");
service.putContactInformation(ACCOUNT_ID, request);
ContactInformation stored = service.getContactInformation(ACCOUNT_ID, mapper.createObjectNode());
assertEquals("Jane Doe", stored.getFullName());
assertEquals("1 Main St", stored.getAddressLine1());
assertEquals("Suite 500", stored.getAddressLine2());
assertNull(stored.getAddressLine3());
assertEquals("Seattle", stored.getCity());
assertEquals("WA", stored.getStateOrRegion());
assertNull(stored.getDistrictOrCounty());
assertEquals("98101", stored.getPostalCode());
assertEquals("US", stored.getCountryCode());
assertEquals("+12065550100", stored.getPhoneNumber());
assertEquals("Example Corp", stored.getCompanyName());
assertEquals("https://example.com", stored.getWebsiteUrl());
}
@Test
void putContactInformationReplacesThePreviousContact() {
ObjectNode first = contactInformationRequest();
contactOf(first).put("CompanyName", "Example Corp");
service.putContactInformation(ACCOUNT_ID, first);
ObjectNode second = contactInformationRequest();
contactOf(second).put("FullName", "John Roe");
service.putContactInformation(ACCOUNT_ID, second);
ContactInformation stored = service.getContactInformation(ACCOUNT_ID, mapper.createObjectNode());
assertEquals("John Roe", stored.getFullName());
assertNull(stored.getCompanyName());
}
@Test
void getContactInformationBeforePutThrowsResourceNotFound() {
AwsException error = assertThrows(AwsException.class,
() -> service.getContactInformation(ACCOUNT_ID, mapper.createObjectNode()));
assertEquals("ResourceNotFoundException", error.getErrorCode());
assertEquals(404, error.getHttpStatus());
}
@Test
void putContactInformationWithoutContactInformationThrowsValidation() {
AwsException error = assertThrows(AwsException.class,
() -> service.putContactInformation(ACCOUNT_ID, mapper.createObjectNode()));
assertEquals("ValidationException", error.getErrorCode());
}
@Test
void putContactInformationWithNonStructureContactInformationThrowsSerialization() {
ObjectNode request = mapper.createObjectNode();
request.put("ContactInformation", "Jane Doe");
AwsException error = assertThrows(AwsException.class,
() -> service.putContactInformation(ACCOUNT_ID, request));
assertEquals("SerializationException", error.getErrorCode());
}
@Test
void putContactInformationWithANonStringMemberThrowsSerialization() {
ObjectNode request = contactInformationRequest();
contactOf(request).put("PostalCode", 98101);
AwsException error = assertThrows(AwsException.class,
() -> service.putContactInformation(ACCOUNT_ID, request));
assertEquals("SerializationException", error.getErrorCode());
}
@ParameterizedTest
@ValueSource(strings = {"FullName", "AddressLine1", "City", "PostalCode", "CountryCode", "PhoneNumber"})
void putContactInformationRejectsAMissingRequiredMember(String member) {
ObjectNode request = contactInformationRequest();
contactOf(request).remove(member);
AwsException error = assertThrows(AwsException.class,
() -> service.putContactInformation(ACCOUNT_ID, request));
assertEquals("ValidationException", error.getErrorCode());
assertTrue(error.getMessage().contains(member), error.getMessage());
}
@ParameterizedTest
@CsvSource({"FullName,50", "AddressLine1,60", "AddressLine2,60", "AddressLine3,60", "City,50",
"StateOrRegion,50", "DistrictOrCounty,50", "PostalCode,20", "CompanyName,50", "WebsiteUrl,256"})
void putContactInformationEnforcesTheMaximumLengthOfEachMember(String member, int maximum) {
ObjectNode request = contactInformationRequest();
contactOf(request).put(member, "1".repeat(maximum));
service.putContactInformation(ACCOUNT_ID, request);
assertContactInformationRejected(member, "1".repeat(maximum + 1));
}
@Test
void putContactInformationRejectsAnEmptyOptionalMember() {
assertContactInformationRejected("AddressLine2", "");
}
@ParameterizedTest
@ValueSource(strings = {"U", "USA"})
void putContactInformationRequiresATwoCharacterCountryCode(String countryCode) {
assertContactInformationRejected("CountryCode", countryCode);
}
@Test
void putContactInformationAcceptsSpacesParenthesesAndHyphensInThePhoneNumber() {
ObjectNode request = contactInformationRequest();
contactOf(request).put("PhoneNumber", "+1 (206) 555-0100");
service.putContactInformation(ACCOUNT_ID, request);
assertEquals("+1 (206) 555-0100",
service.getContactInformation(ACCOUNT_ID, mapper.createObjectNode()).getPhoneNumber());
}
@ParameterizedTest
@ValueSource(strings = {"12065550100", "+", "1+2065550100", "+1 206 555 ext 7"})
void putContactInformationRejectsAPhoneNumberOutsideThePattern(String phoneNumber) {
assertContactInformationRejected("PhoneNumber", phoneNumber);
}
@Test
void putContactInformationLimitsThePhoneNumberToTwentyCharacters() {
ObjectNode request = contactInformationRequest();
contactOf(request).put("PhoneNumber", "+" + "1".repeat(19));
service.putContactInformation(ACCOUNT_ID, request);
assertContactInformationRejected("PhoneNumber", "+" + "1".repeat(20));
}
@ParameterizedTest
@ValueSource(strings = {"US", "CA", "GB", "DE", "JP", "IN", "BR"})
void putContactInformationRequiresStateOrRegionForTheDocumentedCountries(String countryCode) {
ObjectNode request = contactInformationRequest();
ObjectNode contact = contactOf(request);
contact.put("CountryCode", countryCode);
contact.remove("StateOrRegion");
AwsException error = assertThrows(AwsException.class,
() -> service.putContactInformation(ACCOUNT_ID, request));
assertEquals("ValidationException", error.getErrorCode());
assertTrue(error.getMessage().contains("StateOrRegion"), error.getMessage());
}
@Test
void putContactInformationRequiresStateOrRegionForALowercaseCountryCode() {
ObjectNode request = contactInformationRequest();
ObjectNode contact = contactOf(request);
contact.put("CountryCode", "us");
contact.remove("StateOrRegion");
AwsException error = assertThrows(AwsException.class,
() -> service.putContactInformation(ACCOUNT_ID, request));
assertEquals("ValidationException", error.getErrorCode());
}
@Test
void putContactInformationAllowsNoStateOrRegionElsewhere() {
ObjectNode request = contactInformationRequest();
ObjectNode contact = contactOf(request);
contact.put("CountryCode", "FR");
contact.remove("StateOrRegion");
service.putContactInformation(ACCOUNT_ID, request);
assertNull(service.getContactInformation(ACCOUNT_ID, mapper.createObjectNode()).getStateOrRegion());
}
@Test
void contactInformationForAnotherAccountNeedsAnOrganization() {
when(organizations.describeOrganization(ACCOUNT_ID))
.thenThrow(new AwsException("AWSOrganizationsNotInUseException", "no organization", 400));
ObjectNode request = contactInformationRequest();
request.put("AccountId", "111122223333");
AwsException putError = assertThrows(AwsException.class,
() -> service.putContactInformation(ACCOUNT_ID, request));
assertEquals("AccessDeniedException", putError.getErrorCode());
AwsException getError = assertThrows(AwsException.class,
() -> service.getContactInformation(ACCOUNT_ID, request));
assertEquals("AccessDeniedException", getError.getErrorCode());
}
@Test
void contactInformationRejectsAMalformedAccountId() {
ObjectNode request = contactInformationRequest();
request.put("AccountId", "1234");
AwsException putError = assertThrows(AwsException.class,
() -> service.putContactInformation(ACCOUNT_ID, request));
assertEquals("ValidationException", putError.getErrorCode());
AwsException getError = assertThrows(AwsException.class,
() -> service.getContactInformation(ACCOUNT_ID, request));
assertEquals("ValidationException", getError.getErrorCode());
}
@Test
void managementAccountPutsContactInformationForAMemberAccount() {
String member = "111122223333";
Organization organization = new Organization();
organization.setFeatureSet("ALL");
organization.setMasterAccountId(ACCOUNT_ID);
organization.setEnabledServicePrincipals(Map.of(ServicePrincipals.of("account"), Instant.EPOCH));
when(organizations.describeOrganization(ACCOUNT_ID)).thenReturn(organization);
when(organizations.describeAccount(ACCOUNT_ID, ACCOUNT_ID)).thenReturn(new OrganizationAccount());
ObjectNode request = contactInformationRequest();
request.put("AccountId", member);
service.putContactInformation(ACCOUNT_ID, request);
ObjectNode memberRequest = mapper.createObjectNode();
memberRequest.put("AccountId", member);
assertEquals("Jane Doe", service.getContactInformation(ACCOUNT_ID, memberRequest).getFullName());
assertEquals("Jane Doe", service.getContactInformation(member, mapper.createObjectNode()).getFullName());
AwsException error = assertThrows(AwsException.class,
() -> service.getContactInformation(ACCOUNT_ID, mapper.createObjectNode()));
assertEquals("ResourceNotFoundException", error.getErrorCode());
}
@Test
void clearRemovesContactInformation() {
service.putContactInformation(ACCOUNT_ID, contactInformationRequest());
service.clear();
AwsException error = assertThrows(AwsException.class,
() -> service.getContactInformation(ACCOUNT_ID, mapper.createObjectNode()));
assertEquals("ResourceNotFoundException", error.getErrorCode());
}
private void assertContactInformationRejected(String member, String value) {
ObjectNode request = contactInformationRequest();
contactOf(request).put(member, value);
AwsException error = assertThrows(AwsException.class,
() -> service.putContactInformation(ACCOUNT_ID, request),
member + "=" + value);
assertEquals("ValidationException", error.getErrorCode());
assertTrue(error.getMessage().contains(member), error.getMessage());
}
private ObjectNode contactRequest() {
ObjectNode request = mapper.createObjectNode();
request.put("AlternateContactType", "SECURITY");
@@ -0,0 +1,31 @@
package io.floci.aws.services.account;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
/** The valid {@code PutContactInformation} request the primary contact tests start from. */
final class ContactInformationRequests {
private static final ObjectMapper MAPPER = new ObjectMapper();
private ContactInformationRequests() {
}
static ObjectNode contactInformationRequest() {
ObjectNode contact = MAPPER.createObjectNode();
contact.put("FullName", "Jane Doe");
contact.put("AddressLine1", "1 Main St");
contact.put("City", "Seattle");
contact.put("StateOrRegion", "WA");
contact.put("PostalCode", "98101");
contact.put("CountryCode", "US");
contact.put("PhoneNumber", "+12065550100");
ObjectNode request = MAPPER.createObjectNode();
request.set("ContactInformation", contact);
return request;
}
static ObjectNode contactOf(ObjectNode request) {
return (ObjectNode) request.get("ContactInformation");
}
}
@@ -845,6 +845,8 @@ class IamRestRouteEnforcementIntegrationTest {
checkedAs(user, "account", "POST", "/getAlternateContact", "{\"AlternateContactType\":\"BILLING\"}",
"account:GetAlternateContact");
checkedAs(user, "account", "POST", "/getContactInformation", "{}", "account:GetContactInformation");
checkedAs(user, "account", "POST", "/putContactInformation", "{}", "account:PutContactInformation");
checkedAs(user, "controlcatalog", "POST", "/list-controls", "{}", "controlcatalog:ListControls");
checkedAs(user, "fis", "GET", "/experimentTemplates", null, "fis:ListExperimentTemplates");
checkedAs(user, "oam", "POST", "/ListSinks", "{}", "oam:ListSinks");