Skip to content

Commit

Permalink
refactor: coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
lohas1107 committed Jun 24, 2023
1 parent 75f2fa7 commit fb8ccd6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import tw.waterballsa.gaas.application.eventbus.EventBus
import tw.waterballsa.gaas.application.repositories.UserRepository
import tw.waterballsa.gaas.domain.User
import tw.waterballsa.gaas.events.UserCreatedEvent
import java.util.UUID
import java.util.UUID.randomUUID
import javax.inject.Named

@Named
Expand All @@ -22,7 +22,7 @@ class CreateUserUseCase(
eventBus.broadcast(event)
}

user.doesNotHaveIdentity(request.identityProviderId) -> {
!user.hasIdentity(request.identityProviderId) -> {
user = user.addIdentity(request.identityProviderId)
userRepository.update(user)
}
Expand All @@ -32,13 +32,13 @@ class CreateUserUseCase(
class Request(
val email: String,
val identityProviderId: String,
) {
fun toUser(): User = User(
email = email,
nickname = "user_${UUID.randomUUID()}",
identities = listOf(identityProviderId)
)
}
)

private fun Request.toUser(): User = User(
email = email,
nickname = "user_${randomUUID()}",
identities = listOf(identityProviderId)
)
}

fun User.toUserCreatedEvent(): UserCreatedEvent = UserCreatedEvent(id!!, email, nickname)
4 changes: 0 additions & 4 deletions domain/src/main/kotlin/tw/waterballsa/gaas/domain/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ class User(
return identities.contains(identityProviderId)
}

fun doesNotHaveIdentity(identityProviderId: String): Boolean {
return !hasIdentity(identityProviderId)
}

fun addIdentity(identityProviderId: String): User =
User(id, email, nickname, identities + identityProviderId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import tw.waterballsa.gaas.domain.User
class UserData(
@Id
var id: String? = null,
var email: String? = null,
var nickname: String? = null,
var identities: List<String>? = null
val email: String? = null,
val nickname: String? = null,
val identities: List<String>? = null,
) {

fun toDomain(): User =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ abstract class AbstractSpringBootTest {
@Autowired
private lateinit var objectMapper: ObjectMapper

protected val mockUser: User = mockDefaultUser()
protected val mockUser: User = User(
User.Id("1"),
"[email protected]",
"user-437b200d-da9c-449e-b147-114b4822b5aa",
listOf("google-oauth2|102527320242660434908")
)

protected fun <T> ResultActions.getBody(type: Class<T>): T =
andReturn().response.contentAsString.let { objectMapper.readValue(it, type) }
Expand All @@ -34,17 +39,10 @@ abstract class AbstractSpringBootTest {
protected fun MockHttpServletRequestBuilder.withJson(request: Any): MockHttpServletRequestBuilder =
contentType(APPLICATION_JSON).content(request.toJson())

protected fun mockDefaultUser(): User =
User(User.Id("1"),
"[email protected]",
"user-437b200d-da9c-449e-b147-114b4822b5aa",
listOf("google-oauth2|102527320242660434908")
)

protected fun mockJwt(subject: String, email: String): Jwt =
protected fun mockJwt(subject: String): Jwt =
Jwt.withTokenValue("mock-token")
.header("alg", "none")
.subject(subject)
.claim("email", email)
.claim("email", mockUser.email)
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,55 @@ class OAuth2ControllerTest @Autowired constructor(
val userRepository: UserRepository,
) : AbstractSpringBootTest() {

val email = "[email protected]"
val googleIdentityProviderId = "google-oauth2|102527320242660434908"
val discordIdentityProviderId = "discord|102527320242660434908"
private final val googleIdentityProviderId = "google-oauth2|102527320242660434908"
private final val discordIdentityProviderId = "discord|102527320242660434908"

val googleOAuth2Jwt = this.mockJwt(googleIdentityProviderId)
val discordOAuth2Jwt = this.mockJwt(discordIdentityProviderId)

@BeforeEach
fun cleanUp() {
userRepository.deleteAll()
}

@Test
fun givenInvalidJwtSubject_whenUserLogin_thenShouldLoginFailed() {
givenInvalidJwt()
.whenUserLogin()
fun whenUserLoginWithInvalidJwt_thenShouldLoginFailed() {
val invalidJwt = Jwt(
"invalid_token",
null,
null,
mapOf("alg" to "none"),
mapOf("no_email" to "none")
)
whenUserLogin(invalidJwt)
.thenShouldLoginFailed()
}

@Test
fun givenOldEmail_andOldIdentityProviderId_whenUserLogin_thenLoginSuccessfully() {
givenGoogleOAuth2Jwt()
.whenUserLogin()
fun givenUserHasLoggedInViaGoogle_whenUserLoginWithGoogleOAuth2Jwt_thenLoginSuccessfully() {
givenUserHasLoggedInViaGoogle()
whenUserLogin(googleOAuth2Jwt)
.thenLoginSuccessfully()
}

@Test
fun givenOldEmail_andNewIdentityProviderId_whenUserLogin_thenSaveNewIdentityProviderId() {
givenDiscordOAuth2Jwt()
.whenUserLogin()
.thenSaveNewIdentityProviderId()
fun givenUserHasLoggedInViaGoogle_whenUserLoginWithDiscordOAuth2Jwt_thenUserHaveNewIdentity() {
givenUserHasLoggedInViaGoogle()
whenUserLogin(discordOAuth2Jwt)
.thenUserHaveNewIdentity()
}

@Test
fun givenNewEmail_andNewIdentityProviderId_whenUserLogin_thenCreateNewUser() {
givenJwt(googleIdentityProviderId, email)
.whenUserLogin()
fun whenUserLoginWithNewIdentity_thenCreateNewUser() {
whenUserLogin(googleOAuth2Jwt)
.thenCreateNewUser()
}

private fun givenInvalidJwt(): Jwt =
Jwt("invalid_token",
null,
null,
mapOf("alg" to "none"),
mapOf("no_email" to "none"))

private fun givenGoogleOAuth2Jwt(): Jwt {
userRepository.createUser(mockUser)
return givenJwt(googleIdentityProviderId, mockUser.email)
}

private fun givenDiscordOAuth2Jwt(): Jwt {
private fun givenUserHasLoggedInViaGoogle(): User =
userRepository.createUser(mockUser)
return givenJwt(discordIdentityProviderId, mockUser.email)
}

private fun givenJwt(identityProviderId: String, email: String): Jwt =
mockJwt(identityProviderId, email)

private fun Jwt.whenUserLogin(): ResultActions =
mockMvc.perform(get("/").with(jwt().jwt(this)))
private fun whenUserLogin(jwt: Jwt): ResultActions =
mockMvc.perform(get("/").with(jwt().jwt(jwt)))

private fun ResultActions.thenShouldLoginFailed() {
this.andExpect(status().isBadRequest)
Expand All @@ -85,16 +75,16 @@ class OAuth2ControllerTest @Autowired constructor(
this.andExpect(status().isOk)
}

private fun ResultActions.thenSaveNewIdentityProviderId() {
private fun ResultActions.thenUserHaveNewIdentity() {
thenLoginSuccessfully()
userRepository.findByEmail(email)!!
userRepository.findByEmail(mockUser.email)!!
.thenSaveIdentityProviderId(googleIdentityProviderId)
.thenSaveIdentityProviderId(discordIdentityProviderId)
}

private fun ResultActions.thenCreateNewUser() {
thenLoginSuccessfully()
userRepository.findByEmail(email)!!
userRepository.findByEmail(mockUser.email)!!
.thenCreateNickname()
.thenSaveIdentityProviderId(googleIdentityProviderId)
}
Expand Down

0 comments on commit fb8ccd6

Please sign in to comment.