Use pni in DynamoDB account put condition expression

This commit is contained in:
Chris Eager 2024-11-22 15:24:48 -06:00 committed by Chris Eager
parent 5627ed141b
commit 9a19ef82fd
2 changed files with 42 additions and 3 deletions

View File

@ -1419,9 +1419,9 @@ public class Accounts extends AbstractDynamoDbStore {
return TransactWriteItem.builder()
.put(Put.builder()
.conditionExpression("attribute_not_exists(#number) OR #number = :number")
.expressionAttributeNames(Map.of("#number", ATTR_ACCOUNT_E164))
.expressionAttributeValues(Map.of(":number", numberAttr))
.conditionExpression("attribute_not_exists(#pni) OR #pni = :pni")
.expressionAttributeNames(Map.of("#pni", ATTR_PNI_UUID))
.expressionAttributeValues(Map.of(":pni", pniUuidAttr))
.tableName(accountsTableName)
.item(item)
.build())

View File

@ -235,6 +235,45 @@ class AccountsTest {
assertPhoneNumberIdentifierConstraintExists(account.getPhoneNumberIdentifier(), account.getUuid());
}
@Test
void testStoreAciCollisionFails() {
Device device = generateDevice(DEVICE_ID_1);
Account account = generateAccount("+14151112222", UUID.randomUUID(), UUID.randomUUID(), List.of(device));
boolean freshUser = createAccount(account);
assertThat(freshUser).isTrue();
verifyStoredState("+14151112222", account.getUuid(), account.getPhoneNumberIdentifier(), null, account, true);
assertPhoneNumberConstraintExists("+14151112222", account.getUuid());
assertPhoneNumberIdentifierConstraintExists(account.getPhoneNumberIdentifier(), account.getUuid());
account.setNumber("+14153334444", UUID.randomUUID());
assertThrows(IllegalArgumentException.class, () -> createAccount(account),
"Reusing ACI with different PNI should fail");
}
@Test
void testStorePniCollisionFails() {
Device device1 = generateDevice(DEVICE_ID_1);
Account account1 = generateAccount("+14151112222", UUID.randomUUID(), UUID.randomUUID(), List.of(device1));
boolean freshUser = createAccount(account1);
assertThat(freshUser).isTrue();
verifyStoredState("+14151112222", account1.getUuid(), account1.getPhoneNumberIdentifier(), null, account1, true);
assertPhoneNumberConstraintExists("+14151112222", account1.getUuid());
assertPhoneNumberIdentifierConstraintExists(account1.getPhoneNumberIdentifier(), account1.getUuid());
Device device2 = generateDevice(DEVICE_ID_1);
Account account2 = generateAccount("+14151112222", UUID.randomUUID(), account1.getPhoneNumberIdentifier(),
List.of(device2));
assertThrows(AccountAlreadyExistsException.class, () -> accounts.create(account2, Collections.emptyList()),
"New ACI with same PNI should fail");
}
@Test
void testRetrieve() {
final List<Device> devicesFirst = List.of(generateDevice(DEVICE_ID_1), generateDevice(DEVICE_ID_2));