From 74d8addec633202c18d8308d5f64f0b39cb0eb6f Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Fri, 16 Oct 2020 22:51:44 +0300 Subject: [PATCH] Add REST API end point tests Signed-off-by: Pekka Helenius --- .../web/rest/endpoint/IndexTest.java | 33 ++++++ .../web/rest/endpoint/RestAddTest.java | 107 ++++++++++++++++++ .../web/rest/endpoint/RestDeleteTest.java | 44 +++++++ .../web/rest/endpoint/RestEditTest.java | 49 ++++++++ .../web/rest/endpoint/RootUriTests.java | 86 ++++++++++++++ 5 files changed, 319 insertions(+) create mode 100644 bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/IndexTest.java create mode 100644 bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestAddTest.java create mode 100644 bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestDeleteTest.java create mode 100644 bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestEditTest.java create mode 100644 bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RootUriTests.java diff --git a/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/IndexTest.java b/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/IndexTest.java new file mode 100644 index 0000000..2d1e3f3 --- /dev/null +++ b/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/IndexTest.java @@ -0,0 +1,33 @@ +// Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.web.rest.endpoint; + +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; + +/** + * + * TODO: N/A + * + * @author Pekka Helenius + */ + +@TestMethodOrder(Alphanumeric.class) +public class IndexTest extends BookStoreTestWebContextBuilder { + + @Test + @WithUserDetails("admin") + public void testA_getIndexPageExpectRedirectAsAdminUser() throws Exception { + loadPageGet(restApiBaseUrl, 302); + } + + @Test + public void testB_getIndexPageExpectUnauthorizedAsNologin() throws Exception { + loadPageGet(restApiBaseUrl, 401); + } + +} \ No newline at end of file diff --git a/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestAddTest.java b/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestAddTest.java new file mode 100644 index 0000000..a884758 --- /dev/null +++ b/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestAddTest.java @@ -0,0 +1,107 @@ +// Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.web.rest.endpoint; + +import org.junit.Test; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithUserDetails; + +import com.fjordtek.bookstore.web.BookStoreTestWebContextBuilder; + +/** + * + * TODO: N/A + * + * @author Pekka Helenius + */ + +public class RestAddTest extends BookStoreTestWebContextBuilder { + + @Test + @WithUserDetails("salesmanager") + public void testA_postAddBookExpectForbiddenAsMarketingUser() throws Exception { + loadPagePost( + restApiBaseUrl + env.getProperty("page.url.restapi.books"), 403, + MediaType.APPLICATION_JSON, + "{\"title\":\"Halo: The Flood\"," + + "\"year\":2003," + + "\"isbn\":\"0345459-210\"," + + "\"price\":24.99," + + "\"category\":{\"name\":\"sCi-fI\"}," + + "\"author\":{\"lastname\":\"Dietz\"}}" + ); + } + + @Test + @WithUserDetails("admin") + public void testB_postAddBookWithCategoryWithAuthorAsAdminUser() throws Exception { + loadPagePost( + restApiBaseUrl + env.getProperty("page.url.restapi.books"), 200, + MediaType.APPLICATION_JSON, + "{\"title\":\"Halo: The Flood\"," + + "\"year\":2003," + + "\"isbn\":\"0345459-210\"," + + "\"price\":24.99," + + "\"category\":{\"name\":\"sCi-fI\"}," + + "\"author\":{\"lastname\":\"Dietz\"}}" + ); + } + + @Test + @WithUserDetails("admin") + public void testC_postAddBookWithoutCategoryWithAuthorAsAdminUser() throws Exception { + loadPagePost( + restApiBaseUrl + env.getProperty("page.url.restapi.books"), 200, + MediaType.APPLICATION_JSON, + "{\"title\":\"Mass Effect: Retribution\"," + + "\"year\":2010," + + "\"isbn\":\"0345520-722\"," + + "\"price\":29.90," + +// "\"category\":{\"name\":\"Sci-Fi\"}," + + "\"author\":{\"firstname\":\"Drew\",\"lastname\":\"Karpyshyn\"}}" + ); + } + + @Test + @WithUserDetails("admin") + public void testD_postAddAuthorAsAdminUser() throws Exception { + loadPagePost( + restApiBaseUrl + env.getProperty("page.url.restapi.authors"), 201, + MediaType.APPLICATION_JSON, + "{\"firstname\":\"Food\"," + + "\"lastname\":\"Carter\"}" + ); + } + + @Test + @WithUserDetails("helpdesk") + public void testE_postAddAuthorExpectForbiddenAsHelpdeskUser() throws Exception { + loadPagePost( + restApiBaseUrl + env.getProperty("page.url.restapi.authors"), 403, + MediaType.APPLICATION_JSON, + "{\"firstname\":\"Jessica\"," + + "\"lastname\":\"Retina\"}" + ); + } + + @Test + @WithUserDetails("helpdesk") + public void testF_postAddRoleExpectForbiddenAsHelpdeskUser() throws Exception { + loadPagePost( + restApiBaseUrl + env.getProperty("page.url.restapi.roles"), 403, + MediaType.APPLICATION_JSON, + "{\"name\":\"SUPERADMIN\"}" + ); + } + + @Test + @WithUserDetails("admin") + public void testG_postAddRoleAsAdminUser() throws Exception { + loadPagePost( + restApiBaseUrl + env.getProperty("page.url.restapi.roles"), 201, + MediaType.APPLICATION_JSON, + "{\"name\":\"ROOT\"}" + ); + } + +} \ No newline at end of file diff --git a/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestDeleteTest.java b/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestDeleteTest.java new file mode 100644 index 0000000..a61defd --- /dev/null +++ b/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestDeleteTest.java @@ -0,0 +1,44 @@ +// Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.web.rest.endpoint; + +import org.junit.Test; +import org.springframework.security.test.context.support.WithUserDetails; + +import com.fjordtek.bookstore.web.BookStoreTestWebContextBuilder; + +/** + * + * TODO: N/A + * + * @author Pekka Helenius + */ + +public class RestDeleteTest extends BookStoreTestWebContextBuilder { + + @Test + @WithUserDetails("user") + public void testA_deleteExistingBookExpectForbiddenAsNormalUser() throws Exception { + loadPageDelete( + restApiBaseUrl + env.getProperty("page.url.restapi.books") + "/2", 403 + ); + } + + @Test + @WithUserDetails("admin") + public void testB_deleteExistingBookAsAdminUser() throws Exception { + loadPageDelete( + restApiBaseUrl + env.getProperty("page.url.restapi.books") + "/2", 204 + ); + } +/* + @Test + @WithUserDetails("admin") + public void testC_deleteExistingUserRoleAsAdminUser() throws Exception { + // + loadPageDelete( + restApiBaseUrl + env.getProperty("page.url.restapi.userroles") + "/[user_id: 3, role_id: 4]", 204 + ); + } +*/ +} \ No newline at end of file diff --git a/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestEditTest.java b/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestEditTest.java new file mode 100644 index 0000000..fec99ba --- /dev/null +++ b/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestEditTest.java @@ -0,0 +1,49 @@ +// Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.web.rest.endpoint; + +import org.junit.Test; +import org.junit.jupiter.api.MethodOrderer.Alphanumeric; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithUserDetails; + +import com.fjordtek.bookstore.web.BookStoreTestWebContextBuilder; + +/** + * + * TODO: N/A + * + * @author Pekka Helenius + */ + +@TestMethodOrder(Alphanumeric.class) +public class RestEditTest extends BookStoreTestWebContextBuilder { + + @Test + @WithUserDetails("admin") + public void testA_putBookEditAsAdminUser() throws Exception { + loadPagePut( + restApiBaseUrl + env.getProperty("page.url.restapi.books") + "/2", 200, + MediaType.APPLICATION_JSON, + "{\"title\":\"The Witcher: Blood of Elves\"," + + "\"year\":1999," + + "\"isbn\":\"3213221-3\"," + + "\"price\":22.49}" + ); + } + + @Test + @WithUserDetails("user") + public void testB_putBookEditExpectForbiddenAsNormalUser() throws Exception { + loadPagePut( + restApiBaseUrl + env.getProperty("page.url.restapi.books") + "/2", 403, + MediaType.APPLICATION_JSON, + "{\"title\":\"Root flag captured backdoor H4X3DV4LU3!!\"," + + "\"year\":1999," + + "\"isbn\":\"8919312-7\"," + + "\"price\":9950.49}" + ); + } + +} \ No newline at end of file diff --git a/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RootUriTests.java b/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RootUriTests.java new file mode 100644 index 0000000..e31f874 --- /dev/null +++ b/bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RootUriTests.java @@ -0,0 +1,86 @@ +// Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.web.rest.endpoint; + +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; + +/** + * + * TODO: N/A + * + * @author Pekka Helenius + */ + +@TestMethodOrder(Alphanumeric.class) +public class RootUriTests extends BookStoreTestWebContextBuilder { + + @Test + @WithUserDetails("admin") + public void testA_getBookListPageAsAdminUser() throws Exception { + loadPageGet(restApiBaseUrl + env.getProperty("page.url.restapi.books"), 200); + } + + @Test + @WithUserDetails("user") + public void testB_getBookListPageExpectForbiddenAsNormalUser() throws Exception { + loadPageGet(restApiBaseUrl + env.getProperty("page.url.restapi.books"), 403); + } + + + + @Test + @WithUserDetails("admin") + public void testC_getAuthorsPageAsAdminUser() throws Exception { + loadPageGet(restApiBaseUrl + env.getProperty("page.url.restapi.authors"), 200); + } + + @Test + public void testD_getAuthorsPageExpectUnauthorizedAsNologin() throws Exception { + loadPageGet(restApiBaseUrl + env.getProperty("page.url.restapi.authors"), 401); + } + + + @Test + @WithUserDetails("admin") + public void testE_getCategoriesPageAsAdminUser() throws Exception { + loadPageGet(restApiBaseUrl + env.getProperty("page.url.restapi.categories"), 200); + } + + @Test + public void testF_getCategoriesPageExpectUnauthorizedAsNologin() throws Exception { + loadPageGet(restApiBaseUrl + env.getProperty("page.url.restapi.categories"), 401); + } + + + @Test + @WithUserDetails("admin") + public void testG_getUsersPageAsAdminUser() throws Exception { + loadPageGet(restApiBaseUrl + env.getProperty("page.url.restapi.users"), 200); + } + + @Test + @WithUserDetails("salesmanager") + public void testH_getUsersPageExpectForbiddenAsMarketingUser() throws Exception { + loadPageGet(restApiBaseUrl + env.getProperty("page.url.restapi.users"), 403); + } + + + + @Test + @WithUserDetails("admin") + public void testI_getRolesPageAsAdminUser() throws Exception { + loadPageGet(restApiBaseUrl + env.getProperty("page.url.restapi.roles"), 200); + } + + @Test + @WithUserDetails("user") + public void testJ_getRolesPageExpectForbiddenAsNormalUser() throws Exception { + loadPageGet(restApiBaseUrl + env.getProperty("page.url.restapi.roles"), 403); + } + +} \ No newline at end of file