Skip to content

Commit

Permalink
feat: User 삭제 API 개발 (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Mar 10, 2024
1 parent 1ffe225 commit 38990cb
Show file tree
Hide file tree
Showing 29 changed files with 234 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void REPLACE_BOOKMARK_SUCCESS_TEST() throws Exception {

// given
Long targetId = targetInitializer.saveTargetAndGetId("target", Instant.now());
String token = "token";
String token = "bearer token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedId(targetId)
.expectedToken(token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class FeedbackCreateAcceptanceTest extends AbstractFeedbackTestSupporter {
void CREATE_FEEDBACK_TO_SURVEY_SUCCESS() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("nalab", Instant.now());
String token = "nalab-token";
String token = "bearer nalab-token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedToken(token)
.expectedId(targetId)
Expand All @@ -88,7 +88,7 @@ void CREATE_FEEDBACK_TO_SURVEY_SUCCESS() throws Exception {
void CREATE_MULTIPLE_FEEDBACK_TO_SURVEY_SUCCESS() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("hello", Instant.now());
String token = "hello-token";
String token = "bearer hello-token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedToken(token)
.expectedId(targetId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FeedbackFindAcceptanceTest extends AbstractFeedbackTestSupporter {
void FIND_FEED_BACK_SUCCESS() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("hello world", Instant.now());
String token = "mock token";
String token = "bearer token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedId(targetId)
.expectedToken(token)
Expand All @@ -79,7 +79,7 @@ void FIND_FEED_BACK_SUCCESS() throws Exception {
void FIND_FEED_BACK_SUCCESS_ANY_FEEDBACK() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("hello world", Instant.now());
String token = "mock token";
String token = "bearer token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedId(targetId)
.expectedToken(token)
Expand Down Expand Up @@ -112,7 +112,7 @@ void FIND_BOOKMARKED_FEED_BACK_SUCCESS() throws Exception {
void FIND_BOOKMARKED_FEED_BACK_SUCCESS_ANY_FEEDBACK() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("hello world", Instant.now());
String token = "mock token";
String token = "bearer token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedId(targetId)
.expectedToken(token)
Expand All @@ -130,7 +130,7 @@ void FIND_BOOKMARKED_FEED_BACK_SUCCESS_ANY_FEEDBACK() throws Exception {
private Long setUpSurveyAndFeedbackAndBookmark() throws Exception {
// 유저 생성
Long targetId = targetInitializer.saveTargetAndGetId("hello world", Instant.now());
String token = "mock token";
String token = "bearer token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedId(targetId)
.expectedToken(token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class FeedbackFindByTypeAcceptanceTest extends AbstractFeedbackTestSupporter {
void FIND_FEEDBACK_BY_FORM_TYPE_TENDENCY() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("hello world", Instant.now());
String token = "mock token";
String token = "bearer token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedId(targetId)
.expectedToken(token)
Expand All @@ -86,7 +86,7 @@ void FIND_FEEDBACK_BY_FORM_TYPE_TENDENCY() throws Exception {
void FIND_FEEDBACK_BY_FORM_TYPE_CUSTOM() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("hello world", Instant.now());
String token = "mock token";
String token = "bearer token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedId(targetId)
.expectedToken(token)
Expand All @@ -109,7 +109,7 @@ void FIND_FEEDBACK_BY_FORM_TYPE_CUSTOM() throws Exception {
void FIND_FEEDBACK_BY_TYPE_TENDENCY_WITH_NO_FEEDBACK() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("hello world", Instant.now());
String token = "mock token";
String token = "bearer token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedId(targetId)
.expectedToken(token)
Expand All @@ -129,7 +129,7 @@ void FIND_FEEDBACK_BY_TYPE_TENDENCY_WITH_NO_FEEDBACK() throws Exception {
void FIND_FEEDBACK_BY_TYPE_CUSTOM_WITH_NO_FEEDBACK() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("hello world", Instant.now());
String token = "mock token";
String token = "bearer token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedId(targetId)
.expectedToken(token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ReviewersFindAcceptanceTest extends AbstractFeedbackTestSupporter {
void FEEDBACKS_WITH_NO_REVIEWER_TEST() throws Exception {

// given
String token = "token";
String token = "bearer token";
Long targetId = targetInitializer.saveTargetAndGetId("sujin", Instant.now());
applicationEventPublisher.publishEvent(
MockUserRegisterEvent.builder().expectedToken(token).expectedId(targetId).build());
Expand All @@ -84,7 +84,7 @@ void FEEDBACKS_WITH_NO_REVIEWER_TEST() throws Exception {
void FEEDBACKS_WITH_REVIEWERS_TEST() throws Exception {

// given
String token = "token";
String token = "bearer token";
Long targetId = targetInitializer.saveTargetAndGetId("sujin", Instant.now());
applicationEventPublisher.publishEvent(
MockUserRegisterEvent.builder().expectedToken(token).expectedId(targetId).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class SpecificFindAcceptanceTest extends AbstractFeedbackTestSupporter {
void FIND_SPECIFIC_FEEDBACK_SUCCESS_TEST() throws Exception {

// given
String token = "token";
String token = "bearer token";
Long targetId = targetInitializer.saveTargetAndGetId("sujin", Instant.now());
applicationEventPublisher.publishEvent(
MockUserRegisterEvent.builder().expectedToken(token).expectedId(targetId).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class FeedbackFindSummaryAcceptanceTest extends AbstractFeedbackTestSupporter {
void FEEFBACK_SUMMARY_FIND_SUCCESS_TEST_WITH_NO_FEEDBACK() throws Exception {

// given
String token = "token";
String token = "bearer token";
Long targetId = targetInitializer.saveTargetAndGetId("sujin", Instant.now());
applicationEventPublisher.publishEvent(
MockUserRegisterEvent.builder().expectedToken(token).expectedId(targetId).build());
Expand All @@ -81,7 +81,7 @@ void FEEFBACK_SUMMARY_FIND_SUCCESS_TEST_WITH_NO_FEEDBACK() throws Exception {
void FEEFBACK_SUMMARY_FIND_SUCCESS_TEST_WITH_FEEDBACK() throws Exception {

// given
String token = "token";
String token = "bearer token";
Long targetId = targetInitializer.saveTargetAndGetId("sujin", Instant.now());
applicationEventPublisher.publishEvent(
MockUserRegisterEvent.builder().expectedToken(token).expectedId(targetId).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void FIND_REVIEWER_SUMMARY_SUCCESS() throws Exception {

private String saveTargetAndGetToken(String name) {
Long targetId = targetInitializer.saveTargetAndGetId("nalab", Instant.now());
String token = "nalab-token";
String token = "bearer nalab-token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedToken(token)
.expectedId(targetId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void BOOKMARK_SURVEY_TO_TOKEN_OWNER() throws Exception {
// given
var targetId = targetInitializer.saveTargetAndGetId("luffy",
LocalDateTime.now().minusYears(24).toInstant(ZoneOffset.UTC));
var token = "luffy's-double-token";
var token = "bearer luffy's-double-token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedToken(token)
.expectedId(targetId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void CREATE_NEW_SURVEY_SUCCESS_DEFAULT() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("luffy",
LocalDateTime.now().minusYears(24).toInstant(ZoneOffset.UTC));
String token = "luffy's-double-token";
String token = "bearer luffy's-double-token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedToken(token)
.expectedId(targetId)
Expand All @@ -68,7 +68,7 @@ void CREATE_NEW_SURVEY_SUCCESS_SUCCESS_CUSTOM() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("luffy",
LocalDateTime.now().minusYears(24).toInstant(ZoneOffset.UTC));
String token = "luffy's-double-token";
String token = "bearer luffy's-double-token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedToken(token)
.expectedId(targetId)
Expand All @@ -86,7 +86,7 @@ void CREATE_NEW_SURVEY_SUCCESS_SUCCESS_CUSTOM() throws Exception {
void CREATE_NEW_SURVEY_WITH_FAIL() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("luffy", LocalDateTime.now().minusYears(24).toInstant(ZoneOffset.UTC));
String token = "luffy's-double-token";
String token = "bearer luffy's-double-token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedToken(token)
.expectedId(targetId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SurveyExistsAcceptanceTest extends AbstractSurveyTestSupporter {
@DisplayName("token에 해당하는 survey가 존재한다면, true를 반환한다.")
void RETURN_TRUE_IF_SURVEY_EXISTS() throws Exception {
// given
String token = "luffy's-double-token";
String token = "bearer luffy's-double-token";
Long targetId = targetInitializer.saveTargetAndGetId("devxb", Instant.now());
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedToken(token)
Expand All @@ -59,7 +59,7 @@ void RETURN_TRUE_IF_SURVEY_EXISTS() throws Exception {
@DisplayName("token에 해당하는 survey가 존재하지 않는다면, false를 반환한다.")
void RETURN_FALSE_IF_SURVEY_DOES_NOT_EXISTS() throws Exception {
// given
String token = "luffy's-double-token";
String token = "bearer luffy's-double-token";
Long targetId = targetInitializer.saveTargetAndGetId("devxb", Instant.now());
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedToken(token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SurveyFindAcceptanceTest extends AbstractSurveyTestSupporter {
@Test
void SURVEY_FIND_SUCCESS_TEST() throws Exception {

String token = "luffy's-double-token";
String token = "bearer luffy's-double-token";
Long targetId = targetInitializer.saveTargetAndGetId("sujin", Instant.now());
applicationEventPublisher.publishEvent(
MockUserRegisterEvent.builder().expectedToken(token).expectedId(targetId).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SurveyFindidAcceptanceTest extends AbstractSurveyTestSupporter {
void GET_LOGINED_SURVEY_ID_SUCCESS() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("nalab", Instant.now());
String token = "nalab-token";
String token = "bearer nalab-token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedToken(token)
.expectedId(targetId)
Expand All @@ -60,7 +60,7 @@ void GET_LOGINED_SURVEY_ID_SUCCESS() throws Exception {
void GET_LOGINED_SURVEY_ID_FAIL_NO_SURVEY() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("nalab", Instant.now());
String token = "nalab-token";
String token = "bearer nalab-token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedToken(token)
.expectedId(targetId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class TargetFindAcceptanceTest extends AbstractSurveyTestSupporter {
void FIND_TARGET_BY_SURVEY_ID_WITH_NO_AUTHORIZATION() throws Exception {
// given
Long targetId = targetInitializer.saveTargetAndGetId("target", Instant.now());
String token = "token";
String token = "bearer token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedToken(token)
.expectedId(targetId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ protected ResultActions updateTargetPosition(String token, String content) throw
);
}

protected ResultActions deleteUser(String token) throws Exception {
return mockMvc.perform(MockMvcRequestBuilders
.delete(API_VERSION + "/users")
.header(HttpHeaders.AUTHORIZATION, token));
}

@Autowired
final void setMockMvc(MockMvc mockMvc) {
this.mockMvc = mockMvc;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package me.nalab.luffy.api.acceptance.test.user.delete;

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

import java.time.Instant;
import java.util.Set;
import me.nalab.auth.application.common.dto.Payload;
import me.nalab.auth.application.common.utils.JwtUtils;
import me.nalab.auth.mock.api.MockUserRegisterEvent;
import me.nalab.luffy.api.acceptance.test.TargetInitializer;
import me.nalab.luffy.api.acceptance.test.UserInitializer;
import me.nalab.luffy.api.acceptance.test.user.UserAcceptanceTestSupporter;
import me.nalab.user.domain.user.Provider;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.test.context.TestPropertySource;

@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource("classpath:h2.properties")
@ComponentScan("me.nalab")
@EnableJpaRepositories(basePackages = {"me.nalab"})
@EntityScan(basePackages = {"me.nalab"})
class UserDeleteAcceptanceTest extends UserAcceptanceTestSupporter {

@Autowired
private ApplicationEventPublisher applicationEventPublisher;

@Autowired
private TargetInitializer targetInitializer;

@Autowired
private JwtUtils jwtUtils;

@Autowired
private UserInitializer userInitializer;

@Test
@DisplayName("DELETE /v1/users API는 token에 해당하는 유저가 삭제되면 200 OK를 반환한다.")
void DELETE_USER_SUCCESS() throws Exception {
// given
String nickname = "delete_user_success";
String email = "delete_user_success";

var token = "bearer " + createUserAndSetToken(nickname, email);

// when
var result = deleteUser(token);

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

private String createUserAndSetToken(String nickname, String email) {
Long userId = userInitializer.saveUserWithOAuth(Provider.DEFAULT, nickname, email, Instant.now());
Long targetId = targetInitializer.saveTargetAndGetId(nickname, Instant.now());

var token = jwtUtils.createAccessToken(Set.of(new Payload(Payload.Key.USER_ID, String.valueOf(userId)),
new Payload(Payload.Key.TARGET_ID, String.valueOf(targetId))));

applicationEventPublisher.publishEvent(
MockUserRegisterEvent.builder().expectedToken("bearer " + token).expectedId(targetId).build());

return token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void TARGET_POSITION_UPDATE_SUCCESS_TEST() throws Exception {
// given
String nickname = "nickname";
Long targetId = targetInitializer.saveTargetAndGetId(nickname, Instant.now());
String token = "token";
String token = "bearer token";
applicationEventPublisher.publishEvent(MockUserRegisterEvent.builder()
.expectedToken(token)
.expectedId(targetId)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.nalab.auth.interceptor;

public enum AuthenticateHeaderPrefix {

BEARER,
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
throwIfCannotValidToken(token);
Long targetId = getTargetId(token);
request.setAttribute("logined", targetId);
request.setAttribute("tokenType", AuthenticateHeaderPrefix.valueOf(token.split(" ")[0].toUpperCase()));
request.setAttribute("tokenValue", token.split(" ")[1]);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.nalab.auth.mock.interceptor;

public enum AuthenticateHeaderPrefix {

BEARER,
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
String token = request.getHeader("Authorization");
throwIfCannotValidToken(token);
request.setAttribute("logined", expectedId);
request.setAttribute("tokenType", AuthenticateHeaderPrefix.valueOf(token.split(" ")[0].toUpperCase()));
request.setAttribute("tokenValue", token.split(" ")[1]);
}
return true;
}
Expand Down
Loading

0 comments on commit 38990cb

Please sign in to comment.