Skip to content

Commit

Permalink
test: TokenService test
Browse files Browse the repository at this point in the history
  • Loading branch information
stophwan committed Dec 6, 2023
1 parent a1c8bfe commit b0b691b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
@Component
public class TokenProvider {
private final JwtProperties jwtProperties;
private final Algorithm algorithm;

public TokenProvider(JwtProperties jwtProperties) {
this.jwtProperties = jwtProperties;
this.algorithm = Algorithm.HMAC256(jwtProperties.getSecretKey());
}

public String generateToken(Long userId) {
Expand All @@ -37,7 +35,7 @@ public String generateRefreshToken() {
}

public Long extractUserId(String token) {
JWTVerifier jwtverifier = JWT.require(algorithm).withIssuer(jwtProperties.getIssuer()).build();
JWTVerifier jwtverifier = JWT.require(Algorithm.HMAC256(jwtProperties.getSecretKey())).withIssuer(jwtProperties.getIssuer()).build();

Check warning on line 38 in src/main/java/com/plzgraduate/myongjigraduatebe/auth/security/TokenProvider.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/plzgraduate/myongjigraduatebe/auth/security/TokenProvider.java#L38

Added line #L38 was not covered by tests
Claims claims = new Claims(jwtverifier.verify(token));
return claims.id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.plzgraduate.myongjigraduatebe.auth.application.port.in.signin.SignInCommand;
import com.plzgraduate.myongjigraduatebe.auth.application.port.in.TokenResponse;
import com.plzgraduate.myongjigraduatebe.auth.application.port.out.SaveRefreshTokenPort;
import com.plzgraduate.myongjigraduatebe.auth.security.JwtAuthenticationToken;
import com.plzgraduate.myongjigraduatebe.auth.security.TokenProvider;

Expand All @@ -27,10 +28,12 @@ class SignInServiceTest {
private TokenProvider tokenProvider;
@Mock
private AuthenticationManager authenticationManager;
@Mock
private SaveRefreshTokenPort saveRefreshTokenPort;
@InjectMocks
private SignInService signInService;

/**


@DisplayName("로그인을 진행한다.")
@Test
Expand All @@ -40,23 +43,25 @@ void singIn() {
.authId("mju-graduate")
.password("1q2w3e4r!")
.build();
String accessToken = "jwt";
Long userId = 1L;
String accessToken = "accessToken";
String refreshToken = "refreshToken";

Authentication authentication = new JwtAuthenticationToken(
new AuthenticationUser(1L, "mju-graduate"),
null,
Collections.singleton(new SimpleGrantedAuthority("ROLE_USER")));
userId, null, Collections.singleton(new SimpleGrantedAuthority("ROLE_USER")));

given(authenticationManager.authenticate(any(JwtAuthenticationToken.class)))
.willReturn(authentication);
given(tokenProvider.generateToken(any(Authentication.class))).willReturn(accessToken);
given(tokenProvider.generateToken(userId)).willReturn(accessToken);
given(tokenProvider.generateRefreshToken()).willReturn(refreshToken);

//when
TokenResponse tokenResponse = signInService.signIn(command);

//then
then(saveRefreshTokenPort).should(times(1)).saveRefreshToken(refreshToken, userId);
assertThat(tokenResponse.getAccessToken()).isEqualTo(accessToken);
assertThat(tokenResponse.getRefreshToken()).isEqualTo(refreshToken);
}
**/

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.plzgraduate.myongjigraduatebe.auth.security;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;


@ExtendWith(MockitoExtension.class)
class TokenProviderTest {
@Mock
private JwtProperties jwtProperties;
@InjectMocks
private TokenProvider tokenProvider;

@DisplayName("refreshToken을 생성한다.")
@Test
void generateRefreshToken() {
String refreshToken = tokenProvider.generateRefreshToken();
assertEquals(36, refreshToken.length());
}
}

0 comments on commit b0b691b

Please sign in to comment.