Skip to content

Commit

Permalink
Merge pull request #81 from ably/swift-tests-fix-auth
Browse files Browse the repository at this point in the history
Swift tests: Auth
  • Loading branch information
mattheworiordan committed Nov 24, 2015
2 parents da762fd + 96cf5a1 commit 238ef44
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ably-ios/ARTAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ART_ASSUME_NONNULL_BEGIN

@interface ARTAuth : NSObject

@property (nonatomic, readonly, weak) ARTAuthOptions *options;
@property (nonatomic, readonly, strong) ARTAuthOptions *options;
@property (nonatomic, readonly, assign) ARTAuthMethod method;

@property (nonatomic, weak) ARTLog *logger;
Expand Down
26 changes: 26 additions & 0 deletions ably-ios/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,31 @@
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>ably.io</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionRequiresForwardSecrecy</key>
<false/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
<!--Include to allow HTTP requests-->
<key>NSTemporaryThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
</dict>
</plist>
47 changes: 33 additions & 14 deletions ablySpec/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ class Auth : QuickSpec {
let client = ARTRest(options: AblyTests.setupOptions(AblyTests.jsonRestOptions))
client.httpExecutor = mockExecutor

publishTestMessage(client, failOnError: false)

waitUntil(timeout: testTimeout) { done in
client.channels.get("test").publish("message") { error in
done()
}
}

let key64 = NSString(string: "\(client.options.key!)")
.dataUsingEncoding(NSUTF8StringEncoding)?
.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
Expand Down Expand Up @@ -73,7 +77,11 @@ class Auth : QuickSpec {
let clientHTTP = ARTRest(options: options)
clientHTTP.httpExecutor = mockExecutor

publishTestMessage(clientHTTP, failOnError: false)
waitUntil(timeout: testTimeout) { done in
clientHTTP.channels.get("test").publish("message") { error in
done()
}
}

expect(mockExecutor.requests.first).toNot(beNil(), description: "No request found")
expect(mockExecutor.requests.first?.URL).toNot(beNil(), description: "Request is invalid")
Expand All @@ -87,7 +95,11 @@ class Auth : QuickSpec {
let clientHTTPS = ARTRest(options: options)
clientHTTPS.httpExecutor = mockExecutor

publishTestMessage(clientHTTPS, failOnError: false)
waitUntil(timeout: testTimeout) { done in
clientHTTPS.channels.get("test").publish("message") { error in
done()
}
}

expect(mockExecutor.requests.last).toNot(beNil(), description: "No request found")
expect(mockExecutor.requests.last?.URL).toNot(beNil(), description: "Request is invalid")
Expand All @@ -99,18 +111,22 @@ class Auth : QuickSpec {

// RSA3b
it("should send the token in the Authorization header") {
let options = ARTClientOptions()
let options = AblyTests.clientOptions()
options.token = getTestToken()

let client = ARTRest(options: options)
client.httpExecutor = mockExecutor

publishTestMessage(client, failOnError: false)

waitUntil(timeout: testTimeout) { done in
client.channels.get("test").publish("message") { error in
done()
}
}

expect(client.options.token).toNot(beNil(), description: "No access token")

if let currentToken = client.options.token {
let expectedAuthorization = "Bearer \(currentToken)"
let expectedAuthorization = "Bearer \(encodeBase64(currentToken))"

expect(mockExecutor.requests.first).toNot(beNil(), description: "No request found")

Expand Down Expand Up @@ -231,31 +247,35 @@ class Auth : QuickSpec {
let options = AblyTests.setupOptions(AblyTests.jsonRestOptions)

let client = ARTRest(options: options)

// FIXME: Implemented validation of invalid chars

// Check unquoted
let clientIdQuoted = "\"client_string\""
options.clientId = clientIdQuoted

waitUntil(timeout: 10) { done in
// Token
client.calculateAuthorization(ARTAuthMethod.Token) { token, error in
if let e = error {
XCTFail(e.description)
}
expect(client.auth.clientId).to(equal(clientIdQuoted))
expect(client.auth.clientId).to(beNil())
done()
}
}

// Check unescaped
let clientIdBreaklined = "client_string\n"
options.clientId = clientIdBreaklined

waitUntil(timeout: 10) { done in
// Token
client.calculateAuthorization(ARTAuthMethod.Token) { token, error in
if let e = error {
XCTFail(e.description)
}
expect(client.auth.clientId).to(equal(clientIdBreaklined))
expect(client.auth.clientId).to(beNil())
done()
}
}
Expand Down Expand Up @@ -298,8 +318,7 @@ class Auth : QuickSpec {
let client = ARTRest(options: options)
client.httpExecutor = mockExecutor

waitUntil(timeout: 10) { done in
// Publish message
waitUntil(timeout: testTimeout) { done in
client.channels.get("test").publish("message") { error in
if let e = error {
XCTFail(e.description)
Expand Down Expand Up @@ -327,7 +346,7 @@ class Auth : QuickSpec {
let client = ARTRest(options: options)
client.httpExecutor = mockExecutor

waitUntil(timeout: 10) { done in
waitUntil(timeout: testTimeout) { done in
client.channels.get("test").publish("message") { error in
if let e = error {
XCTFail(e.description)
Expand Down

0 comments on commit 238ef44

Please sign in to comment.