BAEL-797 fixing unit test

This commit is contained in:
tschiman
2017-04-29 20:47:35 -06:00
parent bf0c42c8d6
commit f23b54220f
11 changed files with 104 additions and 233 deletions
@@ -76,7 +76,10 @@ export class BookListComponent implements OnInit {
let book: Book = this.books[bookIndex];
this.httpService.deleteBook(book, this.principal.credentials)
.subscribe(() => {
if (this.selectedBook !== null && this.books[bookIndex].id === this.selectedBook.id) {this.selectedBook = null;}
if (this.selectedBook !== null && this.books[bookIndex].id === this.selectedBook.id) {
this.selectedBook = null;
this.onBookSelected.emit(this.selectedBook);
}
this.books.splice(bookIndex, 1); //remove the book at this index;
this.newBooks.splice(bookIndex, 1); //remove the editing book at this index
@@ -0,0 +1,17 @@
package com.baeldung.spring.cloud.bootstrap.gateway;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.servlet.ErrorPageRegistrar;
import org.springframework.boot.web.servlet.ErrorPageRegistry;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@Component
public class ErrorPageConfig implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/"));
}
}
@@ -29,7 +29,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/*").permitAll()
.antMatchers("/*.js","/*.html","/*.ico", "/*").permitAll()
.antMatchers("/book-service/books").permitAll()
.antMatchers("/zipkin/**").permitAll()
.antMatchers("/eureka/**").hasRole("ADMIN")
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -80,7 +80,7 @@ exports = module.exports = __webpack_require__(13)();
// module
exports.push([module.i, "/* You can add global styles to this file, and also import other style files */\n", ""]);
exports.push([module.i, "/* You can add global styles to this file, and also import other style files */\r\n", ""]);
// exports
File diff suppressed because one or more lines are too long
@@ -1,201 +0,0 @@
package com.baeldung.spring.cloud.bootstrap.gateway;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.http.entity.ContentType;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
public class IntegrationLiveTest {
private TestRestTemplate testRestTemplate = new TestRestTemplate();
private String testUrl = "http://localhost:8080";
@Test
public void testAccess() throws Exception {
ResponseEntity<String> response = testRestTemplate.getForEntity(testUrl + "/book-service/books", String.class);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
Assert.assertNotNull(response.getBody());
//try the protected resource and confirm the redirect to login
response = testRestTemplate.getForEntity(testUrl + "/book-service/books/1", String.class);
Assert.assertEquals(HttpStatus.FOUND, response.getStatusCode());
Assert.assertEquals("http://localhost:8080/login", response.getHeaders().get("Location").get(0));
//login as user/password
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.add("username", "user");
form.add("password", "password");
response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class);
//extract the session from the cookie and propagate it to the next request
String sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0];
HttpHeaders headers = new HttpHeaders();
headers.add("Cookie", sessionCookie);
HttpEntity<String> httpEntity = new HttpEntity<>(headers);
addBook();
//request the protected resource
response = testRestTemplate.exchange(testUrl + "/book-service/books/1", HttpMethod.GET, httpEntity, String.class);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
Assert.assertNotNull(response.getBody());
addRatings();
//request the admin protected resource to determine it is still protected
response = testRestTemplate.exchange(testUrl + "/rating-service/ratings", HttpMethod.GET, httpEntity, String.class);
Assert.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
//login as the admin
form.clear();
form.add("username", "admin");
form.add("password", "admin");
response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class);
//extract the session from the cookie and propagate it to the next request
sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0];
headers = new HttpHeaders();
headers.add("Cookie", sessionCookie);
httpEntity = new HttpEntity<>(headers);
//request the protected resource
response = testRestTemplate.exchange(testUrl + "/rating-service/ratings", HttpMethod.GET, httpEntity, String.class);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
Assert.assertNotNull(response.getBody());
//request the discovery resources as the admin
response = testRestTemplate.exchange(testUrl + "/discovery", HttpMethod.GET, httpEntity, String.class);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
}
private void addRatings() {
//login as user/password
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.add("username", "user");
form.add("password", "password");
ResponseEntity<String> response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class);
//extract the session from the cookie and propagate it to the next request
String sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0];
HttpHeaders headers = new HttpHeaders();
headers.add("Cookie", sessionCookie);
headers.add("ContentType", ContentType.APPLICATION_JSON.getMimeType());
Rating rating = new Rating(1L, 4);
HttpEntity<Rating> httpEntity = new HttpEntity<>(rating, headers);
//request the protected resource
ResponseEntity<Rating> bookResponse = testRestTemplate.postForEntity(testUrl + "/rating-service/ratings", httpEntity, Rating.class);
Assert.assertEquals(HttpStatus.OK, bookResponse.getStatusCode());
Assert.assertEquals(rating.getBookId(), bookResponse.getBody().getBookId());
Assert.assertEquals(rating.getStars(), bookResponse.getBody().getStars());
}
private void addBook(){
//login as user/password
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.add("username", "admin");
form.add("password", "admin");
ResponseEntity<String> response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class);
//extract the session from the cookie and propagate it to the next request
String sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0];
HttpHeaders headers = new HttpHeaders();
headers.add("Cookie", sessionCookie);
headers.add("ContentType", ContentType.APPLICATION_JSON.getMimeType());
Book book = new Book("Baeldung", "How to spring cloud");
HttpEntity<Book> httpEntity = new HttpEntity<>(book, headers);
//request the protected resource
ResponseEntity<Book> bookResponse = testRestTemplate.postForEntity(testUrl + "/book-service/books", httpEntity, Book.class);
Assert.assertEquals(HttpStatus.OK, bookResponse.getStatusCode());
Assert.assertEquals(book.getAuthor(), bookResponse.getBody().getAuthor());
Assert.assertEquals(book.getTitle(), bookResponse.getBody().getTitle());
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Book {
private Long id;
private String author;
private String title;
public Book() {
}
public Book(String author, String title) {
this.author = author;
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Rating {
private Long id;
private Long bookId;
private int stars;
public Rating() {
}
public Rating(Long bookId, int stars) {
this.bookId = bookId;
this.stars = stars;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getBookId() {
return bookId;
}
public void setBookId(Long bookId) {
this.bookId = bookId;
}
public int getStars() {
return stars;
}
public void setStars(int stars) {
this.stars = stars;
}
}
}
@@ -1,28 +1,29 @@
package com.baeldung.spring.cloud.bootstrap.gateway;
import static io.restassured.RestAssured.config;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.restassured.RestAssured;
import io.restassured.authentication.FormAuthConfig;
import io.restassured.config.RedirectConfig;
import io.restassured.config.SessionConfig;
import io.restassured.filter.session.SessionFilter;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import static io.restassured.RestAssured.config;
public class LiveTest {
private final String ROOT_URI = "http://localhost:8080";
private final FormAuthConfig formConfig = new FormAuthConfig("/login", "username", "password");
SessionFilter sessionFilter;
@Before
public void setup() {
RestAssured.config = config().redirect(RedirectConfig.redirectConfig()
.followRedirects(false));
RestAssured.config = config()
.redirect(RedirectConfig.redirectConfig().followRedirects(false))
.sessionConfig(new SessionConfig().sessionIdName("SESSION"));
}
@Test
@@ -35,15 +36,16 @@ public class LiveTest {
@Test
public void whenAccessProtectedResourceWithoutLogin_thenRedirectToLogin() {
final Response response = RestAssured.get(ROOT_URI + "/book-service/books/1");
Assert.assertEquals(HttpStatus.FOUND.value(), response.getStatusCode());
Assert.assertEquals("http://localhost:8080/login", response.getHeader("Location"));
Assert.assertEquals(HttpStatus.UNAUTHORIZED.value(), response.getStatusCode());
}
@Test
public void whenAccessProtectedResourceAfterLogin_thenSuccess() {
SessionData sessionData = login();
final Response response = RestAssured.given()
.auth()
.form("user", "password", formConfig)
.auth().preemptive().basic("user", "password")
.header("X-XSRF-TOKEN", sessionData.getCsrf())
.filter(sessionFilter)
.get(ROOT_URI + "/book-service/books/1");
Assert.assertEquals(HttpStatus.OK.value(), response.getStatusCode());
Assert.assertNotNull(response.getBody());
@@ -51,9 +53,11 @@ public class LiveTest {
@Test
public void whenAccessAdminProtectedResource_thenForbidden() {
SessionData sessionData = login();
final Response response = RestAssured.given()
.auth()
.form("user", "password", formConfig)
.auth().preemptive().basic("user", "password")
.header("X-XSRF-TOKEN", sessionData.getCsrf())
.filter(sessionFilter)
.get(ROOT_URI + "/rating-service/ratings");
Assert.assertEquals(HttpStatus.FORBIDDEN.value(), response.getStatusCode());
@@ -61,9 +65,11 @@ public class LiveTest {
@Test
public void whenAdminAccessProtectedResource_thenSuccess() {
SessionData sessionData = login();
final Response response = RestAssured.given()
.auth()
.form("admin", "admin", formConfig)
.auth().preemptive().basic("admin", "admin")
.header("X-XSRF-TOKEN", sessionData.getCsrf())
.filter(sessionFilter)
.get(ROOT_URI + "/rating-service/ratings");
Assert.assertEquals(HttpStatus.OK.value(), response.getStatusCode());
Assert.assertNotNull(response.getBody());
@@ -71,9 +77,11 @@ public class LiveTest {
@Test
public void whenAdminAccessDiscoveryResource_thenSuccess() {
SessionData sessionData = login();
final Response response = RestAssured.given()
.auth()
.form("admin", "admin", formConfig)
.auth().preemptive().basic("admin", "admin")
.header("X-XSRF-TOKEN", sessionData.getCsrf())
.filter(sessionFilter)
.get(ROOT_URI + "/discovery");
Assert.assertEquals(HttpStatus.OK.value(), response.getStatusCode());
}
@@ -83,10 +91,13 @@ public class LiveTest {
final Rating rating = new Rating(1L, 4);
SessionData sessionData = login();
// request the protected resource
final Response ratingResponse = RestAssured.given()
.auth()
.form("admin", "admin", formConfig)
.auth().preemptive().basic("admin", "admin")
.header("X-XSRF-TOKEN", sessionData.getCsrf())
.filter(sessionFilter)
.and()
.contentType(ContentType.JSON)
.body(rating)
@@ -101,10 +112,13 @@ public class LiveTest {
public void whenAddnewBook_thenSuccess() {
final Book book = new Book("Baeldung", "How to spring cloud");
SessionData sessionData = login();
// request the protected resource
final Response bookResponse = RestAssured.given()
.auth()
.form("admin", "admin", formConfig)
.auth().preemptive().basic("admin", "admin")
.header("X-XSRF-TOKEN", sessionData.getCsrf())
.filter(sessionFilter)
.and()
.contentType(ContentType.JSON)
.body(book)
@@ -195,4 +209,41 @@ public class LiveTest {
}
}
private SessionData login() {
sessionFilter = new SessionFilter();
Response getLoginResponse =
RestAssured.given().
filter(sessionFilter).
when().
get("/").
then().
extract().response();
return new SessionData(getLoginResponse.cookie("XSRF-TOKEN"), sessionFilter.getSessionId());
}
private class SessionData {
private String csrf;
private String session;
public SessionData(String csrf, String session) {
this.csrf = csrf;
this.session = session;
}
public String getCsrf() {
return csrf;
}
public void setCsrf(String csrf) {
this.csrf = csrf;
}
public String getSession() {
return session;
}
public void setSession(String session) {
this.session = session;
}
}
}