-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 컨트롤러 레이어 테스트를 위한 설정 미리 추가 (#72) (KAN-129)
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/test/java/org/ioteatime/meonghanyangserver/config/mock/WithCustomMockUser.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,17 @@ | ||
package org.ioteatime.meonghanyangserver.config.mock; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import org.springframework.security.test.context.support.WithSecurityContext; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@WithSecurityContext(factory = WithCustomMockUserSecurityContextFactory.class) | ||
public @interface WithCustomMockUser { | ||
String userId() default "1"; | ||
|
||
String email() default "[email protected]"; | ||
|
||
String username() default "test"; | ||
|
||
String password() default "testpassword"; | ||
} |
26 changes: 26 additions & 0 deletions
26
...rg/ioteatime/meonghanyangserver/config/mock/WithCustomMockUserSecurityContextFactory.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,26 @@ | ||
package org.ioteatime.meonghanyangserver.config.mock; | ||
|
||
import org.ioteatime.meonghanyangserver.user.domain.UserEntity; | ||
import org.ioteatime.meonghanyangserver.user.dto.CustomUserDetail; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.test.context.support.WithSecurityContextFactory; | ||
|
||
public class WithCustomMockUserSecurityContextFactory | ||
implements WithSecurityContextFactory<WithCustomMockUser> { | ||
@Override | ||
public SecurityContext createSecurityContext(WithCustomMockUser annotation) { | ||
String userId = annotation.userId(); | ||
|
||
UserEntity user = UserEntity.builder().id(Long.parseLong(userId)).build(); | ||
CustomUserDetail customUserDetail = new CustomUserDetail(user); | ||
|
||
UsernamePasswordAuthenticationToken token = | ||
new UsernamePasswordAuthenticationToken( | ||
customUserDetail, null, customUserDetail.getAuthorities()); | ||
SecurityContext context = SecurityContextHolder.getContext(); | ||
context.setAuthentication(token); | ||
return context; | ||
} | ||
} |