diff --git a/backend/src/test/java/net/pengcook/authentication/annotation/WithLoginUser.java b/backend/src/test/java/net/pengcook/authentication/annotation/WithLoginUser.java new file mode 100644 index 00000000..164a4958 --- /dev/null +++ b/backend/src/test/java/net/pengcook/authentication/annotation/WithLoginUser.java @@ -0,0 +1,23 @@ +package net.pengcook.authentication.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface WithLoginUser { + + String email() default "tester@pengcook.net"; + + String username() default "tester"; + + String nickname() default "테스트 유저"; + + String image() default "tester.jpg"; + + String birth() default "2000-01-01"; + + String region() default "KOREA"; +} diff --git a/backend/src/test/java/net/pengcook/authentication/annotation/WithLoginUserTest.java b/backend/src/test/java/net/pengcook/authentication/annotation/WithLoginUserTest.java new file mode 100644 index 00000000..3bf01cbb --- /dev/null +++ b/backend/src/test/java/net/pengcook/authentication/annotation/WithLoginUserTest.java @@ -0,0 +1,14 @@ +package net.pengcook.authentication.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import net.pengcook.authentication.extension.LoginExtension; +import org.junit.jupiter.api.extension.ExtendWith; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@ExtendWith(LoginExtension.class) +public @interface WithLoginUserTest { +} diff --git a/backend/src/test/java/net/pengcook/authentication/extension/LoginExtension.java b/backend/src/test/java/net/pengcook/authentication/extension/LoginExtension.java new file mode 100644 index 00000000..4f62f63d --- /dev/null +++ b/backend/src/test/java/net/pengcook/authentication/extension/LoginExtension.java @@ -0,0 +1,57 @@ +package net.pengcook.authentication.extension; + +import io.restassured.RestAssured; +import java.time.LocalDate; +import net.pengcook.authentication.annotation.WithLoginUser; +import net.pengcook.authentication.dto.TokenPayload; +import net.pengcook.authentication.util.JwtTokenManager; +import net.pengcook.user.domain.User; +import net.pengcook.user.repository.UserRepository; +import org.junit.jupiter.api.extension.AfterTestExecutionCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +public class LoginExtension implements BeforeEachCallback, AfterTestExecutionCallback { + + @Override + public void beforeEach(ExtensionContext context) { + + WithLoginUser annotation = context.getRequiredTestMethod().getAnnotation(WithLoginUser.class); + if (annotation != null) { + ApplicationContext applicationContext = SpringExtension.getApplicationContext(context); + JwtTokenManager jwtTokenManager = applicationContext.getBean(JwtTokenManager.class); + UserRepository userRepository = applicationContext.getBean(UserRepository.class); + + User user = findOrSaveUser(annotation, userRepository); + String accessToken = jwtTokenManager.createToken(new TokenPayload(user.getId(), user.getEmail())); + + RestAssured.port = ((ServletWebServerApplicationContext) applicationContext).getWebServer().getPort(); + RestAssured.requestSpecification = RestAssured.given().header("Authorization", "Bearer " + accessToken); + } + } + + @Override + public void afterTestExecution(ExtensionContext extensionContext) { + RestAssured.reset(); + } + + private User findOrSaveUser(WithLoginUser annotation, UserRepository userRepository) { + return userRepository.findByEmail(annotation.email()) + .orElseGet(() -> saveUser(annotation, userRepository)); + } + + private User saveUser(WithLoginUser annotation, UserRepository userRepository) { + User user = new User( + annotation.email(), + annotation.username(), + annotation.nickname(), + annotation.image(), + LocalDate.parse(annotation.birth()), + annotation.region() + ); + return userRepository.save(user); + } +}