MatchingAiApplicationTests.java
6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package daeucna;
import static org.hamcrest.CoreMatchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import javax.crypto.SecretKey;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import daeucna.config.security.dto.AuthorityDto;
import daeucna.config.security.dto.LoginDto;
import daeucna.config.security.dto.UserDto;
import daeucna.config.security.jwt.JwtFilter;
import daeucna.config.security.utils.CommonJson;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;
@SpringBootTest
@ContextConfiguration(classes = MatchingAiApplication.class)
@AutoConfigureMockMvc
@Slf4j
class MatchingAiApplicationTests {
@Autowired
private MockMvc mockMvc;
@Test
void userSignup() throws Exception {
AuthorityDto authorityDto = AuthorityDto.builder().authorityName("ROLE_ADMIN").build();
UserDto userDto = UserDto.builder()
.username("sangkiTest")
.password("sangkiTest")
.nickname("sangkiTest")
.authorityDtoSet(new HashSet<AuthorityDto>( Arrays.asList(authorityDto) ))
.build();
String body = CommonJson.objectToString(userDto);
this.mockMvc.perform(
post("/api/signup")
.contentType(MediaType.APPLICATION_JSON)
.content(body)
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("sangkiTest")));
}
@Test
void authenticate() throws Exception {
fnAuthenticate();
}
MvcResult fnAuthenticate() throws Exception {
LoginDto loginDto = LoginDto.builder()
.username("sangkiTest")
.password("sangkiTest")
.build();
String body = CommonJson.objectToString(loginDto);
MvcResult mvcResult = this.mockMvc.perform(
post("/api/authenticate")
.contentType(MediaType.APPLICATION_JSON)
.content(body)
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("token")))
.andReturn();
return mvcResult;
}
@SuppressWarnings("unchecked")
@Test
void user() throws Exception {
MvcResult mvcResult = fnAuthenticate();
String sReturn = mvcResult.getResponse().getContentAsString();
log.debug("sReturn = " + sReturn);
HashMap<String, String> mapVal = (HashMap<String, String>) CommonJson.stringToObject(sReturn, HashMap.class);
log.debug("token = " + mapVal.get("token") );
this.mockMvc.perform(
get("/api/user")
.header("Authorization", "Bearer " + mapVal.get("token"))
)
.andDo(print())
.andExpect(status().isOk());
}
@SuppressWarnings("unchecked")
@Test
void testRedirect() throws Exception {
MvcResult mvcResult = fnAuthenticate();
String sReturn = mvcResult.getResponse().getContentAsString();
log.debug("sReturn = " + sReturn);
HashMap<String, String> mapVal = (HashMap<String, String>) CommonJson.stringToObject(sReturn, HashMap.class);
log.debug("token = " + mapVal.get("token") );
this.mockMvc.perform(
post("/api/test-redirect")
.header("Authorization", "Bearer " + mapVal.get("token"))
)
.andDo(print())
.andExpect(status().isOk());
}
@SuppressWarnings("unchecked")
@Test
void userSangki() throws Exception {
MvcResult mvcResult = fnAuthenticate();
String sReturn = mvcResult.getResponse().getContentAsString();
log.debug("sReturn = " + sReturn);
HashMap<String, String> mapVal = (HashMap<String, String>) CommonJson.stringToObject(sReturn, HashMap.class);
log.debug("token = " + mapVal.get("token") );
this.mockMvc.perform(
get("/api/user/sangki")
.header("Authorization", "Bearer " + mapVal.get("token"))
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("sangki")));
}
@SuppressWarnings("unchecked")
@Test
void hello() throws Exception {
MvcResult mvcResult = fnAuthenticate();
String sReturn = mvcResult.getResponse().getContentAsString();
log.debug("sReturn = " + sReturn);
HashMap<String, String> mapVal = (HashMap<String, String>) CommonJson.stringToObject(sReturn, HashMap.class);
log.debug("token = " + mapVal.get("token") );
this.mockMvc.perform(
get("/api/hello")
.header("Authorization", "Bearer " + mapVal.get("token"))
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("hello")));
}
@Test
void parseJwt(@Value("${jwt.secret}") String secret) throws Exception {
MvcResult mvcResult = fnAuthenticate();
String sReturn = mvcResult.getResponse().getContentAsString();
HashMap<String, String> mapVal = (HashMap<String, String>) CommonJson.stringToObject(sReturn, HashMap.class);
String accessToken = (String) mapVal.get("token");
byte[] keyBytes = Decoders.BASE64URL.decode(secret);
SecretKey key = Keys.hmacShaKeyFor(keyBytes);
Jws<Claims> claims;
try{
claims = Jwts.parser()
.verifyWith(key)
.build()
.parseSignedClaims(accessToken);
log.debug("parseJwt = " + CommonJson.objectToString(claims));
log.debug("parseJwt(getSubject) = " + claims.getBody().getSubject());
log.debug("parseJwt(claims) = " + CommonJson.objectToString(claims.getBody()));
} catch (Exception ignored) {
log.debug("parseJwt = " + ignored.getLocalizedMessage());
}
}
@Test
void refreshToken() throws Exception {
MvcResult mvcResult = fnAuthenticate();
String sReturn = mvcResult.getResponse().getContentAsString();
HashMap<String, String> mapVal = (HashMap<String, String>) CommonJson.stringToObject(sReturn, HashMap.class);
String refreshToken = (String) mapVal.get("refreshToken");
this.mockMvc.perform(
post("/api/refreshtoken")
.header(JwtFilter.AUTHORIZATION_REFRESH_HEADER, refreshToken)
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("token")))
.andReturn();
}
}