Skip to content

Commit

Permalink
PUB-2572 - Functional Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisS1512 committed Dec 18, 2024
1 parent 3c2073c commit 62e1417
Show file tree
Hide file tree
Showing 11 changed files with 721 additions and 817 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@
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 uk.gov.hmcts.reform.pip.account.management.utils.AccountHelperBase;

import java.io.BufferedWriter;
import java.io.File;
Expand All @@ -24,22 +19,15 @@

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

@ExtendWith(SpringExtension.class)
@ActiveProfiles(profiles = "functional")
@SpringBootTest(classes = {OAuthClient.class})
class BulkAccountTest extends FunctionalTestBase {
class BulkAccountTest extends AccountHelperBase {
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
Expand Down Expand Up @@ -70,7 +58,7 @@ private String generateTestEmail() {

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

Expand Down

Large diffs are not rendered by default.

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpHeaders;
import io.restassured.common.mapper.TypeRef;
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.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import uk.gov.hmcts.reform.pip.account.management.model.CreationEnum;
import uk.gov.hmcts.reform.pip.account.management.model.SystemAdminAccount;
import uk.gov.hmcts.reform.pip.account.management.utils.FunctionalTestBase;
import uk.gov.hmcts.reform.pip.account.management.utils.OAuthClient;
import uk.gov.hmcts.reform.pip.account.management.utils.AccountHelperBase;
import uk.gov.hmcts.reform.pip.account.management.utils.CustomPageImpl;
import uk.gov.hmcts.reform.pip.model.account.PiUser;
import uk.gov.hmcts.reform.pip.model.account.Roles;
import uk.gov.hmcts.reform.pip.model.account.UserProvenances;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;

@ExtendWith(SpringExtension.class)
@ActiveProfiles(profiles = "functional")
@SpringBootTest(classes = {OAuthClient.class})
public class FunctionalAccountFilteringControllerTest extends FunctionalTestBase {
class FunctionalAccountFilteringControllerTest extends AccountHelperBase {
private static final TypeRef<CustomPageImpl<PiUser>> GET_ALL_USERS_TYPE = new TypeRef<>() {};

private static final String TEST_EMAIL_PREFIX = String.format(
"pip-am-test-email-%s", ThreadLocalRandom.current().nextInt(1000, 9999));
private static final String BEARER = "Bearer ";
private static final String ISSUER_ID = "x-issuer-id";
private static final TypeRef<Map<CreationEnum, List<?>>> createdResponseType = new TypeRef<>() {};
private static final TypeRef<PageImpl<PiUser>> getAllUsersType = new TypeRef<>() {};

private Map<String, String> bearer;
private PiUser systemAdminUser;
private String thirdPartyUserId;

private static final String TESTING_SUPPORT_DELETE_ACCOUNT_URL = "/testing-support/account/";
private static final String GET_ALL_THIRD_PARTY_ACCOUNTS_ENDPOINT = "/account/all/third-party";
private static final String CREATE_SYSTEM_ADMIN_SSO = "/account/system-admin";
private static final String CREATE_PI_ACCOUNT_PATH = "/account/add/pi";
private static final String DELETE_ENDPOINT = "/account/delete/%s";
private static final String GET_ALL_THIRD_PARTY_ACCOUNTS = "/account/all/third-party";
private static final String DELETE_ACCOUNT = "/account/delete/%s";
private static final String GET_ADMIN_USER_BY_EMAIL_AND_PROVENANCE = "/account/admin/%s/%s";
private static final String GET_ACCOUNTS_EXCEPT_THIRD_PARTY = "/account/all";

private static final ObjectMapper objectMapper = new ObjectMapper();

private String generateEmail() {
return TEST_EMAIL_PREFIX + "-" +
ThreadLocalRandom.current().nextInt(1000, 9999) + "@justice.gov.uk";
}

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

SystemAdminAccount systemAdminAccount = new SystemAdminAccount();
systemAdminAccount.setEmail(generateEmail());
systemAdminAccount.setSurname("AM E2E Surname");
systemAdminAccount.setFirstName("AM E2E First Name");
systemAdminAccount.setProvenanceUserId(UUID.randomUUID().toString());

systemAdminUser = doPostRequest(CREATE_SYSTEM_ADMIN_SSO,
bearer, objectMapper.writeValueAsString(systemAdminAccount))
.getBody().as(PiUser.class);
systemAdminUser = createSystemAdminAccount();

PiUser piUser = new PiUser();
piUser.setRoles(Roles.GENERAL_THIRD_PARTY);
Expand All @@ -87,48 +48,24 @@ public void startUp() throws JsonProcessingException {
List<PiUser> thirdPartyList = new ArrayList<>();
thirdPartyList.add(piUser);

Map<String, String> headers = new HashMap<>(bearer);
Map<String, String> headers = new ConcurrentHashMap<>(bearer);
headers.put(ISSUER_ID, systemAdminUser.getUserId());

Response createdResponse =
doPostRequest(CREATE_PI_ACCOUNT_PATH, headers, objectMapper.writeValueAsString(thirdPartyList));
doPostRequest(CREATE_PI_ACCOUNT, headers, objectMapper.writeValueAsString(thirdPartyList));

thirdPartyUserId = (String) createdResponse.getBody()
.as(createdResponseType).get(CreationEnum.CREATED_ACCOUNTS).get(0);
thirdPartyUserId = getCreatedAccountUserId(createdResponse);
}

@AfterAll
public void teardown() {
doDeleteRequest(TESTING_SUPPORT_DELETE_ACCOUNT_URL + TEST_EMAIL_PREFIX, bearer);
doDeleteRequest(String.format(DELETE_ENDPOINT, thirdPartyUserId), bearer);
}

private Response createAccount(String email, String provenanceId, Roles role, UserProvenances userProvenance) throws JsonProcessingException {
PiUser piUser = new PiUser();
piUser.setEmail(email);
piUser.setRoles(role);
piUser.setForenames("TEST");
piUser.setSurname("USER");
piUser.setUserProvenance(userProvenance);
piUser.setProvenanceUserId(provenanceId);

List<PiUser> users = new ArrayList<>();
users.add(piUser);

Map<String, String> headers = new HashMap<>(bearer);
headers.put(ISSUER_ID, UUID.randomUUID().toString());

final Response createResponse = doPostRequest(CREATE_PI_ACCOUNT_PATH,
headers, objectMapper.writeValueAsString(users));

assertThat(createResponse.getStatusCode()).isEqualTo(CREATED.value());

return createResponse;
doDeleteRequest(String.format(DELETE_ACCOUNT, thirdPartyUserId), bearer);
}

@Test
void getAllThirdPartyAccounts() {
Response getThirdParties = doGetRequest(GET_ALL_THIRD_PARTY_ACCOUNTS_ENDPOINT, bearer);
void testGetAllThirdPartyAccounts() {
Response getThirdParties = doGetRequest(GET_ALL_THIRD_PARTY_ACCOUNTS, bearer);

assertThat(getThirdParties.getStatusCode()).isEqualTo(OK.value());

Expand All @@ -139,37 +76,35 @@ void getAllThirdPartyAccounts() {
}

@Test
void getAllAccountsExceptThirdPartyDoesNotContainThirdParty() {
void testGetAllAccountsExceptThirdPartyDoesNotContainThirdParty() {
Map<String, String> requestParams = new ConcurrentHashMap<>();
requestParams.put("userId", thirdPartyUserId);
requestParams.put("provenances", UserProvenances.THIRD_PARTY.toString());

Response response = doGetRequestWithRequestParams(String.format(GET_ACCOUNTS_EXCEPT_THIRD_PARTY,
UserProvenances.PI_AAD), bearer, requestParams);
Response response = doGetRequestWithRequestParams(GET_ACCOUNTS_EXCEPT_THIRD_PARTY, bearer, requestParams);

assertThat(response.getStatusCode()).isEqualTo(OK.value());

Page<PiUser> returnedPage = response.getBody().as(getAllUsersType);
Page<PiUser> returnedPage = response.getBody().as(GET_ALL_USERS_TYPE);
assertThat(returnedPage.getTotalElements()).isEqualTo(0);
}

@Test
void getAllAccountsExceptThirdPartyContainsAdminUser() {
void testGetAllAccountsExceptThirdPartyContainsAdminUser() {
Map<String, String> requestParams = new ConcurrentHashMap<>();
requestParams.put("userId", systemAdminUser.getUserId());

Response response = doGetRequestWithRequestParams(String.format(GET_ACCOUNTS_EXCEPT_THIRD_PARTY,
UserProvenances.PI_AAD), bearer, requestParams);
Response response = doGetRequestWithRequestParams(GET_ACCOUNTS_EXCEPT_THIRD_PARTY, bearer, requestParams);

assertThat(response.getStatusCode()).isEqualTo(OK.value());

Page<PiUser> returnedPage = response.getBody().as(getAllUsersType);
Page<PiUser> returnedPage = response.getBody().as(GET_ALL_USERS_TYPE);
assertThat(returnedPage.getTotalElements()).isEqualTo(1);
assertThat(returnedPage.getContent().get(0))
.matches(user -> user.getUserId().equals(systemAdminUser.getUserId()));
}

@Test
void getAdminUserByEmailAndProvenance() throws JsonProcessingException {
void testGetAdminUserByEmailAndProvenance() throws JsonProcessingException {
String email = generateEmail();
createAccount(email, UUID.randomUUID().toString(), Roles.INTERNAL_ADMIN_LOCAL, UserProvenances.PI_AAD);

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpHeaders;
import io.restassured.common.mapper.TypeRef;
import io.restassured.response.Response;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import uk.gov.hmcts.reform.pip.account.management.model.AzureAccount;
import uk.gov.hmcts.reform.pip.account.management.model.CreationEnum;
import uk.gov.hmcts.reform.pip.account.management.utils.AccountHelperBase;
import uk.gov.hmcts.reform.pip.model.account.Roles;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.springframework.http.HttpStatus.OK;

class FunctionalAzureAccountControllerTest extends AccountHelperBase {
private static final String CREATE_AZURE_ACCOUNT = "/account/add/azure";
private static final String GET_AZURE_ACCOUNT_INFO = "/account/azure/%s";

private static final String TEST_FIRST_NAME = "E2E_TEST_AM_FIRST_NAME";
private static final String TEST_LAST_NAME = "E2E_TEST_AM_LAST_NAME";
private static final String TEST_DISPLAY_NAME = "E2E_TEST_AM_DISPLAY_NAME";

private static final TypeRef<Map<CreationEnum, List<? extends AzureAccount>>> AZURE_ACCOUNT_RESPONSE_TYPE
= new TypeRef<>() {};

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

@AfterAll
public void tearDown() {
doDeleteRequest(TESTING_SUPPORT_DELETE_ACCOUNT_URL + TEST_EMAIL_PREFIX, bearer);
}

@Test
void testCreateAzureAccount() throws JsonProcessingException {
String email = generateEmail();

AzureAccount azureAccount = new AzureAccount();
azureAccount.setFirstName(TEST_FIRST_NAME);
azureAccount.setSurname(TEST_LAST_NAME);
azureAccount.setDisplayName(TEST_DISPLAY_NAME);
azureAccount.setRole(Roles.VERIFIED);
azureAccount.setEmail(email);

List<AzureAccount> azureAccounts = new ArrayList<>();
azureAccounts.add(azureAccount);

Map<String, String> headers = new ConcurrentHashMap<>(bearer);
headers.put(ISSUER_ID, UUID.randomUUID().toString());

Response response =
doPostRequest(CREATE_AZURE_ACCOUNT, headers, objectMapper.writeValueAsString(azureAccounts));

assertThat(response.getStatusCode()).isEqualTo(OK.value());

AzureAccount createdAccount = response.getBody()
.as(AZURE_ACCOUNT_RESPONSE_TYPE).get(CreationEnum.CREATED_ACCOUNTS).get(0);

assertThat(createdAccount.getEmail()).isEqualTo(email);
createAccount(email, createdAccount.getAzureAccountId());

Response getResponse = doGetRequest(String.format(GET_AZURE_ACCOUNT_INFO, createdAccount.getAzureAccountId()),
bearer);

assertThat(response.getStatusCode()).isEqualTo(OK.value());

AzureAccount returnedUser = getResponse.getBody().as(AzureAccount.class);

assertThat(returnedUser.getEmail()).isEqualTo(email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,25 @@
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.core.io.ClassPathResource;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import uk.gov.hmcts.reform.pip.account.management.model.MediaApplication;
import uk.gov.hmcts.reform.pip.account.management.model.MediaApplicationStatus;
import uk.gov.hmcts.reform.pip.account.management.utils.FunctionalTestBase;
import uk.gov.hmcts.reform.pip.account.management.utils.OAuthClient;
import uk.gov.hmcts.reform.pip.account.management.utils.AccountHelperBase;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.NO_CONTENT;
import static org.springframework.http.HttpStatus.OK;

@ExtendWith(SpringExtension.class)
@ActiveProfiles(profiles = "functional")
@SpringBootTest(classes = {OAuthClient.class})
@SuppressWarnings("PMD.TooManyMethods")
class MediaApplicationCreationTest extends FunctionalTestBase {
class MediaApplicationCreationTest extends AccountHelperBase {

private static final String TEST_NAME = "E2E Account Management Test Name";
private static final String TEST_EMPLOYER = "E2E Account Management Test Employer";
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 = TEST_EMAIL_PREFIX + "@justice.gov.uk";
private static final String STATUS = "PENDING";

Expand All @@ -50,14 +38,11 @@ class MediaApplicationCreationTest extends FunctionalTestBase {
private static final String GET_APPLICATIONS_BY_STATUS = "/application/status/PENDING";
private static final String REPORTING = "/application/reporting";
private static final String GET_ALL_APPLICATIONS = "/application";
private static final String BEARER = "Bearer ";
private static final String MOCK_FILE = "files/test-image.png";

Map<String, List<String>> reasons =
Map.of("Reason 1", List.of("Reason 1", "Reason 2"), "Reason 2", List.of("Reason 3", "Reason 4"));

private Map<String, String> bearer;

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

0 comments on commit 62e1417

Please sign in to comment.