Skip to content

Commit

Permalink
Merge pull request #624 from supabase-community/more-tests
Browse files Browse the repository at this point in the history
Add more Auth & AuthRestExceptionTests
  • Loading branch information
jan-tennert authored Jun 17, 2024
2 parents 4af0d1e + 85f6527 commit b75cfba
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 4 deletions.
113 changes: 112 additions & 1 deletion GoTrue/src/commonTest/kotlin/AuthRestExceptionTest.kt
Original file line number Diff line number Diff line change
@@ -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<AuthRestException> {
client.auth.signUpWith(Email) {
email = "[email protected]"
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<AuthWeakPasswordException> {
client.auth.signUpWith(Email) {
email = "[email protected]"
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 = "[email protected]"
password = "password"
}
}
assertIsNot<AuthRestException>(exception)
assertIs<BadRequestRestException>(exception)
}
}

}
44 changes: 44 additions & 0 deletions GoTrue/src/commonTest/kotlin/AuthTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<SessionStatus.Authenticated>(client.auth.sessionStatus.value)
client.auth.clearSession()
assertIs<SessionStatus.NotAuthenticated>(client.auth.sessionStatus.value)
}
}

}

fun userSession(expiresIn: Long = 3600) = UserSession(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <reified T> MockRequestHandleScope.respondJson(json: T): HttpResponseData = respondJson(Json.encodeToString(json))
inline fun <reified T> MockRequestHandleScope.respondJson(json: T, code: HttpStatusCode = HttpStatusCode.OK): HttpResponseData = respondJson(Json.encodeToString(json), code)

0 comments on commit b75cfba

Please sign in to comment.