Skip to content

Commit 25efe1c

Browse files
committed
ktlint formatting + update of user data after signInWithCustomToken + photoUrl and displayName implementation
1 parent aa0c563 commit 25efe1c

File tree

10 files changed

+814
-396
lines changed

10 files changed

+814
-396
lines changed

src/main/java/android/net/Uri.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ package android.net
33
import java.net.URI
44
import java.util.Collections
55

6-
class Uri(private val uri: URI) {
7-
6+
class Uri(
7+
private val uri: URI,
8+
) {
89
companion object {
910
@JvmStatic
1011
fun parse(uriString: String) = Uri(URI.create(uriString))
1112
}
1213

14+
override fun toString(): String = uri.toString()
15+
1316
val scheme get() = uri.scheme
1417
val port get() = uri.port
1518
val host get() = uri.host

src/main/java/com/google/firebase/auth/FirebaseAuth.kt

Lines changed: 543 additions & 265 deletions
Large diffs are not rendered by default.

src/main/java/com/google/firebase/auth/FirebaseUser.kt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,45 @@ import com.google.android.gms.tasks.Task
55
abstract class FirebaseUser {
66
abstract val uid: String
77
abstract val email: String?
8+
abstract val photoUrl: String?
9+
abstract val displayName: String?
810
abstract val isAnonymous: Boolean
11+
912
abstract fun delete(): Task<Void>
13+
1014
abstract fun reload(): Task<Void>
11-
abstract fun verifyBeforeUpdateEmail(newEmail: String, actionCodeSettings: ActionCodeSettings?): Task<Unit>
15+
16+
abstract fun verifyBeforeUpdateEmail(
17+
newEmail: String,
18+
actionCodeSettings: ActionCodeSettings?,
19+
): Task<Unit>
20+
1221
abstract fun updateEmail(email: String): Task<Unit>
1322

14-
val displayName: String get() = TODO()
23+
abstract fun getIdToken(forceRefresh: Boolean): Task<GetTokenResult>
24+
25+
abstract fun updateProfile(request: UserProfileChangeRequest): Task<Unit>
26+
1527
val phoneNumber: String get() = TODO()
16-
val photoUrl: String? get() = TODO()
1728
val isEmailVerified: Boolean get() = TODO()
1829
val metadata: FirebaseUserMetadata get() = TODO()
1930
val multiFactor: MultiFactor get() = TODO()
2031
val providerData: List<UserInfo> get() = TODO()
2132
val providerId: String get() = TODO()
22-
abstract fun getIdToken(forceRefresh: Boolean): Task<GetTokenResult>
33+
2334
fun linkWithCredential(credential: AuthCredential): Task<AuthResult> = TODO()
35+
2436
fun sendEmailVerification(): Task<Unit> = TODO()
37+
2538
fun sendEmailVerification(actionCodeSettings: ActionCodeSettings): Task<Unit> = TODO()
39+
2640
fun unlink(provider: String): Task<AuthResult> = TODO()
41+
2742
fun updatePassword(password: String): Task<Unit> = TODO()
43+
2844
fun updatePhoneNumber(credential: AuthCredential): Task<Unit> = TODO()
29-
fun updateProfile(request: UserProfileChangeRequest): Task<Unit> = TODO()
45+
3046
fun reauthenticate(credential: AuthCredential): Task<Unit> = TODO()
47+
3148
fun reauthenticateAndRetrieveData(credential: AuthCredential): Task<AuthResult> = TODO()
3249
}

src/main/java/com/google/firebase/auth/OobRequestType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ internal enum class OobRequestType {
55
EMAIL_SIGNIN,
66
VERIFY_EMAIL,
77
VERIFY_AND_CHANGE_EMAIL
8-
}
8+
}

src/main/java/com/google/firebase/auth/UserProfileChangeRequest.java

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.google.firebase.auth
2+
3+
import android.net.Uri
4+
import android.os.Parcel
5+
import android.os.Parcelable
6+
7+
class UserProfileChangeRequest private constructor(
8+
internal val displayName: String?,
9+
internal val photoUrl: String?,
10+
) : Parcelable {
11+
override fun describeContents(): Int = displayName.hashCode() + photoUrl.hashCode()
12+
13+
override fun writeToParcel(
14+
dest: Parcel,
15+
flags: Int,
16+
) {
17+
dest.writeString(displayName)
18+
dest.writeString(photoUrl)
19+
}
20+
21+
internal companion object CREATOR : Parcelable.Creator<UserProfileChangeRequest> {
22+
override fun createFromParcel(parcel: Parcel): UserProfileChangeRequest {
23+
val displayName = parcel.readString()
24+
val photoUri = parcel.readString()
25+
return UserProfileChangeRequest(displayName, photoUri)
26+
}
27+
28+
override fun newArray(size: Int): Array<UserProfileChangeRequest?> = arrayOfNulls(size)
29+
}
30+
31+
class Builder {
32+
private var displayName: String? = null
33+
private var photoUri: Uri? = null
34+
35+
fun setDisplayName(name: String?): Builder {
36+
this.displayName = name
37+
return this
38+
}
39+
40+
fun setPhotoUri(uri: Uri?): Builder {
41+
this.photoUri = uri
42+
return this
43+
}
44+
45+
fun build(): UserProfileChangeRequest = UserProfileChangeRequest(displayName, photoUri?.toString())
46+
}
47+
}

src/test/kotlin/AppTest.kt

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,34 @@ import org.junit.Test
88
class AppTest : FirebaseTest() {
99
@Test
1010
fun testInitialize() {
11-
FirebasePlatform.initializeFirebasePlatform(object : FirebasePlatform() {
12-
val storage = mutableMapOf<String, String>()
13-
override fun store(key: String, value: String) = storage.set(key, value)
14-
override fun retrieve(key: String) = storage[key]
15-
override fun clear(key: String) { storage.remove(key) }
16-
override fun log(msg: String) = println(msg)
17-
})
18-
val options = FirebaseOptions.Builder()
19-
.setProjectId("my-firebase-project")
20-
.setApplicationId("1:27992087142:android:ce3b6448250083d1")
21-
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
22-
// setDatabaseURL(...)
23-
// setStorageBucket(...)
24-
.build()
11+
FirebasePlatform.initializeFirebasePlatform(
12+
object : FirebasePlatform() {
13+
val storage = mutableMapOf<String, String>()
14+
15+
override fun store(
16+
key: String,
17+
value: String,
18+
) = storage.set(key, value)
19+
20+
override fun retrieve(key: String) = storage[key]
21+
22+
override fun clear(key: String) {
23+
storage.remove(key)
24+
}
25+
26+
override fun log(msg: String) = println(msg)
27+
},
28+
)
29+
val options =
30+
FirebaseOptions
31+
.Builder()
32+
.setProjectId("fir-java-sdk")
33+
.setApplicationId("1:341458593155:web:bf8e1aa37efe01f32d42b6")
34+
.setApiKey("AIzaSyCvVHjTJHyeStnzIE7J9LLtHqWk6reGM08")
35+
.setDatabaseUrl("https://fir-java-sdk-default-rtdb.firebaseio.com")
36+
.setStorageBucket("fir-java-sdk.appspot.com")
37+
.setGcmSenderId("341458593155")
38+
.build()
2539
val app = Firebase.initialize(Application(), options)
2640
}
2741
}

src/test/kotlin/AuthTest.kt

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,97 @@
1-
import com.google.firebase.auth.FirebaseAuth
1+
import android.net.Uri
22
import com.google.firebase.auth.FirebaseAuthInvalidUserException
33
import kotlinx.coroutines.runBlocking
44
import kotlinx.coroutines.tasks.await
55
import kotlinx.coroutines.test.runTest
66
import org.junit.Assert.assertEquals
7+
import org.junit.Assert.assertNotEquals
78
import org.junit.Assert.assertThrows
9+
import org.junit.Before
810
import org.junit.Test
11+
import java.util.UUID
912

1013
class AuthTest : FirebaseTest() {
11-
private fun createAuth(): FirebaseAuth {
12-
return FirebaseAuth(app).apply {
14+
private val email = "email${UUID.randomUUID()}@example.com"
15+
16+
@Before
17+
fun initialize() {
18+
auth.apply {
1319
useEmulator("localhost", 9099)
1420
}
1521
}
1622

1723
@Test
18-
fun `should authenticate via anonymous auth`() = runTest {
19-
val auth = createAuth()
24+
fun `should authenticate via anonymous auth`() =
25+
runTest {
26+
auth.signInAnonymously().await()
2027

21-
auth.signInAnonymously().await()
28+
assertEquals(true, auth.currentUser?.isAnonymous)
29+
}
2230

23-
assertEquals(true, auth.currentUser?.isAnonymous)
24-
}
31+
@Test
32+
fun `should create user via email and password`() =
33+
runTest {
34+
val createResult = auth.createUserWithEmailAndPassword(email, "test123").await()
35+
assertNotEquals(null, createResult.user?.uid)
36+
assertEquals(null, createResult.user?.displayName)
37+
// assertEquals(null, createResult.user?.phoneNumber)
38+
assertEquals(false, createResult.user?.isAnonymous)
39+
assertEquals(email, createResult.user?.email)
40+
assertNotEquals("", createResult.user!!.email)
41+
42+
val signInResult = auth.signInWithEmailAndPassword(email, "test123").await()
43+
assertEquals(createResult.user?.uid, signInResult.user?.uid)
44+
}
2545

2646
@Test
27-
fun `should authenticate via email and password`() = runTest {
28-
val auth = createAuth()
47+
fun `should authenticate via email and password`() =
48+
runTest {
49+
auth.createUserWithEmailAndPassword(email, "test123").await()
2950

30-
auth.signInWithEmailAndPassword("email@example.com", "securepassword").await()
51+
auth.signInWithEmailAndPassword(email, "test123").await()
3152

32-
assertEquals(false, auth.currentUser?.isAnonymous)
33-
}
53+
assertEquals(false, auth.currentUser?.isAnonymous)
54+
}
3455

3556
@Test
36-
fun `should throw exception on invalid password`() {
37-
val auth = createAuth()
57+
fun `should update displayName and photoUrl`() =
58+
runTest {
59+
auth
60+
.createUserWithEmailAndPassword(email, "test123")
61+
.await()
62+
.user
63+
auth.currentUser
64+
?.updateProfile(
65+
com.google.firebase.auth.UserProfileChangeRequest
66+
.Builder()
67+
.setDisplayName("testDisplayName")
68+
.setPhotoUri(Uri.parse("https://picsum.photos/100"))
69+
.build(),
70+
)?.await()
71+
assertEquals("testDisplayName", auth.currentUser?.displayName)
72+
assertEquals("https://picsum.photos/100", auth.currentUser?.photoUrl)
73+
}
3874

39-
val exception = assertThrows(FirebaseAuthInvalidUserException::class.java) {
40-
runBlocking {
41-
auth.signInWithEmailAndPassword("email@example.com", "wrongpassword").await()
42-
}
75+
@Test
76+
fun `should sign in anonymously`() =
77+
runTest {
78+
val signInResult = auth.signInAnonymously().await()
79+
assertNotEquals("", signInResult.user!!.email)
80+
assertEquals(true, signInResult.user?.isAnonymous)
4381
}
4482

45-
assertEquals("INVALID_PASSWORD", exception.errorCode)
46-
}
83+
@Test
84+
fun `should throw exception on invalid password`() =
85+
runTest {
86+
auth.createUserWithEmailAndPassword(email, "test123").await()
87+
88+
val exception =
89+
assertThrows(FirebaseAuthInvalidUserException::class.java) {
90+
runBlocking {
91+
auth.signInWithEmailAndPassword(email, "wrongpassword").await()
92+
}
93+
}
94+
95+
assertEquals("INVALID_PASSWORD", exception.errorCode)
96+
}
4797
}

0 commit comments

Comments
 (0)