Skip to content

Commit

Permalink
feat: add admin token generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-yhc committed Jan 9, 2024
1 parent 0d84266 commit b027c9e
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import swm.hkcc.LGTM.app.global.dto.ApiDataResponse;
import swm.hkcc.LGTM.app.global.exception.GeneralException;
import swm.hkcc.LGTM.app.modules.admin.service.AdminService;
Expand All @@ -27,6 +24,12 @@ public ApiDataResponse<Boolean> deleteMember(
} catch (DataAccessException e) {
throw new GeneralException("유저의 활동 기록이 있어 삭제할 수 없는 유저입니다.", e.getCause());
}
}

@GetMapping("/token/{id}")
public ApiDataResponse<String> getMemberToken(
@PathVariable("id") Long id
) {
return ApiDataResponse.of(adminService.getMemberToken(id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import swm.hkcc.LGTM.app.modules.auth.constants.TokenType;
import swm.hkcc.LGTM.app.modules.auth.utils.jwt.TokenProvider;
import swm.hkcc.LGTM.app.modules.member.domain.Member;
import swm.hkcc.LGTM.app.modules.member.exception.NotExistMember;
import swm.hkcc.LGTM.app.modules.member.repository.MemberRepository;
import swm.hkcc.LGTM.app.modules.tag.domain.TechTagPerMember;
import swm.hkcc.LGTM.app.modules.tag.repository.TechTagPerMemberRepository;
Expand All @@ -18,7 +22,7 @@ public class AdminService {

private final MemberRepository memberRepository;
private final TechTagPerMemberRepository techTagPerMemberRepository;

private final TokenProvider tokenProvider;

@Transactional
public boolean deleteMember(Long id) {
Expand All @@ -31,4 +35,8 @@ public boolean deleteMember(Long id) {
return true;
}

public String getMemberToken(Long id) {
Member member = memberRepository.findById(id).orElseThrow(() -> new NotExistMember());
return tokenProvider.createToken(member.getGithubId(), TokenType.ACCESS_TOKEN);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package swm.hkcc.LGTM.app.modules.admin.controller;


import com.epages.restdocs.apispec.ResourceSnippetParameters;
import com.epages.restdocs.apispec.SimpleType;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.RestDocumentationExtension;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import swm.hkcc.LGTM.app.global.constant.ResponseCode;
import swm.hkcc.LGTM.app.modules.admin.service.AdminService;
import swm.hkcc.LGTM.app.modules.auth.constants.TokenType;
import swm.hkcc.LGTM.app.modules.auth.utils.jwt.TokenProvider;
import swm.hkcc.LGTM.app.modules.member.domain.Authority;
import swm.hkcc.LGTM.app.modules.member.domain.Junior;
import swm.hkcc.LGTM.app.modules.member.domain.Member;
import swm.hkcc.LGTM.app.modules.member.repository.MemberRepository;
import swm.hkcc.LGTM.app.modules.suggestion.dto.SuggestionDto;
import swm.hkcc.LGTM.app.modules.suggestion.dto.SuggestionListDto;
import swm.hkcc.LGTM.app.modules.suggestion.exception.NotExistSuggestion;
import swm.hkcc.LGTM.app.modules.suggestion.service.SuggestionService;
import swm.hkcc.LGTM.utils.CustomMDGenerator;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document;
import static com.epages.restdocs.apispec.ResourceDocumentation.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
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;
import static swm.hkcc.LGTM.utils.CustomMDGenerator.tableHead;
import static swm.hkcc.LGTM.utils.CustomMDGenerator.tableRow;

@Slf4j
@SpringBootTest
@Transactional
@ActiveProfiles("test")
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
public class AdminGetTokenTest {
private MockMvc mockMvc;
@Autowired
private TokenProvider tokenProvider;
@MockBean
private MemberRepository memberRepository;
@MockBean
private AdminService adminService;
private Member mockJunior;
private Member mockSenior;

@BeforeEach
void setUp(@Autowired WebApplicationContext webApplicationContext, RestDocumentationContextProvider restDocumentationContextProvider) {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(springSecurity())
.apply(documentationConfiguration(restDocumentationContextProvider))
.addFilters(new CharacterEncodingFilter("UTF-8", true))
.alwaysDo(print())
.build();
}

@Test
@DisplayName("유저 토큰 발급")
void getToken() throws Exception {
// given
Member junior = getMockJunior();
String token = getMockToken(junior);
given(memberRepository.findOneByGithubId(anyString())).willReturn(Optional.ofNullable(junior));
given(memberRepository.findById(any())).willReturn(Optional.ofNullable(junior));
given(adminService.getMemberToken(any())).willReturn(token);

// when
// then
ResultActions actions = mockMvc.perform(get("/admin/token/{id}", 1L)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data").value(token));

actions.andDo(document("admin-get-token",
preprocessRequest(prettyPrint()), // request JSON 정렬하여 출력
preprocessResponse(prettyPrint()), // response JSON 정렬하여 출력
resource(
ResourceSnippetParameters.builder()
.description("유저 토큰 발급")
.summary("유저 토큰 발급")
.tag("Admin")
.pathParameters(parameterWithName("id").type(SimpleType.NUMBER).description("토큰을 발급받을 유저의 id"))
.responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("responseCode").type(JsonFieldType.NUMBER).description("응답 코드"),
fieldWithPath("message").type(JsonFieldType.STRING).description("응답 메시지"),
fieldWithPath("data").type(JsonFieldType.STRING).description("발급된 토큰")
)
.build())));
}

private Member getMockJunior() {
if (mockJunior == null) {
mockJunior = Member.builder()
.memberId(1L)
.githubId("junior")
.nickName("junior")
.junior(Junior.builder().build())
.build();
mockJunior.setRoles(Collections.singletonList(Authority.builder().name("ROLE_USER").build()));
}
return mockJunior;
}

private Member getMockSenior() {
if (mockSenior == null) {
mockSenior = Member.builder()
.memberId(2L)
.githubId("senior")
.nickName("senior")
.build();
mockSenior.setRoles(Collections.singletonList(Authority.builder().name("ROLE_USER").build()));
}
return mockSenior;
}

private String getMockToken(Member member) {
return tokenProvider.createToken(member.getGithubId(), TokenType.ACCESS_TOKEN);
}

}

0 comments on commit b027c9e

Please sign in to comment.