Skip to content

Commit

Permalink
Implement anonymous sign-up API (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinlee1703 authored Oct 21, 2023
2 parents cbf12ef + 4a0b740 commit d48f87b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,18 @@ public ResponseEntity<Message> signOut(HttpServletRequest request, @RequestBody
.data(null)
.build());
}

@PostMapping("/sign-up/anonymous")
@Operation(summary = "비회원 회원가입 메서드", description = "비회원 회원가입을 하기 위한 메서드입니다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "회원가입 성공"),
})
public ResponseEntity<Message> signUpAnonymous(HttpServletRequest request) {
JwtTokenDto jwtTokenDto = authService.signUpAnonymous();
return ResponseEntity.ok(
SuccessMessage.builder()
.path(request.getRequestURI())
.data(jwtTokenDto)
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,21 @@ public void signOut(String username, SignOutDto signOutDto) {
}
refreshTokenService.deleteRefreshToken(username);
}

/**
* 비회원 회원가입
* @return
*/
public JwtTokenDto signUpAnonymous() {
User user = User.builder()
.username(userService.generateOAuthUsername())
.password(authConfig.getDefaultPassword())
.nickname(authConfig.getDefaultNickname())
.role(Role.ANONYMOUS)
.build();
User savedUser = userService.create(user);
JwtTokenDto jwtTokenDto = generateJwtTokenDto(savedUser);
refreshTokenService.storeRefreshToken(jwtTokenDto, user.getUsername());
return jwtTokenDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public enum Role {
ADMIN("ROLE_ADMIN,ROLE_USER,ROLE_TEMPORARY"),
USER("ROLE_USER,ROLE_TEMPORARY"),
TEMPORARY("ROLE_TEMPORARY");
ANONYMOUS("ROLE_ANONYMOUS");

private String value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
.authorizeHttpRequests(authorizeHttpRequests ->
authorizeHttpRequests.requestMatchers(
"/auth/sign-up",
"/auth/sign-up/anonymous",
"/auth/sign-in",
"/auth/sign-in/**",
"/giftcards/**",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,25 @@ public void signOutTest() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(signOutDto)))
.andExpect(status().isOk());
}

@Test
@WithMockUser(username = "test", roles = "ANONYMOUS")
void signUpAnonymous() throws Exception {
// given
JwtTokenDto jwtTokenDto = JwtTokenDto.builder()
.accessToken("myawesomejwt")
.refreshToken("myawesomejwt")
.build();

// when
when(authService.signUpAnonymous()).thenReturn(jwtTokenDto);

// then
mockMvc.perform(post("/auth/sign-up/anonymous")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.access_token").exists())
.andExpect(jsonPath("$.data.refresh_token").exists());
}
}

0 comments on commit d48f87b

Please sign in to comment.