Extract a common base class for verification code store tests.
This commit is contained in:
parent
fc421d3f21
commit
ce3fb7fa99
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
* 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,80 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2013-2021 Signal Messenger, LLC
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.whispersystems.textsecuregcm.storage;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.whispersystems.textsecuregcm.auth.StoredVerificationCode;
|
||||||
|
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 {
|
||||||
|
|
||||||
|
private static final String PHONE_NUMBER = "+14151112222";
|
||||||
|
|
||||||
|
protected abstract VerificationCodeStore getVerificationCodeStore();
|
||||||
|
|
||||||
|
protected abstract boolean expectNullPushCode();
|
||||||
|
|
||||||
|
protected abstract boolean expectEmptyTwilioSid();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testStoreAndFind() {
|
||||||
|
assertEquals(Optional.empty(), getVerificationCodeStore().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);
|
||||||
|
|
||||||
|
{
|
||||||
|
final Optional<StoredVerificationCode> maybeRetrievedCode = getVerificationCodeStore().findForNumber(PHONE_NUMBER);
|
||||||
|
|
||||||
|
assertTrue(maybeRetrievedCode.isPresent());
|
||||||
|
compareStoredVerificationCode(originalCode, maybeRetrievedCode.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
getVerificationCodeStore().insert(PHONE_NUMBER, secondCode);
|
||||||
|
|
||||||
|
{
|
||||||
|
final Optional<StoredVerificationCode> maybeRetrievedCode = getVerificationCodeStore().findForNumber(PHONE_NUMBER);
|
||||||
|
|
||||||
|
assertTrue(maybeRetrievedCode.isPresent());
|
||||||
|
compareStoredVerificationCode(secondCode, maybeRetrievedCode.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRemove() {
|
||||||
|
assertEquals(Optional.empty(), getVerificationCodeStore().findForNumber(PHONE_NUMBER));
|
||||||
|
|
||||||
|
getVerificationCodeStore().insert(PHONE_NUMBER, new StoredVerificationCode("1234", 1111, "abcd", "0987"));
|
||||||
|
assertTrue(getVerificationCodeStore().findForNumber(PHONE_NUMBER).isPresent());
|
||||||
|
|
||||||
|
getVerificationCodeStore().remove(PHONE_NUMBER);
|
||||||
|
assertFalse(getVerificationCodeStore().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());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (expectEmptyTwilioSid()) {
|
||||||
|
assertEquals(Optional.empty(), retrieved.getTwilioVerificationSid());
|
||||||
|
} else {
|
||||||
|
assertEquals(original.getTwilioVerificationSid(), retrieved.getTwilioVerificationSid());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,222 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2013-2020 Signal Messenger, LLC
|
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.whispersystems.textsecuregcm.tests.storage;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
|
||||||
|
|
||||||
import com.opentable.db.postgres.embedded.LiquibasePreparer;
|
|
||||||
import com.opentable.db.postgres.junit5.EmbeddedPostgresExtension;
|
|
||||||
import com.opentable.db.postgres.junit5.PreparedDbExtension;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.Optional;
|
|
||||||
import org.jdbi.v3.core.Jdbi;
|
|
||||||
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 org.whispersystems.textsecuregcm.configuration.CircuitBreakerConfiguration;
|
|
||||||
import org.whispersystems.textsecuregcm.storage.FaultTolerantDatabase;
|
|
||||||
import org.whispersystems.textsecuregcm.storage.PendingAccounts;
|
|
||||||
|
|
||||||
class PendingAccountsTest {
|
|
||||||
|
|
||||||
@RegisterExtension
|
|
||||||
static PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forClasspathLocation("accountsdb.xml"));
|
|
||||||
|
|
||||||
private PendingAccounts pendingAccounts;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void setupAccountsDao() {
|
|
||||||
this.pendingAccounts = new PendingAccounts(new FaultTolerantDatabase("pending_accounts-test", Jdbi.create(db.getTestDatabase()), new CircuitBreakerConfiguration()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testStore() throws SQLException {
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode("1234", 1111, null, null));
|
|
||||||
|
|
||||||
PreparedStatement statement = db.getTestDatabase().getConnection().prepareStatement("SELECT * FROM pending_accounts WHERE number = ?");
|
|
||||||
statement.setString(1, "+14151112222");
|
|
||||||
|
|
||||||
ResultSet resultSet = statement.executeQuery();
|
|
||||||
|
|
||||||
if (resultSet.next()) {
|
|
||||||
assertThat(resultSet.getString("verification_code")).isEqualTo("1234");
|
|
||||||
assertThat(resultSet.getLong("timestamp")).isEqualTo(1111);
|
|
||||||
assertThat(resultSet.getString("push_code")).isNull();
|
|
||||||
} else {
|
|
||||||
throw new AssertionError("no results");
|
|
||||||
}
|
|
||||||
|
|
||||||
assertThat(resultSet.next()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testStoreWithPushChallenge() throws SQLException {
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode(null, 1111, "112233", null));
|
|
||||||
|
|
||||||
PreparedStatement statement = db.getTestDatabase().getConnection().prepareStatement("SELECT * FROM pending_accounts WHERE number = ?");
|
|
||||||
statement.setString(1, "+14151112222");
|
|
||||||
|
|
||||||
ResultSet resultSet = statement.executeQuery();
|
|
||||||
|
|
||||||
if (resultSet.next()) {
|
|
||||||
assertThat(resultSet.getString("verification_code")).isNull();
|
|
||||||
assertThat(resultSet.getLong("timestamp")).isEqualTo(1111);
|
|
||||||
assertThat(resultSet.getString("push_code")).isEqualTo("112233");
|
|
||||||
} else {
|
|
||||||
throw new AssertionError("no results");
|
|
||||||
}
|
|
||||||
|
|
||||||
assertThat(resultSet.next()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testStoreWithTwilioVerificationSid() throws SQLException {
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode(null, 1111, null, "id1"));
|
|
||||||
|
|
||||||
PreparedStatement statement = db.getTestDatabase().getConnection()
|
|
||||||
.prepareStatement("SELECT * FROM pending_accounts WHERE number = ?");
|
|
||||||
statement.setString(1, "+14151112222");
|
|
||||||
|
|
||||||
ResultSet resultSet = statement.executeQuery();
|
|
||||||
|
|
||||||
if (resultSet.next()) {
|
|
||||||
assertThat(resultSet.getString("verification_code")).isNull();
|
|
||||||
assertThat(resultSet.getLong("timestamp")).isEqualTo(1111);
|
|
||||||
assertThat(resultSet.getString("push_code")).isNull();
|
|
||||||
assertThat(resultSet.getString("twilio_verification_sid")).isEqualTo("id1");
|
|
||||||
} else {
|
|
||||||
throw new AssertionError("no results");
|
|
||||||
}
|
|
||||||
|
|
||||||
assertThat(resultSet.next()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testRetrieve() throws Exception {
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode("4321", 2222, null, null));
|
|
||||||
pendingAccounts.insert("+14151113333", new StoredVerificationCode("1212", 5555, null, null));
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> verificationCode = pendingAccounts.findForNumber("+14151112222");
|
|
||||||
|
|
||||||
assertThat(verificationCode.isPresent()).isTrue();
|
|
||||||
assertThat(verificationCode.get().getCode()).isEqualTo("4321");
|
|
||||||
assertThat(verificationCode.get().getTimestamp()).isEqualTo(2222);
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> missingCode = pendingAccounts.findForNumber("+11111111111");
|
|
||||||
assertThat(missingCode.isPresent()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testRetrieveWithPushChallenge() throws Exception {
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode("4321", 2222, "bar", null));
|
|
||||||
pendingAccounts.insert("+14151113333", new StoredVerificationCode("1212", 5555, "bang", null));
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> verificationCode = pendingAccounts.findForNumber("+14151112222");
|
|
||||||
|
|
||||||
assertThat(verificationCode.isPresent()).isTrue();
|
|
||||||
assertThat(verificationCode.get().getCode()).isEqualTo("4321");
|
|
||||||
assertThat(verificationCode.get().getTimestamp()).isEqualTo(2222);
|
|
||||||
assertThat(verificationCode.get().getPushCode()).isEqualTo("bar");
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> missingCode = pendingAccounts.findForNumber("+11111111111");
|
|
||||||
assertThat(missingCode.isPresent()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testRetrieveWithTwilioVerificationSid() throws Exception {
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode("4321", 2222, "bar", "id1"));
|
|
||||||
pendingAccounts.insert("+14151113333", new StoredVerificationCode("1212", 5555, "bang", "id2"));
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> verificationCode = pendingAccounts.findForNumber("+14151112222");
|
|
||||||
|
|
||||||
assertThat(verificationCode).isPresent();
|
|
||||||
assertThat(verificationCode.get().getCode()).isEqualTo("4321");
|
|
||||||
assertThat(verificationCode.get().getTimestamp()).isEqualTo(2222);
|
|
||||||
assertThat(verificationCode.get().getPushCode()).isEqualTo("bar");
|
|
||||||
assertThat(verificationCode.get().getTwilioVerificationSid()).contains("id1");
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> missingCode = pendingAccounts.findForNumber("+11111111111");
|
|
||||||
assertThat(missingCode).isNotPresent();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testOverwrite() throws Exception {
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode("4321", 2222, null, null));
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode("4444", 3333, null, null));
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> verificationCode = pendingAccounts.findForNumber("+14151112222");
|
|
||||||
|
|
||||||
assertThat(verificationCode.isPresent()).isTrue();
|
|
||||||
assertThat(verificationCode.get().getCode()).isEqualTo("4444");
|
|
||||||
assertThat(verificationCode.get().getTimestamp()).isEqualTo(3333);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testOverwriteWithPushToken() throws Exception {
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode("4321", 2222, "bar", null));
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode("4444", 3333, "bang", null));
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> verificationCode = pendingAccounts.findForNumber("+14151112222");
|
|
||||||
|
|
||||||
assertThat(verificationCode.isPresent()).isTrue();
|
|
||||||
assertThat(verificationCode.get().getCode()).isEqualTo("4444");
|
|
||||||
assertThat(verificationCode.get().getTimestamp()).isEqualTo(3333);
|
|
||||||
assertThat(verificationCode.get().getPushCode()).isEqualTo("bang");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testOverwriteWithTwilioVerificationSid() throws Exception {
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode("4321", 2222, "bar", "id1"));
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode("4444", 3333, "bang", "id2"));
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> verificationCode = pendingAccounts.findForNumber("+14151112222");
|
|
||||||
|
|
||||||
assertThat(verificationCode.isPresent()).isTrue();
|
|
||||||
assertThat(verificationCode.get().getCode()).isEqualTo("4444");
|
|
||||||
assertThat(verificationCode.get().getTimestamp()).isEqualTo(3333);
|
|
||||||
assertThat(verificationCode.get().getPushCode()).isEqualTo("bang");
|
|
||||||
assertThat(verificationCode.get().getTwilioVerificationSid()).contains("id2");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testVacuum() {
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode("4321", 2222, null, null));
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode("4444", 3333, null, null));
|
|
||||||
pendingAccounts.vacuum();
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> verificationCode = pendingAccounts.findForNumber("+14151112222");
|
|
||||||
|
|
||||||
assertThat(verificationCode.isPresent()).isTrue();
|
|
||||||
assertThat(verificationCode.get().getCode()).isEqualTo("4444");
|
|
||||||
assertThat(verificationCode.get().getTimestamp()).isEqualTo(3333);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testRemove() {
|
|
||||||
pendingAccounts.insert("+14151112222", new StoredVerificationCode("4321", 2222, "bar", null));
|
|
||||||
pendingAccounts.insert("+14151113333", new StoredVerificationCode("1212", 5555, null, null));
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> verificationCode = pendingAccounts.findForNumber("+14151112222");
|
|
||||||
|
|
||||||
assertThat(verificationCode.isPresent()).isTrue();
|
|
||||||
assertThat(verificationCode.get().getCode()).isEqualTo("4321");
|
|
||||||
assertThat(verificationCode.get().getTimestamp()).isEqualTo(2222);
|
|
||||||
|
|
||||||
pendingAccounts.remove("+14151112222");
|
|
||||||
|
|
||||||
verificationCode = pendingAccounts.findForNumber("+14151112222");
|
|
||||||
assertThat(verificationCode.isPresent()).isFalse();
|
|
||||||
|
|
||||||
verificationCode = pendingAccounts.findForNumber("+14151113333");
|
|
||||||
assertThat(verificationCode.isPresent()).isTrue();
|
|
||||||
assertThat(verificationCode.get().getCode()).isEqualTo("1212");
|
|
||||||
assertThat(verificationCode.get().getTimestamp()).isEqualTo(5555);
|
|
||||||
assertThat(verificationCode.get().getPushCode()).isNull();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,107 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2013-2020 Signal Messenger, LLC
|
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.whispersystems.textsecuregcm.tests.storage;
|
|
||||||
|
|
||||||
import com.opentable.db.postgres.embedded.LiquibasePreparer;
|
|
||||||
import com.opentable.db.postgres.junit.EmbeddedPostgresRules;
|
|
||||||
import com.opentable.db.postgres.junit.PreparedDbRule;
|
|
||||||
import org.jdbi.v3.core.Jdbi;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.whispersystems.textsecuregcm.auth.StoredVerificationCode;
|
|
||||||
import org.whispersystems.textsecuregcm.configuration.CircuitBreakerConfiguration;
|
|
||||||
import org.whispersystems.textsecuregcm.storage.FaultTolerantDatabase;
|
|
||||||
import org.whispersystems.textsecuregcm.storage.PendingDevices;
|
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
|
||||||
|
|
||||||
public class PendingDevicesTest {
|
|
||||||
|
|
||||||
@Rule
|
|
||||||
public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forClasspathLocation("accountsdb.xml"));
|
|
||||||
|
|
||||||
private PendingDevices pendingDevices;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setupAccountsDao() {
|
|
||||||
this.pendingDevices = new PendingDevices(new FaultTolerantDatabase("peding_devices-test", Jdbi.create(db.getTestDatabase()), new CircuitBreakerConfiguration()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testStore() throws SQLException {
|
|
||||||
pendingDevices.insert("+14151112222", new StoredVerificationCode("1234", 1111, null, null));
|
|
||||||
|
|
||||||
PreparedStatement statement = db.getTestDatabase().getConnection().prepareStatement("SELECT * FROM pending_devices WHERE number = ?");
|
|
||||||
statement.setString(1, "+14151112222");
|
|
||||||
|
|
||||||
ResultSet resultSet = statement.executeQuery();
|
|
||||||
|
|
||||||
if (resultSet.next()) {
|
|
||||||
assertThat(resultSet.getString("verification_code")).isEqualTo("1234");
|
|
||||||
assertThat(resultSet.getLong("timestamp")).isEqualTo(1111);
|
|
||||||
} else {
|
|
||||||
throw new AssertionError("no results");
|
|
||||||
}
|
|
||||||
|
|
||||||
assertThat(resultSet.next()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testRetrieve() throws Exception {
|
|
||||||
pendingDevices.insert("+14151112222", new StoredVerificationCode("4321", 2222, null, null));
|
|
||||||
pendingDevices.insert("+14151113333", new StoredVerificationCode("1212", 5555, null, null));
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> verificationCode = pendingDevices.findForNumber("+14151112222");
|
|
||||||
|
|
||||||
assertThat(verificationCode.isPresent()).isTrue();
|
|
||||||
assertThat(verificationCode.get().getCode()).isEqualTo("4321");
|
|
||||||
assertThat(verificationCode.get().getTimestamp()).isEqualTo(2222);
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> missingCode = pendingDevices.findForNumber("+11111111111");
|
|
||||||
assertThat(missingCode.isPresent()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOverwrite() throws Exception {
|
|
||||||
pendingDevices.insert("+14151112222", new StoredVerificationCode("4321", 2222, null, null));
|
|
||||||
pendingDevices.insert("+14151112222", new StoredVerificationCode("4444", 3333, null, null));
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> verificationCode = pendingDevices.findForNumber("+14151112222");
|
|
||||||
|
|
||||||
assertThat(verificationCode.isPresent()).isTrue();
|
|
||||||
assertThat(verificationCode.get().getCode()).isEqualTo("4444");
|
|
||||||
assertThat(verificationCode.get().getTimestamp()).isEqualTo(3333);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testRemove() {
|
|
||||||
pendingDevices.insert("+14151112222", new StoredVerificationCode("4321", 2222, null, null));
|
|
||||||
pendingDevices.insert("+14151113333", new StoredVerificationCode("1212", 5555, null, null));
|
|
||||||
|
|
||||||
Optional<StoredVerificationCode> verificationCode = pendingDevices.findForNumber("+14151112222");
|
|
||||||
|
|
||||||
assertThat(verificationCode.isPresent()).isTrue();
|
|
||||||
assertThat(verificationCode.get().getCode()).isEqualTo("4321");
|
|
||||||
assertThat(verificationCode.get().getTimestamp()).isEqualTo(2222);
|
|
||||||
|
|
||||||
pendingDevices.remove("+14151112222");
|
|
||||||
|
|
||||||
verificationCode = pendingDevices.findForNumber("+14151112222");
|
|
||||||
assertThat(verificationCode.isPresent()).isFalse();
|
|
||||||
|
|
||||||
verificationCode = pendingDevices.findForNumber("+14151113333");
|
|
||||||
assertThat(verificationCode.isPresent()).isTrue();
|
|
||||||
assertThat(verificationCode.get().getCode()).isEqualTo("1212");
|
|
||||||
assertThat(verificationCode.get().getTimestamp()).isEqualTo(5555);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue