-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PP-11205 Refactor ForgottenPasswordDbFixture (#2195)
- 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
Showing
7 changed files
with
115 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 40 additions & 15 deletions
55
src/test/java/uk/gov/pay/adminusers/fixtures/ForgottenPasswordDbFixture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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") | ||
|
@@ -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") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters