FUNContextProviderTests.m 2.9 KB

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