-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: auth - domain, infrastructure layer 테스팅
- Loading branch information
1 parent
2ecc8db
commit 54b92b5
Showing
11 changed files
with
457 additions
and
7 deletions.
There are no files selected for viewing
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
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
50 changes: 50 additions & 0 deletions
50
src/test/java/com/inq/wishhair/wesharewishhair/auth/domain/AuthCodeRepositoryTest.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,50 @@ | ||
package com.inq.wishhair.wesharewishhair.auth.domain; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import com.inq.wishhair.wesharewishhair.auth.domain.entity.AuthCode; | ||
import com.inq.wishhair.wesharewishhair.common.support.RepositoryTestSupport; | ||
|
||
@DisplayName("[AuthCodeRepository Test] - Domain Layer") | ||
class AuthCodeRepositoryTest extends RepositoryTestSupport { | ||
|
||
@Autowired | ||
private AuthCodeRepository authCodeRepository; | ||
|
||
@Test | ||
@DisplayName("[해당 이메일에 해당되는 인증코드를 삭제한다]") | ||
void deleteByEmail() { | ||
//given | ||
final String email = "email"; | ||
AuthCode authCode = new AuthCode(email, "code"); | ||
authCodeRepository.save(authCode); | ||
|
||
//when | ||
authCodeRepository.deleteByEmail(email); | ||
|
||
//then | ||
Optional<AuthCode> actual = authCodeRepository.findByEmail(email); | ||
assertThat(actual).isNotPresent(); | ||
} | ||
|
||
@Test | ||
@DisplayName("[해당 이메일을 가진 인증코드를 조회한다]") | ||
void findByEmail() { | ||
//given | ||
final String email = "email"; | ||
AuthCode authCode = new AuthCode(email, "code"); | ||
authCodeRepository.save(authCode); | ||
|
||
//when | ||
Optional<AuthCode> actual = authCodeRepository.findByEmail(email); | ||
|
||
//then | ||
assertThat(actual).contains(authCode); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/test/java/com/inq/wishhair/wesharewishhair/auth/domain/AuthTokenManagerTest.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,61 @@ | ||
package com.inq.wishhair.wesharewishhair.auth.domain; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.function.Executable; | ||
|
||
import com.inq.wishhair.wesharewishhair.auth.infrastructure.jwt.AuthAuthTokenManagerAdaptor; | ||
import com.inq.wishhair.wesharewishhair.auth.infrastructure.jwt.JwtTokenProvider; | ||
import com.inq.wishhair.wesharewishhair.auth.stub.JwtTokenProviderStub; | ||
|
||
@DisplayName("[AuthTokenManager Test] - Infrastructure Layer") | ||
class AuthTokenManagerTest { | ||
|
||
private final JwtTokenProvider jwtTokenProvider = new JwtTokenProviderStub(); | ||
private final AuthTokenManager authTokenManager = new AuthAuthTokenManagerAdaptor(jwtTokenProvider); | ||
|
||
@Test | ||
@DisplayName("[인증 토큰을 발행한다]") | ||
void generate() { | ||
//when | ||
AuthToken actual = authTokenManager.generate(1L); | ||
|
||
//then | ||
AuthToken expected = new AuthToken( | ||
jwtTokenProvider.createAccessToken(1L), | ||
jwtTokenProvider.createRefreshToken(1L) | ||
); | ||
|
||
assertThat(actual).isEqualTo(expected); | ||
} | ||
|
||
@Test | ||
@DisplayName("[토큰에서 id 를 추출한다]") | ||
void getId() { | ||
//given | ||
String token = jwtTokenProvider.createRefreshToken(1L); | ||
|
||
//when | ||
Long actual = authTokenManager.getId(token); | ||
|
||
//then | ||
Long expected = jwtTokenProvider.getPayload(token); | ||
assertThat(actual).isEqualTo(expected); | ||
} | ||
|
||
@Test | ||
@DisplayName("[토큰을 검증한다]") | ||
void validateToken() { | ||
//given | ||
String token = jwtTokenProvider.createRefreshToken(1L); | ||
|
||
//when | ||
Executable when = () -> authTokenManager.validateToken(token); | ||
|
||
//then | ||
assertDoesNotThrow(when); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/test/java/com/inq/wishhair/wesharewishhair/auth/domain/TokenRepositoryTest.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,93 @@ | ||
package com.inq.wishhair.wesharewishhair.auth.domain; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import com.inq.wishhair.wesharewishhair.auth.domain.entity.Token; | ||
import com.inq.wishhair.wesharewishhair.common.support.RepositoryTestSupport; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
|
||
@DisplayName("[TokenRepository Test] - Domain Layer") | ||
class TokenRepositoryTest extends RepositoryTestSupport { | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Autowired | ||
private TokenRepository tokenRepository; | ||
|
||
@Test | ||
@DisplayName("[해당 유저아이디를 가진 토큰을 조회한다]") | ||
void findByUserId() { | ||
//given | ||
final long userId = 1L; | ||
Token token = Token.issue(userId, "token"); | ||
tokenRepository.save(token); | ||
|
||
//when | ||
Optional<Token> actual = tokenRepository.findByUserId(userId); | ||
|
||
//then | ||
assertThat(actual).contains(token); | ||
} | ||
|
||
@Test | ||
@DisplayName("[해당 유저 아이디와 리프래쉬 토큰을 가진 토큰을 조회한다]") | ||
void findByUserIdAndRefreshToken() { | ||
//given | ||
final String refreshToken = "token"; | ||
final long userId = 1L; | ||
Token token = Token.issue(userId, refreshToken); | ||
tokenRepository.save(token); | ||
|
||
//when | ||
Optional<Token> actual = tokenRepository.findByUserIdAndRefreshToken(userId, refreshToken); | ||
|
||
//then | ||
assertThat(actual).contains(token); | ||
} | ||
|
||
@Test | ||
@DisplayName("[해당 유저 아이디를 가진 토큰을 삭제한다]") | ||
void deleteByUserId() { | ||
//given | ||
final long userId = 1L; | ||
Token token = Token.issue(userId, "token"); | ||
tokenRepository.save(token); | ||
|
||
//when | ||
tokenRepository.deleteByUserId(userId); | ||
|
||
//then | ||
Optional<Token> actual = tokenRepository.findByUserId(userId); | ||
assertThat(actual).isNotPresent(); | ||
} | ||
|
||
@Test | ||
@DisplayName("[해당 유저 아이디를 가진 토큰의 리프래쉬 토큰을 업데이트한다]") | ||
void updateRefreshTokenByUserId() { | ||
//given | ||
final long userId = 1L; | ||
Token token = Token.issue(userId, "token"); | ||
tokenRepository.save(token); | ||
|
||
final String newRefreshToken = "refreshToken"; | ||
|
||
//when | ||
tokenRepository.updateRefreshTokenByUserId(userId, newRefreshToken); | ||
entityManager.flush(); | ||
entityManager.clear(); | ||
|
||
//then | ||
Optional<Token> actual = tokenRepository.findByUserId(userId); | ||
assertThat(actual).isPresent(); | ||
assertThat(actual.get().getRefreshToken()).isEqualTo(newRefreshToken); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/com/inq/wishhair/wesharewishhair/auth/domain/entity/AuthCodeTest.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,24 @@ | ||
package com.inq.wishhair.wesharewishhair.auth.domain.entity; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@DisplayName("[AuthCode Test] - Domain Layer") | ||
class AuthCodeTest { | ||
|
||
@Test | ||
@DisplayName("[인증 코드를 변경한다]") | ||
void updateCode() { | ||
//given | ||
AuthCode authCode = new AuthCode("email", "code"); | ||
final String newCode = "newCode"; | ||
|
||
//when | ||
authCode.updateCode(newCode); | ||
|
||
//then | ||
assertThat(authCode.getCode()).isEqualTo(newCode); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/java/com/inq/wishhair/wesharewishhair/auth/domain/entity/TokenTest.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,42 @@ | ||
package com.inq.wishhair.wesharewishhair.auth.domain.entity; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@DisplayName("[Token Test] - Domain Layer") | ||
class TokenTest { | ||
|
||
@Test | ||
@DisplayName("[토큰을 생성한다]") | ||
void issueTest() { | ||
//given | ||
final Long userId = 1L; | ||
final String token = "token"; | ||
|
||
//when | ||
Token actual = Token.issue(userId, token); | ||
|
||
//then | ||
assertAll( | ||
() -> assertThat(actual.getUserId()).isEqualTo(userId), | ||
() -> assertThat(actual.getRefreshToken()).isEqualTo(token) | ||
); | ||
} | ||
|
||
@Test | ||
@DisplayName("[토큰을 변경한다]") | ||
void updateRefreshTokenTest() { | ||
//given | ||
Token token = Token.issue(1L, "token"); | ||
final String newToken = "newToken"; | ||
|
||
//when | ||
token.updateRefreshToken(newToken); | ||
|
||
//then | ||
assertThat(token.getRefreshToken()).isEqualTo(newToken); | ||
} | ||
} |
Oops, something went wrong.