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

Fix Part of #5001: Create Firebase Wrapper #5280

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4a68b9f
Create firebase auth wrapper
adhiamboperes Dec 11, 2023
1361ca3
Refactor firebase auth wrapper
adhiamboperes Dec 20, 2023
529216c
Refactor dagger bindings for firestore dependencies
adhiamboperes Dec 21, 2023
78997b2
Merge branch 'nps-optional-response-upload' of github.com:oppia/oppia…
adhiamboperes Dec 21, 2023
99b8e55
Refactor TestAuthenticationModule.kt to provide FirebaseAuthWrapper
adhiamboperes Dec 21, 2023
c8be1ac
Remove FakeAuthenticationController and refactored its usages to the …
adhiamboperes Dec 21, 2023
6331f71
Remove DebugFirestoreEventLogger.kt and usages.
adhiamboperes Dec 21, 2023
042a122
Update AuthenticationControllerTest tests
adhiamboperes Dec 21, 2023
37ad547
Fix static analysis checks
adhiamboperes Dec 21, 2023
07f8d0d
Fix bazel build errors
adhiamboperes Dec 22, 2023
93b08ea
Reformat auth/BUILD.bazel
adhiamboperes Dec 22, 2023
d60c7ee
Create wrapper for firebase auth instance
adhiamboperes Jan 4, 2024
215793e
Fix lint errors
adhiamboperes Jan 4, 2024
aba6319
Fix failing tests
adhiamboperes Jan 10, 2024
4197aa6
Fix failing lint checks
adhiamboperes Jan 10, 2024
2bfff35
Fix test failures
adhiamboperes Jan 10, 2024
aae2e24
Fix BUILD file formatting
adhiamboperes Jan 10, 2024
0f46645
Fix DebugFirestoreEventLoggerImplTest
adhiamboperes Jan 11, 2024
14f500b
Fix build failures
adhiamboperes Jan 11, 2024
f906b29
Fix test file exemption warnings
adhiamboperes Jan 11, 2024
06e1fbc
Fix bazel build input file
adhiamboperes Jan 11, 2024
fc80d61
Fix bazel build input file
adhiamboperes Jan 11, 2024
a67f28a
Fix failing tests
adhiamboperes Jan 11, 2024
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
4 changes: 2 additions & 2 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@

# All domain and utility-specific shared test infrastructure.
/testing/src/main/java/org/oppia/android/testing/FakeAnalyticsEventLogger.kt @oppia/android-app-infrastructure-reviewers
/testing/src/main/java/org/oppia/android/testing/FakeAuthenticationController.kt @oppia/android-app-infrastructure-reviewers
/testing/src/main/java/org/oppia/android/testing/FakeExceptionLogger.kt @oppia/android-app-infrastructure-reviewers
/testing/src/main/java/org/oppia/android/testing/FakeFirebaseAuthWrapperImpl.kt @oppia/android-app-infrastructure-reviewers
/testing/src/main/java/org/oppia/android/testing/FakeFirestoreEventLogger.kt @oppia/android-app-infrastructure-reviewers
/testing/src/main/java/org/oppia/android/testing/FakePerformanceMetricAssessor.kt @oppia/android-app-infrastructure-reviewers
/testing/src/main/java/org/oppia/android/testing/FakePerformanceMetricsEventLogger.kt @oppia/android-app-infrastructure-reviewers
Expand All @@ -102,8 +102,8 @@
/testing/src/main/java/org/oppia/android/testing/TestImageLoaderModule.kt @oppia/android-app-infrastructure-reviewers
/testing/src/main/java/org/oppia/android/testing/TestLogReportingModule.kt @oppia/android-app-infrastructure-reviewers
/testing/src/test/java/org/oppia/android/testing/FakeAnalyticsEventLoggerTest.kt @oppia/android-app-infrastructure-reviewers
/testing/src/test/java/org/oppia/android/testing/FakeAuthenticationControllerTest.kt @oppia/android-app-infrastructure-reviewers
/testing/src/test/java/org/oppia/android/testing/FakeExceptionLoggerTest.kt @oppia/android-app-infrastructure-reviewers
/testing/src/test/java/org/oppia/android/testing/FakeFirebaseAuthWrapperImplTest.kt @oppia/android-app-infrastructure-reviewers
/testing/src/test/java/org/oppia/android/testing/FakeFirestoreEventLoggerTest.kt @oppia/android-app-infrastructure-reviewers
/testing/src/test/java/org/oppia/android/testing/FakePerformanceMetricAssessorTest.kt @oppia/android-app-infrastructure-reviewers
/testing/src/test/java/org/oppia/android/testing/FakePerformanceMetricsEventLoggerTest.kt @oppia/android-app-infrastructure-reviewers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.oppia.android.app.translation.AppLanguageResourceHandler
import org.oppia.android.app.viewmodel.ObservableViewModel
import org.oppia.android.util.locale.OppiaLocale
import org.oppia.android.util.logging.firebase.DebugAnalyticsEventLogger
import org.oppia.android.util.logging.firebase.DebugFirestoreEventLogger
import org.oppia.android.util.logging.firebase.DebugFirestoreEventLoggerImpl
import javax.inject.Inject

/**
Expand All @@ -15,7 +15,7 @@ import javax.inject.Inject
@FragmentScope
class ViewEventLogsViewModel @Inject constructor(
debugAnalyticsEventLogger: DebugAnalyticsEventLogger,
debugFirestoreEventLogger: DebugFirestoreEventLogger,
debugFirestoreEventLogger: DebugFirestoreEventLoggerImpl,
private val machineLocale: OppiaLocale.MachineLocale,
private val resourceHandler: AppLanguageResourceHandler
) : ObservableViewModel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.firebase.FirebaseApp
import com.google.firebase.crashlytics.FirebaseCrashlytics
import dagger.Binds
import dagger.Component
import dagger.Module
import dagger.Provides
Expand Down Expand Up @@ -47,7 +46,6 @@ import org.oppia.android.app.translation.testing.ActivityRecreatorTestModule
import org.oppia.android.app.utility.OrientationChangeAction.Companion.orientationLandscape
import org.oppia.android.data.backends.gae.NetworkConfigProdModule
import org.oppia.android.data.backends.gae.NetworkModule
import org.oppia.android.domain.auth.AuthenticationWrapper
import org.oppia.android.domain.classify.InteractionsModule
import org.oppia.android.domain.classify.rules.algebraicexpressioninput.AlgebraicExpressionInputModule
import org.oppia.android.domain.classify.rules.continueinteraction.ContinueModule
Expand Down Expand Up @@ -83,9 +81,10 @@ import org.oppia.android.domain.platformparameter.PlatformParameterSingletonModu
import org.oppia.android.domain.question.QuestionModule
import org.oppia.android.domain.topic.PrimeTopicAssetsControllerModule
import org.oppia.android.domain.workmanager.WorkManagerConfigurationModule
import org.oppia.android.testing.FakeAuthenticationController
import org.oppia.android.testing.FakeFirestoreEventLogger
import org.oppia.android.testing.OppiaTestRule
import org.oppia.android.testing.RunOn
import org.oppia.android.testing.TestAuthenticationModule
import org.oppia.android.testing.TestPlatform
import org.oppia.android.testing.junit.InitializeDefaultLocaleRule
import org.oppia.android.testing.robolectric.RobolectricModule
import org.oppia.android.testing.threading.TestCoroutineDispatchers
Expand All @@ -103,12 +102,13 @@ import org.oppia.android.util.logging.ExceptionLogger
import org.oppia.android.util.logging.LoggerModule
import org.oppia.android.util.logging.SyncStatusModule
import org.oppia.android.util.logging.firebase.DebugAnalyticsEventLogger
import org.oppia.android.util.logging.firebase.DebugFirestoreEventLogger
import org.oppia.android.util.logging.firebase.DebugFirestoreEventLoggerImpl
import org.oppia.android.util.logging.firebase.FirebaseAnalyticsEventLogger
import org.oppia.android.util.logging.firebase.FirebaseExceptionLogger
import org.oppia.android.util.logging.firebase.FirebaseLogUploaderModule
import org.oppia.android.util.logging.firebase.FirestoreEventLogger
import org.oppia.android.util.logging.firebase.FirestoreEventLoggerProdImpl
import org.oppia.android.util.logging.firebase.FirestoreInstanceWrapper
import org.oppia.android.util.logging.firebase.FirestoreInstanceWrapperImpl
import org.oppia.android.util.logging.performancemetrics.PerformanceMetricsAssessorModule
import org.oppia.android.util.logging.performancemetrics.PerformanceMetricsConfigurationsModule
import org.oppia.android.util.logging.performancemetrics.PerformanceMetricsEventLogger
Expand Down Expand Up @@ -157,7 +157,7 @@ class ViewEventLogsFragmentTest {
lateinit var fakeOppiaClock: FakeOppiaClock

@Inject
lateinit var debugFirestoreEventLogger: DebugFirestoreEventLogger
lateinit var firestoreEventLogger: FirestoreEventLogger

@Before
fun setUp() {
Expand Down Expand Up @@ -397,7 +397,9 @@ class ViewEventLogsFragmentTest {
}
}

@Test
@Test // TODO(#5143): On robolectric, there is a conflict between Firestore's Sqlite and
// robolectric's ShadowSQLiteConnection but this is resolved in newer versions of robolectric.
@RunOn(TestPlatform.ESPRESSO)
fun testViewEventLogsFragment_dateAndTimeIsDisplayedCorrectly() {
launch(ViewEventLogsTestActivity::class.java).use { scenario ->
testCoroutineDispatchers.runCurrent()
Expand Down Expand Up @@ -612,7 +614,7 @@ class ViewEventLogsFragmentTest {
.setTimestamp(TEST_TIMESTAMP + 50000)
.build()

debugFirestoreEventLogger.uploadEvent(eventLog)
firestoreEventLogger.uploadEvent(eventLog)
}

private fun createOptionalSurveyResponseContext(
Expand Down Expand Up @@ -699,14 +701,6 @@ class ViewEventLogsFragmentTest {
fun provideFirestoreLogStorageCacheSize(): Int = 2
}

@Module
interface TestAuthModule {
@Binds
fun bindFakeAuthenticationController(
fakeAuthenticationController: FakeAuthenticationController
): AuthenticationWrapper
}

@Module
class TestLogReportingModule {
@Provides
Expand All @@ -728,12 +722,14 @@ class ViewEventLogsFragmentTest {

@Provides
@Singleton
fun provideFakeFirestoreEventLogger(): DebugFirestoreEventLogger = FakeFirestoreEventLogger()
fun provideDebugFirestoreEventLogger(
debugFirestoreEventLogger: DebugFirestoreEventLoggerImpl
): FirestoreEventLogger = debugFirestoreEventLogger

@Provides
@Singleton
fun provideFirestoreLogger(factory: FirestoreEventLoggerProdImpl.Factory):
FirestoreEventLogger = factory.createFirestoreEventLogger()
fun provideFirebaseFirestoreInstanceWrapper(wrapperImpl: FirestoreInstanceWrapperImpl):
FirestoreInstanceWrapper = wrapperImpl
}

// TODO(#59): Figure out a way to reuse modules instead of needing to re-declare them.
Expand Down Expand Up @@ -766,7 +762,7 @@ class ViewEventLogsFragmentTest {
PerformanceMetricsConfigurationsModule::class, TestingBuildFlavorModule::class,
EventLoggingConfigurationModule::class, ActivityRouterModule::class,
CpuPerformanceSnapshotterModule::class, ExplorationProgressModule::class,
TestAuthModule::class,
TestAuthenticationModule::class,
]
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.oppia.android.domain.auth

import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import kotlinx.coroutines.CompletableDeferred
import org.oppia.android.util.data.AsyncResult
import javax.inject.Inject
Expand All @@ -10,23 +8,22 @@ import javax.inject.Singleton
/** Controller for signing in and retrieving a Firebase user. */
@Singleton
class AuthenticationController @Inject constructor(
private val firebaseAuth: FirebaseAuth
) : AuthenticationWrapper {
private val firebaseAuthWrapper: FirebaseAuthWrapper
) {
/** Returns the current signed in user or null if there is no authenticated user. */
override fun getCurrentSignedInUser(): FirebaseUser? {
return firebaseAuth.currentUser
}
val currentFirebaseUser: FirebaseUserWrapper? = firebaseAuthWrapper.currentUser

/** Returns the result of an authentication task. */
override fun signInAnonymously(): CompletableDeferred<AsyncResult<Any?>> {
fun signInAnonymouslyWithFirebase(): CompletableDeferred<AsyncResult<Any?>> {
val deferredResult = CompletableDeferred<AsyncResult<Any?>>()
firebaseAuth.signInAnonymously()
.addOnSuccessListener {
firebaseAuthWrapper.signInAnonymously(
onSuccess = {
deferredResult.complete(AsyncResult.Success(null))
},
onFailure = { exception ->
deferredResult.complete(AsyncResult.Failure(exception))
}
.addOnFailureListener {
deferredResult.complete(AsyncResult.Failure(it))
}
)

return deferredResult
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package org.oppia.android.domain.auth

import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import dagger.Module
import dagger.Provides
import javax.inject.Singleton

/** Provides an implementation of [AuthenticationWrapper]. */
/** Provides an implementation of [FirebaseAuthWrapper]. */
@Module
class AuthenticationModule {
@Provides
@Singleton
fun provideAuthenticationController():
AuthenticationWrapper = AuthenticationController(Firebase.auth)
fun provideFirebaseAuthWrapper(firebaseAuthInstanceWrapperImpl: FirebaseAuthInstanceWrapperImpl):
FirebaseAuthWrapper = FirebaseAuthWrapperImpl(firebaseAuthInstanceWrapperImpl)
}

This file was deleted.

44 changes: 39 additions & 5 deletions domain/src/main/java/org/oppia/android/domain/auth/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library")

kt_android_library(
name = "authentication_controller",
srcs = ["AuthenticationController.kt"],
srcs = [
"AuthenticationController.kt",
],
visibility = ["//:oppia_api_visibility"],
deps = [
":authentication_listener",
":firebase_auth_wrapper",
"//third_party:javax_inject_javax_inject",
],
)

kt_android_library(
name = "authentication_listener",
srcs = ["AuthenticationWrapper.kt"],
name = "firebase_auth_wrapper",
srcs = [
"FirebaseAuthInstance.kt",
"FirebaseAuthInstanceWrapper.kt",
"FirebaseAuthWrapper.kt",
"FirebaseUserWrapper.kt",
],
visibility = ["//:oppia_api_visibility"],
deps = [
"//third_party:com_google_firebase_firebase-auth-ktx",
Expand All @@ -28,11 +35,38 @@ kt_android_library(

kt_android_library(
name = "auth_module",
srcs = ["AuthenticationModule.kt"],
srcs = [
"AuthenticationModule.kt",
],
visibility = ["//:oppia_prod_module_visibility"],
deps = [
":authentication_controller",
":dagger",
":firebase_auth_wrapper_impl",
],
)

kt_android_library(
name = "firebase_auth_wrapper_impl",
srcs = [
"FirebaseAuthWrapperImpl.kt",
],
visibility = ["//:oppia_prod_module_visibility"],
deps = [
":dagger",
":firebase_auth_instance_wrapper_impl",
],
)

kt_android_library(
name = "firebase_auth_instance_wrapper_impl",
srcs = [
"FirebaseAuthInstanceWrapperImpl.kt",
],
visibility = ["//:oppia_prod_module_visibility"],
deps = [
":dagger",
":firebase_auth_wrapper",
],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.oppia.android.domain.auth

import com.google.firebase.auth.FirebaseAuth

/** Wrapper for [FirebaseAuth], used to pass an instance of [FirebaseAuth]. */
data class FirebaseAuthInstance(
val firebaseAuth: FirebaseAuth
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.oppia.android.domain.auth

/** Interface for providing an implementation of [FirebaseAuthInstance]. */
interface FirebaseAuthInstanceWrapper {
/** Returns a wrapped instance of FirebaseAuth. */
val firebaseAuthInstance: FirebaseAuthInstance
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.oppia.android.domain.auth

import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import javax.inject.Inject

/** Implementation of [FirebaseAuthInstanceWrapper]. */
class FirebaseAuthInstanceWrapperImpl @Inject constructor() : FirebaseAuthInstanceWrapper {
override val firebaseAuthInstance: FirebaseAuthInstance
get() = FirebaseAuthInstance(Firebase.auth)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.oppia.android.domain.auth

/** Wrapper for FirebaseAuth. */
interface FirebaseAuthWrapper {
/** Returns the current signed in user or null if there is no authenticated user. */
val currentUser: FirebaseUserWrapper?

/** Returns the authentication result. */
fun signInAnonymously(onSuccess: () -> Unit, onFailure: (Throwable) -> Unit)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.oppia.android.domain.auth

import javax.inject.Inject
import javax.inject.Singleton

/** Production implementation of FirebaseAuthWrapper. */
@Singleton
class FirebaseAuthWrapperImpl @Inject constructor(
private val firebaseWrapper: FirebaseAuthInstanceWrapperImpl
) : FirebaseAuthWrapper {
override val currentUser: FirebaseUserWrapper?
get() = firebaseWrapper.firebaseAuthInstance.firebaseAuth.currentUser?.let {
FirebaseUserWrapper(it.uid)
}

override fun signInAnonymously(onSuccess: () -> Unit, onFailure: (Throwable) -> Unit) {
firebaseWrapper.firebaseAuthInstance.firebaseAuth.signInAnonymously()
.addOnSuccessListener {
onSuccess.invoke()
}
.addOnFailureListener { task ->
val exception = task.cause
if (exception != null) {
onFailure.invoke(exception)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.oppia.android.domain.auth

import com.google.firebase.auth.FirebaseUser

/** Wrapper for [FirebaseUser]. */
data class FirebaseUserWrapper(
val uid: String,
)
Loading
Loading