Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 80e65b2

Browse files
Security_part_11_3: add JwtCardsControllerIT.java
1 parent 522a15c commit 80e65b2

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package me.oldboy.integration.controllers.api_jwt_scenario;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
6+
import lombok.SneakyThrows;
7+
import me.oldboy.config.test_data_source.TestContainerInit;
8+
import me.oldboy.dto.card_dto.CardReadDto;
9+
import me.oldboy.integration.annotation.IT;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.http.MediaType;
14+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
15+
import org.springframework.test.web.servlet.MockMvc;
16+
import org.springframework.test.web.servlet.MvcResult;
17+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
18+
import org.springframework.web.context.WebApplicationContext;
19+
20+
import java.util.List;
21+
22+
import static me.oldboy.test_constant.TestConstantFields.EXIST_EMAIL;
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
25+
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
26+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29+
30+
@IT
31+
class JwtCardsControllerIT extends TestContainerInit {
32+
33+
@Autowired
34+
private WebApplicationContext webApplicationContext;
35+
private MockMvc mockMvc;
36+
37+
@BeforeEach
38+
void setUp(){
39+
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
40+
.apply(springSecurity())
41+
.build();
42+
}
43+
44+
@Test
45+
@SneakyThrows
46+
void getCardDetails_ShouldReturnDtoList_AndOkAdminAuth_Test() {
47+
MvcResult result = mockMvc.perform(get("/api/myCards")
48+
.accept(MediaType.APPLICATION_JSON)
49+
.with(jwt().jwt(builder -> builder
50+
.claim("sub", EXIST_EMAIL)
51+
.claim("scope", "openid profile"))
52+
.authorities(new SimpleGrantedAuthority("ROLE_ADMIN"))))
53+
.andExpect(status().isOk())
54+
.andReturn();
55+
56+
String strResult = result.getResponse().getContentAsString();
57+
List<CardReadDto> listFromResponse = new ObjectMapper()
58+
.registerModule(new JavaTimeModule())
59+
.readValue(strResult, new TypeReference<List<CardReadDto>>() {});
60+
61+
assertThat(listFromResponse.size()).isGreaterThan(1);
62+
}
63+
64+
@Test
65+
@SneakyThrows
66+
void getCardDetails_ShouldReturnForbidden_NotAdminAuth_Test() {
67+
mockMvc.perform(get("/api/myCards")
68+
.accept(MediaType.APPLICATION_JSON)
69+
.with(jwt().jwt(builder -> builder
70+
.claim("sub", "user3@test.com"))
71+
.authorities(new SimpleGrantedAuthority("ROLE_USER"))))
72+
.andExpect(status().isForbidden())
73+
.andExpect(content().string(""));
74+
}
75+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /