-
Notifications
You must be signed in to change notification settings - Fork 6
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 #38 from fmasa/auth-emulator
[WIP] Auth: Add support for emulator
- Loading branch information
Showing
10 changed files
with
164 additions
and
39 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"emulators": { | ||
"auth": { | ||
"port": 9099 | ||
}, | ||
"ui": { | ||
"enabled": true | ||
}, | ||
"singleProjectMode": true | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import com.google.firebase.auth.FirebaseAuth | ||
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.assertThrows | ||
import org.junit.Test | ||
|
||
class AuthTest : FirebaseTest() { | ||
private fun createAuth(): FirebaseAuth { | ||
return FirebaseAuth(app).apply { | ||
useEmulator("localhost", 9099) | ||
} | ||
} | ||
|
||
@Test | ||
fun `should authenticate via anonymous auth`() = runTest { | ||
val auth = createAuth() | ||
|
||
auth.signInAnonymously().await() | ||
|
||
assertEquals(true, auth.currentUser?.isAnonymous) | ||
} | ||
|
||
@Test | ||
fun `should authenticate via email and password`() = runTest { | ||
val auth = createAuth() | ||
|
||
auth.signInWithEmailAndPassword("[email protected]", "securepassword").await() | ||
|
||
assertEquals(false, auth.currentUser?.isAnonymous) | ||
} | ||
|
||
@Test | ||
fun `should throw exception on invalid password`() { | ||
val auth = createAuth() | ||
|
||
val exception = assertThrows(FirebaseAuthInvalidUserException::class.java) { | ||
runBlocking { | ||
auth.signInWithEmailAndPassword("[email protected]", "wrongpassword").await() | ||
} | ||
} | ||
|
||
assertEquals("INVALID_PASSWORD", exception.errorCode) | ||
} | ||
} |
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,9 +1,33 @@ | ||
import android.app.Application | ||
import com.google.firebase.Firebase | ||
import com.google.firebase.FirebaseApp | ||
import com.google.firebase.FirebaseOptions | ||
import com.google.firebase.FirebasePlatform | ||
import com.google.firebase.initialize | ||
import org.junit.Before | ||
import java.io.File | ||
|
||
abstract class FirebaseTest { | ||
protected val app: FirebaseApp get() { | ||
val options = FirebaseOptions.Builder() | ||
.setProjectId("my-firebase-project") | ||
.setApplicationId("1:27992087142:android:ce3b6448250083d1") | ||
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw") | ||
.build() | ||
|
||
return Firebase.initialize(Application(), options) | ||
} | ||
|
||
@Before | ||
fun beforeEach() { | ||
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) | ||
override fun getDatabasePath(name: String) = File("./build/$name") | ||
}) | ||
FirebaseApp.clearInstancesForTest() | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/test/resources/firebase_data/auth_export/accounts.json
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,29 @@ | ||
{ | ||
"kind": "identitytoolkit#DownloadAccountResponse", | ||
"users": [ | ||
{ | ||
"localId": "Ijat10t0F1gvH1VrClkkSqEcId1p", | ||
"lastLoginAt": "1728509249920", | ||
"displayName": "", | ||
"photoUrl": "", | ||
"emailVerified": true, | ||
"email": "[email protected]", | ||
"salt": "fakeSaltHsRxYqy9iKVQRLwz8975", | ||
"passwordHash": "fakeHash:salt=fakeSaltHsRxYqy9iKVQRLwz8975:password=securepassword", | ||
"passwordUpdatedAt": 1728509249921, | ||
"validSince": "1728509249", | ||
"mfaInfo": [], | ||
"createdAt": "1728509249920", | ||
"providerUserInfo": [ | ||
{ | ||
"providerId": "password", | ||
"email": "[email protected]", | ||
"federatedId": "[email protected]", | ||
"rawId": "[email protected]", | ||
"displayName": "", | ||
"photoUrl": "" | ||
} | ||
] | ||
} | ||
] | ||
} |
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,8 @@ | ||
{ | ||
"signIn": { | ||
"allowDuplicateEmails": false | ||
}, | ||
"emailPrivacyConfig": { | ||
"enableImprovedEmailPrivacy": false | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/test/resources/firebase_data/firebase-export-metadata.json
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,7 @@ | ||
{ | ||
"version": "13.3.1", | ||
"auth": { | ||
"version": "13.3.1", | ||
"path": "auth_export" | ||
} | ||
} |