Skip to content

Commit

Permalink
Merge branch 'master' into PUB-2576
Browse files Browse the repository at this point in the history
  • Loading branch information
NatashaAlker authored Dec 19, 2024
2 parents 6dd6539 + fd8b02e commit e980853
Show file tree
Hide file tree
Showing 24 changed files with 879 additions and 131 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ dependencies {
implementation group: 'com.opencsv', name: 'opencsv', version: '5.9'
implementation group: 'commons-validator', name: 'commons-validator', version: '1.9.0'

implementation group: 'com.github.hmcts', name: 'pip-data-models', version: '2.1.32', {
implementation group: 'com.github.hmcts', name: 'pip-data-models', version: '2.1.34', {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-data-jpa'
}
implementation group: 'io.hypersistence', name: 'hypersistence-utils-hibernate-63', version: '3.8.3'
Expand Down
1 change: 1 addition & 0 deletions charts/pip-account-management/values.stg.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ java:
environment:
ENABLE_TESTING_SUPPORT_API: true
MANAGED_IDENTITY_CLIENT_ID: 8d0ead51-3b31-44df-a78e-ada4eea9fe87
MAX_SYSTEM_ADMIN_ACCOUNTS: 20
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package uk.gov.hmcts.reform.pip.account.management.controllers;

import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpHeaders;
import io.restassured.response.Response;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import uk.gov.hmcts.reform.pip.account.management.utils.FunctionalTestBase;
import uk.gov.hmcts.reform.pip.account.management.utils.OAuthClient;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@ExtendWith(SpringExtension.class)
@ActiveProfiles(profiles = "functional")
@SpringBootTest(classes = {OAuthClient.class})
class BulkAccountTest extends FunctionalTestBase {
private static final String USER_ID = UUID.randomUUID().toString();
private static final String EMAIL_PREFIX = "pip-am-test-email-";
private static final String TEST_SUITE_PREFIX = String.format("%s-",
ThreadLocalRandom.current().nextInt(1000, 9999));
private static final String TEST_SUITE_EMAIL_PREFIX = EMAIL_PREFIX + TEST_SUITE_PREFIX;
private static final String BULK_UPLOAD_URL = "account/media-bulk-upload";
private static final String TESTING_SUPPORT_ACCOUNT_URL = "/testing-support/account/";
private static final String BEARER = "Bearer ";
private static final String ISSUER_ID = "x-issuer-id";

private String mockFile;
private Map<String, String> bearer;
private Map<String, String> issuerId;

@BeforeAll
void startUp() throws IOException {
bearer = Map.of(HttpHeaders.AUTHORIZATION, BEARER + accessToken);
issuerId = Map.of(ISSUER_ID, USER_ID);

StringBuilder csvContent = new StringBuilder(400);
csvContent.append("email,firstName,surname\n");

for (int i = 0; i < 5; i++) {
String email = generateTestEmail();
csvContent.append(email).append(",testBulkFirstName,testBulkSurname\n");
}

Path mockFilePath = Files.createTempFile("mock-bulk-upload", ".csv");
try (BufferedWriter writer = Files.newBufferedWriter(mockFilePath)) {
writer.write(csvContent.toString());
}

mockFile = mockFilePath.toString();
}

private String generateTestEmail() {
String prefix = String.format("%s", ThreadLocalRandom.current().nextInt(1000, 9999));
return TEST_SUITE_EMAIL_PREFIX + prefix + "@justice.gov.uk";
}

@AfterAll
public void teardown() throws IOException {
doDeleteRequest(TESTING_SUPPORT_ACCOUNT_URL + TEST_SUITE_EMAIL_PREFIX, bearer);
Files.deleteIfExists(Path.of(mockFile));
}

@Test
void createAccountsInBulk() {
File mockBulkUploadFile = new File(mockFile);

Response response = doPostMultipartForBulk(BULK_UPLOAD_URL, bearer, issuerId, mockBulkUploadFile);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK.value());
assertThat(response.jsonPath().getList("CREATED_ACCOUNTS").size()).isEqualTo(5);
assertThat(response.jsonPath().getList("ERRORED_ACCOUNTS").isEmpty()).isTrue();
}

@Test
void shouldReturnOkButNotCreateAccountsForDuplicates() {
File mockBulkUploadFile = new File(mockFile);

Response response = doPostMultipartForBulk(BULK_UPLOAD_URL, bearer, issuerId, mockBulkUploadFile);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK.value());
assertThat(response.jsonPath().getList("CREATED_ACCOUNTS").isEmpty()).isTrue();
assertThat(response.jsonPath().getList("ERRORED_ACCOUNTS").isEmpty()).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MediaApplicationCreationTest extends FunctionalTestBase {
private static final String TEST_EMAIL_PREFIX = String.format(
"pip-am-test-email-%s", ThreadLocalRandom.current().nextInt(1000, 9999));

private static final String TEST_EMAIL = String.format(TEST_EMAIL_PREFIX + "@justice.gov.uk");
private static final String TEST_EMAIL = TEST_EMAIL_PREFIX + "@justice.gov.uk";
private static final String STATUS = "PENDING";

private static final String TESTING_SUPPORT_APPLICATION_URL = "/testing-support/application/";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package uk.gov.hmcts.reform.pip.account.management.controllers;

import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpHeaders;
import io.restassured.response.Response;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import uk.gov.hmcts.reform.pip.account.management.utils.FunctionalTestBase;
import uk.gov.hmcts.reform.pip.account.management.utils.OAuthClient;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@ExtendWith(SpringExtension.class)
@ActiveProfiles(profiles = "functional")
@SpringBootTest(classes = {OAuthClient.class})
class SystemAdminAccountCreationTest extends FunctionalTestBase {
private static final String TEST_USER_EMAIL_PREFIX = String.format(
"pip-am-test-email-%s", ThreadLocalRandom.current().nextInt(1000, 9999));
private static final String TEST_USER_EMAIL = TEST_USER_EMAIL_PREFIX + "@justice.gov.uk";
private static final String TEST_USER_PROVENANCE_ID = UUID.randomUUID().toString();

private static final String TESTING_SUPPORT_ACCOUNT_URL = "/testing-support/account/";
private static final String ACCOUNT_URL = "/account";
private static final String SYSTEM_ADMIN_URL = ACCOUNT_URL + "/system-admin";
private static final String BEARER = "Bearer ";

private Map<String, String> bearer;

@BeforeAll
public void startUp() {
bearer = Map.of(HttpHeaders.AUTHORIZATION, BEARER + accessToken);
}

@AfterAll
public void teardown() {
doDeleteRequest(TESTING_SUPPORT_ACCOUNT_URL + TEST_USER_EMAIL, bearer);
}

@Test
void createSystemAdminAccount() {
String requestBody = """
{
"email": "%s",
"provenanceUserId": "%s"
}
""".formatted(TEST_USER_EMAIL, TEST_USER_PROVENANCE_ID);

Response response = doPostRequest(SYSTEM_ADMIN_URL, bearer, requestBody);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK.value());
assertThat(response.jsonPath().getString("email")).isEqualTo(TEST_USER_EMAIL);
assertThat(response.jsonPath().getString("provenanceUserId")).isEqualTo(TEST_USER_PROVENANCE_ID);
}

@Test
void shouldFailToCreateSystemAdminAccountWithoutEmail() {
String requestBody = """
{
"provenanceUserId": "%s"
}
""".formatted(TEST_USER_PROVENANCE_ID);

Response response = doPostRequest(SYSTEM_ADMIN_URL, bearer, requestBody);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());
}

@Test
void shouldFailToCreateSystemAdminAccountWithoutProvenanceUserId() {
String requestBody = """
{
"email": "%s"
}
""".formatted(TEST_USER_EMAIL);

Response response = doPostRequest(SYSTEM_ADMIN_URL, bearer, requestBody);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package uk.gov.hmcts.reform.pip.account.management.controllers;

import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpHeaders;
import io.restassured.response.Response;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import uk.gov.hmcts.reform.pip.account.management.utils.FunctionalTestBase;
import uk.gov.hmcts.reform.pip.account.management.utils.OAuthClient;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@ExtendWith(SpringExtension.class)
@ActiveProfiles(profiles = "functional")
@SpringBootTest(classes = {OAuthClient.class})
class SystemAdminB2CAccountCreationTest extends FunctionalTestBase {
private static final String TEST_USER_EMAIL_PREFIX_1 = String.format(
"pip-am-test-email-%s", ThreadLocalRandom.current().nextInt(1000, 9999));
private static final String TEST_USER_EMAIL_PREFIX_2 = String.format(
"pip-am-test-email-%s", ThreadLocalRandom.current().nextInt(1000, 9999));
private static final String TEST_USER_EMAIL_PREFIX_3 = String.format(
"pip-am-test-email-%s", ThreadLocalRandom.current().nextInt(1000, 9999));
private static final String TEST_USER_EMAIL_1 = TEST_USER_EMAIL_PREFIX_1 + "@justice.gov.uk";
private static final String TEST_USER_EMAIL_2 = TEST_USER_EMAIL_PREFIX_2 + "@justice.gov.uk";
private static final String TEST_USER_EMAIL_3 = TEST_USER_EMAIL_PREFIX_3 + "@justice.gov.uk";
private static final String TEST_USER_PROVENANCE_ID = UUID.randomUUID().toString();

private static final String TESTING_SUPPORT_ACCOUNT_URL = "/testing-support/account/";
private static final String ACCOUNT_URL = "/account";
private static final String SYSTEM_ADMIN_B2C_URL = ACCOUNT_URL + "/add/system-admin";
private static final String SYSTEM_ADMIN_SSO_URL = ACCOUNT_URL + "/system-admin";
private static final String BEARER = "Bearer ";
private static final String ISSUER_ID = "x-issuer-id";

private Map<String, String> bearer;
private Map<String, String> issuerId;

@BeforeAll
public void startUp() {
bearer = Map.of(HttpHeaders.AUTHORIZATION, BEARER + accessToken);

String requestBody = """
{
"email": "%s",
"provenanceUserId": "%s"
}
""".formatted(TEST_USER_EMAIL_3, UUID.randomUUID().toString());

String userId = doPostRequest(SYSTEM_ADMIN_SSO_URL, bearer, requestBody)
.jsonPath().getString("userId");

issuerId = Map.of(ISSUER_ID, userId);
}

@AfterAll
public void teardown() {
doDeleteRequest(TESTING_SUPPORT_ACCOUNT_URL + TEST_USER_EMAIL_1, bearer);
doDeleteRequest(TESTING_SUPPORT_ACCOUNT_URL + TEST_USER_EMAIL_2, bearer);
doDeleteRequest(TESTING_SUPPORT_ACCOUNT_URL + TEST_USER_EMAIL_3, bearer);
}

@Test
void createSystemAdminB2CAccount() {
String requestBody = """
{
"email": "%s",
"provenanceUserId": "%s"
}
""".formatted(TEST_USER_EMAIL_1, TEST_USER_PROVENANCE_ID);

Response response = doPostRequestForB2C(SYSTEM_ADMIN_B2C_URL, bearer, issuerId, requestBody);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK.value());
assertThat(response.jsonPath().getString("email")).isEqualTo(TEST_USER_EMAIL_1);
assertThat(response.jsonPath().getString("provenanceUserId")).isNotNull();
}

@Test
void shouldCreateSystemAdminB2CAccountWithoutProvenanceUserId() {
String requestBody = """
{
"email": "%s"
}
""".formatted(TEST_USER_EMAIL_2);

Response response = doPostRequestForB2C(SYSTEM_ADMIN_B2C_URL, bearer, issuerId, requestBody);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK.value());
assertThat(response.jsonPath().getString("email")).isEqualTo(TEST_USER_EMAIL_2);
assertThat(response.jsonPath().getString("provenanceUserId")).isNotNull();
}

@Test
void shouldFailToCreateSystemAdminB2CAccountWithoutEmail() {
String requestBody = """
{
"provenanceUserId": "%s"
}
""".formatted(TEST_USER_PROVENANCE_ID);

Response response = doPostRequestForB2C(SYSTEM_ADMIN_B2C_URL, bearer, issuerId, requestBody);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());
}

@Test
void shouldFailToCreateSystemAdminB2CAccountWithoutIssuerId() {
String requestBody = """
{
"email": "%s",
"provenanceUserId": "%s"
}
""".formatted(TEST_USER_EMAIL_1, TEST_USER_PROVENANCE_ID);

Response response = doPostRequest(SYSTEM_ADMIN_B2C_URL, bearer, requestBody);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,33 @@ protected Response doPostRequest(final String path, final Map<String, String> ad
.thenReturn();
}

protected Response doPostRequestForB2C(final String path, final Map<String, String> additionalHeaders,
final Map<String, String> issuerId, final String body) {
return given()
.relaxedHTTPSValidation()
.headers(getRequestHeaders(additionalHeaders))
.headers(getRequestHeaders(issuerId))
.body(body)
.when()
.post(path)
.thenReturn();
}

protected Response doPostMultipartForBulk(final String path, final Map<String, String> additionalHeaders,
final Map<String, String> issuerId, final File mediaList) {
return given()
.relaxedHTTPSValidation()
.headers(additionalHeaders)
.headers(getRequestHeaders(issuerId))
.contentType("multipart/form-data")
.multiPart("mediaList", mediaList)
.when()
.post(path)
.thenReturn();
}

protected Response doPostMultipartForApplication(final String path, final Map<String, String> additionalHeaders,
final File file, String name, String email, String employer, String status) {
final File file, String name, String email, String employer, String status) {
return given()
.relaxedHTTPSValidation()
.headers(additionalHeaders)
Expand Down
Loading

0 comments on commit e980853

Please sign in to comment.