Skip to content

Commit

Permalink
Merge pull request #350 from ably/rsa8f3
Browse files Browse the repository at this point in the history
RSA8f3
  • Loading branch information
ricardopereira committed Apr 8, 2016
2 parents 614995b + 73c6c60 commit 07b9e10
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
7 changes: 1 addition & 6 deletions Source/ARTAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,7 @@ - (void)setProtocolClientId:(NSString *)clientId {

- (NSString *)getClientId {
if (_protocolClientId) {
// Check wildcard
if ([_protocolClientId isEqual:@"*"])
// Any client
return nil;
else
return _protocolClientId;
return _protocolClientId;
}
else if (self.tokenDetails) {
// Check wildcard
Expand Down
42 changes: 41 additions & 1 deletion Spec/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,19 @@ class Auth : QuickSpec {
it("identity should be anonymous for all operations") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
let realtime = ARTRealtime(options: options)
let realtime = AblyTests.newRealtime(options)
defer { realtime.close() }
expect(realtime.auth.clientId).to(beNil())

let transport = realtime.transport as! TestProxyTransport
transport.beforeProcessingReceivedMessage = { message in
if message.action == .Connected {
if let details = message.connectionDetails {
details.clientId = nil
}
}
}

waitUntil(timeout: testTimeout) { done in
realtime.connection.once(.Connected) { stateChange in
expect(stateChange!.reason).to(beNil())
Expand Down Expand Up @@ -908,6 +917,37 @@ class Auth : QuickSpec {

}

// RSA8f3
it("ensure the message published with a wildcard '*' does not have a clientId") {
let options = AblyTests.commonAppSetup()
// Request a token with a wildcard '*' value clientId
options.token = getTestToken(clientId: "*")
let rest = ARTRest(options: options)
rest.httpExecutor = mockExecutor
let channel = rest.channels.get("test")

waitUntil(timeout: testTimeout) { done in
let message = ARTMessage(name: nil, data: "no client")
expect(message.clientId).to(beNil())
channel.publish([message]) { error in
expect(error).to(beNil())
switch extractBodyAsMessages(mockExecutor.requests.first) {
case .Failure(let error):
fail(error)
case .Success(let httpBody):
expect(httpBody.unbox.first!["clientId"]).to(beNil())
}
channel.history { page, error in
expect(error).to(beNil())
expect(page!.items).to(haveCount(1))
expect((page!.items[0] as! ARTMessage).clientId).to(beNil())
done()
}
}
}
expect(rest.auth.clientId).to(beNil())
}

struct ExpectedTokenParams {
static let clientId = "client_from_params"
static let ttl = 5.0
Expand Down
17 changes: 17 additions & 0 deletions Spec/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,23 @@ func extractBodyAsJSON(request: NSMutableURLRequest?) -> Result<NSDictionary> {
return Result.Success(Box(httpBody))
}

func extractBodyAsMessages(request: NSMutableURLRequest?) -> Result<[NSDictionary]> {
guard let request = request
else { return Result(error: "No request found") }

guard let bodyData = request.HTTPBody
else { return Result(error: "No HTTPBody") }

guard let json = try? NSJSONSerialization.JSONObjectWithData(bodyData, options: .MutableLeaves)
else { return Result(error: "Invalid json") }

guard let httpBody = json as? NSArray
else { return Result(error: "HTTPBody has invalid format") }

return Result.Success(Box(httpBody.map{$0 as! NSDictionary}))
}


/// Records each request and response for test purpose.
@objc
class MockHTTPExecutor: NSObject, ARTHTTPExecutor {
Expand Down

0 comments on commit 07b9e10

Please sign in to comment.