Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: user & oauth #20

Closed
2 tasks done
Zerohertz opened this issue Nov 1, 2024 · 2 comments · Fixed by #34 or #35
Closed
2 tasks done

refactor: user & oauth #20

Zerohertz opened this issue Nov 1, 2024 · 2 comments · Fixed by #34 or #35

Comments

@Zerohertz
Copy link
Member

Zerohertz commented Nov 1, 2024

To Do

  • Dto, Controller swagger annotation 추가
  • Exception → service layer로 이동
@Zerohertz
Copy link
Member Author

@ssoheeh

@Transactional
public User loginOrRegister(GithubDto githubDto, String githubToken) {
return userRepository.findByUsername(githubDto.getUsername())
.orElseGet(() -> registerNewUser(githubDto, githubToken));
}

ResponseEntity<GithubDto> userInfoResponse = restTemplate.exchange(
"https://api.github.com/user",
HttpMethod.GET,
getUserInfo(githubAccessToken),
GithubDto.class);
GithubDto githubDto = userInfoResponse.getBody();
log.info("Received user info. Username: " + githubDto.getUsername());
// 사용자 등록 또는 로그인 처리
User user = userService.loginOrRegister(githubDto, githubAccessToken);

재 로그인 시 githubToken 값은 변경되지 않나요?

@Zerohertz
Copy link
Member Author

@Test
@DisplayName("이미 존재하는 사용자가 로그인하는 상황에 새롭게 DB에 생성하지 않고 조회하여 반환")
void loginOrRegister_existingUser_returnsUser() {
/* given */
String username = "User1";
String email = "[email protected]";
String thumbnail = "https://avatars.githubusercontent.com/u/0?v=4";
String githubTokenOld = "gho_nGh9jQ4tXYi9GIPe9cgbur5Mf2RHPW2x8iOn";
String githubTokenNew = "gho_dCZucktIcoFI2hqhCm6ATBw7Z08ecT20dJt3";
GithubDto githubDto = GithubDto.builder()
.username(username)
.email(email)
.thumbnail(thumbnail)
.build();
User existingUser = User.builder()
.username(username)
.email(email)
.thumbnail(thumbnail)
.oauth("github")
.githubToken(githubTokenOld)
.build();
given(userRepository.findByUsername(username)).willReturn(Optional.of(existingUser));
/* when */
User result = userService.loginOrRegister(githubDto, githubTokenNew);
/* then */
then(userRepository).should(never()).save(any(User.class));
assertThat(result)
.usingRecursiveComparison()
.ignoringFields("githubToken")
.isEqualTo(existingUser);
// FIXME: 해당 시나리오에서 제대로 동작하고 있지 않음. (related: #23)
// 이미 존재하는 사용자가 다시 로그인할 때 새로운 깃허브 토큰을 사용해야하는데 잘 이뤄지지 않고 있음.
// assertThat(result.getGithubToken()).isNotEqualTo(existingUser.getGithubToken());
// assertThat(result).isNotEqualTo(existingUser);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment