-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad06150
commit 61e703a
Showing
10 changed files
with
126 additions
and
100 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
31 changes: 31 additions & 0 deletions
31
src/main/java/in/koreatech/koin/domain/user/UserToken.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,31 @@ | ||
package in.koreatech.koin.domain.user; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.redis.core.RedisHash; | ||
import org.springframework.data.redis.core.TimeToLive; | ||
|
||
@Getter | ||
@RedisHash("refreshToken") | ||
public class UserToken { | ||
|
||
@Id | ||
private Long id; | ||
|
||
private final String refreshToken; | ||
|
||
@TimeToLive(unit = TimeUnit.DAYS) | ||
private final Long expiration; | ||
|
||
private UserToken(Long id, String refreshToken, Long expiration) { | ||
this.id = id; | ||
this.refreshToken = refreshToken; | ||
this.expiration = expiration; | ||
|
||
} | ||
|
||
public static UserToken create(Long userId, String refreshToken) { | ||
return new UserToken(userId, refreshToken, 3L); | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/main/java/in/koreatech/koin/repository/UserTokenRepository.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,12 @@ | ||
package in.koreatech.koin.repository; | ||
|
||
import in.koreatech.koin.domain.user.UserToken; | ||
import java.util.Optional; | ||
import org.springframework.data.repository.Repository; | ||
|
||
public interface UserTokenRepository extends Repository<UserToken, Long> { | ||
|
||
UserToken save(UserToken userToken); | ||
|
||
Optional<UserToken> findById(Long userId); | ||
} |
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
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
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 |
---|---|---|
|
@@ -2,96 +2,75 @@ | |
|
||
|
||
import static org.assertj.core.api.SoftAssertions.assertSoftly; | ||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; | ||
import static org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD; | ||
|
||
import in.koreatech.koin.AcceptanceTest; | ||
import in.koreatech.koin.domain.user.User; | ||
import in.koreatech.koin.domain.user.UserToken; | ||
import in.koreatech.koin.domain.user.UserType; | ||
import in.koreatech.koin.repository.UserRepository; | ||
//import in.koreatech.koin.repository.UserTokenRepository; | ||
import in.koreatech.koin.repository.UserTokenRepository; | ||
import io.restassured.RestAssured; | ||
import io.restassured.http.ContentType; | ||
import io.restassured.response.ExtractableResponse; | ||
import io.restassured.response.Response; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
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.http.HttpStatus; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
|
||
@SpringBootTest(webEnvironment = RANDOM_PORT) | ||
@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) | ||
class UserApiTest { | ||
|
||
@LocalServerPort | ||
int port; | ||
class UserApiTest extends AcceptanceTest { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
// @Autowired | ||
// private UserTokenRepository tokenRepository; | ||
|
||
private | ||
|
||
@BeforeEach | ||
void setUp() { | ||
RestAssured.port = port; | ||
} | ||
@Autowired | ||
private UserTokenRepository tokenRepository; | ||
|
||
@Nested | ||
@Test | ||
@DisplayName("사용자가 로그인을 수행한다") | ||
class userLogin { | ||
|
||
@Test | ||
@DisplayName("사용자가 로그인을 수행한다") | ||
void userLoginSuccess() { | ||
User user = User.builder() | ||
.password("1234") | ||
.nickname("주노") | ||
.name("최준호") | ||
.phoneNumber("010-1234-5678") | ||
.userType(UserType.STUDENT) | ||
.email("[email protected]") | ||
.isAuthed(true) | ||
.isDeleted(false) | ||
.build(); | ||
|
||
userRepository.save(user); | ||
|
||
ExtractableResponse<Response> response = RestAssured | ||
.given() | ||
.log().all() | ||
.body(""" | ||
{ | ||
"email": "[email protected]", | ||
"password": "1234" | ||
} | ||
""") | ||
.when() | ||
.log().all() | ||
.post("/user/login") | ||
.then() | ||
.log().all() | ||
.statusCode(HttpStatus.OK.value()) | ||
.extract(); | ||
|
||
User userResult = userRepository.findById(user.getId()).get(); | ||
|
||
assertSoftly( | ||
softly -> { | ||
softly.assertThat(response.jsonPath().getString("token")).isNotNull(); | ||
softly.assertThat(response.jsonPath().getString("refresh_token")).isNotNull(); | ||
softly.assertThat(response.jsonPath().getString("user_type")).isEqualTo("STUDENT"); | ||
softly.assertThat(userResult.getLastLoggedAt()).isNotNull(); | ||
void userLoginSuccess() { | ||
User user = User.builder() | ||
.password("1234") | ||
.nickname("주노") | ||
.name("최준호") | ||
.phoneNumber("010-1234-5678") | ||
.userType(UserType.STUDENT) | ||
.email("[email protected]") | ||
.isAuthed(true) | ||
.isDeleted(false) | ||
.build(); | ||
|
||
userRepository.save(user); | ||
|
||
ExtractableResponse<Response> response = RestAssured | ||
.given() | ||
.log().all() | ||
.body(""" | ||
{ | ||
"email": "[email protected]", | ||
"password": "1234" | ||
} | ||
); | ||
} | ||
|
||
""") | ||
.contentType(ContentType.JSON) | ||
.when() | ||
.log().all() | ||
.post("/user/login") | ||
.then() | ||
.log().all() | ||
.statusCode(HttpStatus.CREATED.value()) | ||
.extract(); | ||
|
||
User userResult = userRepository.findById(user.getId()).get(); | ||
UserToken token = tokenRepository.findById(user.getId()).get(); | ||
|
||
assertSoftly( | ||
softly -> { | ||
softly.assertThat(response.jsonPath().getString("token")).isNotNull(); | ||
softly.assertThat(response.jsonPath().getString("refresh_token")).isNotNull(); | ||
softly.assertThat(response.jsonPath().getString("refresh_token")) | ||
.isEqualTo(token.getRefreshToken()); | ||
softly.assertThat(response.jsonPath().getString("user_type")).isEqualTo("STUDENT"); | ||
} | ||
); | ||
} | ||
|
||
|
||
} |