FIRGetProjectConfigResponseTests.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. @import FirebaseAuth;
  18. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  19. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  20. /** @var kTestAPIKey
  21. @brief Fake API key used for testing.
  22. */
  23. static NSString *const kTestAPIKey = @"APIKey";
  24. /** @var kTestFirebaseAppID
  25. @brief Fake Firebase app ID used for testing.
  26. */
  27. static NSString *const kTestFirebaseAppID = @"appID";
  28. /** @var kTestProjectID
  29. @brief Fake project ID used for testing.
  30. */
  31. static NSString *const kTestProjectID = @"21141651616";
  32. /** @var kTestDomain1
  33. @brief Fake allowlisted domain used for testing.
  34. */
  35. static NSString *const kTestDomain1 = @"localhost";
  36. /** @var kTestDomain2
  37. @brief Fake allowlisted domain used for testing.
  38. */
  39. static NSString *const kTestDomain2 = @"example.firebaseapp.com";
  40. /** @var kMissingAPIKeyErrorMessage
  41. @brief The error message the server would respond with if the API Key was missing.
  42. */
  43. static NSString *const kMissingAPIKeyErrorMessage = @"MISSING_API_KEY";
  44. @interface FIRGetProjectConfigResponseTests : XCTestCase
  45. @end
  46. @implementation FIRGetProjectConfigResponseTests {
  47. /** @var _RPCIssuer
  48. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  49. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  50. */
  51. FIRFakeBackendRPCIssuer *_RPCIssuer;
  52. }
  53. - (void)setUp {
  54. [super setUp];
  55. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  56. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  57. _RPCIssuer = RPCIssuer;
  58. }
  59. - (void)tearDown {
  60. _RPCIssuer = nil;
  61. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  62. [super tearDown];
  63. }
  64. /** @fn testMissingAPIKeyError
  65. @brief This test simulates a missing API key error. Since the API key is provided to the backend
  66. from the auth library this error should map to an internal error.
  67. */
  68. - (void)testMissingAPIKeyError {
  69. FIRAuthRequestConfiguration *requestConfiguration =
  70. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey appID:kTestFirebaseAppID];
  71. FIRGetProjectConfigRequest *request =
  72. [[FIRGetProjectConfigRequest alloc] initWithRequestConfiguration:requestConfiguration];
  73. __block BOOL callbackInvoked;
  74. __block FIRGetProjectConfigResponse *RPCResponse;
  75. __block NSError *RPCError;
  76. [FIRAuthBackend getProjectConfig:request
  77. callback:^(FIRGetProjectConfigResponse *_Nullable response,
  78. NSError *_Nullable error) {
  79. callbackInvoked = YES;
  80. RPCResponse = response;
  81. RPCError = error;
  82. }];
  83. [_RPCIssuer respondWithServerErrorMessage:kMissingAPIKeyErrorMessage];
  84. XCTAssert(callbackInvoked);
  85. XCTAssertNotNil(RPCError);
  86. XCTAssertEqualObjects(RPCError.domain, [FIRAuthErrors FIRAuthErrorDomain]);
  87. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInternalError);
  88. XCTAssertNotNil(RPCError.userInfo[NSUnderlyingErrorKey]);
  89. }
  90. /** @fn testSuccessFulGetProjectConfigRequest
  91. @brief This test simulates a successful @c getProjectConfig Flow.
  92. */
  93. - (void)testSuccessFulGetProjectConfigRequest {
  94. FIRAuthRequestConfiguration *requestConfiguration =
  95. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey appID:kTestFirebaseAppID];
  96. FIRGetProjectConfigRequest *request =
  97. [[FIRGetProjectConfigRequest alloc] initWithRequestConfiguration:requestConfiguration];
  98. __block BOOL callbackInvoked;
  99. __block FIRGetProjectConfigResponse *RPCResponse;
  100. __block NSError *RPCError;
  101. [FIRAuthBackend getProjectConfig:request
  102. callback:^(FIRGetProjectConfigResponse *_Nullable response,
  103. NSError *_Nullable error) {
  104. callbackInvoked = YES;
  105. RPCResponse = response;
  106. RPCError = error;
  107. }];
  108. [_RPCIssuer respondWithJSON:@{
  109. @"projectId" : kTestProjectID,
  110. @"authorizedDomains" : @[ kTestDomain1, kTestDomain2 ]
  111. }];
  112. XCTAssert(callbackInvoked);
  113. XCTAssertNil(RPCError);
  114. XCTAssertEqualObjects(kTestProjectID, RPCResponse.projectID);
  115. XCTAssertEqualObjects(kTestDomain1, RPCResponse.authorizedDomains[0]);
  116. XCTAssertEqualObjects(kTestDomain2, RPCResponse.authorizedDomains[1]);
  117. }
  118. @end