Browse Source

Add REST API end point tests

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.4-alpha
Pekka Helenius 4 years ago
parent
commit
74d8addec6
5 changed files with 319 additions and 0 deletions
  1. +33
    -0
      bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/IndexTest.java
  2. +107
    -0
      bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestAddTest.java
  3. +44
    -0
      bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestDeleteTest.java
  4. +49
    -0
      bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestEditTest.java
  5. +86
    -0
      bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RootUriTests.java

+ 33
- 0
bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/IndexTest.java View File

@ -0,0 +1,33 @@
// Pekka Helenius <fincer89@hotmail.com>, 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);
}
}

+ 107
- 0
bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestAddTest.java View File

@ -0,0 +1,107 @@
// Pekka Helenius <fincer89@hotmail.com>, 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\"}"
);
}
}

+ 44
- 0
bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestDeleteTest.java View File

@ -0,0 +1,44 @@
// Pekka Helenius <fincer89@hotmail.com>, 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
);
}
*/
}

+ 49
- 0
bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RestEditTest.java View File

@ -0,0 +1,49 @@
// Pekka Helenius <fincer89@hotmail.com>, 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}"
);
}
}

+ 86
- 0
bookstore/src/test/java/com/fjordtek/bookstore/web/rest/endpoint/RootUriTests.java View File

@ -0,0 +1,86 @@
// Pekka Helenius <fincer89@hotmail.com>, 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);
}
}

Loading…
Cancel
Save