FIRAppDistributionTests.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <Foundation/Foundation.h>
  15. #import <OCMock/OCMock.h>
  16. #import <XCTest/XCTest.h>
  17. #import "FirebaseAppDistribution/Sources/FIRAppDistributionMachO.h"
  18. #import "FirebaseAppDistribution/Sources/FIRAppDistributionUIService.h"
  19. #import "FirebaseAppDistribution/Sources/FIRFADApiService.h"
  20. #import "FirebaseAppDistribution/Sources/Private/FIRAppDistribution.h"
  21. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  22. #import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
  23. #import "GoogleUtilities/AppDelegateSwizzler/Private/GULAppDelegateSwizzler.h"
  24. #import "GoogleUtilities/UserDefaults/Private/GULUserDefaults.h"
  25. @interface FIRAppDistributionTests : XCTestCase
  26. @property(nonatomic, strong) FIRAppDistribution *appDistribution;
  27. @end
  28. @interface FIRAppDistribution (PrivateUnitTesting)
  29. - (instancetype)initWithApp:(FIRApp *)app appInfo:(NSDictionary *)appInfo;
  30. - (void)fetchNewLatestRelease:(void (^)(FIRAppDistributionRelease *_Nullable release,
  31. NSError *_Nullable error))completion;
  32. - (NSError *)mapFetchReleasesError:(NSError *)error;
  33. - (NSString *)getAppVersion;
  34. - (NSString *)getAppBuild;
  35. @end
  36. @implementation FIRAppDistributionTests {
  37. id _mockFIRAppClass;
  38. id _mockFIRFADApiService;
  39. id _mockFIRAppDistributionUIService;
  40. id _mockFIRInstallations;
  41. id _mockInstallationToken;
  42. id _mockMachO;
  43. NSString *_mockAuthToken;
  44. NSString *_mockInstallationId;
  45. NSArray *_mockReleases;
  46. NSString *_mockCodeHash;
  47. NSString *_mockDisplayVersion;
  48. NSString *_mockBuildVersion;
  49. }
  50. - (void)setUp {
  51. [super setUp];
  52. _mockAuthToken = @"this-is-an-auth-token";
  53. _mockCodeHash = @"this-is-a-fake-code-hash";
  54. _mockDisplayVersion = @"mock-display-version";
  55. _mockBuildVersion = @"mock-build-version";
  56. _mockFIRAppClass = OCMClassMock([FIRApp class]);
  57. _mockFIRFADApiService = OCMClassMock([FIRFADApiService class]);
  58. _mockFIRAppDistributionUIService = OCMPartialMock([FIRAppDistributionUIService sharedInstance]);
  59. _mockFIRInstallations = OCMClassMock([FIRInstallations class]);
  60. _mockInstallationToken = OCMClassMock([FIRInstallationsAuthTokenResult class]);
  61. _mockMachO = OCMClassMock([FIRAppDistributionMachO class]);
  62. id mockBundle = OCMClassMock([NSBundle class]);
  63. OCMStub([_mockFIRAppClass defaultApp]).andReturn(_mockFIRAppClass);
  64. OCMStub([_mockFIRAppDistributionUIService initializeUIState]);
  65. OCMStub([_mockFIRInstallations installations]).andReturn(_mockFIRInstallations);
  66. OCMStub([_mockInstallationToken authToken]).andReturn(_mockAuthToken);
  67. OCMStub([_mockMachO alloc]).andReturn(_mockMachO);
  68. OCMStub([_mockMachO initWithPath:OCMOCK_ANY]).andReturn(_mockMachO);
  69. OCMStub([mockBundle mainBundle]).andReturn(mockBundle);
  70. OCMStub([mockBundle executablePath]).andReturn(@"this-is-a-fake-executablePath");
  71. NSDictionary<NSString *, NSString *> *dict = [[NSDictionary<NSString *, NSString *> alloc] init];
  72. self.appDistribution = [[FIRAppDistribution alloc] initWithApp:_mockFIRAppClass appInfo:dict];
  73. _mockInstallationId = @"this-id-is-fake-ccccc";
  74. _mockReleases = @[
  75. @{
  76. @"codeHash" : @"this-is-another-code-hash",
  77. @"displayVersion" : @"1.0.0",
  78. @"buildVersion" : @"111",
  79. @"releaseNotes" : @"This is a release",
  80. @"downloadUrl" : @"http://faketyfakefake.download"
  81. },
  82. @{
  83. @"latest" : @YES,
  84. @"codeHash" : _mockCodeHash,
  85. @"displayVersion" : _mockDisplayVersion,
  86. @"buildVersion" : _mockBuildVersion,
  87. @"releaseNotes" : @"This is a release too",
  88. @"downloadUrl" : @"http://faketyfakefake.download"
  89. }
  90. ];
  91. }
  92. - (void)tearDown {
  93. [super tearDown];
  94. [[GULUserDefaults standardUserDefaults] removeObjectForKey:@"FIRFADSignInState"];
  95. [_mockFIRAppClass stopMocking];
  96. [_mockFIRFADApiService stopMocking];
  97. [_mockFIRAppDistributionUIService stopMocking];
  98. [_mockFIRInstallations stopMocking];
  99. [_mockInstallationToken stopMocking];
  100. [_mockMachO stopMocking];
  101. }
  102. - (void)mockInstallationIdCompletion:(NSString *_Nullable)identifier
  103. error:(NSError *_Nullable)error {
  104. [OCMStub([_mockFIRInstallations installationIDWithCompletion:OCMOCK_ANY])
  105. andDo:^(NSInvocation *invocation) {
  106. __unsafe_unretained void (^handler)(NSString *identifier, NSError *_Nullable error);
  107. [invocation getArgument:&handler atIndex:2];
  108. handler(identifier, error);
  109. }];
  110. }
  111. - (void)verifyInstallationIdCompletion {
  112. OCMVerify([_mockFIRInstallations installationIDWithCompletion:OCMOCK_ANY]);
  113. }
  114. - (void)mockUIServiceRegistrationCompletion:(NSError *_Nullable)error {
  115. [OCMStub([_mockFIRAppDistributionUIService appDistributionRegistrationFlow:OCMOCK_ANY
  116. withCompletion:OCMOCK_ANY])
  117. andDo:^(NSInvocation *invocation) {
  118. __unsafe_unretained void (^handler)(NSError *_Nullable error);
  119. [invocation getArgument:&handler atIndex:3];
  120. handler(error);
  121. }];
  122. }
  123. - (void)verifyRegistrationCompletion {
  124. OCMVerify([_mockFIRAppDistributionUIService appDistributionRegistrationFlow:OCMOCK_ANY
  125. withCompletion:OCMOCK_ANY]);
  126. }
  127. - (void)rejectRegistrationCompletion {
  128. OCMReject([_mockFIRAppDistributionUIService appDistributionRegistrationFlow:OCMOCK_ANY
  129. withCompletion:OCMOCK_ANY]);
  130. }
  131. - (void)mockUIServiceShowUICompletion:(BOOL)continued {
  132. [OCMStub([_mockFIRAppDistributionUIService showUIAlertWithCompletion:OCMOCK_ANY])
  133. andDo:^(NSInvocation *invocation) {
  134. __unsafe_unretained void (^handler)(BOOL continued);
  135. [invocation getArgument:&handler atIndex:2];
  136. handler(continued);
  137. }];
  138. }
  139. - (void)verifyShowUICompletion {
  140. OCMVerify([_mockFIRAppDistributionUIService showUIAlertWithCompletion:OCMOCK_ANY]);
  141. }
  142. - (void)rejectShowUICompletion {
  143. OCMReject([_mockFIRAppDistributionUIService showUIAlertWithCompletion:OCMOCK_ANY]);
  144. }
  145. - (void)mockFetchReleasesCompletion:(NSArray *)releases error:(NSError *)error {
  146. [OCMStub([_mockFIRFADApiService fetchReleasesWithCompletion:OCMOCK_ANY])
  147. andDo:^(NSInvocation *invocation) {
  148. __unsafe_unretained void (^handler)(NSArray *releases, NSError *_Nullable error);
  149. [invocation getArgument:&handler atIndex:2];
  150. handler(releases, error);
  151. }];
  152. }
  153. - (void)verifyFetchReleasesCompletion {
  154. OCMVerify([_mockFIRFADApiService fetchReleasesWithCompletion:[OCMArg any]]);
  155. }
  156. - (void)rejectFetchReleasesCompletion {
  157. OCMReject([_mockFIRFADApiService fetchReleasesWithCompletion:[OCMArg any]]);
  158. }
  159. - (void)testInitWithApp {
  160. XCTAssertNotNil([self appDistribution]);
  161. }
  162. - (void)testSignInWithCompletionPersistSignInStateSuccess {
  163. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  164. [self mockUIServiceRegistrationCompletion:nil];
  165. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  166. XCTestExpectation *expectation =
  167. [self expectationWithDescription:@"Persist sign in state succeeds."];
  168. [[self appDistribution] signInTesterWithCompletion:^(NSError *_Nullable error) {
  169. XCTAssertNil(error);
  170. [expectation fulfill];
  171. }];
  172. [self waitForExpectations:@[ expectation ] timeout:5.0];
  173. XCTAssertTrue([[self appDistribution] isTesterSignedIn]);
  174. [self verifyInstallationIdCompletion];
  175. [self verifyRegistrationCompletion];
  176. [self verifyFetchReleasesCompletion];
  177. }
  178. - (void)testSignInWithCompletionInstallationIDNotFoundFailure {
  179. NSError *mockError =
  180. [NSError errorWithDomain:@"this.is.fake"
  181. code:3
  182. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  183. [self mockInstallationIdCompletion:_mockInstallationId error:mockError];
  184. [self mockUIServiceRegistrationCompletion:nil];
  185. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  186. XCTestExpectation *expectation =
  187. [self expectationWithDescription:@"Persist sign in state fails."];
  188. [[self appDistribution] signInTesterWithCompletion:^(NSError *_Nullable error) {
  189. XCTAssertNotNil(error);
  190. XCTAssertEqual([error code], FIRAppDistributionErrorUnknown);
  191. [expectation fulfill];
  192. }];
  193. [self waitForExpectations:@[ expectation ] timeout:5.0];
  194. XCTAssertFalse([[self appDistribution] isTesterSignedIn]);
  195. [self verifyInstallationIdCompletion];
  196. [self rejectRegistrationCompletion];
  197. [self rejectFetchReleasesCompletion];
  198. }
  199. - (void)testSignInWithCompletionDelegateFailureDoesNotPersist {
  200. NSError *mockError =
  201. [NSError errorWithDomain:@"fake.app.delegate.domain"
  202. code:4
  203. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  204. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  205. [self mockUIServiceRegistrationCompletion:mockError];
  206. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  207. XCTestExpectation *expectation =
  208. [self expectationWithDescription:
  209. @"Persist sign in state fails when the delegate recieves a failure."];
  210. [[self appDistribution] signInTesterWithCompletion:^(NSError *_Nullable error) {
  211. XCTAssertNotNil(error);
  212. XCTAssertEqual([error code], 4);
  213. [expectation fulfill];
  214. }];
  215. [self waitForExpectations:@[ expectation ] timeout:5.0];
  216. XCTAssertFalse([[self appDistribution] isTesterSignedIn]);
  217. [self verifyInstallationIdCompletion];
  218. [self verifyRegistrationCompletion];
  219. [self rejectFetchReleasesCompletion];
  220. }
  221. - (void)testSignInWithCompletionFetchReleasesFailureDoesNotPersist {
  222. NSError *mockError =
  223. [NSError errorWithDomain:kFIRFADApiErrorDomain
  224. code:FIRFADApiErrorUnauthenticated
  225. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  226. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  227. [self mockUIServiceRegistrationCompletion:nil];
  228. [self mockFetchReleasesCompletion:_mockReleases error:mockError];
  229. XCTestExpectation *expectation = [self
  230. expectationWithDescription:@"Persist sign in state fails when we fail to fetch releases."];
  231. [[self appDistribution] signInTesterWithCompletion:^(NSError *_Nullable error) {
  232. XCTAssertNotNil(error);
  233. XCTAssertEqual([error code], FIRAppDistributionErrorAuthenticationFailure);
  234. XCTAssertEqual([error domain], FIRAppDistributionErrorDomain);
  235. [expectation fulfill];
  236. }];
  237. [self waitForExpectations:@[ expectation ] timeout:5.0];
  238. XCTAssertFalse([[self appDistribution] isTesterSignedIn]);
  239. [self verifyInstallationIdCompletion];
  240. [self verifyRegistrationCompletion];
  241. [self verifyFetchReleasesCompletion];
  242. }
  243. - (void)testSignOutSuccess {
  244. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  245. [self mockUIServiceRegistrationCompletion:nil];
  246. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  247. XCTestExpectation *expectation =
  248. [self expectationWithDescription:@"Persist sign out state succeeds."];
  249. [[self appDistribution] signInTesterWithCompletion:^(NSError *_Nullable error) {
  250. XCTAssertTrue([[self appDistribution] isTesterSignedIn]);
  251. XCTAssertNil(error);
  252. [expectation fulfill];
  253. }];
  254. [self waitForExpectations:@[ expectation ] timeout:5.0];
  255. [[self appDistribution] signOutTester];
  256. XCTAssertFalse([[self appDistribution] isTesterSignedIn]);
  257. [self verifyInstallationIdCompletion];
  258. [self verifyRegistrationCompletion];
  259. [self verifyFetchReleasesCompletion];
  260. }
  261. - (void)testFetchNewLatestReleaseSuccess {
  262. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  263. OCMStub([_mockMachO codeHash]).andReturn(@"this-is-old");
  264. XCTestExpectation *expectation =
  265. [self expectationWithDescription:@"Fetch latest release succeeds."];
  266. [[self appDistribution] fetchNewLatestRelease:^(FIRAppDistributionRelease *_Nullable release,
  267. NSError *_Nullable error) {
  268. XCTAssertNotNil(release);
  269. XCTAssertNil(error);
  270. [expectation fulfill];
  271. }];
  272. [self waitForExpectations:@[ expectation ] timeout:5.0];
  273. [self verifyFetchReleasesCompletion];
  274. }
  275. - (void)testFetchNewLatestReleaseNoNewRelease {
  276. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  277. OCMStub([_mockMachO codeHash]).andReturn(_mockCodeHash);
  278. OCMStub([[self appDistribution] getAppVersion]).andReturn(_mockDisplayVersion);
  279. OCMStub([[self appDistribution] getAppBuild]).andReturn(_mockBuildVersion);
  280. XCTestExpectation *expectation =
  281. [self expectationWithDescription:@"Fetch latest release with no new release succeeds."];
  282. [[self appDistribution] fetchNewLatestRelease:^(FIRAppDistributionRelease *_Nullable release,
  283. NSError *_Nullable error) {
  284. XCTAssertNil(release);
  285. XCTAssertNil(error);
  286. [expectation fulfill];
  287. }];
  288. [self waitForExpectations:@[ expectation ] timeout:5.0];
  289. [self verifyFetchReleasesCompletion];
  290. }
  291. - (void)testFetchNewLatestReleaseFailure {
  292. NSError *mockError =
  293. [NSError errorWithDomain:kFIRFADApiErrorDomain
  294. code:FIRFADApiErrorTimeout
  295. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  296. [self mockFetchReleasesCompletion:nil error:mockError];
  297. OCMStub([_mockMachO codeHash]).andReturn(@"this-is-old");
  298. XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch latest release fails."];
  299. [[self appDistribution] fetchNewLatestRelease:^(FIRAppDistributionRelease *_Nullable release,
  300. NSError *_Nullable error) {
  301. XCTAssertNil(release);
  302. XCTAssertNotNil(error);
  303. XCTAssertEqual([error code], FIRAppDistributionErrorNetworkFailure);
  304. XCTAssertEqual([error domain], FIRAppDistributionErrorDomain);
  305. [expectation fulfill];
  306. }];
  307. [self waitForExpectations:@[ expectation ] timeout:5.0];
  308. [self verifyFetchReleasesCompletion];
  309. OCMReject([_mockMachO codeHash]);
  310. }
  311. - (void)testCheckForUpdateWithCompletionTesterSignedIn {
  312. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  313. [self mockUIServiceRegistrationCompletion:nil];
  314. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  315. [self mockUIServiceShowUICompletion:NO];
  316. // Sign in the tester
  317. XCTestExpectation *expectation =
  318. [self expectationWithDescription:@"Persist sign in state succeeds."];
  319. [[self appDistribution] signInTesterWithCompletion:^(NSError *_Nullable error) {
  320. XCTAssertNil(error);
  321. [expectation fulfill];
  322. }];
  323. [self waitForExpectations:@[ expectation ] timeout:5.0];
  324. XCTAssertTrue([[self appDistribution] isTesterSignedIn]);
  325. // Should Call check for update without calling the UIService
  326. XCTestExpectation *checkForUpdateExpectation =
  327. [self expectationWithDescription:@"Check for update does not prompt user"];
  328. [[self appDistribution]
  329. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  330. NSError *_Nullable error) {
  331. XCTAssertNil(error);
  332. XCTAssertNotNil(release);
  333. [checkForUpdateExpectation fulfill];
  334. }];
  335. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  336. [self rejectShowUICompletion];
  337. }
  338. - (void)testCheckForUpdateWithCompletionDifferentCodeHashClicksYesSuccess {
  339. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  340. [self mockUIServiceRegistrationCompletion:nil];
  341. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  342. [self mockUIServiceShowUICompletion:YES];
  343. OCMStub([[self appDistribution] getAppVersion]).andReturn(_mockDisplayVersion);
  344. OCMStub([[self appDistribution] getAppBuild]).andReturn(_mockBuildVersion);
  345. OCMStub([_mockMachO codeHash]).andReturn(@"this-is-old");
  346. XCTestExpectation *checkForUpdateExpectation =
  347. [self expectationWithDescription:@"Check for update does prompt user"];
  348. [[self appDistribution]
  349. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  350. NSError *_Nullable error) {
  351. XCTAssertNil(error);
  352. XCTAssertNotNil(release);
  353. [checkForUpdateExpectation fulfill];
  354. }];
  355. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  356. [self verifyShowUICompletion];
  357. OCMVerify([_mockMachO codeHash]);
  358. }
  359. - (void)testCheckForUpdateWithCompletionDifferentVersionAndBuildClicksYesSuccess {
  360. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  361. [self mockUIServiceRegistrationCompletion:nil];
  362. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  363. [self mockUIServiceShowUICompletion:YES];
  364. OCMStub([[self appDistribution] getAppVersion]).andReturn(@"different-version");
  365. OCMStub([[self appDistribution] getAppBuild]).andReturn(@"different-build");
  366. OCMReject([_mockMachO codeHash]);
  367. XCTestExpectation *checkForUpdateExpectation =
  368. [self expectationWithDescription:@"Check for update does prompt user"];
  369. [[self appDistribution]
  370. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  371. NSError *_Nullable error) {
  372. XCTAssertNil(error);
  373. XCTAssertNotNil(release);
  374. [checkForUpdateExpectation fulfill];
  375. }];
  376. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  377. [self verifyShowUICompletion];
  378. }
  379. - (void)testCheckForUpdateWithCompletionDifferentVersionClicksYesSuccess {
  380. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  381. [self mockUIServiceRegistrationCompletion:nil];
  382. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  383. [self mockUIServiceShowUICompletion:YES];
  384. OCMStub([[self appDistribution] getAppVersion]).andReturn(@"different-version");
  385. OCMStub([[self appDistribution] getAppBuild]).andReturn(_mockBuildVersion);
  386. OCMReject([_mockMachO codeHash]);
  387. XCTestExpectation *checkForUpdateExpectation =
  388. [self expectationWithDescription:@"Check for update does prompt user"];
  389. [[self appDistribution]
  390. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  391. NSError *_Nullable error) {
  392. XCTAssertNil(error);
  393. XCTAssertNotNil(release);
  394. [checkForUpdateExpectation fulfill];
  395. }];
  396. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  397. [self verifyShowUICompletion];
  398. }
  399. - (void)testCheckForUpdateWithCompletionDifferentBuildClicksYesSuccess {
  400. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  401. [self mockUIServiceRegistrationCompletion:nil];
  402. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  403. [self mockUIServiceShowUICompletion:YES];
  404. OCMStub([[self appDistribution] getAppVersion]).andReturn(_mockDisplayVersion);
  405. OCMStub([[self appDistribution] getAppBuild]).andReturn(@"different-build");
  406. OCMReject([_mockMachO codeHash]);
  407. XCTestExpectation *checkForUpdateExpectation =
  408. [self expectationWithDescription:@"Check for update does prompt user"];
  409. [[self appDistribution]
  410. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  411. NSError *_Nullable error) {
  412. XCTAssertNil(error);
  413. XCTAssertNotNil(release);
  414. [checkForUpdateExpectation fulfill];
  415. }];
  416. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  417. [self verifyShowUICompletion];
  418. }
  419. - (void)testCheckForUpdateWithCompletionClicksYesFailure {
  420. NSError *mockError =
  421. [NSError errorWithDomain:@"this.is.fake"
  422. code:3
  423. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  424. [self mockInstallationIdCompletion:_mockInstallationId error:mockError];
  425. [self mockUIServiceRegistrationCompletion:nil];
  426. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  427. [self mockUIServiceShowUICompletion:YES];
  428. XCTestExpectation *checkForUpdateExpectation =
  429. [self expectationWithDescription:@"Check for update does prompt user"];
  430. [[self appDistribution]
  431. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  432. NSError *_Nullable error) {
  433. XCTAssertNotNil(error);
  434. XCTAssertNil(release);
  435. [checkForUpdateExpectation fulfill];
  436. }];
  437. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  438. [self verifyShowUICompletion];
  439. }
  440. - (void)testCheckForUpdateWithCompletionClicksNo {
  441. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  442. [self mockUIServiceRegistrationCompletion:nil];
  443. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  444. [self mockUIServiceShowUICompletion:NO];
  445. XCTestExpectation *checkForUpdateExpectation =
  446. [self expectationWithDescription:@"Check for update does prompt user"];
  447. [[self appDistribution]
  448. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  449. NSError *_Nullable error) {
  450. XCTAssertNotNil(error);
  451. XCTAssertEqual([error code], FIRAppDistributionErrorAuthenticationCancelled);
  452. XCTAssertNil(release);
  453. [checkForUpdateExpectation fulfill];
  454. }];
  455. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  456. [self verifyShowUICompletion];
  457. }
  458. - (void)testHandleFetchReleasesErrorTimeout {
  459. NSError *mockError =
  460. [NSError errorWithDomain:kFIRFADApiErrorDomain
  461. code:FIRFADApiErrorTimeout
  462. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  463. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  464. XCTAssertNotNil(handledError);
  465. XCTAssertEqual([handledError code], FIRAppDistributionErrorNetworkFailure);
  466. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  467. }
  468. - (void)testHandleFetchReleasesErrorUnauthenticated {
  469. NSError *mockError =
  470. [NSError errorWithDomain:kFIRFADApiErrorDomain
  471. code:FIRFADApiErrorUnauthenticated
  472. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  473. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  474. XCTAssertNotNil(handledError);
  475. XCTAssertEqual([handledError code], FIRAppDistributionErrorAuthenticationFailure);
  476. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  477. }
  478. - (void)testHandleFetchReleasesErrorUnauthorized {
  479. NSError *mockError =
  480. [NSError errorWithDomain:kFIRFADApiErrorDomain
  481. code:FIRFADApiErrorUnauthorized
  482. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  483. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  484. XCTAssertNotNil(handledError);
  485. XCTAssertEqual([handledError code], FIRAppDistributionErrorAuthenticationFailure);
  486. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  487. }
  488. - (void)testHandleFetchReleasesErrorTokenGenerationFailure {
  489. NSError *mockError =
  490. [NSError errorWithDomain:kFIRFADApiErrorDomain
  491. code:FIRFADApiTokenGenerationFailure
  492. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  493. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  494. XCTAssertNotNil(handledError);
  495. XCTAssertEqual([handledError code], FIRAppDistributionErrorAuthenticationFailure);
  496. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  497. }
  498. - (void)testHandleFetchReleasesErrorInstallationIdentifierFailure {
  499. NSError *mockError =
  500. [NSError errorWithDomain:kFIRFADApiErrorDomain
  501. code:FIRFADApiInstallationIdentifierError
  502. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  503. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  504. XCTAssertNotNil(handledError);
  505. XCTAssertEqual([handledError code], FIRAppDistributionErrorAuthenticationFailure);
  506. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  507. }
  508. - (void)testHandleFetchReleasesErrorNotFound {
  509. NSError *mockError =
  510. [NSError errorWithDomain:kFIRFADApiErrorDomain
  511. code:FIRFADApiErrorNotFound
  512. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  513. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  514. XCTAssertNotNil(handledError);
  515. XCTAssertEqual([handledError code], FIRAppDistributionErrorAuthenticationFailure);
  516. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  517. }
  518. - (void)testHandleFetchReleasesErrorApiDomainErrorUnknown {
  519. NSError *mockError =
  520. [NSError errorWithDomain:kFIRFADApiErrorDomain
  521. code:209
  522. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  523. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  524. XCTAssertNotNil(handledError);
  525. XCTAssertEqual([handledError code], FIRAppDistributionErrorUnknown);
  526. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  527. }
  528. - (void)testHandleFetchReleasesErrorUnknownDomainError {
  529. NSError *mockError =
  530. [NSError errorWithDomain:@"this.is.not.an.api.failure"
  531. code:4
  532. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  533. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  534. XCTAssertNotNil(handledError);
  535. XCTAssertEqual([handledError code], FIRAppDistributionErrorUnknown);
  536. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  537. }
  538. @end