-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #624 from supabase-community/more-tests
Add more Auth & AuthRestExceptionTests
- Loading branch information
Showing
3 changed files
with
160 additions
and
4 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
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) | ||
} | ||
} | ||
|
||
} |
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