Skip to content

Commit

Permalink
test: auth - domain, infrastructure layer 테스팅
Browse files Browse the repository at this point in the history
  • Loading branch information
EunChanNam committed Oct 27, 2023
1 parent 2ecc8db commit 54b92b5
Show file tree
Hide file tree
Showing 11 changed files with 457 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.inq.wishhair.wesharewishhair.auth.domain.entity.Token;

public interface TokenRepository extends JpaRepository<Token, Long> {
public interface TokenRepository {

Token save(Token token);

Optional<Token> findByUserId(Long userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public JwtTokenProvider(
this.refreshTokenValidity = refreshTokenValidity;
}

public String createAccessToken(final Long memberId) {
return createToken(memberId, accessTokenValidity);
public String createAccessToken(final Long userId) {
return createToken(userId, accessTokenValidity);
}

public String createRefreshToken(final Long memberId) {
return createToken(memberId, refreshTokenValidity);
public String createRefreshToken(final Long userId) {
return createToken(userId, refreshTokenValidity);
}

public Long getPayload(final String token) {
Expand Down
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Loading

0 comments on commit 54b92b5

Please sign in to comment.