diff --git a/src/main/java/com/example/pladialmserver/user/dto/TokenDto.java b/src/main/java/com/example/pladialmserver/user/dto/TokenDto.java index 07c722cb..f451fa51 100644 --- a/src/main/java/com/example/pladialmserver/user/dto/TokenDto.java +++ b/src/main/java/com/example/pladialmserver/user/dto/TokenDto.java @@ -2,8 +2,10 @@ import com.example.pladialmserver.user.entity.Role; import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; +import lombok.NoArgsConstructor; import javax.validation.constraints.NotBlank; diff --git a/src/main/java/com/example/pladialmserver/user/dto/request/EmailPWReq.java b/src/main/java/com/example/pladialmserver/user/dto/request/EmailPWReq.java index 1dc78fc0..2385d7a8 100644 --- a/src/main/java/com/example/pladialmserver/user/dto/request/EmailPWReq.java +++ b/src/main/java/com/example/pladialmserver/user/dto/request/EmailPWReq.java @@ -1,6 +1,7 @@ package com.example.pladialmserver.user.dto.request; import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Builder; import lombok.Data; import javax.validation.constraints.Email; @@ -8,6 +9,7 @@ import javax.validation.constraints.Pattern; @Data +@Builder public class EmailPWReq { @Schema(type = "String", description = "이메일", example = "1234@email.com", required = true) @Email(message = "U0002") diff --git a/src/main/java/com/example/pladialmserver/user/entity/User.java b/src/main/java/com/example/pladialmserver/user/entity/User.java index 8cfe8783..47a07eeb 100644 --- a/src/main/java/com/example/pladialmserver/user/entity/User.java +++ b/src/main/java/com/example/pladialmserver/user/entity/User.java @@ -71,7 +71,8 @@ public class User extends BaseEntity { private List officeBookingList = new ArrayList<>(); @Builder - public User(String name, String email, String password, Department department, String phone, Role role, String fcmToken, String assets, Affiliation affiliation) { + public User(Long userId, String name, String email, String password, Department department, String phone, Role role, String fcmToken, String assets, Affiliation affiliation) { + this.userId = userId; this.name = name; this.email = email; this.password = password; diff --git a/src/test/java/com/example/pladialmserver/user/service/UserServiceTest.java b/src/test/java/com/example/pladialmserver/user/service/UserServiceTest.java new file mode 100644 index 00000000..b5aacf67 --- /dev/null +++ b/src/test/java/com/example/pladialmserver/user/service/UserServiceTest.java @@ -0,0 +1,147 @@ +package com.example.pladialmserver.user.service; + +import com.example.pladialmserver.global.utils.JwtUtil; +import com.example.pladialmserver.user.dto.TokenDto; +import com.example.pladialmserver.user.dto.request.EmailPWReq; +import com.example.pladialmserver.user.entity.Role; +import com.example.pladialmserver.user.entity.User; +import com.example.pladialmserver.user.repository.AffiliationRepository; +import com.example.pladialmserver.user.repository.DepartmentRepository; +import com.example.pladialmserver.user.repository.user.UserRepository; +import org.junit.jupiter.api.AfterEach; +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.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; + +import java.util.Optional; + +import static com.example.pladialmserver.user.service.model.TestUserInfo.*; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class UserServiceTest { + @InjectMocks + private UserService userService; + @Mock + private UserRepository userRepository; + @Mock + private AffiliationRepository affiliationRepository; + @Mock + private DepartmentRepository departmentRepository; + @Mock + JwtUtil jwtUtil; + @Spy + BCryptPasswordEncoder passwordEncoder; + + @BeforeEach + void setUp() { + } + + @AfterEach + void tearDown() { + } + + @Test + @DisplayName("[성공] 로그인") + void login(){ + // given + User user = setUpUser(setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD)); + EmailPWReq req = setUpEmailPWReq("test@email.com", "asdf1234!"); + + // when + // stub 생성 + doReturn(Optional.of(user)).when(userRepository).findByEmailAndIsEnable(req.getEmail(), true); + when(jwtUtil.createToken(user.getUserId(), user.getRole())).thenReturn(TokenDto.toDto("accessToken", "refreshToken", user.getRole())); + TokenDto dto = userService.login(req); + + // then + assertThat(req.getEmail()).isEqualTo(user.getEmail()); + assertThat(passwordEncoder.matches(req.getPassword(), user.getPassword())).isTrue(); + assertThat(dto.getAccessToken()).isEqualTo("accessToken"); + assertThat(dto.getRefreshToken()).isEqualTo("refreshToken"); + + // verify + verify(userRepository, times(1)).findByEmailAndIsEnable(any(String.class), any(Boolean.class)); + verify(jwtUtil, times(1)).createToken(any(Long.class), any(Role.class)); + verify(passwordEncoder, times(1)).encode(any(String.class)); + } + + @Test + void getUserName() { + } + + @Test + void setExpiredToken() { + } + + @Test + void reissue() { + } + + @Test + void verifyEmail() { + } + + @Test + void checkEmailCode() { + } + + @Test + void resetPassword() { + } + + @Test + void getResponsibilityList() { + } + + @Test + void resignUser() { + } + + @Test + void updateUser() { + } + + @Test + void getUserInfo() { + } + + @Test + void sendAssetsEmail() { + } + + @Test + void createUser() { + } + + @Test + void updateUserByAdmin() { + } + + @Test + void getDepartmentList() { + } + + @Test + void getUserList() { + } + + @Test + void getUserInfoByAdmin() { + } + + @Test + void resignUserByAdmin() { + } + + @Test + void getUserNotification() { + } +} \ No newline at end of file diff --git a/src/test/java/com/example/pladialmserver/user/service/model/TestUserInfo.java b/src/test/java/com/example/pladialmserver/user/service/model/TestUserInfo.java new file mode 100644 index 00000000..f131df0d --- /dev/null +++ b/src/test/java/com/example/pladialmserver/user/service/model/TestUserInfo.java @@ -0,0 +1,48 @@ +package com.example.pladialmserver.user.service.model; + +import com.example.pladialmserver.user.dto.request.EmailPWReq; +import com.example.pladialmserver.user.entity.Affiliation; +import com.example.pladialmserver.user.entity.Department; +import com.example.pladialmserver.user.entity.Role; +import com.example.pladialmserver.user.entity.User; + +public class TestUserInfo { + + public static final String PASSWORD = "asdf1234!"; + + + public static EmailPWReq setUpEmailPWReq(String email, String pw){ + return EmailPWReq.builder() + .email(email) + .password(pw) + .fcmToken("1234545") + .build(); + } + + public static User setUpUser(Department department, Affiliation affiliation, String password){ + return User.builder() + .userId(1L) + .name("홍길동") + .email("test@email.com") + .password(password) + .fcmToken("1234545") + .phone("010-0000-0000") + .department(department) + .role(Role.ADMIN) + .assets("A12345") + .affiliation(affiliation) + .build(); + } + + public static Department setUpDepartment(){ + return Department.builder() + .name("마케팅") + .build(); + } + + public static Affiliation setUpAffiliation(){ + return Affiliation.builder() + .name("플래디") + .build(); + } +}