Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ create login test annotation #116

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 "[email protected]";

String username() default "tester";

String nickname() default "테스트 유저";

String image() default "tester.jpg";

String birth() default "2000-01-01";

String region() default "KOREA";
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restdocs 로 붙인 Specification 을 덮어쓰는 것 같은데 이부분도 한번 고려해봐야겠네요...
기존 걸 들고와서 헤더만 추가할 수 있는지 등등!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아, 만약 Restdocs 가 대입하는 specification 의 순서가 나중일 경우도 생각해서 양쪽 다 처리해여할 수도 있겠네요!

}
}

@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);
}
}
Loading