-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
backend/src/test/java/net/pengcook/authentication/annotation/WithLoginUser.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,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"; | ||
} |
14 changes: 14 additions & 0 deletions
14
backend/src/test/java/net/pengcook/authentication/annotation/WithLoginUserTest.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,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 { | ||
} |
57 changes: 57 additions & 0 deletions
57
backend/src/test/java/net/pengcook/authentication/extension/LoginExtension.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,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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restdocs 로 붙인 Specification 을 덮어쓰는 것 같은데 이부분도 한번 고려해봐야겠네요...
기존 걸 들고와서 헤더만 추가할 수 있는지 등등!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아, 만약 Restdocs 가 대입하는 specification 의 순서가 나중일 경우도 생각해서 양쪽 다 처리해여할 수도 있겠네요!