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

Swift tests: RSC9 #70

Merged
merged 2 commits into from
Nov 17, 2015
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
1 change: 1 addition & 0 deletions ably-ios/ARTAuth+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ART_ASSUME_NONNULL_BEGIN

// CONNECTED ProtocolMessage may contain a clientId
- (void)setProtocolClientId:(NSString *)clientId;
- (void)setTokenDetails:(ARTAuthTokenDetails *)tokenDetails;

@end

Expand Down
8 changes: 6 additions & 2 deletions ably-ios/ARTAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ - (void)authorise:(ARTAuthTokenParams *)tokenParams options:(ARTAuthOptions *)op
callback(nil, error);
}
} else {
_tokenDetails = tokenDetails;
[self setTokenDetails:tokenDetails];
if (callback) {
callback(tokenDetails, nil);
callback(self.tokenDetails, nil);
}
}
}];
Expand Down Expand Up @@ -262,6 +262,10 @@ - (void)setProtocolClientId:(NSString *)clientId {
_protocolClientId = clientId;
}

- (void)setTokenDetails:(ARTAuthTokenDetails *)tokenDetails {
_tokenDetails = tokenDetails;
}

- (NSString *)getClientId {
if (_protocolClientId) {
// Check wildcard
Expand Down
66 changes: 66 additions & 0 deletions ablySpec/RestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,72 @@ class RestClient: QuickSpec {
}
}

// RSC9
it("should use Auth to manage authentication") {
let options = AblyTests.commonAppSetup()
let client = ARTRest(options: options)
let auth = client.auth

expect(auth.method.rawValue).to(equal(ARTAuthMethod.Basic.rawValue))

waitUntil(timeout: 25.0) { done in
auth.requestToken(nil, withOptions: options, callback: { tokenDetailsA, errorA in
if let e = errorA {
XCTFail(e.description)
done()
}
else if let currentTokenDetails = tokenDetailsA {
auth.setTokenDetails(currentTokenDetails)
}

auth.authorise(nil, options: options, force: false, callback: { tokenDetailsB, errorB in
if let e = errorB {
XCTFail(e.description)
done()
}
// Use the same token because it is valid
expect(auth.tokenDetails?.token).to(equal(tokenDetailsB?.token))
done()
})
})
}
}

// RSC10
it("should request another token after current one is no longer valid") {
let options = AblyTests.commonAppSetup()
let client = ARTRest(options: options)
let auth = client.auth

let tokenParams = ARTAuthTokenParams()
tokenParams.ttl = 3.0 //Seconds

waitUntil(timeout: 25.0) { done in
auth.requestToken(tokenParams, withOptions: options) { tokenDetailsA, errorA in
if let e = errorA {
XCTFail(e.description)
done()
}
else if let currentTokenDetails = tokenDetailsA {
auth.setTokenDetails(currentTokenDetails)
}

// Delay for token expiration
delay(tokenParams.ttl) {
auth.authorise(tokenParams, options: options, force: false) { tokenDetailsB, errorB in
if let e = errorB {
XCTFail(e.description)
done()
}
// Different token
expect(tokenDetailsA?.token).toNot(equal(tokenDetailsB?.token))
done()
}
}
}
}
}

} //RestClient
}
}
9 changes: 9 additions & 0 deletions ablySpec/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ func getTestToken() -> String {
return token ?? ""
}

public func delay(seconds: NSTimeInterval, closure: ()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(seconds * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}

// TODO: after merge use robrix/Box
class Box<T> {
let unbox: T
Expand Down