-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/test/java/gdsc/konkuk/platformcore/annotation/CustomMockUser.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,16 @@ | ||
package gdsc.konkuk.platformcore.annotation; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
import org.springframework.security.test.context.support.WithSecurityContext; | ||
|
||
import gdsc.konkuk.platformcore.domain.member.entity.MemberRole; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@WithSecurityContext(factory = WithMyCustomUserSecurityContextFactory.class) | ||
public @interface CustomMockUser { | ||
String memberId() default "202011288"; | ||
|
||
MemberRole role() default MemberRole.MEMBER; | ||
} |
36 changes: 36 additions & 0 deletions
36
...test/java/gdsc/konkuk/platformcore/annotation/WithMyCustomUserSecurityContextFactory.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,36 @@ | ||
package gdsc.konkuk.platformcore.annotation; | ||
|
||
import java.util.List; | ||
|
||
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 gdsc.konkuk.platformcore.domain.member.entity.MemberRole; | ||
|
||
public class WithMyCustomUserSecurityContextFactory implements WithSecurityContextFactory<CustomMockUser> { | ||
@Override | ||
public SecurityContext createSecurityContext(CustomMockUser annotation) { | ||
String memberId = annotation.memberId(); | ||
MemberRole role = annotation.role(); | ||
|
||
Authentication auth = new UsernamePasswordAuthenticationToken( | ||
memberId, | ||
"", | ||
buildGrantedAuthoritiesFromRole(role) | ||
); | ||
|
||
SecurityContext context = SecurityContextHolder.getContext(); | ||
context.setAuthentication(auth); | ||
|
||
return context; | ||
} | ||
|
||
private List<GrantedAuthority> buildGrantedAuthoritiesFromRole(final MemberRole role) { | ||
return List.of(new SimpleGrantedAuthority(role.toString())); | ||
} | ||
} |