mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-23 02:48:00 +01:00
Retire Postgres-backed pending account/device tables.
This commit is contained in:
committed by
Jon Chambers
parent
530b2a310f
commit
d128bc782a
@@ -1,154 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2021 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.whispersystems.textsecuregcm.auth.StoredVerificationCode;
|
||||
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicVerificationCodeStoreMigrationConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicVerificationCodeStoreMigrationConfiguration.WriteDestination;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class PendingAccountsManagerTest {
|
||||
|
||||
private PendingAccounts postgresPendingAccounts;
|
||||
private VerificationCodeStoreDynamoDb dynamoDbPendingAccounts;
|
||||
private DynamicVerificationCodeStoreMigrationConfiguration migrationConfiguration;
|
||||
|
||||
private PendingAccountsManager pendingAccountsManager;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
final DynamicConfigurationManager dynamicConfigurationManager = mock(DynamicConfigurationManager.class);
|
||||
final DynamicConfiguration dynamicConfiguration = mock(DynamicConfiguration.class);
|
||||
migrationConfiguration = mock(DynamicVerificationCodeStoreMigrationConfiguration.class);
|
||||
|
||||
when(dynamicConfigurationManager.getConfiguration()).thenReturn(dynamicConfiguration);
|
||||
when(dynamicConfiguration.getPendingAccountsMigrationConfiguration()).thenReturn(migrationConfiguration);
|
||||
|
||||
postgresPendingAccounts = mock(PendingAccounts.class);
|
||||
dynamoDbPendingAccounts = mock(VerificationCodeStoreDynamoDb.class);
|
||||
|
||||
pendingAccountsManager = new PendingAccountsManager(postgresPendingAccounts, dynamoDbPendingAccounts, dynamicConfigurationManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
void storePostgres() {
|
||||
final String number = "+18005551234";
|
||||
final StoredVerificationCode code = mock(StoredVerificationCode.class);
|
||||
|
||||
when(migrationConfiguration.getWriteDestination()).thenReturn(WriteDestination.POSTGRES);
|
||||
|
||||
pendingAccountsManager.store(number, code);
|
||||
|
||||
verify(postgresPendingAccounts).insert(number, code);
|
||||
verify(dynamoDbPendingAccounts, never()).insert(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void storeDynamoDb() {
|
||||
final String number = "+18005551234";
|
||||
final StoredVerificationCode code = mock(StoredVerificationCode.class);
|
||||
|
||||
when(migrationConfiguration.getWriteDestination()).thenReturn(WriteDestination.DYNAMODB);
|
||||
|
||||
pendingAccountsManager.store(number, code);
|
||||
|
||||
verify(dynamoDbPendingAccounts).insert(number, code);
|
||||
verify(postgresPendingAccounts, never()).insert(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void remove() {
|
||||
final String number = "+18005551234";
|
||||
|
||||
pendingAccountsManager.remove(number);
|
||||
|
||||
verify(postgresPendingAccounts).remove(number);
|
||||
verify(dynamoDbPendingAccounts).remove(number);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCodeForNumber() {
|
||||
final String number = "+18005551234";
|
||||
|
||||
final StoredVerificationCode postgresCode = mock(StoredVerificationCode.class);
|
||||
final StoredVerificationCode dynamoDbCode = mock(StoredVerificationCode.class);
|
||||
|
||||
{
|
||||
when(migrationConfiguration.isReadPostgres()).thenReturn(false);
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(false);
|
||||
when(postgresPendingAccounts.findForNumber(number)).thenReturn(Optional.empty());
|
||||
when(dynamoDbPendingAccounts.findForNumber(number)).thenReturn(Optional.empty());
|
||||
|
||||
assertEquals(Optional.empty(), pendingAccountsManager.getCodeForNumber(number));
|
||||
|
||||
when(migrationConfiguration.isReadPostgres()).thenReturn(true);
|
||||
|
||||
assertEquals(Optional.empty(), pendingAccountsManager.getCodeForNumber(number));
|
||||
|
||||
when(dynamoDbPendingAccounts.findForNumber(number)).thenReturn(Optional.of(dynamoDbCode));
|
||||
|
||||
assertEquals(Optional.empty(), pendingAccountsManager.getCodeForNumber(number));
|
||||
|
||||
when(postgresPendingAccounts.findForNumber(number)).thenReturn(Optional.of(postgresCode));
|
||||
|
||||
assertEquals(Optional.of(postgresCode), pendingAccountsManager.getCodeForNumber(number));
|
||||
}
|
||||
|
||||
{
|
||||
when(migrationConfiguration.isReadPostgres()).thenReturn(false);
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(false);
|
||||
when(postgresPendingAccounts.findForNumber(number)).thenReturn(Optional.empty());
|
||||
when(dynamoDbPendingAccounts.findForNumber(number)).thenReturn(Optional.empty());
|
||||
|
||||
assertEquals(Optional.empty(), pendingAccountsManager.getCodeForNumber(number));
|
||||
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(true);
|
||||
|
||||
assertEquals(Optional.empty(), pendingAccountsManager.getCodeForNumber(number));
|
||||
|
||||
when(postgresPendingAccounts.findForNumber(number)).thenReturn(Optional.of(postgresCode));
|
||||
|
||||
assertEquals(Optional.empty(), pendingAccountsManager.getCodeForNumber(number));
|
||||
|
||||
when(dynamoDbPendingAccounts.findForNumber(number)).thenReturn(Optional.of(dynamoDbCode));
|
||||
|
||||
assertEquals(Optional.of(dynamoDbCode), pendingAccountsManager.getCodeForNumber(number));
|
||||
}
|
||||
|
||||
{
|
||||
when(migrationConfiguration.isReadPostgres()).thenReturn(false);
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(false);
|
||||
when(postgresPendingAccounts.findForNumber(number)).thenReturn(Optional.of(postgresCode));
|
||||
when(dynamoDbPendingAccounts.findForNumber(number)).thenReturn(Optional.of(dynamoDbCode));
|
||||
|
||||
assertEquals(Optional.empty(), pendingAccountsManager.getCodeForNumber(number));
|
||||
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(true);
|
||||
|
||||
assertEquals(Optional.of(dynamoDbCode), pendingAccountsManager.getCodeForNumber(number));
|
||||
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(false);
|
||||
when(migrationConfiguration.isReadPostgres()).thenReturn(true);
|
||||
|
||||
assertEquals(Optional.of(postgresCode), pendingAccountsManager.getCodeForNumber(number));
|
||||
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(true);
|
||||
|
||||
assertEquals(Optional.of(postgresCode), pendingAccountsManager.getCodeForNumber(number));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2021 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
import com.opentable.db.postgres.embedded.LiquibasePreparer;
|
||||
import com.opentable.db.postgres.junit5.EmbeddedPostgresExtension;
|
||||
import com.opentable.db.postgres.junit5.PreparedDbExtension;
|
||||
import org.jdbi.v3.core.Jdbi;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.whispersystems.textsecuregcm.configuration.CircuitBreakerConfiguration;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
class PendingAccountsTest extends VerificationCodeStoreTest {
|
||||
|
||||
@RegisterExtension
|
||||
public static PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forClasspathLocation("accountsdb.xml"));
|
||||
|
||||
private PendingAccounts pendingAccounts;
|
||||
|
||||
@BeforeEach
|
||||
void setupAccountsDao() throws SQLException {
|
||||
this.pendingAccounts = new PendingAccounts(new FaultTolerantDatabase("pending_accounts-test", Jdbi.create(db.getTestDatabase()), new CircuitBreakerConfiguration()));
|
||||
|
||||
try (final Statement deleteStatement = db.getTestDatabase().getConnection().createStatement()) {
|
||||
deleteStatement.execute("DELETE FROM pending_accounts");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected VerificationCodeStore getVerificationCodeStore() {
|
||||
return pendingAccounts;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean expectNullPushCode() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean expectEmptyTwilioSid() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,153 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2021 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Optional;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.whispersystems.textsecuregcm.auth.StoredVerificationCode;
|
||||
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicVerificationCodeStoreMigrationConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicVerificationCodeStoreMigrationConfiguration.WriteDestination;
|
||||
|
||||
class PendingDevicesManagerTest {
|
||||
|
||||
private PendingDevices postgresPendingDevices;
|
||||
private VerificationCodeStoreDynamoDb dynamoDbPendingDevices;
|
||||
private DynamicVerificationCodeStoreMigrationConfiguration migrationConfiguration;
|
||||
|
||||
private PendingDevicesManager pendingDevicesManager;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
final DynamicConfigurationManager dynamicConfigurationManager = mock(DynamicConfigurationManager.class);
|
||||
final DynamicConfiguration dynamicConfiguration = mock(DynamicConfiguration.class);
|
||||
migrationConfiguration = mock(DynamicVerificationCodeStoreMigrationConfiguration.class);
|
||||
|
||||
when(dynamicConfigurationManager.getConfiguration()).thenReturn(dynamicConfiguration);
|
||||
when(dynamicConfiguration.getPendingDevicesMigrationConfiguration()).thenReturn(migrationConfiguration);
|
||||
|
||||
postgresPendingDevices = mock(PendingDevices.class);
|
||||
dynamoDbPendingDevices = mock(VerificationCodeStoreDynamoDb.class);
|
||||
|
||||
pendingDevicesManager = new PendingDevicesManager(postgresPendingDevices, dynamoDbPendingDevices, dynamicConfigurationManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
void storePostgres() {
|
||||
final String number = "+18005551234";
|
||||
final StoredVerificationCode code = mock(StoredVerificationCode.class);
|
||||
|
||||
when(migrationConfiguration.getWriteDestination()).thenReturn(WriteDestination.POSTGRES);
|
||||
|
||||
pendingDevicesManager.store(number, code);
|
||||
|
||||
verify(postgresPendingDevices).insert(number, code);
|
||||
verify(dynamoDbPendingDevices, never()).insert(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void storeDynamoDb() {
|
||||
final String number = "+18005551234";
|
||||
final StoredVerificationCode code = mock(StoredVerificationCode.class);
|
||||
|
||||
when(migrationConfiguration.getWriteDestination()).thenReturn(WriteDestination.DYNAMODB);
|
||||
|
||||
pendingDevicesManager.store(number, code);
|
||||
|
||||
verify(dynamoDbPendingDevices).insert(number, code);
|
||||
verify(postgresPendingDevices, never()).insert(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void remove() {
|
||||
final String number = "+18005551234";
|
||||
|
||||
pendingDevicesManager.remove(number);
|
||||
|
||||
verify(postgresPendingDevices).remove(number);
|
||||
verify(dynamoDbPendingDevices).remove(number);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCodeForNumber() {
|
||||
final String number = "+18005551234";
|
||||
|
||||
final StoredVerificationCode postgresCode = mock(StoredVerificationCode.class);
|
||||
final StoredVerificationCode dynamoDbCode = mock(StoredVerificationCode.class);
|
||||
|
||||
{
|
||||
when(migrationConfiguration.isReadPostgres()).thenReturn(false);
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(false);
|
||||
when(postgresPendingDevices.findForNumber(number)).thenReturn(Optional.empty());
|
||||
when(dynamoDbPendingDevices.findForNumber(number)).thenReturn(Optional.empty());
|
||||
|
||||
assertEquals(Optional.empty(), pendingDevicesManager.getCodeForNumber(number));
|
||||
|
||||
when(migrationConfiguration.isReadPostgres()).thenReturn(true);
|
||||
|
||||
assertEquals(Optional.empty(), pendingDevicesManager.getCodeForNumber(number));
|
||||
|
||||
when(dynamoDbPendingDevices.findForNumber(number)).thenReturn(Optional.of(dynamoDbCode));
|
||||
|
||||
assertEquals(Optional.empty(), pendingDevicesManager.getCodeForNumber(number));
|
||||
|
||||
when(postgresPendingDevices.findForNumber(number)).thenReturn(Optional.of(postgresCode));
|
||||
|
||||
assertEquals(Optional.of(postgresCode), pendingDevicesManager.getCodeForNumber(number));
|
||||
}
|
||||
|
||||
{
|
||||
when(migrationConfiguration.isReadPostgres()).thenReturn(false);
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(false);
|
||||
when(postgresPendingDevices.findForNumber(number)).thenReturn(Optional.empty());
|
||||
when(dynamoDbPendingDevices.findForNumber(number)).thenReturn(Optional.empty());
|
||||
|
||||
assertEquals(Optional.empty(), pendingDevicesManager.getCodeForNumber(number));
|
||||
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(true);
|
||||
|
||||
assertEquals(Optional.empty(), pendingDevicesManager.getCodeForNumber(number));
|
||||
|
||||
when(postgresPendingDevices.findForNumber(number)).thenReturn(Optional.of(postgresCode));
|
||||
|
||||
assertEquals(Optional.empty(), pendingDevicesManager.getCodeForNumber(number));
|
||||
|
||||
when(dynamoDbPendingDevices.findForNumber(number)).thenReturn(Optional.of(dynamoDbCode));
|
||||
|
||||
assertEquals(Optional.of(dynamoDbCode), pendingDevicesManager.getCodeForNumber(number));
|
||||
}
|
||||
|
||||
{
|
||||
when(migrationConfiguration.isReadPostgres()).thenReturn(false);
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(false);
|
||||
when(postgresPendingDevices.findForNumber(number)).thenReturn(Optional.of(postgresCode));
|
||||
when(dynamoDbPendingDevices.findForNumber(number)).thenReturn(Optional.of(dynamoDbCode));
|
||||
|
||||
assertEquals(Optional.empty(), pendingDevicesManager.getCodeForNumber(number));
|
||||
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(true);
|
||||
|
||||
assertEquals(Optional.of(dynamoDbCode), pendingDevicesManager.getCodeForNumber(number));
|
||||
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(false);
|
||||
when(migrationConfiguration.isReadPostgres()).thenReturn(true);
|
||||
|
||||
assertEquals(Optional.of(postgresCode), pendingDevicesManager.getCodeForNumber(number));
|
||||
|
||||
when(migrationConfiguration.isReadDynamoDb()).thenReturn(true);
|
||||
|
||||
assertEquals(Optional.of(postgresCode), pendingDevicesManager.getCodeForNumber(number));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2021 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
import com.opentable.db.postgres.embedded.LiquibasePreparer;
|
||||
import com.opentable.db.postgres.junit5.EmbeddedPostgresExtension;
|
||||
import com.opentable.db.postgres.junit5.PreparedDbExtension;
|
||||
import org.jdbi.v3.core.Jdbi;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.whispersystems.textsecuregcm.configuration.CircuitBreakerConfiguration;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class PendingDevicesTest extends VerificationCodeStoreTest {
|
||||
|
||||
@RegisterExtension
|
||||
public static PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forClasspathLocation("accountsdb.xml"));
|
||||
|
||||
private PendingDevices pendingDevices;
|
||||
|
||||
@BeforeEach
|
||||
public void setupAccountsDao() throws SQLException {
|
||||
this.pendingDevices = new PendingDevices(new FaultTolerantDatabase("peding_devices-test", Jdbi.create(db.getTestDatabase()), new CircuitBreakerConfiguration()));
|
||||
|
||||
try (final Statement deleteStatement = db.getTestDatabase().getConnection().createStatement()) {
|
||||
deleteStatement.execute("DELETE FROM pending_devices");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected VerificationCodeStore getVerificationCodeStore() {
|
||||
return pendingDevices;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean expectNullPushCode() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean expectEmptyTwilioSid() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2013-2021 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Optional;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.whispersystems.textsecuregcm.auth.StoredVerificationCode;
|
||||
|
||||
class StoredVerificationCodeManagerTest {
|
||||
|
||||
private VerificationCodeStore verificationCodeStore;
|
||||
|
||||
private StoredVerificationCodeManager storedVerificationCodeManager;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
verificationCodeStore = mock(VerificationCodeStore.class);
|
||||
|
||||
storedVerificationCodeManager = new StoredVerificationCodeManager(verificationCodeStore);
|
||||
}
|
||||
|
||||
@Test
|
||||
void store() {
|
||||
final String number = "+18005551234";
|
||||
final StoredVerificationCode code = mock(StoredVerificationCode.class);
|
||||
|
||||
storedVerificationCodeManager.store(number, code);
|
||||
|
||||
verify(verificationCodeStore).insert(number, code);
|
||||
}
|
||||
|
||||
@Test
|
||||
void remove() {
|
||||
final String number = "+18005551234";
|
||||
|
||||
storedVerificationCodeManager.remove(number);
|
||||
|
||||
verify(verificationCodeStore).remove(number);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCodeForNumber() {
|
||||
final String number = "+18005551234";
|
||||
|
||||
when(verificationCodeStore.findForNumber(number)).thenReturn(Optional.empty());
|
||||
assertEquals(Optional.empty(), storedVerificationCodeManager.getCodeForNumber(number));
|
||||
|
||||
final StoredVerificationCode storedVerificationCode = mock(StoredVerificationCode.class);
|
||||
|
||||
when(verificationCodeStore.findForNumber(number)).thenReturn(Optional.of(storedVerificationCode));
|
||||
assertEquals(Optional.of(storedVerificationCode), storedVerificationCodeManager.getCodeForNumber(number));
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2021 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
|
||||
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;
|
||||
|
||||
class VerificationCodeStoreDynamoDbTest extends VerificationCodeStoreTest {
|
||||
|
||||
private VerificationCodeStoreDynamoDb verificationCodeStore;
|
||||
|
||||
private static final String TABLE_NAME = "verification_code_test";
|
||||
|
||||
@RegisterExtension
|
||||
static final DynamoDbExtension DYNAMO_DB_EXTENSION = DynamoDbExtension.builder()
|
||||
.tableName(TABLE_NAME)
|
||||
.hashKey(VerificationCodeStoreDynamoDb.KEY_E164)
|
||||
.attributeDefinition(AttributeDefinition.builder()
|
||||
.attributeName(VerificationCodeStoreDynamoDb.KEY_E164)
|
||||
.attributeType(ScalarAttributeType.S)
|
||||
.build())
|
||||
.build();
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
verificationCodeStore = new VerificationCodeStoreDynamoDb(DYNAMO_DB_EXTENSION.getDynamoDbClient(), TABLE_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected VerificationCodeStore getVerificationCodeStore() {
|
||||
return verificationCodeStore;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean expectNullPushCode() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean expectEmptyTwilioSid() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -5,76 +5,87 @@
|
||||
|
||||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.whispersystems.textsecuregcm.auth.StoredVerificationCode;
|
||||
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
|
||||
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
abstract class VerificationCodeStoreTest {
|
||||
class VerificationCodeStoreTest {
|
||||
|
||||
private VerificationCodeStore verificationCodeStore;
|
||||
|
||||
private static final String TABLE_NAME = "verification_code_test";
|
||||
|
||||
private static final String PHONE_NUMBER = "+14151112222";
|
||||
|
||||
protected abstract VerificationCodeStore getVerificationCodeStore();
|
||||
@RegisterExtension
|
||||
static final DynamoDbExtension DYNAMO_DB_EXTENSION = DynamoDbExtension.builder()
|
||||
.tableName(TABLE_NAME)
|
||||
.hashKey(VerificationCodeStore.KEY_E164)
|
||||
.attributeDefinition(AttributeDefinition.builder()
|
||||
.attributeName(VerificationCodeStore.KEY_E164)
|
||||
.attributeType(ScalarAttributeType.S)
|
||||
.build())
|
||||
.build();
|
||||
|
||||
protected abstract boolean expectNullPushCode();
|
||||
|
||||
protected abstract boolean expectEmptyTwilioSid();
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
verificationCodeStore = new VerificationCodeStore(DYNAMO_DB_EXTENSION.getDynamoDbClient(), TABLE_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStoreAndFind() {
|
||||
assertEquals(Optional.empty(), getVerificationCodeStore().findForNumber(PHONE_NUMBER));
|
||||
assertEquals(Optional.empty(), verificationCodeStore.findForNumber(PHONE_NUMBER));
|
||||
|
||||
final StoredVerificationCode originalCode = new StoredVerificationCode("1234", 1111, "abcd", "0987");
|
||||
final StoredVerificationCode secondCode = new StoredVerificationCode("5678", 2222, "efgh", "7890");
|
||||
|
||||
getVerificationCodeStore().insert(PHONE_NUMBER, originalCode);
|
||||
|
||||
verificationCodeStore.insert(PHONE_NUMBER, originalCode);
|
||||
{
|
||||
final Optional<StoredVerificationCode> maybeRetrievedCode = getVerificationCodeStore().findForNumber(PHONE_NUMBER);
|
||||
final Optional<StoredVerificationCode> maybeCode = verificationCodeStore.findForNumber(PHONE_NUMBER);
|
||||
|
||||
assertTrue(maybeRetrievedCode.isPresent());
|
||||
compareStoredVerificationCode(originalCode, maybeRetrievedCode.get());
|
||||
assertTrue(maybeCode.isPresent());
|
||||
assertTrue(storedVerificationCodesAreEqual(originalCode, maybeCode.get()));
|
||||
}
|
||||
|
||||
getVerificationCodeStore().insert(PHONE_NUMBER, secondCode);
|
||||
|
||||
verificationCodeStore.insert(PHONE_NUMBER, secondCode);
|
||||
{
|
||||
final Optional<StoredVerificationCode> maybeRetrievedCode = getVerificationCodeStore().findForNumber(PHONE_NUMBER);
|
||||
final Optional<StoredVerificationCode> maybeCode = verificationCodeStore.findForNumber(PHONE_NUMBER);
|
||||
|
||||
assertTrue(maybeRetrievedCode.isPresent());
|
||||
compareStoredVerificationCode(secondCode, maybeRetrievedCode.get());
|
||||
assertTrue(maybeCode.isPresent());
|
||||
assertTrue(storedVerificationCodesAreEqual(secondCode, maybeCode.get()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemove() {
|
||||
assertEquals(Optional.empty(), getVerificationCodeStore().findForNumber(PHONE_NUMBER));
|
||||
assertEquals(Optional.empty(), verificationCodeStore.findForNumber(PHONE_NUMBER));
|
||||
|
||||
getVerificationCodeStore().insert(PHONE_NUMBER, new StoredVerificationCode("1234", 1111, "abcd", "0987"));
|
||||
assertTrue(getVerificationCodeStore().findForNumber(PHONE_NUMBER).isPresent());
|
||||
verificationCodeStore.insert(PHONE_NUMBER, new StoredVerificationCode("1234", 1111, "abcd", "0987"));
|
||||
assertTrue(verificationCodeStore.findForNumber(PHONE_NUMBER).isPresent());
|
||||
|
||||
getVerificationCodeStore().remove(PHONE_NUMBER);
|
||||
assertFalse(getVerificationCodeStore().findForNumber(PHONE_NUMBER).isPresent());
|
||||
verificationCodeStore.remove(PHONE_NUMBER);
|
||||
assertFalse(verificationCodeStore.findForNumber(PHONE_NUMBER).isPresent());
|
||||
}
|
||||
|
||||
private void compareStoredVerificationCode(final StoredVerificationCode original, final StoredVerificationCode retrieved) {
|
||||
assertEquals(original.getCode(), retrieved.getCode());
|
||||
assertEquals(original.getTimestamp(), retrieved.getTimestamp());
|
||||
|
||||
if (expectNullPushCode()) {
|
||||
assertNull(retrieved.getPushCode());
|
||||
} else {
|
||||
assertEquals(original.getPushCode(), retrieved.getPushCode());
|
||||
private static boolean storedVerificationCodesAreEqual(final StoredVerificationCode first, final StoredVerificationCode second) {
|
||||
if (first == null && second == null) {
|
||||
return true;
|
||||
} else if (first == null || second == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (expectEmptyTwilioSid()) {
|
||||
assertEquals(Optional.empty(), retrieved.getTwilioVerificationSid());
|
||||
} else {
|
||||
assertEquals(original.getTwilioVerificationSid(), retrieved.getTwilioVerificationSid());
|
||||
}
|
||||
return Objects.equals(first.getCode(), second.getCode()) &&
|
||||
first.getTimestamp() == second.getTimestamp() &&
|
||||
Objects.equals(first.getPushCode(), second.getPushCode()) &&
|
||||
Objects.equals(first.getTwilioVerificationSid(), second.getTwilioVerificationSid());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||
import org.whispersystems.textsecuregcm.storage.DynamicConfigurationManager;
|
||||
import org.whispersystems.textsecuregcm.storage.MessagesManager;
|
||||
import org.whispersystems.textsecuregcm.storage.PendingAccountsManager;
|
||||
import org.whispersystems.textsecuregcm.storage.StoredVerificationCodeManager;
|
||||
import org.whispersystems.textsecuregcm.storage.UsernamesManager;
|
||||
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||
import org.whispersystems.textsecuregcm.util.Hex;
|
||||
@@ -107,7 +107,7 @@ class AccountControllerTest {
|
||||
private static final String VALID_CAPTCHA_TOKEN = "valid_token";
|
||||
private static final String INVALID_CAPTCHA_TOKEN = "invalid_token";
|
||||
|
||||
private static PendingAccountsManager pendingAccountsManager = mock(PendingAccountsManager.class);
|
||||
private static StoredVerificationCodeManager pendingAccountsManager = mock(StoredVerificationCodeManager.class);
|
||||
private static AccountsManager accountsManager = mock(AccountsManager.class);
|
||||
private static AbusiveHostRules abusiveHostRules = mock(AbusiveHostRules.class);
|
||||
private static RateLimiters rateLimiters = mock(RateLimiters.class);
|
||||
|
||||
@@ -46,7 +46,7 @@ import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||
import org.whispersystems.textsecuregcm.storage.Device;
|
||||
import org.whispersystems.textsecuregcm.storage.Device.DeviceCapabilities;
|
||||
import org.whispersystems.textsecuregcm.storage.MessagesManager;
|
||||
import org.whispersystems.textsecuregcm.storage.PendingDevicesManager;
|
||||
import org.whispersystems.textsecuregcm.storage.StoredVerificationCodeManager;
|
||||
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||
import org.whispersystems.textsecuregcm.util.VerificationCode;
|
||||
|
||||
@@ -54,7 +54,7 @@ import org.whispersystems.textsecuregcm.util.VerificationCode;
|
||||
public class DeviceControllerTest {
|
||||
@Path("/v1/devices")
|
||||
static class DumbVerificationDeviceController extends DeviceController {
|
||||
public DumbVerificationDeviceController(PendingDevicesManager pendingDevices,
|
||||
public DumbVerificationDeviceController(StoredVerificationCodeManager pendingDevices,
|
||||
AccountsManager accounts,
|
||||
MessagesManager messages,
|
||||
DirectoryQueue cdsSender,
|
||||
@@ -70,7 +70,7 @@ public class DeviceControllerTest {
|
||||
}
|
||||
}
|
||||
|
||||
private PendingDevicesManager pendingDevicesManager = mock(PendingDevicesManager.class);
|
||||
private StoredVerificationCodeManager pendingDevicesManager = mock(StoredVerificationCodeManager.class);
|
||||
private AccountsManager accountsManager = mock(AccountsManager.class );
|
||||
private MessagesManager messagesManager = mock(MessagesManager.class);
|
||||
private DirectoryQueue directoryQueue = mock(DirectoryQueue.class);
|
||||
|
||||
Reference in New Issue
Block a user