-
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
3 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
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
11 changes: 11 additions & 0 deletions
11
src/test/java/io/oeid/mogakgo/core/support/WithMockCustomUser.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,11 @@ | ||
package io.oeid.mogakgo.core.support; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import org.springframework.security.test.context.support.WithSecurityContext; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@WithSecurityContext(factory = WithMockCustomUserSecurityContextFactory.class) | ||
public @interface WithMockCustomUser { | ||
long userId() default 1L; | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/io/oeid/mogakgo/core/support/WithMockCustomUserSecurityContextFactory.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,44 @@ | ||
package io.oeid.mogakgo.core.support; | ||
|
||
import io.oeid.mogakgo.core.properties.JwtProperties; | ||
import io.oeid.mogakgo.domain.auth.jwt.JwtAuthenticationToken; | ||
import io.oeid.mogakgo.domain.auth.jwt.JwtHelper; | ||
import java.util.List; | ||
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; | ||
|
||
public class WithMockCustomUserSecurityContextFactory implements | ||
WithSecurityContextFactory<WithMockCustomUser> { | ||
|
||
private static JwtProperties initProperties() { | ||
JwtProperties properties = new JwtProperties(); | ||
properties.setHeader("test"); | ||
properties.setIssuer("test"); | ||
properties.setClientSecret("test"); | ||
properties.setAccessTokenExpiryHour(1); | ||
properties.setRefreshTokenExpiryHour(3); | ||
return properties; | ||
} | ||
|
||
private final JwtHelper jwtHelper; | ||
|
||
public WithMockCustomUserSecurityContextFactory() { | ||
this.jwtHelper = new JwtHelper( | ||
initProperties() | ||
); | ||
} | ||
|
||
@Override | ||
public SecurityContext createSecurityContext(WithMockCustomUser mockCustomUser) { | ||
SecurityContext context = SecurityContextHolder.createEmptyContext(); | ||
List<GrantedAuthority> authorities = List.of(new SimpleGrantedAuthority("ROLE_USER")); | ||
var token = jwtHelper.sign(mockCustomUser.userId(), | ||
authorities.stream().map(GrantedAuthority::getAuthority).toArray(String[]::new)); | ||
var auth = new JwtAuthenticationToken(token, null, authorities); | ||
context.setAuthentication(auth); | ||
return context; | ||
} | ||
} |