Skip to content

Commit

Permalink
♻️ improve test with random port
Browse files Browse the repository at this point in the history
  • Loading branch information
HaiSeong committed Jul 30, 2024
1 parent 156a5d9 commit 5f1cb4c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,36 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import io.restassured.RestAssured;
import net.pengcook.authentication.domain.JwtTokenManager;
import net.pengcook.authentication.domain.TokenExtractor;
import net.pengcook.authentication.domain.TokenPayload;
import net.pengcook.authentication.domain.TokenType;
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.authentication.exception.JwtTokenException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.web.context.request.NativeWebRequest;

@SpringBootTest
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class LoginUserArgumentResolverTest {

@LocalServerPort
int port;
@Autowired
private JwtTokenManager jwtTokenManager;
@Autowired
private TokenExtractor tokenExtractor;

@BeforeEach
void setUp() {
RestAssured.port = port;
}

@Test
@DisplayName("로그인한 사용자 정보를 추출한다.")
void resolveArgument() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.FirebaseToken;
import java.time.LocalDate;
import java.util.regex.Pattern;
import net.pengcook.authentication.domain.JwtTokenManager;
import net.pengcook.authentication.domain.TokenPayload;
import net.pengcook.authentication.domain.TokenType;
Expand All @@ -34,6 +35,8 @@
@Sql(scripts = "/data/users.sql")
class LoginServiceTest {

private static final Pattern JWT_PATTERN = Pattern.compile("^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+$");

@MockBean
private FirebaseAuth firebaseAuth;
@Autowired
Expand All @@ -55,7 +58,7 @@ void loginWithGoogleWithEmailAlreadyRegistered() throws FirebaseAuthException {
GoogleLoginResponse googleLoginResponse = loginService.loginWithGoogle(request);

assertAll(
() -> assertThat(googleLoginResponse.accessToken()).isNotNull(),
() -> assertThat(googleLoginResponse.accessToken()).matches(JWT_PATTERN),
() -> assertThat(googleLoginResponse.registered()).isTrue()
);
}
Expand Down Expand Up @@ -134,8 +137,8 @@ void refresh() {
TokenRefreshResponse refresh = loginService.refresh(refreshToken);

assertAll(
() -> assertThat(refresh.accessToken()).isNotNull(),
() -> assertThat(refresh.refreshToken()).isNotNull().isNotSameAs(refreshToken)
() -> assertThat(refresh.accessToken()).matches(JWT_PATTERN),
() -> assertThat(refresh.refreshToken()).matches(JWT_PATTERN).isNotSameAs(refreshToken)
);
}

Expand Down

0 comments on commit 5f1cb4c

Please sign in to comment.