-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ : add IT tests using scripts with mongo testcontainer
- Loading branch information
1 parent
4943c60
commit b9e4442
Showing
4 changed files
with
271 additions
and
39 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/test/java/io/codeka/gaia/modules/controller/ModuleRestControllerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package io.codeka.gaia.modules.controller; | ||
|
||
import io.codeka.gaia.test.MongoContainer; | ||
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.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import static org.hamcrest.Matchers.*; | ||
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.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
/** | ||
* Simple integration test that validates the security configuration of the TeamsRestController, and its http routes | ||
*/ | ||
@SpringBootTest | ||
@DirtiesContext | ||
@Testcontainers | ||
@AutoConfigureMockMvc | ||
@WithMockUser(value = "admin", roles = "ADMIN") | ||
class ModuleRestControllerIT { | ||
|
||
@Container | ||
private static MongoContainer mongoContainer = new MongoContainer() | ||
.withScript("src/test/resources/db/00_team.js") | ||
.withScript("src/test/resources/db/10_user.js") | ||
.withScript("src/test/resources/db/20_module.js"); | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@BeforeEach | ||
void setup() { | ||
mongoContainer.resetDatabase(); | ||
} | ||
|
||
@Test | ||
@WithMockUser("Mary J") | ||
void findAllModules_shouldReturnLimitedModules_forStandardUsers() throws Exception { | ||
mockMvc.perform(get("/api/modules")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$", hasSize(1))) | ||
.andExpect(jsonPath("$[0]name", is("terraform-docker-mongo-limited"))) | ||
.andExpect(jsonPath("$[0]authorizedTeams..id", contains("Not Ze Team"))); | ||
} | ||
|
||
@Test | ||
void findAllModules_shouldReturnAllModules_forAdmin() throws Exception { | ||
mockMvc.perform(get("/api/modules")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$", hasSize(2))) | ||
.andExpect(jsonPath("$[*]name", contains("terraform-docker-mongo", "terraform-docker-mongo-limited"))) | ||
.andExpect(jsonPath("$..authorizedTeams..id", contains("Ze Team", "Not Ze Team"))); | ||
} | ||
|
||
@Test | ||
@WithMockUser("Mary J") | ||
void findModule_shouldReturnModule_forStandardUsers() throws Exception { | ||
mockMvc.perform(get("/api/modules/845543d0-20a5-466c-8978-33c9a4661606")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.name", is("terraform-docker-mongo-limited"))); | ||
} | ||
|
||
@Test | ||
@WithMockUser("Mary J") | ||
void findModule_shouldNotReturnModuleOfOtherTeams_forStandardUsers() throws Exception { | ||
mockMvc.perform(get("/api/modules/e01f9925-a559-45a2-8a55-f93dc434c676")) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
void findModule_shouldReturnModules_forAdmin() throws Exception { | ||
mockMvc.perform(get("/api/modules/e01f9925-a559-45a2-8a55-f93dc434c676")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.name", is("terraform-docker-mongo"))); | ||
} | ||
|
||
@Test | ||
void findModule_shouldReturnModulesOfOtherTeams_forAdmin() throws Exception { | ||
mockMvc.perform(get("/api/modules/845543d0-20a5-466c-8978-33c9a4661606")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.name", is("terraform-docker-mongo-limited"))); | ||
} | ||
|
||
@Test | ||
@WithMockUser("Mary J") | ||
void saveModule_shouldNotBeAccessible_forStandardUsers() throws Exception { | ||
mockMvc.perform(put("/api/modules/test") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content("{\"name\":\"module-test\"}")) | ||
.andExpect(status().isForbidden()); | ||
} | ||
|
||
@Test | ||
void saveModule_shouldBeAccessible_forAdmin() throws Exception { | ||
mockMvc.perform(put("/api/modules/test") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content("{\"name\":\"module-test\"}")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.id", notNullValue())) | ||
.andExpect(jsonPath("$.name", is("module-test"))); | ||
} | ||
|
||
} |
115 changes: 115 additions & 0 deletions
115
src/test/java/io/codeka/gaia/stacks/controller/StackRestControllerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package io.codeka.gaia.stacks.controller; | ||
|
||
import io.codeka.gaia.test.MongoContainer; | ||
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.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import static org.hamcrest.Matchers.*; | ||
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; | ||
|
||
/** | ||
* Simple integration test that validates the security configuration of the TeamsRestController, and its http routes | ||
*/ | ||
@SpringBootTest | ||
@DirtiesContext | ||
@Testcontainers | ||
@AutoConfigureMockMvc | ||
@WithMockUser(value = "admin", roles = "ADMIN") | ||
class StackRestControllerIT { | ||
|
||
@Container | ||
private static MongoContainer mongoContainer = new MongoContainer() | ||
.withScript("src/test/resources/db/00_team.js") | ||
.withScript("src/test/resources/db/10_user.js") | ||
.withScript("src/test/resources/db/20_module.js") | ||
.withScript("src/test/resources/db/30_stack.js"); | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@BeforeEach | ||
void setup() { | ||
mongoContainer.resetDatabase(); | ||
} | ||
|
||
@Test | ||
@WithMockUser("Mary J") | ||
void listStacks_shouldReturnLimitedStacks_forStandardUsers() throws Exception { | ||
mockMvc.perform(get("/api/stacks")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$", hasSize(1))) | ||
.andExpect(jsonPath("$[0]name", is("mongo-instance-limited"))) | ||
.andExpect(jsonPath("$[0]ownerTeam.id", is("Not Ze Team"))); | ||
} | ||
|
||
@Test | ||
void listStacks_shouldReturnAllStacks_forAdmin() throws Exception { | ||
mockMvc.perform(get("/api/stacks")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$", hasSize(3))) | ||
.andExpect(jsonPath("$..name", contains("mongo-instance-1", "mongo-instance-2", "mongo-instance-limited"))) | ||
.andExpect(jsonPath("$..ownerTeam.id", contains("Ze Team", "Ze Team", "Not Ze Team"))); | ||
} | ||
|
||
@Test | ||
@WithMockUser("Mary J") | ||
void getStacks_shouldReturnStack_forStandardUsers() throws Exception { | ||
mockMvc.perform(get("/api/stacks/845543d0-20a5-466c-8978-33c9a4661606")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.name", is("mongo-instance-limited"))) | ||
.andExpect(jsonPath("$.ownerTeam.id", is("Not Ze Team"))) | ||
.andExpect(jsonPath("$.estimatedRunningCost", is(0))); | ||
} | ||
|
||
@Test | ||
@WithMockUser("Mary J") | ||
void getStacks_shouldNotReturnStackOfOtherTeams_forStandardUsers() throws Exception { | ||
mockMvc.perform(get("/api/stacks/5a215b6b-fe53-4afa-85f0-a10175a7f264")) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
void getStacks_shouldReturnStack_forAdmin() throws Exception { | ||
mockMvc.perform(get("/api/stacks/5a215b6b-fe53-4afa-85f0-a10175a7f264")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.name", is("mongo-instance-1"))) | ||
.andExpect(jsonPath("$.ownerTeam.id", is("Ze Team"))) | ||
.andExpect(jsonPath("$.estimatedRunningCost", is(0))); | ||
} | ||
|
||
@Test | ||
@WithMockUser("Mary J") | ||
void saveModule_shouldBeAccessible() throws Exception { | ||
mockMvc.perform(post("/api/stacks") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content("{\"name\":\"stack-test\"}")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.id", notNullValue())) | ||
.andExpect(jsonPath("$.ownerTeam.id", is("Not Ze Team"))) | ||
.andExpect(jsonPath("$.createdBy.username", is("Mary J"))) | ||
.andExpect(jsonPath("$.createdAt", notNullValue())); | ||
} | ||
|
||
@Test | ||
@WithMockUser("Mary J") | ||
void updateModule_shouldBeAccessible() throws Exception { | ||
mockMvc.perform(put("/api/stacks/test") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content("{\"name\":\"stack-test\"}")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.updatedBy.username", is("Mary J"))) | ||
.andExpect(jsonPath("$.updatedAt", notNullValue())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.