From 20a87437fd863dd25f149e0e4b102d01071cf326 Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Wed, 14 Oct 2020 13:04:37 +0300 Subject: [PATCH] Add BookRestController end point tests Signed-off-by: Pekka Helenius --- .../bookstore/web/json/endpoint/BookTest.java | 74 +++++++++++++++++++ .../web/json/endpoint/BooklistTest.java | 71 ++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 bookstore/src/test/java/com/fjordtek/bookstore/web/json/endpoint/BookTest.java create mode 100644 bookstore/src/test/java/com/fjordtek/bookstore/web/json/endpoint/BooklistTest.java diff --git a/bookstore/src/test/java/com/fjordtek/bookstore/web/json/endpoint/BookTest.java b/bookstore/src/test/java/com/fjordtek/bookstore/web/json/endpoint/BookTest.java new file mode 100644 index 0000000..7d63060 --- /dev/null +++ b/bookstore/src/test/java/com/fjordtek/bookstore/web/json/endpoint/BookTest.java @@ -0,0 +1,74 @@ +// Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.web.json.endpoint; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +import org.junit.jupiter.api.MethodOrderer.Alphanumeric; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.security.test.context.support.WithUserDetails; + +import com.fjordtek.bookstore.web.BookStoreTestWebContextBuilder; + +/** + * Web application end point test + *

+ * Test target: single book JSON data page + * + * @author Pekka Helenius + */ + +@TestMethodOrder(Alphanumeric.class) +public class BookTest extends BookStoreTestWebContextBuilder { + + @Test + public void testA_NotGetJsonAsNologin() throws Exception { + book.setPublish(true); + bookRepository.save(book); + /* + * Expect redirect, not 403 + * Unauthorized users: we pretend the resource (end point) does not exist at all + */ + loadPageGet(bookJsonUrl, 302); + } + + @Test + @WithUserDetails("user") + public void testB_GetJsonAsNormalUser() throws Exception { +/* + book.setPublish(true); + bookRepository.save(book); +*/ + loadPageGet(bookJsonUrl, 200); + } + + @Test + @WithUserDetails("admin") + public void testC_GetHiddenJsonAsAdminUser() throws Exception { + book.setPublish(false); + bookRepository.save(book); + + /* + * Is book ISBN present? + */ + assertThat( + loadPageContents(bookJsonUrl, 200).contains(book.getIsbn().toString()) + ).isEqualTo(true); + } + + @Test + @WithUserDetails("user") + public void testD_NotGetHiddenJsonAsNormalUser() throws Exception { +/* + book.setPublish(false); + bookRepository.save(book); +*/ + /* + * Expect redirect, not 403 + * Unauthorized users: we pretend the resource (end point) does not exist at all + */ + loadPageGet(bookJsonUrl, 302); + } + +} \ No newline at end of file diff --git a/bookstore/src/test/java/com/fjordtek/bookstore/web/json/endpoint/BooklistTest.java b/bookstore/src/test/java/com/fjordtek/bookstore/web/json/endpoint/BooklistTest.java new file mode 100644 index 0000000..421e432 --- /dev/null +++ b/bookstore/src/test/java/com/fjordtek/bookstore/web/json/endpoint/BooklistTest.java @@ -0,0 +1,71 @@ +// Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.web.json.endpoint; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +import org.junit.jupiter.api.MethodOrderer.Alphanumeric; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.security.test.context.support.WithUserDetails; + +import com.fjordtek.bookstore.web.BookStoreTestWebContextBuilder; + +/** + * Web application end point test + *

+ * Test target: book JSON data list page + * + * @author Pekka Helenius + */ + +@TestMethodOrder(Alphanumeric.class) +public class BooklistTest extends BookStoreTestWebContextBuilder { + + @Test + public void testA_NotGetJsonListAsNologin() throws Exception { + /* + * Expect redirect, not 403 + * Unauthorized users: we pretend the resource (end point) does not exist at all + */ + loadPageGet(bookJsonListUrl, 302); + } + + @Test + @WithUserDetails("user") + public void testB_GetJsonListAsNormalUser() throws Exception { + loadPageGet(bookJsonListUrl, 200); + } + + @Test + @WithUserDetails("admin") + public void testC_GetHiddenJsonListAsAdminUser() throws Exception { + book.setPublish(false); + bookRepository.save(book); + + /* + * Is book ISBN present? + */ + assertThat( + pageContentsJsonList().contains(book.getIsbn().toString()) + ).isEqualTo(true); + + } + + @Test + @WithUserDetails("user") + public void testD_NotGetHiddenJsonListAsNormalUser() throws Exception { +/* + book.setPublish(false); + bookRepository.save(book); +*/ + /* + * Is book ISBN present? + */ + assertThat( + pageContentsJsonList().contains(book.getIsbn().toString()) + ).isEqualTo(false); + + } + +} \ No newline at end of file