-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: get user me * feat: get user me via jwt * refactor: coding style
- Loading branch information
Showing
6 changed files
with
77 additions
and
23 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
20 changes: 16 additions & 4 deletions
20
spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/UserController.kt
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 |
---|---|---|
@@ -1,15 +1,27 @@ | ||
package tw.waterballsa.gaas.spring.controllers | ||
|
||
import org.springframework.security.core.annotation.AuthenticationPrincipal | ||
import org.springframework.security.oauth2.jwt.Jwt | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import tw.waterballsa.gaas.application.usecases.GetUserUseCase | ||
import tw.waterballsa.gaas.domain.User | ||
import tw.waterballsa.gaas.spring.controllers.presenter.GetUserPresenter | ||
import tw.waterballsa.gaas.spring.controllers.viewmodel.GetUserViewModel | ||
|
||
@RestController | ||
@RequestMapping("/users") | ||
class UserController( | ||
private val getUserUseCase: GetUserUseCase | ||
) { | ||
@GetMapping("/users/{id}") | ||
fun getUser(@PathVariable id: String): User = getUserUseCase.execute(User.Id(id)) | ||
@GetMapping("/me") | ||
fun getUser(@AuthenticationPrincipal principal: Jwt): GetUserViewModel { | ||
val request = principal.toRequest() | ||
val presenter = GetUserPresenter() | ||
getUserUseCase.execute(request, presenter) | ||
return presenter.viewModel | ||
} | ||
} | ||
|
||
private fun Jwt.toRequest(): GetUserUseCase.Request = | ||
GetUserUseCase.Request(claims["email"] as String) |
20 changes: 20 additions & 0 deletions
20
spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/presenter/GetUserPresenter.kt
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,20 @@ | ||
package tw.waterballsa.gaas.spring.controllers.presenter | ||
|
||
import tw.waterballsa.gaas.application.usecases.GetUserUseCase | ||
import tw.waterballsa.gaas.domain.User | ||
import tw.waterballsa.gaas.spring.controllers.viewmodel.GetUserViewModel | ||
|
||
class GetUserPresenter : GetUserUseCase.Presenter { | ||
lateinit var viewModel: GetUserViewModel | ||
|
||
override fun present(user: User) { | ||
viewModel = user.toViewModel() | ||
} | ||
|
||
private fun User.toViewModel(): GetUserViewModel = | ||
GetUserViewModel( | ||
id = id!!.value, | ||
email = email, | ||
nickname = nickname, | ||
) | ||
} |
7 changes: 7 additions & 0 deletions
7
spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/viewmodel/GetUserViewModel.kt
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,7 @@ | ||
package tw.waterballsa.gaas.spring.controllers.viewmodel | ||
|
||
data class GetUserViewModel( | ||
val id: String, | ||
val email: String, | ||
val nickname: String | ||
) |
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ package tw.waterballsa.gaas.spring.it.controllers | |
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc | ||
import org.springframework.test.web.servlet.ResultActions | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath | ||
|
@@ -12,8 +11,6 @@ import tw.waterballsa.gaas.application.repositories.UserRepository | |
import tw.waterballsa.gaas.domain.User | ||
import tw.waterballsa.gaas.spring.it.AbstractSpringBootTest | ||
|
||
|
||
@AutoConfigureMockMvc(addFilters = false) | ||
class UserControllerTest @Autowired constructor( | ||
val userRepository: UserRepository, | ||
) : AbstractSpringBootTest() { | ||
|
@@ -24,28 +21,35 @@ class UserControllerTest @Autowired constructor( | |
} | ||
|
||
@Test | ||
fun givenUserCreated_whenGetUser_thenGetUserSuccessfully() { | ||
val user = User(User.Id("1"), "[email protected]", "winner5566") | ||
givenUserCreated(user) | ||
findUserById("1").thenGetUserSuccessfully(user) | ||
fun givenUserHasLoggedIn_whenGetUserSelf_thenGetUserSuccessfully() { | ||
givenUserHasLoggedIn() | ||
.whenGetUserSelf() | ||
.thenGetUserSuccessfully() | ||
} | ||
|
||
@Test | ||
fun givenUserNotCreated_whenGetUser_thenUserNotFound() { | ||
findUserById("0").thenUserNotFound() | ||
fun givenUserDoesNotLogIn_whenGetUserSelf_thenUserNotFound() { | ||
givenUserDoesNotLogIn() | ||
.whenGetUserSelf() | ||
.thenUserNotFound() | ||
} | ||
|
||
private fun givenUserCreated(user: User) { | ||
userRepository.createUser(user) | ||
private fun givenUserDoesNotLogIn(): User = this.mockUser | ||
|
||
private fun givenUserHasLoggedIn(): User { | ||
return userRepository.createUser(mockUser) | ||
} | ||
|
||
private fun findUserById(id: String): ResultActions = mockMvc.perform(get("/users/$id")) | ||
private fun User.whenGetUserSelf(): ResultActions { | ||
val jwt = identities.first().toJwt() | ||
return mockMvc.perform(get("/users/me").withJwt(jwt)) | ||
} | ||
|
||
private fun ResultActions.thenGetUserSuccessfully(user: User) { | ||
private fun ResultActions.thenGetUserSuccessfully() { | ||
this.andExpect(status().isOk) | ||
.andExpect(jsonPath("$.id").value(user.id!!.value)) | ||
.andExpect(jsonPath("$.email").value(user.email)) | ||
.andExpect(jsonPath("$.nickname").value(user.nickname)) | ||
.andExpect(jsonPath("$.id").value(mockUser.id!!.value)) | ||
.andExpect(jsonPath("$.email").value(mockUser.email)) | ||
.andExpect(jsonPath("$.nickname").value(mockUser.nickname)) | ||
} | ||
|
||
private fun ResultActions.thenUserNotFound() { | ||
|