-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(bauhaus cors config) * test (cors : tests skeletons) * test (cors : tests ok) --------- Co-authored-by: Bibonne Fabrice <[email protected]>
- Loading branch information
Showing
4 changed files
with
150 additions
and
18 deletions.
There are no files selected for viewing
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
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
64 changes: 64 additions & 0 deletions
64
src/test/java/fr/insee/rmes/integration/CorsConfigDefaultToAppHostPropertyForOriginTest.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,64 @@ | ||
package fr.insee.rmes.integration; | ||
|
||
import fr.insee.rmes.config.BaseConfigForMvcTests; | ||
import fr.insee.rmes.config.auth.security.CommonSecurityConfiguration; | ||
import fr.insee.rmes.external.services.authentication.stamps.StampsService; | ||
import fr.insee.rmes.webservice.PublicResources; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
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.springframework.test.web.servlet.MockMvc; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WebMvcTest(controllers = PublicResources.class, properties = { | ||
"fr.insee.rmes.bauhaus.cors.allowedOrigin=", | ||
"fr.insee.rmes.bauhaus.appHost=http://localhost:80", | ||
"logging.level.org.springframework.web=debug"}) | ||
@Import({BaseConfigForMvcTests.class, CommonSecurityConfiguration.class}) | ||
class CorsConfigDefaultToAppHostPropertyForOriginTest { | ||
|
||
@Autowired | ||
// Perform mock request on http://localhost:80 | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private StampsService stampsService; | ||
|
||
@ValueSource(strings = { | ||
"http://bauhaus", | ||
"https://localhost:80", | ||
"http://localhost:8080" | ||
}) | ||
@ParameterizedTest | ||
void whenPerformCorsRequest_shouldBeDenied(String origin) throws Exception { | ||
mockMvc.perform(get("/stamps") | ||
.header("Accept", "application/json") | ||
.header("Origin", origin) | ||
).andExpect(status().isForbidden()) | ||
.andExpect(content().string("Invalid CORS request")); | ||
|
||
} | ||
|
||
@Test | ||
void whenPerformNoCorsRequest_shouldBeOK() throws Exception { | ||
mockMvc.perform(get("/stamps") | ||
.header("Accept", "application/json") | ||
).andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
void whenHeaderOriginIsOK_shouldBeOK() throws Exception { | ||
mockMvc.perform(get("/stamps") | ||
.header("Accept", "application/json") | ||
.header("Origin", "http://localhost:80") | ||
).andExpect(status().isOk()); | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
src/test/java/fr/insee/rmes/integration/CorsConfigTest.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,63 @@ | ||
package fr.insee.rmes.integration; | ||
|
||
import fr.insee.rmes.config.BaseConfigForMvcTests; | ||
import fr.insee.rmes.config.auth.security.CommonSecurityConfiguration; | ||
import fr.insee.rmes.external.services.authentication.stamps.StampsService; | ||
import fr.insee.rmes.webservice.PublicResources; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
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.springframework.test.web.servlet.MockMvc; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WebMvcTest(controllers = PublicResources.class, properties = { | ||
"fr.insee.rmes.bauhaus.cors.allowedOrigin=http://localhost:80", | ||
"fr.insee.rmes.bauhaus.appHost=http://myfront:80", | ||
"logging.level.org.springframework.web=debug"}) | ||
@Import({BaseConfigForMvcTests.class, CommonSecurityConfiguration.class}) | ||
class CorsConfigTest { | ||
|
||
@Autowired | ||
// Perform mock request on http://localhost:80 | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private StampsService stampsService; | ||
|
||
@ValueSource(strings = { | ||
"http://bauhaus", | ||
"https://localhost:80", | ||
"http://localhost:8080" | ||
}) | ||
@ParameterizedTest | ||
void whenPerformCorsRequest_shouldBeDenied(String origin) throws Exception { | ||
mockMvc.perform(get("/stamps") | ||
.header("Accept", "application/json") | ||
.header("Origin", origin) | ||
).andExpect(status().isForbidden()) | ||
.andExpect(content().string("Invalid CORS request")); | ||
} | ||
|
||
@Test | ||
void whenPerformNoCorsRequest_shouldBeOK() throws Exception { | ||
mockMvc.perform(get("/stamps") | ||
.header("Accept", "application/json") | ||
).andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
void whenHeaderOriginIsOK_shouldBeOK() throws Exception { | ||
mockMvc.perform(get("/stamps") | ||
.header("Accept", "application/json") | ||
.header("Origin", "http://localhost:80") | ||
).andExpect(status().isOk()); | ||
} | ||
|
||
} |