diff options
author | Georg Pfuetzenreuter | 2022-05-13 20:09:42 +0200 |
---|---|---|
committer | Georg Pfuetzenreuter | 2022-05-13 20:09:42 +0200 |
commit | 2ff99d29bc8873afa9c7cd9da57a7a7cf2072ea5 (patch) | |
tree | 4c460f1c5db9e1df06acefde5347a154b32010e8 /src/test/java | |
parent | bf6f91520f9a0ebb2a1ba2182a2ddc3043325c37 (diff) | |
download | pubsh-web-2ff99d29bc8873afa9c7cd9da57a7a7cf2072ea5.tar.gz pubsh-web-2ff99d29bc8873afa9c7cd9da57a7a7cf2072ea5.tar.bz2 pubsh-web-2ff99d29bc8873afa9c7cd9da57a7a7cf2072ea5.zip |
Init unit tests
Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
Diffstat (limited to 'src/test/java')
-rw-r--r-- | src/test/java/net/libertacasa/pubsh/web/WebApplicationTest.java | 81 | ||||
-rw-r--r-- | src/test/java/net/libertacasa/pubsh/web/WebApplicationTests.java | 13 |
2 files changed, 81 insertions, 13 deletions
diff --git a/src/test/java/net/libertacasa/pubsh/web/WebApplicationTest.java b/src/test/java/net/libertacasa/pubsh/web/WebApplicationTest.java new file mode 100644 index 0000000..70a69b3 --- /dev/null +++ b/src/test/java/net/libertacasa/pubsh/web/WebApplicationTest.java @@ -0,0 +1,81 @@ +package net.libertacasa.pubsh.web; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +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.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import com.c4_soft.springaddons.security.oauth2.test.annotations.Claims; +import com.c4_soft.springaddons.security.oauth2.test.annotations.OpenIdClaims; +import com.c4_soft.springaddons.security.oauth2.test.annotations.StringClaim; +import com.c4_soft.springaddons.security.oauth2.test.annotations.keycloak.WithMockKeycloakAuth; + +//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest +@AutoConfigureMockMvc +class WebApplicationTest { + + @Test + void contextLoads() { + } + + @Autowired + private MockMvc mvc; + + @Test + public void getRoot() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/")) + .andExpect(status().isFound()) + .andExpect(redirectedUrl("/portal")); + } + + @Test + public void getPortalNoAuth() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/portal")) + .andExpect(status().isFound()) + .andExpect(redirectedUrl("/sso/login")); + } + + @Test + public void getAdminNoAuth() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/admin")) + .andExpect(status().isFound()) + .andExpect(redirectedUrl("/sso/login")); + } + + @Test + @WithMockKeycloakAuth("TotallyLegitUserWithZeroAdministrativePermissions") + public void getAdminWrongAuth() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/admin")) + .andExpect(status().isForbidden()); + } + + @Test + @WithMockKeycloakAuth( + authorities = { "devel-user" }, + claims = @OpenIdClaims( + sub = "12345", + email = "regular-user@example.com", + emailVerified = true, + //nickName = "TotallyLegitUserWithSuperPowers", + //preferredUsername = "TotallyLegitUserWithSuperPowers", + otherClaims = @Claims(stringClaims = @StringClaim(name = "username", value = "regular-user")))) + public void getPortalWithAuth() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/portal")) + .andExpect(status().isOk()) + .andExpect(content().string(startsWith("<!DOCTYPE html>"))) + .andExpect(content().string(containsString("Hello, <span>regular-user</span>."))) + .andExpect(content().string(containsString("Generate new throw-away shell:"))); + } + +} diff --git a/src/test/java/net/libertacasa/pubsh/web/WebApplicationTests.java b/src/test/java/net/libertacasa/pubsh/web/WebApplicationTests.java deleted file mode 100644 index ffb21f0..0000000 --- a/src/test/java/net/libertacasa/pubsh/web/WebApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.libertacasa.pubsh.web; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class WebApplicationTests { - - @Test - void contextLoads() { - } - -} |