generated from grodin/kotlin-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: More FireBase auth integration tests (#129)
Co-authored-by: Joseph Cooper <[email protected]>
- Loading branch information
Showing
14 changed files
with
146 additions
and
10 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ public class ActualAuthWorkflowTest : | |
"onAuthenticated action outputs Authenticated(user) from workflow" { | ||
val workflow = ActualAuthWorkflow(TestAuthService(), NullSignupWorkflow) | ||
val fakeCredential = EmailPasswordCredential("[email protected]", "12345") | ||
val fakeUser = TestUser("user1", UserUid("1")) | ||
val fakeUser = TestUser("user1", UserUid("1"), "[email protected]") | ||
val (_, maybeOutput) = | ||
workflow | ||
.onAuthenticated(fakeUser) | ||
|
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 |
---|---|---|
|
@@ -50,7 +50,7 @@ internal class ActualSignUpWorkflowTest : | |
"onUserCreated sets output to created user" { | ||
val workflow = ActualSignUpWorkflow(TestAuthService()) | ||
val credential = EmailPasswordCredential("blah@blah", "password") | ||
val user = TestUser("user1", UserUid("1234")) | ||
val user = TestUser("user1", UserUid("1234"), "[email protected]") | ||
|
||
val (_, maybeOutput) = | ||
workflow | ||
|
12 changes: 11 additions & 1 deletion
12
core/src/test/kotlin/com/omricat/maplibrarian/auth/TestUser.kt
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,6 +1,16 @@ | ||
package com.omricat.maplibrarian.auth | ||
|
||
import com.omricat.maplibrarian.model.EmailAddress | ||
import com.omricat.maplibrarian.model.User | ||
import com.omricat.maplibrarian.model.UserUid | ||
|
||
internal data class TestUser(override val displayName: String, override val id: UserUid) : User | ||
internal data class TestUser( | ||
override val displayName: String, | ||
override val id: UserUid, | ||
override val emailAddress: EmailAddress | ||
) : User { | ||
companion object { | ||
operator fun invoke(displayName: String, id: UserUid, emailAddress: String): TestUser = | ||
TestUser(displayName, id, EmailAddress(emailAddress)) | ||
} | ||
} |
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
56 changes: 55 additions & 1 deletion
56
...ase/src/main/kotlin/com/omricat/maplibrarian/firebase/auth/FirebaseAuthEmulatorRestApi.kt
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,21 +1,75 @@ | ||
package com.omricat.maplibrarian.firebase.auth | ||
|
||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import com.omricat.maplibrarian.auth.EmailPasswordCredential | ||
import com.omricat.maplibrarian.model.EmailAddress | ||
import com.omricat.maplibrarian.model.User | ||
import com.omricat.maplibrarian.model.UserUid | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.HttpUrl | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.RequestBody | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import retrofit2.Call | ||
import retrofit2.Response | ||
import retrofit2.Retrofit | ||
import retrofit2.create | ||
import retrofit2.http.Body | ||
import retrofit2.http.DELETE | ||
import retrofit2.http.Headers | ||
import retrofit2.http.POST | ||
import retrofit2.http.Path | ||
|
||
class FirebaseAuthEmulatorRestApi(private val projectId: String, baseUrl: HttpUrl) { | ||
|
||
private val jsonMediaType = "application/json; charset=utf-8".toMediaType() | ||
|
||
private interface AuthEmulatorApi { | ||
|
||
@DELETE("/emulator/v1/projects/{project-id}/accounts") | ||
fun deleteAllUsers(@Path("project-id") projectId: String): Call<Unit> | ||
|
||
@Headers("Authorization: Bearer owner") | ||
@POST("/identitytoolkit.googleapis.com/v1/projects/{project-id}/accounts") | ||
fun createUser( | ||
@Path("project-id") projectId: String, | ||
@Body body: RequestBody | ||
): Call<TestUser> | ||
} | ||
|
||
@Serializable | ||
data class TestUser(override val displayName: String, val localId: String, val email: String) : | ||
User { | ||
override val id: UserUid = UserUid(localId) | ||
override val emailAddress: EmailAddress = EmailAddress(email) | ||
} | ||
|
||
private val wrappedApi: AuthEmulatorApi = Retrofit.Builder().baseUrl(baseUrl).build().create() | ||
private val json = Json { ignoreUnknownKeys = true } | ||
|
||
private val wrappedApi: AuthEmulatorApi = | ||
Retrofit.Builder() | ||
.baseUrl(baseUrl) | ||
.addConverterFactory(json.asConverterFactory(jsonMediaType)) | ||
.build() | ||
.create() | ||
fun deleteAllUsers(): Response<Unit> = wrappedApi.deleteAllUsers(projectId).execute() | ||
|
||
fun createUser(credential: EmailPasswordCredential): Response<TestUser> { | ||
|
||
val jsonBody = | ||
"""{ | ||
"customAttributes":"", | ||
"displayName":"", | ||
"photoUrl":"", | ||
"email":"${credential.emailAddress}", | ||
"password":"${credential.password}", | ||
"phoneNumber":"", | ||
"emailVerified":false, | ||
"mfaInfo":[] | ||
}""" | ||
.toRequestBody(contentType = jsonMediaType) | ||
val call = wrappedApi.createUser(projectId, jsonBody) | ||
return call.execute() | ||
} | ||
} |
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
File renamed without changes.
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
12 changes: 12 additions & 0 deletions
12
...result-assertk-extensions/src/main/kotlin/com/omricat/result/kotest/assertk/assertions.kt
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.omricat.result.kotest.assertk | ||
|
||
import assertk.Assert | ||
import assertk.assertions.isInstanceOf | ||
import com.github.michaelbull.result.Err | ||
import com.github.michaelbull.result.Ok | ||
import com.github.michaelbull.result.Result | ||
|
||
public fun <V> Assert<Result<V, *>>.isOk(): Assert<V> = isInstanceOf<Ok<V>>().transform { it.value } | ||
|
||
public fun <E> Assert<Result<*, E>>.isErr(): Assert<E> = | ||
isInstanceOf<Err<E>>().transform { it.error } |
File renamed without changes.