FUNContextProviderTests.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "FIRAuthInteropFake.h"
  16. #import "Functions/FirebaseFunctions/FUNContext.h"
  17. #import "SharedTestUtilities/AppCheckFake/FIRAppCheckFake.h"
  18. #import "SharedTestUtilities/AppCheckFake/FIRAppCheckTokenResultFake.h"
  19. #import "SharedTestUtilities/FIRMessagingInteropFake.h"
  20. @interface FUNContextProviderTests : XCTestCase
  21. @property(nonatomic) FIRMessagingInteropFake *messagingFake;
  22. @property(nonatomic) FIRAppCheckFake *appCheckFake;
  23. @property(strong, nonatomic) FIRAppCheckTokenResultFake *appCheckTokenSuccess;
  24. @property(strong, nonatomic) FIRAppCheckTokenResultFake *appCheckTokenError;
  25. @end
  26. @implementation FUNContextProviderTests
  27. - (void)setUp {
  28. self.messagingFake = [[FIRMessagingInteropFake alloc] init];
  29. self.appCheckFake = [[FIRAppCheckFake alloc] init];
  30. self.appCheckTokenSuccess = [[FIRAppCheckTokenResultFake alloc] initWithToken:@"valid_token"
  31. error:nil];
  32. self.appCheckTokenError = [[FIRAppCheckTokenResultFake alloc]
  33. initWithToken:@"dummy token"
  34. error:[NSError errorWithDomain:@"testAppCheckError" code:-1 userInfo:nil]];
  35. }
  36. - (void)testContextWithAuth {
  37. FIRAuthInteropFake *auth = [[FIRAuthInteropFake alloc] initWithToken:@"token"
  38. userID:@"userID"
  39. error:nil];
  40. FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:(id<FIRAuthInterop>)auth
  41. messaging:self.messagingFake
  42. appCheck:nil];
  43. XCTestExpectation *expectation =
  44. [self expectationWithDescription:@"Context should have auth keys."];
  45. [provider getContext:^(FUNContext *context, NSError *_Nullable error) {
  46. XCTAssertNotNil(context);
  47. XCTAssert([context.authToken isEqualToString:@"token"]);
  48. XCTAssert([context.FCMToken isEqualToString:self.messagingFake.FCMToken]);
  49. XCTAssertNil(error);
  50. [expectation fulfill];
  51. }];
  52. [self waitForExpectations:@[ expectation ] timeout:0.1];
  53. }
  54. - (void)testContextWithAuthError {
  55. NSError *authError = [[NSError alloc] initWithDomain:@"com.functions.tests" code:4 userInfo:nil];
  56. FIRAuthInteropFake *auth = [[FIRAuthInteropFake alloc] initWithToken:nil
  57. userID:nil
  58. error:authError];
  59. FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:(id<FIRAuthInterop>)auth
  60. messaging:self.messagingFake
  61. appCheck:nil];
  62. XCTestExpectation *expectation =
  63. [self expectationWithDescription:@"Completion handler should fail with Auth error."];
  64. [provider getContext:^(FUNContext *context, NSError *_Nullable error) {
  65. XCTAssertNotNil(context);
  66. XCTAssertNil(context.authToken);
  67. XCTAssertEqual(error, auth.error);
  68. [expectation fulfill];
  69. }];
  70. [self waitForExpectations:@[ expectation ] timeout:0.1];
  71. }
  72. - (void)testContextWithoutAuth {
  73. FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:nil
  74. messaging:nil
  75. appCheck:nil];
  76. XCTestExpectation *expectation =
  77. [self expectationWithDescription:@"Completion handler should succeed without Auth."];
  78. [provider getContext:^(FUNContext *context, NSError *_Nullable error) {
  79. XCTAssertNotNil(context);
  80. XCTAssertNil(error);
  81. XCTAssertNil(context.authToken);
  82. XCTAssertNil(context.FCMToken);
  83. [expectation fulfill];
  84. }];
  85. [self waitForExpectations:@[ expectation ] timeout:0.1];
  86. }
  87. - (void)testContextWithAppCheckOnlySuccess {
  88. self.appCheckFake.tokenResult = self.appCheckTokenSuccess;
  89. FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:nil
  90. messaging:nil
  91. appCheck:self.appCheckFake];
  92. XCTestExpectation *expectation =
  93. [self expectationWithDescription:@"Completion handler should succeed without Auth."];
  94. [provider getContext:^(FUNContext *context, NSError *_Nullable error) {
  95. XCTAssertNotNil(context);
  96. XCTAssertNil(error);
  97. XCTAssertNil(context.authToken);
  98. XCTAssertNil(context.FCMToken);
  99. XCTAssertEqualObjects(context.appCheckToken, self.appCheckTokenSuccess.token);
  100. [expectation fulfill];
  101. }];
  102. [self waitForExpectations:@[ expectation ] timeout:0.1];
  103. }
  104. - (void)testContextWithAppCheckOnlyError {
  105. self.appCheckFake.tokenResult = self.appCheckTokenError;
  106. FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:nil
  107. messaging:nil
  108. appCheck:self.appCheckFake];
  109. XCTestExpectation *expectation =
  110. [self expectationWithDescription:@"Completion handler should succeed without Auth."];
  111. [provider getContext:^(FUNContext *context, NSError *_Nullable error) {
  112. XCTAssertNotNil(context);
  113. XCTAssertNil(error);
  114. XCTAssertNil(context.authToken);
  115. XCTAssertNil(context.FCMToken);
  116. // Expect a dummy token to be passed even in the case of app check error.
  117. XCTAssertEqualObjects(context.appCheckToken, self.appCheckTokenError.token);
  118. [expectation fulfill];
  119. }];
  120. [self waitForExpectations:@[ expectation ] timeout:0.1];
  121. }
  122. - (void)testAllContextsAvailableSuccess {
  123. self.appCheckFake.tokenResult = self.appCheckTokenSuccess;
  124. FIRAuthInteropFake *auth = [[FIRAuthInteropFake alloc] initWithToken:@"token"
  125. userID:@"userID"
  126. error:nil];
  127. FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:auth
  128. messaging:self.messagingFake
  129. appCheck:self.appCheckFake];
  130. XCTestExpectation *expectation =
  131. [self expectationWithDescription:@"Completion handler should succeed without Auth."];
  132. [provider getContext:^(FUNContext *context, NSError *_Nullable error) {
  133. XCTAssertNotNil(context);
  134. XCTAssertNil(error);
  135. XCTAssert([context.authToken isEqualToString:@"token"]);
  136. XCTAssert([context.FCMToken isEqualToString:self.messagingFake.FCMToken]);
  137. XCTAssertEqualObjects(context.appCheckToken, self.appCheckTokenSuccess.token);
  138. [expectation fulfill];
  139. }];
  140. [self waitForExpectations:@[ expectation ] timeout:0.1];
  141. }
  142. - (void)testAllContextsAuthAndAppCheckError {
  143. self.appCheckFake.tokenResult = self.appCheckTokenError;
  144. NSError *authError = [[NSError alloc] initWithDomain:@"com.functions.tests" code:4 userInfo:nil];
  145. FIRAuthInteropFake *auth = [[FIRAuthInteropFake alloc] initWithToken:nil
  146. userID:nil
  147. error:authError];
  148. FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:auth
  149. messaging:self.messagingFake
  150. appCheck:self.appCheckFake];
  151. XCTestExpectation *expectation =
  152. [self expectationWithDescription:@"Completion handler should succeed without Auth."];
  153. [provider getContext:^(FUNContext *context, NSError *_Nullable error) {
  154. XCTAssertNotNil(context);
  155. XCTAssertEqual(error, auth.error);
  156. XCTAssertNil(context.authToken);
  157. XCTAssert([context.FCMToken isEqualToString:self.messagingFake.FCMToken]);
  158. // Expect a dummy token to be passed even in the case of app check error.
  159. XCTAssertEqualObjects(context.appCheckToken, self.appCheckTokenError.token);
  160. [expectation fulfill];
  161. }];
  162. [self waitForExpectations:@[ expectation ] timeout:0.1];
  163. }
  164. @end