Skip to content

Commit

Permalink
βœ… : fix tests after security process rework
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubuisson committed Apr 9, 2020
1 parent eb6dbbf commit 81e78a3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.hamcrest.Matchers.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
Expand Down Expand Up @@ -95,6 +96,7 @@ void findModule_shouldReturnModulesOfOtherTeams_forAdmin() throws Exception {
@WithMockUser("Mary J")
void saveModule_shouldNotBeAccessible_forStandardUsers() throws Exception {
mockMvc.perform(put("/api/modules/e01f9925-a559-45a2-8a55-f93dc434c676")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"module-test\"," +
"\"terraformImage\":{\"repository\":\"hashicorp/terraform\",\"tag\":\"latest\"}," +
Expand All @@ -105,6 +107,7 @@ void saveModule_shouldNotBeAccessible_forStandardUsers() throws Exception {
@Test
void saveModule_shouldBeAccessible_forAdmin() throws Exception {
mockMvc.perform(put("/api/modules/e01f9925-a559-45a2-8a55-f93dc434c676")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"module-test\"," +
"\"terraformImage\":{\"repository\":\"hashicorp/terraform\",\"tag\":\"latest\"}," +
Expand All @@ -117,6 +120,7 @@ void saveModule_shouldBeAccessible_forAdmin() throws Exception {
@Test
void saveModule_shouldValidateModuleContent_forBlankFields() throws Exception {
mockMvc.perform(put("/api/modules/stacks")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
// empty module
.content("{}"))
Expand All @@ -128,6 +132,7 @@ void saveModule_shouldValidateModuleContent_forBlankFields() throws Exception {
@Test
void saveModule_shouldValidateModuleVariables_forBlankFields() throws Exception {
mockMvc.perform(put("/api/modules/stacks")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
// empty variable name
.content("{\"variables\":[{\"name\":\" \"}]}"))
Expand All @@ -138,6 +143,7 @@ void saveModule_shouldValidateModuleVariables_forBlankFields() throws Exception
@Test
void saveModule_shouldValidateTerraformImage_forBlankFields() throws Exception {
mockMvc.perform(put("/api/modules/stacks")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
// empty terraform image
.content("{\"terraformImage\":{\"repository\":\" \",\"tag\":\" \"}}"))
Expand All @@ -149,6 +155,7 @@ void saveModule_shouldValidateTerraformImage_forBlankFields() throws Exception {
@Test
void saveModule_shouldValidateTerraformImage_forWrongRepositoryName() throws Exception {
mockMvc.perform(put("/api/modules/stacks")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
// empty terraform image
.content("{\"terraformImage\":{\"repository\":\"wrong+pattern+image\",\"tag\":\"shame\"}}"))
Expand All @@ -160,11 +167,12 @@ void saveModule_shouldValidateTerraformImage_forWrongRepositoryName() throws Exc
@Test
void createModule_shouldSaveAModule() throws Exception {
mockMvc.perform(post("/api/modules")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
// empty variable name
.content("{\"name\":\"new-module\"}"))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.name", is("new-module")));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.hamcrest.Matchers.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -92,6 +93,7 @@ void getStacks_shouldReturnStack_forAdmin() throws Exception {
@WithMockUser("Mary J")
void saveStack_shouldBeAccessible_forStandardUser() throws Exception {
mockMvc.perform(post("/api/stacks")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"stack-test\", \"moduleId\": \"e01f9925-a559-45a2-8a55-f93dc434c676\"}"))
.andExpect(status().isOk())
Expand All @@ -105,6 +107,7 @@ void saveStack_shouldBeAccessible_forStandardUser() throws Exception {
@WithMockUser("Mary J")
void updateStack_shouldBeAccessible_forStandardUser() throws Exception {
mockMvc.perform(put("/api/stacks/test")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"stack-test\", \"moduleId\": \"e01f9925-a559-45a2-8a55-f93dc434c676\"}"))
.andExpect(status().isOk())
Expand All @@ -115,6 +118,7 @@ void updateStack_shouldBeAccessible_forStandardUser() throws Exception {
@Test
void saveStack_shouldValidateStackContent() throws Exception {
mockMvc.perform(post("/api/stacks")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
// empty name and module id
.content("{}"))
Expand All @@ -126,6 +130,7 @@ void saveStack_shouldValidateStackContent() throws Exception {
@Test
void saveStack_shouldValidateStackContent_forBlankFields() throws Exception {
mockMvc.perform(post("/api/stacks")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
// empty name and module id
.content("{\"name\":\" \",\"moduleId\":\" \"}"))
Expand All @@ -137,6 +142,7 @@ void saveStack_shouldValidateStackContent_forBlankFields() throws Exception {
@Test
void updateStack_shouldValidateStackContent() throws Exception {
mockMvc.perform(put("/api/stacks/test")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
// empty name and module id
.content("{\"name\":\"\", \"moduleId\": \"\"}"))
Expand All @@ -148,6 +154,7 @@ void updateStack_shouldValidateStackContent() throws Exception {
@Test
void saveStack_shouldValidateStackVariables() throws Exception {
mockMvc.perform(post("/api/stacks")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
// null variable
.content("{\"name\":\"stack-test\", \"moduleId\": \"b39ccd07-80f5-455f-a6b3-b94f915738c4\", \"variableValues\":{}}"))
Expand All @@ -158,6 +165,7 @@ void saveStack_shouldValidateStackVariables() throws Exception {
@Test
void saveStack_shouldWork_stackIsValid() throws Exception {
mockMvc.perform(post("/api/stacks")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
// empty name
.content("{\"name\":\"stack-test\", \"moduleId\": \"b39ccd07-80f5-455f-a6b3-b94f915738c4\", \"variableValues\":{\"mongo_container_name\":\"someContainerName\"}}"))
Expand All @@ -167,11 +175,12 @@ void saveStack_shouldWork_stackIsValid() throws Exception {
@Test
void saveStack_shouldValidateStackVariablesRegex() throws Exception {
mockMvc.perform(post("/api/stacks")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
// null variable
.content("{\"name\":\"stack-test\", \"moduleId\": \"b39ccd07-80f5-455f-a6b3-b94f915738c4\", \"variableValues\":{\"mongo_container_name\":\"someContainerName\",\"mongo_exposed_port\":\"toto\"}}"))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.message", is("variables should match the regex")));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
Expand Down Expand Up @@ -74,6 +75,7 @@ void users_shouldBeExposed_atSpecificUrl() throws Exception {
@Test
void saveUser_shouldBeExposed_atSpecificUrl() throws Exception {
mockMvc.perform(put("/api/users/test")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content("{\"username\":\"Bob\"}"))
.andExpect(status().isOk())
Expand All @@ -94,6 +96,7 @@ void users_canBeChangedOfTeam() throws Exception {

// when
mockMvc.perform(put("/api/users/Darth Vader")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content("{\"username\": \"Darth Vader\",\"team\": {\"id\": \"Sith\"}}"))
.andDo(print())
Expand All @@ -111,4 +114,4 @@ void users_canBeChangedOfTeam() throws Exception {
mongoContainer.resetDatabase();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class UserRepositoryIT {
assertThat(result)
.isNotNull
.isPresent
.hasValueSatisfying { assertThat(it).isEqualTo("Samantha Carter") }
.hasValueSatisfying { assertThat(it.username).isEqualTo("Samantha Carter") }
}

}

0 comments on commit 81e78a3

Please sign in to comment.