diff --git a/GoTrue/src/commonTest/kotlin/AuthRestExceptionTest.kt b/GoTrue/src/commonTest/kotlin/AuthRestExceptionTest.kt index b502192a..a9fff77a 100644 --- a/GoTrue/src/commonTest/kotlin/AuthRestExceptionTest.kt +++ b/GoTrue/src/commonTest/kotlin/AuthRestExceptionTest.kt @@ -1,5 +1,116 @@ +import io.github.jan.supabase.SupabaseClientBuilder +import io.github.jan.supabase.exceptions.BadRequestRestException +import io.github.jan.supabase.gotrue.Auth +import io.github.jan.supabase.gotrue.auth +import io.github.jan.supabase.gotrue.exception.AuthRestException +import io.github.jan.supabase.gotrue.exception.AuthWeakPasswordException +import io.github.jan.supabase.gotrue.minimalSettings +import io.github.jan.supabase.gotrue.providers.builtin.Email +import io.github.jan.supabase.testing.createMockedSupabaseClient +import io.github.jan.supabase.testing.respondJson +import io.ktor.http.HttpStatusCode +import kotlinx.coroutines.test.runTest +import kotlinx.serialization.json.add +import kotlinx.serialization.json.buildJsonObject +import kotlinx.serialization.json.put +import kotlinx.serialization.json.putJsonArray +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFails +import kotlin.test.assertFailsWith +import kotlin.test.assertIs +import kotlin.test.assertIsNot + class AuthRestExceptionTest { - //TODO + private val configuration: SupabaseClientBuilder.() -> Unit = { + install(Auth) { + minimalSettings() + } + } + + @Test + fun testErrorsWithErrorCode() { + runTest { + val client = createMockedSupabaseClient( + configuration = configuration, + requestHandler = { + respondJson( + code = HttpStatusCode.BadRequest, + json = buildJsonObject { + put("error_code", "error_code") + put("message", "error_message") + } + ) + } + ) + val exception = assertFailsWith { + client.auth.signUpWith(Email) { + email = "example@email.com" + password = "password" + } + } + assertEquals("error_code", exception.error) + assertEquals("error_message", exception.message) + } + } + + @Test + fun testPasswordWeakAuthRestException() { + runTest { + val client = createMockedSupabaseClient( + configuration = configuration, + requestHandler = { + respondJson( + code = HttpStatusCode.BadRequest, + json = buildJsonObject { + put("error_code", "weak_password") + put("message", "error_message") + put("weak_password", buildJsonObject { + putJsonArray("reasons") { + add("reason1") + add("reason2") + } + }) + } + ) + } + ) + val exception = assertFailsWith { + client.auth.signUpWith(Email) { + email = "example@email.com" + password = "password" + } + } + assertEquals("weak_password", exception.error) + assertEquals("error_message", exception.message) + assertEquals(listOf("reason1", "reason2"), exception.reasons) + } + } + + @Test + fun testErrorsWithoutErrorCode() { + runTest { + val client = createMockedSupabaseClient( + configuration = configuration, + requestHandler = { + respondJson( + code = HttpStatusCode.BadRequest, + json = buildJsonObject { + put("message", "error_message") + } + ) + } + ) + val exception = assertFails { + client.auth.signUpWith(Email) { + email = "example@email.com" + password = "password" + } + } + assertIsNot(exception) + assertIs(exception) + } + } } \ No newline at end of file diff --git a/GoTrue/src/commonTest/kotlin/AuthTest.kt b/GoTrue/src/commonTest/kotlin/AuthTest.kt index 125000f4..09415de3 100644 --- a/GoTrue/src/commonTest/kotlin/AuthTest.kt +++ b/GoTrue/src/commonTest/kotlin/AuthTest.kt @@ -5,12 +5,15 @@ import io.github.jan.supabase.gotrue.SessionStatus import io.github.jan.supabase.gotrue.auth import io.github.jan.supabase.gotrue.minimalSettings import io.github.jan.supabase.gotrue.providers.Github +import io.github.jan.supabase.gotrue.user.Identity +import io.github.jan.supabase.gotrue.user.UserInfo import io.github.jan.supabase.gotrue.user.UserSession import io.github.jan.supabase.testing.createMockedSupabaseClient import io.github.jan.supabase.testing.pathAfterVersion import io.github.jan.supabase.testing.respondJson import io.ktor.http.Url import kotlinx.coroutines.test.runTest +import kotlinx.serialization.json.buildJsonObject import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertIs @@ -153,6 +156,47 @@ class AuthTest { } + @Test + fun testSessionMethods() { + runTest { + val client = createMockedSupabaseClient(configuration = configuration) + client.auth.awaitInitialization() + val expectedIdentities = listOf(Identity("id", buildJsonObject { }, provider = "provider", userId = "userId")) + val expectedUser = UserInfo( + id = "id", + aud = "aud", + identities = expectedIdentities + ) + val session = UserSession( + accessToken = "accessToken", + refreshToken = "refreshToken", + expiresIn = 3600, + tokenType = "Bearer", + user = expectedUser + ) + client.auth.importSession(session) + assertEquals(session, (client.auth.sessionStatus.value as SessionStatus.Authenticated).session) + assertEquals(session, client.auth.currentSessionOrNull()) + assertEquals(expectedUser, client.auth.currentUserOrNull()) + assertEquals(expectedIdentities, client.auth.currentIdentitiesOrNull()) + client.auth.clearSession() + assertNull(client.auth.currentSessionOrNull()) + } + } + + @Test + fun testClearSession() { + runTest { + val client = createMockedSupabaseClient(configuration = configuration) + client.auth.awaitInitialization() + val session = userSession() + client.auth.importSession(session) + assertIs(client.auth.sessionStatus.value) + client.auth.clearSession() + assertIs(client.auth.sessionStatus.value) + } + } + } fun userSession(expiresIn: Long = 3600) = UserSession( diff --git a/test-common/src/commonMain/kotlin/io/github/jan/supabase/testing/KtorUtils.kt b/test-common/src/commonMain/kotlin/io/github/jan/supabase/testing/KtorUtils.kt index 25721075..a36a4d7f 100644 --- a/test-common/src/commonMain/kotlin/io/github/jan/supabase/testing/KtorUtils.kt +++ b/test-common/src/commonMain/kotlin/io/github/jan/supabase/testing/KtorUtils.kt @@ -6,6 +6,7 @@ import io.ktor.client.engine.mock.toByteArray import io.ktor.client.request.HttpResponseData import io.ktor.http.ContentType import io.ktor.http.HttpHeaders +import io.ktor.http.HttpStatusCode import io.ktor.http.content.OutgoingContent import io.ktor.http.headersOf import kotlinx.serialization.encodeToString @@ -16,8 +17,8 @@ suspend fun OutgoingContent.toJsonElement(): JsonElement { return Json.decodeFromString(toByteArray().decodeToString()) } -fun MockRequestHandleScope.respondJson(json: String): HttpResponseData { - return respond(json, headers = headersOf(HttpHeaders.ContentType, ContentType.Application.Json.toString())) +fun MockRequestHandleScope.respondJson(json: String, code: HttpStatusCode = HttpStatusCode.OK): HttpResponseData { + return respond(json, headers = headersOf(HttpHeaders.ContentType, ContentType.Application.Json.toString()), status = code) } -inline fun MockRequestHandleScope.respondJson(json: T): HttpResponseData = respondJson(Json.encodeToString(json)) \ No newline at end of file +inline fun MockRequestHandleScope.respondJson(json: T, code: HttpStatusCode = HttpStatusCode.OK): HttpResponseData = respondJson(Json.encodeToString(json), code) \ No newline at end of file