SEGContentManagerTests.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2019 Google
  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 <XCTest/XCTest.h>
  15. #import "FirebaseSegmentation/Sources/SEGContentManager.h"
  16. #import "FirebaseSegmentation/Sources/SEGDatabaseManager.h"
  17. #import "FirebaseSegmentation/Sources/SEGNetworkManager.h"
  18. #import <OCMock/OCMock.h>
  19. #import "FirebaseCore/Sources/Public/FirebaseCore/FirebaseCore.h"
  20. #import "FirebaseInstallations/Source/Library/InstallationsIDController/FIRInstallationsIDController.h"
  21. #import "FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FirebaseInstallations.h"
  22. @interface SEGContentManager (ForTest)
  23. - (instancetype)initWithDatabaseManager:databaseManager networkManager:networkManager;
  24. @end
  25. @interface FIRInstallations (Tests)
  26. @property(nonatomic, readwrite, strong) FIROptions *appOptions;
  27. @property(nonatomic, readwrite, strong) NSString *appName;
  28. - (instancetype)initWithAppOptions:(FIROptions *)appOptions
  29. appName:(NSString *)appName
  30. installationsIDController:(FIRInstallationsIDController *)installationsIDController
  31. prefetchAuthToken:(BOOL)prefetchAuthToken;
  32. @end
  33. @interface FIRInstallationsAuthTokenResult (ForTest)
  34. - (instancetype)initWithToken:(NSString *)token expirationDate:(NSDate *)expirationTime;
  35. @end
  36. @interface SEGContentManagerTests : XCTestCase
  37. @property(nonatomic) SEGContentManager *contentManager;
  38. @property(nonatomic) id networkManagerMock;
  39. @property(nonatomic) id mockIDController;
  40. @property(nonatomic) FIROptions *appOptions;
  41. @property(readonly) NSString *firebaseAppName;
  42. @property(strong, readonly, nonatomic) id mockInstallations;
  43. @end
  44. @implementation SEGContentManagerTests
  45. - (void)setUp {
  46. // Setup FIRApp.
  47. _firebaseAppName = @"my-firebase-app-id";
  48. XCTAssertNoThrow([FIRApp configureWithName:self.firebaseAppName options:[self FIRAppOptions]]);
  49. // Installations Mock
  50. NSString *FID = @"fid-is-better-than-iid";
  51. _mockInstallations = OCMClassMock([FIRInstallations class]);
  52. OCMStub([_mockInstallations installationsWithApp:[FIRApp appNamed:self.firebaseAppName]])
  53. .andReturn(_mockInstallations);
  54. FIRInstallationsAuthTokenResult *FISToken =
  55. [[FIRInstallationsAuthTokenResult alloc] initWithToken:@"fake-fis-token" expirationDate:nil];
  56. OCMStub([_mockInstallations
  57. installationIDWithCompletion:([OCMArg invokeBlockWithArgs:FID, [NSNull null], nil])]);
  58. OCMStub([_mockInstallations
  59. authTokenWithCompletion:([OCMArg invokeBlockWithArgs:FISToken, [NSNull null], nil])]);
  60. // Mock the network manager.
  61. self.networkManagerMock = OCMClassMock([SEGNetworkManager class]);
  62. OCMStub([self.networkManagerMock
  63. makeAssociationRequestToBackendWithData:[OCMArg any]
  64. token:[OCMArg any]
  65. completion:([OCMArg
  66. invokeBlockWithArgs:@YES, [NSNull null], nil])]);
  67. // Initialize the content manager.
  68. self.contentManager =
  69. [[SEGContentManager alloc] initWithDatabaseManager:[SEGDatabaseManager sharedInstance]
  70. networkManager:self.networkManagerMock];
  71. }
  72. - (void)tearDown {
  73. [self.networkManagerMock stopMocking];
  74. self.networkManagerMock = nil;
  75. self.contentManager = nil;
  76. self.mockIDController = nil;
  77. }
  78. // Associate a fake custom installation id and fake firebase installation id.
  79. // TODO(mandard): check for result and add more tests.
  80. - (void)testAssociateCustomInstallationIdentifierSuccessfully {
  81. XCTestExpectation *expectation =
  82. [self expectationWithDescription:@"associateCustomInstallation for contentmanager"];
  83. [_contentManager
  84. associateCustomInstallationIdentiferNamed:@"my-custom-id"
  85. firebaseApp:self.firebaseAppName
  86. completion:^(BOOL success, NSDictionary *result) {
  87. XCTAssertTrue(success,
  88. @"Could not associate custom installation ID");
  89. [expectation fulfill];
  90. }];
  91. [self waitForExpectationsWithTimeout:10 handler:nil];
  92. }
  93. #pragma mark private
  94. - (FIROptions *)FIRAppOptions {
  95. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"1:123:ios:123abc"
  96. GCMSenderID:@"correct_gcm_sender_id"];
  97. options.APIKey = @"AIzaSaaaaaaaaaaaaaaaaaaaaaaaaaaa1111111";
  98. options.projectID = @"abc-xyz-123";
  99. return options;
  100. }
  101. @end