-
Notifications
You must be signed in to change notification settings - Fork 44
uts: add ProxyManager and ProxySession for integration tests
#1210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
141 changes: 141 additions & 0 deletions
141
uts/src/test/kotlin/io/ably/lib/realtime/integration/proxy/AuthReauthTest.kt
This file contains hidden or 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,141 @@ | ||
| package io.ably.lib.realtime.integration.proxy | ||
|
|
||
| import io.ably.lib.awaitState | ||
| import io.ably.lib.pollUntil | ||
| import io.ably.lib.realtime.ConnectionState | ||
| import io.ably.lib.rest.AblyRest | ||
| import io.ably.lib.rest.Auth | ||
| import io.ably.lib.test.helper.ProxyManager | ||
| import io.ably.lib.test.helper.ProxySession | ||
| import io.ably.lib.test.helper.SandboxApp | ||
| import io.ably.lib.test.helper.connectThroughProxy | ||
| import io.ably.lib.uts.infra.TestRealtimeClient | ||
| import kotlinx.coroutines.runBlocking | ||
| import kotlinx.coroutines.test.runTest | ||
| import org.junit.jupiter.api.AfterAll | ||
| import org.junit.jupiter.api.BeforeAll | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.TestInstance | ||
| import java.util.Collections | ||
| import java.util.concurrent.atomic.AtomicInteger | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertNotNull | ||
| import kotlin.test.assertTrue | ||
| import kotlin.time.Duration.Companion.seconds | ||
|
|
||
| /** | ||
| * Proxy integration test against Ably Sandbox endpoint. | ||
| * | ||
| * Uses the programmable proxy (`uts/test/proxy/`) to inject transport-level faults while the | ||
| * SDK communicates with the real Ably backend. See | ||
| * `uts/test/realtime/integration/helpers/proxy.md` for proxy infrastructure details. | ||
| * | ||
| * Spec points: RTN22, RTC8a. | ||
| * Unit-test counterparts: `server_initiated_reauth_test.md` (RTN22), `realtime_authorize.md` (RTC8a). | ||
| */ | ||
| @TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
| class AuthReauthTest { | ||
|
sacOO7 marked this conversation as resolved.
|
||
|
|
||
| private lateinit var app: SandboxApp | ||
|
|
||
| @BeforeAll | ||
| fun setUpAll() = runBlocking { | ||
| ProxyManager.ensureProxy() | ||
| app = SandboxApp.create() | ||
| } | ||
|
|
||
| @AfterAll | ||
| fun tearDownAll() = runBlocking { | ||
| if (::app.isInitialized) app.delete() | ||
| } | ||
|
|
||
| /** | ||
| * @UTS realtime/proxy/RTN22/server-initiated-reauth-0 | ||
| * @UTS realtime/proxy/RTC8a/server-initiated-reauth-0 | ||
| */ | ||
| @Test | ||
| fun `RTN22, RTC8a - server-initiated re-authentication`() = runTest { | ||
| // No proxy rules: the AUTH injection is triggered imperatively after the SDK connects. | ||
| val session = ProxySession.create(rules = emptyList()) | ||
|
|
||
| // Re-authentication is observed via an authCallback. The spec generates a JWT from the | ||
| // sandbox key parts; the idiomatic ably-java equivalent is a locally-signed TokenRequest | ||
| // produced from the same key — no external JWT library required. The realtime client then | ||
| // exchanges it for a token (through the proxy), satisfying RTC8a. | ||
| val tokenSigner = AblyRest(app.defaultKey) | ||
| val authCallbackCount = AtomicInteger(0) | ||
| val authCallback = Auth.TokenCallback { params -> | ||
| authCallbackCount.incrementAndGet() | ||
| tokenSigner.auth.createTokenRequest(params, null) | ||
| } | ||
|
|
||
| // Keep the JSON protocol (ClientOptionsBuilder default): the proxy injects/inspects frames | ||
| // as JSON, so the assertions below read `message.get("action")` from the proxy log. | ||
| val client = TestRealtimeClient { | ||
| this.authCallback = authCallback | ||
| connectThroughProxy(session) | ||
| autoConnect = false | ||
| } | ||
|
|
||
| try { | ||
| // Connect through proxy | ||
| client.connect() | ||
| awaitState(client, ConnectionState.connected, 15.seconds) | ||
|
|
||
| // Record identity and auth state before injection | ||
| val originalConnectionId = client.connection.id | ||
| val originalAuthCallbackCount = authCallbackCount.get() | ||
| assertNotNull(originalConnectionId) | ||
| assertTrue(originalAuthCallbackCount >= 1) | ||
|
|
||
| // Record state changes from this point | ||
| val stateChanges = Collections.synchronizedList(mutableListOf<ConnectionState>()) | ||
| client.connection.on { change -> stateChanges.add(change.current) } | ||
|
|
||
| // Inject a server-initiated AUTH ProtocolMessage (action 17), simulating Ably | ||
| // requesting re-authentication. | ||
| session.triggerAction( | ||
| mapOf("type" to "inject_to_client", "message" to mapOf("action" to 17)), | ||
| ) | ||
|
|
||
| // Wait for the SDK to invoke authCallback again and send its AUTH response. | ||
| // Allow time for the token request round-trip to the sandbox. | ||
| pollUntil { stateChanges.size > 1 } | ||
|
|
||
| // authCallback was called again (re-authentication triggered) | ||
| assertEquals(originalAuthCallbackCount + 1, authCallbackCount.get()) | ||
|
|
||
| // Connection remains CONNECTED (re-auth does not disrupt the connection) | ||
| assertEquals(ConnectionState.connected, client.connection.state) | ||
|
|
||
| // Connection ID is unchanged (no reconnection occurred) | ||
| assertEquals(originalConnectionId, client.connection.id) | ||
|
|
||
| // No state transitions away from CONNECTED occurred | ||
| val nonConnectedChanges = stateChanges.filter { it != ConnectionState.connected } | ||
| assertEquals(0, nonConnectedChanges.size) | ||
|
|
||
| // RTC8a: the client sends an AUTH (action 17) frame carrying the renewed auth details. | ||
| val clientAuthFrames = session.getLog().filter { | ||
| it.type == "ws_frame" && | ||
| it.direction == "client_to_server" && | ||
| it.message?.get("action")?.asInt == 17 && | ||
| it.message.get("auth")?.isJsonNull == false | ||
| } | ||
|
|
||
| assertTrue( | ||
| clientAuthFrames.isNotEmpty(), | ||
| "Expected at least one client-to-server AUTH frame carrying auth details", | ||
| ) | ||
| } finally { | ||
| // Nest teardown so session/tokenSigner are always cleaned up even if close-wait times out. | ||
| try { | ||
| client.close() | ||
| awaitState(client, ConnectionState.closed, 10.seconds) | ||
| } finally { | ||
| session.close() | ||
| runCatching { tokenSigner.close() } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.