Skip to content

Commit

Permalink
PP-11205 Refactor ForgottenPasswordDbFixture (#2195)
Browse files Browse the repository at this point in the history
- Refactored `ForgottenPasswordDbFixture` so it doesn't rely on the `ForgottenPassword` model used by services and resource classes. Using the `ForgottenPassword` model couples tests and main code tightly and any changes for testing impact API.
  • Loading branch information
kbottla authored Oct 26, 2023
1 parent 3dd720f commit 2f4f1c2
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 51 deletions.
6 changes: 3 additions & 3 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
"filename": "src/test/java/uk/gov/pay/adminusers/pact/ContractTest.java",
"hashed_secret": "614770647df3ab100b871bfc0d20e72c8625a5c4",
"is_verified": false,
"line_number": 73
"line_number": 78
}
],
"src/test/java/uk/gov/pay/adminusers/persistence/dao/GovUkPayAgreementDaoIT.java": [
Expand Down Expand Up @@ -380,7 +380,7 @@
"filename": "src/test/java/uk/gov/pay/adminusers/resources/ResetPasswordResourceIT.java",
"hashed_secret": "ff667b1e79f00abb68c320a096f851ce96b5de4d",
"is_verified": false,
"line_number": 22
"line_number": 24
}
],
"src/test/java/uk/gov/pay/adminusers/resources/ServiceResourceGovUkPayAgreementResourceIT.java": [
Expand Down Expand Up @@ -466,5 +466,5 @@
}
]
},
"generated_at": "2023-10-26T09:06:36Z"
"generated_at": "2023-10-26T11:32:22Z"
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,64 @@
package uk.gov.pay.adminusers.fixtures;

import org.apache.commons.lang3.RandomStringUtils;
import uk.gov.pay.adminusers.app.util.RandomIdGenerator;
import uk.gov.pay.adminusers.utils.DatabaseTestHelper;

import java.time.ZoneId;
import java.time.ZonedDateTime;

import static java.time.temporal.ChronoUnit.MINUTES;
import static java.time.ZonedDateTime.now;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
import static org.apache.commons.lang3.RandomUtils.nextInt;
import static uk.gov.pay.adminusers.model.ForgottenPassword.forgottenPassword;

public class ForgottenPasswordDbFixture {

private DatabaseTestHelper databaseTestHelper;
private int userId;
private ZonedDateTime date = ZonedDateTime.now(ZoneId.of("UTC"));
private String forgottenPasswordCode = RandomStringUtils.randomAlphanumeric(100);
private ZonedDateTime date = now(ZoneId.of("UTC"));
private String forgottenPasswordCode = randomAlphanumeric(100);
private ZonedDateTime createdAt = now(ZoneId.of("UTC"));

private ForgottenPasswordDbFixture(DatabaseTestHelper databaseTestHelper, int userId) {
this.databaseTestHelper = databaseTestHelper;
private final Integer id = nextInt();

public static ForgottenPasswordDbFixture aForgottenPasswordDbFixture() {
return new ForgottenPasswordDbFixture();
}

public ForgottenPasswordDbFixture insert() {
databaseTestHelper.insertForgottenPassword(id, date, forgottenPasswordCode, userId, createdAt);
return this;
}

public ForgottenPasswordDbFixture withExpiryDate(ZonedDateTime expiryDate) {
this.date = expiryDate;
return this;
}

public ForgottenPasswordDbFixture withCreatedAt(ZonedDateTime createdAt) {
this.createdAt = createdAt;
return this;
}

public ForgottenPasswordDbFixture withUserId(int userId) {
this.userId = userId;
return this;
}

public static ForgottenPasswordDbFixture forgottenPasswordDbFixture(DatabaseTestHelper databaseHelper, int userId) {
return new ForgottenPasswordDbFixture(databaseHelper, userId);
public ForgottenPasswordDbFixture withCode(String forgottenPasswordCode) {
this.forgottenPasswordCode = forgottenPasswordCode;
return this;
}

public String insertForgottenPassword() {
databaseTestHelper.add(forgottenPassword(nextInt(), forgottenPasswordCode, date, RandomIdGenerator.randomUuid()), userId);
public ForgottenPasswordDbFixture withDatabaseTestHelper(DatabaseTestHelper databaseHelper) {
this.databaseTestHelper = databaseHelper;
return this;
}

public String getForgottenPasswordCode() {
return forgottenPasswordCode;
}

public ForgottenPasswordDbFixture expired() {
date = ZonedDateTime.now(ZoneId.of("UTC")).minus(91, MINUTES);
return this;
public ZonedDateTime getDate() {
return date;
}

}
16 changes: 13 additions & 3 deletions src/test/java/uk/gov/pay/adminusers/pact/ContractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.junit.BeforeClass;
import org.junit.ClassRule;
import uk.gov.pay.adminusers.infra.AppWithPostgresAndSqsRule;
import uk.gov.pay.adminusers.model.ForgottenPassword;
import uk.gov.pay.adminusers.model.GoLiveStage;
import uk.gov.pay.adminusers.model.Permission;
import uk.gov.pay.adminusers.model.Role;
Expand All @@ -22,6 +21,7 @@
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
import static uk.gov.pay.adminusers.app.util.RandomIdGenerator.randomInt;
import static uk.gov.pay.adminusers.app.util.RandomIdGenerator.randomUuid;
import static uk.gov.pay.adminusers.fixtures.ForgottenPasswordDbFixture.aForgottenPasswordDbFixture;
import static uk.gov.pay.adminusers.fixtures.InviteDbFixture.inviteDbFixture;
import static uk.gov.pay.adminusers.fixtures.RoleDbFixture.roleDbFixture;
import static uk.gov.pay.adminusers.fixtures.ServiceDbFixture.serviceDbFixture;
Expand Down Expand Up @@ -57,7 +57,12 @@ public void aUserExistsWithAForgottenPasswordRequest() {
String userExternalId = randomUuid();
createUserWithinAService(userExternalId, randomUuid() + "@example.com", "password", "cp5wa");
List<Map<String, Object>> userByExternalId = dbHelper.findUserByExternalId(userExternalId);
dbHelper.add(ForgottenPassword.forgottenPassword(code, userExternalId), (Integer) userByExternalId.get(0).get("id"));

aForgottenPasswordDbFixture()
.withDatabaseTestHelper(dbHelper)
.withUserId((Integer) userByExternalId.get(0).get("id"))
.withCode(code)
.insert();
}

@State("a user exists with max login attempts")
Expand All @@ -73,7 +78,12 @@ public void aForgottenPasswordEntryExist() {
String existingUserExternalId = "7d19aff33f8948deb97ed16b2912dcd3";
createUserWithinAService(existingUserExternalId, "[email protected]", "password", "cp5wa");
List<Map<String, Object>> userByName = dbHelper.findUserByExternalId(existingUserExternalId);
dbHelper.add(ForgottenPassword.forgottenPassword(code, existingUserExternalId), (Integer) userByName.get(0).get("id"));

aForgottenPasswordDbFixture()
.withDatabaseTestHelper(dbHelper)
.withUserId((Integer) userByName.get(0).get("id"))
.withCode(code)
.insert();
}

@State("a user and user admin exists in service with the given ids before a delete operation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
import java.util.Optional;

import static java.time.temporal.ChronoUnit.MINUTES;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import static org.apache.commons.lang3.RandomStringUtils.random;
import static org.exparity.hamcrest.date.ZonedDateTimeMatchers.within;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static uk.gov.pay.adminusers.app.util.RandomIdGenerator.randomInt;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static uk.gov.pay.adminusers.app.util.RandomIdGenerator.randomUuid;
import static uk.gov.pay.adminusers.fixtures.ForgottenPasswordDbFixture.aForgottenPasswordDbFixture;
import static uk.gov.pay.adminusers.fixtures.UserDbFixture.userDbFixture;
import static uk.gov.pay.adminusers.model.ForgottenPassword.forgottenPassword;

Expand Down Expand Up @@ -73,16 +73,20 @@ public void shouldFindForgottenPasswordByCode_ifNotExpired() {
UserEntity userEntity = userDao.findByExternalId(userExternalId).get();

ZonedDateTime notExpired = ZonedDateTime.now().minusMinutes(89);
ForgottenPassword forgottenPassword = forgottenPassword(randomInt(), forgottenPasswordCode, notExpired, userExternalId);

databaseHelper.add(forgottenPassword, userEntity.getId());
aForgottenPasswordDbFixture()
.withDatabaseTestHelper(databaseHelper)
.withUserId(userEntity.getId())
.withExpiryDate(notExpired)
.withCode(forgottenPasswordCode)
.insert();

Optional<ForgottenPasswordEntity> forgottenPasswordEntityOptional = forgottenPasswordDao.findNonExpiredByCode(forgottenPassword.getCode());
Optional<ForgottenPasswordEntity> forgottenPasswordEntityOptional = forgottenPasswordDao.findNonExpiredByCode(forgottenPasswordCode);
assertTrue(forgottenPasswordEntityOptional.isPresent());

ForgottenPasswordEntity forgottenPasswordEntity = forgottenPasswordEntityOptional.get();
assertThat(forgottenPasswordEntity.getCode(), is(forgottenPassword.getCode()));
assertThat(forgottenPasswordEntity.getDate(), within(1, MINUTES, forgottenPassword.getDate()));
assertThat(forgottenPasswordEntity.getCode(), is(forgottenPasswordCode));
assertThat(forgottenPasswordEntity.getDate(), within(1, MINUTES, notExpired));
}

@Test
Expand All @@ -95,11 +99,15 @@ public void shouldNotFindForgottenPasswordByCode_ifExpired() {
UserEntity userEntity = userDao.findByExternalId(userExternalId).get();

ZonedDateTime expired = ZonedDateTime.now().minusMinutes(91);
ForgottenPassword forgottenPassword = forgottenPassword(randomInt(), forgottenPasswordCode, expired, userExternalId);

databaseHelper.add(forgottenPassword, userEntity.getId());
aForgottenPasswordDbFixture()
.withDatabaseTestHelper(databaseHelper)
.withUserId(userEntity.getId())
.withExpiryDate(expired)
.withCode(forgottenPasswordCode)
.insert();

Optional<ForgottenPasswordEntity> forgottenPasswordEntityOptional = forgottenPasswordDao.findNonExpiredByCode(forgottenPassword.getCode());
Optional<ForgottenPasswordEntity> forgottenPasswordEntityOptional = forgottenPasswordDao.findNonExpiredByCode(forgottenPasswordCode);
assertFalse(forgottenPasswordEntityOptional.isPresent());
}

Expand All @@ -113,15 +121,19 @@ public void shouldRemoveForgottenPasswordEntity() {
UserEntity userEntity = userDao.findByExternalId(userExternalId).get();

ZonedDateTime notExpired = ZonedDateTime.now().minusMinutes(89);
ForgottenPassword forgottenPassword = forgottenPassword(randomInt(), forgottenPasswordCode, notExpired, userExternalId);

databaseHelper.add(forgottenPassword, userEntity.getId());
aForgottenPasswordDbFixture()
.withDatabaseTestHelper(databaseHelper)
.withUserId(userEntity.getId())
.withExpiryDate(notExpired)
.withCode(forgottenPasswordCode)
.insert();

Optional<ForgottenPasswordEntity> forgottenPasswordEntityOptional = forgottenPasswordDao.findNonExpiredByCode(forgottenPassword.getCode());
Optional<ForgottenPasswordEntity> forgottenPasswordEntityOptional = forgottenPasswordDao.findNonExpiredByCode(forgottenPasswordCode);
assertTrue(forgottenPasswordEntityOptional.isPresent());

forgottenPasswordDao.remove(forgottenPasswordEntityOptional.get());

assertThat(forgottenPasswordDao.findNonExpiredByCode(forgottenPassword.getCode()).isPresent(), is(false));
assertThat(forgottenPasswordDao.findNonExpiredByCode(forgottenPasswordCode).isPresent(), is(false));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.pay.adminusers.resources;

import org.junit.jupiter.api.Test;
import uk.gov.pay.adminusers.fixtures.ForgottenPasswordDbFixture;

import java.util.Map;

Expand All @@ -10,7 +11,7 @@
import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
import static org.hamcrest.core.Is.is;
import static uk.gov.pay.adminusers.app.util.RandomIdGenerator.randomUuid;
import static uk.gov.pay.adminusers.fixtures.ForgottenPasswordDbFixture.forgottenPasswordDbFixture;
import static uk.gov.pay.adminusers.fixtures.ForgottenPasswordDbFixture.aForgottenPasswordDbFixture;
import static uk.gov.pay.adminusers.fixtures.UserDbFixture.userDbFixture;

public class ForgottenPasswordResourceIT extends IntegrationTest {
Expand Down Expand Up @@ -54,7 +55,12 @@ public void shouldGetForgottenPassword_whenGetByCode_forAnExistingForgottenPassw
String username = randomUuid();
String email = username + "@example.com";
int userId = userDbFixture(databaseHelper).withEmail(email).insertUser().getId();
String forgottenPasswordCode = forgottenPasswordDbFixture(databaseHelper, userId).insertForgottenPassword();

String forgottenPasswordCode = aForgottenPasswordDbFixture()
.withDatabaseTestHelper(databaseHelper)
.withUserId(userId)
.insert()
.getForgottenPasswordCode();

givenSetup()
.when()
Expand All @@ -63,7 +69,6 @@ public void shouldGetForgottenPassword_whenGetByCode_forAnExistingForgottenPassw
.then()
.statusCode(OK.getStatusCode())
.body("code", is(forgottenPasswordCode));

}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package uk.gov.pay.adminusers.resources;

import org.junit.BeforeClass;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Map;

import static io.restassured.http.ContentType.JSON;
import static java.time.temporal.ChronoUnit.MINUTES;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.Is.is;
import static uk.gov.pay.adminusers.app.util.RandomIdGenerator.randomUuid;
import static uk.gov.pay.adminusers.fixtures.ForgottenPasswordDbFixture.forgottenPasswordDbFixture;
import static uk.gov.pay.adminusers.fixtures.ForgottenPasswordDbFixture.aForgottenPasswordDbFixture;
import static uk.gov.pay.adminusers.fixtures.UserDbFixture.userDbFixture;

public class ResetPasswordResourceIT extends IntegrationTest {
Expand All @@ -33,7 +35,12 @@ public void before() {
@Test
public void resetPassword_shouldReturn204_whenCodeIsValid_changingTheOldPasswordToTheNewEncryptedOne() throws Exception {

String forgottenPasswordCode = forgottenPasswordDbFixture(databaseHelper, userId).insertForgottenPassword();
String forgottenPasswordCode = aForgottenPasswordDbFixture()
.withDatabaseTestHelper(databaseHelper)
.withUserId(userId)
.insert()
.getForgottenPasswordCode();

String password = "iPromiseIWon'tForgetThisPassword";

Map<Object, Object> payload = Map.of(
Expand Down Expand Up @@ -84,8 +91,13 @@ public void resetPassword_shouldReturn400_whenCodeIsInvalid_andCurrentEncryptedP
@Test
public void resetPassword_shouldReturn400_whenCodeHasExpired_andCurrentEncryptedPasswordShouldNotChange() throws Exception {


String expiredForgottenPasswordCode = forgottenPasswordDbFixture(databaseHelper, userId).expired().insertForgottenPassword();
ZonedDateTime expired = ZonedDateTime.now(ZoneId.of("UTC")).minus(91, MINUTES);
String expiredForgottenPasswordCode = aForgottenPasswordDbFixture()
.withDatabaseTestHelper(databaseHelper)
.withUserId(userId)
.withExpiryDate(expired)
.insert()
.getForgottenPasswordCode();

Map<Object, Object> payload = Map.of(
"forgotten_password_code", expiredForgottenPasswordCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.jdbi.v3.core.Jdbi;
import org.postgresql.util.PGobject;
import uk.gov.pay.adminusers.model.ForgottenPassword;
import uk.gov.pay.adminusers.model.MerchantDetails;
import uk.gov.pay.adminusers.model.Permission;
import uk.gov.pay.adminusers.model.Role;
Expand Down Expand Up @@ -175,15 +174,16 @@ public DatabaseTestHelper add(Permission permission) {
return this;
}

public DatabaseTestHelper add(ForgottenPassword forgottenPassword, Integer userId) {
public DatabaseTestHelper insertForgottenPassword(Integer id, ZonedDateTime date, String code, Integer userId, ZonedDateTime createdAt) {
jdbi.withHandle(handle ->
handle
.createUpdate("INSERT INTO forgotten_passwords(id, date, code, \"userId\") " +
"VALUES (:id, :date, :code, :userId)")
.bind("id", forgottenPassword.getId())
.bind("date", from(forgottenPassword.getDate().toInstant()))
.bind("code", forgottenPassword.getCode())
.createUpdate("INSERT INTO forgotten_passwords(id, date, code, \"userId\", \"createdAt\") " +
"VALUES (:id, :date, :code, :userId, :createdAt)")
.bind("id", id)
.bind("date", from(date.toInstant()))
.bind("code", code)
.bind("userId", userId)
.bind("createdAt", from(date.toInstant()))
.execute()
);
return this;
Expand Down

0 comments on commit 2f4f1c2

Please sign in to comment.