-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test : SecurityConfig Test 코드 작성 (#112)
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/test/java/gdsc/binaryho/imhere/security/SecurityConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package gdsc.binaryho.imhere.security; | ||
|
||
import static gdsc.binaryho.imhere.mock.fixture.MemberFixture.MOCK_STUDENT; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
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.MockMvcResultMatchers.header; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import gdsc.binaryho.imhere.core.member.Role; | ||
import gdsc.binaryho.imhere.core.member.infrastructure.MemberRepository; | ||
import gdsc.binaryho.imhere.security.jwt.Token; | ||
import gdsc.binaryho.imhere.security.jwt.TokenService; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
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.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
@SpringBootTest | ||
@ExtendWith(MockitoExtension.class) | ||
@AutoConfigureMockMvc | ||
public class SecurityConfigTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private TokenService tokenService; | ||
|
||
@MockBean | ||
private MemberRepository memberRepository; | ||
|
||
@Mock | ||
private DefaultOAuth2UserService defaultOAuth2UserService; | ||
|
||
private static final String ACCESS_TOKEN_PREFIX = "Token "; | ||
|
||
@Test | ||
public void 인증이_필요한_경로에_접근하면_깃허브_로그인_페이지로_Redirection_된다() throws Exception { | ||
mockMvc.perform(post("/") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
) | ||
.andExpect(status().is3xxRedirection()) | ||
.andExpect(header().string("Location", containsString("/oauth2/authorization/github"))); | ||
} | ||
|
||
@Test | ||
public void 토큰을_통해_인가_할_수_있다() throws Exception { | ||
given(memberRepository.findById(any())) | ||
.willReturn(Optional.of(MOCK_STUDENT)); | ||
Token token = tokenService.createToken(1L, Role.STUDENT); | ||
|
||
mockMvc.perform(get("/api/lecture") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header(HttpHeaders.AUTHORIZATION, ACCESS_TOKEN_PREFIX + token.getAccessToken()) | ||
) | ||
.andExpect(status().is2xxSuccessful()); | ||
} | ||
|
||
@Test | ||
public void 권한이_없는_토큰_요청은_403_응답을_반환한다() throws Exception { | ||
given(memberRepository.findById(any())) | ||
.willReturn(Optional.of(MOCK_STUDENT)); | ||
Token token = tokenService.createToken(1L, Role.STUDENT); | ||
|
||
mockMvc.perform(post("/api/admin/role/1") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header(HttpHeaders.AUTHORIZATION, ACCESS_TOKEN_PREFIX + token.getAccessToken()) | ||
) | ||
.andExpect(status().isForbidden()); | ||
} | ||
} |