File tree Expand file tree Collapse file tree
main/kotlin/com/duckbox/service
test/kotlin/com/duckbox/service Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -48,16 +48,24 @@ class EmailService (
4848 }
4949 val token = code.toString()
5050
51- // create EmailAuth entity
5251 val expirationTime = createTime + EXPIRATION_TIME
53- emailAuthRepository.save(
54- EmailAuth (
55- email = targetEmail,
56- token = token,
57- expirationTime = expirationTime,
58- expired = false
59- )
60- )
52+ runCatching {
53+ emailAuthRepository.findByEmail(targetEmail)
54+ }.onSuccess {
55+ // re-request
56+ it.token = token
57+ it.expirationTime = expirationTime
58+ it.expired = false
59+ emailAuthRepository.save(it)
60+ }.onFailure {
61+ // create EmailAuth entity
62+ emailAuthRepository.save(EmailAuth (
63+ email = targetEmail,
64+ token = token,
65+ expirationTime = expirationTime,
66+ expired = false
67+ ))
68+ }
6169
6270 return token
6371 }
Original file line number Diff line number Diff line change @@ -42,7 +42,24 @@ class EmailServiceTest {
4242 emailAuth.apply {
4343 assertThat(email).isEqualTo(testEmail)
4444 assertThat(expired).isEqualTo(false )
45- assertThat(token).isEqualTo(token)
45+ assertThat(this .token).isEqualTo(token)
46+ }
47+ }
48+
49+ @Test
50+ fun is_createEmailToken_works_well_on_re_request () {
51+ // act
52+ val token: String = emailService.createEmailToken(testEmail)
53+ val token2: String = emailService.createEmailToken(testEmail)
54+
55+ // assert
56+ assertThat(token.length).isEqualTo(6 )
57+ assertThat(token).isNotEqualTo(token2)
58+ val emailAuth = emailAuthRepository.findByEmail(testEmail)
59+ emailAuth.apply {
60+ assertThat(email).isEqualTo(testEmail)
61+ assertThat(expired).isEqualTo(false )
62+ assertThat(this .token).isEqualTo(token2)
4663 }
4764 }
4865
You can’t perform that action at this time.
0 commit comments