-
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.
- Loading branch information
Showing
5 changed files
with
49 additions
and
65 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
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 |
---|---|---|
|
@@ -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) } | ||
|
@@ -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() | ||
} |
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 |
---|---|---|
|
@@ -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) | ||
|
@@ -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) | ||
} | ||
|