FUNContextProviderTests.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/FIRMessagingInteropFake.h"
  18. @interface FUNContextProviderTests : XCTestCase
  19. @property(nonatomic) FIRMessagingInteropFake *messagingFake;
  20. @end
  21. @implementation FUNContextProviderTests
  22. - (void)setUp {
  23. self.messagingFake = [[FIRMessagingInteropFake alloc] init];
  24. }
  25. - (void)testContextWithAuth {
  26. FIRAuthInteropFake *auth = [[FIRAuthInteropFake alloc] initWithToken:@"token"
  27. userID:@"userID"
  28. error:nil];
  29. FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:(id<FIRAuthInterop>)auth
  30. messaging:self.messagingFake];
  31. XCTestExpectation *expectation =
  32. [self expectationWithDescription:@"Context should have auth keys."];
  33. [provider getContext:^(FUNContext *_Nullable context, NSError *_Nullable error) {
  34. XCTAssert([context.authToken isEqualToString:@"token"]);
  35. XCTAssert([context.FCMToken isEqualToString:self.messagingFake.FCMToken]);
  36. XCTAssertNil(error);
  37. [expectation fulfill];
  38. }];
  39. [self waitForExpectations:@[ expectation ] timeout:0.1];
  40. }
  41. - (void)testContextWithAuthError {
  42. NSError *authError = [[NSError alloc] initWithDomain:@"com.functions.tests" code:4 userInfo:nil];
  43. FIRAuthInteropFake *auth = [[FIRAuthInteropFake alloc] initWithToken:nil
  44. userID:nil
  45. error:authError];
  46. FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:(id<FIRAuthInterop>)auth
  47. messaging:self.messagingFake];
  48. XCTestExpectation *expectation =
  49. [self expectationWithDescription:@"Completion handler should fail with Auth error."];
  50. [provider getContext:^(FUNContext *_Nullable context, NSError *_Nullable error) {
  51. XCTAssertNil(context);
  52. XCTAssertEqual(error, auth.error);
  53. [expectation fulfill];
  54. }];
  55. [self waitForExpectations:@[ expectation ] timeout:0.1];
  56. }
  57. - (void)testContextWithoutAuth {
  58. FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:nil messaging:nil];
  59. XCTestExpectation *expectation =
  60. [self expectationWithDescription:@"Completion handler should succeed without Auth."];
  61. [provider getContext:^(FUNContext *_Nullable context, NSError *_Nullable error) {
  62. XCTAssertNil(error);
  63. XCTAssertNil(context.authToken);
  64. XCTAssertNil(context.FCMToken);
  65. [expectation fulfill];
  66. }];
  67. [self waitForExpectations:@[ expectation ] timeout:0.1];
  68. }
  69. @end