Skip to content

Commit

Permalink
DTSAM-473_FTA_Fix (#2050)
Browse files Browse the repository at this point in the history
* Updated F-004.

* Updated F-004.

* DTSAM-473 recreate IDAM users in Test Data Loader

   [DTSAM-473](https://tools.hmcts.net/jira/browse/DTSAM-473)

Use BEFTA Test Data Loader to create IDAM test users.

* DTSAM-473 adjust test user name values to avoid IDAM vaidation issues

* DTSAM-473 Adjust judicial booking regions

   [DTSAM-473](https://tools.hmcts.net/jira/browse/DTSAM-473)

Adjust judicial booking regions to aovid triggering multi-region code.

---------

Co-authored-by: Matt Nayler <matt.nayler@cgi.com>
Co-authored-by: Matt Nayler <57350764+mattnayler@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 22, 2024
1 parent 3c39ca4 commit 5b497c9
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 7 deletions.
1 change: 1 addition & 0 deletions Jenkinsfile_CNP
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def branchesToSync = ['demo', 'ithc', 'perftest']
// Vars needed for functional and smoke tests run against AKS
env.IDAM_URL = "https://idam-api.aat.platform.hmcts.net"
env.IDAM_API_URL_BASE = "https://idam-api.aat.platform.hmcts.net"
env.IDAM_TESTING_SUPPORT_URL = "https://idam-testing-support-api.aat.platform.hmcts.net"
env.IDAM_S2S_URL = "http://rpe-service-auth-provider-aat.service.core-compute-aat.internal"
env.BEFTA_S2S_CLIENT_ID = "am_org_role_mapping_service"
env.OAUTH2_REDIRECT_URI = "http://am-role-assignment-service-aat.service.core-compute-aat.internal/oauth2redirect"
Expand Down
1 change: 1 addition & 0 deletions Jenkinsfile_nightly
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ withNightlyPipeline(type, product, component) {
// Vars needed for functional and smoke tests run against AKS
env.IDAM_URL = "https://idam-api.aat.platform.hmcts.net"
env.IDAM_API_URL_BASE = "https://idam-api.aat.platform.hmcts.net"
env.IDAM_TESTING_SUPPORT_URL = "https://idam-testing-support-api.aat.platform.hmcts.net"
env.IDAM_S2S_URL = "http://rpe-service-auth-provider-aat.service.core-compute-aat.internal"
env.BEFTA_S2S_CLIENT_ID = "am_org_role_mapping_service"
env.OAUTH2_REDIRECT_URI = "http://am-role-assignment-service-aat.service.core-compute-aat.internal/oauth2redirect"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.gov.hmcts.befta.BeftaTestDataLoader;
import uk.gov.hmcts.befta.DefaultTestAutomationAdapter;
import uk.gov.hmcts.befta.player.BackEndFunctionalTestScenarioContext;
import uk.gov.hmcts.befta.util.EnvironmentVariableUtils;
Expand All @@ -19,6 +20,11 @@ public class OrgRoleMappingAmTestAutomationAdapter extends DefaultTestAutomation

public static final String EMAIL_TEMPLATE = "ORM-func-test-user-%s@justice.gov.uk";

@Override
public BeftaTestDataLoader getDataLoader() {
return new OrmTestDataLoader();
}

@Override
public Object calculateCustomValue(BackEndFunctionalTestScenarioContext scenarioContext, Object key) {
//the docAMUrl is is referring the self link in PR
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package uk.gov.hmcts.reform.orgrolemapping.befta;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import uk.gov.hmcts.befta.BeftaMain;
import uk.gov.hmcts.befta.DefaultBeftaTestDataLoader;
import uk.gov.hmcts.befta.auth.UserTokenProviderConfig;
import uk.gov.hmcts.befta.data.UserData;
import uk.gov.hmcts.befta.util.BeftaUtils;
import uk.gov.hmcts.befta.util.EnvironmentVariableUtils;
import uk.gov.hmcts.befta.util.JsonUtils;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

public class OrmTestDataLoader extends DefaultBeftaTestDataLoader {

@Override
public void doLoadTestData() {

}

@Override
public boolean isTestDataLoadedForCurrentRound() {
return false;
}

@Override
public void loadDataIfNotLoadedVeryRecently() {
RestAssured.useRelaxedHTTPSValidation();
try {
createUsersInIdam();
} catch (Exception e) {
// Write details to log then swallow this error as this failure is not critical. i.e. allow it to continue
// so any errors related to the deployment under test will re-occur during smoke / functional test run
// which has better fault reporting.
BeftaUtils.defaultLog("Error creating test users in IDAM.", e);
}
}

private void createUsersInIdam() {
String idamTsUrl = EnvironmentVariableUtils.getOptionalVariable("IDAM_TESTING_SUPPORT_URL");

if (StringUtils.isBlank(idamTsUrl)) {
BeftaUtils.defaultLog("Skipping user creation in IDAM as no URL configured.");
} else {
File idamUsersDir = BeftaUtils.getFileFromResource("idamUsers");
if (idamUsersDir.exists() && idamUsersDir.isDirectory()) {
UserData amBeftaUser1 = getAmBeftaUser1();

FileFilter fileFilter = file -> !file.isDirectory() && file.getName().endsWith(".json");
for (final File jsonFile : Objects.requireNonNull(idamUsersDir.listFiles(fileFilter))) {
createUserFromFile(jsonFile, idamTsUrl, amBeftaUser1.getAccessToken());
}
}
}
}

private void createUserFromFile(File jsonFile, String idamTsUrl, String accessToken) {

JsonNode requestJson;

try {
requestJson = JsonUtils.readObjectFromJsonFile(jsonFile.getPath(), JsonNode.class);

// adjust for environment variables
updateNodeValueFromEnvironmentVariable(requestJson, "password");
updateNodeValueFromEnvironmentVariable(requestJson.findValue("user"), "email");

} catch (IOException e) {
throw new RuntimeException("Error loading user data from: " + jsonFile.getPath(), e);
}

Response response = RestAssured
.given(new RequestSpecBuilder().setBaseUri(idamTsUrl).build())
.header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken)
.body(requestJson)
.contentType(ContentType.JSON)
.when()
.post("/test/idam/users");

if (response.getStatusCode() == HttpStatus.CREATED.value()) {
BeftaUtils.defaultLog("IDAM user created from: " + jsonFile.getPath());

} else if (response.getStatusCode() == HttpStatus.CONFLICT.value()) {
BeftaUtils.defaultLog("IDAM user already exists for: " + jsonFile.getPath());

} else {
BeftaUtils.defaultLog("Error when creating IDAM user from: " + jsonFile.getPath());
String message = "Call to create IDAM user failed with response body: " + response.body().prettyPrint();
message += "\nand http code: " + response.statusCode();
throw new RuntimeException(message);
}
}

private UserData getAmBeftaUser1() {

UserData amBeftaUser1;

try {
// reuse user from FTAs
JsonNode userJson = JsonUtils.readObjectFromJsonFile(
BeftaUtils.getFileFromResource("features/common/users/AmBeftaUser1.td.json").getPath(),
JsonNode.class
);

// adjust for environment variables
amBeftaUser1 = new UserData(
EnvironmentVariableUtils.resolvePossibleVariable(userJson.findValue("username").asText()),
EnvironmentVariableUtils.resolvePossibleVariable(userJson.findValue("password").asText())
);

} catch (IOException e) {
throw new RuntimeException("Error loading user data for AmBeftaUser1", e);
}

try {
BeftaMain.getAdapter().authenticate(amBeftaUser1, UserTokenProviderConfig.DEFAULT_INSTANCE.getClientId());

} catch (ExecutionException e) {
throw new RuntimeException("Authenticating as AmBeftaUser1 failed.", e);
}

return amBeftaUser1;
}

private void updateNodeValueFromEnvironmentVariable(JsonNode parentNode, String fieldName) {
String value = EnvironmentVariableUtils.resolvePossibleVariable(parentNode.findValue(fieldName).asText());
((ObjectNode) parentNode).put(fieldName, value);
}

}
2 changes: 1 addition & 1 deletion src/functionalTest/resources/features/F-004/F-004.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@F-004 @FeatureToggle(EV:JUDICIAL_FTA_ENABLED=on) @Ignore
@F-004 @FeatureToggle(EV:JUDICIAL_FTA_ENABLED=on)
Feature: F-004 : Refresh Role Assignments for Judicial Users

Background:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"bookingRequest": {
"userId": "${[scenarioContext][parentContext][childContexts][S-031_DeleteDataForRoleAssignments][testData][request][body][userIds][0]}",
"locationId": "10",
"regionId": "6",
"regionId": "2",
"beginDate": "${[scenarioContext][customValues][today]}",
"endDate": "${[scenarioContext][customValues][today]}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"bookingRequest": {
"userId": "${[scenarioContext][parentContext][childContexts][S-032_DeleteDataForRoleAssignments][testData][request][body][userIds][0]}",
"locationId": "10",
"regionId": "6",
"regionId": "2",
"beginDate": "${[scenarioContext][customValues][today]}",
"endDate": "${[scenarioContext][customValues][today]}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"users": {
"invokingUser": {
"_extends_": "AmBeftaUser1"
"_extends_": "AmJRDUser1"
}
},
"request": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"users": {
"invokingUser": {
"_extends_": "AmBeftaUser1"
"_extends_": "AmJRDUser1"
}
},
"request": {
Expand All @@ -24,7 +24,7 @@
"reference":"${[scenarioContext][testData][request][body][userIds][0]}"
},
"body": {
"_extends_": "JRDTribunalJudgeFeepaidUser"
"_extends_": "JRDTribunalJudgeFeepaidUserForBooking"
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"users": {
"invokingUser": {
"_extends_": "AmBeftaUser1"
"_extends_": "AmJRDUser1"
}
},
"request": {
Expand Down
20 changes: 20 additions & 0 deletions src/functionalTest/resources/idamUsers/AmJrdUser1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"password": "[[$TEST_AM_JRD_USER1_BEFTA_PASSWORD]]",
"user": {
"id": "ff076da7-9fca-492c-806f-b0c6115530a6",
"email": "[[$TEST_AM_JRD_USER1_BEFTA]]",
"forename": "AM-ORM",
"surname": "FTA",
"displayName": "AM-ORM FTA",
"roleNames": [
"caseworker",
"cwd-admin",
"cwd-system-user",
"jrd-system-user",
"org-role-cwd-system-user",
"judiciary"
],
"accountStatus": "ACTIVE",
"recordType": "LIVE"
}
}

0 comments on commit 5b497c9

Please sign in to comment.