-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from PLADI-ALM/test/PDS-81-login
[PDS-81/test] 로그인 API 성공 단위 테스트
- Loading branch information
Showing
5 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/main/java/com/example/pladialmserver/user/dto/request/EmailPWReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
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; | ||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.Pattern; | ||
|
||
@Data | ||
@Builder | ||
public class EmailPWReq { | ||
@Schema(type = "String", description = "이메일", example = "[email protected]", required = true) | ||
@Email(message = "U0002") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
src/test/java/com/example/pladialmserver/user/service/UserServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("[email protected]", "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() { | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/com/example/pladialmserver/user/service/model/TestUserInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("[email protected]") | ||
.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(); | ||
} | ||
} |