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

[DPMBE-63] 이미지 관련 테스트 코드를 작성한다 #78

Merged
merged 15 commits into from
Jun 13, 2023

Conversation

kdomo
Copy link
Member

@kdomo kdomo commented Jun 12, 2023

개요

작업사항

  • S3Properties 테스트코드 추가
  • PresignUrl 관련 Controller, UseCase, Service 테스트코드 추가
  • 이미지 성공적으로 올렸을때 요청하는 API UseCase, Service, Adapter 테스트코드 추가

변경로직

  • PictureAdapter 파일명을 PromiseAdapter로 실수해버려서 수정했슴니당😥
  • PictureDomainService 내의 validate 하는 부분에서 throw 키워드 누락되어 추가하였습니다

참고

AutoConfigureMockMvc 관련

AutoConfigureMockMvc는 mock 테스트 시 필요한 의존성을 불러오는 어노테이션입니다.

하지만 디폴트로 우리가 만든 Security Filter가 아닌 디플트로 지정된 Filter가 불러와집니다.

그렇게 되면서 csrf().disable() 설정을 적용하였지만 디폴트에서는 켜져있기때문에 csrf토큰이 없어서 403 에러가 발생합니다 (POST, DELETE 요청에 한해서)

따라서 Controller Test에서는 Filter를 불러오지 않고 추 후 Security 테스트를 넣기로 결정하였습니당

https://kth990303.tistory.com/408
https://stackoverflow.com/questions/21749781/why-i-received-an-error-403-with-mockmvc-and-junit

@kdomo kdomo added the test test 코드 작성 label Jun 12, 2023
@kdomo kdomo self-assigned this Jun 12, 2023
Copy link
Member

@ImNM ImNM left a comment

Choose a reason for hiding this comment

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

GOODD

Comment on lines +23 to +31

@BeforeEach
fun setup() {
val securityContext = SecurityContextHolder.createEmptyContext()
val authentication = UsernamePasswordAuthenticationToken("1", null, setOf(SimpleGrantedAuthority("ROLE_USER")))
securityContext.authentication = authentication
SecurityContextHolder.setContext(securityContext)
}

Copy link
Member

Choose a reason for hiding this comment

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

오 스프링연동해서 테스트 안해도
시큐리티 조작을 할수 있군요

Copy link
Member Author

Choose a reason for hiding this comment

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

SecurityUtils쪽 content가 null이여서 content만들어서 강제로 넣었슴니다! 😁

Copy link
Collaborator

Choose a reason for hiding this comment

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

이거 AOP 로 테스트 빼다가 저 난감했었는데 이렇게 넣었네요 나중에 제가 @WithMockUser나 커스텀하게 세팅해논거 반영하면 Before 뺄 수도 있을 것 같아요 ( 다른 서비스 다 ContextHolder 필요할 수도 있어서 )

Comment on lines 24 to 28
// given
ImageUrlDto(
url = "https://whatnow.kr/1.jpg",
key = "1.jpg",
)
Copy link
Member

Choose a reason for hiding this comment

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

요거 지워도될둣?

Comment on lines +18 to +32
var s3Properties: S3Properties = S3Properties(
S3Properties.S3Secret(
accessKey = "accessKey",
secretKey = "secretKey",
region = "ap-northeast-2",
endpoint = "https://kr.object.ncloudstorage.com",
bucket = "bucket",
),
)

var amazonS3: AmazonS3 =
AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(AwsClientBuilder.EndpointConfiguration(s3Properties.s3.endpoint, s3Properties.s3.region))
.withCredentials(AWSStaticCredentialsProvider(BasicAWSCredentials(s3Properties.s3.accessKey, s3Properties.s3.secretKey)))
.build()
Copy link
Member

Choose a reason for hiding this comment

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

인증정보를 이렇게 입력해도
url 생성은 되나보네용?

Copy link
Member Author

@kdomo kdomo Jun 12, 2023

Choose a reason for hiding this comment

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

네네 url 생성은 AmazonS3 인터페이스 구현체에서 만들어주네용
실제 key 값들은 Param에 붙는 형태여서 테스트할때는 무방하네욤

Copy link
Collaborator

Choose a reason for hiding this comment

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

그러게여 호오..

@kdomo kdomo changed the title [DPMBE-62] 이미지 관련 테스트 코드를 작성한다 [DPMBE-63] 이미지 관련 테스트 코드를 작성한다 Jun 12, 2023
Comment on lines +23 to +31

@BeforeEach
fun setup() {
val securityContext = SecurityContextHolder.createEmptyContext()
val authentication = UsernamePasswordAuthenticationToken("1", null, setOf(SimpleGrantedAuthority("ROLE_USER")))
securityContext.authentication = authentication
SecurityContextHolder.setContext(securityContext)
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

이거 AOP 로 테스트 빼다가 저 난감했었는데 이렇게 넣었네요 나중에 제가 @WithMockUser나 커스텀하게 세팅해논거 반영하면 Before 뺄 수도 있을 것 같아요 ( 다른 서비스 다 ContextHolder 필요할 수도 있어서 )

Comment on lines +18 to +32
var s3Properties: S3Properties = S3Properties(
S3Properties.S3Secret(
accessKey = "accessKey",
secretKey = "secretKey",
region = "ap-northeast-2",
endpoint = "https://kr.object.ncloudstorage.com",
bucket = "bucket",
),
)

var amazonS3: AmazonS3 =
AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(AwsClientBuilder.EndpointConfiguration(s3Properties.s3.endpoint, s3Properties.s3.region))
.withCredentials(AWSStaticCredentialsProvider(BasicAWSCredentials(s3Properties.s3.accessKey, s3Properties.s3.secretKey)))
.build()
Copy link
Collaborator

Choose a reason for hiding this comment

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

그러게여 호오..

@kdomo
Copy link
Member Author

kdomo commented Jun 13, 2023

Controller 테스트만 추가하고 머지하겠습니다

@sonarcloud
Copy link

sonarcloud bot commented Jun 13, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@kdomo kdomo merged commit 113f28a into develop Jun 13, 2023
@kdomo kdomo deleted the feature/DPMBE-63 branch June 13, 2023 15:26
kdomo added a commit that referenced this pull request Jun 16, 2023
* chore: spring security test 의존성 추가

* test: S3Properties Test

* test: S3UploadPresignedUrl Service Test

* test: GetPresignedUrl UseCase Test

* test: Image Controller Test

* fix: WAIT, LATE일때 throw 키워드 추가, PictureAdapter로 파일이름 수정

* test: PictureDomain Servicer Test

* test: PictureUploadSuccess UseCase Test

* test: Picture Adapter Test

* style: spotless

* test: S3UploadPresignedUrl Service Test resultUrl 수정

* fix: GetPresignedUrl UseCase Test에 ImageUrlDto 필요없는 부분 제거

* test: Picture Controller Test

* fix: AutoConfigureMockMvc에 addFilters = false 설정으로 filter 안불러오도록 변경

* style: spotless
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test test 코드 작성
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DPMBE-63] 이미지 관련 테스트 코드를 작성한다
3 participants