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

signupNewUser implementation #33

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions src/main/java/android/net/Uri.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package android.net
import java.net.URI
import java.util.Collections

class Uri(private val uri: URI) {

class Uri(
private val uri: URI,
) {
companion object {
@JvmStatic
fun parse(uriString: String) = Uri(URI.create(uriString))
}

override fun toString(): String = uri.toString()

val scheme get() = uri.scheme
val port get() = uri.port
val host get() = uri.host
Expand Down
794 changes: 601 additions & 193 deletions src/main/java/com/google/firebase/auth/FirebaseAuth.kt

Large diffs are not rendered by default.

31 changes: 24 additions & 7 deletions src/main/java/com/google/firebase/auth/FirebaseUser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,46 @@ import com.google.android.gms.tasks.Task

abstract class FirebaseUser {
abstract val uid: String
abstract val email: String?
abstract val photoUrl: String?
abstract val displayName: String?
abstract val isAnonymous: Boolean

abstract fun delete(): Task<Void>

abstract fun reload(): Task<Void>

val email: String get() = TODO()
val displayName: String get() = TODO()
abstract fun verifyBeforeUpdateEmail(
newEmail: String,
actionCodeSettings: ActionCodeSettings?,
): Task<Unit>

abstract fun updateEmail(email: String): Task<Unit>

abstract fun getIdToken(forceRefresh: Boolean): Task<GetTokenResult>

abstract fun updateProfile(request: UserProfileChangeRequest): Task<Unit>

val phoneNumber: String get() = TODO()
val photoUrl: String? get() = TODO()
val isEmailVerified: Boolean get() = TODO()
val metadata: FirebaseUserMetadata get() = TODO()
val multiFactor: MultiFactor get() = TODO()
val providerData: List<UserInfo> get() = TODO()
val providerId: String get() = TODO()
abstract fun getIdToken(forceRefresh: Boolean): Task<GetTokenResult>

fun linkWithCredential(credential: AuthCredential): Task<AuthResult> = TODO()

fun sendEmailVerification(): Task<Unit> = TODO()

fun sendEmailVerification(actionCodeSettings: ActionCodeSettings): Task<Unit> = TODO()

fun unlink(provider: String): Task<AuthResult> = TODO()
fun updateEmail(email: String): Task<Unit> = TODO()

fun updatePassword(password: String): Task<Unit> = TODO()

fun updatePhoneNumber(credential: AuthCredential): Task<Unit> = TODO()
fun updateProfile(request: UserProfileChangeRequest): Task<Unit> = TODO()
fun verifyBeforeUpdateEmail(newEmail: String, actionCodeSettings: ActionCodeSettings?): Task<Unit> = TODO()

fun reauthenticate(credential: AuthCredential): Task<Unit> = TODO()

fun reauthenticateAndRetrieveData(credential: AuthCredential): Task<AuthResult> = TODO()
}
8 changes: 8 additions & 0 deletions src/main/java/com/google/firebase/auth/OobRequestType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.google.firebase.auth

internal enum class OobRequestType {
PASSWORD_RESET,
EMAIL_SIGNIN,
VERIFY_EMAIL,
VERIFY_AND_CHANGE_EMAIL
}

This file was deleted.

47 changes: 47 additions & 0 deletions src/main/java/com/google/firebase/auth/UserProfileChangeRequest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.google.firebase.auth

import android.net.Uri
import android.os.Parcel
import android.os.Parcelable

class UserProfileChangeRequest private constructor(
internal val displayName: String?,
internal val photoUrl: String?,
) : Parcelable {
override fun describeContents(): Int = displayName.hashCode() + photoUrl.hashCode()

override fun writeToParcel(
dest: Parcel,
flags: Int,
) {
dest.writeString(displayName)
dest.writeString(photoUrl)
}

internal companion object CREATOR : Parcelable.Creator<UserProfileChangeRequest> {
override fun createFromParcel(parcel: Parcel): UserProfileChangeRequest {
val displayName = parcel.readString()
val photoUri = parcel.readString()
return UserProfileChangeRequest(displayName, photoUri)
}

override fun newArray(size: Int): Array<UserProfileChangeRequest?> = arrayOfNulls(size)
}

class Builder {
private var displayName: String? = null
private var photoUri: Uri? = null

fun setDisplayName(name: String?): Builder {
this.displayName = name
return this
}

fun setPhotoUri(uri: Uri?): Builder {
this.photoUri = uri
return this
}

fun build(): UserProfileChangeRequest = UserProfileChangeRequest(displayName, photoUri?.toString())
}
}
42 changes: 28 additions & 14 deletions src/test/kotlin/AppTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,34 @@ import org.junit.Test
class AppTest : FirebaseTest() {
@Test
fun testInitialize() {
FirebasePlatform.initializeFirebasePlatform(object : FirebasePlatform() {
val storage = mutableMapOf<String, String>()
override fun store(key: String, value: String) = storage.set(key, value)
override fun retrieve(key: String) = storage[key]
override fun clear(key: String) { storage.remove(key) }
override fun log(msg: String) = println(msg)
})
val options = FirebaseOptions.Builder()
.setProjectId("my-firebase-project")
.setApplicationId("1:27992087142:android:ce3b6448250083d1")
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
// setDatabaseURL(...)
// setStorageBucket(...)
.build()
FirebasePlatform.initializeFirebasePlatform(
object : FirebasePlatform() {
val storage = mutableMapOf<String, String>()

override fun store(
key: String,
value: String,
) = storage.set(key, value)

override fun retrieve(key: String) = storage[key]

override fun clear(key: String) {
storage.remove(key)
}

override fun log(msg: String) = println(msg)
},
)
val options =
FirebaseOptions
.Builder()
.setProjectId("fir-java-sdk")
.setApplicationId("1:341458593155:web:bf8e1aa37efe01f32d42b6")
.setApiKey("AIzaSyCvVHjTJHyeStnzIE7J9LLtHqWk6reGM08")
.setDatabaseUrl("https://fir-java-sdk-default-rtdb.firebaseio.com")
.setStorageBucket("fir-java-sdk.appspot.com")
.setGcmSenderId("341458593155")
.build()
val app = Firebase.initialize(Application(), options)
}
}
107 changes: 86 additions & 21 deletions src/test/kotlin/AuthTest.kt
Original file line number Diff line number Diff line change
@@ -1,47 +1,112 @@
import com.google.firebase.auth.FirebaseAuth
import android.net.Uri
import com.google.firebase.auth.FirebaseAuthInvalidUserException
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertThrows
import org.junit.Before
import org.junit.Test
import java.util.UUID

class AuthTest : FirebaseTest() {
private fun createAuth(): FirebaseAuth {
return FirebaseAuth(app).apply {
private val email = "email${UUID.randomUUID()}@example.com"

@Before
fun initialize() {
auth.apply {
useEmulator("localhost", 9099)
}
}

@Test
fun `should authenticate via anonymous auth`() = runTest {
val auth = createAuth()
fun `should authenticate via anonymous auth`() =
runTest {
auth.signInAnonymously().await()

auth.signInAnonymously().await()
assertEquals(true, auth.currentUser?.isAnonymous)
}

assertEquals(true, auth.currentUser?.isAnonymous)
}
@Test
fun `should create user via email and password`() =
runTest {
val createResult = auth.createUserWithEmailAndPassword(email, "test123").await()
assertNotEquals(null, createResult.user?.uid)
assertEquals(null, createResult.user?.displayName)
// assertEquals(null, createResult.user?.phoneNumber)
assertEquals(false, createResult.user?.isAnonymous)
assertEquals(email, createResult.user?.email)
assertNotEquals("", createResult.user!!.email)

val signInResult = auth.signInWithEmailAndPassword(email, "test123").await()
assertEquals(createResult.user?.uid, signInResult.user?.uid)
}

@Test
fun `should authenticate via email and password`() = runTest {
val auth = createAuth()
fun `should authenticate via email and password`() =
runTest {
auth.createUserWithEmailAndPassword(email, "test123").await()

auth.signInWithEmailAndPassword("email@example.com", "securepassword").await()
auth.signInWithEmailAndPassword(email, "test123").await()

assertEquals(false, auth.currentUser?.isAnonymous)
}
assertEquals(false, auth.currentUser?.isAnonymous)
}

/*@Test
fun `should authenticate via custom token`() =
runTest {
val user = auth.createUserWithEmailAndPassword(email, "test123").await()
auth
.signInWithCustomToken(
user.user
.getIdToken(false)
.await()
.token ?: "",
).await()

assertEquals(false, auth.currentUser?.isAnonymous)
}*/

@Test
fun `should throw exception on invalid password`() {
val auth = createAuth()
fun `should update displayName and photoUrl`() =
runTest {
auth
.createUserWithEmailAndPassword(email, "test123")
.await()
.user
auth.currentUser
?.updateProfile(
com.google.firebase.auth.UserProfileChangeRequest
.Builder()
.setDisplayName("testDisplayName")
.setPhotoUri(Uri.parse("https://picsum.photos/100"))
.build(),
)?.await()
assertEquals("testDisplayName", auth.currentUser?.displayName)
assertEquals("https://picsum.photos/100", auth.currentUser?.photoUrl)
}

val exception = assertThrows(FirebaseAuthInvalidUserException::class.java) {
runBlocking {
auth.signInWithEmailAndPassword("[email protected]", "wrongpassword").await()
}
@Test
fun `should sign in anonymously`() =
runTest {
val signInResult = auth.signInAnonymously().await()
assertNotEquals("", signInResult.user!!.email)
assertEquals(true, signInResult.user?.isAnonymous)
}

assertEquals("INVALID_PASSWORD", exception.errorCode)
}
@Test
fun `should throw exception on invalid password`() =
runTest {
auth.createUserWithEmailAndPassword(email, "test123").await()

val exception =
assertThrows(FirebaseAuthInvalidUserException::class.java) {
runBlocking {
auth.signInWithEmailAndPassword(email, "wrongpassword").await()
}
}

assertEquals("INVALID_PASSWORD", exception.errorCode)
}
}
57 changes: 57 additions & 0 deletions src/test/kotlin/FirebaseAuthTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

import android.net.Uri
import com.google.firebase.auth.FirebaseUser
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Test
import java.util.UUID

internal class FirebaseAuthTest : FirebaseTest() {
@Test
fun testCreateUserWithEmailAndPassword() =
runTest {
val email = "test+${UUID.randomUUID()}@test.com"
val createResult = auth.createUserWithEmailAndPassword(email, "test123").await()
assertNotEquals(null, createResult.user?.uid)
// assertEquals(null, createResult.user?.displayName)
// assertEquals(null, createResult.user?.phoneNumber)
assertEquals(false, createResult.user?.isAnonymous)
assertEquals(email, createResult.user?.email)
assertNotEquals("", createResult.user!!.email)

val signInResult = auth.signInWithEmailAndPassword(email, "test123").await()
assertEquals(createResult.user?.uid, signInResult.user?.uid)
}

@Test
fun testUpdateProfile() =
runTest {
val user = createUser()
user
?.updateProfile(
com.google.firebase.auth.UserProfileChangeRequest
.Builder()
.setDisplayName("testDisplayName")
.setPhotoUri(Uri.parse("https://picsum.photos/100"))
.build(),
)?.await()
assertEquals("testDisplayName", auth.currentUser?.displayName)
assertEquals("https://picsum.photos/100", auth.currentUser?.photoUrl)
}

@Test
fun testSignInAnonymously() =
runTest {
val signInResult = auth.signInAnonymously().await()
assertNotEquals("", signInResult.user!!.email)
assertEquals(true, signInResult.user?.isAnonymous)
}

private suspend fun createUser(email: String = "test+${UUID.randomUUID()}@test.com"): FirebaseUser? =
auth
.createUserWithEmailAndPassword(email, "test123")
.await()
.user
}
Loading
Loading