Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PUB-2752 Add account smoke tests #496

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ configurations {
integrationTestImplementation.extendsFrom testImplementation
integrationTestRuntimeOnly.extendsFrom runtimeOnly

smokeTestImplementation.extendsFrom testImplementation
smokeTestImplementation.extendsFrom functionalTestImplementation
smokeTestRuntimeOnly.extendsFrom runtimeOnly

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

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.TestInstance;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.ActiveProfiles;
import org.testcontainers.shaded.com.fasterxml.jackson.core.JsonProcessingException;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
import uk.gov.hmcts.reform.pip.account.management.model.MediaApplicationStatus;
import uk.gov.hmcts.reform.pip.account.management.model.account.AzureAccount;
import uk.gov.hmcts.reform.pip.account.management.model.account.CreationEnum;
import uk.gov.hmcts.reform.pip.account.management.utils.OAuthClient;
import uk.gov.hmcts.reform.pip.account.management.utils.SmokeTestBase;
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.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

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

@SpringBootTest(classes = {OAuthClient.class})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ActiveProfiles("smoke")
class SmokeTest extends SmokeTestBase {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final String BASE_ACCOUNT_URL = "/account";
private static final String CREATE_PI_ACCOUNT_URL = BASE_ACCOUNT_URL + "/add/pi";
private static final String CREATE_AZURE_ACCOUNT_URL = BASE_ACCOUNT_URL + "/add/azure";
private static final String MEDIA_APPLICATION_URL = "/application";
private static final String TESTING_SUPPORT_DELETE_ACCOUNT_URL = "/testing-support/account/";
private static final String TESTING_SUPPORT_APPLICATION_URL = "/testing-support/application/";

private static final String ISSUER_ID_HEADER = "x-issuer-id";
private static final String ISSUER_ID = UUID.randomUUID().toString();
private static final String TEST_FIRST_NAME = "SmokeTestFirstName";
private static final String TEST_SURNAME = "SmokeTestSurname";
private static final String TEST_DISPLAY_NAME = "SmokeTestName";
private static final String TEST_EMPLOYER = "SmokeTestEmployer";
private static final String TEST_EMAIL_PREFIX = "SmokeTestEmail-"
+ ThreadLocalRandom.current().nextInt(1000, 9999);
private static final String TEST_EMAIL = TEST_EMAIL_PREFIX + "@justice.gov.uk";

private static final String MOCK_FILE = "test-image.png";
private static final TypeRef<Map<CreationEnum, List<? extends AzureAccount>>> AZURE_ACCOUNT_RESPONSE_TYPE
= new TypeRef<>() {};

private static final String STATUS_CODE_MATCH = "Status code does not match";
private static final String RESPONSE_BODY_MATCH = "Response body does not match";

@BeforeAll
public void setup() {
OBJECT_MAPPER.findAndRegisterModules();
}

@AfterAll
public void teardown() {
doDeleteRequest(TESTING_SUPPORT_DELETE_ACCOUNT_URL + TEST_EMAIL_PREFIX);
doDeleteRequest(TESTING_SUPPORT_APPLICATION_URL + TEST_EMAIL_PREFIX);
}

@Test
void testHealthCheck() {
Response response = doGetRequest("");

assertThat(response.statusCode())
.as(STATUS_CODE_MATCH)
.isEqualTo(OK.value());

assertThat(response.body().asString())
.as(RESPONSE_BODY_MATCH)
.isEqualTo("Welcome to account-management");
}

@Test
void testCreateUserAccount() throws JsonProcessingException {
AzureAccount azureAccount = new AzureAccount();
azureAccount.setFirstName(TEST_FIRST_NAME);
azureAccount.setSurname(TEST_SURNAME);
azureAccount.setDisplayName(TEST_DISPLAY_NAME);
azureAccount.setRole(Roles.VERIFIED);
azureAccount.setEmail(TEST_EMAIL);

Response response = doPostRequest(CREATE_AZURE_ACCOUNT_URL, Map.of(ISSUER_ID_HEADER, ISSUER_ID),
OBJECT_MAPPER.writeValueAsString(List.of(azureAccount)));

String azureAccountId = response.getBody().as(AZURE_ACCOUNT_RESPONSE_TYPE)
.get(CreationEnum.CREATED_ACCOUNTS)
.get(0)
.getAzureAccountId();

PiUser piUser = new PiUser();
piUser.setEmail(TEST_EMAIL);
piUser.setRoles(Roles.VERIFIED);
piUser.setForenames(TEST_FIRST_NAME);
piUser.setSurname(TEST_SURNAME);
piUser.setUserProvenance(UserProvenances.PI_AAD);
piUser.setProvenanceUserId(azureAccountId);

response = doPostRequest(CREATE_PI_ACCOUNT_URL, Map.of(ISSUER_ID_HEADER, ISSUER_ID),
OBJECT_MAPPER.writeValueAsString(List.of(piUser)));

assertThat(response.getStatusCode())
.as(STATUS_CODE_MATCH)
.isEqualTo(CREATED.value());
}

@Test
void testCreateMediaApplication() throws IOException {
Response response = doPostMultipartForApplication(MEDIA_APPLICATION_URL,
new ClassPathResource(MOCK_FILE).getFile(),
TEST_DISPLAY_NAME, TEST_EMAIL, TEST_EMPLOYER,
MediaApplicationStatus.PENDING.toString());

assertThat(response.getStatusCode())
.as(STATUS_CODE_MATCH)
.isEqualTo(OK.value());
}
}

This file was deleted.

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

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import static io.restassured.RestAssured.given;

@Component
public class OAuthClient {
@Value("${CLIENT_ID_FT}")
private String clientId;

@Value("${CLIENT_SECRET_FT}")
private String clientSecret;

@Value("${TENANT_ID}")
private String tenantId;

@Value("${APP_URI}")
private String scope;

public String generateAccessToken() {
return given()
.relaxedHTTPSValidation()
.header("content-type", "application/x-www-form-urlencoded")
.formParam("client_id", clientId)
.formParam("scope", scope + "/.default")
.formParam("client_secret", clientSecret)
.formParam("grant_type", "client_credentials")
.baseUri("https://login.microsoftonline.com/" + tenantId + "/oauth2/v2.0/token")
.post()
.body()
.jsonPath()
.get("access_token");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package uk.gov.hmcts.reform.pip.account.management.utils;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static io.restassured.RestAssured.given;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;

@SpringBootTest(classes = {OAuthClient.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class SmokeTestBase {
private String accessToken;

@Value("${TEST_URL:http://localhost:6969}")
private String testUrl;

@Autowired
private OAuthClient authClient;

@BeforeAll
void startup() {
RestAssured.baseURI = testUrl;
accessToken = authClient.generateAccessToken();
}

protected Response doGetRequest(final String path) {
return given()
.relaxedHTTPSValidation()
.when()
.get(path)
.thenReturn();
}

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

protected Response doPostMultipartForApplication(String path, File file, String name, String email,
String employer, String status) {
return given()
.relaxedHTTPSValidation()
.headers(Map.of(AUTHORIZATION, "bearer " + accessToken))
.multiPart("file", file)
.multiPart("fullName", name)
.multiPart("email", email)
.multiPart("employer", employer)
.multiPart("status", status)
.when()
.post(path)
.thenReturn();
}

protected Response doDeleteRequest(final String path) {
return given()
.relaxedHTTPSValidation()
.headers(getRequestHeaders(Collections.emptyMap()))
.when()
.delete(path)
.thenReturn();
}

private Map<String, String> getRequestHeaders(final Map<String, String> additionalHeaders) {
final Map<String, String> headers = new ConcurrentHashMap<>();
headers.put(AUTHORIZATION, "bearer " + accessToken);
headers.put(CONTENT_TYPE, "application/json");

if (!CollectionUtils.isEmpty(additionalHeaders)) {
headers.putAll(additionalHeaders);
}
return headers;
}
}
Binary file added src/smokeTest/resources/test-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.