-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathMemberTest.java
65 lines (55 loc) · 2.49 KB
/
MemberTest.java
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
package nextstep.app;
import nextstep.app.domain.Member;
import nextstep.security.service.MemberRepository;
import nextstep.app.infrastructure.InmemoryMemberRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
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.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.util.Base64Utils;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class MemberTest {
private static final Member TEST_MEMBER = InmemoryMemberRepository.TEST_MEMBER_1;
@Autowired
private MockMvc mockMvc;
@Autowired
private MemberRepository memberRepository;
@BeforeEach
void setUp() {
memberRepository.save(TEST_MEMBER);
}
@DisplayName("Basic Auth 인증 성공 후 회원 목록 조회")
@Test
void members() throws Exception {
String token = TEST_MEMBER.getEmail() + ":" + TEST_MEMBER.getPassword();
String encoded = Base64Utils.encodeToString(token.getBytes());
ResultActions loginResponse = mockMvc.perform(get("/members")
.header("Authorization", "Basic " + encoded)
.contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
);
loginResponse.andDo(print());
loginResponse.andExpect(status().isOk());
loginResponse.andExpect(jsonPath("$[*].email", hasItem(TEST_MEMBER.getEmail())));
}
@DisplayName("Basic Auth 인증 실패 시 에러 응답")
@Test
void members_fail() throws Exception {
ResultActions loginResponse = mockMvc.perform(get("/members")
.header("Authorization", "Basic " + "invalid")
.contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
);
loginResponse.andDo(print());
loginResponse.andExpect(status().isUnauthorized());
}
}