FIRAppDistributionTests.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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 <XCTest/XCTest.h>
  16. #import "OCMock.h"
  17. #import <GoogleUtilities/GULAppDelegateSwizzler.h>
  18. #import <GoogleUtilities/GULUserDefaults.h>
  19. #import "FirebaseAppDistribution/Sources/FIRAppDistributionMachO.h"
  20. #import "FirebaseAppDistribution/Sources/FIRAppDistributionUIService.h"
  21. #import "FirebaseAppDistribution/Sources/FIRFADApiService.h"
  22. #import "FirebaseAppDistribution/Sources/Private/FIRAppDistribution.h"
  23. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  24. #import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.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)testFetchNewLatestReleaseUnauthenticatedFailure {
  312. NSError *mockError =
  313. [NSError errorWithDomain:kFIRFADApiErrorDomain
  314. code:FIRFADApiErrorUnauthenticated
  315. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  316. [self mockFetchReleasesCompletion:nil error:mockError];
  317. OCMStub([_mockMachO codeHash]).andReturn(@"this-is-old");
  318. [[GULUserDefaults standardUserDefaults] setBool:YES forKey:@"FIRFADSignInState"];
  319. XCTAssertTrue([[self appDistribution] isTesterSignedIn]);
  320. XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch latest release fails."];
  321. [[self appDistribution] fetchNewLatestRelease:^(FIRAppDistributionRelease *_Nullable release,
  322. NSError *_Nullable error) {
  323. XCTAssertNil(release);
  324. XCTAssertNotNil(error);
  325. XCTAssertEqual([error code], FIRAppDistributionErrorAuthenticationFailure);
  326. XCTAssertEqual([error domain], FIRAppDistributionErrorDomain);
  327. XCTAssertFalse([[self appDistribution] isTesterSignedIn]);
  328. [expectation fulfill];
  329. }];
  330. [self waitForExpectations:@[ expectation ] timeout:5.0];
  331. [self verifyFetchReleasesCompletion];
  332. OCMReject([_mockMachO codeHash]);
  333. }
  334. - (void)testCheckForUpdateWithCompletionTesterSignedIn {
  335. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  336. [self mockUIServiceRegistrationCompletion:nil];
  337. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  338. [self mockUIServiceShowUICompletion:NO];
  339. // Sign in the tester
  340. XCTestExpectation *expectation =
  341. [self expectationWithDescription:@"Persist sign in state succeeds."];
  342. [[self appDistribution] signInTesterWithCompletion:^(NSError *_Nullable error) {
  343. XCTAssertNil(error);
  344. [expectation fulfill];
  345. }];
  346. [self waitForExpectations:@[ expectation ] timeout:5.0];
  347. XCTAssertTrue([[self appDistribution] isTesterSignedIn]);
  348. // Should Call check for update without calling the UIService
  349. XCTestExpectation *checkForUpdateExpectation =
  350. [self expectationWithDescription:@"Check for update does not prompt user"];
  351. [[self appDistribution]
  352. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  353. NSError *_Nullable error) {
  354. XCTAssertNil(error);
  355. XCTAssertNotNil(release);
  356. [checkForUpdateExpectation fulfill];
  357. }];
  358. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  359. [self rejectShowUICompletion];
  360. }
  361. - (void)testCheckForUpdateWithCompletionDifferentCodeHashClicksYesSuccess {
  362. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  363. [self mockUIServiceRegistrationCompletion:nil];
  364. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  365. [self mockUIServiceShowUICompletion:YES];
  366. OCMStub([[self appDistribution] getAppVersion]).andReturn(_mockDisplayVersion);
  367. OCMStub([[self appDistribution] getAppBuild]).andReturn(_mockBuildVersion);
  368. OCMStub([_mockMachO codeHash]).andReturn(@"this-is-old");
  369. XCTestExpectation *checkForUpdateExpectation =
  370. [self expectationWithDescription:@"Check for update does prompt user"];
  371. [[self appDistribution]
  372. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  373. NSError *_Nullable error) {
  374. XCTAssertNil(error);
  375. XCTAssertNotNil(release);
  376. [checkForUpdateExpectation fulfill];
  377. }];
  378. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  379. [self verifyShowUICompletion];
  380. OCMVerify([_mockMachO codeHash]);
  381. }
  382. - (void)testCheckForUpdateWithCompletionDifferentVersionAndBuildClicksYesSuccess {
  383. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  384. [self mockUIServiceRegistrationCompletion:nil];
  385. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  386. [self mockUIServiceShowUICompletion:YES];
  387. OCMStub([[self appDistribution] getAppVersion]).andReturn(@"different-version");
  388. OCMStub([[self appDistribution] getAppBuild]).andReturn(@"different-build");
  389. OCMReject([_mockMachO codeHash]);
  390. XCTestExpectation *checkForUpdateExpectation =
  391. [self expectationWithDescription:@"Check for update does prompt user"];
  392. [[self appDistribution]
  393. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  394. NSError *_Nullable error) {
  395. XCTAssertNil(error);
  396. XCTAssertNotNil(release);
  397. [checkForUpdateExpectation fulfill];
  398. }];
  399. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  400. [self verifyShowUICompletion];
  401. }
  402. - (void)testCheckForUpdateWithCompletionDifferentVersionClicksYesSuccess {
  403. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  404. [self mockUIServiceRegistrationCompletion:nil];
  405. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  406. [self mockUIServiceShowUICompletion:YES];
  407. OCMStub([[self appDistribution] getAppVersion]).andReturn(@"different-version");
  408. OCMStub([[self appDistribution] getAppBuild]).andReturn(_mockBuildVersion);
  409. OCMReject([_mockMachO codeHash]);
  410. XCTestExpectation *checkForUpdateExpectation =
  411. [self expectationWithDescription:@"Check for update does prompt user"];
  412. [[self appDistribution]
  413. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  414. NSError *_Nullable error) {
  415. XCTAssertNil(error);
  416. XCTAssertNotNil(release);
  417. [checkForUpdateExpectation fulfill];
  418. }];
  419. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  420. [self verifyShowUICompletion];
  421. }
  422. - (void)testCheckForUpdateWithCompletionDifferentBuildClicksYesSuccess {
  423. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  424. [self mockUIServiceRegistrationCompletion:nil];
  425. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  426. [self mockUIServiceShowUICompletion:YES];
  427. OCMStub([[self appDistribution] getAppVersion]).andReturn(_mockDisplayVersion);
  428. OCMStub([[self appDistribution] getAppBuild]).andReturn(@"different-build");
  429. OCMReject([_mockMachO codeHash]);
  430. XCTestExpectation *checkForUpdateExpectation =
  431. [self expectationWithDescription:@"Check for update does prompt user"];
  432. [[self appDistribution]
  433. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  434. NSError *_Nullable error) {
  435. XCTAssertNil(error);
  436. XCTAssertNotNil(release);
  437. [checkForUpdateExpectation fulfill];
  438. }];
  439. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  440. [self verifyShowUICompletion];
  441. }
  442. - (void)testCheckForUpdateWithCompletionClicksYesFailure {
  443. NSError *mockError =
  444. [NSError errorWithDomain:@"this.is.fake"
  445. code:3
  446. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  447. [self mockInstallationIdCompletion:_mockInstallationId error:mockError];
  448. [self mockUIServiceRegistrationCompletion:nil];
  449. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  450. [self mockUIServiceShowUICompletion:YES];
  451. XCTestExpectation *checkForUpdateExpectation =
  452. [self expectationWithDescription:@"Check for update does prompt user"];
  453. [[self appDistribution]
  454. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  455. NSError *_Nullable error) {
  456. XCTAssertNotNil(error);
  457. XCTAssertNil(release);
  458. [checkForUpdateExpectation fulfill];
  459. }];
  460. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  461. [self verifyShowUICompletion];
  462. }
  463. - (void)testCheckForUpdateWithCompletionClicksNo {
  464. [self mockInstallationIdCompletion:_mockInstallationId error:nil];
  465. [self mockUIServiceRegistrationCompletion:nil];
  466. [self mockFetchReleasesCompletion:_mockReleases error:nil];
  467. [self mockUIServiceShowUICompletion:NO];
  468. XCTestExpectation *checkForUpdateExpectation =
  469. [self expectationWithDescription:@"Check for update does prompt user"];
  470. [[self appDistribution]
  471. checkForUpdateWithCompletion:^(FIRAppDistributionRelease *_Nullable release,
  472. NSError *_Nullable error) {
  473. XCTAssertNotNil(error);
  474. XCTAssertEqual([error code], FIRAppDistributionErrorAuthenticationCancelled);
  475. XCTAssertNil(release);
  476. [checkForUpdateExpectation fulfill];
  477. }];
  478. [self waitForExpectations:@[ checkForUpdateExpectation ] timeout:5.0];
  479. [self verifyShowUICompletion];
  480. }
  481. - (void)testHandleFetchReleasesErrorTimeout {
  482. NSError *mockError =
  483. [NSError errorWithDomain:kFIRFADApiErrorDomain
  484. code:FIRFADApiErrorTimeout
  485. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  486. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  487. XCTAssertNotNil(handledError);
  488. XCTAssertEqual([handledError code], FIRAppDistributionErrorNetworkFailure);
  489. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  490. }
  491. - (void)testHandleFetchReleasesErrorUnauthenticated {
  492. NSError *mockError =
  493. [NSError errorWithDomain:kFIRFADApiErrorDomain
  494. code:FIRFADApiErrorUnauthenticated
  495. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  496. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  497. XCTAssertNotNil(handledError);
  498. XCTAssertEqual([handledError code], FIRAppDistributionErrorAuthenticationFailure);
  499. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  500. }
  501. - (void)testHandleFetchReleasesErrorUnauthorized {
  502. NSError *mockError =
  503. [NSError errorWithDomain:kFIRFADApiErrorDomain
  504. code:FIRFADApiErrorUnauthorized
  505. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  506. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  507. XCTAssertNotNil(handledError);
  508. XCTAssertEqual([handledError code], FIRAppDistributionErrorAuthenticationFailure);
  509. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  510. }
  511. - (void)testHandleFetchReleasesErrorTokenGenerationFailure {
  512. NSError *mockError =
  513. [NSError errorWithDomain:kFIRFADApiErrorDomain
  514. code:FIRFADApiTokenGenerationFailure
  515. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  516. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  517. XCTAssertNotNil(handledError);
  518. XCTAssertEqual([handledError code], FIRAppDistributionErrorAuthenticationFailure);
  519. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  520. }
  521. - (void)testHandleFetchReleasesErrorInstallationIdentifierFailure {
  522. NSError *mockError =
  523. [NSError errorWithDomain:kFIRFADApiErrorDomain
  524. code:FIRFADApiInstallationIdentifierError
  525. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  526. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  527. XCTAssertNotNil(handledError);
  528. XCTAssertEqual([handledError code], FIRAppDistributionErrorAuthenticationFailure);
  529. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  530. }
  531. - (void)testHandleFetchReleasesErrorNotFound {
  532. NSError *mockError =
  533. [NSError errorWithDomain:kFIRFADApiErrorDomain
  534. code:FIRFADApiErrorNotFound
  535. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  536. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  537. XCTAssertNotNil(handledError);
  538. XCTAssertEqual([handledError code], FIRAppDistributionErrorAuthenticationFailure);
  539. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  540. }
  541. - (void)testHandleFetchReleasesErrorApiDomainErrorUnknown {
  542. NSError *mockError =
  543. [NSError errorWithDomain:kFIRFADApiErrorDomain
  544. code:209
  545. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  546. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  547. XCTAssertNotNil(handledError);
  548. XCTAssertEqual([handledError code], FIRAppDistributionErrorUnknown);
  549. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  550. }
  551. - (void)testHandleFetchReleasesErrorUnknownDomainError {
  552. NSError *mockError =
  553. [NSError errorWithDomain:@"this.is.not.an.api.failure"
  554. code:4
  555. userInfo:@{NSLocalizedDescriptionKey : @"This is unfortunate."}];
  556. NSError *handledError = [[self appDistribution] mapFetchReleasesError:mockError];
  557. XCTAssertNotNil(handledError);
  558. XCTAssertEqual([handledError code], FIRAppDistributionErrorUnknown);
  559. XCTAssertEqual([handledError domain], FIRAppDistributionErrorDomain);
  560. }
  561. @end