From a26d45e5c0d481a38d533cb056b7ea274bb0febd Mon Sep 17 00:00:00 2001 From: Bhaskara Navuluri Date: Mon, 6 Jun 2022 20:35:38 +0530 Subject: [PATCH] Fixed the Integration Build Pipeline --- .../spring-boot-springdoc/pom.xml | 6 +- .../baeldung/jwt/SecurityConfiguration.java | 4 +- .../jwt/SecurityTokenApplication.java | 4 +- .../jwt/OpenApiJwtIntegrationTest.java | 8 +- .../MongoAuthApplicationIntegrationTest.java | 118 ------------------ 5 files changed, 11 insertions(+), 129 deletions(-) delete mode 100644 spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/mongoauth/MongoAuthApplicationIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-springdoc/pom.xml b/spring-boot-modules/spring-boot-springdoc/pom.xml index 88e616119d..18b1774920 100644 --- a/spring-boot-modules/spring-boot-springdoc/pom.xml +++ b/spring-boot-modules/spring-boot-springdoc/pom.xml @@ -1,7 +1,5 @@ - + 4.0.0 spring-boot-springdoc 0.0.1-SNAPSHOT @@ -112,6 +110,8 @@ application.properties data.sql schema.sql + app.key + app.pub diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/jwt/SecurityConfiguration.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/jwt/SecurityConfiguration.java index 6b42a8f1bb..216740f979 100644 --- a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/jwt/SecurityConfiguration.java +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/jwt/SecurityConfiguration.java @@ -52,8 +52,8 @@ public class SecurityConfiguration { //@formatter:off return http .authorizeHttpRequests(authorizeRequests -> authorizeRequests - .antMatchers("/api/auth/**", "/swagger-ui.html", "/swagger-ui/**", "/v3/api-docs/**", "/webjars/**", - "/swagger-ui/index.html") + .antMatchers("/api/auth/**", "/swagger-ui-custom.html" ,"/swagger-ui.html", "/swagger-ui/**", "/v3/api-docs/**", "/webjars/**", + "/swagger-ui/index.html","/api-docs/**") .permitAll() .anyRequest() .authenticated()) diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/jwt/SecurityTokenApplication.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/jwt/SecurityTokenApplication.java index 4c0c4f01d8..ba396e1703 100644 --- a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/jwt/SecurityTokenApplication.java +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/jwt/SecurityTokenApplication.java @@ -11,7 +11,7 @@ public class SecurityTokenApplication { * @param args */ public static void main(String[] args) { - SpringApplication.run(SecurityTokenApplication.class, args); + SpringApplication.run(SecurityTokenApplication.class); } - } + diff --git a/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/jwt/OpenApiJwtIntegrationTest.java b/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/jwt/OpenApiJwtIntegrationTest.java index 1ea88d14fa..fecd4101eb 100644 --- a/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/jwt/OpenApiJwtIntegrationTest.java +++ b/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/jwt/OpenApiJwtIntegrationTest.java @@ -30,7 +30,7 @@ class OpenApiJwtIntegrationTest { assertNotNull(authenticationApi); - String response = this.restTemplate.getForObject("http://localhost:" + port + "/swagger-ui.html", String.class); + String response = this.restTemplate.getForObject("http://localhost:" + port + "/swagger-ui/index.html", String.class); assertNotNull(response); assertTrue(response.contains("Swagger UI")); @@ -43,7 +43,7 @@ class OpenApiJwtIntegrationTest { assertNotNull(authenticationApi); - ResponseEntity response = this.restTemplate.getForEntity("http://localhost:" + port + "/v3/api-docs", + ResponseEntity response = this.restTemplate.getForEntity("http://localhost:" + port + "/api-docs", String.class); assertNotNull(response); @@ -59,7 +59,7 @@ class OpenApiJwtIntegrationTest { assertNotNull(authenticationApi); - ResponseEntity response = this.restTemplate.getForEntity("http://localhost:" + port + "/v3/api-docs", + ResponseEntity response = this.restTemplate.getForEntity("http://localhost:" + port + "/api-docs", String.class); assertNotNull(response); @@ -75,7 +75,7 @@ class OpenApiJwtIntegrationTest { assertNotNull(authenticationApi); - ResponseEntity response = this.restTemplate.getForEntity("http://localhost:" + port + "/v3/api-docs", + ResponseEntity response = this.restTemplate.getForEntity("http://localhost:" + port + "/api-docs", String.class); assertNotNull(response); diff --git a/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/mongoauth/MongoAuthApplicationIntegrationTest.java b/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/mongoauth/MongoAuthApplicationIntegrationTest.java deleted file mode 100644 index b7994cad9e..0000000000 --- a/spring-security-modules/spring-security-web-boot-3/src/test/java/com/baeldung/mongoauth/MongoAuthApplicationIntegrationTest.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.baeldung.mongoauth; - -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; -import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import java.util.Collections; -import java.util.HashSet; - -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.data.mongodb.core.MongoTemplate; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.context.WebApplicationContext; - -import com.baeldung.mongoauth.domain.Role; -import com.baeldung.mongoauth.domain.User; -import com.baeldung.mongoauth.domain.UserRole; - -@SpringBootTest(classes = { MongoAuthApplication.class }) -@AutoConfigureMockMvc -@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) -class MongoAuthApplicationIntegrationTest { - - @Autowired - private WebApplicationContext context; - - @Autowired - private MongoTemplate mongoTemplate; - - @Autowired - private BCryptPasswordEncoder bCryptPasswordEncoder; - - private MockMvc mvc; - - private static final String USER_NAME = "user@gmail.com"; - private static final String ADMIN_NAME = "admin@gmail.com"; - private static final String PASSWORD = "password"; - - @BeforeEach - public void setup() { - - setUp(); - - mvc = MockMvcBuilders.webAppContextSetup(context) - .apply(springSecurity()) - .build(); - } - - private void setUp() { - Role roleUser = new Role(); - roleUser.setName("ROLE_USER"); - mongoTemplate.save(roleUser); - - User user = new User(); - user.setUsername(USER_NAME); - user.setPassword(bCryptPasswordEncoder.encode(PASSWORD)); - - UserRole userRole = new UserRole(); - userRole.setRole(roleUser); - user.setUserRoles(new HashSet<>(Collections.singletonList(userRole))); - mongoTemplate.save(user); - - User admin = new User(); - admin.setUsername(ADMIN_NAME); - admin.setPassword(bCryptPasswordEncoder.encode(PASSWORD)); - - Role roleAdmin = new Role(); - roleAdmin.setName("ROLE_ADMIN"); - mongoTemplate.save(roleAdmin); - - UserRole adminRole = new UserRole(); - adminRole.setRole(roleAdmin); - admin.setUserRoles(new HashSet<>(Collections.singletonList(adminRole))); - mongoTemplate.save(admin); - } - - @Test - void givenUserCredentials_whenInvokeUserAuthorizedEndPoint_thenReturn200() throws Exception { - mvc.perform(get("/user").with(httpBasic(USER_NAME, PASSWORD))) - .andExpect(status().isOk()); - } - - @Test - void givenUserNotExists_whenInvokeEndPoint_thenReturn401() throws Exception { - mvc.perform(get("/user").with(httpBasic("not_existing_user", "password"))) - .andExpect(status().isUnauthorized()); - } - - @Test - void givenUserExistsAndWrongPassword_whenInvokeEndPoint_thenReturn401() throws Exception { - mvc.perform(get("/user").with(httpBasic(USER_NAME, "wrong_password"))) - .andExpect(status().isUnauthorized()); - } - - @Test - void givenUserCredentials_whenInvokeAdminAuthorizedEndPoint_thenReturn403() throws Exception { - mvc.perform(get("/admin").with(httpBasic(USER_NAME, PASSWORD))) - .andExpect(status().isForbidden()); - } - - @Test - void givenAdminCredentials_whenInvokeAdminAuthorizedEndPoint_thenReturn200() throws Exception { - mvc.perform(get("/admin").with(httpBasic(ADMIN_NAME, PASSWORD))) - .andExpect(status().isOk()); - - mvc.perform(get("/user").with(httpBasic(ADMIN_NAME, PASSWORD))) - .andExpect(status().isOk()); - } - -}