-
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
82 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,41 @@ | ||
package com.ably.chat | ||
|
||
import io.ably.lib.realtime.AblyRealtime | ||
import io.ably.lib.realtime.ChannelState | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
import io.ably.lib.types.ClientOptions as PubSubClientOptions | ||
|
||
class SandboxTest { | ||
|
||
lateinit var sandboxApiKey: String | ||
|
||
@Before | ||
fun setUp() = runTest { | ||
sandboxApiKey = createAPIKey() | ||
} | ||
|
||
@Test | ||
fun basicIntegrationTest() = runTest { | ||
val chatClient = createSandboxChatClient(sandboxApiKey) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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.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) | ||
|
||
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") | ||
|
||
suspend fun createAPIKey(): String { | ||
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" | ||
return body.asJsonObject["keys"].asJsonArray[5].asJsonObject["keyStr"].asString | ||
} |
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