aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Auth/Tests/FIRAuthNotificationManagerTests.m
blob: c980eac8b03f11fced7e5e6351efc1a43001a1f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import <XCTest/XCTest.h>

#import "FIRAuthAppCredential.h"
#import "FIRAuthAppCredentialManager.h"
#import "FIRAuthNotificationManager.h"
#import <OCMock/OCMock.h>

NS_ASSUME_NONNULL_BEGIN

/** @var kReceipt
    @brief A fake receipt used for testing.
 */
static NSString *const kReceipt = @"FAKE_RECEIPT";

/** @var kSecret
    @brief A fake secret used for testing.
 */
static NSString *const kSecret = @"FAKE_SECRET";

/** @class FIRAuthFakeForwardingDelegate
    @brief The base class for a fake UIApplicationDelegate that forwards remote notifications.
 */
@interface FIRAuthFakeForwardingDelegate : NSObject<UIApplicationDelegate>

/** @property notificationManager
    @brief The notification manager to forward.
 */
@property(nonatomic, strong) FIRAuthNotificationManager *notificationManager;

/** @property forwardsNotification
    @brief Whether notifications are being forwarded.
 */
@property(nonatomic, assign) BOOL forwardsNotification;

/** @property notificationReceived
    @brief Whether a notification has been received.
 */
@property(nonatomic, assign) BOOL notificationReceived;

/** @property notificationhandled
    @brief Whether a notification has been handled.
 */
@property(nonatomic, assign) BOOL notificationhandled;

@end
@implementation FIRAuthFakeForwardingDelegate
@end

/** @class FIRAuthFakeForwardingDelegate
    @brief A fake UIApplicationDelegate that implements the modern deegate method to receive
        notification.
 */
@interface FIRAuthModernForwardingDelegate : FIRAuthFakeForwardingDelegate
@end
@implementation FIRAuthModernForwardingDelegate

- (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
          fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  self.notificationReceived = YES;
  if (self.forwardsNotification) {
    self.notificationhandled = [self.notificationManager canHandleNotification:userInfo];
  }
}

@end

/** @class FIRAuthLegacyForwardingDelegate
    @brief A fake UIApplicationDelegate that implements the legacy deegate method to receive
        notification.
 */
@interface FIRAuthLegacyForwardingDelegate : FIRAuthFakeForwardingDelegate
@end
@implementation FIRAuthLegacyForwardingDelegate

- (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo {
  self.notificationReceived = YES;
  if (self.forwardsNotification) {
    self.notificationhandled = [self.notificationManager canHandleNotification:userInfo];
  }
}

@end

/** @class FIRAuthNotificationManagerTests
    @brief Unit tests for @c FIRAuthNotificationManager .
 */
@interface FIRAuthNotificationManagerTests : XCTestCase
@end
@implementation FIRAuthNotificationManagerTests {
  /** @var _mockApplication
      @brief The mock UIApplication for testing.
   */
  id _mockApplication;

  /** @var _mockAppCredentialManager
      @brief The mock FIRAuthAppCredentialManager for testing.
   */
  id _mockAppCredentialManager;

  /** @var _notificationManager
      @brief The FIRAuthNotificationManager to be tested.
   */
  FIRAuthNotificationManager *_notificationManager;

  /** @var _modernDelegate
      @brief The modern fake UIApplicationDelegate for testing.
   */
  FIRAuthModernForwardingDelegate *_modernDelegate;

  /** @var _legacyDelegate
      @brief The legacy fake UIApplicationDelegate for testing.
   */
  FIRAuthLegacyForwardingDelegate *_legacyDelegate;
}

- (void)setUp {
  _mockApplication = OCMClassMock([UIApplication class]);
  _mockAppCredentialManager = OCMClassMock([FIRAuthAppCredentialManager class]);
  _notificationManager =
      [[FIRAuthNotificationManager alloc] initWithApplication:_mockApplication
                                         appCredentialManager:_mockAppCredentialManager];
  _modernDelegate = [[FIRAuthModernForwardingDelegate alloc] init];
  _modernDelegate.notificationManager = _notificationManager;
  _legacyDelegate = [[FIRAuthLegacyForwardingDelegate alloc] init];
  _legacyDelegate.notificationManager = _notificationManager;
}

/** @fn testForwardingModernDelegate
    @brief Tests checking notification forwarding on modern fake delegate.
 */
- (void)testForwardingModernDelegate {
  [self verifyForwarding:YES delegate:_modernDelegate];
}

/** @fn testForwardingLegacyDelegate
    @brief Tests checking notification forwarding on legacy fake delegate.
 */
- (void)testForwardingLegacyDelegate {
  [self verifyForwarding:YES delegate:_legacyDelegate];
}

/** @fn testNotForwardingModernDelegate
    @brief Tests checking notification not forwarding on modern fake delegate.
 */
- (void)testNotForwardingModernDelegate {
  [self verifyForwarding:NO delegate:_modernDelegate];
}

/** @fn testNotForwardingLegacyDelegate
    @brief Tests checking notification not forwarding on legacy fake delegate.
 */
- (void)testNotForwardingLegacyDelegate {
  [self verifyForwarding:NO delegate:_legacyDelegate];
}

/** @fn verifyForwarding:delegate:
    @brief Tests checking notification forwarding on a particular delegate.
    @param forwarding Whether the notification is being forwarded or not.
    @param delegate The fake UIApplicationDelegate used for testing.
 */
- (void)verifyForwarding:(BOOL)forwarding
                delegate:(FIRAuthFakeForwardingDelegate *)delegate {
  delegate.forwardsNotification = forwarding;
  OCMStub([_mockApplication delegate]).andReturn(delegate);
  XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  [_notificationManager
      checkNotificationForwardingWithCallback:^(BOOL isNotificationBeingForwarded) {
    XCTAssertEqual(isNotificationBeingForwarded, forwarding);
    [expectation fulfill];
  }];
  XCTAssertFalse(delegate.notificationReceived);
  NSTimeInterval timeout = _notificationManager.timeout * (1.5 - forwarding);
  [self waitForExpectationsWithTimeout:timeout handler:nil];
  XCTAssertTrue(delegate.notificationReceived);
  XCTAssertEqual(delegate.notificationhandled, forwarding);
}

/** @fn testCachedResult
    @brief Test notification forwarding is only checked once.
 */
- (void)testCachedResult {
  FIRAuthFakeForwardingDelegate *delegate = _modernDelegate;
  [self verifyForwarding:NO delegate:delegate];
  delegate.notificationReceived = NO;
  __block BOOL calledBack = NO;
  [_notificationManager
      checkNotificationForwardingWithCallback:^(BOOL isNotificationBeingForwarded) {
    XCTAssertFalse(isNotificationBeingForwarded);
    calledBack = YES;
  }];
  XCTAssertTrue(calledBack);
  XCTAssertFalse(delegate.notificationReceived);
}

/** @fn testMultipleCallbacks
    @brief Test multiple callbacks are handled correctly.
 */
- (void)testMultipleCallbacks {
  FIRAuthFakeForwardingDelegate *delegate = _legacyDelegate;
  delegate.forwardsNotification = YES;
  OCMStub([_mockApplication delegate]).andReturn(delegate);
  XCTestExpectation *expectation1 = [self expectationWithDescription:@"callback1"];
  [_notificationManager
      checkNotificationForwardingWithCallback:^(BOOL isNotificationBeingForwarded) {
    XCTAssertTrue(isNotificationBeingForwarded);
    [expectation1 fulfill];
  }];
  XCTestExpectation *expectation2 = [self expectationWithDescription:@"callback2"];
  [_notificationManager
      checkNotificationForwardingWithCallback:^(BOOL isNotificationBeingForwarded) {
    XCTAssertTrue(isNotificationBeingForwarded);
    [expectation2 fulfill];
  }];
  XCTAssertFalse(delegate.notificationReceived);
  [self waitForExpectationsWithTimeout:_notificationManager.timeout * .5 handler:nil];
  XCTAssertTrue(delegate.notificationReceived);
  XCTAssertTrue(delegate.notificationhandled);
}

/** @fn testPassingToCredentialManager
    @brief Test notification with the right structure is passed to credential manager.
 */
- (void)testPassingToCredentialManager {
  NSDictionary *payload = @{ @"receipt" : kReceipt, @"secret" : kSecret };
  NSDictionary *notification = @{ @"com.google.firebase.auth" : payload };
  OCMExpect([_mockAppCredentialManager canFinishVerificationWithReceipt:kReceipt secret:kSecret])
      .andReturn(YES);
  XCTAssertTrue([_notificationManager canHandleNotification:notification]);
  OCMVerifyAll(_mockAppCredentialManager);

  // JSON string form
  NSData *data = [NSJSONSerialization dataWithJSONObject:payload options:0 error:NULL];
  NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  notification = @{ @"com.google.firebase.auth" : string };
  OCMExpect([_mockAppCredentialManager canFinishVerificationWithReceipt:kReceipt secret:kSecret])
      .andReturn(YES);
  XCTAssertTrue([_notificationManager canHandleNotification:notification]);
  OCMVerifyAll(_mockAppCredentialManager);
}

/** @fn testNotHandling
    @brief Test unrecognized notifications are not handled.
 */
- (void)testNotHandling {
  XCTAssertFalse([_notificationManager canHandleNotification:@{
    @"random" : @"string"
  }]);
  XCTAssertFalse([_notificationManager canHandleNotification:@{
    @"com.google.firebase.auth" : @"something wrong"
  }]);
  XCTAssertFalse([_notificationManager canHandleNotification:@{
    @"com.google.firebase.auth" : @{
      @"receipt" : kReceipt
      // missing secret
    }
  }]);
  XCTAssertFalse([_notificationManager canHandleNotification:@{
    @"com.google.firebase.auth" : @{
      // missing receipt
      @"secret" : kSecret
    }
  }]);
  XCTAssertFalse([_notificationManager canHandleNotification:@{
    @"com.google.firebase.auth" : @{
      // probing notification does not belong to this instance
      @"warning" : @"asdf"
    }
  }]);
}

@end

NS_ASSUME_NONNULL_END