Skip to content

Commit

Permalink
test : SecurityConfig Test 코드 작성 (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
binary-ho committed Mar 12, 2024
1 parent 05e79f0 commit b6ecacb
Showing 1 changed file with 83 additions and 0 deletions.
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());
}
}

0 comments on commit b6ecacb

Please sign in to comment.