Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#120 Handle duplicate yandex id connect #132

Merged
merged 3 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ data class YandexUser(
val login: Login,
) {
@JvmInline
value class Id(val number: Int)
value class Id(val number: Int) {
override fun toString(): String = number.toString()
}

@JvmInline
value class Login(val text: String)
value class Login(val text: String) {
override fun toString(): String = text
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package ru.vityaman.lms.botalka.storage.jooq

import ru.vityaman.lms.botalka.core.exception.InvalidValueException
import ru.vityaman.lms.botalka.core.external.yandex.YandexUser
import ru.vityaman.lms.botalka.core.model.User
import ru.vityaman.lms.botalka.core.storage.YandexAuthStorage
import ru.vityaman.lms.botalka.storage.jooq.exception.UniqueViolationException
import ru.vityaman.lms.botalka.storage.jooq.tables.references.AUTH_YANDEX

class JooqYandexAuthStorage(
private val database: JooqDatabase,
) : YandexAuthStorage {
override suspend fun create(userId: User.Id, yandexUser: YandexUser) =
override suspend fun create(userId: User.Id, yandexUser: YandexUser) = try {
database.only {
insertInto(
AUTH_YANDEX,
Expand All @@ -21,6 +23,15 @@ class JooqYandexAuthStorage(
yandexUser.login.text,
)
}.let { }
} catch (e: UniqueViolationException) {
throw InvalidValueException(
buildString {
append("yandex user '${yandexUser.login}' ")
append("(id '${yandexUser.id}') already connected")
},
e,
)
}

override suspend fun corresponding(id: YandexUser.Id): User.Id? =
database.maybe {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ru.vityaman.lms.botalka.app.spring.api.http.endpoint
import kotlinx.coroutines.reactor.awaitSingle
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.web.reactive.function.client.WebClientResponseException
import ru.vityaman.lms.botalka.app.spring.BotalkaTestSuite
import ru.vityaman.lms.botalka.app.spring.api.http.client.Api
import ru.vityaman.lms.botalka.app.spring.api.http.client.Auth2ViaCodeMessage
Expand Down Expand Up @@ -32,4 +34,31 @@ class AuthApiTest : BotalkaTestSuite() {
),
).awaitSingle()
}

@Test
fun duplicateRegistration(): Unit = runBlocking {
val api = Api.ofNewbie()

api.user.postUser(
PostUserRequestMessage(
user = UserDraftMessage("vanya"),
credential = Auth2ViaCodeMessage(
kind = CredentialKindMessage.yandex_code,
code = 654_321,
),
),
).awaitSingle()

assertThrows<WebClientResponseException.BadRequest> {
api.user.postUser(
PostUserRequestMessage(
user = UserDraftMessage("vitya"),
credential = Auth2ViaCodeMessage(
kind = CredentialKindMessage.yandex_code,
code = 654_321,
),
),
).awaitSingle()
}
}
}
Loading