xref: /expo/ios/Tests/Api/EXApiV2Tests.m (revision 5db43c74)
1#import <XCTest/XCTest.h>
2
3#import "EXApiV2Client.h"
4#import "EXApiV2Client+EXRemoteNotifications.h"
5#import "EXClientTestCase.h"
6#import "EXKernel.h"
7#import "EXRemoteNotificationManager.h"
8#import "ExpoKit.h"
9
10@interface EXApiV2Tests : EXClientTestCase
11
12@property (nonatomic, strong) XCTestExpectation *expectToRegisterForRemoteNotifications;
13@property (nonatomic, strong) XCTestExpectation *expectToPostDeviceToken;
14@property (nonatomic, strong) XCTestExpectation *expectToFetchExpoPushToken;
15
16@end
17
18@implementation EXApiV2Tests
19
20- (void)tearDown
21{
22  [[NSNotificationCenter defaultCenter] removeObserver:self];
23}
24
25- (void)testFetchVersions
26{
27  XCTestExpectation *expectToFetch = [[XCTestExpectation alloc] initWithDescription:@"Shared apiv2 client should download the versions endpoint"];
28  [[EXApiV2Client sharedClient] callRemoteMethod:@"versions"
29                                       arguments:nil
30                                      httpMethod:@"GET"
31                               completionHandler:^(EXApiV2Result * _Nullable response, NSError * _Nullable error) {
32                                 XCTAssertNil(error, @"Unexpected error while fetching versions endpoint: %@", error.localizedDescription);
33                                 // versions endpoint doesn't conform to `data` response format, so all we can check here is
34                                 // whether anything was fetched at all
35                                 if (response.httpStatusCode == 200) {
36                                   [expectToFetch fulfill];
37                                 } else {
38                                   XCTAssert(NO, @"EXApiV2Client wasn't able to fetch the versions endpoint");
39                                 }
40                               }];
41  [self waitForExpectations:@[ expectToFetch ] timeout:30.0f];
42}
43
44- (void)testMalformedRequest
45{
46  XCTestExpectation *expectToFail = [[XCTestExpectation alloc] initWithDescription:@"Shared apiv2 client should receive a failed response from a malformed request"];
47  // this endpoint expects an `ids` parameter, omit it on purpose
48  [[EXApiV2Client sharedClient] callRemoteMethod:@"push/getReceipts"
49                                       arguments:nil
50                                      httpMethod:@"GET"
51                               completionHandler:^(EXApiV2Result * _Nullable response, NSError * _Nullable error) {
52                                 XCTAssert(response.httpStatusCode == 400, @"Malformed request expects http 400 response");
53                                 XCTAssertNotNil(response.error, @"Malformed request expects an error");
54                                 [expectToFail fulfill];
55                               }];
56  [self waitForExpectations:@[ expectToFail ] timeout:30.0f];
57}
58
59- (void)testDeviceToken
60{
61  BOOL isDevice = YES;
62#if TARGET_IPHONE_SIMULATOR
63  isDevice = NO;
64#endif
65  if (!isDevice || ![UIApplication sharedApplication].isRegisteredForRemoteNotifications) {
66    // don't try to run this if we don't have permission or aren't on a device.
67  } else {
68    _expectToRegisterForRemoteNotifications = [[XCTestExpectation alloc] initWithDescription:@"App should register for remote notifs"];
69    _expectToPostDeviceToken = [[XCTestExpectation alloc] initWithDescription:@"EXApiV2Client should post the apns device token to the Expo server"];
70    _expectToFetchExpoPushToken = [[XCTestExpectation alloc] initWithDescription:@"EXApiV2Client should fetch the Expo Push token"];
71    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didRegisterForRemoteNotifications:) name:EXAppDidRegisterForRemoteNotificationsNotification object:nil];
72    [[EXKernel sharedInstance].serviceRegistry.remoteNotificationManager registerForRemoteNotifications];
73    [self waitForExpectations:@[ _expectToRegisterForRemoteNotifications, _expectToPostDeviceToken, _expectToFetchExpoPushToken ] timeout:30.0f];
74  }
75}
76
77#pragma mark - notif listeners
78
79- (void)_didRegisterForRemoteNotifications:(NSNotification *)notif
80{
81  [_expectToRegisterForRemoteNotifications fulfill];
82  [[NSNotificationCenter defaultCenter] removeObserver:self name:EXAppDidRegisterForRemoteNotificationsNotification object:nil];
83  NSData *token;
84  if (notif.userInfo && notif.userInfo[@"token"]) {
85    token = notif.userInfo[@"token"];
86    [[EXApiV2Client sharedClient] updateDeviceToken:token completionHandler:^(NSError * _Nullable postError) {
87      XCTAssertNil(postError, @"Unexpected error while posting device token: %@", postError.localizedDescription);
88      [self->_expectToPostDeviceToken fulfill];
89    }];
90    [[EXApiV2Client sharedClient] getExpoPushTokenForExperience:@"@exponent/home"
91                                                   easProjectId:nil
92                                                    deviceToken:token
93                                              completionHandler:^(NSString * _Nullable expoPushToken, NSError * _Nullable error) {
94                                                XCTAssertNil(error, @"Unexpected error while fetching Expo push token: %@", error.localizedDescription);
95                                                XCTAssert([expoPushToken isKindOfClass:[NSString class]], @"Expo Push token should be a string");
96                                                [self->_expectToFetchExpoPushToken fulfill];
97                                              }];
98  }
99}
100
101@end
102