Skip to content
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

Should not wait for AblyTest.options in 'context' scope #1023

Merged
merged 2 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 57 additions & 23 deletions Spec/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1735,13 +1735,22 @@ class Auth : QuickSpec {
// RSA8b
context("should support all TokenParams") {

let options = AblyTests.commonAppSetup()
let currentClientId = "client_string"
options.clientId = currentClientId

let rest = ARTRest(options: options)
var options: ARTClientOptions!
var rest: ARTRest!

func setupDependencies() {
if (options == nil) {
options = AblyTests.commonAppSetup()
options.clientId = currentClientId
rest = ARTRest(options: options)
}
}

it("using defaults") {
setupDependencies()
QuintinWillison marked this conversation as resolved.
Show resolved Hide resolved

// Default values
let defaultTokenParams = ARTTokenParams(clientId: currentClientId)
defaultTokenParams.ttl = ARTDefault.ttl() as NSNumber // Set by the server.
Expand All @@ -1762,6 +1771,8 @@ class Auth : QuickSpec {
}

it("overriding defaults") {
setupDependencies()

// Custom values
let expectedTtl = 4800.0
let expectedCapability = "{\"canpublish:*\":[\"publish\"]}"
Expand Down Expand Up @@ -4088,12 +4099,12 @@ class Auth : QuickSpec {
}

context("with invalid credentials") {
options.token = getJWTToken(invalid: true)
options.autoConnect = false
let client = AblyTests.newRealtime(options)
defer { client.dispose(); client.close() }

it("fails to connect with reason 'invalid signature'") {
options.token = getJWTToken(invalid: true)
options.autoConnect = false
let client = AblyTests.newRealtime(options)
defer { client.dispose(); client.close() }

waitUntil(timeout: testTimeout) { done in
client.connection.once(.failed) { stateChange in
guard let reason = stateChange?.reason else {
Expand All @@ -4112,11 +4123,20 @@ class Auth : QuickSpec {
// RSA8g RSA8c
context("when using authUrl") {
let options = AblyTests.clientOptions()
let keys = getKeys()
options.authUrl = URL(string: echoServerAddress)! as URL

var keys: [String: String]!

func setupDependencies() {
if (keys == nil) {
keys = getKeys()
}
}

context("with valid credentials") {
it("fetches a channels and posts a message") {
setupDependencies()

options.authParams = [URLQueryItem]() as [URLQueryItem]?
options.authParams?.append(URLQueryItem(name: "keyName", value: keys["keyName"]) as URLQueryItem)
options.authParams?.append(URLQueryItem(name: "keySecret", value: keys["keySecret"]) as URLQueryItem)
Expand All @@ -4138,6 +4158,8 @@ class Auth : QuickSpec {

context("with wrong credentials") {
it("fails to connect with reason 'invalid signature'") {
setupDependencies()

options.authParams = [URLQueryItem]() as [URLQueryItem]?
options.authParams?.append(URLQueryItem(name: "keyName", value: keys["keyName"]) as URLQueryItem)
options.authParams?.append(URLQueryItem(name: "keySecret", value: "INVALID") as URLQueryItem)
Expand All @@ -4161,6 +4183,8 @@ class Auth : QuickSpec {
context("when token expires") {

it ("receives a 40142 error from the server") {
setupDependencies()

let tokenDuration = 5.0
options.authParams = [URLQueryItem]() as [URLQueryItem]?
options.authParams?.append(URLQueryItem(name: "keyName", value: keys["keyName"]) as URLQueryItem)
Expand All @@ -4185,6 +4209,8 @@ class Auth : QuickSpec {
// RTC8a4
context("when the server sends and AUTH protocol message") {
it("client reauths correctly without going through a disconnection") {
setupDependencies()

// The server sends an AUTH protocol message 30 seconds before a token expires
// We create a token that lasts 35 seconds, so there's room to receive the AUTH message
let tokenDuration = 35.0
Expand Down Expand Up @@ -4372,10 +4398,9 @@ class Auth : QuickSpec {
let options = AblyTests.clientOptions()

context("when the JWT token embeds an Ably token") {
options.tokenDetails = ARTTokenDetails(token: getJWTToken(jwtType: "embedded")!)
let client = ARTRest(options: options)

it ("pulls stats successfully") {
options.tokenDetails = ARTTokenDetails(token: getJWTToken(jwtType: "embedded")!)
let client = ARTRest(options: options)
waitUntil(timeout: testTimeout) { done in
client.stats { stats, error in
expect(error).to(beNil())
Expand All @@ -4386,10 +4411,9 @@ class Auth : QuickSpec {
}

context("when the JWT token embeds an Ably token and it is requested as encrypted") {
options.tokenDetails = ARTTokenDetails(token: getJWTToken(jwtType: "embedded", encrypted: 1)!)
let client = ARTRest(options: options)

it ("pulls stats successfully") {
options.tokenDetails = ARTTokenDetails(token: getJWTToken(jwtType: "embedded", encrypted: 1)!)
let client = ARTRest(options: options)
waitUntil(timeout: testTimeout) { done in
client.stats { stats, error in
expect(error).to(beNil())
Expand All @@ -4401,14 +4425,24 @@ class Auth : QuickSpec {

// RSA4f, RSA8c
context("when the JWT token is returned with application/jwt content type") {
let options = AblyTests.clientOptions()
let keys = getKeys()
options.authUrl = URL(string: echoServerAddress)! as URL
options.authParams = [URLQueryItem]() as [URLQueryItem]?
options.authParams?.append(URLQueryItem(name: "keyName", value: keys["keyName"]) as URLQueryItem)
options.authParams?.append(URLQueryItem(name: "keySecret", value: keys["keySecret"]) as URLQueryItem)
options.authParams?.append(URLQueryItem(name: "returnType", value: "jwt") as URLQueryItem)
let client = ARTRest(options: options)
var client: ARTRest!

func setupDependencies() {
if (client == nil) {
let options = AblyTests.clientOptions()
let keys = getKeys()
options.authUrl = URL(string: echoServerAddress)! as URL
options.authParams = [URLQueryItem]() as [URLQueryItem]?
options.authParams?.append(URLQueryItem(name: "keyName", value: keys["keyName"]) as URLQueryItem)
options.authParams?.append(URLQueryItem(name: "keySecret", value: keys["keySecret"]) as URLQueryItem)
options.authParams?.append(URLQueryItem(name: "returnType", value: "jwt") as URLQueryItem)
client = ARTRest(options: options)
}
}

beforeEach {
setupDependencies()
}

it("the client successfully connects and pulls stats") {
waitUntil(timeout: testTimeout) { done in
Expand Down
13 changes: 10 additions & 3 deletions Spec/RealtimeClientChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2195,15 +2195,22 @@ class RealtimeClientChannel: QuickSpec {

// RTL6c4
context("will result in an error if the") {
let options = AblyTests.commonAppSetup()
options.suspendedRetryTimeout = 0.3
options.autoConnect = false
var options: ARTClientOptions!
var client: ARTRealtime!
var channel: ARTRealtimeChannel!

let previousConnectionStateTtl = ARTDefault.connectionStateTtl()

func setupDependencies() {
if (options == nil) {
options = AblyTests.commonAppSetup()
options.suspendedRetryTimeout = 0.3
options.autoConnect = false
}
}

beforeEach {
setupDependencies()
ARTDefault.setConnectionStateTtl(0.3)
client = AblyTests.newRealtime(options)
channel = client.channels.get("test")
Expand Down
30 changes: 21 additions & 9 deletions Spec/RealtimeClientConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2833,15 +2833,15 @@ class RealtimeClientConnection: QuickSpec {

// RTN15g RTN15g1
context("when connection (ttl + idle interval) period has passed since last activity") {
let options = AblyTests.commonAppSetup()
// We want this to be > than the sum of customTtlInterval and customIdleInterval
options.disconnectedRetryTimeout = 5.0
var client: ARTRealtime!
var connectionId = ""
let customTtlInterval: TimeInterval = 0.1
let customIdleInterval: TimeInterval = 0.1

it("uses a new connection") {
let options = AblyTests.commonAppSetup()
// We want this to be > than the sum of customTtlInterval and customIdleInterval
options.disconnectedRetryTimeout = 5.0 + customTtlInterval + customIdleInterval
client = AblyTests.newRealtime(options)
client.internal.shouldImmediatelyReconnect = false
client.connect()
Expand Down Expand Up @@ -2872,6 +2872,9 @@ class RealtimeClientConnection: QuickSpec {
}
// RTN15g3
it("reattaches to the same channels after a new connection has been established") {
let options = AblyTests.commonAppSetup()
// We want this to be > than the sum of customTtlInterval and customIdleInterval
options.disconnectedRetryTimeout = 5.0
client = AblyTests.newRealtime(options)
client.internal.shouldImmediatelyReconnect = false
defer { client.close() }
Expand Down Expand Up @@ -2909,11 +2912,11 @@ class RealtimeClientConnection: QuickSpec {

// RTN15g2
context("when connection (ttl + idle interval) period has NOT passed since last activity") {
let options = AblyTests.commonAppSetup()
var client: ARTRealtime!
var connectionId = ""

it("uses the same connection") {
let options = AblyTests.commonAppSetup()
client = AblyTests.newRealtime(options)
client.connect()
defer { client.close() }
Expand Down Expand Up @@ -4600,13 +4603,21 @@ class RealtimeClientConnection: QuickSpec {
}
}

let jsonOptions = AblyTests.commonAppSetup()
jsonOptions.useBinaryProtocol = false
// Keep the same key and channel prefix
let msgpackOptions = jsonOptions.copy() as! ARTClientOptions
msgpackOptions.useBinaryProtocol = true
var jsonOptions: ARTClientOptions!
var msgpackOptions: ARTClientOptions!

func setupDependencies() {
if (jsonOptions == nil) {
jsonOptions = AblyTests.commonAppSetup()
jsonOptions.useBinaryProtocol = false
// Keep the same key and channel prefix
msgpackOptions = jsonOptions.copy() as! ARTClientOptions
msgpackOptions.useBinaryProtocol = true
}
}

it("should send messages through raw JSON POST and retrieve equal messages through MsgPack and JSON") {
setupDependencies()
let restPublishClient = ARTRest(options: jsonOptions)
let realtimeSubscribeClientMsgPack = AblyTests.newRealtime(msgpackOptions)
let realtimeSubscribeClientJSON = AblyTests.newRealtime(jsonOptions)
Expand Down Expand Up @@ -4657,6 +4668,7 @@ class RealtimeClientConnection: QuickSpec {
}

it("should send messages through MsgPack and JSON and retrieve equal messages through raw JSON GET") {
setupDependencies()
let restPublishClientMsgPack = ARTRest(options: msgpackOptions)
let restPublishClientJSON = ARTRest(options: jsonOptions)
let restRetrieveClient = ARTRest(options: jsonOptions)
Expand Down