diff --git a/CHANGELOG.md b/CHANGELOG.md index 92524284db..f6383607ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,9 +25,10 @@ * Minimum R8: 8.0.34. ### Internal -- None. +- Reworked test app initializer framework. + -## 2.0.1 (YYYY-MM-DD) +## 2.0.1-SNAPSHOT (YYYY-MM-DD) ### Breaking changes * None. diff --git a/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/JsonStyleRealmObject.kt b/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/JsonStyleRealmObject.kt index 41f1afce62..bb8eb8a34a 100644 --- a/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/JsonStyleRealmObject.kt +++ b/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/JsonStyleRealmObject.kt @@ -23,9 +23,10 @@ import io.realm.kotlin.types.annotations.PrimaryKey import org.mongodb.kbson.ObjectId class JsonStyleRealmObject : RealmObject { + // Act as partition key to separate individual test runs + var selector: String = "DEFAULT" @PrimaryKey @PersistedName("_id") - var id: String = ObjectId().toHexString() - var selector: String = "DEFAULT" + var id: ObjectId = ObjectId() var value: RealmAny? = null } diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/RealmAnyNestedCollectionTests.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/RealmAnyNestedCollectionTests.kt index e13edb5c8f..f3a6fb0f7a 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/RealmAnyNestedCollectionTests.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/RealmAnyNestedCollectionTests.kt @@ -30,6 +30,7 @@ import io.realm.kotlin.internal.platform.runBlocking import io.realm.kotlin.test.common.utils.assertFailsWithMessage import io.realm.kotlin.test.platform.PlatformUtils import io.realm.kotlin.types.RealmAny +import org.mongodb.kbson.ObjectId import kotlin.test.AfterTest import kotlin.test.BeforeTest import kotlin.test.Test @@ -527,26 +528,26 @@ class RealmAnyNestedCollectionTests { @Test fun query() = runBlocking { + var listId: ObjectId? = null + var dictId: ObjectId? = null + var embeddedId: ObjectId? = null realm.write { - copyToRealm( + listId = copyToRealm( JsonStyleRealmObject().apply { - id = "LIST" value = realmAnyListOf(4, 5, 6) } - ) - copyToRealm( + ).id + dictId = copyToRealm( JsonStyleRealmObject().apply { - id = "DICT" value = realmAnyDictionaryOf( "key1" to 7, "key2" to 8, "key3" to 9, ) } - ) - copyToRealm( + ).id + embeddedId = copyToRealm( JsonStyleRealmObject().apply { - id = "EMBEDDED" value = realmAnyListOf( listOf(4, 5, 6), mapOf( @@ -556,35 +557,35 @@ class RealmAnyNestedCollectionTests { ) ) } - ) + ).id } assertEquals(3, realm.query().find().size) // Matching lists realm.query("value[0] == 4").find().single().run { - assertEquals("LIST", id) + assertEquals(listId, id) } realm.query("value[*] == 4").find().single().run { - assertEquals("LIST", id) + assertEquals(listId, id) } realm.query("value[*] == {4, 5, 6}").find().single().run { - assertEquals("LIST", id) + assertEquals(listId, id) } // Matching dictionaries realm.query("value.key1 == 7").find().single().run { - assertEquals("DICT", id) + assertEquals(dictId, id) } realm.query("value['key1'] == 7").find().single().run { - assertEquals("DICT", id) + assertEquals(dictId, id) } realm.query("value[*] == 7").find().single().run { - assertEquals("DICT", id) + assertEquals(dictId, id) } assertEquals(0, realm.query("value.unknown == 3").find().size) realm.query("value.@keys == 'key1'").find().single().run { - assertEquals("DICT", id) + assertEquals(dictId, id) } assertEquals(0, realm.query("value.@keys == 'unknown'").find().size) @@ -593,19 +594,19 @@ class RealmAnyNestedCollectionTests { // Matching across all elements and in nested structures realm.query("value[*][*] == 4").find().single().run { - assertEquals("EMBEDDED", id) + assertEquals(embeddedId, id) } realm.query("value[*][*] == 7").find().single().run { - assertEquals("EMBEDDED", id) + assertEquals(embeddedId, id) } realm.query("value[*].@keys == 'key1'").find().single().run { - assertEquals("EMBEDDED", id) + assertEquals(embeddedId, id) } realm.query("value[*].key3[0] == 9").find().single().run { - assertEquals("EMBEDDED", id) + assertEquals(embeddedId, id) } realm.query("value[0][*] == {4, 5, 6}").find().single().run { - assertEquals("EMBEDDED", id) + assertEquals(embeddedId, id) } // FIXME Core issue https://github.com/realm/realm-core/issues/7393 // realm.query("value[*][*] == {4, 5, 6}").find().single().run { diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedCollectionNotificationTest.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedCollectionNotificationTest.kt index 9d1c69dc25..bc7e30da2d 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedCollectionNotificationTest.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedCollectionNotificationTest.kt @@ -71,7 +71,6 @@ class RealmAnyNestedCollectionNotificationTest { val o: JsonStyleRealmObject = realm.write { copyToRealm( JsonStyleRealmObject().apply { - id = "SET" value = realmAnyListOf(realmAnyListOf(1, 2, 3)) } ) diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedDictionaryNotificationTest.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedDictionaryNotificationTest.kt index 0b25592dd4..baf7aee88a 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedDictionaryNotificationTest.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedDictionaryNotificationTest.kt @@ -84,7 +84,6 @@ class RealmAnyNestedDictionaryNotificationTest : FlowableTests, DeletableEntityN val o: JsonStyleRealmObject = realm.write { copyToRealm( JsonStyleRealmObject().apply { - id = "DICTIONARY" value = realmAnyDictionaryOf( "root" to realmAnyDictionaryOf( "key1" to 1, diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedListNotificationTest.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedListNotificationTest.kt index fab126948e..e3a263419e 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedListNotificationTest.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/notifications/RealmAnyNestedListNotificationTest.kt @@ -84,7 +84,6 @@ class RealmAnyNestedListNotificationTest : FlowableTests, DeletableEntityNotific val o: JsonStyleRealmObject = realm.write { copyToRealm( JsonStyleRealmObject().apply { - id = "LIST" value = realmAnyListOf(realmAnyListOf(1, 2, 3)) } ) @@ -281,7 +280,7 @@ class RealmAnyNestedListNotificationTest : FlowableTests, DeletableEntityNotific val asList = findLatest(parent)!!.value!!.asList() println(asList.size) asList.add( - RealmAny.create(JsonStyleRealmObject().apply { id = "CHILD" }) + RealmAny.create(JsonStyleRealmObject()) ) } channel.receiveOrFail(message = "List add").let { diff --git a/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/TestApp.kt b/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/TestApp.kt index ab221414d6..079ab3b377 100644 --- a/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/TestApp.kt +++ b/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/TestApp.kt @@ -32,10 +32,8 @@ import io.realm.kotlin.mongodb.User import io.realm.kotlin.mongodb.internal.AppConfigurationImpl import io.realm.kotlin.test.mongodb.util.AppAdmin import io.realm.kotlin.test.mongodb.util.AppAdminImpl +import io.realm.kotlin.test.mongodb.util.AppInitializer import io.realm.kotlin.test.mongodb.util.AppServicesClient -import io.realm.kotlin.test.mongodb.util.BaasApp -import io.realm.kotlin.test.mongodb.util.Service -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.initializeDefault import io.realm.kotlin.test.platform.PlatformUtils import io.realm.kotlin.test.util.TestHelper import kotlinx.coroutines.CloseableCoroutineDispatcher @@ -91,27 +89,23 @@ open class TestApp private constructor( @OptIn(ExperimentalKBsonSerializerApi::class, ExperimentalCoroutinesApi::class) constructor( testId: String?, - appName: String = TEST_APP_PARTITION, + appInitializer: AppInitializer, dispatcher: CoroutineDispatcher = singleThreadDispatcher("$testId-dispatcher"), builder: (AppConfiguration.Builder) -> AppConfiguration.Builder = { - it.syncRootDirectory(PlatformUtils.createTempDir("$appName-$testId")) + it.syncRootDirectory(PlatformUtils.createTempDir("${appInitializer.name}-$testId")) }, debug: Boolean = false, networkTransport: NetworkTransport? = null, ejson: EJson = EJson, - initialSetup: suspend AppServicesClient.(app: BaasApp, service: Service) -> Unit = { app: BaasApp, service: Service -> - initializeDefault(app, service) - } ) : this( dispatcher, build( debug = debug, - appName = appName, + appInitializer = appInitializer, dispatcher = dispatcher, builder = builder, networkTransport = networkTransport, ejson = ejson, - initialSetup = initialSetup ) ) @@ -172,12 +166,11 @@ open class TestApp private constructor( @Suppress("LongParameterList") fun build( debug: Boolean, - appName: String, + appInitializer: AppInitializer, dispatcher: CoroutineDispatcher, builder: (AppConfiguration.Builder) -> AppConfiguration.Builder, networkTransport: NetworkTransport?, ejson: EJson, - initialSetup: suspend AppServicesClient.(app: BaasApp, service: Service) -> Unit ): Pair { val appAdmin: AppAdmin = runBlocking(dispatcher) { AppServicesClient.build( @@ -185,7 +178,7 @@ open class TestApp private constructor( debug = debug, dispatcher = dispatcher ).run { - val baasApp = getOrCreateApp(appName, initialSetup) + val baasApp = getOrCreateApp(appInitializer) AppAdminImpl(this, baasApp) } diff --git a/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/util/AppServicesClient.kt b/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/util/AppServicesClient.kt index eb6e02c732..8f151ab5a9 100644 --- a/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/util/AppServicesClient.kt +++ b/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/util/AppServicesClient.kt @@ -42,7 +42,6 @@ import io.realm.kotlin.mongodb.sync.SyncMode import io.realm.kotlin.schema.RealmClassKind import io.realm.kotlin.test.mongodb.SyncServerConfig import io.realm.kotlin.test.mongodb.TEST_APP_CLUSTER_NAME -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.initialize import io.realm.kotlin.types.BaseRealmObject import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.delay @@ -77,6 +76,7 @@ import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.seconds private const val ADMIN_PATH = "/api/admin/v3.0" +private const val PRIVATE_PATH = "/api/private/v1.0" data class SyncPermissions( val read: Boolean, @@ -327,8 +327,17 @@ data class BaasApp( @SerialName("client_app_id") val clientAppId: String, val name: String, @SerialName("domain_id") val domainId: String, - @SerialName("group_id") val groupId: String -) + @SerialName("group_id") val groupId: String, + + @Transient private val _client: AppServicesClient? = null +) { + val client: AppServicesClient + get() = _client ?: TODO("App should be copy'ed with _client set to the AppServicesClient that retrieved it") + val url: String + get() = client.baseUrl + ADMIN_PATH + "/groups/${this.groupId}/apps/${this._id}" + val privateUrl: String + get() = client.baseUrl + PRIVATE_PATH + "/groups/${this.groupId}/apps/${this._id}" +} /** * Client to interact with App Services Server. It allows to create Applications and tweak their @@ -336,147 +345,34 @@ data class BaasApp( */ class AppServicesClient( val baseUrl: String, - private val groupUrl: String, - private val httpClient: HttpClient, + private val groupId: String, + internal val httpClient: HttpClient, val dispatcher: CoroutineDispatcher, ) { - companion object { - // Default serializer fails with - // InvalidMutabilityException: mutation attempt of frozen kotlin.collections.HashMap - // on native. Have tried the various workarounds from - // https://github.com/Kotlin/kotlinx.serialization/issues/1450 - // but only one that works is manual invoking the deserializer - @OptIn(InternalSerializationApi::class) - private suspend inline fun HttpClient.typedRequest( - method: HttpMethod, - url: String, - crossinline block: HttpRequestBuilder.() -> Unit = {} - ): T { - val response: HttpResponse = this@typedRequest.request(url) { - this.method = method - this.apply(block) - } - if (!response.status.isSuccess()) { - throw IllegalStateException("Http request failed: $url. ${response.status}: ${response.bodyAsText()}") - } - return response.bodyAsText() - .let { - Json { ignoreUnknownKeys = true }.decodeFromString( - T::class.serializer(), - it - ) - } - } - - // Default serializer fails with - // InvalidMutabilityException: mutation attempt of frozen kotlin.collections.HashMap - // on native. Have tried the various workarounds from - // https://github.com/Kotlin/kotlinx.serialization/issues/1450 - // but only one that works is manual invoking the deserializer - @OptIn(InternalSerializationApi::class) - private suspend inline fun HttpClient.typedListRequest( - method: HttpMethod, - url: String, - crossinline block: HttpRequestBuilder.() -> Unit = {} - ): List { - return this@typedListRequest.request(url) { - this.method = method - this.apply(block) - }.bodyAsText() - .let { - Json { ignoreUnknownKeys = true }.decodeFromString( - ListSerializer(T::class.serializer()), - it - ) - } - } - - suspend fun build( - debug: Boolean, - baseUrl: String, - dispatcher: CoroutineDispatcher, - ): AppServicesClient { - val adminUrl = baseUrl + ADMIN_PATH - // Work around issues on Native with the Ktor client being created and used - // on different threads. - // Log in using unauthorized client - val unauthorizedClient = defaultClient("realm-baas-unauthorized", debug) - - var loginMethod: String = "local-userpass" - var json: Map = mapOf("username" to SyncServerConfig.email, "password" to SyncServerConfig.password) - if (SyncServerConfig.publicApiKey.isNotEmpty()) { - loginMethod = "mongodb-cloud" - json = mapOf("username" to SyncServerConfig.publicApiKey, "apiKey" to SyncServerConfig.privateApiKey) - } - val loginResponse = unauthorizedClient.typedRequest( - HttpMethod.Post, - "$adminUrl/auth/providers/$loginMethod/login" - ) { - contentType(ContentType.Application.Json) - setBody(json) - } - - // Setup authorized client for the rest of the requests - val accessToken = loginResponse.access_token - unauthorizedClient.close() - val httpClient = defaultClient("realm-baas-authorized", debug) { - expectSuccess = true - - defaultRequest { - headers { - append("Authorization", "Bearer $accessToken") - } - } - install(ContentNegotiation) { - json( - Json { - prettyPrint = true - isLenient = true - } - ) - } - install(Logging) { - // Set to LogLevel.ALL to debug Admin API requests. All relevant - // data for each request/response will be console or LogCat. - level = LogLevel.INFO - } - } - - // Collect app group id - val groupId = httpClient.typedRequest(Get, "$adminUrl/auth/profile") - .roles - .first() - .group_id - - return AppServicesClient( - baseUrl, - "$adminUrl/groups/$groupId", - httpClient, - dispatcher - ) - } - } + val groupUrl: String + get() = baseUrl + ADMIN_PATH + "/groups/$groupId" fun closeClient() { httpClient.close() } - suspend fun getOrCreateApp( - appName: String, - initializer: suspend AppServicesClient.(app: BaasApp, service: Service) -> Unit - ): BaasApp = - getApp(appName) ?: createApp(appName) { - initialize(this, initializer) + suspend fun getOrCreateApp(appInitializer: AppInitializer): BaasApp { + val app = getApp(appInitializer.name) + return app ?: createApp(appInitializer.name) { + appInitializer.initialize(this@AppServicesClient, this) } + } - private suspend fun getApp(appName: String): BaasApp? = - withContext(dispatcher) { + private suspend fun getApp(appName: String): BaasApp? { + val withContext = withContext(dispatcher) { httpClient.typedListRequest(Get, "$groupUrl/apps") .firstOrNull { it.name == appName - } + }?.copy(_client = this@AppServicesClient) } + return withContext + } private suspend fun createApp( appName: String, @@ -489,7 +385,7 @@ class AppServicesClient( httpClient.typedRequest(Post, "$groupUrl/apps") { setBody(Json.parseToJsonElement("""{"name": $appName}""")) contentType(ContentType.Application.Json) - }.apply { + }.copy(_client = this@AppServicesClient).apply { initializer(this) } } @@ -536,6 +432,17 @@ class AppServicesClient( val BaasApp.url: String get() = "$groupUrl/apps/${this._id}" + suspend fun BaasApp.toggleFeatures(features: Set, enable: Boolean) { + withContext(dispatcher) { + httpClient.typedRequest( + Post, + "$privateUrl/features" + ) { + setBody(Json.parseToJsonElement("""{ "action": "${if (enable) "enable" else "disable"}", "feature_flags": [ ${features.joinToString { "\"$it\"" }} ] }""")) + contentType(ContentType.Application.Json) + } + } + } suspend fun BaasApp.addFunction(function: Function): Function = withContext(dispatcher) { @@ -732,7 +639,7 @@ class AppServicesClient( .first { val type = if (TEST_APP_CLUSTER_NAME.isEmpty()) "mongodb" else "mongodb-atlas" it.type == type - } + }.copy(app = this@mongodbService) } } @@ -965,4 +872,121 @@ class AppServicesClient( } } } + companion object { + suspend fun build( + debug: Boolean, + baseUrl: String, + dispatcher: CoroutineDispatcher, + ): AppServicesClient { + val adminUrl = baseUrl + ADMIN_PATH + // Work around issues on Native with the Ktor client being created and used + // on different threads. + // Log in using unauthorized client + val unauthorizedClient = defaultClient("realm-baas-unauthorized", debug) + + var loginMethod: String = "local-userpass" + var json: Map = mapOf("username" to SyncServerConfig.email, "password" to SyncServerConfig.password) + if (SyncServerConfig.publicApiKey.isNotEmpty()) { + loginMethod = "mongodb-cloud" + json = mapOf("username" to SyncServerConfig.publicApiKey, "apiKey" to SyncServerConfig.privateApiKey) + } + val loginResponse = unauthorizedClient.typedRequest( + HttpMethod.Post, + "$adminUrl/auth/providers/$loginMethod/login" + ) { + contentType(ContentType.Application.Json) + setBody(json) + } + + // Setup authorized client for the rest of the requests + val accessToken = loginResponse.access_token + unauthorizedClient.close() + + val httpClient = defaultClient("realm-baas-authorized", debug) { + expectSuccess = true + + defaultRequest { + headers { + append("Authorization", "Bearer $accessToken") + } + } + install(ContentNegotiation) { + json( + Json { + prettyPrint = true + isLenient = true + } + ) + } + install(Logging) { + // Set to LogLevel.ALL to debug Admin API requests. All relevant + // data for each request/response will be console or LogCat. + level = LogLevel.ALL + } + } + + // Collect app group id + val groupId = httpClient.typedRequest(Get, "$adminUrl/auth/profile") + .roles + .first() + .group_id ?: "null" + + return AppServicesClient( + baseUrl, + groupId, + httpClient, + dispatcher + ) + } + } +} + +// Default serializer fails with +// InvalidMutabilityException: mutation attempt of frozen kotlin.collections.HashMap +// on native. Have tried the various workarounds from +// https://github.com/Kotlin/kotlinx.serialization/issues/1450 +// but only one that works is manual invoking the deserializer +@OptIn(InternalSerializationApi::class) +private suspend inline fun HttpClient.typedRequest( + method: HttpMethod, + url: String, + crossinline block: HttpRequestBuilder.() -> Unit = {} +): T { + val response: HttpResponse = this@typedRequest.request(url) { + this.method = method + this.apply(block) + } + if (!response.status.isSuccess()) { + throw IllegalStateException("Http request failed: $url. ${response.status}: ${response.bodyAsText()}") + } + return response.bodyAsText() + .let { + Json { ignoreUnknownKeys = true }.decodeFromString( + T::class.serializer(), + it + ) + } +} + +// Default serializer fails with +// InvalidMutabilityException: mutation attempt of frozen kotlin.collections.HashMap +// on native. Have tried the various workarounds from +// https://github.com/Kotlin/kotlinx.serialization/issues/1450 +// but only one that works is manual invoking the deserializer +@OptIn(InternalSerializationApi::class) +private suspend inline fun HttpClient.typedListRequest( + method: HttpMethod, + url: String, + crossinline block: HttpRequestBuilder.() -> Unit = {} +): List { + return this@typedListRequest.request(url) { + this.method = method + this.apply(block) + }.bodyAsText() + .let { + Json { ignoreUnknownKeys = true }.decodeFromString( + ListSerializer(T::class.serializer()), + it + ) + } } diff --git a/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/util/KtorTestAppInitializer.kt b/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/util/KtorTestAppInitializer.kt deleted file mode 100644 index 7abcb1333a..0000000000 --- a/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/util/KtorTestAppInitializer.kt +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2022 Realm Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.realm.kotlin.test.mongodb.util - -import io.ktor.http.HttpMethod - -object KtorTestAppInitializer { - - // Setups the app with the functions and https endpoints required to run the KtorNetworkTransportTests - suspend fun AppServicesClient.initialize(app: BaasApp, methods: List) = - with(app) { - // We have to create a function per method because the request parameter does not - // has what method triggered it. - methods.forEach { httpMethod: HttpMethod -> - val method = httpMethod.value - val function = addFunction( - Function( - name = "test_network_transport_$method", - runAsSystem = true, - source = - """ - exports = async function (request, response) { - response.setHeader('Content-Type', 'text/plain'); - let isSuccess = request.query["success"] == "true"; - - if (isSuccess) { - response.setStatusCode(200); - response.setBody("$method-success"); - } else { - response.setStatusCode(500); - response.setBody("$method-failure"); - } - } - """.trimIndent() - ) - ) - - addEndpoint( - """ - { - "route": "/test_network_transport", - "function_name": "${function.name}", - "function_id": "${function._id}", - "http_method": "$method", - "validation_method": "NO_VALIDATION", - "secret_id": "", - "secret_name": "", - "create_user_on_auth": false, - "fetch_custom_user_data": false, - "respond_result": false, - "disabled": false, - "return_type": "JSON" - } - """.trimIndent() - ) - } - } -} diff --git a/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/util/TestAppInitializer.kt b/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/util/TestAppInitializer.kt index 5116d6e406..5647d6f32c 100644 --- a/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/util/TestAppInitializer.kt +++ b/packages/test-sync/src/commonMain/kotlin/io/realm/kotlin/test/mongodb/util/TestAppInitializer.kt @@ -22,29 +22,128 @@ import io.realm.kotlin.test.mongodb.common.FLEXIBLE_SYNC_SCHEMA import io.realm.kotlin.test.mongodb.common.PARTITION_BASED_SCHEMA import kotlinx.serialization.json.Json -object TestAppInitializer { - // Setups a test app - suspend fun AppServicesClient.initializeDefault(app: BaasApp, service: Service) { - addEmailProvider(app) - - when (app.name) { - TEST_APP_PARTITION -> initializePartitionSync(app, service) - TEST_APP_FLEX -> initializeFlexibleSync(app, service) +interface AppInitializer { + val name: String + suspend fun initialize(client: AppServicesClient, app: BaasApp) +} + +open class BaseAppInitializer( + override val name: String, + val block: (suspend AppServicesClient.(app: BaasApp) -> Unit)? = null, +) : AppInitializer { + + override suspend fun initialize(client: AppServicesClient, app: BaasApp) { + with(client) { + with(app) { + addFunction(insertDocument) + addFunction(queryDocument) + addFunction(deleteDocument) + addFunction(countDocuments) + + val testAuthFuncId = addFunction(testAuthFunc)._id + addAuthProvider( + """ + { + "type": "custom-function", + "config": { + "authFunctionId": "$testAuthFuncId", + "authFunctionName": "${testAuthFunc.name}" + } + } + """.trimIndent() + ) + + addAuthProvider("""{"type": "anon-user"}""") + + // Enable 'API-KEY' by updating it. It exists by default in the server so we cannot add. + getAuthProvider("api-key").run { + enable(true) + } + + val (type: String, config: String) = if (TEST_APP_CLUSTER_NAME.isEmpty()) { + "mongodb" to """{ "uri": "mongodb://localhost:26000" }""" + } else { + "mongodb-atlas" to """{ "clusterName": "$TEST_APP_CLUSTER_NAME" }""" + } + addService( + """ + { + "name": "BackingDB", + "type": "$type", + "config": $config + } + """.trimIndent() + ).let { service: Service -> + val dbName = app.clientAppId + service.addDefaultRule( + """ + { + "roles": [{ + "name": "defaultRole", + "apply_when": {}, + "document_filters": { + "read": true, + "write": true + }, + "write": true, + "read": true, + "insert": true, + "delete": true + }] + } + """.trimIndent() + ) + + app.setCustomUserData( + """ + { + "mongo_service_id": ${service._id}, + "enabled": true, + "database_name": "$dbName", + "collection_name": "UserData", + "user_id_field": "user_id" + } + """.trimIndent() + ) + } + with(client) { + block?.invoke(this, app) + } + app.setDevelopmentMode(true) + } } } +} - @Suppress("LongMethod") - suspend fun AppServicesClient.initializeFlexibleSync( - app: BaasApp, - service: Service, - recoveryDisabled: Boolean = false - ) { - val databaseName = app.clientAppId +object DefaultPartitionBasedAppInitializer : + BaseAppInitializer( + TEST_APP_PARTITION, + { app -> + addEmailProvider(app) + initializePartitionSync(app) + } + ) - app.setSchema(FLEXIBLE_SYNC_SCHEMA) +object DefaultFlexibleSyncAppInitializer : + BaseAppInitializer( + TEST_APP_FLEX, + { app -> + addEmailProvider(app) + initializeFlexibleSync(app) + } + ) + +@Suppress("LongMethod") +suspend fun AppServicesClient.initializeFlexibleSync( + app: BaasApp, + recoveryDisabled: Boolean = false, // TODO +) { + val databaseName = app.clientAppId - service.setSyncConfig( - """ + app.setSchema(FLEXIBLE_SYNC_SCHEMA) + + app.mongodbService.setSyncConfig( + """ { "flexible_sync": { "state": "enabled", @@ -63,32 +162,30 @@ object TestAppInitializer { ] } } - """.trimIndent() - ) + """.trimIndent() + ) + app.waitUntilInitialSyncCompletes() +} - app.waitUntilInitialSyncCompletes() - } +@Suppress("LongMethod") +suspend fun AppServicesClient.initializePartitionSync( + app: BaasApp, + recoveryDisabled: Boolean = false, // TODO +) { + val databaseName = app.clientAppId + + app.addFunction(canReadPartition) + app.addFunction(canWritePartition) - @Suppress("LongMethod") - suspend fun AppServicesClient.initializePartitionSync( - app: BaasApp, - service: Service, - recoveryDisabled: Boolean = false // TODO - ) { - val databaseName = app.clientAppId - - app.addFunction(canReadPartition) - app.addFunction(canWritePartition) - - app.setSchema( - schema = PARTITION_BASED_SCHEMA, - extraProperties = mapOf( - "realm_id" to PrimitivePropertyType.Type.STRING - ) + app.setSchema( + schema = PARTITION_BASED_SCHEMA, + extraProperties = mapOf( + "realm_id" to PrimitivePropertyType.Type.STRING ) + ) - service.setSyncConfig( - """ + app.mongodbService.setSyncConfig( + """ { "sync": { "state": "enabled", @@ -122,22 +219,22 @@ object TestAppInitializer { } } } - """.trimIndent() - ) + """.trimIndent() + ) - app.waitUntilInitialSyncCompletes() - } + app.waitUntilInitialSyncCompletes() +} - suspend fun AppServicesClient.addEmailProvider( - app: BaasApp, - autoConfirm: Boolean = true, - runConfirmationFunction: Boolean = false - ) = with(app) { - val confirmFuncId = addFunction(confirmFunc)._id - val resetFuncId = addFunction(resetFunc)._id +suspend fun AppServicesClient.addEmailProvider( + app: BaasApp, + autoConfirm: Boolean = true, + runConfirmationFunction: Boolean = false, +) = with(app) { + val confirmFuncId = addFunction(confirmFunc)._id + val resetFuncId = addFunction(resetFunc)._id - addAuthProvider( - """ + addAuthProvider( + """ { "type": "local-userpass", "config": { @@ -153,93 +250,14 @@ object TestAppInitializer { "runResetFunction": false } } - """.trimIndent() - ) - } - - suspend fun AppServicesClient.initialize( - app: BaasApp, - block: suspend AppServicesClient.(app: BaasApp, service: Service) -> Unit - ) = with(app) { - addFunction(insertDocument) - addFunction(queryDocument) - addFunction(deleteDocument) - addFunction(countDocuments) - - val testAuthFuncId = addFunction(testAuthFunc)._id - addAuthProvider( - """ - { - "type": "custom-function", - "config": { - "authFunctionId": "$testAuthFuncId", - "authFunctionName": "${testAuthFunc.name}" - } - } - """.trimIndent() - ) - - addAuthProvider("""{"type": "anon-user"}""") - - // Enable 'API-KEY' by updating it. It exists by default in the server so we cannot add. - getAuthProvider("api-key").run { - enable(true) - } - - val (type: String, config: String) = if (TEST_APP_CLUSTER_NAME.isEmpty()) { - "mongodb" to """{ "uri": "mongodb://localhost:26000" }""" - } else { - "mongodb-atlas" to """{ "clusterName": "$TEST_APP_CLUSTER_NAME" }""" - } - addService( - """ - { - "name": "BackingDB", - "type": "$type", - "config": $config - } - """.trimIndent() - ).let { service: Service -> - val dbName = app.clientAppId - service.addDefaultRule( - """ - { - "roles": [{ - "name": "defaultRole", - "apply_when": {}, - "document_filters": { - "read": true, - "write": true - }, - "write": true, - "read": true, - "insert": true, - "delete": true - }] - } - """.trimIndent() - ) - - app.setCustomUserData( - """ - { - "mongo_service_id": ${service._id}, - "enabled": true, - "database_name": "$dbName", - "collection_name": "UserData", - "user_id_field": "user_id" - } - """.trimIndent() - ) - - block(app, service) - } - } + """.trimIndent() + ) +} - private val insertDocument = Function( - name = "insertDocument", - source = - """ +private val insertDocument = Function( + name = "insertDocument", + source = + """ exports = function (service, db, collection, document) { const mongodb = context.services.get(service); const result = mongodb @@ -250,13 +268,13 @@ object TestAppInitializer { return result; } - """.trimIndent() - ) + """.trimIndent() +) - private val deleteDocument = Function( - name = "deleteDocument", - source = - """ +private val deleteDocument = Function( + name = "deleteDocument", + source = + """ exports = function (service, db, collection, query) { const mongodb = context.services.get(service); const result = mongodb @@ -267,13 +285,13 @@ object TestAppInitializer { return result; } - """.trimIndent() - ) + """.trimIndent() +) - private val queryDocument = Function( - name = "queryDocument", - source = - """ +private val queryDocument = Function( + name = "queryDocument", + source = + """ exports = function (service, db, collection, query) { const mongodb = context.services.get(service); const result = mongodb @@ -283,14 +301,13 @@ object TestAppInitializer { return result; } - - """.trimIndent() - ) + """.trimIndent() +) - private val countDocuments = Function( - name = "countDocuments", - source = - """ +private val countDocuments = Function( + name = "countDocuments", + source = + """ exports = function (service, db, collection) { const mongodb = context.services.get(service); const result = mongodb @@ -300,13 +317,13 @@ object TestAppInitializer { return { value: result }; } - """.trimIndent() - ) + """.trimIndent() +) - private val testAuthFunc = Function( - name = "testAuthFunc", - source = - """ +private val testAuthFunc = Function( + name = "testAuthFunc", + source = + """ exports = ({mail, id}) => { // Auth function will fail for emails with a domain different to @10gen.com // or with id lower than 666 @@ -317,14 +334,13 @@ object TestAppInitializer { return mail; } } - - """.trimIndent() - ) + """.trimIndent() +) - private val confirmFunc = Function( - name = "confirmFunc", - source = - """ +private val confirmFunc = Function( + name = "confirmFunc", + source = + """ exports = async ({ token, tokenId, username }) => { // process the confirm token, tokenId and username @@ -350,14 +366,13 @@ object TestAppInitializer { return { status: 'fail' }; } } - - """.trimIndent() - ) + """.trimIndent() +) - private val resetFunc = Function( - name = "resetFunc", - source = - """ +private val resetFunc = Function( + name = "resetFunc", + source = + """ exports = ({ token, tokenId, username, password }, customParam1, customParam2) => { if (customParam1 != "say-the-magic-word" || customParam2 != 42) { return { status: 'fail' }; @@ -365,14 +380,13 @@ object TestAppInitializer { return { status: 'success' }; } } - - """.trimIndent() - ) + """.trimIndent() +) - private val canReadPartition = Function( - name = "canReadPartition", - source = - """ +private val canReadPartition = Function( + name = "canReadPartition", + source = + """ /** * Users with an email that contains `_noread_` do not have read access, * all others do. @@ -385,90 +399,84 @@ object TestAppInitializer { return true; } } - - """.trimIndent() - ) + """.trimIndent() +) - private val canWritePartition = Function( +private val canWritePartition = + Function( name = "canWritePartition", source = """ - /** - * Users with an email that contains `_nowrite_` do not have write access, - * all others do. - */ - exports = async (partition) => { - const email = context.user.data.email; - if (email != undefined) { - return(!email.includes("_nowrite_")); - } else { - return true; - } - } - + /** + * Users with an email that contains `_nowrite_` do not have write access, + * all others do. + */ + exports = async (partition) => { + const email = context.user.data.email; + if (email != undefined) { + return(!email.includes("_nowrite_")); + } else { + return true; + } + } """.trimIndent() ) - val FIRST_ARG_FUNCTION = Function( - name = "firstArg", - source = - """ +val FIRST_ARG_FUNCTION = Function( + name = "firstArg", + source = + """ exports = function(arg){ // Returns first argument return arg }; - - """.trimIndent() - ) + """.trimIndent() +) - val SUM_FUNCTION = Function( - name = "sum", - source = - """ +val SUM_FUNCTION = Function( + name = "sum", + source = + """ exports = function(...args) { return parseInt(args.reduce((a,b) => a + b, 0)); }; - - """.trimIndent() - ) + """.trimIndent() +) - val NULL_FUNCTION = Function( - name = "null", - source = - """ +val NULL_FUNCTION = Function( + name = "null", + source = + """ exports = function(arg){ return null; }; - - """.trimIndent() - ) + """.trimIndent() +) - val ERROR_FUNCTION = Function( - name = "error", - source = - """ +val ERROR_FUNCTION = Function( + name = "error", + source = + """ exports = function(arg){ return unknown; }; - - """.trimIndent() - ) + """.trimIndent() +) - val VOID_FUNCTION = Function( - name = "void", - source = - """ +val VOID_FUNCTION = Function( + name = "void", + source = + """ exports = function(arg){ return void(0); }; - - """.trimIndent() - ) + """.trimIndent() +) - val AUTHORIZED_ONLY_FUNCTION = Function( - name = "authorizedOnly", - source = - """ +val AUTHORIZED_ONLY_FUNCTION = Function( + name = "authorizedOnly", + source = + """ exports = function(arg){ /* Accessing application's values: @@ -485,10 +493,9 @@ object TestAppInitializer { */ return {arg: context.user}; }; - - """.trimIndent(), - canEvaluate = Json.decodeFromString( - """ + """.trimIndent(), + canEvaluate = Json.decodeFromString( + """ { "%%user.data.email": { "%in": [ @@ -496,7 +503,6 @@ object TestAppInitializer { ] } } - """.trimIndent() - ) + """.trimIndent() ) -} +) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/ApiKeyAuthTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/ApiKeyAuthTests.kt index 6b4e3f0add..0c6a6e30d7 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/ApiKeyAuthTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/ApiKeyAuthTests.kt @@ -19,9 +19,9 @@ import io.realm.kotlin.internal.platform.runBlocking import io.realm.kotlin.mongodb.User import io.realm.kotlin.mongodb.auth.ApiKeyAuth import io.realm.kotlin.mongodb.exceptions.ServiceException -import io.realm.kotlin.test.mongodb.TEST_APP_PARTITION import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.util.TestHelper import org.mongodb.kbson.BsonObjectId import kotlin.test.AfterTest @@ -50,7 +50,7 @@ class ApiKeyAuthTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName, appName = TEST_APP_PARTITION) + app = TestApp(this::class.simpleName, DefaultPartitionBasedAppInitializer) user = app.createUserAndLogin() provider = user.apiKeyAuth } diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/AppConfigurationTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/AppConfigurationTests.kt index c0c23ef492..0400b24695 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/AppConfigurationTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/AppConfigurationTests.kt @@ -34,6 +34,7 @@ import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage import io.realm.kotlin.test.mongodb.createUserAndLogIn import io.realm.kotlin.test.mongodb.use +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.platform.PlatformUtils import io.realm.kotlin.test.util.TestChannel import io.realm.kotlin.test.util.TestHelper @@ -167,9 +168,11 @@ class AppConfigurationTests { @Test fun syncRootDirectory_appendDirectoryToPath() = runBlocking { val expectedRoot = pathOf(appFilesDirectory(), "myCustomDir") - TestApp("syncRootDirectory_appendDirectoryToPath", builder = { - it.syncRootDirectory(expectedRoot) - }).use { app -> + TestApp( + "syncRootDirectory_appendDirectoryToPath", + DefaultPartitionBasedAppInitializer, + builder = { it.syncRootDirectory(expectedRoot) } + ).use { app -> val (email, password) = TestHelper.randomEmail() to "password1234" val user = app.createUserAndLogIn(email, password) assertEquals(expectedRoot, app.configuration.syncRootDirectory) @@ -387,6 +390,8 @@ class AppConfigurationTests { fun customHeadersTest() = runBlocking { TestApp( "customHeadersTest", + DefaultPartitionBasedAppInitializer, + debug = true, builder = { builder -> builder.customRequestHeaders { put(CUSTOM_HEADER_NAME, CUSTOM_HEADER_VALUE) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/AppTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/AppTests.kt index ee9a0e90bc..373678ac1c 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/AppTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/AppTests.kt @@ -36,12 +36,13 @@ import io.realm.kotlin.mongodb.exceptions.AuthException import io.realm.kotlin.mongodb.exceptions.ServiceException import io.realm.kotlin.mongodb.sync.SyncConfiguration import io.realm.kotlin.test.mongodb.SyncServerConfig -import io.realm.kotlin.test.mongodb.TEST_APP_FLEX import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.asTestApp import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage import io.realm.kotlin.test.mongodb.createUserAndLogIn import io.realm.kotlin.test.mongodb.use +import io.realm.kotlin.test.mongodb.util.DefaultFlexibleSyncAppInitializer +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.platform.PlatformUtils import io.realm.kotlin.test.util.TestChannel import io.realm.kotlin.test.util.TestHelper @@ -68,7 +69,7 @@ class AppTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName) + app = TestApp(this::class.simpleName, DefaultPartitionBasedAppInitializer) } @AfterTest @@ -382,7 +383,7 @@ class AppTests { val key = TestHelper.getRandomKey() TestApp( "encryptedMetadataRealm", - appName = TEST_APP_FLEX, + DefaultFlexibleSyncAppInitializer, builder = { it .encryptionKey(key) @@ -421,7 +422,7 @@ class AppTests { val correctKey = TestHelper.getRandomKey() TestApp( "encryptedMetadataRealm_openWithWrongKeyThrows", - appName = TEST_APP_FLEX, + DefaultFlexibleSyncAppInitializer, builder = { it .encryptionKey(correctKey) @@ -461,7 +462,7 @@ class AppTests { // Create new test app with a random encryption key TestApp( "encryptedMetadataRealm_openWithoutKeyThrows", - appName = TEST_APP_FLEX, + DefaultFlexibleSyncAppInitializer, builder = { it .encryptionKey(TestHelper.getRandomKey()) @@ -502,6 +503,7 @@ class AppTests { fun changeBaseUrl() { TestApp( testId = "changeBaseUrl", + DefaultPartitionBasedAppInitializer, builder = { builder -> // We create a test app that points to the default base url // this app is not going to be validated yet. @@ -527,6 +529,7 @@ class AppTests { fun changeBaseUrl_null() { TestApp( testId = "changeBaseUrl", + DefaultPartitionBasedAppInitializer, ).use { testApp -> assertEquals(SyncServerConfig.url, testApp.baseUrl) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/AsymmetricSyncTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/AsymmetricSyncTests.kt index 1e2675c481..fbdddadf81 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/AsymmetricSyncTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/AsymmetricSyncTests.kt @@ -30,10 +30,10 @@ import io.realm.kotlin.mongodb.sync.SyncConfiguration import io.realm.kotlin.mongodb.syncSession import io.realm.kotlin.schema.RealmClassKind import io.realm.kotlin.test.StandaloneDynamicMutableRealm -import io.realm.kotlin.test.mongodb.TEST_APP_FLEX import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.common.utils.uploadAllLocalChangesOrFail import io.realm.kotlin.test.mongodb.createUserAndLogIn +import io.realm.kotlin.test.mongodb.util.DefaultFlexibleSyncAppInitializer import io.realm.kotlin.test.util.TestHelper import io.realm.kotlin.test.util.use import kotlinx.atomicfu.atomic @@ -56,7 +56,7 @@ class AsymmetricSyncTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName, appName = TEST_APP_FLEX) + app = TestApp(this::class.simpleName, DefaultFlexibleSyncAppInitializer) val (email, password) = TestHelper.randomEmail() to "password1234" val user = runBlocking { app.createUserAndLogIn(email, password) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/CredentialsTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/CredentialsTests.kt index 53f3bf8fb8..75e1e3f828 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/CredentialsTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/CredentialsTests.kt @@ -35,6 +35,7 @@ import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.asTestApp import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage import io.realm.kotlin.test.mongodb.createUserAndLogIn +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.util.TestHelper import kotlinx.serialization.Serializable import kotlin.test.AfterTest @@ -60,7 +61,7 @@ class CredentialsTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName) + app = TestApp(this::class.simpleName, DefaultPartitionBasedAppInitializer) } @AfterTest diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/EmailPasswordAuthTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/EmailPasswordAuthTests.kt index 40ee4d0ac1..39f123121c 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/EmailPasswordAuthTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/EmailPasswordAuthTests.kt @@ -10,13 +10,13 @@ import io.realm.kotlin.mongodb.exceptions.ServiceException import io.realm.kotlin.mongodb.exceptions.UserAlreadyConfirmedException import io.realm.kotlin.mongodb.exceptions.UserAlreadyExistsException import io.realm.kotlin.mongodb.exceptions.UserNotFoundException -import io.realm.kotlin.test.mongodb.TEST_APP_PARTITION import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.asTestApp import io.realm.kotlin.test.mongodb.syncServerAppName import io.realm.kotlin.test.mongodb.util.BaasApp -import io.realm.kotlin.test.mongodb.util.Service -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.addEmailProvider +import io.realm.kotlin.test.mongodb.util.BaseAppInitializer +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer +import io.realm.kotlin.test.mongodb.util.addEmailProvider import io.realm.kotlin.test.util.TestHelper import kotlin.test.AfterTest import kotlin.test.BeforeTest @@ -32,7 +32,7 @@ class EmailPasswordAuthWithAutoConfirmTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName, appName = TEST_APP_PARTITION) + app = TestApp(this::class.simpleName, DefaultPartitionBasedAppInitializer) } @AfterTest @@ -243,9 +243,13 @@ class EmailPasswordAuthWithEmailConfirmTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName, appName = syncServerAppName("em-cnfrm"), initialSetup = { app: BaasApp, service: Service -> - addEmailProvider(app, autoConfirm = false) - }) + app = TestApp( + this::class.simpleName, + object : BaseAppInitializer( + syncServerAppName("em-cnfrm"), + { app: BaasApp -> addEmailProvider(app, autoConfirm = false) } + ) {} + ) } @AfterTest @@ -281,9 +285,15 @@ class EmailPasswordAuthWithCustomFunctionTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName, appName = syncServerAppName("em-cstm"), initialSetup = { app: BaasApp, service: Service -> - addEmailProvider(app, autoConfirm = false, runConfirmationFunction = true) - }) + app = TestApp( + this::class.simpleName, + object : BaseAppInitializer( + syncServerAppName("em-cstm"), + { app -> + addEmailProvider(app, autoConfirm = false, runConfirmationFunction = true) + } + ) {} + ) } @AfterTest diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FLXProgressListenerTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FLXProgressListenerTests.kt index 9fe4307aa6..9c67b39196 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FLXProgressListenerTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FLXProgressListenerTests.kt @@ -26,10 +26,10 @@ import io.realm.kotlin.mongodb.sync.Progress import io.realm.kotlin.mongodb.sync.ProgressMode import io.realm.kotlin.mongodb.sync.SyncConfiguration import io.realm.kotlin.mongodb.syncSession -import io.realm.kotlin.test.mongodb.TEST_APP_FLEX import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.createUserAndLogIn import io.realm.kotlin.test.mongodb.use +import io.realm.kotlin.test.mongodb.util.DefaultFlexibleSyncAppInitializer import io.realm.kotlin.test.util.TestChannel import io.realm.kotlin.test.util.receiveOrFail import io.realm.kotlin.test.util.use @@ -66,7 +66,7 @@ class FLXProgressListenerTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName, appName = TEST_APP_FLEX) + app = TestApp(this::class.simpleName, DefaultFlexibleSyncAppInitializer) partitionValue = org.mongodb.kbson.ObjectId().toString() } @@ -233,7 +233,7 @@ class FLXProgressListenerTests { @Test fun completesOnClose() = runBlocking { val channel = TestChannel(capacity = 5, onBufferOverflow = BufferOverflow.DROP_OLDEST, failIfBufferIsEmptyOnCancel = false) - TestApp("completesOnClose", TEST_APP_FLEX).use { app -> + TestApp("completesOnClose", DefaultFlexibleSyncAppInitializer).use { app -> val user = app.createUserAndLogIn() val realm = Realm.open(createSyncConfig(user)) try { diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FlexibleSyncConfigurationTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FlexibleSyncConfigurationTests.kt index be13d1f51f..337a520fae 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FlexibleSyncConfigurationTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FlexibleSyncConfigurationTests.kt @@ -24,10 +24,10 @@ import io.realm.kotlin.mongodb.User import io.realm.kotlin.mongodb.sync.InitialSubscriptionsCallback import io.realm.kotlin.mongodb.sync.SyncConfiguration import io.realm.kotlin.mongodb.sync.SyncMode -import io.realm.kotlin.test.mongodb.TEST_APP_FLEX import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.asTestApp import io.realm.kotlin.test.mongodb.createUserAndLogIn +import io.realm.kotlin.test.mongodb.util.DefaultFlexibleSyncAppInitializer import io.realm.kotlin.test.util.TestHelper import kotlinx.atomicfu.atomic import kotlin.test.AfterTest @@ -47,7 +47,7 @@ class FlexibleSyncConfigurationTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName, appName = TEST_APP_FLEX) + app = TestApp(this::class.simpleName, DefaultFlexibleSyncAppInitializer) val (email, password) = TestHelper.randomEmail() to "password1234" val user = runBlocking { app.createUserAndLogIn(email, password) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FlexibleSyncIntegrationTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FlexibleSyncIntegrationTests.kt index 640c777eee..6951cab536 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FlexibleSyncIntegrationTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FlexibleSyncIntegrationTests.kt @@ -29,11 +29,11 @@ import io.realm.kotlin.mongodb.subscriptions import io.realm.kotlin.mongodb.sync.SyncConfiguration import io.realm.kotlin.mongodb.sync.SyncSession import io.realm.kotlin.mongodb.syncSession -import io.realm.kotlin.test.mongodb.TEST_APP_FLEX import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.common.utils.uploadAllLocalChangesOrFail import io.realm.kotlin.test.mongodb.common.utils.waitForSynchronizationOrFail import io.realm.kotlin.test.mongodb.createUserAndLogIn +import io.realm.kotlin.test.mongodb.util.DefaultFlexibleSyncAppInitializer import io.realm.kotlin.test.util.TestChannel import io.realm.kotlin.test.util.TestHelper import io.realm.kotlin.test.util.receiveOrFail @@ -63,7 +63,7 @@ class FlexibleSyncIntegrationTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName, appName = TEST_APP_FLEX) + app = TestApp(this::class.simpleName, DefaultFlexibleSyncAppInitializer) val (email, password) = TestHelper.randomEmail() to "password1234" runBlocking { app.createUserAndLogIn(email, password) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FunctionsTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FunctionsTests.kt index 747b51c812..20646dd322 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FunctionsTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/FunctionsTests.kt @@ -40,15 +40,14 @@ import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage import io.realm.kotlin.test.mongodb.createUserAndLogIn import io.realm.kotlin.test.mongodb.syncServerAppName -import io.realm.kotlin.test.mongodb.util.BaasApp -import io.realm.kotlin.test.mongodb.util.Service -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.AUTHORIZED_ONLY_FUNCTION -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.ERROR_FUNCTION -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.FIRST_ARG_FUNCTION -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.NULL_FUNCTION -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.SUM_FUNCTION -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.VOID_FUNCTION -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.initializeDefault +import io.realm.kotlin.test.mongodb.util.AUTHORIZED_ONLY_FUNCTION +import io.realm.kotlin.test.mongodb.util.BaseAppInitializer +import io.realm.kotlin.test.mongodb.util.ERROR_FUNCTION +import io.realm.kotlin.test.mongodb.util.FIRST_ARG_FUNCTION +import io.realm.kotlin.test.mongodb.util.NULL_FUNCTION +import io.realm.kotlin.test.mongodb.util.SUM_FUNCTION +import io.realm.kotlin.test.mongodb.util.VOID_FUNCTION +import io.realm.kotlin.test.mongodb.util.addEmailProvider import io.realm.kotlin.test.util.TypeDescriptor import io.realm.kotlin.types.MutableRealmInt import io.realm.kotlin.types.RealmAny @@ -201,7 +200,18 @@ class FunctionsTests { fun setup() { app = TestApp( FunctionsTests::class.simpleName, - syncServerAppName("funcs"), + object : BaseAppInitializer( + syncServerAppName("funcs"), + { app -> + addEmailProvider(app) + app.addFunction(FIRST_ARG_FUNCTION) + app.addFunction(NULL_FUNCTION) + app.addFunction(SUM_FUNCTION) + app.addFunction(ERROR_FUNCTION) + app.addFunction(VOID_FUNCTION) + app.addFunction(AUTHORIZED_ONLY_FUNCTION) + } + ) {}, ejson = EJson( serializersModule = SerializersModule { polymorphic(RealmObject::class) { @@ -209,15 +219,7 @@ class FunctionsTests { } } ) - ) { app: BaasApp, service: Service -> - initializeDefault(app, service) - app.addFunction(FIRST_ARG_FUNCTION) - app.addFunction(NULL_FUNCTION) - app.addFunction(SUM_FUNCTION) - app.addFunction(ERROR_FUNCTION) - app.addFunction(VOID_FUNCTION) - app.addFunction(AUTHORIZED_ONLY_FUNCTION) - } + ) anonUser = runBlocking { app.login(Credentials.anonymous()) } diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/GeoSpatialTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/GeoSpatialTests.kt index 1cdbe71ff3..58c2943aab 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/GeoSpatialTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/GeoSpatialTests.kt @@ -25,9 +25,9 @@ import io.realm.kotlin.internal.platform.runBlocking import io.realm.kotlin.mongodb.User import io.realm.kotlin.mongodb.sync.SyncConfiguration import io.realm.kotlin.mongodb.syncSession -import io.realm.kotlin.test.mongodb.TEST_APP_FLEX import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.createUserAndLogIn +import io.realm.kotlin.test.mongodb.util.DefaultFlexibleSyncAppInitializer import io.realm.kotlin.test.util.TestHelper import io.realm.kotlin.test.util.use import io.realm.kotlin.types.geo.Distance @@ -49,7 +49,7 @@ class GeoSpatialTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName, appName = TEST_APP_FLEX) + app = TestApp(this::class.simpleName, DefaultFlexibleSyncAppInitializer) } @AfterTest diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/HttpLogObfuscatorTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/HttpLogObfuscatorTests.kt index f72eba9784..bb8cffc67d 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/HttpLogObfuscatorTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/HttpLogObfuscatorTests.kt @@ -29,8 +29,11 @@ import io.realm.kotlin.mongodb.ext.call import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.common.utils.CustomLogCollector import io.realm.kotlin.test.mongodb.syncServerAppName -import io.realm.kotlin.test.mongodb.util.TestAppInitializer -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.initializeDefault +import io.realm.kotlin.test.mongodb.util.BaseAppInitializer +import io.realm.kotlin.test.mongodb.util.FIRST_ARG_FUNCTION +import io.realm.kotlin.test.mongodb.util.NULL_FUNCTION +import io.realm.kotlin.test.mongodb.util.SUM_FUNCTION +import io.realm.kotlin.test.mongodb.util.addEmailProvider import io.realm.kotlin.test.util.receiveOrFail import kotlinx.coroutines.CancellationException import kotlinx.coroutines.async @@ -122,13 +125,12 @@ class HttpLogObfuscatorTests { private fun initApp(): TestApp { return TestApp( this::class.simpleName, - appName = syncServerAppName("obfsctr"), - initialSetup = { app, service -> - initializeDefault(app, service) - app.addFunction(TestAppInitializer.FIRST_ARG_FUNCTION) - app.addFunction(TestAppInitializer.SUM_FUNCTION) - app.addFunction(TestAppInitializer.NULL_FUNCTION) - } + object : BaseAppInitializer(syncServerAppName("obfsctr"), { app -> + addEmailProvider(app) + app.addFunction(FIRST_ARG_FUNCTION) + app.addFunction(SUM_FUNCTION) + app.addFunction(NULL_FUNCTION) + }) {} ) } @@ -148,14 +150,16 @@ class HttpLogObfuscatorTests { RealmLog.add(logger) app = TestApp( "nullObfuscator", - appName = syncServerAppName("null-obf"), + object : BaseAppInitializer( + syncServerAppName("null-obf"), + { app -> + addEmailProvider(app) + app.addFunction(FIRST_ARG_FUNCTION) + app.addFunction(SUM_FUNCTION) + app.addFunction(NULL_FUNCTION) + } + ) {}, builder = { it.httpLogObfuscator(null) }, - initialSetup = { app, service -> - initializeDefault(app, service) - app.addFunction(TestAppInitializer.FIRST_ARG_FUNCTION) - app.addFunction(TestAppInitializer.SUM_FUNCTION) - app.addFunction(TestAppInitializer.NULL_FUNCTION) - } ) // Create user and log in @@ -190,9 +194,9 @@ class HttpLogObfuscatorTests { // Calling functions with arguments results in these not being obfuscated with(user.functions) { - call(TestAppInitializer.FIRST_ARG_FUNCTION.name, 42.0) - call(TestAppInitializer.SUM_FUNCTION.name, 42.0, 1.0) - call(TestAppInitializer.NULL_FUNCTION.name) + call(FIRST_ARG_FUNCTION.name, 42.0) + call(SUM_FUNCTION.name, 42.0, 1.0) + call(NULL_FUNCTION.name) } // Verify that none of the logs are obfuscated @@ -315,9 +319,9 @@ class HttpLogObfuscatorTests { async { with(user.functions) { - call(TestAppInitializer.FIRST_ARG_FUNCTION.name, 42.0) - call(TestAppInitializer.SUM_FUNCTION.name, 42.0, 1.0) - call(TestAppInitializer.NULL_FUNCTION.name) + call(FIRST_ARG_FUNCTION.name, 42.0) + call(SUM_FUNCTION.name, 42.0, 1.0) + call(NULL_FUNCTION.name) } } // 1st custom function call - request (obfuscate arguments) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/MutableSubscriptionSetTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/MutableSubscriptionSetTests.kt index 248aa3e7f0..9e5f9524a0 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/MutableSubscriptionSetTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/MutableSubscriptionSetTests.kt @@ -27,10 +27,10 @@ import io.realm.kotlin.mongodb.sync.SubscriptionSet import io.realm.kotlin.mongodb.sync.SubscriptionSetState import io.realm.kotlin.mongodb.sync.SyncConfiguration import io.realm.kotlin.mongodb.syncSession -import io.realm.kotlin.test.mongodb.TEST_APP_FLEX import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.common.utils.uploadAllLocalChangesOrFail import io.realm.kotlin.test.mongodb.createUserAndLogIn +import io.realm.kotlin.test.mongodb.util.DefaultFlexibleSyncAppInitializer import io.realm.kotlin.test.util.TestHelper import io.realm.kotlin.test.util.toRealmInstant import io.realm.kotlin.test.util.use @@ -57,7 +57,7 @@ class MutableSubscriptionSetTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName, appName = TEST_APP_FLEX) + app = TestApp(this::class.simpleName, DefaultFlexibleSyncAppInitializer) val (email, password) = TestHelper.randomEmail() to "password1234" val user = runBlocking { app.createUserAndLogIn(email, password) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/PBSProgressListenerTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/PBSProgressListenerTests.kt index 0ab083e3ce..3b65a396e3 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/PBSProgressListenerTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/PBSProgressListenerTests.kt @@ -29,11 +29,11 @@ import io.realm.kotlin.mongodb.sync.ProgressMode import io.realm.kotlin.mongodb.sync.SyncConfiguration import io.realm.kotlin.mongodb.sync.SyncSession import io.realm.kotlin.mongodb.syncSession -import io.realm.kotlin.test.mongodb.TEST_APP_PARTITION import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.common.utils.uploadAllLocalChangesOrFail import io.realm.kotlin.test.mongodb.createUserAndLogIn import io.realm.kotlin.test.mongodb.use +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.util.TestChannel import io.realm.kotlin.test.util.receiveOrFail import io.realm.kotlin.test.util.use @@ -70,7 +70,7 @@ class PBSProgressListenerTests { @BeforeTest fun setup() { RealmLog.setLevel(LogLevel.INFO) - app = TestApp(this::class.simpleName, appName = TEST_APP_PARTITION) + app = TestApp(this::class.simpleName, DefaultPartitionBasedAppInitializer) partitionValue = org.mongodb.kbson.ObjectId().toString() } @@ -265,7 +265,7 @@ class PBSProgressListenerTests { fun completesOnClose() = runBlocking { val channel = TestChannel(capacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST) - TestApp("completesOnClose", TEST_APP_PARTITION).use { app -> + TestApp("completesOnClose", DefaultPartitionBasedAppInitializer).use { app -> val user = app.createUserAndLogIn() val realm = Realm.open(createSyncConfig(user)) try { diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SubscriptionExtensionsTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SubscriptionExtensionsTests.kt index 45cf941a6f..3644f630b0 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SubscriptionExtensionsTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SubscriptionExtensionsTests.kt @@ -31,10 +31,10 @@ import io.realm.kotlin.mongodb.sync.SyncConfiguration import io.realm.kotlin.mongodb.sync.WaitForSync import io.realm.kotlin.mongodb.syncSession import io.realm.kotlin.query.RealmResults -import io.realm.kotlin.test.mongodb.TEST_APP_FLEX import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.common.utils.uploadAllLocalChangesOrFail import io.realm.kotlin.test.mongodb.createUserAndLogIn +import io.realm.kotlin.test.mongodb.util.DefaultFlexibleSyncAppInitializer import io.realm.kotlin.test.util.TestHelper import io.realm.kotlin.test.util.use import kotlinx.coroutines.TimeoutCancellationException @@ -61,7 +61,7 @@ class SubscriptionExtensionsTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName, appName = TEST_APP_FLEX) + app = TestApp(this::class.simpleName, DefaultFlexibleSyncAppInitializer) val (email, password) = TestHelper.randomEmail() to "password1234" val user = runBlocking { app.createUserAndLogIn(email, password) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SubscriptionSetTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SubscriptionSetTests.kt index 993957978e..6c28da7467 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SubscriptionSetTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SubscriptionSetTests.kt @@ -24,12 +24,12 @@ import io.realm.kotlin.mongodb.subscriptions import io.realm.kotlin.mongodb.sync.Subscription import io.realm.kotlin.mongodb.sync.SubscriptionSetState import io.realm.kotlin.mongodb.sync.SyncConfiguration -import io.realm.kotlin.test.mongodb.TEST_APP_FLEX -import io.realm.kotlin.test.mongodb.TEST_APP_PARTITION import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.common.utils.waitForSynchronizationOrFail import io.realm.kotlin.test.mongodb.createUserAndLogIn import io.realm.kotlin.test.mongodb.use +import io.realm.kotlin.test.mongodb.util.DefaultFlexibleSyncAppInitializer +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.util.TestHelper import io.realm.kotlin.test.util.use import kotlin.test.AfterTest @@ -56,7 +56,7 @@ class SubscriptionSetTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName, appName = TEST_APP_FLEX) + app = TestApp(this::class.simpleName, DefaultFlexibleSyncAppInitializer) val (email, password) = TestHelper.randomEmail() to "password1234" val user = runBlocking { app.createUserAndLogIn(email, password) @@ -89,7 +89,7 @@ class SubscriptionSetTests { @Test fun subscriptions_failOnNonFlexibleSyncRealms() { - TestApp(this::class.simpleName, appName = TEST_APP_PARTITION).use { testApp -> + TestApp(this::class.simpleName, DefaultPartitionBasedAppInitializer).use { testApp -> val (email, password) = TestHelper.randomEmail() to "password1234" val user = runBlocking { testApp.createUserAndLogIn(email, password) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SubscriptionTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SubscriptionTests.kt index ffe94723a1..a6c8680285 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SubscriptionTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SubscriptionTests.kt @@ -28,6 +28,7 @@ import io.realm.kotlin.mongodb.sync.asQuery import io.realm.kotlin.query.RealmQuery import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.createUserAndLogIn +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.util.TestHelper.randomEmail import io.realm.kotlin.test.util.toRealmInstant import io.realm.kotlin.types.RealmInstant @@ -55,7 +56,7 @@ class SubscriptionTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName) + app = TestApp(this::class.simpleName, DefaultPartitionBasedAppInitializer,) val (email, password) = randomEmail() to "password1234" val user = runBlocking { app.createUserAndLogIn(email, password) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncClientResetIntegrationTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncClientResetIntegrationTests.kt index 3a1011c711..66a26acc6b 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncClientResetIntegrationTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncClientResetIntegrationTests.kt @@ -47,9 +47,11 @@ import io.realm.kotlin.test.mongodb.TEST_APP_FLEX import io.realm.kotlin.test.mongodb.TEST_APP_PARTITION import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.createUserAndLogIn -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.addEmailProvider -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.initializeFlexibleSync -import io.realm.kotlin.test.mongodb.util.TestAppInitializer.initializePartitionSync +import io.realm.kotlin.test.mongodb.util.BaasApp +import io.realm.kotlin.test.mongodb.util.BaseAppInitializer +import io.realm.kotlin.test.mongodb.util.addEmailProvider +import io.realm.kotlin.test.mongodb.util.initializeFlexibleSync +import io.realm.kotlin.test.mongodb.util.initializePartitionSync import io.realm.kotlin.test.util.TestChannel import io.realm.kotlin.test.util.TestHelper import io.realm.kotlin.test.util.receiveOrFail @@ -118,16 +120,15 @@ class SyncClientResetIntegrationTests { RealmLog.add(ClientResetLoggerInspector(logChannel)) val app = TestApp( this::class.simpleName, - appName = appName, - initialSetup = { app, service -> + object : BaseAppInitializer(appName, { app: BaasApp -> addEmailProvider(app) when (syncMode) { SyncMode.PARTITION_BASED -> - initializePartitionSync(app, service, recoveryDisabled) + initializePartitionSync(app, recoveryDisabled) SyncMode.FLEXIBLE -> - initializeFlexibleSync(app, service, recoveryDisabled) + initializeFlexibleSync(app, recoveryDisabled) } - } + }) {}, ) try { val (email, password) = TestHelper.randomEmail() to "password1234" diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncClientTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncClientTests.kt index 52fe439bea..dd8d461b38 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncClientTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncClientTests.kt @@ -8,6 +8,7 @@ import io.realm.kotlin.mongodb.sync.SyncConfiguration import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.asTestApp import io.realm.kotlin.test.mongodb.createUserAndLogIn +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.util.TestHelper import io.realm.kotlin.test.util.use import kotlin.test.AfterTest @@ -28,7 +29,7 @@ class SyncClientTests { @BeforeTest fun setup() { - app = TestApp(this::class.simpleName) + app = TestApp(this::class.simpleName, DefaultPartitionBasedAppInitializer) val (email, password) = TestHelper.randomEmail() to "password1234" user = runBlocking { app.createUserAndLogIn(email, password) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncConfigTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncConfigTests.kt index 235dadff6b..5474ac22cb 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncConfigTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncConfigTests.kt @@ -48,6 +48,7 @@ import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.asTestApp import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage import io.realm.kotlin.test.mongodb.createUserAndLogIn +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.util.TestHelper import io.realm.kotlin.test.util.TestHelper.getRandomKey import io.realm.kotlin.test.util.TestHelper.randomEmail @@ -86,7 +87,7 @@ class SyncConfigTests { @BeforeTest fun setup() { partitionValue = TestHelper.randomPartitionValue() - app = TestApp(this::class.simpleName) + app = TestApp(this::class.simpleName, DefaultPartitionBasedAppInitializer) } @AfterTest diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncSessionTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncSessionTests.kt index 5832f3b170..ec372c0bb3 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncSessionTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncSessionTests.kt @@ -36,6 +36,7 @@ import io.realm.kotlin.test.mongodb.asTestApp import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage import io.realm.kotlin.test.mongodb.common.utils.uploadAllLocalChangesOrFail import io.realm.kotlin.test.mongodb.createUserAndLogIn +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.platform.PlatformUtils import io.realm.kotlin.test.util.TestChannel import io.realm.kotlin.test.util.TestHelper @@ -78,7 +79,7 @@ class SyncSessionTests { @BeforeTest fun setup() { partitionValue = TestHelper.randomPartitionValue() - app = TestApp(this::class.simpleName) + app = TestApp(this::class.simpleName, DefaultPartitionBasedAppInitializer) val (email, password) = TestHelper.randomEmail() to "password1234" user = runBlocking { app.createUserAndLogIn(email, password) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncedRealmTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncedRealmTests.kt index 46d54cb425..6755835db8 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncedRealmTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncedRealmTests.kt @@ -61,13 +61,14 @@ import io.realm.kotlin.query.RealmResults import io.realm.kotlin.schema.RealmClass import io.realm.kotlin.schema.RealmSchema import io.realm.kotlin.schema.ValuePropertyType -import io.realm.kotlin.test.mongodb.TEST_APP_FLEX import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.asTestApp import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage import io.realm.kotlin.test.mongodb.common.utils.uploadAllLocalChangesOrFail import io.realm.kotlin.test.mongodb.createUserAndLogIn import io.realm.kotlin.test.mongodb.use +import io.realm.kotlin.test.mongodb.util.DefaultFlexibleSyncAppInitializer +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.platform.PlatformUtils import io.realm.kotlin.test.util.TestChannel import io.realm.kotlin.test.util.TestHelper @@ -130,7 +131,7 @@ class SyncedRealmTests { @BeforeTest fun setup() { partitionValue = TestHelper.randomPartitionValue() - app = TestApp(this::class.simpleName) + app = TestApp(this::class.simpleName, DefaultPartitionBasedAppInitializer) val (email, password) = randomEmail() to "password1234" val user = runBlocking { @@ -720,12 +721,14 @@ class SyncedRealmTests { fun roundtripCollectionsInMixed() = runBlocking { val (email1, password1) = randomEmail() to "password1234" val (email2, password2) = randomEmail() to "password1234" - val app = TestApp(this::class.simpleName, appName = TEST_APP_FLEX) + val app = TestApp(this::class.simpleName, DefaultFlexibleSyncAppInitializer) val user1 = app.createUserAndLogIn(email1, password1) val user2 = app.createUserAndLogIn(email2, password2) // Create object with all types val selector = ObjectId().toString() + var parentId: ObjectId? = null + var childId: ObjectId? = null createFlexibleSyncConfig( user = user1, @@ -737,14 +740,13 @@ class SyncedRealmTests { realm.write { val child = ( JsonStyleRealmObject().apply { - this.id = "CHILD" this.selector = selector } ) + childId = child.id - copyToRealm( + parentId = copyToRealm( JsonStyleRealmObject().apply { - this.id = "PARENT" this.selector = selector value = realmAnyDictionaryOf( "primitive" to 1, @@ -753,7 +755,7 @@ class SyncedRealmTests { "dictionary" to realmAnyDictionaryOf("dictkey1" to 1, "dictkey2" to "Realm", "dictkey3" to child, "dictkey4" to realmAnyListOf(1, 2, 3)) ) } - ) + ).id } realm.syncSession.uploadAllLocalChangesOrFail() } @@ -766,7 +768,7 @@ class SyncedRealmTests { ).let { config -> Realm.open(config).use { realm -> realm.syncSession.downloadAllServerChanges(10.seconds) - val flow = realm.query("_id = $0", "PARENT").asFlow() + val flow = realm.query("_id = $0", parentId).asFlow() val parent = withTimeout(10.seconds) { flow.first { it.list.size >= 1 @@ -778,18 +780,18 @@ class SyncedRealmTests { value["list"]!!.asList().let { assertEquals(1, it[0]!!.asInt()) assertEquals("Realm", it[1]!!.asString()) - assertEquals("CHILD", it[2]!!.asRealmObject().id) + assertEquals(childId, it[2]!!.asRealmObject().id) it[3]!!.asDictionary().let { dict -> assertEquals(1, dict["listkey1"]!!.asInt()) assertEquals("Realm", dict["listkey2"]!!.asString()) - assertEquals("CHILD", dict["listkey3"]!!.asRealmObject().id) + assertEquals(childId, dict["listkey3"]!!.asRealmObject().id) } - assertEquals("CHILD", it[2]!!.asRealmObject().id) + assertEquals(childId, it[2]!!.asRealmObject().id) } value["dictionary"]!!.asDictionary().let { assertEquals(1, it["dictkey1"]!!.asInt()) assertEquals("Realm", it["dictkey2"]!!.asString()) - assertEquals("CHILD", it["dictkey3"]!!.asRealmObject().id) + assertEquals(childId, it["dictkey3"]!!.asRealmObject().id) it["dictkey4"]!!.asList().let { assertEquals(realmAnyListOf(1, 2, 3).asList(), it) } @@ -803,7 +805,7 @@ class SyncedRealmTests { fun collectionsInMixed_asFlow() = runBlocking { val (email1, password1) = randomEmail() to "password1234" val (email2, password2) = randomEmail() to "password1234" - val app = TestApp(this::class.simpleName, appName = TEST_APP_FLEX) + val app = TestApp(this::class.simpleName, DefaultFlexibleSyncAppInitializer) val user1 = app.createUserAndLogIn(email1, password1) val user2 = app.createUserAndLogIn(email2, password2) @@ -1138,7 +1140,7 @@ class SyncedRealmTests { fun writeCopyTo_localToFlexibleSync_throws() = runBlocking { TestApp( this::class.simpleName, - appName = io.realm.kotlin.test.mongodb.TEST_APP_FLEX, + DefaultFlexibleSyncAppInitializer, builder = { it.syncRootDirectory(PlatformUtils.createTempDir("flx-sync-")) } @@ -1217,7 +1219,7 @@ class SyncedRealmTests { fun writeCopyTo_flexibleSyncToLocal() = runBlocking { TestApp( "writeCopyTo_flexibleSyncToLocal", - appName = io.realm.kotlin.test.mongodb.TEST_APP_FLEX, + DefaultFlexibleSyncAppInitializer, builder = { it.syncRootDirectory(PlatformUtils.createTempDir("flx-sync-")) } @@ -1340,7 +1342,7 @@ class SyncedRealmTests { fun writeCopyTo_flexibleSyncToFlexibleSync() = runBlocking { TestApp( "writeCopyTo_flexibleSyncToFlexibleSync", - appName = io.realm.kotlin.test.mongodb.TEST_APP_FLEX, + DefaultFlexibleSyncAppInitializer, builder = { it.syncRootDirectory(PlatformUtils.createTempDir("flx-sync-")) } @@ -1443,7 +1445,7 @@ class SyncedRealmTests { fun accessSessionAfterRemoteChange() = runBlocking { TestApp( "accessSessionAfterRemoteChange", - appName = io.realm.kotlin.test.mongodb.TEST_APP_FLEX, + DefaultFlexibleSyncAppInitializer, builder = { it.syncRootDirectory(PlatformUtils.createTempDir("flx-sync-")) } @@ -1634,7 +1636,7 @@ class SyncedRealmTests { fun createInitialRealmFx() = runBlocking { TestApp( "createInitialRealmFx", - appName = io.realm.kotlin.test.mongodb.TEST_APP_FLEX, + DefaultFlexibleSyncAppInitializer, builder = { it.syncRootDirectory(PlatformUtils.createTempDir("flx-sync-")) } @@ -1689,7 +1691,7 @@ class SyncedRealmTests { @OptIn(ExperimentalKBsonSerializerApi::class) TestApp( "initialRealm_flexibleSync", - appName = io.realm.kotlin.test.mongodb.TEST_APP_FLEX, + DefaultFlexibleSyncAppInitializer, builder = { it.syncRootDirectory(PlatformUtils.createTempDir("flx-sync-")) } diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/UserProfileTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/UserProfileTests.kt index 87e056b344..5c5593f9c6 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/UserProfileTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/UserProfileTests.kt @@ -27,6 +27,7 @@ import io.realm.kotlin.mongodb.ext.profileAsBsonDocument import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.asTestApp import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.SerializationException @@ -102,6 +103,7 @@ class UserProfileTests { fun setUp() { app = TestApp( this::class.simpleName, + DefaultPartitionBasedAppInitializer, networkTransport = object : NetworkTransport { override val authorizationHeaderName: String? get() = "" diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/UserTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/UserTests.kt index e51b042d48..6efbbd81e0 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/UserTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/UserTests.kt @@ -39,6 +39,7 @@ import io.realm.kotlin.test.mongodb.common.mongo.CustomDataType import io.realm.kotlin.test.mongodb.common.mongo.TEST_SERVICE_NAME import io.realm.kotlin.test.mongodb.common.mongo.customEjsonSerializer import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.util.TestHelper import io.realm.kotlin.test.util.TestHelper.randomEmail import kotlinx.serialization.SerialName @@ -71,7 +72,7 @@ class UserTests { @BeforeTest fun setUp() { - app = TestApp(this::class.simpleName) + app = TestApp(this::class.simpleName, DefaultPartitionBasedAppInitializer) } @AfterTest diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/internal/KtorNetworkTransportTest.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/internal/KtorNetworkTransportTest.kt index fcef828d8f..2acb753971 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/internal/KtorNetworkTransportTest.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/internal/KtorNetworkTransportTest.kt @@ -26,11 +26,10 @@ import io.realm.kotlin.internal.util.CoroutineDispatcherFactory import io.realm.kotlin.internal.util.use import io.realm.kotlin.mongodb.internal.KtorNetworkTransport import io.realm.kotlin.test.mongodb.TEST_SERVER_BASE_URL -import io.realm.kotlin.test.mongodb.syncServerAppName +import io.realm.kotlin.test.mongodb.util.AppInitializer import io.realm.kotlin.test.mongodb.util.AppServicesClient import io.realm.kotlin.test.mongodb.util.BaasApp -import io.realm.kotlin.test.mongodb.util.KtorTestAppInitializer.initialize -import io.realm.kotlin.test.mongodb.util.Service +import io.realm.kotlin.test.mongodb.util.Function import io.realm.kotlin.test.util.TestChannel import io.realm.kotlin.test.util.receiveOrFail import io.realm.kotlin.test.util.trySendOrFail @@ -50,6 +49,64 @@ val TEST_METHODS = listOf( HttpMethod.Delete, ) +object KtorTestAppInitializer : AppInitializer { + override suspend fun initialize(client: AppServicesClient, app: BaasApp) { + client.initialize(app, TEST_METHODS) + } + + // Setups the app with the functions and https endpoints required to run the KtorNetworkTransportTests + suspend fun AppServicesClient.initialize(app: BaasApp, methods: List) = + with(app) { + // We have to create a function per method because the request parameter does not + // has what method triggered it. + methods.forEach { httpMethod: HttpMethod -> + val method = httpMethod.value + val function = addFunction( + Function( + name = "test_network_transport_$method", + runAsSystem = true, + source = + """ + exports = async function (request, response) { + response.setHeader('Content-Type', 'text/plain'); + let isSuccess = request.query["success"] == "true"; + + if (isSuccess) { + response.setStatusCode(200); + response.setBody("$method-success"); + } else { + response.setStatusCode(500); + response.setBody("$method-failure"); + } + } + """.trimIndent() + ) + ) + + addEndpoint( + """ + { + "route": "/test_network_transport", + "function_name": "${function.name}", + "function_id": "${function._id}", + "http_method": "$method", + "validation_method": "NO_VALIDATION", + "secret_id": "", + "secret_name": "", + "create_user_on_auth": false, + "fetch_custom_user_data": false, + "respond_result": false, + "disabled": false, + "return_type": "JSON" + } + """.trimIndent() + ) + } + } + + override val name: String = "ktor-test-app" +} + internal class KtorNetworkTransportTest { private lateinit var transport: KtorNetworkTransport private lateinit var endpoint: String @@ -76,9 +133,7 @@ internal class KtorNetworkTransportTest { dispatcher = dispatcher ) appClient.run { - getOrCreateApp(syncServerAppName("ktor")) { app: BaasApp, service: Service -> - initialize(app, TEST_METHODS) - } + getOrCreateApp(KtorTestAppInitializer) } } diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/mongo/MongoClientTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/mongo/MongoClientTests.kt index d2399aaeb0..e27973998f 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/mongo/MongoClientTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/mongo/MongoClientTests.kt @@ -23,9 +23,9 @@ import io.realm.kotlin.mongodb.ext.collection import io.realm.kotlin.mongodb.ext.insertOne import io.realm.kotlin.mongodb.mongo.MongoClient import io.realm.kotlin.mongodb.mongo.realmSerializerModule -import io.realm.kotlin.test.mongodb.TEST_APP_FLEX import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage +import io.realm.kotlin.test.mongodb.util.DefaultFlexibleSyncAppInitializer import kotlinx.serialization.SerializationException import org.mongodb.kbson.ExperimentalKBsonSerializerApi import org.mongodb.kbson.serialization.EJson @@ -47,7 +47,7 @@ class MongoClientTests { fun setUp() { app = TestApp( this::class.simpleName, - appName = TEST_APP_FLEX, + DefaultFlexibleSyncAppInitializer, ) val user = app.createUserAndLogin() client = user.mongoClient(TEST_SERVICE_NAME, eJson = EJson(serializersModule = realmSerializerModule(setOf(CollectionDataType::class)))) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/mongo/MongoCollectionTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/mongo/MongoCollectionTests.kt index d4c38b4969..91ef0aea63 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/mongo/MongoCollectionTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/mongo/MongoCollectionTests.kt @@ -48,12 +48,12 @@ import io.realm.kotlin.mongodb.mongo.realmSerializerModule import io.realm.kotlin.mongodb.sync.SyncConfiguration import io.realm.kotlin.mongodb.syncSession import io.realm.kotlin.notifications.ResultsChange -import io.realm.kotlin.test.mongodb.TEST_APP_FLEX import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.asTestApp import io.realm.kotlin.test.mongodb.common.FLEXIBLE_SYNC_SCHEMA import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage import io.realm.kotlin.test.mongodb.common.utils.retry +import io.realm.kotlin.test.mongodb.util.DefaultFlexibleSyncAppInitializer import io.realm.kotlin.test.util.receiveOrFail import io.realm.kotlin.test.util.use import io.realm.kotlin.types.BaseRealmObject @@ -158,7 +158,7 @@ sealed class MongoCollectionTests { open fun setUp() { app = TestApp( testId = this::class.simpleName, - appName = TEST_APP_FLEX, + DefaultFlexibleSyncAppInitializer, builder = { it.httpLogObfuscator(null) } diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/mongo/MongoDatabaseTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/mongo/MongoDatabaseTests.kt index ed135800ed..fc99981af3 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/mongo/MongoDatabaseTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/mongo/MongoDatabaseTests.kt @@ -21,9 +21,9 @@ import io.realm.kotlin.internal.platform.runBlocking import io.realm.kotlin.mongodb.ext.insertOne import io.realm.kotlin.mongodb.mongo.MongoClient import io.realm.kotlin.mongodb.mongo.MongoDatabase -import io.realm.kotlin.test.mongodb.TEST_APP_FLEX import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage +import io.realm.kotlin.test.mongodb.util.DefaultFlexibleSyncAppInitializer import kotlinx.serialization.SerializationException import org.mongodb.kbson.BsonDocument import org.mongodb.kbson.BsonInt32 @@ -48,7 +48,7 @@ class MongoDatabaseTests { fun setUp() { app = TestApp( this::class.simpleName, - appName = TEST_APP_FLEX, + DefaultFlexibleSyncAppInitializer, ) val user = app.createUserAndLogin() client = user.mongoClient(TEST_SERVICE_NAME) diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/nonlatin/NonLatinTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/nonlatin/NonLatinTests.kt index fd880b43b9..93032998eb 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/nonlatin/NonLatinTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/nonlatin/NonLatinTests.kt @@ -10,6 +10,7 @@ import io.realm.kotlin.test.mongodb.TestApp import io.realm.kotlin.test.mongodb.asTestApp import io.realm.kotlin.test.mongodb.common.PARTITION_BASED_SCHEMA import io.realm.kotlin.test.mongodb.createUserAndLogIn +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.util.TestChannel import io.realm.kotlin.test.util.TestHelper import io.realm.kotlin.test.util.receiveOrFail @@ -37,7 +38,7 @@ class NonLatinTests { fun setup() { partitionValue = TestHelper.randomPartitionValue() @OptIn(ExperimentalKBsonSerializerApi::class) - app = TestApp(this::class.simpleName) + app = TestApp(this::class.simpleName, DefaultPartitionBasedAppInitializer) val (email, password) = TestHelper.randomEmail() to "password1234" user = runBlocking { app.createUserAndLogIn(email, password) diff --git a/packages/test-sync/src/jvmTest/kotlin/io/realm/kotlin/test/mongodb/jvm/RealmTests.kt b/packages/test-sync/src/jvmTest/kotlin/io/realm/kotlin/test/mongodb/jvm/RealmTests.kt index e63ed5fde9..3d0d9e5df5 100644 --- a/packages/test-sync/src/jvmTest/kotlin/io/realm/kotlin/test/mongodb/jvm/RealmTests.kt +++ b/packages/test-sync/src/jvmTest/kotlin/io/realm/kotlin/test/mongodb/jvm/RealmTests.kt @@ -22,6 +22,7 @@ import io.realm.kotlin.entities.sync.ParentPk import io.realm.kotlin.mongodb.Credentials import io.realm.kotlin.mongodb.sync.SyncConfiguration import io.realm.kotlin.test.mongodb.TestApp +import io.realm.kotlin.test.mongodb.util.DefaultPartitionBasedAppInitializer import io.realm.kotlin.test.platform.PlatformUtils import io.realm.kotlin.test.util.TestHelper import kotlinx.coroutines.runBlocking @@ -42,7 +43,7 @@ class RealmTests { @Ignore // See https://github.com/realm/realm-kotlin/issues/1627 fun cleanupAllRealmThreadsOnClose() = runBlocking { @OptIn(ExperimentalKBsonSerializerApi::class) - val app = TestApp("cleanupAllRealmThreadsOnClose") + val app = TestApp("cleanupAllRealmThreadsOnClose", DefaultPartitionBasedAppInitializer) val user = app.login(Credentials.anonymous()) val configuration = SyncConfiguration.create(user, TestHelper.randomPartitionValue(), setOf(ParentPk::class, ChildPk::class)) Realm.open(configuration).close()