Skip to content

Commit

Permalink
test: Auth Api 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
EunChanNam committed Oct 27, 2023
1 parent d64059a commit aa808a5
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfig {
Expand All @@ -12,4 +15,9 @@ public class SecurityConfig {
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http.csrf(AbstractHttpConfigurer::disable).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.mockito.Mockito;
import org.springframework.security.crypto.password.PasswordEncoder;

Expand Down Expand Up @@ -144,4 +145,14 @@ void failByInvalidPw() {
.hasMessageContaining(ErrorCode.LOGIN_FAIL.getMessage());
}
}

@Test
@DisplayName("[로그아웃을 한다]")
void logout() {
//when
Executable when = () -> authService.logout(1L);

//then
assertDoesNotThrow(when);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.inq.wishhair.wesharewishhair.auth.presentation;

import static com.inq.wishhair.wesharewishhair.common.fixture.AuthFixture.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

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.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import com.inq.wishhair.wesharewishhair.auth.application.AuthService;
import com.inq.wishhair.wesharewishhair.auth.application.dto.response.LoginResponse;
import com.inq.wishhair.wesharewishhair.auth.presentation.dto.request.LoginRequest;
import com.inq.wishhair.wesharewishhair.common.support.ApiTestSupport;
import com.inq.wishhair.wesharewishhair.global.config.SecurityConfig;

@WebMvcTest(value = {AuthController.class, SecurityConfig.class})
@DisplayName("[AuthController 테스트] - API")
class AuthControllerTest extends ApiTestSupport {

private static final String LOGIN_URL = "/api/auth/login";
private static final String LOGOUT_URL = "/api/auth/logout";

@Autowired
private MockMvc mockMvc;
@MockBean
private AuthService authService;

@Test
@DisplayName("[로그인 API 를 호출한다]")
void login() throws Exception {
//given
String email = "email";
String pw = "pw";
LoginRequest loginRequest = new LoginRequest(email, pw);

LoginResponse loginResponse = new LoginResponse("token", "token");
given(authService.login(email, pw))
.willReturn(loginResponse);

//when
ResultActions result = mockMvc.perform(
MockMvcRequestBuilders
.post(LOGIN_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(toJson(loginRequest))

);

//then
result.andExpectAll(
jsonPath("$.accessToken").value(loginResponse.accessToken()),
jsonPath("$.refreshToken").value(loginResponse.refreshToken())
);
}

@Test
@DisplayName("[로그아웃 API 를 호출한다]")
void logout() throws Exception {
//when
ResultActions result = mockMvc.perform(
MockMvcRequestBuilders
.post(LOGOUT_URL)
.header(AUTHORIZATION, ACCESS_TOKEN)
);

//then
result.andExpect(status().isOk());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.inq.wishhair.wesharewishhair.auth.presentation;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

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.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import com.inq.wishhair.wesharewishhair.auth.application.MailAuthService;
import com.inq.wishhair.wesharewishhair.auth.presentation.dto.request.AuthKeyRequest;
import com.inq.wishhair.wesharewishhair.auth.presentation.dto.request.MailRequest;
import com.inq.wishhair.wesharewishhair.common.support.ApiTestSupport;
import com.inq.wishhair.wesharewishhair.global.config.SecurityConfig;
import com.inq.wishhair.wesharewishhair.user.application.utils.UserValidator;

@WebMvcTest(value = {MailAuthController.class, SecurityConfig.class})
@DisplayName("[MailAuthController 테스트] - API")
class MailAuthControllerTest extends ApiTestSupport {

private static final String CHECK_DUPLICATED_EMAIL_URL = "/api/email/check";
private static final String SEND_AUTHORIZATION_MAIL_URL = "/api/email/send";
private static final String AUTHORIZE_KEY_URL = "/api/email/validate";
private static final String EMAIL = "[email protected]";

@Autowired
private MockMvc mockMvc;
@MockBean
private UserValidator userValidator;
@MockBean
private MailAuthService mailAuthService;

@Test
@DisplayName("[이메일 중복검사 API 를 호출한다]")
void checkDuplicateEmail() throws Exception {
//given
MailRequest mailRequest = new MailRequest(EMAIL);

//when
ResultActions result = mockMvc.perform(
MockMvcRequestBuilders
.post(CHECK_DUPLICATED_EMAIL_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(toJson(mailRequest))
);

//then
result.andExpect(status().isOk());
}

@Test
@DisplayName("[인증 메일 발송 API 를 호출한다]")
void sendAuthorizationMail() throws Exception {
//given
MailRequest mailRequest = new MailRequest(EMAIL);

//when
ResultActions result = mockMvc.perform(
MockMvcRequestBuilders
.post(SEND_AUTHORIZATION_MAIL_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(toJson(mailRequest))
);

//then
result.andExpect(status().isOk());
}

@Test
@DisplayName("[인증코드 확인 API 를 호출한다]")
void authorizeKey() throws Exception {
//given
AuthKeyRequest authKeyRequest = new AuthKeyRequest(EMAIL, "authcode");

//when
ResultActions result = mockMvc.perform(
MockMvcRequestBuilders
.post(AUTHORIZE_KEY_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(toJson(authKeyRequest))
);

//then
result.andExpect(status().isOk());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.inq.wishhair.wesharewishhair.auth.presentation;

import static com.inq.wishhair.wesharewishhair.common.fixture.AuthFixture.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

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.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import com.inq.wishhair.wesharewishhair.auth.application.TokenReissueService;
import com.inq.wishhair.wesharewishhair.auth.application.dto.response.TokenResponse;
import com.inq.wishhair.wesharewishhair.common.support.ApiTestSupport;
import com.inq.wishhair.wesharewishhair.global.config.SecurityConfig;

@WebMvcTest(value = {TokenReissueController.class, SecurityConfig.class})
@DisplayName("[TokenReissueController 테스트] - API")
class TokenReissueControllerTest extends ApiTestSupport {

private static final String REISSUE_TOKEN_URL = "/api/token/reissue";

@Autowired
private MockMvc mockMvc;
@MockBean
private TokenReissueService tokenReissueService;

@Test
@DisplayName("[토큰 재발급 API 를 호출한다]")
void reissueToken() throws Exception {
//given
TokenResponse tokenResponse = new TokenResponse("accessToken", "refreshToken");
given(tokenReissueService.reissueToken(1L, TOKEN))
.willReturn(tokenResponse);

//when
ResultActions result = mockMvc.perform(
MockMvcRequestBuilders
.post(REISSUE_TOKEN_URL)
.header(AUTHORIZATION, ACCESS_TOKEN)
);

//then
result.andExpectAll(
status().isOk(),
jsonPath("$.accessToken").value(tokenResponse.accessToken()),
jsonPath("$.refreshToken").value(tokenResponse.refreshToken())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.inq.wishhair.wesharewishhair.common.fixture;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class AuthFixture {

public static final String TOKEN = "token";
public static final String BEARER = "Bearer ";
public static final String AUTHORIZATION = "Authorization";
public static final String ACCESS_TOKEN = BEARER + TOKEN;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.inq.wishhair.wesharewishhair.common.support;

import static com.inq.wishhair.wesharewishhair.common.fixture.AuthFixture.*;
import static org.mockito.BDDMockito.*;

import org.junit.jupiter.api.BeforeEach;
import org.springframework.boot.test.mock.mockito.MockBean;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inq.wishhair.wesharewishhair.auth.domain.AuthToken;
import com.inq.wishhair.wesharewishhair.auth.domain.AuthTokenManager;

public abstract class ApiTestSupport {

private final ObjectMapper objectMapper = new ObjectMapper();

@MockBean
protected AuthTokenManager authTokenManager;

@BeforeEach
public void setAuthorization() {
given(authTokenManager.generate(any(Long.class))).willReturn(new AuthToken(TOKEN, TOKEN));
given(authTokenManager.getId(anyString())).willReturn(1L);
}

public String toJson(Object object) throws JsonProcessingException {
return objectMapper.writeValueAsString(object);
}
}

0 comments on commit aa808a5

Please sign in to comment.