Skip to content

Commit

Permalink
[test] oauth2 로그인 성공한 순간 지역, 학교 없이 회원가입 처리 확인용 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
khyojun committed Nov 11, 2024
1 parent 4f2987d commit a6e9313
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ResponseEntity<String> login(Authentication authentication,

CustomUserDetails principal = (CustomUserDetails) authentication.getPrincipal();

authService.join(joinRequestDto, principal);
authService.afterRealjoin(joinRequestDto, principal);

response.setHeader("accessToken", principal.getAccessToken());
return ResponseEntity.ok("회원 가입 성공!");
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/gitbal/backend/api/auth/dto/UserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,13 @@ public record UserDto(
Region region,
String majorLanguage,
String nickname,
Long score,
String profile_img,

Introduction introduction) {

public static UserDto of(School school, Region region, String majorLanguage, String nickname, Long score,
public static UserDto of(School school, Region region, String majorLanguage, String nickname,
String profile_img, Introduction introduction) {
return new UserDto(school, region, majorLanguage, nickname, score, profile_img, introduction);
return new UserDto(school, region, majorLanguage, nickname, profile_img, introduction);
}

public static User toEntity(UserDto userDto) {
return User.builder()
.school(userDto.school())
.region(userDto.region())
.majorLanguage(userDto.majorLanguage)
.nickname(userDto.nickname())
.score(userDto.score())
.profile_img(userDto.profile_img())
.build();
}
}
38 changes: 29 additions & 9 deletions src/main/java/gitbal/backend/api/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
Expand All @@ -42,24 +41,47 @@ public class AuthService {


@Transactional
public void join(JoinRequestDto joinRequestDto, CustomUserDetails user) {
public void afterRealjoin(JoinRequestDto joinRequestDto, CustomUserDetails user) {

String nickname = user.getNickname();

User findUser = userRepository.findByNickname(nickname)
.orElseThrow(() -> new JoinException("유저가 존재하지 않습니다."));
if(Boolean.FALSE.equals(findUser.getFirstLogined()))
findUser.toggleLogined();
GitbalApiDto gitbalApiDto = GitbalApiDto.of(userService.calculateUserScore(nickname));
// if(Boolean.FALSE.equals(findUser.getFirstLogined()))
// findUser.toggleLogined();
// GitbalApiDto gitbalApiDto = GitbalApiDto.of(userService.calculateUserScore(nickname));

//loginRequestDto 학교이름, 지역이름, 프로필 이미지 이름
UserDto userDto = initUserDto(joinRequestDto, gitbalApiDto, nickname);
UserDto userDto = initUserDto(joinRequestDto, nickname);
joinUpdate(findUser, userDto);
updateRank();
}


private UserDto initUserDto(JoinRequestDto joinRequestDto, GitbalApiDto gitbalApiDto,
@Transactional
public void earlyJoin(String nickname){
//String nickname = user.getNickname();

User findUser = userRepository.findByNickname(nickname)
.orElseThrow(() -> new JoinException("유저가 존재하지 않습니다."));

if(Boolean.FALSE.equals(findUser.getFirstLogined()))
findUser.toggleLogined();

GitbalApiDto gitbalApiDto = GitbalApiDto.of(userService.calculateUserScore(nickname));

initEarlyUserDto(gitbalApiDto, findUser);
}

private void initEarlyUserDto(GitbalApiDto gitbalApiDto, User user) {
user.updateScore(gitbalApiDto.getScore());
userService.updateUserRank();
userService.updateUserGrade();
}



private UserDto initUserDto(JoinRequestDto joinRequestDto,
String nickname) {

School findSchool = findSchool(joinRequestDto);
Expand All @@ -72,7 +94,6 @@ private UserDto initUserDto(JoinRequestDto joinRequestDto, GitbalApiDto gitbalAp
findRegion,
majorLanguage,
nickname,
gitbalApiDto.getScore(),
userService.findUserImgByUsername(nickname),
userService.findByUserName(nickname).getIntroduction()
);
Expand Down Expand Up @@ -118,7 +139,6 @@ private void joinUpdate(User findUser, UserDto userDto) {
userDto.region(),
userDto.majorLanguage(),
userDto.nickname(),
userDto.score(),
userDto.profile_img(),
0,
userDto.introduction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public ResponseEntity<RegionListPageResponseDto<RegionListDto>> getRegionList()

private List<RegionListDto> convertListToDto(List<Region> regions) {
List<RegionListDto> regionListDtos = new ArrayList<>();

for(int index = 0; index < regions.size(); index++) {
Region region = regions.get(index);
regionListDtos.add(new RegionListDto(
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/gitbal/backend/domain/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,20 @@ public User(School school, Region region,

public void joinUpdateUser(School school, Region region,
String majorLanguage,
String nickname, Long score, String profile_img, int userRank, Introduction introduction) {
String nickname, String profile_img, int userRank, Introduction introduction) {
this.school = school;
this.region = region;
this.majorLanguage = majorLanguage;
this.nickname = nickname;
this.profile_img = profile_img;
this.userRank = userRank;
this.introduction = introduction;
}


public void joinMockUpdateUser(School school, Region region,
String majorLanguage,
String nickname, Long score, String profile_img, int userRank, Introduction introduction) {
this.school = school;
this.region = region;
this.majorLanguage = majorLanguage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void run(String... args) throws Exception {
scoring(user);

// Update user with the new relations
user.joinUpdateUser(school, region, randomMajorLanguagesForUser,
user.joinMockUpdateUser(school, region, randomMajorLanguagesForUser,
user.getNickname(), user.getScore(), user.getProfile_img(), 0, user.getIntroduction());

userRepository.save(user);
Expand Down Expand Up @@ -131,7 +131,7 @@ private void createUserWithNickname(String nickName) {

String randomMajorLanguagesForUser = createRandomMajorLanguagesForUser(user);

user.joinUpdateUser(school, region, randomMajorLanguagesForUser,
user.joinMockUpdateUser(school, region, randomMajorLanguagesForUser,
user.getNickname(), user.getScore(), user.getProfile_img(), 0, introductionRepository.createIntroductionAndReturn());
userRepository.save(user);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gitbal.backend.global.security;

import gitbal.backend.api.auth.service.AuthService;
import gitbal.backend.api.auth.service.LoginService;
import gitbal.backend.domain.user.User;
import gitbal.backend.domain.user.UserRepository;
Expand Down Expand Up @@ -33,6 +34,7 @@ public class OAuth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationS
private final JwtTokenProvider jwtTokenProvider;
private final UserService userService;
private final LoginService loginService;
private final AuthService authService;

@Override
@Transactional
Expand All @@ -46,6 +48,9 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
updateUser(githubOAuth2UserInfo);
tokenRefresh(githubOAuth2UserInfo);
}

authService.earlyJoin(githubOAuth2UserInfo.getNickname());

String url = loginService.madeRedirectUrl(githubOAuth2UserInfo.getNickname());


Expand Down

0 comments on commit a6e9313

Please sign in to comment.