Skip to content

Commit

Permalink
[FEAT] 테스트용 유저 인증 어노테이션 구현 (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
tidavid1 authored Mar 4, 2024
1 parent d476b53 commit 55a6548
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.oeid.mogakgo.core.properties;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -11,7 +10,7 @@
@Getter
@Component
@ConfigurationProperties(prefix = "jwt")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor
public class JwtProperties {

private String header;
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/io/oeid/mogakgo/core/support/WithMockCustomUser.java
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;
}
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;
}
}

0 comments on commit 55a6548

Please sign in to comment.