Skip to content

Commit

Permalink
[ECO-5014] feat: basic sandbox setup
Browse files Browse the repository at this point in the history
added basic sandbox setup for simple happy-path testing
  • Loading branch information
ttypic committed Nov 13, 2024
1 parent 4861812 commit 0b019ad
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
1 change: 1 addition & 0 deletions chat-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies {
testImplementation(libs.junit)
testImplementation(libs.mockk)
testImplementation(libs.coroutine.test)
testImplementation(libs.bundles.ktor.client)
androidTestImplementation(libs.androidx.test.core)
androidTestImplementation(libs.androidx.test.runner)
androidTestImplementation(libs.androidx.junit)
Expand Down
45 changes: 45 additions & 0 deletions chat-android/src/test/java/com/ably/chat/Sandbox.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.ably.chat

import com.google.gson.JsonElement
import com.google.gson.JsonParser
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.request.delete
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
import io.ktor.http.ContentType
import io.ktor.http.contentType

val client = HttpClient(CIO)

class Sandbox {
private lateinit var appId: String

lateinit var apiKey: String
private set

suspend fun setUp() {
val response: HttpResponse = client.post("https://sandbox-rest.ably.io/apps") {
contentType(ContentType.Application.Json)
setBody(loadAppCreationRequestBody().toString())
}
val body = JsonParser.parseString(response.bodyAsText())
// From JS chat repo at 7985ab7 — "The key we need to use is the one at index 5, which gives enough permissions to interact with Chat and Channels"
apiKey = body.asJsonObject["keys"].asJsonArray[5].asJsonObject["keyStr"].asString
appId = body.asJsonObject["appId"].asString
}

suspend fun tearDown() {
client.delete("https://sandbox-rest.ably.io/apps/$appId")
}
}

suspend fun loadAppCreationRequestBody(): JsonElement =
JsonParser.parseString(
client.get("https://raw.githubusercontent.com/ably/ably-common/refs/heads/main/test-resources/test-app-setup.json") {
contentType(ContentType.Application.Json)
}.bodyAsText(),
).asJsonObject.get("post_apps")
47 changes: 47 additions & 0 deletions chat-android/src/test/java/com/ably/chat/SandboxTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.ably.chat

import io.ably.lib.realtime.AblyRealtime
import io.ably.lib.realtime.ChannelState
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import io.ably.lib.types.ClientOptions as PubSubClientOptions

class SandboxTest {

private val sandbox: Sandbox = Sandbox()

@Before
fun setUp() = runTest {
sandbox.setUp()
}

@After
fun tearDown() = runTest {
sandbox.tearDown()
}

@Test
fun basicIntegrationTest() = runTest {
val chatClient = createSandboxChatClient(sandbox.apiKey)
val room = chatClient.rooms.get("basketball")
room.attach()
assertEquals(ChannelState.attached, room.messages.channel.state)
}
}

private fun createSandboxChatClient(apiKey: String): DefaultChatClient {
val realtime = createSandboxRealtime(apiKey)
return DefaultChatClient(realtime, ClientOptions())
}

private fun createSandboxRealtime(chatApiKey: String, chatClientId: String = "sandbox-client"): AblyRealtime =
AblyRealtime(
PubSubClientOptions().apply {
key = chatApiKey
environment = "sandbox"
clientId = chatClientId
},
)
9 changes: 8 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ gson = "2.11.0"
mockk = "1.13.12"
coroutine = "1.8.1"
build-config = "5.4.0"
ktor = "3.0.1"

[libraries]
junit = { group = "junit", name = "junit", version.ref = "junit" }
Expand Down Expand Up @@ -50,9 +51,15 @@ mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" }
coroutine-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutine" }
coroutine-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutine" }

ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-client-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" }

[bundles]
ktor-client = ["ktor-client-core", "ktor-client-cio"]

[plugins]
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt"}
android-kotlin = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
android-kotlin = { id = "org.jetbrains.kotlin.android", version = "2.0.21" }
android-library = { id = "com.android.library", version.ref = "agp" }
android-application = { id = "com.android.application", version.ref = "agp" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
Expand Down

0 comments on commit 0b019ad

Please sign in to comment.