|
10 | 10 | import org.junit.jupiter.api.BeforeEach;
|
11 | 11 | import org.junit.jupiter.api.Test;
|
12 | 12 | import org.junit.jupiter.api.extension.ExtendWith;
|
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
13 | 15 | import org.springframework.beans.factory.annotation.Autowired;
|
14 | 16 | import org.springframework.beans.factory.annotation.Value;
|
15 | 17 | import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
|
|
62 | 64 | @AutoConfigureRestDocs(outputDir = "target/generated-snippets",uriScheme = "https", uriHost = "vholic.com", uriPort = 8300)
|
63 | 65 | public class CustomerIntegrationTest {
|
64 | 66 |
|
| 67 | + private static final Logger logger = LoggerFactory.getLogger(CustomerIntegrationTest.class); |
| 68 | + |
| 69 | + |
65 | 70 | @Autowired
|
66 | 71 | private MockMvc mockMvc;
|
67 | 72 |
|
@@ -625,6 +630,93 @@ public void testLoginWithInvalidCredentials_EXPOSE() throws Exception {
|
625 | 630 | assertEquals(userMessage, CustomSecurityUserExceptionMessage.AUTHENTICATION_WRONG_GRANT_TYPE.getMessage());
|
626 | 631 | }
|
627 | 632 |
|
| 633 | + @Test |
| 634 | + public void testFetchResourceWithInvalidCredentialsAndValidCredentialsButWithNoPermission() throws Exception { |
| 635 | + |
| 636 | + MvcResult result = mockMvc.perform(RestDocumentationRequestBuilders.post("/oauth2/token") |
| 637 | + .header(HttpHeaders.AUTHORIZATION, basicHeader) |
| 638 | + .header(KnifeHttpHeaders.APP_TOKEN, "APPTOKENTESTRESOURCE") |
| 639 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 640 | + .param("grant_type", "password") |
| 641 | + .param("username", testUserName) |
| 642 | + .param("password", testUserPassword)) |
| 643 | + .andExpect(status().isOk()) |
| 644 | + .andDo(document( "{class-name}/{method-name}/oauth-access-token", |
| 645 | + preprocessRequest(new AccessTokenMaskingPreprocessor()), |
| 646 | + preprocessResponse(new AccessTokenMaskingPreprocessor(), prettyPrint()), |
| 647 | + requestHeaders( |
| 648 | + headerWithName(HttpHeaders.AUTHORIZATION).description("Connect the received client_id and client_secret with ':', use the base64 function, and write Basic at the beginning. ex) Basic base64(client_id:client_secret)"), |
| 649 | + headerWithName(KnifeHttpHeaders.APP_TOKEN).optional().description("Not having a value does not mean you cannot log in, but cases without an App-Token value share the same access_token. Please include it as a required value according to the device-specific session policy.") |
| 650 | + ), |
| 651 | + formParameters( |
| 652 | + parameterWithName("grant_type").description("Uses the password method among Oauth2 grant_types. Please write password."), |
| 653 | + parameterWithName("username").description("This is the user's email address."), |
| 654 | + parameterWithName("password").description("This is the user's password.") |
| 655 | + ))) |
| 656 | + .andReturn(); |
| 657 | + |
| 658 | + |
| 659 | + String responseString = result.getResponse().getContentAsString(StandardCharsets.UTF_8); |
| 660 | + JSONObject jsonResponse = new JSONObject(responseString); |
| 661 | + String finalAccessTokenForTestResource = jsonResponse.getString("access_token"); |
| 662 | + |
| 663 | + |
| 664 | + |
| 665 | + result = mockMvc.perform(RestDocumentationRequestBuilders.get("/api/v1/customers/5") |
| 666 | + .contentType(MediaType.APPLICATION_JSON) |
| 667 | + .header(HttpHeaders.AUTHORIZATION, "Bearer " + finalAccessTokenForTestResource + "1")) |
| 668 | + .andDo(document( "{class-name}/{method-name}/customers-id", |
| 669 | + preprocessRequest(new AccessTokenMaskingPreprocessor()), |
| 670 | + preprocessResponse(new AccessTokenMaskingPreprocessor(), prettyPrint()), |
| 671 | + requestHeaders( |
| 672 | + headerWithName(HttpHeaders.AUTHORIZATION).description("Bearer XXX") |
| 673 | + ))) |
| 674 | + .andExpect(status().isUnauthorized()).andReturn(); // 401 |
| 675 | + |
| 676 | + responseString = result.getResponse().getContentAsString(StandardCharsets.UTF_8); |
| 677 | + jsonResponse = new JSONObject(responseString); |
| 678 | + |
| 679 | + |
| 680 | + String userMessage = jsonResponse.getString("userMessage"); |
| 681 | + |
| 682 | + assertEquals(userMessage, CustomSecurityUserExceptionMessage.AUTHENTICATION_TOKEN_FAILURE.getMessage()); |
| 683 | + |
| 684 | + |
| 685 | + |
| 686 | + |
| 687 | + result = mockMvc.perform(RestDocumentationRequestBuilders.get("/api/v1/customers/5") |
| 688 | + .contentType(MediaType.APPLICATION_JSON) |
| 689 | + .header(HttpHeaders.AUTHORIZATION, "Bearer " + finalAccessTokenForTestResource)) |
| 690 | + .andDo(document( "{class-name}/{method-name}/customers-id", |
| 691 | + preprocessRequest(new AccessTokenMaskingPreprocessor()), |
| 692 | + preprocessResponse(new AccessTokenMaskingPreprocessor(), prettyPrint()), |
| 693 | + requestHeaders( |
| 694 | + headerWithName(HttpHeaders.AUTHORIZATION).description("Bearer XXX") |
| 695 | + ))) |
| 696 | + .andExpect(status().isForbidden()).andReturn(); // 403 |
| 697 | + |
| 698 | + responseString = result.getResponse().getContentAsString(StandardCharsets.UTF_8); |
| 699 | + jsonResponse = new JSONObject(responseString); |
| 700 | + userMessage = jsonResponse.getString("userMessage"); |
| 701 | + |
| 702 | + assertEquals(userMessage, CustomSecurityUserExceptionMessage.AUTHORIZATION_FAILURE.getMessage()); |
| 703 | + |
| 704 | + |
| 705 | + // Remove Access Token DB done tested |
| 706 | + mockMvc.perform(RestDocumentationRequestBuilders.get("/api/v1/customers/me/logout") |
| 707 | + .contentType(MediaType.APPLICATION_JSON) |
| 708 | + .header(HttpHeaders.AUTHORIZATION, "Bearer " + finalAccessTokenForTestResource)) |
| 709 | + |
| 710 | + .andDo(document( "{class-name}/{method-name}/oauth-customer-logout", |
| 711 | + requestHeaders( |
| 712 | + headerWithName(HttpHeaders.AUTHORIZATION).description("Bearer XXX") |
| 713 | + ),relaxedResponseFields( |
| 714 | + fieldWithPath("logout").description("If true, logout is successful on the backend, if false, it fails. However, ignore this message and, considering UX, delete the token on the client side and move to the login screen.") |
| 715 | + |
| 716 | + ))); |
| 717 | + } |
| 718 | + |
| 719 | + |
628 | 720 |
|
629 | 721 |
|
630 | 722 | private static class AccessTokenMaskingPreprocessor implements OperationPreprocessor {
|
|
0 commit comments