Skip to content

Commit

Permalink
Merge pull request #194 from PlanIt-Project/BE_test#192
Browse files Browse the repository at this point in the history
Be test#192
  • Loading branch information
moonjin-kim authored Apr 18, 2024
2 parents 4d193d8 + 173bc9a commit 4c8b522
Show file tree
Hide file tree
Showing 26 changed files with 4,232 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ build/
src/main/resources/application-dev.yml
src/main/resources/application-test.yml
src/main/logs
src/main/logs/**
src/main/image
src/main/image/**
### STS ###
.apt_generated
.classpath
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation "io.jsonwebtoken:jjwt:0.9.1"
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
testImplementation 'org.springframework.security:spring-security-test'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
1,838 changes: 1,838 additions & 0 deletions logs/access-2024-04-14.0.log

Large diffs are not rendered by default.

918 changes: 918 additions & 0 deletions logs/access-2024-04-17.0.log

Large diffs are not rendered by default.

612 changes: 612 additions & 0 deletions logs/access-2024-04-18.0.log

Large diffs are not rendered by default.

Empty file added logs/error-2024-04-14.0.log
Empty file.
Empty file added logs/error-2024-04-17.0.log
Empty file.
Empty file added logs/error-2024-04-18.0.log
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.sideProject.PlanIT.common.security;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.IOException;

public class MockSpringSecurityFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) {}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
SecurityContextHolder.getContext()
.setAuthentication((Authentication) ((HttpServletRequest) req).getUserPrincipal());
chain.doFilter(req, res);
}

@Override
public void destroy() {
SecurityContextHolder.clearContext();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ public class ProgramAdminController {
public ApiResponse<Page<ProgramResponseDto>> find(
@RequestParam(value = "option", required = false, defaultValue = "VALID") ProgramSearchStatus option,
@PageableDefault(size = 10) Pageable pageable,
Principal principal) {
Long id = Long.parseLong(principal.getName());

Authentication authentication) {
Long id = 1L;
return ApiResponse.ok(
programService.find(id,option,pageable)
);
Expand Down Expand Up @@ -76,7 +75,7 @@ public ApiResponse<String> refund(@PathVariable("id") Long id) {
@PutMapping("/{id}")
public ApiResponse<String> modify(
@PathVariable("id") Long id,
ProgramModifyRequestDto request) {
@Valid @RequestBody ProgramModifyRequestDto request) {
return ApiResponse.ok(
programService.modify(id, request)
);
Expand All @@ -102,7 +101,7 @@ public ApiResponse<Page<FindRegistrationResponseDto>> findRegistration(
}


@GetMapping("/registration/{id}")
@GetMapping("/registration/user/{id}")
public ApiResponse<Page<FindRegistrationResponseDto>> findRegistrationByUser(
@PathVariable("id") Long id,
@PageableDefault(size = 10) Pageable pageable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public class ApproveRequestDto {
public ApproveRequestDto(Long trainer) {
this.trainer = trainer;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class ProgramModifyRequestDto {

@NotNull
@NotNull(message = "시작시간이 없습니다.")
String startTime;
@NotNull(message = "시작시간이 없습니다.")
String endTime;
@NotNull(message = "회원 정보가 없습니다.")
Long memberId;
@NotNull(message = "트레이너 정보가 없습니다.")
Long employId;

@Builder
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import com.sideProject.PlanIT.domain.user.entity.Member;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Optional;

@Getter
@NoArgsConstructor
public class ProgramResponseDto {
long id;
String productName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class RegistrationResponseDto {
Long id;
String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public Long suspendProgram(Long id, LocalDate now) {
@Override
public Long resumeProgram(Long id, LocalDate now) {
Program program = programRepository.findById(id).orElseThrow(() ->
new CustomException("존재하지 않는 프로그램입니다.", ErrorCode.PROGRAM_NOT_FOUND)
new CustomException(id+"는 존재하지 않는 프로그램입니다.", ErrorCode.PROGRAM_NOT_FOUND)
);

if(program.getStatus() != ProgramStatus.SUSPEND) {
Expand All @@ -205,10 +205,6 @@ public Long resumeProgram(Long id, LocalDate now) {
public Long approve(Long registrationId, Long trainerId, LocalDateTime localDateTime) {
Registration registration = getRegistrationById(registrationId);

Member member = memberRepository.findById(registration.getMember().getId()).orElseThrow(() ->
new CustomException(registration.getMember().getId() + "는 존재하지 않는 회원입니다.", ErrorCode.MEMBER_NOT_FOUND)
);

Employee trainer = null;
if(registration.getProduct().getType().equals(ProductType.PT)) {
if(trainerId == null) {
Expand Down Expand Up @@ -239,7 +235,7 @@ public Long approve(Long registrationId, Long trainerId, LocalDateTime localDate
.remainedNumber(product.getNumber())
.product(product)
.employee(trainer)
.member(member)
.member(registration.getMember())
.registration(registration)
.status(ProgramStatus.IN_PROGRESS)
.startAt(startAt)
Expand Down Expand Up @@ -352,7 +348,7 @@ private void validMemberAccess(Member member, Program program) {
private Page<Program> findProgramByEmploy(Member member, ProgramSearchStatus option,Pageable pageable) {
log.info("findEmployeeByMemberId = {}" , member.getId());
Employee employee = employeeRepository.findByMemberId(member.getId()).orElseThrow(()->
new CustomException("존재하지 않는 직원입니다.", ErrorCode.EMPLOYEE_NOT_FOUND)
new CustomException("회원 "+member.getId()+"의 직원정보가 없습니다.", ErrorCode.EMPLOYEE_NOT_FOUND)
);
//ALL이면 모든 상태 조회, VALID이면 IN_PROCESS인 경우만 조회
if(option == ProgramSearchStatus.ALL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@Getter
@NoArgsConstructor
public class ChangeReservationRequestDto {
@NotNull(message = "예약 날짜가 주어지지 않았습니다.")
@DateTimeFormat(pattern = "yyyy-MM-dd")
LocalDate reservationDate;

@DateTimeFormat(pattern = "HH:mm")
List<LocalTime> reservationTimes;

@Builder
public ChangeReservationRequestDto(LocalDate reservationDate, List<LocalTime> reservationTimes) {
this.reservationDate = reservationDate;
this.reservationTimes = reservationTimes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class ReservationRequestDto {
@NotNull(message = "프로그램이 주어지지 않았습니다.")
Long programId;

@Builder
public ReservationRequestDto(Long programId) {
this.programId = programId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.sideProject.PlanIT.domain.controller_test_module;

import com.sideProject.PlanIT.domain.user.entity.Member;
import com.sideProject.PlanIT.domain.user.entity.enums.MemberRole;
import org.springframework.security.test.context.support.WithSecurityContext;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@WithSecurityContext(factory = WithMockCustomSecurityContextFactory.class)
public @interface WithMockCustomMember {
String first() default "name";

MemberRole second() default MemberRole.MEMBER;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.sideProject.PlanIT.domain.controller_test_module;

import com.sideProject.PlanIT.domain.user.entity.Member;
import com.sideProject.PlanIT.domain.user.entity.enums.Gender;
import com.sideProject.PlanIT.domain.user.entity.enums.MemberRole;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.support.WithSecurityContextFactory;

import java.util.Collection;
import java.util.Collections;

public class WithMockCustomSecurityContextFactory implements WithSecurityContextFactory<WithMockCustomMember> {
@Override
public SecurityContext createSecurityContext(WithMockCustomMember annotation) {
SecurityContext context = SecurityContextHolder.createEmptyContext();
Member member = new Member(annotation.first(),"[email protected]","test","01000000000",null,"",annotation.second(), Gender.MALE);
Collection<GrantedAuthority> authorities = Collections.singleton(new SimpleGrantedAuthority(member.getRole().toString()));
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(member.getId(), null, authorities);
context.setAuthentication(authenticationToken);

return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.StandardCharsets;
import java.time.LocalDate;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -61,7 +62,7 @@ Banner initNotInTimeBanner(String title, String imagePath) {
}

MultipartFile createMockMutipartFile(String name) {
return new MockMultipartFile(name, (byte[]) null);
return new MockMultipartFile("file", "test.txt", "text/plain", "test file".getBytes(StandardCharsets.UTF_8) );
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void findProductTest2() {
// when, then
assertThatThrownBy(() -> productService.findProduct(productId))
.isInstanceOf(CustomException.class)
.hasMessage("상품이 존재하지 않습니다.");
.hasMessage(productId+"는 존재하지 않는 상품입니다.");
}
}
}
Loading

0 comments on commit 4c8b522

Please sign in to comment.