Fixed the Integration Build Pipeline
This commit is contained in:
parent
24cdf2b672
commit
a26d45e5c0
|
@ -1,7 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-boot-springdoc</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
@ -112,6 +110,8 @@
|
|||
<include>application.properties</include>
|
||||
<include>data.sql</include>
|
||||
<include>schema.sql</include>
|
||||
<include>app.key</include>
|
||||
<include>app.pub</include>
|
||||
</includes>
|
||||
</resource>
|
||||
</resources>
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -11,7 +11,7 @@ public class SecurityTokenApplication {
|
|||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SecurityTokenApplication.class, args);
|
||||
SpringApplication.run(SecurityTokenApplication.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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<String> response = this.restTemplate.getForEntity("http://localhost:" + port + "/v3/api-docs",
|
||||
ResponseEntity<String> response = this.restTemplate.getForEntity("http://localhost:" + port + "/api-docs",
|
||||
String.class);
|
||||
|
||||
assertNotNull(response);
|
||||
|
@ -59,7 +59,7 @@ class OpenApiJwtIntegrationTest
|
|||
{
|
||||
assertNotNull(authenticationApi);
|
||||
|
||||
ResponseEntity<String> response = this.restTemplate.getForEntity("http://localhost:" + port + "/v3/api-docs",
|
||||
ResponseEntity<String> response = this.restTemplate.getForEntity("http://localhost:" + port + "/api-docs",
|
||||
String.class);
|
||||
|
||||
assertNotNull(response);
|
||||
|
@ -75,7 +75,7 @@ class OpenApiJwtIntegrationTest
|
|||
{
|
||||
assertNotNull(authenticationApi);
|
||||
|
||||
ResponseEntity<String> response = this.restTemplate.getForEntity("http://localhost:" + port + "/v3/api-docs",
|
||||
ResponseEntity<String> response = this.restTemplate.getForEntity("http://localhost:" + port + "/api-docs",
|
||||
String.class);
|
||||
|
||||
assertNotNull(response);
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue