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

[JT-81] AuthenticationFilter 테스트 작성 #70

Merged
merged 4 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import shop.jtoon.dto.MemberDto;
import shop.jtoon.dto.SignUpDto;
import shop.jtoon.dto.OAuthSignUpDto;
import shop.jtoon.entity.LoginType;
import shop.jtoon.entity.Member;
import shop.jtoon.exception.DuplicatedException;
import shop.jtoon.exception.InvalidRequestException;
import shop.jtoon.exception.NotFoundException;
import shop.jtoon.member.request.SignUpReq;
import shop.jtoon.member.request.LocalSignUpReq;
import shop.jtoon.repository.MemberRepository;
import shop.jtoon.security.request.LoginReq;
import shop.jtoon.security.service.JwtService;
Expand All @@ -35,10 +35,10 @@ public class MemberService {
private final PasswordEncoder passwordEncoder;

@Transactional
public void signUp(SignUpReq signUpReq) {
validateDuplicateEmail(signUpReq.email());
String encryptedPassword = passwordEncoder.encode(signUpReq.password());
Member member = signUpReq.toEntity(encryptedPassword);
public void signUp(LocalSignUpReq localSignUpReq) {
validateDuplicateEmail(localSignUpReq.email());
String encryptedPassword = passwordEncoder.encode(localSignUpReq.password());
Member member = localSignUpReq.toEntity(encryptedPassword);

memberRepository.save(member);
}
Expand Down Expand Up @@ -72,10 +72,10 @@ public void loginMember(LoginReq loginReq, HttpServletResponse response) {
}

@Transactional
public Member generateOrGetSocialMember(SignUpDto signUpDto) {
Optional<Member> member = memberRepository.findByEmail(signUpDto.email());
public Member generateOrGetSocialMember(OAuthSignUpDto OAuthSignUpDto) {
Optional<Member> member = memberRepository.findByEmail(OAuthSignUpDto.email());

return member.orElseGet(() -> memberRepository.save(signUpDto.toEntity(BLANK)));
return member.orElseGet(() -> memberRepository.save(OAuthSignUpDto.toEntity(BLANK)));
}

public MemberDto findMemberDtoByEmail(String email) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import lombok.RequiredArgsConstructor;
import shop.jtoon.member.application.EmailService;
import shop.jtoon.member.application.MemberService;
import shop.jtoon.member.request.SignUpReq;
import shop.jtoon.member.request.LocalSignUpReq;
import shop.jtoon.security.request.LoginReq;

@RestController
Expand All @@ -27,8 +27,8 @@ public class MemberController {

@PostMapping("/sign-up")
@ResponseStatus(HttpStatus.CREATED)
public void signUp(@RequestBody @Valid SignUpReq signUpReq) {
memberService.signUp(signUpReq);
public void signUp(@RequestBody @Valid LocalSignUpReq localSignUpReq) {
memberService.signUp(localSignUpReq);
}

@GetMapping("/email-authorization")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import shop.jtoon.entity.Member;
import shop.jtoon.entity.Role;

public record SignUpReq(
public record LocalSignUpReq(
@Pattern(regexp = EMAIL_PATTERN) String email,
@Pattern(regexp = PASSWORD_PATTERN) String password,
@NotBlank @Size(max = 10) String name,
Expand Down
3 changes: 3 additions & 0 deletions module-domain/domain-member/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ dependencies {

// Test
testImplementation 'org.springframework.boot:spring-boot-starter-test'

// H2
testImplementation 'com.h2database:h2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.util.Map;
import java.util.UUID;

import lombok.AccessLevel;
import lombok.Builder;
import shop.jtoon.entity.LoginType;
Expand Down Expand Up @@ -56,8 +55,8 @@ private static OAuthAttributes ofKakao(LoginType loginType, String nameAttribute
return null;
}

public SignUpDto toSignUpDto() {
return SignUpDto.builder()
public OAuthSignUpDto toSignUpDto() {
return OAuthSignUpDto.builder()
.email(this.email)
.password(UUID.randomUUID().toString())
.name(this.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import shop.jtoon.entity.Role;

@Builder
public record SignUpDto(
public record OAuthSignUpDto(
String email,
String password,
String name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package shop.jtoon;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MemberDomainApplicationTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package shop.jtoon.repository;

import static org.assertj.core.api.Assertions.*;
import static shop.jtoon.entity.Gender.*;
import static shop.jtoon.entity.LoginType.*;
import static shop.jtoon.entity.Role.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;

import shop.jtoon.config.JpaConfig;
import shop.jtoon.entity.Gender;
import shop.jtoon.entity.LoginType;
import shop.jtoon.entity.Member;
import shop.jtoon.entity.Role;

@DataJpaTest
@Import(JpaConfig.class)
class MemberRepositoryTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기본 JPA Repo test까지 하셨네요.. 대박!


@Autowired
MemberRepository memberRepository;

Member member;

@BeforeEach
void initMember() {
member = Member.builder()
.email("abc@naver.com")
.password("Testing!123")
.name("HI")
.nickname("Unique")
.gender(MALE)
.phone("01012345678")
.role(USER)
.loginType(LOCAL)
.build();
}

@Test
@DisplayName("MemberRepository 빈 등록 성공")
void MemberRepository_bean_NotNull() {
assertThat(memberRepository).isNotNull();
}

@Test
@DisplayName("멤버 저장 성공")
void MemberRepository_save_success() {
// given, when
Member saved = memberRepository.save(member);

// then
assertThat(saved.getId()).isNotNull();
}

@Nested
@DisplayName("멤버 엔티티 생성 실패 테스트")
class MemberEntityTest {

String email = "abc@gmail.com";
String password = "testing!123";
String name = "HI";
String nickname = "Unique";
Gender gender = MALE;
String phone = "01012345678";
Role role = USER;
LoginType loginType = LOCAL;

@Test
@DisplayName("멤버 엔티티 이메일 null 테스트")
void Member_email_null_fail() {
// given, when, then
assertThatThrownBy(() -> Member.builder()
.email(null)
.password(password)
.name(name)
.nickname(nickname)
.gender(gender)
.phone(phone)
.role(role)
.loginType(loginType)
.build()).isInstanceOf(NullPointerException.class);
}

@Test
@DisplayName("멤버 엔티티 비밀번호 null 테스트")
void Member_password_null_fail() {
// given, when, then
assertThatThrownBy(() -> Member.builder()
.email(email)
.password(null)
.name(name)
.nickname(nickname)
.gender(gender)
.phone(phone)
.role(role)
.loginType(loginType)
.build()).isInstanceOf(NullPointerException.class);
}

@Test
@DisplayName("멤버 엔티티 이름 null 테스트")
void Member_name_null_fail() {
// given, when, then
assertThatThrownBy(() -> Member.builder()
.email(email)
.password(password)
.name(null)
.nickname(nickname)
.gender(gender)
.phone(phone)
.role(role)
.loginType(loginType)
.build()).isInstanceOf(NullPointerException.class);
}

@Test
@DisplayName("멤버 엔티티 닉네임 null 테스트")
void Member_nickname_null_fail() {
// given, when, then
assertThatThrownBy(() -> Member.builder()
.email(email)
.password(password)
.name(name)
.nickname(null)
.gender(gender)
.phone(phone)
.role(role)
.loginType(loginType)
.build()).isInstanceOf(NullPointerException.class);
}

@Test
@DisplayName("멤버 엔티티 성별 null 테스트")
void Member_gender_null_fail() {
// given, when, then
assertThatThrownBy(() -> Member.builder()
.email(email)
.password(password)
.name(name)
.nickname(nickname)
.gender(null)
.phone(phone)
.role(role)
.loginType(loginType)
.build()).isInstanceOf(NullPointerException.class);
}

@Test
@DisplayName("멤버 엔티티 전화번호 null 테스트")
void Member_phone_null_fail() {
// given, when, then
assertThatThrownBy(() -> Member.builder()
.email(email)
.password(password)
.name(name)
.nickname(nickname)
.gender(gender)
.phone(null)
.role(role)
.loginType(loginType)
.build()).isInstanceOf(NullPointerException.class);
}
}
}
3 changes: 3 additions & 0 deletions module-internal/core-web/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ dependencies {

// OAuth2
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'

// Test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.servlet.HandlerExceptionResolver;

Expand Down Expand Up @@ -59,6 +61,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
authenticationService,
jwtService),
UsernamePasswordAuthenticationFilter.class)
.exceptionHandling(exceptionHandling -> exceptionHandling
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED)))
.oauth2Login(login -> login
.userInfoEndpoint(endpoint -> endpoint.userService(customOAuth2UserService))
.successHandler(oAuth2SuccessHandler)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package shop.jtoon.security;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SecurityApplicationTest {
}
Loading