Skip to content

Commit 9ee4157

Browse files
committed
test(acceptance): add 403 auto-retry test
Add tests for subscrib auto-retry in case of 403.
1 parent 77e45e5 commit 9ee4157

File tree

4 files changed

+83
-13
lines changed

4 files changed

+83
-13
lines changed

PubNub/Data/Managers/PNSubscriber.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,8 @@ - (void)handleFailedSubscriptionStatus:(PNSubscribeStatus *)status {
13301330
statusCategory == PNTLSConnectionFailedCategory) {
13311331

13321332
__weak __typeof(self) weakSelf = self;
1333-
((PNStatus *)status).automaticallyRetry = (statusCategory != PNMalformedFilterExpressionCategory &&
1333+
((PNStatus *)status).automaticallyRetry = (statusCategory != PNAccessDeniedCategory &&
1334+
statusCategory != PNMalformedFilterExpressionCategory &&
13341335
statusCategory != PNRequestURITooLongCategory);
13351336
PNSubscriberState subscriberState = PNAccessRightsErrorSubscriberState;
13361337
((PNStatus *)status).retryCancelBlock = ^{

Tests/Tests/Contract/Steps/Subscribe/PNSubscribeContractTestSteps.m

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ @implementation PNSubscribeContractTestSteps
1515
- (void)setup {
1616
[self startCucumberHookEventsListening];
1717

18-
Given(@"the crypto keyset", ^(NSArray<NSString *> *args, NSDictionary *userInfo) {
19-
self.configuration.cipherKey = @"enigma";
20-
});
21-
22-
Given(@"the invalid-crypto keyset", ^(NSArray<NSString *> *args, NSDictionary *userInfo) {
23-
self.configuration.cipherKey = @"secret";
24-
});
25-
2618
When(@"I subscribe", ^(NSArray<NSString *> *args, NSDictionary *userInfo) {
2719
self.testedFeatureType = PNSubscribeOperation;
2820

@@ -55,6 +47,12 @@ - (void)setup {
5547

5648
[self pauseMainQueueFor:0.5f];
5749
});
50+
51+
Match(@[@"*"], @"I don't auto-retry subscribe", ^(NSArray<NSString *> *args, NSDictionary *userInfo) {
52+
self.allowsPendingRequests = YES;
53+
// Delay execution to ensure that there is no automated retry.
54+
[self pauseMainQueueFor:2.f];
55+
});
5856
}
5957

6058
#pragma mark -

Tests/Tests/Helpers/PNContractTestCase.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ NS_ASSUME_NONNULL_BEGIN
3333
*/
3434
@property (nonatomic, assign) PNOperationType testedFeatureType;
3535

36+
/**
37+
* @brief Whether at the end of the test on expectation check it is allowed to have pending requests
38+
* or not.
39+
*/
40+
@property (nonatomic, assign, class) BOOL allowsPendingRequests;
41+
@property (nonatomic, assign) BOOL allowsPendingRequests;
42+
3643

3744
#pragma mark - Initialization & Configuration
3845

Tests/Tests/Helpers/PNContractTestCase.m

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#pragma mark Types & Constants
1919

20+
static BOOL _allowsPendingRequests = NO;
21+
2022
/**
2123
* @brief Origin which should be used to reach mock server for contract testing.
2224
*/
@@ -159,6 +161,22 @@ @implementation PNContractTestCase
159161

160162
#pragma mark - Information
161163

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+
162180
- (PNConfiguration *)configuration {
163181
if (!self.currentConfiguration) {
164182
self.currentConfiguration = [PNConfiguration configurationWithPublishKey:kPNDefaultPublishKey
@@ -245,8 +263,12 @@ - (void)setup {
245263
error:nil];
246264

247265
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:@", "]);
250272
}
251273
}
252274

@@ -262,6 +284,26 @@ - (void)setup {
262284
// Nothing to do. Mock server will simulate proper error here.
263285
});
264286

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+
265307
Then(@"I receive successful response", ^(NSArray<NSString *> *args, NSDictionary *userInfo) {
266308
PNStatus *status = [self lastStatus];
267309
PNResult *result = [self lastResult];
@@ -291,6 +333,24 @@ - (void)setup {
291333
}
292334
});
293335

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+
294354
// Complete known contract steps configuration.
295355
[[PNAccessContractTestSteps new] setup];
296356
[[PNFilesContractTestSteps new] setup];
@@ -317,7 +377,10 @@ - (void)subscribeClient:(PubNub *)client
317377
client = client ?: self.client;
318378

319379
[_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+
321384
subscribeCompletedInTime = YES;
322385
dispatch_semaphore_signal(semaphore);
323386
}
@@ -379,7 +442,8 @@ - (void)unsubscribeClient:(PubNub *)client
379442
NSString *clientIdentifier = receiver.currentConfiguration.uuid;
380443

381444
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;
383447
}
384448

385449
if (receivedRequiredCount) {

0 commit comments

Comments
 (0)