fix live test (#2160)

* minor logging fix

* spring security sso

* use basic auth

* use form login

* cleanup

* cleanup

* final cleanup

* second client app for sso

* spring boot bootstrap

* add logic

* cleanup

* add simple controller

* add thymeleaf and security

* minor fix

* minor fix

* add more boot properties

* fix live test

* fix live test
This commit is contained in:
Doha2012 2017-06-26 16:28:35 +02:00 committed by Eugen
parent 52663a6bf5
commit 590cb07df6
3 changed files with 18 additions and 15 deletions

View File

@ -2,15 +2,16 @@ package org.baeldung;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.FilterType;
@Configuration @Configuration
@EnableAutoConfiguration @EnableAutoConfiguration
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.voter.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multiplelogin.*"), @ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.voter.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multipleauthproviders.*"),
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multipleentrypoints.*") }) @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multiplelogin.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multipleentrypoints.*") })
public class Application { public class Application extends SpringBootServletInitializer {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(Application.class, args); SpringApplication.run(Application.class, args);
} }

View File

@ -12,6 +12,7 @@ import org.baeldung.persistence.model.Organization;
import org.baeldung.persistence.model.Privilege; import org.baeldung.persistence.model.Privilege;
import org.baeldung.persistence.model.User; import org.baeldung.persistence.model.User;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
@ -25,6 +26,9 @@ public class SetupData {
@Autowired @Autowired
private OrganizationRepository organizationRepository; private OrganizationRepository organizationRepository;
@Autowired
private PasswordEncoder encoder;
@PostConstruct @PostConstruct
public void init() { public void init() {
initOrganizations(); initOrganizations();
@ -38,14 +42,14 @@ public class SetupData {
// //
final User user1 = new User(); final User user1 = new User();
user1.setUsername("john"); user1.setUsername("john");
user1.setPassword("123"); user1.setPassword(encoder.encode("123"));
user1.setPrivileges(new HashSet<Privilege>(Arrays.asList(privilege1))); user1.setPrivileges(new HashSet<Privilege>(Arrays.asList(privilege1)));
user1.setOrganization(organizationRepository.findByName("FirstOrg")); user1.setOrganization(organizationRepository.findByName("FirstOrg"));
userRepository.save(user1); userRepository.save(user1);
// //
final User user2 = new User(); final User user2 = new User();
user2.setUsername("tom"); user2.setUsername("tom");
user2.setPassword("111"); user2.setPassword(encoder.encode("111"));
user2.setPrivileges(new HashSet<Privilege>(Arrays.asList(privilege1, privilege2))); user2.setPrivileges(new HashSet<Privilege>(Arrays.asList(privilege1, privilege2)));
user2.setOrganization(organizationRepository.findByName("SecondOrg")); user2.setOrganization(organizationRepository.findByName("SecondOrg"));
userRepository.save(user2); userRepository.save(user2);

View File

@ -2,19 +2,17 @@ package org.baeldung.web;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.baeldung.persistence.model.Foo;
import org.junit.Test;
import org.springframework.http.MediaType;
import io.restassured.RestAssured; import io.restassured.RestAssured;
import io.restassured.authentication.FormAuthConfig; import io.restassured.authentication.FormAuthConfig;
import io.restassured.response.Response; import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification; import io.restassured.specification.RequestSpecification;
public class ApplicationLiveTest { import org.baeldung.persistence.model.Foo;
import org.junit.Test;
import org.springframework.http.MediaType;
private final FormAuthConfig formAuthConfig = new FormAuthConfig("http://localhost:8082/spring-security-mvc-boot/login", "username", "password");
public class ApplicationLiveTest {
@Test @Test
public void givenUserWithReadPrivilegeAndHasPermission_whenGetFooById_thenOK() { public void givenUserWithReadPrivilegeAndHasPermission_whenGetFooById_thenOK() {
@ -31,7 +29,7 @@ public class ApplicationLiveTest {
@Test @Test
public void givenUserWithWritePrivilegeAndHasPermission_whenPostFoo_thenOk() { public void givenUserWithWritePrivilegeAndHasPermission_whenPostFoo_thenOk() {
final Response response = givenAuth("tom", "111").contentType(MediaType.APPLICATION_JSON_VALUE).body(new Foo("sample")).post("http://localhost:8082/spring-security-mvc-boot/foos"); final Response response = givenAuth("tom", "111").and().body(new Foo("sample")).and().contentType(MediaType.APPLICATION_JSON_VALUE).post("http://localhost:8082/spring-security-mvc-boot/foos");
assertEquals(201, response.getStatusCode()); assertEquals(201, response.getStatusCode());
assertTrue(response.asString().contains("id")); assertTrue(response.asString().contains("id"));
} }
@ -62,6 +60,6 @@ public class ApplicationLiveTest {
// //
private RequestSpecification givenAuth(String username, String password) { private RequestSpecification givenAuth(String username, String password) {
return RestAssured.given().auth().form(username, password, formAuthConfig); return RestAssured.given().log().uri().auth().form(username, password, new FormAuthConfig("/spring-security-mvc-boot/login","username","password"));
} }
} }