Skip to content

Commit

Permalink
Fix #3528: Setup Infrastructure to test using local dev server (#3529)
Browse files Browse the repository at this point in the history
* Introducing UiAutomator

* Using 23 api level

* some local tweaks

* Updated implementation based on adb commands

* Nit fixes

* Nit fixes

* Requested changes

* fix class name

* update dependencies

* init OppiaTestApplication.kt and TestApplicationComponent.kt

* updated manifest

* Nit fixes

* Nit fixes

* Using seperate Manifest for Test Application and instrumentation tests

* bazel lint check

* Nit fix

* Requested changes

* updated maven_install

* Nit fix

* Add CODEOWNERS

* Added KDoc

* NIT

* Added comments

* Added oppia_instrumentation_test.bzl

* added exempted_file_path

* changed to EndToEndTest modules

* added exempted_file_paths

* addded usesClearTextTraffix to reference the localhost

* Helper utility shifted to testing module

* NIT

* NIT

* deleted the previous utility

* ignoring instrumentation in ComputeAffectedTests.kt

* NIT

* NIT

* NIT

* changed startsWith("instrumentation/")

* filtered bazel targets

* NIT

* NIT

* filtering the instrumentation from the total affected targets

* NIT

* changed directory to javatests

* NIT

* NIT

* fixed scripts

* NIT

* Updated and NIT

* Added tests for modules

* updated testfile exemptions

* updated the ComputedAffectedTests

* ignoring player directory

* Created NetworkConfigModule.kt

* Fixed CI failures

* NIT

* Requested changes

* NIT

* NIT

* NIT

* Added kdoc

* NIT

* updated tests

* NIT

* updated computedAffectedTEsts

* fix OppiaTestApplication

* updated failing tests

* updated ComputeAffectedTestsTest

* added TestBazelWorkspace tests

* fixed tests

* updated test_manifest

* fixed confict test
  • Loading branch information
FareesHussain authored Aug 19, 2021
1 parent d219920 commit 000bbc4
Show file tree
Hide file tree
Showing 52 changed files with 1,211 additions and 107 deletions.
7 changes: 7 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ package_group(
],
)

package_group(
name = "oppia_e2e_testing_visibility",
packages = [
"//instrumentation/...",
],
)

# Special visibility group specific to debug mode. This provides access to the target for both the
# Oppia tests and specific debug only packages.
package_group(
Expand Down
1 change: 1 addition & 0 deletions app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ kt_android_library(
":resources",
":view_models",
":views",
"//data/src/main/java/org/oppia/android/data/backends/gae:network_config_prod_module",
"//data/src/main/java/org/oppia/android/data/backends/gae:prod_module",
"//model:arguments_java_proto_lite",
"//third_party:androidx_databinding_databinding-adapters",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.oppia.android.app.devoptions.DeveloperOptionsStarterModule
import org.oppia.android.app.shim.IntentFactoryShimModule
import org.oppia.android.app.shim.ViewBindingShimModule
import org.oppia.android.app.topic.PracticeTabModule
import org.oppia.android.data.backends.gae.NetworkConfigProdModule
import org.oppia.android.data.backends.gae.NetworkModule
import org.oppia.android.domain.classify.InteractionsModule
import org.oppia.android.domain.classify.rules.continueinteraction.ContinueModule
Expand Down Expand Up @@ -81,7 +82,7 @@ import javax.inject.Singleton
FirebaseLogUploaderModule::class, NetworkModule::class, PracticeTabModule::class,
PlatformParameterModule::class, ExplorationStorageModule::class,
DeveloperOptionsStarterModule::class, DeveloperOptionsModule::class,
NetworkConnectionUtilDebugModule::class,
NetworkConnectionUtilDebugModule::class, NetworkConfigProdModule::class,
// TODO(#59): Remove this module once we completely migrate to Bazel from Gradle as we can then
// directly exclude debug files from the build and thus won't be requiring this module.
NetworkConnectionDebugUtilModule::class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.oppia.android.app.devoptions.DeveloperOptionsModule
import org.oppia.android.app.devoptions.DeveloperOptionsStarterModule
import org.oppia.android.app.shim.ViewBindingShimModule
import org.oppia.android.app.topic.PracticeTabModule
import org.oppia.android.data.backends.gae.NetworkConfigProdModule
import org.oppia.android.data.backends.gae.NetworkModule
import org.oppia.android.domain.classify.InteractionsModule
import org.oppia.android.domain.classify.rules.continueinteraction.ContinueModule
Expand Down Expand Up @@ -131,8 +132,8 @@ class HelpActivityTest {
HintsAndSolutionConfigModule::class, HintsAndSolutionProdModule::class,
FirebaseLogUploaderModule::class, FakeOppiaClockModule::class, PracticeTabModule::class,
DeveloperOptionsStarterModule::class, DeveloperOptionsModule::class,
ExplorationStorageModule::class, NetworkConnectionUtilDebugModule::class,
NetworkConnectionDebugUtilModule::class
ExplorationStorageModule::class, NetworkConfigProdModule::class,
NetworkConnectionUtilDebugModule::class, NetworkConnectionDebugUtilModule::class
]
)
interface TestApplicationComponent : ApplicationComponent {
Expand Down
1 change: 1 addition & 0 deletions data/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ load("//data:data_test.bzl", "data_test")
# keep sorted
TEST_DEPS = [
":dagger",
"//data/src/main/java/org/oppia/android/data/backends/gae:network_config_prod_module",
"//data/src/main/java/org/oppia/android/data/backends/gae:oppia_retrofit",
"//data/src/main/java/org/oppia/android/data/backends/gae:prod_module",
"//data/src/main/java/org/oppia/android/data/backends/gae/model",
Expand Down
30 changes: 23 additions & 7 deletions data/src/main/java/org/oppia/android/data/backends/gae/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ kt_android_library(
visibility = ["//:oppia_testing_visibility"],
deps = [
":constants",
":network_settings",
":network_config_annotations",
"//model:arguments_java_proto_lite",
"//third_party:com_squareup_okhttp3_okhttp",
"//third_party:javax_inject_javax_inject",
Expand All @@ -25,11 +25,15 @@ kt_android_library(
)

kt_android_library(
name = "network_settings",
name = "network_config_prod_module",
srcs = [
"NetworkSettings.kt",
"NetworkConfigProdModule.kt",
],
visibility = ["//:oppia_prod_module_visibility"],
deps = [
":dagger",
":network_config_annotations",
],
visibility = ["//:oppia_testing_visibility"],
)

kt_android_library(
Expand All @@ -39,14 +43,26 @@ kt_android_library(
],
)

kt_android_library(
name = "network_config_annotations",
srcs = [
"BaseUrl.kt",
"XssiPrefix.kt",
],
visibility = ["//:oppia_api_visibility"],
deps = [
"//third_party:javax_inject_javax_inject",
],
)

kt_android_library(
name = "oppia_retrofit",
srcs = [
"OppiaRetrofit.kt",
],
visibility = ["//:oppia_testing_visibility"],
visibility = ["//:oppia_api_visibility"],
deps = [
":dagger",
"//third_party:javax_inject_javax_inject",
],
)

Expand All @@ -58,8 +74,8 @@ kt_android_library(
visibility = ["//:oppia_prod_module_visibility"],
deps = [
":dagger",
":network_config_annotations",
":network_interceptors",
":network_settings",
":oppia_retrofit",
"//data/src/main/java/org/oppia/android/data/backends/gae/api",
"//third_party:com_squareup_retrofit2_converter-moshi",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.oppia.android.data.backends.gae

import javax.inject.Qualifier

/** Qualifier for an application-injectable string representing the server URL used for networking. */
@Qualifier
annotation class BaseUrl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import javax.inject.Singleton
* The Interceptor removes XSSI_PREFIX from every response to produce valid Json.
*/
@Singleton
class JsonPrefixNetworkInterceptor @Inject constructor() : Interceptor {
class JsonPrefixNetworkInterceptor @Inject constructor(
@XssiPrefix private val xssiPrefix: String
) : Interceptor {

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
Expand All @@ -34,6 +36,6 @@ class JsonPrefixNetworkInterceptor @Inject constructor() : Interceptor {

/** Removes the XSSI prefix from the specified raw JSON & returns the result. */
fun removeXssiPrefix(rawJson: String): String {
return rawJson.removePrefix(NetworkSettings.XSSI_PREFIX).trimStart()
return rawJson.removePrefix(xssiPrefix).trimStart()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.oppia.android.data.backends.gae

import dagger.Module
import dagger.Provides

/** Provides network-specific constants. */
@Module
class NetworkConfigProdModule {

/** Provides BaseUrl that connects to production server. */
@Provides
@BaseUrl
fun provideNetworkBaseUrl(): String {
return "https://oppia.org"
}

/**
* Prefix in Json response for extra layer of security in API calls
* https://github.com/oppia/oppia/blob/8f9eed/feconf.py#L319
* Remove this prefix from every Json response which is achieved in [NetworkInterceptor]
*/
@Provides
@XssiPrefix
fun provideXssiPrefix(): String {
return ")]}'"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ class NetworkModule {
@Singleton
fun provideRetrofitInstance(
jsonPrefixNetworkInterceptor: JsonPrefixNetworkInterceptor,
remoteAuthNetworkInterceptor: RemoteAuthNetworkInterceptor
remoteAuthNetworkInterceptor: RemoteAuthNetworkInterceptor,
@BaseUrl baseUrl: String
): Retrofit {
val client = OkHttpClient.Builder()
.addInterceptor(jsonPrefixNetworkInterceptor)
.addInterceptor(remoteAuthNetworkInterceptor)
.build()

return Retrofit.Builder()
.baseUrl(NetworkSettings.getBaseUrl())
.baseUrl(baseUrl)
.addConverterFactory(MoshiConverterFactory.create())
.client(client)
.build()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.oppia.android.data.backends.gae

import javax.inject.Qualifier

/**
* Qualifier for an application-injectable string representing the prefix in Json response used for
* security in API calls.
*/
@Qualifier
annotation class XssiPrefix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import retrofit2.Retrofit
import javax.inject.Inject
import javax.inject.Singleton

/** Tests for [JsonPrefixNetworkInterceptorTest] */
/** Tests for [JsonPrefixNetworkInterceptor]. */
@RunWith(AndroidJUnit4::class)
@LooperMode(LooperMode.Mode.PAUSED)
class JsonPrefixNetworkInterceptorTest {
Expand Down Expand Up @@ -95,7 +95,7 @@ class JsonPrefixNetworkInterceptorTest {

// TODO(#89): Move this to a common test application component.
@Singleton
@Component(modules = [NetworkModule::class])
@Component(modules = [TestNetworkModule::class, NetworkConfigProdModule::class])
interface TestApplicationComponent {
@Component.Builder
interface Builder {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.oppia.android.data.backends.gae

import android.app.Application
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import dagger.BindsInstance
import dagger.Component
import dagger.Module
import dagger.Provides
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import javax.inject.Inject
import javax.inject.Singleton

/** Tests for [NetworkConfigProdModule]. */
@RunWith(AndroidJUnit4::class)
@LooperMode(LooperMode.Mode.PAUSED)
@Config(application = NetworkConfigProdModuleTest.TestApplication::class)
class NetworkConfigProdModuleTest {

@field:[Inject BaseUrl]
lateinit var baseUrl: String

@field:[Inject XssiPrefix]
lateinit var xssiPrefix: String

@Before
fun setUp() {
setUpTestApplicationComponent()
}

@Test
fun testModule_baseUrl_isProdUrl() {
assertThat(baseUrl).isEqualTo("https://oppia.org")
}

@Test
fun testModule_xssiPrefix_isXssiPrefix() {
assertThat(xssiPrefix).isEqualTo(")]}'")
}

private fun setUpTestApplicationComponent() {
ApplicationProvider.getApplicationContext<TestApplication>()
.inject(this)
}

@Module
class TestModule {
@Provides
@Singleton
fun provideContext(application: Application): Context {
return application
}
}

@Singleton
@Component(
modules = [
TestModule::class, NetworkConfigProdModule::class
]
)

interface TestApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun setApplication(application: Application): Builder
fun build(): TestApplicationComponent
}

fun inject(networkConfigModuleTest: NetworkConfigProdModuleTest)
}

class TestApplication : Application() {
private val component: TestApplicationComponent by lazy {
DaggerNetworkConfigProdModuleTest_TestApplicationComponent.builder()
.setApplication(this)
.build()
}

fun inject(networkConfigModuleTest: NetworkConfigProdModuleTest) {
component.inject(networkConfigModuleTest)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import retrofit2.converter.moshi.MoshiConverterFactory
import javax.inject.Inject
import javax.inject.Singleton

/** Tests for [RemoteAuthNetworkInterceptor] */
/** Tests for [RemoteAuthNetworkInterceptor]. */
@RunWith(AndroidJUnit4::class)
@Config(application = RemoteAuthNetworkInterceptorTest.TestApplication::class)
@LooperMode(LooperMode.Mode.PAUSED)
Expand Down
Loading

0 comments on commit 000bbc4

Please sign in to comment.