generated from hmcts/spring-boot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from hmcts/lau-819_functional_test_refactor
Lau 819 functional test refactor
- Loading branch information
Showing
12 changed files
with
366 additions
and
15 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
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
130 changes: 128 additions & 2 deletions
130
src/functionalTest/java/uk/gov/hmcts/reform/idam/UserRestoreFunctionalTest.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,22 +1,148 @@ | ||
package uk.gov.hmcts.reform.idam; | ||
|
||
import jakarta.inject.Inject; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.parallel.Execution; | ||
import org.junit.jupiter.api.parallel.ExecutionMode; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import uk.gov.hmcts.reform.idam.helpers.IdamUserDataProvider; | ||
import uk.gov.hmcts.reform.idam.helpers.LauDeletionLogEntryProvider; | ||
import uk.gov.hmcts.reform.idam.helpers.LauIdamBackendServiceProvider; | ||
import uk.gov.hmcts.reform.idam.service.IdamUserRestorerService; | ||
import uk.gov.hmcts.reform.idam.service.remote.responses.DeletionLog; | ||
import uk.gov.hmcts.reform.idam.util.RestoreSummary; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.awaitility.Awaitility.with; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
@SpringBootTest | ||
@ActiveProfiles("functional") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Execution(ExecutionMode.SAME_THREAD) | ||
class UserRestoreFunctionalTest { | ||
|
||
@Inject | ||
IdamUserRestorerService idamUserRestoreService; | ||
|
||
@Inject | ||
private IdamUserDataProvider idamUserDataProvider; | ||
|
||
@Inject | ||
private LauDeletionLogEntryProvider lauDeletionLogEntryProvider; | ||
|
||
@Inject | ||
LauIdamBackendServiceProvider lauIdamBackendServiceProvider; | ||
|
||
@Inject | ||
private RestoreSummary restoreSummary; | ||
|
||
List<String> logEntryUserIds = new ArrayList<>(); | ||
|
||
@Test | ||
@DirtiesContext | ||
void givenDeletedUserExistsThenShouldAbleToRestoreDeletedUsers() { | ||
assertTrue("Dummy condition", true); | ||
List<DeletionLog> lauDeletionLogs = lauDeletionLogEntryProvider.createDeletionLogLau(); | ||
assertEquals("Deletion Log entry has not created", 1, lauDeletionLogs.size()); | ||
logEntryUserIds.add(lauDeletionLogs.get(0).getUserId()); | ||
|
||
idamUserRestoreService.run(); | ||
|
||
with().await() | ||
.untilAsserted(() -> { | ||
List<String> userIds = restoreSummary.getSuccessful(); | ||
assertEquals("User has not been restored successfully", 1, userIds.size()); | ||
}); | ||
} | ||
|
||
@Test | ||
@DirtiesContext | ||
void givenDeletedUserExistsInIdamThenFailedToRestoreDueToReinstatedAndActiveAccount() { | ||
String userId = idamUserDataProvider.createIdamUser(); | ||
List<DeletionLog> deletionLogs = lauDeletionLogEntryProvider.postDeletedLogEntryWithExistingUserId(userId, 4); | ||
assertEquals("Deletion Log entry has not created", 1, deletionLogs.size()); | ||
logEntryUserIds.add(deletionLogs.get(0).getUserId()); | ||
|
||
idamUserRestoreService.run(); | ||
|
||
with().await() | ||
.untilAsserted(() -> { | ||
List<String> failedUserIds = restoreSummary.getFailedToRestoreDueToReinstatedAndActiveAccount(); | ||
assertEquals("User has not been restored successfully", 1, failedUserIds.size()); | ||
}); | ||
} | ||
|
||
@Test | ||
@DirtiesContext | ||
void givenDeletedUserExistsInIdamAndRetiredThenFailedToRestoreDueToReinstatedAccount() { | ||
String userId = idamUserDataProvider.createAndRetireIdamUser(); | ||
List<DeletionLog> deletionLogs = lauDeletionLogEntryProvider.postDeletedLogEntryWithExistingUserId(userId, 6); | ||
assertEquals("Deletion Log entry has not created", 1, deletionLogs.size()); | ||
logEntryUserIds.add(deletionLogs.get(0).getUserId()); | ||
|
||
idamUserRestoreService.run(); | ||
|
||
with().await() | ||
.untilAsserted(() -> { | ||
List<String> failedUserIds = restoreSummary.getFailedToRestoreDueToReinstatedAccount(); | ||
assertEquals("User has not been restored successfully", 1, failedUserIds.size()); | ||
}); | ||
} | ||
|
||
@Test | ||
@DirtiesContext | ||
void givenDeletedUserExistsInIdamThenFailedToRestoreDueToNewAccountWithSameEmail() { | ||
String emailAddress = idamUserDataProvider.createIdamUserAndReturnEmailAddress(); | ||
List<DeletionLog> deletionLogs = lauDeletionLogEntryProvider | ||
.postDeletedLogEntryWithExistingEmail(emailAddress, 8); | ||
assertEquals("Deletion Log entry has not created", 1, deletionLogs.size()); | ||
logEntryUserIds.add(deletionLogs.get(0).getUserId()); | ||
|
||
idamUserRestoreService.run(); | ||
|
||
with().await() | ||
.untilAsserted(() -> { | ||
List<String> failedUserIds = restoreSummary.getFailedToRestoreDueToNewAccountWithSameEmail(); | ||
assertEquals("There should be 1 user fail to restore", 1, failedUserIds.size()); | ||
}); | ||
} | ||
|
||
@Test | ||
@DirtiesContext | ||
void givenDeletedUserExistsInIdamWithEmailAndRetiredThenFailedToRestoreDueToDuplicateEmail() { | ||
String emailAddress = idamUserDataProvider.createAndRetireIdamUserAndReturnEmailAddress(); | ||
List<DeletionLog> deletionLogs = lauDeletionLogEntryProvider | ||
.postDeletedLogEntryWithExistingEmail(emailAddress, 10); | ||
|
||
assertEquals("Deletion Log entry has not created", 1, deletionLogs.size()); | ||
|
||
logEntryUserIds.add(deletionLogs.get(0).getUserId()); | ||
|
||
idamUserRestoreService.run(); | ||
|
||
with().await() | ||
.untilAsserted(() -> { | ||
List<String> failedUserIds = restoreSummary.getFailedToRestoreDueToDuplicateEmail(); | ||
assertEquals("There should be 1 user fail to restore", 1, failedUserIds.size()); | ||
}); | ||
} | ||
|
||
@AfterEach | ||
public void teardown() { | ||
if (!logEntryUserIds.isEmpty()) { | ||
for (String logEntryUserId : logEntryUserIds) { | ||
int statusCode = lauIdamBackendServiceProvider.deleteLogEntry(logEntryUserId); | ||
assertEquals("Log entry not deleted", 204, statusCode); | ||
} | ||
} | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/functionalTest/java/uk/gov/hmcts/reform/idam/helpers/DeletedAccountsRequest.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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package uk.gov.hmcts.reform.idam.helpers; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import uk.gov.hmcts.reform.idam.service.remote.responses.DeletionLog; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Builder | ||
public class DeletedAccountsRequest { | ||
private List<DeletionLog> deletionLogs; | ||
} |
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,6 +1,8 @@ | ||
package uk.gov.hmcts.reform.idam.helpers; | ||
|
||
import io.restassured.RestAssured; | ||
import io.restassured.response.ExtractableResponse; | ||
import io.restassured.response.Response; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.json.JSONArray; | ||
|
@@ -23,7 +25,7 @@ public class IdamUserDataProvider { | |
private final SecurityUtil securityUtil; | ||
|
||
@Value("${idam.api.url}") | ||
private String idamApi = "https://idam-api.aat.platform.hmcts.net"; | ||
private String idamApi; | ||
|
||
@Value("${stale-users.roles}") | ||
private String roleToDelete; | ||
|
@@ -32,43 +34,46 @@ public class IdamUserDataProvider { | |
private static final String RETIRE_USER_PATH_TMPL = "api/v1/staleUsers/%s/retire"; | ||
|
||
public String setup() { | ||
RestAssured.baseURI = idamApi; | ||
securityUtil.generateTokens(); | ||
String userId = createUser(); | ||
ExtractableResponse<Response> res = createUser("[email protected]","Lau ", | ||
"Test","{Pass12345Y"); | ||
String userId = res.path("id"); | ||
retireUser(userId); | ||
return userId; | ||
} | ||
|
||
private String createUser() { | ||
private ExtractableResponse<Response> createUser(String email, String foreName, String lastName, String password) { | ||
return RestAssured.given() | ||
.header("Authorization", idamTokenGenerator.getIdamAuthorizationHeader()) | ||
.header("Content-Type", "application/json") | ||
.body(makeUser()) | ||
.body(makeUser(email,foreName,lastName,password)) | ||
.baseUri(idamApi) | ||
.when() | ||
.post(CREATE_USER_PATH) | ||
.then() | ||
.statusCode(201) | ||
.extract() | ||
.path("id"); | ||
.extract(); | ||
} | ||
|
||
private void retireUser(String userId) { | ||
RestAssured.given() | ||
.header("Authorization", idamTokenGenerator.getIdamAuthorizationHeader()) | ||
.header("Content-Type", "application/json") | ||
.baseUri(idamApi) | ||
.post(String.format(RETIRE_USER_PATH_TMPL, userId)) | ||
.then() | ||
.statusCode(200); | ||
} | ||
|
||
private String makeUser() { | ||
private String makeUser(String email, String foreName, String lastName, String password) { | ||
JSONObject user = new JSONObject(); | ||
try { | ||
String name = UUID.randomUUID().toString(); | ||
user.put("email", "DisposerTest-" + name + "@example.org"); | ||
user.put("forename", "Lau " + name); | ||
user.put("surname", "Test"); | ||
user.put("password", "{Pass12345Y"); | ||
String[] emailParts = email.split("-"); | ||
user.put("email", emailParts[0] + name + emailParts[1]); | ||
user.put("forename", foreName + name); | ||
user.put("surname", lastName); | ||
user.put("password", password); | ||
JSONArray roles = new JSONArray(); | ||
JSONObject role = new JSONObject(); | ||
role.put("code", roleToDelete); | ||
|
@@ -80,4 +85,38 @@ private String makeUser() { | |
|
||
return user.toString(); | ||
} | ||
|
||
public String createIdamUser() { | ||
securityUtil.generateTokens(); | ||
ExtractableResponse<Response> res = createRestorerUser(); | ||
return res.path("id"); | ||
} | ||
|
||
public String createAndRetireIdamUser() { | ||
securityUtil.generateTokens(); | ||
ExtractableResponse<Response> res = createRestorerUser(); | ||
String userId = res.path("id"); | ||
retireUser(userId); | ||
return userId; | ||
} | ||
|
||
public String createIdamUserAndReturnEmailAddress() { | ||
securityUtil.generateTokens(); | ||
ExtractableResponse<Response> res = createRestorerUser(); | ||
return res.path("email"); | ||
} | ||
|
||
public String createAndRetireIdamUserAndReturnEmailAddress() { | ||
securityUtil.generateTokens(); | ||
ExtractableResponse<Response> res = createRestorerUser(); | ||
String emailAddress = res.path("email"); | ||
String userId = res.path("id"); | ||
retireUser(userId); | ||
return emailAddress; | ||
} | ||
|
||
public ExtractableResponse<Response> createRestorerUser() { | ||
return createUser("[email protected]","LauRestorer ", | ||
"TestRestorer","{Pass12345Y"); | ||
} | ||
} |
Oops, something went wrong.