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

✨ create login test annotation #116

merged 1 commit into from
Jul 30, 2024

Conversation

HaiSeong
Copy link

구현

E2E 테스트시 로그인 테스트를 쉽게 하기위해 어노테이션을 만들었습니다.

사용법

  1. 사용할 ControllerTest 단에 @WithLoginUserTest을 붙여줍니다.
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@WithLoginUserTest
@Sql("/data/users.sql")
class UserControllerTest {
...
  1. 사용할 메서드에 @WithLoginUser를 붙여줍니다.
  • 인자 없이 전달하면 기본값으로 [email protected] 이메일을 가지는 테스트 유저가 생성되고 로그인됨
  • (email, username, nickname, image, birth, region)을 설정할 수 있음. data.sql에 이미 있는 email의 유저를 만들면 다시 생성되지 않고 기존 유저를 찾아옴.
  • 가짜 로그인이 아니라 실제로 userRepository에 저장됨. 테스트중 유저가 필요하면 userRepository.findByEmail을 통해 조회 가능
    @Test
    @WithLoginUser(email = "[email protected]")
    @DisplayName("사용자의 정보를 조회한다.")
    void getUserProfile() {
        long id = 1L;
        UserResponse expected = new UserResponse(
                id,
                "[email protected]",
                "loki",
                "로키",
                "loki.jpg",
                LocalDate.of(1999, 8, 8),
                "KOREA"
        );

        UserResponse actual = RestAssured.given().log().all()
                .contentType(ContentType.JSON)
                .when().get("/api/user/me")
                .then().log().all()
                .statusCode(200)
                .extract()
                .as(UserResponse.class);

        assertThat(actual).isEqualTo(expected);
    }
  1. RestAssured에 따로 헤더를 정의하지 않아도 다음과 같이 토큰이 들어가서 로그인이됨

스크린샷 2024-07-29 오전 12 20 03

@HaiSeong HaiSeong added ✨ feature new feature BE Backend labels Jul 28, 2024
@HaiSeong HaiSeong requested review from geoje and tackyu July 28, 2024 15:22
@HaiSeong HaiSeong self-assigned this Jul 28, 2024
Copy link

Overall Project 91.65% 🍏

There is no coverage information present for the Files changed

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 의 순서가 나중일 경우도 생각해서 양쪽 다 처리해여할 수도 있겠네요!

@HaiSeong HaiSeong merged commit 19aae6d into be/dev Jul 30, 2024
1 check passed
@HaiSeong HaiSeong deleted the be/feat/115 branch July 30, 2024 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BE Backend ✨ feature new feature
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants