FIRAppCheckTests.m 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import <OCMock/OCMock.h>
  18. #import "FBLPromise+Testing.h"
  19. #import <FirebaseAppCheck/FirebaseAppCheck.h>
  20. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheck.h"
  21. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckErrors.h"
  22. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckProvider.h"
  23. #import "FirebaseAppCheck/Interop/FIRAppCheckInterop.h"
  24. #import "FirebaseAppCheck/Interop/FIRAppCheckTokenResultInterop.h"
  25. #import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
  26. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckSettings.h"
  27. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckTokenResult.h"
  28. #import "FirebaseAppCheck/Sources/Core/Storage/FIRAppCheckStorage.h"
  29. #import "FirebaseAppCheck/Sources/Core/TokenRefresh/FIRAppCheckTokenRefreshResult.h"
  30. #import "FirebaseAppCheck/Sources/Core/TokenRefresh/FIRAppCheckTokenRefresher.h"
  31. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  32. // Since DeviceCheck is the default attestation provider for AppCheck, disable
  33. // test cases that may be dependent on DeviceCheck being available.
  34. #if FIR_DEVICE_CHECK_SUPPORTED_TARGETS
  35. // The FAC token value returned when an error occurs.
  36. static NSString *const kDummyToken = @"eyJlcnJvciI6IlVOS05PV05fRVJST1IifQ==";
  37. @interface FIRAppCheck (Tests) <FIRAppCheckInterop>
  38. - (instancetype)initWithAppName:(NSString *)appName
  39. appCheckProvider:(id<FIRAppCheckProvider>)appCheckProvider
  40. storage:(id<FIRAppCheckStorageProtocol>)storage
  41. tokenRefresher:(id<FIRAppCheckTokenRefresherProtocol>)tokenRefresher
  42. notificationCenter:(NSNotificationCenter *)notificationCenter
  43. settings:(id<FIRAppCheckSettingsProtocol>)settings;
  44. - (nullable instancetype)initWithApp:(FIRApp *)app;
  45. @end
  46. @interface FIRAppCheckTests : XCTestCase
  47. @property(nonatomic) NSString *appName;
  48. @property(nonatomic) OCMockObject<FIRAppCheckStorageProtocol> *mockStorage;
  49. @property(nonatomic) OCMockObject<FIRAppCheckProvider> *mockAppCheckProvider;
  50. @property(nonatomic) OCMockObject<FIRAppCheckTokenRefresherProtocol> *mockTokenRefresher;
  51. @property(nonatomic) OCMockObject<FIRAppCheckSettingsProtocol> *mockSettings;
  52. @property(nonatomic) NSNotificationCenter *notificationCenter;
  53. @property(nonatomic) FIRAppCheck<FIRAppCheckInterop> *appCheck;
  54. @property(nonatomic, copy, nullable) FIRAppCheckTokenRefreshBlock tokenRefreshHandler;
  55. @end
  56. @implementation FIRAppCheckTests
  57. - (void)setUp {
  58. [super setUp];
  59. self.appName = @"FIRAppCheckTests";
  60. self.mockStorage = OCMProtocolMock(@protocol(FIRAppCheckStorageProtocol));
  61. self.mockAppCheckProvider = OCMProtocolMock(@protocol(FIRAppCheckProvider));
  62. self.mockTokenRefresher = OCMProtocolMock(@protocol(FIRAppCheckTokenRefresherProtocol));
  63. self.mockSettings = OCMProtocolMock(@protocol(FIRAppCheckSettingsProtocol));
  64. self.notificationCenter = [[NSNotificationCenter alloc] init];
  65. [self stubSetTokenRefreshHandler];
  66. self.appCheck = [[FIRAppCheck alloc] initWithAppName:self.appName
  67. appCheckProvider:self.mockAppCheckProvider
  68. storage:self.mockStorage
  69. tokenRefresher:self.mockTokenRefresher
  70. notificationCenter:self.notificationCenter
  71. settings:self.mockSettings];
  72. }
  73. - (void)tearDown {
  74. self.appCheck = nil;
  75. [self.mockAppCheckProvider stopMocking];
  76. self.mockAppCheckProvider = nil;
  77. [self.mockStorage stopMocking];
  78. self.mockStorage = nil;
  79. [self.mockTokenRefresher stopMocking];
  80. self.mockTokenRefresher = nil;
  81. [super tearDown];
  82. }
  83. - (void)testInitWithApp {
  84. NSString *googleAppID = @"testInitWithApp_googleAppID";
  85. NSString *appName = @"testInitWithApp_appName";
  86. NSString *appGroupID = @"testInitWithApp_appGroupID";
  87. // 1. Stub FIRApp and validate usage.
  88. id mockApp = OCMStrictClassMock([FIRApp class]);
  89. id mockAppOptions = OCMStrictClassMock([FIROptions class]);
  90. OCMStub([mockApp name]).andReturn(appName);
  91. OCMStub([(FIRApp *)mockApp options]).andReturn(mockAppOptions);
  92. OCMExpect([mockAppOptions googleAppID]).andReturn(googleAppID);
  93. OCMExpect([mockAppOptions appGroupID]).andReturn(appGroupID);
  94. // 2. Stub FIRAppCheckTokenRefresher and validate usage.
  95. id mockTokenRefresher = OCMClassMock([FIRAppCheckTokenRefresher class]);
  96. OCMExpect([mockTokenRefresher alloc]).andReturn(mockTokenRefresher);
  97. id refresherDateValidator =
  98. [OCMArg checkWithBlock:^BOOL(FIRAppCheckTokenRefreshResult *refreshResult) {
  99. XCTAssertEqual(refreshResult.status, FIRAppCheckTokenRefreshStatusNever);
  100. XCTAssertEqual(refreshResult.tokenExpirationDate, nil);
  101. XCTAssertEqual(refreshResult.tokenReceivedAtDate, nil);
  102. return YES;
  103. }];
  104. id settingsValidator = [OCMArg checkWithBlock:^BOOL(id obj) {
  105. XCTAssert([obj isKindOfClass:[FIRAppCheckSettings class]]);
  106. return YES;
  107. }];
  108. OCMExpect([mockTokenRefresher initWithRefreshResult:refresherDateValidator
  109. settings:settingsValidator])
  110. .andReturn(mockTokenRefresher);
  111. OCMExpect([mockTokenRefresher setTokenRefreshHandler:[OCMArg any]]);
  112. // 3. Stub FIRAppCheckStorage and validate usage.
  113. id mockStorage = OCMClassMock([FIRAppCheckStorage class]);
  114. OCMExpect([mockStorage alloc]).andReturn(mockStorage);
  115. OCMExpect([mockStorage initWithAppName:appName appID:googleAppID accessGroup:appGroupID])
  116. .andReturn(mockStorage);
  117. // 4. Stub attestation provider.
  118. OCMockObject<FIRAppCheckProviderFactory> *mockProviderFactory =
  119. OCMProtocolMock(@protocol(FIRAppCheckProviderFactory));
  120. OCMockObject<FIRAppCheckProvider> *mockProvider = OCMProtocolMock(@protocol(FIRAppCheckProvider));
  121. OCMExpect([mockProviderFactory createProviderWithApp:mockApp]).andReturn(mockProvider);
  122. [FIRAppCheck setAppCheckProviderFactory:mockProviderFactory];
  123. // 5. Call init.
  124. FIRAppCheck *appCheck = [[FIRAppCheck alloc] initWithApp:mockApp];
  125. XCTAssert([appCheck isKindOfClass:[FIRAppCheck class]]);
  126. // 6. Verify mocks.
  127. OCMVerifyAll(mockApp);
  128. OCMVerifyAll(mockAppOptions);
  129. OCMVerifyAll(mockTokenRefresher);
  130. OCMVerifyAll(mockStorage);
  131. OCMVerifyAll(mockProviderFactory);
  132. OCMVerifyAll(mockProvider);
  133. // 7. Stop mocking real class mocks.
  134. [mockApp stopMocking];
  135. mockApp = nil;
  136. [mockAppOptions stopMocking];
  137. mockAppOptions = nil;
  138. [mockTokenRefresher stopMocking];
  139. mockTokenRefresher = nil;
  140. [mockStorage stopMocking];
  141. mockStorage = nil;
  142. }
  143. - (void)testAppCheckDefaultInstance {
  144. // Should throw an exception when the default app is not configured.
  145. XCTAssertThrows([FIRAppCheck appCheck]);
  146. // Configure default FIRApp.
  147. FIROptions *options =
  148. [[FIROptions alloc] initWithGoogleAppID:@"1:100000000000:ios:aaaaaaaaaaaaaaaaaaaaaaaa"
  149. GCMSenderID:@"sender_id"];
  150. options.APIKey = @"api_key";
  151. options.projectID = @"project_id";
  152. [FIRApp configureWithOptions:options];
  153. // Check.
  154. XCTAssertNotNil([FIRAppCheck appCheck]);
  155. [FIRApp resetApps];
  156. }
  157. - (void)testAppCheckInstanceForApp {
  158. FIROptions *options =
  159. [[FIROptions alloc] initWithGoogleAppID:@"1:100000000000:ios:aaaaaaaaaaaaaaaaaaaaaaaa"
  160. GCMSenderID:@"sender_id"];
  161. options.APIKey = @"api_key";
  162. options.projectID = @"project_id";
  163. [FIRApp configureWithName:@"testAppCheckInstanceForApp" options:options];
  164. FIRApp *app = [FIRApp appNamed:@"testAppCheckInstanceForApp"];
  165. XCTAssertNotNil(app);
  166. XCTAssertNotNil([FIRAppCheck appCheckWithApp:app]);
  167. [FIRApp resetApps];
  168. }
  169. #pragma mark - Public Get Token
  170. - (void)testGetToken_WhenNoCache_Success {
  171. // 1. Create expected token and configure expectations.
  172. FIRAppCheckToken *expectedToken = [self validToken];
  173. NSArray * /*[tokenNotification, getToken]*/ expectations =
  174. [self configuredExpectations_GetTokenWhenNoCache_withExpectedToken:expectedToken];
  175. // 2. Request token and verify result.
  176. [self.appCheck
  177. tokenForcingRefresh:NO
  178. completion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  179. [expectations.lastObject fulfill];
  180. XCTAssertNotNil(token);
  181. XCTAssertEqualObjects(token.token, expectedToken.token);
  182. XCTAssertNil(error);
  183. }];
  184. // 3. Wait for expectations and validate mocks.
  185. [self waitForExpectations:expectations timeout:0.5];
  186. [self verifyAllMocks];
  187. }
  188. - (void)testGetToken_WhenCachedTokenIsValid_Success {
  189. [self assertGetToken_WhenCachedTokenIsValid_Success];
  190. }
  191. - (void)testGetTokenForcingRefresh_WhenCachedTokenIsValid_Success {
  192. // 1. Create expected token and configure expectations.
  193. FIRAppCheckToken *expectedToken = [self validToken];
  194. NSArray * /*[tokenNotification, getToken]*/ expectations =
  195. [self configuredExpectations_GetTokenForcingRefreshWhenCacheIsValid_withExpectedToken:
  196. expectedToken];
  197. // 2. Request token and verify result.
  198. [self.appCheck
  199. tokenForcingRefresh:YES
  200. completion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  201. [expectations.lastObject fulfill];
  202. XCTAssertNotNil(token);
  203. XCTAssertEqualObjects(token.token, expectedToken.token);
  204. XCTAssertNil(error);
  205. }];
  206. // 3. Wait for expectations and validate mocks.
  207. [self waitForExpectations:expectations timeout:0.5];
  208. [self verifyAllMocks];
  209. }
  210. - (void)testGetToken_WhenCachedTokenExpired_Success {
  211. // 1. Create expected token and configure expectations.
  212. FIRAppCheckToken *expectedToken = [self validToken];
  213. NSArray * /*[tokenNotification, getToken]*/ expectations =
  214. [self configuredExpectations_GetTokenWhenCachedTokenExpired_withExpectedToken:expectedToken];
  215. // 2. Request token and verify result.
  216. [self.appCheck
  217. tokenForcingRefresh:NO
  218. completion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  219. [expectations.lastObject fulfill];
  220. XCTAssertNotNil(token);
  221. XCTAssertEqualObjects(token.token, expectedToken.token);
  222. XCTAssertNil(error);
  223. }];
  224. // 3. Wait for expectations and validate mocks.
  225. [self waitForExpectations:expectations timeout:0.5];
  226. [self verifyAllMocks];
  227. }
  228. - (void)testGetToken_AppCheckProviderError {
  229. // 1. Create expected token and error and configure expectations.
  230. FIRAppCheckToken *cachedToken = [self soonExpiringToken];
  231. NSError *providerError = [NSError errorWithDomain:@"FIRAppCheckTests" code:-1 userInfo:nil];
  232. NSArray * /*[tokenNotification, getToken]*/ expectations =
  233. [self configuredExpectations_GetTokenWhenError_withError:providerError andToken:cachedToken];
  234. // 2. Request token and verify result.
  235. [self.appCheck
  236. tokenForcingRefresh:NO
  237. completion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  238. [expectations.lastObject fulfill];
  239. XCTAssertNil(token);
  240. XCTAssertNotNil(error);
  241. XCTAssertNotEqualObjects(error, providerError);
  242. XCTAssertEqualObjects(error.domain, FIRAppCheckErrorDomain);
  243. XCTAssertEqualObjects(error.userInfo[NSUnderlyingErrorKey], providerError);
  244. }];
  245. // 3. Wait for expectations and validate mocks.
  246. [self waitForExpectations:expectations timeout:0.5];
  247. [self verifyAllMocks];
  248. }
  249. - (void)testGetToken_ServerUnreachableError {
  250. // 1. Create expected error and configure expectations.
  251. NSError *serverError = [FIRAppCheckErrorUtil APIErrorWithNetworkError:[self internalError]];
  252. NSArray * /*[tokenNotification, getToken]*/ expectations =
  253. [self configuredExpectations_GetTokenWhenError_withError:serverError andToken:nil];
  254. // 2. Request token and verify result.
  255. [self.appCheck
  256. tokenForcingRefresh:NO
  257. completion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  258. [expectations.lastObject fulfill];
  259. XCTAssertNil(token);
  260. XCTAssertNotNil(error);
  261. XCTAssertEqualObjects(error, serverError);
  262. XCTAssertEqualObjects(error.domain, FIRAppCheckErrorDomain);
  263. }];
  264. // 3. Wait for expectations and validate mocks.
  265. [self waitForExpectations:expectations timeout:0.5];
  266. [self verifyAllMocks];
  267. }
  268. - (void)testGetToken_KeychainError {
  269. // 1. Expect token to be requested from storage.
  270. NSError *keychainError = [FIRAppCheckErrorUtil keychainErrorWithError:[self internalError]];
  271. OCMExpect([self.mockStorage getToken]).andReturn([FBLPromise resolvedWith:keychainError]);
  272. // 2. Expect token requested from app check provider.
  273. FIRAppCheckToken *expectedToken = [self validToken];
  274. id completionArg = [OCMArg invokeBlockWithArgs:expectedToken, [NSNull null], nil];
  275. OCMExpect([self.mockAppCheckProvider getTokenWithCompletion:completionArg]);
  276. // 3. Expect new token to be stored.
  277. OCMExpect([self.mockStorage setToken:expectedToken])
  278. .andReturn([FBLPromise resolvedWith:keychainError]);
  279. // 4. Don't expect token update notification to be sent.
  280. XCTestExpectation *notificationExpectation = [self tokenUpdateNotificationWithExpectedToken:@""
  281. isInverted:YES];
  282. // 5. Request token and verify result.
  283. XCTestExpectation *getTokenExpectation = [self expectationWithDescription:@"getToken"];
  284. [self.appCheck
  285. tokenForcingRefresh:NO
  286. completion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  287. [getTokenExpectation fulfill];
  288. XCTAssertNil(token);
  289. XCTAssertNotNil(error);
  290. XCTAssertEqualObjects(error, keychainError);
  291. XCTAssertEqualObjects(error.domain, FIRAppCheckErrorDomain);
  292. }];
  293. // 3. Wait for expectations and validate mocks.
  294. [self waitForExpectations:@[ notificationExpectation, getTokenExpectation ] timeout:0.5];
  295. [self verifyAllMocks];
  296. }
  297. - (void)testGetToken_UnsupportedError {
  298. // 1. Create expected error and configure expectations.
  299. NSError *providerError =
  300. [FIRAppCheckErrorUtil unsupportedAttestationProvider:@"AppAttestProvider"];
  301. NSArray * /*[tokenNotification, getToken]*/ expectations =
  302. [self configuredExpectations_GetTokenWhenError_withError:providerError andToken:nil];
  303. // 2. Request token and verify result.
  304. [self.appCheck
  305. tokenForcingRefresh:NO
  306. completion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  307. [expectations.lastObject fulfill];
  308. XCTAssertNil(token);
  309. XCTAssertNotNil(error);
  310. XCTAssertEqualObjects(error, providerError);
  311. XCTAssertEqualObjects(error.domain, FIRAppCheckErrorDomain);
  312. }];
  313. // 3. Wait for expectations and validate mocks.
  314. [self waitForExpectations:expectations timeout:0.5];
  315. [self verifyAllMocks];
  316. }
  317. #pragma mark - FIRAppCheckInterop Get Token
  318. - (void)testInteropGetToken_WhenNoCache_Success {
  319. // 1. Create expected token and configure expectations.
  320. FIRAppCheckToken *expectedToken = [self validToken];
  321. NSArray * /*[tokenNotification, getToken]*/ expectations =
  322. [self configuredExpectations_GetTokenWhenNoCache_withExpectedToken:expectedToken];
  323. // 2. Request token and verify result.
  324. [self.appCheck getTokenForcingRefresh:NO
  325. completion:^(id<FIRAppCheckTokenResultInterop> tokenResult) {
  326. [expectations.lastObject fulfill];
  327. XCTAssertNotNil(tokenResult);
  328. XCTAssertEqualObjects(tokenResult.token, expectedToken.token);
  329. XCTAssertNil(tokenResult.error);
  330. }];
  331. // 3. Wait for expectations and validate mocks.
  332. [self waitForExpectations:expectations timeout:0.5];
  333. [self verifyAllMocks];
  334. }
  335. - (void)testInteropGetToken_WhenCachedTokenIsValid_Success {
  336. [self assertInteropGetToken_WhenCachedTokenIsValid_Success];
  337. }
  338. - (void)testInteropGetTokenForcingRefresh_WhenCachedTokenIsValid_Success {
  339. // 1. Create expected token and configure expectations.
  340. FIRAppCheckToken *expectedToken = [self validToken];
  341. NSArray * /*[tokenNotification, getToken]*/ expectations =
  342. [self configuredExpectations_GetTokenForcingRefreshWhenCacheIsValid_withExpectedToken:
  343. expectedToken];
  344. // 2. Request token and verify result.
  345. [self.appCheck getTokenForcingRefresh:YES
  346. completion:^(id<FIRAppCheckTokenResultInterop> tokenResult) {
  347. [expectations.lastObject fulfill];
  348. XCTAssertNotNil(tokenResult);
  349. XCTAssertEqualObjects(tokenResult.token, expectedToken.token);
  350. XCTAssertNil(tokenResult.error);
  351. }];
  352. // 3. Wait for expectations and validate mocks.
  353. [self waitForExpectations:expectations timeout:0.5];
  354. [self verifyAllMocks];
  355. }
  356. - (void)testInteropGetToken_WhenCachedTokenExpired_Success {
  357. // 1. Create expected token and configure expectations.
  358. FIRAppCheckToken *expectedToken = [self validToken];
  359. NSArray * /*[tokenNotification, getToken]*/ expectations =
  360. [self configuredExpectations_GetTokenWhenCachedTokenExpired_withExpectedToken:expectedToken];
  361. // 2. Request token and verify result.
  362. [self.appCheck getTokenForcingRefresh:NO
  363. completion:^(id<FIRAppCheckTokenResultInterop> tokenResult) {
  364. [expectations.lastObject fulfill];
  365. XCTAssertNotNil(tokenResult);
  366. XCTAssertEqualObjects(tokenResult.token, expectedToken.token);
  367. XCTAssertNil(tokenResult.error);
  368. }];
  369. // 3. Wait for expectations and validate mocks.
  370. [self waitForExpectations:expectations timeout:0.5];
  371. [self verifyAllMocks];
  372. }
  373. - (void)testInteropGetToken_AppCheckProviderError {
  374. // 1. Create expected tokens and errors and configure expectations.
  375. FIRAppCheckToken *cachedToken = [self soonExpiringToken];
  376. NSError *providerError = [NSError errorWithDomain:@"FIRAppCheckTests" code:-1 userInfo:nil];
  377. NSArray * /*[tokenNotification, getToken]*/ expectations =
  378. [self configuredExpectations_GetTokenWhenError_withError:providerError andToken:cachedToken];
  379. // 2. Request token and verify result.
  380. [self.appCheck
  381. getTokenForcingRefresh:NO
  382. completion:^(id<FIRAppCheckTokenResultInterop> result) {
  383. [expectations.lastObject fulfill];
  384. XCTAssertNotNil(result);
  385. XCTAssertEqualObjects(result.token, kDummyToken);
  386. XCTAssertEqualObjects(result.error, providerError);
  387. // Interop API does not wrap errors in public domain.
  388. XCTAssertNotEqualObjects(result.error.domain, FIRAppCheckErrorDomain);
  389. }];
  390. // 3. Wait for expectations and validate mocks.
  391. [self waitForExpectations:expectations timeout:0.5];
  392. [self verifyAllMocks];
  393. }
  394. #pragma mark - Token refresher
  395. - (void)testTokenRefreshTriggeredAndRefreshSuccess {
  396. // 1. Expect token to be requested from storage.
  397. OCMExpect([self.mockStorage getToken]).andReturn([FBLPromise resolvedWith:nil]);
  398. // 2. Expect token requested from app check provider.
  399. NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:10000];
  400. FIRAppCheckToken *tokenToReturn = [[FIRAppCheckToken alloc] initWithToken:@"valid"
  401. expirationDate:expirationDate];
  402. id completionArg = [OCMArg invokeBlockWithArgs:tokenToReturn, [NSNull null], nil];
  403. OCMExpect([self.mockAppCheckProvider getTokenWithCompletion:completionArg]);
  404. // 3. Expect new token to be stored.
  405. OCMExpect([self.mockStorage setToken:tokenToReturn])
  406. .andReturn([FBLPromise resolvedWith:tokenToReturn]);
  407. // 4. Expect token update notification to be sent.
  408. XCTestExpectation *notificationExpectation =
  409. [self tokenUpdateNotificationWithExpectedToken:tokenToReturn.token];
  410. // 5. Trigger refresh and expect the result.
  411. if (self.tokenRefreshHandler == nil) {
  412. XCTFail(@"`tokenRefreshHandler` must be not `nil`.");
  413. return;
  414. }
  415. XCTestExpectation *completionExpectation = [self expectationWithDescription:@"completion"];
  416. self.tokenRefreshHandler(^(FIRAppCheckTokenRefreshResult *refreshResult) {
  417. [completionExpectation fulfill];
  418. XCTAssertEqualObjects(refreshResult.tokenExpirationDate, expirationDate);
  419. XCTAssertEqual(refreshResult.status, FIRAppCheckTokenRefreshStatusSuccess);
  420. });
  421. [self waitForExpectations:@[ notificationExpectation, completionExpectation ] timeout:0.5];
  422. [self verifyAllMocks];
  423. }
  424. - (void)testTokenRefreshTriggeredAndRefreshError {
  425. // 1. Expect token to be requested from storage.
  426. OCMExpect([self.mockStorage getToken]).andReturn([FBLPromise resolvedWith:nil]);
  427. // 2. Expect token requested from app check provider.
  428. NSError *providerError = [self internalError];
  429. id completionArg = [OCMArg invokeBlockWithArgs:[NSNull null], providerError, nil];
  430. OCMExpect([self.mockAppCheckProvider getTokenWithCompletion:completionArg]);
  431. // 3. Don't expect token requested from app check provider.
  432. OCMReject([self.mockAppCheckProvider getTokenWithCompletion:[OCMArg any]]);
  433. // 4. Don't expect token update notification to be sent.
  434. XCTestExpectation *notificationExpectation = [self tokenUpdateNotificationWithExpectedToken:@""
  435. isInverted:YES];
  436. // 5. Trigger refresh and expect the result.
  437. if (self.tokenRefreshHandler == nil) {
  438. XCTFail(@"`tokenRefreshHandler` must be not `nil`.");
  439. return;
  440. }
  441. XCTestExpectation *completionExpectation = [self expectationWithDescription:@"completion"];
  442. self.tokenRefreshHandler(^(FIRAppCheckTokenRefreshResult *refreshResult) {
  443. [completionExpectation fulfill];
  444. XCTAssertEqual(refreshResult.status, FIRAppCheckTokenRefreshStatusFailure);
  445. XCTAssertNil(refreshResult.tokenExpirationDate);
  446. XCTAssertNil(refreshResult.tokenReceivedAtDate);
  447. });
  448. [self waitForExpectations:@[ notificationExpectation, completionExpectation ] timeout:0.5];
  449. [self verifyAllMocks];
  450. }
  451. #pragma mark - Token update notifications
  452. - (void)testTokenUpdateNotificationKeys {
  453. XCTAssertEqualObjects([self.appCheck tokenDidChangeNotificationName],
  454. @"FIRAppCheckAppCheckTokenDidChangeNotification");
  455. XCTAssertEqualObjects([self.appCheck notificationAppNameKey],
  456. @"FIRAppCheckAppNameNotificationKey");
  457. XCTAssertEqualObjects([self.appCheck notificationTokenKey], @"FIRAppCheckTokenNotificationKey");
  458. }
  459. #pragma mark - Auto-refresh enabled
  460. - (void)testIsTokenAutoRefreshEnabled {
  461. // Expect value from settings to be used.
  462. [[[self.mockSettings expect] andReturnValue:@(NO)] isTokenAutoRefreshEnabled];
  463. XCTAssertFalse(self.appCheck.isTokenAutoRefreshEnabled);
  464. [[[self.mockSettings expect] andReturnValue:@(YES)] isTokenAutoRefreshEnabled];
  465. XCTAssertTrue(self.appCheck.isTokenAutoRefreshEnabled);
  466. OCMVerifyAll(self.mockSettings);
  467. }
  468. - (void)testSetIsTokenAutoRefreshEnabled {
  469. OCMExpect([self.mockSettings setIsTokenAutoRefreshEnabled:YES]);
  470. self.appCheck.isTokenAutoRefreshEnabled = YES;
  471. OCMExpect([self.mockSettings setIsTokenAutoRefreshEnabled:NO]);
  472. self.appCheck.isTokenAutoRefreshEnabled = NO;
  473. OCMVerifyAll(self.mockSettings);
  474. }
  475. #pragma mark - Merging multiple get token requests
  476. - (void)testGetToken_WhenCalledSeveralTimesSuccess_ThenThereIsOnlyOneOperation {
  477. // 1. Expect a token to be requested and stored.
  478. NSArray * /*[expectedToken, storeTokenPromise]*/ expectedTokenAndPromise =
  479. [self expectTokenRequestFromAppCheckProvider];
  480. FIRAppCheckToken *expectedToken = expectedTokenAndPromise.firstObject;
  481. FBLPromise *storeTokenPromise = expectedTokenAndPromise.lastObject;
  482. // 2. Expect token update notification to be sent.
  483. XCTestExpectation *notificationExpectation =
  484. [self tokenUpdateNotificationWithExpectedToken:expectedToken.token];
  485. // 3. Request token several times.
  486. NSInteger getTokenCallsCount = 10;
  487. NSMutableArray *getTokenCompletionExpectations =
  488. [NSMutableArray arrayWithCapacity:getTokenCallsCount];
  489. for (NSInteger i = 0; i < getTokenCallsCount; i++) {
  490. // 3.1. Expect a completion to be called for each method call.
  491. XCTestExpectation *getTokenExpectation =
  492. [self expectationWithDescription:[NSString stringWithFormat:@"getToken%@", @(i)]];
  493. [getTokenCompletionExpectations addObject:getTokenExpectation];
  494. // 3.2. Request token and verify result.
  495. [self.appCheck
  496. tokenForcingRefresh:NO
  497. completion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  498. [getTokenExpectation fulfill];
  499. XCTAssertNotNil(token);
  500. XCTAssertEqualObjects(token.token, expectedToken.token);
  501. XCTAssertNil(error);
  502. }];
  503. }
  504. // 3.3. Fulfill the pending promise to finish the get token operation.
  505. [storeTokenPromise fulfill:expectedToken];
  506. // 4. Wait for expectations and validate mocks.
  507. NSArray *expectations =
  508. [getTokenCompletionExpectations arrayByAddingObject:notificationExpectation];
  509. [self waitForExpectations:expectations timeout:0.5];
  510. [self verifyAllMocks];
  511. // 5. Check a get token call after.
  512. [self assertGetToken_WhenCachedTokenIsValid_Success];
  513. }
  514. - (void)testGetToken_WhenCalledSeveralTimesError_ThenThereIsOnlyOneOperation {
  515. // 1. Expect a token to be requested and stored.
  516. NSArray * /*[expectedToken, storeTokenPromise]*/ expectedTokenAndPromise =
  517. [self expectTokenRequestFromAppCheckProvider];
  518. FIRAppCheckToken *expectedToken = expectedTokenAndPromise.firstObject;
  519. FBLPromise *storeTokenPromise = expectedTokenAndPromise.lastObject;
  520. // 1.1. Create an expected error to be rejected with later.
  521. NSError *storageError = [NSError errorWithDomain:self.name code:0 userInfo:nil];
  522. // 2. Don't expect token update notification to be sent.
  523. XCTestExpectation *notificationExpectation =
  524. [self tokenUpdateNotificationWithExpectedToken:expectedToken.token isInverted:YES];
  525. // 3. Request token several times.
  526. NSInteger getTokenCallsCount = 10;
  527. NSMutableArray *getTokenCompletionExpectations =
  528. [NSMutableArray arrayWithCapacity:getTokenCallsCount];
  529. for (NSInteger i = 0; i < getTokenCallsCount; i++) {
  530. // 3.1. Expect a completion to be called for each method call.
  531. XCTestExpectation *getTokenExpectation =
  532. [self expectationWithDescription:[NSString stringWithFormat:@"getToken%@", @(i)]];
  533. [getTokenCompletionExpectations addObject:getTokenExpectation];
  534. // 3.2. Request token and verify result.
  535. [self.appCheck
  536. tokenForcingRefresh:NO
  537. completion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  538. [getTokenExpectation fulfill];
  539. XCTAssertNil(token);
  540. XCTAssertNotNil(error);
  541. XCTAssertNotEqualObjects(error, storageError);
  542. XCTAssertEqualObjects(error.domain, FIRAppCheckErrorDomain);
  543. XCTAssertEqualObjects(error.userInfo[NSUnderlyingErrorKey], storageError);
  544. }];
  545. }
  546. // 3.3. Reject the pending promise to finish the get token operation.
  547. [storeTokenPromise reject:storageError];
  548. // 4. Wait for expectations and validate mocks.
  549. NSArray *expectations =
  550. [getTokenCompletionExpectations arrayByAddingObject:notificationExpectation];
  551. [self waitForExpectations:expectations timeout:0.5];
  552. [self verifyAllMocks];
  553. // 5. Check a get token call after.
  554. [self assertGetToken_WhenCachedTokenIsValid_Success];
  555. }
  556. - (void)testInteropGetToken_WhenCalledSeveralTimesSuccess_ThenThereIsOnlyOneOperation {
  557. // 1. Expect a token to be requested and stored.
  558. NSArray * /*[expectedToken, storeTokenPromise]*/ expectedTokenAndPromise =
  559. [self expectTokenRequestFromAppCheckProvider];
  560. FIRAppCheckToken *expectedToken = expectedTokenAndPromise.firstObject;
  561. FBLPromise *storeTokenPromise = expectedTokenAndPromise.lastObject;
  562. // 2. Expect token update notification to be sent.
  563. XCTestExpectation *notificationExpectation =
  564. [self tokenUpdateNotificationWithExpectedToken:expectedToken.token];
  565. // 3. Request token several times.
  566. NSInteger getTokenCallsCount = 10;
  567. NSMutableArray *getTokenCompletionExpectations =
  568. [NSMutableArray arrayWithCapacity:getTokenCallsCount];
  569. for (NSInteger i = 0; i < getTokenCallsCount; i++) {
  570. // 3.1. Expect a completion to be called for each method call.
  571. XCTestExpectation *getTokenExpectation =
  572. [self expectationWithDescription:[NSString stringWithFormat:@"getToken%@", @(i)]];
  573. [getTokenCompletionExpectations addObject:getTokenExpectation];
  574. // 3.2. Request token and verify result.
  575. [self.appCheck getTokenForcingRefresh:NO
  576. completion:^(id<FIRAppCheckTokenResultInterop> tokenResult) {
  577. [getTokenExpectation fulfill];
  578. XCTAssertNotNil(tokenResult);
  579. XCTAssertEqualObjects(tokenResult.token, expectedToken.token);
  580. XCTAssertNil(tokenResult.error);
  581. }];
  582. }
  583. // 3.3. Fulfill the pending promise to finish the get token operation.
  584. [storeTokenPromise fulfill:expectedToken];
  585. // 4. Wait for expectations and validate mocks.
  586. NSArray *expectations =
  587. [getTokenCompletionExpectations arrayByAddingObject:notificationExpectation];
  588. [self waitForExpectations:expectations timeout:0.5];
  589. [self verifyAllMocks];
  590. // 5. Check a get token call after.
  591. [self assertInteropGetToken_WhenCachedTokenIsValid_Success];
  592. }
  593. - (void)testInteropGetToken_WhenCalledSeveralTimesError_ThenThereIsOnlyOneOperation {
  594. // 1. Expect a token to be requested and stored.
  595. NSArray * /*[expectedToken, storeTokenPromise]*/ expectedTokenAndPromise =
  596. [self expectTokenRequestFromAppCheckProvider];
  597. FIRAppCheckToken *expectedToken = expectedTokenAndPromise.firstObject;
  598. FBLPromise *storeTokenPromise = expectedTokenAndPromise.lastObject;
  599. // 1.1. Create an expected error to be reject the store token promise with later.
  600. NSError *storageError = [NSError errorWithDomain:self.name code:0 userInfo:nil];
  601. // 2. Don't expect token update notification to be sent.
  602. XCTestExpectation *notificationExpectation =
  603. [self tokenUpdateNotificationWithExpectedToken:expectedToken.token isInverted:YES];
  604. // 3. Request token several times.
  605. NSInteger getTokenCallsCount = 10;
  606. NSMutableArray *getTokenCompletionExpectations =
  607. [NSMutableArray arrayWithCapacity:getTokenCallsCount];
  608. for (NSInteger i = 0; i < getTokenCallsCount; i++) {
  609. // 3.1. Expect a completion to be called for each method call.
  610. XCTestExpectation *getTokenExpectation =
  611. [self expectationWithDescription:[NSString stringWithFormat:@"getToken%@", @(i)]];
  612. [getTokenCompletionExpectations addObject:getTokenExpectation];
  613. // 3.2. Request token and verify result.
  614. [self.appCheck getTokenForcingRefresh:NO
  615. completion:^(id<FIRAppCheckTokenResultInterop> tokenResult) {
  616. [getTokenExpectation fulfill];
  617. XCTAssertNotNil(tokenResult);
  618. XCTAssertEqualObjects(tokenResult.error, storageError);
  619. XCTAssertEqualObjects(tokenResult.token, kDummyToken);
  620. }];
  621. }
  622. // 3.3. Reject the pending promise to finish the get token operation.
  623. [storeTokenPromise reject:storageError];
  624. // 4. Wait for expectations and validate mocks.
  625. NSArray *expectations =
  626. [getTokenCompletionExpectations arrayByAddingObject:notificationExpectation];
  627. [self waitForExpectations:expectations timeout:0.5];
  628. [self verifyAllMocks];
  629. // 5. Check a get token call after.
  630. [self assertInteropGetToken_WhenCachedTokenIsValid_Success];
  631. }
  632. #pragma mark - Helpers
  633. - (NSError *)internalError {
  634. return [NSError errorWithDomain:@"com.internal.error" code:-1 userInfo:nil];
  635. }
  636. - (FIRAppCheckToken *)validToken {
  637. return [[FIRAppCheckToken alloc] initWithToken:[NSUUID UUID].UUIDString
  638. expirationDate:[NSDate distantFuture]];
  639. }
  640. - (FIRAppCheckToken *)soonExpiringToken {
  641. NSDate *soonExpiringTokenDate = [NSDate dateWithTimeIntervalSinceNow:4.5 * 60];
  642. return [[FIRAppCheckToken alloc] initWithToken:@"valid" expirationDate:soonExpiringTokenDate];
  643. }
  644. - (void)stubSetTokenRefreshHandler {
  645. id arg = [OCMArg checkWithBlock:^BOOL(id handler) {
  646. self.tokenRefreshHandler = handler;
  647. return YES;
  648. }];
  649. OCMExpect([self.mockTokenRefresher setTokenRefreshHandler:arg]);
  650. }
  651. - (XCTestExpectation *)tokenUpdateNotificationWithExpectedToken:(NSString *)expectedToken {
  652. return [self tokenUpdateNotificationWithExpectedToken:expectedToken isInverted:NO];
  653. }
  654. - (XCTestExpectation *)tokenUpdateNotificationWithExpectedToken:(NSString *)expectedToken
  655. isInverted:(BOOL)isInverted {
  656. XCTestExpectation *expectation =
  657. [self expectationForNotification:[self.appCheck tokenDidChangeNotificationName]
  658. object:nil
  659. notificationCenter:self.notificationCenter
  660. handler:^BOOL(NSNotification *_Nonnull notification) {
  661. XCTAssertEqualObjects(
  662. notification.userInfo[[self.appCheck notificationAppNameKey]],
  663. self.appName);
  664. XCTAssertEqualObjects(
  665. notification.userInfo[[self.appCheck notificationTokenKey]],
  666. expectedToken);
  667. XCTAssertEqualObjects(notification.object, self.appCheck);
  668. return YES;
  669. }];
  670. expectation.inverted = isInverted;
  671. return expectation;
  672. }
  673. - (void)assertGetToken_WhenCachedTokenIsValid_Success {
  674. // 1. Create expected token and configure expectations.
  675. FIRAppCheckToken *cachedToken = [self validToken];
  676. NSArray * /*[tokenNotification, getToken]*/ expectations =
  677. [self configuredExpectations_GetTokenWhenCacheTokenIsValid_withExpectedToken:cachedToken];
  678. // 2. Request token and verify result.
  679. [self.appCheck
  680. tokenForcingRefresh:NO
  681. completion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  682. [expectations.lastObject fulfill];
  683. XCTAssertNotNil(token);
  684. XCTAssertEqualObjects(token.token, cachedToken.token);
  685. XCTAssertNil(error);
  686. }];
  687. // 3. Wait for expectations and validate mocks.
  688. [self waitForExpectations:expectations timeout:0.5];
  689. [self verifyAllMocks];
  690. }
  691. - (void)assertInteropGetToken_WhenCachedTokenIsValid_Success {
  692. // 1. Create expected token and configure expectations.
  693. FIRAppCheckToken *cachedToken = [self validToken];
  694. NSArray * /*[tokenNotification, getToken]*/ expectations =
  695. [self configuredExpectations_GetTokenWhenCacheTokenIsValid_withExpectedToken:cachedToken];
  696. // 2. Request token and verify result.
  697. [self.appCheck getTokenForcingRefresh:NO
  698. completion:^(id<FIRAppCheckTokenResultInterop> tokenResult) {
  699. [expectations.lastObject fulfill];
  700. XCTAssertNotNil(tokenResult);
  701. XCTAssertEqualObjects(tokenResult.token, cachedToken.token);
  702. XCTAssertNil(tokenResult.error);
  703. }];
  704. // 3. Wait for expectations and validate mocks.
  705. [self waitForExpectations:expectations timeout:0.5];
  706. [self verifyAllMocks];
  707. }
  708. - (NSArray<XCTestExpectation *> *)configuredExpectations_GetTokenWhenNoCache_withExpectedToken:
  709. (FIRAppCheckToken *)expectedToken {
  710. // 1. Expect token to be requested from storage.
  711. OCMExpect([self.mockStorage getToken]).andReturn([FBLPromise resolvedWith:nil]);
  712. // 2. Expect token requested from app check provider.
  713. id completionArg = [OCMArg invokeBlockWithArgs:expectedToken, [NSNull null], nil];
  714. OCMExpect([self.mockAppCheckProvider getTokenWithCompletion:completionArg]);
  715. // 3. Expect new token to be stored.
  716. OCMExpect([self.mockStorage setToken:expectedToken])
  717. .andReturn([FBLPromise resolvedWith:expectedToken]);
  718. // 4. Expect token update notification to be sent.
  719. XCTestExpectation *tokenNotificationExpectation =
  720. [self tokenUpdateNotificationWithExpectedToken:expectedToken.token];
  721. // 5. Expect token request to be completed.
  722. XCTestExpectation *getTokenExpectation = [self expectationWithDescription:@"getToken"];
  723. return @[ tokenNotificationExpectation, getTokenExpectation ];
  724. }
  725. - (NSArray<XCTestExpectation *> *)
  726. configuredExpectations_GetTokenWhenCacheTokenIsValid_withExpectedToken:
  727. (FIRAppCheckToken *)expectedToken {
  728. // 1. Expect token to be requested from storage.
  729. OCMExpect([self.mockStorage getToken]).andReturn([FBLPromise resolvedWith:expectedToken]);
  730. // 2. Don't expect token requested from app check provider.
  731. OCMReject([self.mockAppCheckProvider getTokenWithCompletion:[OCMArg any]]);
  732. // 3. Don't expect token update notification to be sent.
  733. XCTestExpectation *tokenNotificationExpectation =
  734. [self tokenUpdateNotificationWithExpectedToken:@"" isInverted:YES];
  735. // 4. Expect token request to be completed.
  736. XCTestExpectation *getTokenExpectation = [self expectationWithDescription:@"getToken"];
  737. return @[ tokenNotificationExpectation, getTokenExpectation ];
  738. }
  739. - (NSArray<XCTestExpectation *> *)
  740. configuredExpectations_GetTokenForcingRefreshWhenCacheIsValid_withExpectedToken:
  741. (FIRAppCheckToken *)expectedToken {
  742. // 1. Don't expect token to be requested from storage.
  743. OCMReject([self.mockStorage getToken]);
  744. // 2. Expect token requested from app check provider.
  745. id completionArg = [OCMArg invokeBlockWithArgs:expectedToken, [NSNull null], nil];
  746. OCMExpect([self.mockAppCheckProvider getTokenWithCompletion:completionArg]);
  747. // 3. Expect new token to be stored.
  748. OCMExpect([self.mockStorage setToken:expectedToken])
  749. .andReturn([FBLPromise resolvedWith:expectedToken]);
  750. // 4. Expect token update notification to be sent.
  751. XCTestExpectation *notificationExpectation =
  752. [self tokenUpdateNotificationWithExpectedToken:expectedToken.token];
  753. // 5. Expect token request to be completed.
  754. XCTestExpectation *getTokenExpectation = [self expectationWithDescription:@"getToken"];
  755. return @[ notificationExpectation, getTokenExpectation ];
  756. }
  757. - (NSArray<XCTestExpectation *> *)
  758. configuredExpectations_GetTokenWhenCachedTokenExpired_withExpectedToken:
  759. (FIRAppCheckToken *)expectedToken {
  760. // 1. Expect token to be requested from storage.
  761. FIRAppCheckToken *cachedToken = [[FIRAppCheckToken alloc] initWithToken:@"expired"
  762. expirationDate:[NSDate date]];
  763. OCMExpect([self.mockStorage getToken]).andReturn([FBLPromise resolvedWith:cachedToken]);
  764. // 2. Expect token requested from app check provider.
  765. id completionArg = [OCMArg invokeBlockWithArgs:expectedToken, [NSNull null], nil];
  766. OCMExpect([self.mockAppCheckProvider getTokenWithCompletion:completionArg]);
  767. // 3. Expect new token to be stored.
  768. OCMExpect([self.mockStorage setToken:expectedToken])
  769. .andReturn([FBLPromise resolvedWith:expectedToken]);
  770. // 4. Expect token update notification to be sent.
  771. XCTestExpectation *notificationExpectation =
  772. [self tokenUpdateNotificationWithExpectedToken:expectedToken.token];
  773. // 5. Expect token request to be completed.
  774. XCTestExpectation *getTokenExpectation = [self expectationWithDescription:@"getToken"];
  775. return @[ notificationExpectation, getTokenExpectation ];
  776. }
  777. - (NSArray<XCTestExpectation *> *)
  778. configuredExpectations_GetTokenWhenError_withError:(NSError *_Nonnull)error
  779. andToken:(FIRAppCheckToken *_Nullable)token {
  780. // 1. Expect token to be requested from storage.
  781. OCMExpect([self.mockStorage getToken]).andReturn([FBLPromise resolvedWith:token]);
  782. // 2. Expect token requested from app check provider.
  783. id completionArg = [OCMArg invokeBlockWithArgs:[NSNull null], error, nil];
  784. OCMExpect([self.mockAppCheckProvider getTokenWithCompletion:completionArg]);
  785. // 3. Don't expect token requested from app check provider.
  786. OCMReject([self.mockAppCheckProvider getTokenWithCompletion:[OCMArg any]]);
  787. // 4. Expect token update notification to be sent.
  788. XCTestExpectation *notificationExpectation = [self tokenUpdateNotificationWithExpectedToken:@""
  789. isInverted:YES];
  790. // 5. Expect token request to be completed.
  791. XCTestExpectation *getTokenExpectation = [self expectationWithDescription:@"getToken"];
  792. return @[ notificationExpectation, getTokenExpectation ];
  793. }
  794. - (NSArray *)expectTokenRequestFromAppCheckProvider {
  795. // 1. Expect token to be requested from storage.
  796. OCMExpect([self.mockStorage getToken]).andReturn([FBLPromise resolvedWith:nil]);
  797. // 2. Expect token requested from app check provider.
  798. FIRAppCheckToken *expectedToken = [self validToken];
  799. id completionArg = [OCMArg invokeBlockWithArgs:expectedToken, [NSNull null], nil];
  800. OCMExpect([self.mockAppCheckProvider getTokenWithCompletion:completionArg]);
  801. // 3. Expect new token to be stored.
  802. // 3.1. Create a pending promise to resolve later.
  803. FBLPromise<FIRAppCheckToken *> *storeTokenPromise = [FBLPromise pendingPromise];
  804. // 3.2. Stub storage set token method.
  805. OCMExpect([self.mockStorage setToken:expectedToken]).andReturn(storeTokenPromise);
  806. return @[ expectedToken, storeTokenPromise ];
  807. }
  808. - (void)verifyAllMocks {
  809. OCMVerifyAll(self.mockAppCheckProvider);
  810. OCMVerifyAll(self.mockStorage);
  811. OCMVerifyAll(self.mockSettings);
  812. OCMVerifyAll(self.mockTokenRefresher);
  813. }
  814. @end
  815. #endif // FIR_DEVICE_CHECK_SUPPORTED_TARGETS