17
17
18
18
#pragma mark Types & Constants
19
19
20
+ static BOOL _allowsPendingRequests = NO ;
21
+
20
22
/* *
21
23
* @brief Origin which should be used to reach mock server for contract testing.
22
24
*/
@@ -159,6 +161,22 @@ @implementation PNContractTestCase
159
161
160
162
#pragma mark - Information
161
163
164
+ + (BOOL )allowsPendingRequests {
165
+ return _allowsPendingRequests;
166
+ }
167
+
168
+ + (void )setAllowsPendingRequests : (BOOL )allowsPendingRequests {
169
+ _allowsPendingRequests = allowsPendingRequests;
170
+ }
171
+
172
+ - (BOOL )allowsPendingRequests {
173
+ return [PNContractTestCase class ].allowsPendingRequests ;
174
+ }
175
+
176
+ - (void )setAllowsPendingRequests : (BOOL )allowsPendingRequests {
177
+ [PNContractTestCase class ].allowsPendingRequests = allowsPendingRequests;
178
+ }
179
+
162
180
- (PNConfiguration *)configuration {
163
181
if (!self.currentConfiguration ) {
164
182
self.currentConfiguration = [PNConfiguration configurationWithPublishKey: kPNDefaultPublishKey
@@ -245,8 +263,12 @@ - (void)setup {
245
263
error: nil ];
246
264
247
265
NSArray <NSString *> *pendingExpectation = [response valueForKeyPath: @" expectations.pending" ];
248
- if (pendingExpectation.count ) {
249
- XCTAssertTrue (false , @" Expectations not met: %@ " , [pendingExpectation componentsJoinedByString: @" , " ]);
266
+ NSArray <NSString *> *failedExpectation = [response valueForKeyPath: @" expectations.failed" ];
267
+ if (pendingExpectation.count && !self.allowsPendingRequests ) {
268
+ XCTAssertTrue (false , @" Expectations not met (pending): %@ " , [pendingExpectation componentsJoinedByString: @" , " ]);
269
+ }
270
+ if (failedExpectation.count ) {
271
+ XCTAssertTrue (false , @" Expectations not met (failed): %@ " , [failedExpectation componentsJoinedByString: @" , " ]);
250
272
}
251
273
}
252
274
@@ -262,6 +284,26 @@ - (void)setup {
262
284
// Nothing to do. Mock server will simulate proper error here.
263
285
});
264
286
287
+ Given (@" the crypto keyset" , ^(NSArray <NSString *> *args, NSDictionary *userInfo) {
288
+ self.configuration .cipherKey = @" enigma" ;
289
+ });
290
+
291
+ Given (@" the invalid-crypto keyset" , ^(NSArray <NSString *> *args, NSDictionary *userInfo) {
292
+ self.configuration .cipherKey = @" secret" ;
293
+ });
294
+
295
+ Given (@" auth key" , ^(NSArray <NSString *> *args, NSDictionary *userInfo) {
296
+ self.configuration .authKey = @" test-auth-key" ;
297
+ });
298
+
299
+ Given (@" no auth key" , ^(NSArray <NSString *> *args, NSDictionary *userInfo) {
300
+ // Nothing to do. By default PubNub client doesn't have configured authKey.
301
+ });
302
+
303
+ Given (@" token" , ^(NSArray <NSString *> *args, NSDictionary *userInfo) {
304
+ [self .client setAuthToken: @" my-test-token" ];
305
+ });
306
+
265
307
Then (@" I receive successful response" , ^(NSArray <NSString *> *args, NSDictionary *userInfo) {
266
308
PNStatus *status = [self lastStatus ];
267
309
PNResult *result = [self lastResult ];
@@ -291,6 +333,24 @@ - (void)setup {
291
333
}
292
334
});
293
335
336
+ Then (@" I receive access denied status" , ^(NSArray <NSString *> *args, NSDictionary *userInfo) {
337
+ PNStatus *status = [self lastStatus ];
338
+
339
+ XCTAssertNotNil (status, @" Last API call should fail" );
340
+ XCTAssertTrue (status.isError , @" Last API call should report error" );
341
+ XCTAssertEqual (status.category , PNAccessDeniedCategory);
342
+ XCTAssertEqual ([self lastStatus ].statusCode , 403 );
343
+ });
344
+
345
+ Match (@[@" *" ], @" I receive access denied status" , ^(NSArray <NSString *> *args, NSDictionary *userInfo) {
346
+ PNStatus *status = [self lastStatus ];
347
+
348
+ XCTAssertNotNil (status, @" Last API call should fail" );
349
+ XCTAssertTrue (status.isError , @" Last API call should report error" );
350
+ XCTAssertEqual (status.category , PNAccessDeniedCategory);
351
+ XCTAssertEqual ([self lastStatus ].statusCode , 403 );
352
+ });
353
+
294
354
// Complete known contract steps configuration.
295
355
[[PNAccessContractTestSteps new ] setup ];
296
356
[[PNFilesContractTestSteps new ] setup ];
@@ -317,7 +377,10 @@ - (void)subscribeClient:(PubNub *)client
317
377
client = client ?: self.client ;
318
378
319
379
[_statusHandlers addObject: ^void (PubNub *receiver, PNStatus *status) {
320
- if (status.operation == PNSubscribeOperation && status.category == PNConnectedCategory) {
380
+ if (status.operation == PNSubscribeOperation &&
381
+ (status.category == PNConnectedCategory || status.category == PNAccessDeniedCategory)) {
382
+ [self storeRequestStatus: status];
383
+
321
384
subscribeCompletedInTime = YES ;
322
385
dispatch_semaphore_signal (semaphore);
323
386
}
@@ -379,7 +442,8 @@ - (void)unsubscribeClient:(PubNub *)client
379
442
NSString *clientIdentifier = receiver.currentConfiguration .uuid ;
380
443
381
444
if ([client.currentConfiguration.uuid isEqualToString: clientIdentifier]) {
382
- receivedRequiredCount = messagesCount >= [weakSelf messagesCountForClient: receiver onChannel: channel];
445
+ NSUInteger messagesCountForClient = [weakSelf messagesCountForClient: receiver onChannel: channel];
446
+ receivedRequiredCount = messagesCountForClient > 0 && messagesCount >= messagesCountForClient;
383
447
}
384
448
385
449
if (receivedRequiredCount) {
0 commit comments