Skip to content

Commit

Permalink
- security test helper module;
Browse files Browse the repository at this point in the history
- fix enrollment tests;
  • Loading branch information
anton-liauchuk committed Jun 24, 2020
1 parent 6c2f6d8 commit 78616b0
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 1 deletion.
3 changes: 3 additions & 0 deletions course-enrollments/web/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ dependencies {
compileOnly 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'

testImplementation project(':users:users-web')
testImplementation project(':security:security-test')
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testImplementation 'org.mockito:mockito-junit-jupiter:3.3.0'
testImplementation 'org.assertj:assertj-core:3.15.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.educational.platform.course.enrollments.api;

import com.educational.platform.security.SignUpHelper;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
Expand All @@ -20,6 +22,9 @@
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CourseEnrollmentApiTest {

@Autowired
SignUpHelper signUpHelper;

@LocalServerPort
private int port;

Expand All @@ -30,8 +35,11 @@ void setup() {

@Test
void register_validCourse_createdWithUUID() {
var token = signUpHelper.signUpStudent();

given()
.contentType(ContentType.JSON)
.header("Authorization", "Bearer " + token)
.body("{\n" +
" \"student\": \"username\"\n" +
"}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import com.educational.platform.course.enrollments.CourseEnrollmentController;
import com.educational.platform.course.enrollments.register.RegisterStudentToCourseCommand;
import com.educational.platform.course.enrollments.register.RegisterStudentToCourseCommandHandler;
import com.educational.platform.users.security.WebSecurityConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;

import javax.validation.ConstraintViolationException;
Expand All @@ -24,7 +28,8 @@
/**
* Represents course enrollment controller integration tests.
*/
@WebMvcTest(controllers = CourseEnrollmentController.class, excludeAutoConfiguration = {SecurityAutoConfiguration.class})
@WebMvcTest(controllers = CourseEnrollmentController.class, excludeAutoConfiguration = {SecurityAutoConfiguration.class}, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = WebSecurityConfig.class)})
public class CourseEnrollmentControllerIntegrationTest {

@Autowired
Expand All @@ -34,6 +39,7 @@ public class CourseEnrollmentControllerIntegrationTest {
private RegisterStudentToCourseCommandHandler handler;

@Test
@WithMockUser(username = "student", roles = "STUDENT")
void enroll_validRequest_created() throws Exception {
this.mockMvc.perform(post("/courses/{uuid}/course-enrollments", UUID.fromString("123e4567-e89b-12d3-a456-426655440001"))
.content("{\n" +
Expand All @@ -45,6 +51,7 @@ void enroll_validRequest_created() throws Exception {
}

@Test
@WithMockUser(username = "student", roles = "STUDENT")
void enroll_relatedResourceIsNotResolvedException_badRequest() throws Exception {
doThrow(RelatedResourceIsNotResolvedException.class).when(handler).handle(any(RegisterStudentToCourseCommand.class));

Expand All @@ -58,6 +65,7 @@ void enroll_relatedResourceIsNotResolvedException_badRequest() throws Exception
}

@Test
@WithMockUser(username = "student", roles = "STUDENT")
void enroll_constraintViolationException_badRequest() throws Exception {
final ConstraintViolationException exception = mock(ConstraintViolationException.class);
doReturn(new HashSet<>()).when(exception).getConstraintViolations();
Expand Down
4 changes: 4 additions & 0 deletions course-enrollments/web/src/test/resources/insert_data.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
DELETE FROM course_enrollment;
DELETE FROM course;
DELETE FROM student;

INSERT INTO course (uuid) VALUES ('123E4567E89B12D3A456426655440001');
INSERT INTO student (username) VALUES ('username');
7 changes: 7 additions & 0 deletions security/test/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencies {
implementation project(':users:users-web')
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'io.rest-assured:rest-assured:4.2.0'
implementation 'io.rest-assured:json-path:4.2.0'
implementation 'io.rest-assured:xml-path:4.2.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.educational.platform.security;

import io.restassured.http.ContentType;
import org.springframework.stereotype.Component;

import static io.restassured.RestAssured.given;

/**
* Represents the component for signing up test user and getting the token. Should be used in API tests.
*/
@Component
public class SignUpHelper {

/**
* Signs up the student and returns the token.
*
* @return token.
*/
public String signUpStudent() {
return given()
.contentType(ContentType.JSON)
.body("{\n" +
" \"role\": \"ROLE_STUDENT\",\n" +
" \"username\": \"username\",\n" +
" \"email\": \"[email protected]\",\n" +
" \"password\": \"password\"\n" +
"}")

.when()
.post("/users/sign-up")
.asString();
}

}
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ findProject(':users:web')?.name = 'users-web'

include 'security:config'
findProject(':security:config')?.name = 'security-config'
include 'security:test'
findProject(':security:test')?.name = 'security-test'

0 comments on commit 78616b0

Please sign in to comment.