Skip to content

Commit

Permalink
Merge pull request #229 from PLADI-ALM/test/PDS-81-login-fail
Browse files Browse the repository at this point in the history
[PDS-81/test] 로그인 실패 코드 작성
  • Loading branch information
dangnak2 authored Nov 25, 2023
2 parents 33cbf6d + b42e4e9 commit c645eb1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public class User extends BaseEntity {
private List<OfficeBooking> officeBookingList = new ArrayList<>();

@Builder
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;
public User(String name, String email, String password, Department department, String phone, Role role, String fcmToken, String assets, Affiliation affiliation) {
this.name = name;
this.email = email;
this.password = password;
Expand All @@ -84,7 +83,7 @@ public User(Long userId, String name, String email, String password, Department
this.affiliation = affiliation;
}

public User(Long userId, String name, String email, String password, String phone, String assets, Department department, Role role, Affiliation affiliation) {
public User(Long userId, String name, String email, String password, String phone, String assets, Department department, Role role, Affiliation affiliation, String fcmToken) {
this.userId = userId;
this.name = name;
this.email = email;
Expand All @@ -94,6 +93,7 @@ public User(Long userId, String name, String email, String password, String phon
this.department = department;
this.role = role;
this.affiliation = affiliation;
this.fcmToken = fcmToken;
}

public static User toEntity(CreateUserReq req, Department department, Affiliation affiliation){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ void getProductBookings() {
@DisplayName("[성공] 장비 예약 개별 조회")
void getProductBookingDetail_SUCCESS() {
// given
User basicUser = setUpBasicUser(setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User adminUser = setUpUser(setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User basicUser = setUpUser(1L, Role.BASIC, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User adminUser = setUpUser(2L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
ResourceBooking resourceBooking = setUpResourceBooking(basicUser, adminUser);
// when
doReturn(Optional.of(resourceBooking)).when(resourceBookingRepository).findById(resourceBooking.getResourceBookingId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.pladialmserver.equipment.repository.EquipmentCategoryRepository;
import com.example.pladialmserver.equipment.repository.EquipmentRepository;
import com.example.pladialmserver.global.utils.JwtUtil;
import com.example.pladialmserver.user.entity.Role;
import com.example.pladialmserver.user.entity.User;
import com.example.pladialmserver.user.repository.user.UserRepository;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -49,7 +50,7 @@ void tearDown() {
@DisplayName("[성공] 구매 비품 추가")
void registerEquipmentSuccess() {
// given
User user = setUpUser(setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User user = setUpUser(1L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
RegisterEquipmentReq req = setUpRegisterEquipmentInfo("맥심커피", "10박스", "기타", "S1305", "맥심커피입니다.", "photo/maxim.png");

// when
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.pladialmserver.user.service;

import com.example.pladialmserver.global.exception.BaseException;
import com.example.pladialmserver.global.exception.BaseResponseCode;
import com.example.pladialmserver.global.utils.JwtUtil;
import com.example.pladialmserver.user.dto.TokenDto;
import com.example.pladialmserver.user.dto.request.EmailPWReq;
Expand All @@ -23,6 +25,7 @@

import static com.example.pladialmserver.user.service.model.TestUserInfo.*;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -52,7 +55,7 @@ void tearDown() {
@DisplayName("[성공] 로그인")
void login(){
// given
User user = setUpUser(setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User user = setUpUser(1L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
EmailPWReq req = setUpEmailPWReq("[email protected]", "asdf1234!");

// when
Expand All @@ -72,6 +75,21 @@ void login(){
verify(jwtUtil, times(1)).createToken(any(Long.class), any(Role.class));
verify(passwordEncoder, times(1)).encode(any(String.class));
}
@Test
@DisplayName("[실패] 로그인")
void loginFail(){
// given
User user = setUpUser(1L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
EmailPWReq req = setUpEmailPWReq("[email protected]", "asdf1234!");
// when
doThrow(new BaseException(BaseResponseCode.USER_NOT_FOUND)).when(userRepository).findByEmailAndIsEnable(req.getEmail(), true);
// then
BaseException exception = assertThrows(BaseException.class, () -> {
userService.login(req);
});
assertThat(exception.getBaseResponseCode()).isEqualTo(BaseResponseCode.USER_NOT_FOUND);
}


@Test
void getUserName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,18 @@ public static EmailPWReq setUpEmailPWReq(String email, String pw){
.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 User setUpUser(Long userId, Role role, Department department, Affiliation affiliation, String password){
return new User(
userId,
"홍길동",
"[email protected]",
password,
department,
"010-0000-0000",
role,
"1234545",
"A12345",
affiliation);
}

public static User setUpBasicUser(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.BASIC)
.assets("A12345")
.affiliation(affiliation)
.build();
department,
role,
affiliation,
"1234545");
}

public static Department setUpDepartment(){
Expand Down

0 comments on commit c645eb1

Please sign in to comment.