-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ECO-5014] feat: basic sandbox setup
added basic sandbox setup for simple happy-path testing
- Loading branch information
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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 | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters