-
Notifications
You must be signed in to change notification settings - Fork 1
[DPMBE-63] 이미지 관련 테스트 코드를 작성한다 #78
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GOODD
|
||
@BeforeEach | ||
fun setup() { | ||
val securityContext = SecurityContextHolder.createEmptyContext() | ||
val authentication = UsernamePasswordAuthenticationToken("1", null, setOf(SimpleGrantedAuthority("ROLE_USER"))) | ||
securityContext.authentication = authentication | ||
SecurityContextHolder.setContext(securityContext) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 스프링연동해서 테스트 안해도
시큐리티 조작을 할수 있군요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SecurityUtils쪽 content가 null이여서 content만들어서 강제로 넣었슴니다! 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이거 AOP 로 테스트 빼다가 저 난감했었는데 이렇게 넣었네요 나중에 제가 @WithMockUser나 커스텀하게 세팅해논거 반영하면 Before 뺄 수도 있을 것 같아요 ( 다른 서비스 다 ContextHolder 필요할 수도 있어서 )
// given | ||
ImageUrlDto( | ||
url = "https://whatnow.kr/1.jpg", | ||
key = "1.jpg", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요거 지워도될둣?
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
인증정보를 이렇게 입력해도
url 생성은 되나보네용?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
네네 url 생성은 AmazonS3 인터페이스 구현체에서 만들어주네용
실제 key 값들은 Param에 붙는 형태여서 테스트할때는 무방하네욤
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그러게여 호오..
|
||
@BeforeEach | ||
fun setup() { | ||
val securityContext = SecurityContextHolder.createEmptyContext() | ||
val authentication = UsernamePasswordAuthenticationToken("1", null, setOf(SimpleGrantedAuthority("ROLE_USER"))) | ||
securityContext.authentication = authentication | ||
SecurityContextHolder.setContext(securityContext) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이거 AOP 로 테스트 빼다가 저 난감했었는데 이렇게 넣었네요 나중에 제가 @WithMockUser나 커스텀하게 세팅해논거 반영하면 Before 뺄 수도 있을 것 같아요 ( 다른 서비스 다 ContextHolder 필요할 수도 있어서 )
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그러게여 호오..
Controller 테스트만 추가하고 머지하겠습니다 |
Kudos, SonarCloud Quality Gate passed! |
* 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
개요
작업사항
변경로직
참고
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