Skip to content

Commit

Permalink
Merge pull request #198 from hmcts/lau-819_functional_test_refactor
Browse files Browse the repository at this point in the history
Lau 819 functional test refactor
  • Loading branch information
MartinYSpasov authored Dec 1, 2023
2 parents 3a6f779 + 2f37f14 commit 4dcf999
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 15 deletions.
1 change: 1 addition & 0 deletions Jenkinsfile_CNP
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ withPipeline(type, product, component) {
env.S2S_URL = 'http://rpe-service-auth-provider-aat.service.core-compute-aat.internal'
env.IDAM_API_URL = 'https://idam-api.aat.platform.hmcts.net'
env.ROLE_ASSIGNMENT_HOST = 'http://am-role-assignment-service-aat.service.core-compute-aat.internal'
env.LAU_API_URL = 'http://lau-idam-backend-aat.service.core-compute-aat.internal'

echo "Just waiting a while to ensure that the pod has run the job"
sh "sleep 120s"
Expand Down
1 change: 1 addition & 0 deletions Jenkinsfile_nightly
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ withNightlyPipeline(type, product, component) {
env.S2S_URL = 'http://rpe-service-auth-provider-aat.service.core-compute-aat.internal'
env.IDAM_API_URL = 'https://idam-api.aat.platform.hmcts.net'
env.ROLE_ASSIGNMENT_HOST = 'http://am-role-assignment-service-aat.service.core-compute-aat.internal'
env.LAU_API_URL = 'http://lau-idam-backend-aat.service.core-compute-aat.internal'

loadVaultSecrets(secrets)
enableAksStagingDeployment()
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ dependencies {
testImplementation group: 'io.cucumber', name: 'cucumber-java', version: versions.cucumber
testImplementation group: 'com.github.tomakehurst', name: 'wiremock-jre8-standalone', version: '3.0.1'
testImplementation group: 'com.google.code.gson', name: 'gson', version: '2.10.1'
testImplementation group: 'org.awaitility', name: 'awaitility', version: '3.0.0'

testImplementation(platform(group: 'org.junit', name: 'junit-bom', version: '5.10.1'))
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
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;
Expand All @@ -21,6 +24,8 @@
@ActiveProfiles("functional")
@RequiredArgsConstructor
@Slf4j
@Execution(ExecutionMode.SAME_THREAD)
@Disabled
class UserDeletionFunctionalTest {

@Inject
Expand Down
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);
}
}
}

}
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;
}
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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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");
}
}
Loading

0 comments on commit 4dcf999

Please sign in to comment.