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

Story: [CCLS 2191] Use common auth starter #12

Merged
merged 5 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 5 additions & 4 deletions assessment-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ apply plugin: 'uk.gov.laa.ccms.springboot.laa-ccms-spring-boot-gradle-plugin'

dependencies {

implementation 'io.swagger.core.v3:swagger-annotations:2.2.22'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.data:spring-data-commons'
implementation 'org.springdoc:springdoc-openapi-ui:1.7.0'
implementation 'com.google.code.findbugs:jsr305:3.0.2'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
Expand Down Expand Up @@ -39,13 +39,14 @@ openApiGenerate {
configOptions = [
delegatePattern : "false",
interfaceOnly : "true", // This will only generate interfaces, not implementations
dateLibrary : "java17",
java17 : "true",
dateLibrary : "legacy",
useTags : "true",
skipDefaultInterface : "true",
useJakartaEe : "true",
documentationProvider : "none",
serializableModel : "true"
serializableModel : "true",
annotationLibrary : "swagger2",
useSpringBoot3 : "true"
]
}

Expand Down
21 changes: 21 additions & 0 deletions assessment-api/open-api-specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ paths:
description: 'Bad request'
'401':
description: 'Unauthorized'
'403':
description: 'Forbidden'
'404':
description: 'Not found'
'500':
Expand Down Expand Up @@ -57,6 +59,8 @@ paths:
description: 'Bad request'
'401':
description: 'Unauthorized'
'403':
description: 'Forbidden'
'404':
description: 'Not found'
'500':
Expand All @@ -81,6 +85,8 @@ paths:
description: 'Bad request'
'401':
description: 'Unauthorized'
'403':
description: 'Forbidden'
'500':
description: 'Internal server error'
/assessments/{assessment-id}:
Expand All @@ -102,6 +108,8 @@ paths:
description: 'Bad request'
'401':
description: 'Unauthorized'
'403':
description: 'Forbidden'
'404':
description: 'Not found'
'500':
Expand All @@ -127,6 +135,8 @@ paths:
description: 'Bad request'
'401':
description: 'Unauthorized'
'403':
description: 'Forbidden'
'500':
description: 'Internal server error'
patch:
Expand All @@ -150,6 +160,8 @@ paths:
description: 'Bad request'
'401':
description: 'Unauthorized'
'403':
description: 'Forbidden'
'404':
description: 'Not found'
'500':
Expand All @@ -170,12 +182,19 @@ paths:
description: 'Bad request'
'401':
description: 'Unauthorized'
'403':
description: 'Forbidden'
'404':
description: 'Not found'
'500':
description: 'Internal server error'

components:
securitySchemes:
ApiKeyAuth:
type: apiKey
in: header
name: Authorization
parameters:
assessmentType:
name: name
Expand Down Expand Up @@ -372,3 +391,5 @@ components:
target_entity_id:
type: string

security:
- ApiKeyAuth: []
6 changes: 5 additions & 1 deletion assessment-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation files('lib/ojdbc8.jar')

//Enable access token authentication
implementation 'uk.gov.laa.ccms.springboot:laa-ccms-spring-boot-starter-auth:0.0.3-b2f8726-SNAPSHOT'
farrell-m marked this conversation as resolved.
Show resolved Hide resolved

//Enable Swagger UI
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0'

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
Expand All @@ -25,6 +28,7 @@ dependencies {
testImplementation 'org.testcontainers:testcontainers'
testImplementation 'org.testcontainers:junit-jupiter'
testImplementation 'org.testcontainers:oracle-xe'
testImplementation 'com.h2database:h2'
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ static void properties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", oracleContainerSingleton.getOracleContainer()::getJdbcUrl);
registry.add("spring.datasource.username", oracleContainerSingleton.getOracleContainer()::getUsername);
registry.add("spring.datasource.password", oracleContainerSingleton.getOracleContainer()::getPassword);

registry.add("laa.ccms.springboot.starter.auth.authorized-clients", () -> "[{\"name\":\"caab-ui\",\"roles\":[\"ALL\"],\"token\":\"78bd752c-814c-4fb5-801b-193839c8e768\"}]");
registry.add("laa.ccms.springboot.starter.auth.authorized-roles", () -> "[{\"name\":\"ALL\",\"URIs\":[\"/**\"]}]");
registry.add("laa.ccms.springboot.starter.auth.unprotected-uris", () -> "[\"\"]");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,25 @@ spring:
jpa:
database-platform: org.hibernate.dialect.OracleDialect
hibernate:
ddl-auto: none
ddl-auto: none

laa.ccms.springboot.starter.auth:
authentication-header: "Authorization"
authorized-clients: '[
{
"name": "integration-test-runner",
"roles": [
"ALL"
],
"token": "78bd752c-814c-4fb5-801b-193839c8e768"
}
]'
authorized-roles: '[
{
"name": "ALL",
"URIs": [
"/**"
]
}
]'
unprotected-uris: [ "/swagger-ui.html", "/swagger-ui/**", "/v3/api-docs/**", "/favicon.ico", "/open-api-specification.yml"]
22 changes: 22 additions & 0 deletions assessment-service/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,25 @@ spring:

server:
port: 8008


laa.ccms.springboot.starter.auth:
authentication-header: "Authorization"
authorized-clients: '[
{
"name": "caab-ui",
"roles": [
"ALL"
],
"token": "78bd752c-814c-4fb5-801b-193839c8e768"
}
]'
authorized-roles: '[
{
"name": "ALL",
"URIs": [
"/**"
]
}
]'
unprotected-uris: [ "/swagger-ui.html", "/swagger-ui/**", "/v3/api-docs/**", "/favicon.ico", "/open-api-specification.yml"]
6 changes: 6 additions & 0 deletions assessment-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ spring:
hibernate:
ddl-auto: none
open-in-view: false

laa.ccms.springboot.starter.auth:
authentication-header: "Authorization"
authorized-clients: ${AUTHORIZED_CLIENTS}
authorized-roles: ${AUTHORIZED_ROLES}
unprotected-uris: ${UNPROTECTED_URIS}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package uk.gov.laa.ccms.caab.assessment.controller;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
Expand All @@ -8,37 +12,48 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
import jakarta.servlet.ServletException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import uk.gov.laa.ccms.caab.assessment.advice.GlobalExceptionHandler;
import uk.gov.laa.ccms.caab.assessment.exception.ApplicationException;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentDetail;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentDetails;
import uk.gov.laa.ccms.caab.assessment.model.PatchAssessmentDetail;
import uk.gov.laa.ccms.caab.assessment.service.AssessmentService;

@WebMvcTest(AssessmentController.class)
@Import(GlobalExceptionHandler.class)
import java.util.ArrayList;
import java.util.List;

@ExtendWith(SpringExtension.class)
@WebAppConfiguration
class AssessmentControllerTest {

@MockBean
@Mock
private AssessmentService assessmentService;

@Autowired
@InjectMocks
private AssessmentController applicationController;

private MockMvc mockMvc;

@BeforeEach
public void setup() {
mockMvc = standaloneSetup(applicationController)
.build();
}

@Test
public void createAssessment_createsAssessmentSuccessfully() throws Exception {
Long assessmentId = 1L;
Expand Down Expand Up @@ -71,7 +86,7 @@ public void getAssessment() throws Exception {
}

@Test
public void getAssessment_throwsNotFound() throws Exception {
public void getAssessment_throwsNotFound() {
Long assessmentId = 1L;
String errorMessage = String.format("Assessment with id %s not found", assessmentId);

Expand All @@ -81,10 +96,16 @@ public void getAssessment_throwsNotFound() throws Exception {
errorMessage,
HttpStatus.NOT_FOUND));

this.mockMvc.perform(get("/assessments/{assessment-id}", assessmentId))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.error_message").value(errorMessage))
.andExpect(jsonPath("$.http_status").value(HttpStatus.NOT_FOUND.value()));
ServletException ex = assertThrows(ServletException.class, () ->
this.mockMvc.perform(get("/assessments/{assessment-id}", assessmentId)),
"Expected ServletException to be thrown, but wasn't.");

assertTrue(ex.getMessage().contains(errorMessage));
assertInstanceOf(ApplicationException.class, ex.getRootCause());

ApplicationException appEx = (ApplicationException) ex.getRootCause();
assertEquals(HttpStatus.NOT_FOUND, appEx.getHttpStatus());
assertEquals(errorMessage, appEx.getErrorMessage());

verify(assessmentService).getAssessment(assessmentId);
}
Expand Down
23 changes: 22 additions & 1 deletion assessment-service/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,25 @@ spring:
jpa:
database-platform: org.hibernate.dialect.H2Dialect
hibernate:
ddl-auto: none
ddl-auto: none

laa.ccms.springboot.starter.auth:
authentication-header: "Authorization"
authorized-clients: '[
{
"name": "test-runner",
"roles": [
"ALL"
],
"token": "78bd752c-814c-4fb5-801b-193839c8e768"
}
]'
authorized-roles: '[
{
"name": "ALL",
"URIs": [
"/**"
]
}
]'
unprotected-uris: [ "" ]
Loading