Update AccountControllerTest for accounts that may not have phone numbers

This commit is contained in:
Jon Chambers authored and Jon Chambers committed 2026-08-12 09:09:29 -04:00
1 parent 7ec1f78406
commit 7d39f2952b
2 files changed
+46 -9

No files matched your search

@@ -55,7 +55,6 @@ import org.whispersystems.textsecuregcm.entities.ReserveUsernameHashResponse;
import org.whispersystems.textsecuregcm.entities.UsernameHashResponse;
import org.whispersystems.textsecuregcm.entities.UsernameLinkHandle;
import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier;
import org.whispersystems.textsecuregcm.identity.IdentityType;
import org.whispersystems.textsecuregcm.identity.ServiceIdentifier;
import org.whispersystems.textsecuregcm.limits.RateLimitedByIp;
import org.whispersystems.textsecuregcm.limits.RateLimiters;
@@ -208,7 +207,7 @@ public class AccountController {
throw new ForbiddenException();
}
accounts.updateDevice(account.getIdentifier(IdentityType.ACI), targetDeviceId, d -> d.setName(deviceName.deviceName()));
accounts.updateDevice(account.getAccountIdentifier(), targetDeviceId, d -> d.setName(deviceName.deviceName()));
}
@PUT
@@ -229,7 +228,7 @@ public class AccountController {
List.of(phoneNumberRecoveryPasswordsManager.buildTransactWriteItemForStorePassword(phoneNumberIdentifier, recoveryPassword))))
.orElseGet(Collections::emptyList);
final Account updatedAccount = accounts.update(auth.accountIdentifier(), a -> {
accounts.update(auth.accountIdentifier(), a -> {
a.getDevice(auth.deviceId()).ifPresent(d -> {
d.setFetchesMessages(attributes.getFetchesMessages());
d.setName(attributes.getName());
@@ -243,6 +242,11 @@ public class AccountController {
a.setRegistrationLockFromAttributes(attributes);
a.setUnidentifiedAccessKey(attributes.getUnidentifiedAccessKey());
a.setUnrestrictedUnidentifiedAccess(attributes.isUnrestrictedUnidentifiedAccess());
if (attributes.isDiscoverableByPhoneNumber() && a.getNumberOptional().isEmpty()) {
throw new BadRequestException("account does not have a phone number");
}
a.setDiscoverableByPhoneNumber(attributes.isDiscoverableByPhoneNumber());
attributes.recoveryPassword().ifPresent(a::setAccountRecoveryPassword);
@@ -430,7 +434,7 @@ public class AccountController {
} else {
usernameLinkHandle = UUID.randomUUID();
}
updateUsernameLink(account.getIdentifier(IdentityType.ACI), usernameLinkHandle, encryptedUsername.usernameLinkEncryptedValue());
updateUsernameLink(account.getAccountIdentifier(), usernameLinkHandle, encryptedUsername.usernameLinkEncryptedValue());
return new UsernameLinkHandle(usernameLinkHandle);
}
@@ -197,12 +197,12 @@ class AccountControllerTest {
Optional.of(registrationLockCredentials.salt()), Instant.ofEpochMilli(System.currentTimeMillis())));
when(senderRegLockAccount.getLastSeen()).thenReturn(System.currentTimeMillis());
when(senderRegLockAccount.getAccountIdentifier()).thenReturn(SENDER_REG_LOCK_UUID);
when(senderRegLockAccount.getNumber()).thenReturn(SENDER_REG_LOCK);
when(senderRegLockAccount.getNumberOptional()).thenReturn(Optional.of(SENDER_REG_LOCK));
when(senderTransfer.getRegistrationLock()).thenReturn(
new StoredRegistrationLock(Optional.empty(), Optional.empty(), Instant.ofEpochMilli(System.currentTimeMillis())));
when(senderTransfer.getAccountIdentifier()).thenReturn(SENDER_TRANSFER_UUID);
when(senderTransfer.getNumber()).thenReturn(SENDER_TRANSFER);
when(senderTransfer.getNumberOptional()).thenReturn(Optional.of(SENDER_TRANSFER));
when(accountsManager.getByE164(eq(SENDER_PIN))).thenReturn(Optional.of(senderPinAccount));
when(accountsManager.getByE164(eq(SENDER_REG_LOCK))).thenReturn(Optional.of(senderRegLockAccount));
@@ -375,6 +375,27 @@ class AccountControllerTest {
assertThat(identityResponse.entitlements().backup().backupLevel()).isEqualTo(100);
assertThat(identityResponse.entitlements().backup().expiration()).isEqualTo(truncatedExpiration);
assertThat(identityResponse.number()).hasValue(AuthHelper.VALID_NUMBER);
assertThat(identityResponse.pni()).hasValue(AuthHelper.VALID_PNI);
}
}
@Test
void testWhoAmINoPhoneNumber() {
try (final Response response = resources.getJerseyTest()
.target("/v1/accounts/whoami")
.request()
.header(HttpHeaders.AUTHORIZATION, AuthHelper.getAuthHeader(AuthHelper.NUMBERLESS_UUID, AuthHelper.NUMBERLESS_PASSWORD))
.get()) {
assertThat(response.getStatus()).isEqualTo(200);
final AccountIdentityResponse identityResponse = response.readEntity(AccountIdentityResponse.class);
assertThat(identityResponse.uuid()).isEqualTo(AuthHelper.NUMBERLESS_UUID);
assertThat(identityResponse.number()).isEmpty();
assertThat(identityResponse.pni()).isEmpty();
}
}
@@ -641,7 +662,7 @@ class AccountControllerTest {
assertThat(response.getStatus()).isEqualTo(200);
final UsernameHashResponse respEntity = response.readEntity(UsernameHashResponse.class);
assertArrayEquals(respEntity.usernameHash(), USERNAME_HASH_1);
assertArrayEquals(USERNAME_HASH_1, respEntity.usernameHash());
assertEquals(respEntity.usernameLinkHandle(), uuid);
verify(usernameZkProofVerifier).verifyProof(ZK_PROOF, USERNAME_HASH_1);
}
@@ -678,7 +699,7 @@ class AccountControllerTest {
assertThat(response.getStatus()).isEqualTo(200);
final UsernameHashResponse respEntity = response.readEntity(UsernameHashResponse.class);
assertArrayEquals(respEntity.usernameHash(), USERNAME_HASH_1);
assertArrayEquals(USERNAME_HASH_1, respEntity.usernameHash());
assertNull(respEntity.usernameLinkHandle());
verify(usernameZkProofVerifier).verifyProof(ZK_PROOF, USERNAME_HASH_1);
}
@@ -821,7 +842,6 @@ class AccountControllerTest {
);
}
@Test
void testSetAccountAttributesNoDiscoverabilityChange() {
try (final Response response = resources.getJerseyTest()
@@ -835,6 +855,19 @@ class AccountControllerTest {
}
}
@Test
void testSetAccountAttributesDiscoverableWithNoNumber() {
try (final Response response = resources.getJerseyTest()
.target("/v1/accounts/attributes/")
.request()
.header(HttpHeaders.AUTHORIZATION, AuthHelper.getAuthHeader(AuthHelper.NUMBERLESS_UUID, AuthHelper.NUMBERLESS_PASSWORD))
.put(Entity.json(new AccountAttributes(false, 2222, 3333, null, null, true, null, null)
.setUnidentifiedAccessKey(new byte[16])))) {
assertThat(response.getStatus()).isEqualTo(400);
}
}
@Test
void testSetAccountAttributesEnableDiscovery() {
try (final Response response = resources.getJerseyTest()