FIRGetProjectConfigResponseTests.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/Sources/Public/FirebaseAuth/FIRAuthErrors.h"
  18. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  19. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetProjectConfigRequest.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetProjectConfigResponse.h"
  21. #import "FirebaseAuth/Sources/Utilities/FIRAuthInternalErrors.h"
  22. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  23. /** @var kTestAPIKey
  24. @brief Fake API key used for testing.
  25. */
  26. static NSString *const kTestAPIKey = @"APIKey";
  27. /** @var kTestFirebaseAppID
  28. @brief Fake Firebase app ID used for testing.
  29. */
  30. static NSString *const kTestFirebaseAppID = @"appID";
  31. /** @var kTestProjectID
  32. @brief Fake project ID used for testing.
  33. */
  34. static NSString *const kTestProjectID = @"21141651616";
  35. /** @var kTestDomain1
  36. @brief Fake allowlisted domain used for testing.
  37. */
  38. static NSString *const kTestDomain1 = @"localhost";
  39. /** @var kTestDomain2
  40. @brief Fake allowlisted domain used for testing.
  41. */
  42. static NSString *const kTestDomain2 = @"example.firebaseapp.com";
  43. /** @var kMissingAPIKeyErrorMessage
  44. @brief The error message the server would respond with if the API Key was missing.
  45. */
  46. static NSString *const kMissingAPIKeyErrorMessage = @"MISSING_API_KEY";
  47. @interface FIRGetProjectConfigResponseTests : XCTestCase
  48. @end
  49. @implementation FIRGetProjectConfigResponseTests {
  50. /** @var _RPCIssuer
  51. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  52. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  53. */
  54. FIRFakeBackendRPCIssuer *_RPCIssuer;
  55. }
  56. - (void)setUp {
  57. [super setUp];
  58. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  59. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  60. _RPCIssuer = RPCIssuer;
  61. }
  62. - (void)tearDown {
  63. _RPCIssuer = nil;
  64. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  65. [super tearDown];
  66. }
  67. /** @fn testMissingAPIKeyError
  68. @brief This test simulates a missing API key error. Since the API key is provided to the backend
  69. from the auth library this error should map to an internal error.
  70. */
  71. - (void)testMissingAPIKeyError {
  72. FIRAuthRequestConfiguration *requestConfiguration =
  73. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey appID:kTestFirebaseAppID];
  74. FIRGetProjectConfigRequest *request =
  75. [[FIRGetProjectConfigRequest alloc] initWithRequestConfiguration:requestConfiguration];
  76. __block BOOL callbackInvoked;
  77. __block FIRGetProjectConfigResponse *RPCResponse;
  78. __block NSError *RPCError;
  79. [FIRAuthBackend getProjectConfig:request
  80. callback:^(FIRGetProjectConfigResponse *_Nullable response,
  81. NSError *_Nullable error) {
  82. callbackInvoked = YES;
  83. RPCResponse = response;
  84. RPCError = error;
  85. }];
  86. [_RPCIssuer respondWithServerErrorMessage:kMissingAPIKeyErrorMessage];
  87. XCTAssert(callbackInvoked);
  88. XCTAssertNotNil(RPCError);
  89. XCTAssertEqualObjects(RPCError.domain, FIRAuthErrorDomain);
  90. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInternalError);
  91. XCTAssertNotNil(RPCError.userInfo[NSUnderlyingErrorKey]);
  92. }
  93. /** @fn testSuccessFulGetProjectConfigRequest
  94. @brief This test simulates a successful @c getProjectConfig Flow.
  95. */
  96. - (void)testSuccessFulGetProjectConfigRequest {
  97. FIRAuthRequestConfiguration *requestConfiguration =
  98. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey appID:kTestFirebaseAppID];
  99. FIRGetProjectConfigRequest *request =
  100. [[FIRGetProjectConfigRequest alloc] initWithRequestConfiguration:requestConfiguration];
  101. __block BOOL callbackInvoked;
  102. __block FIRGetProjectConfigResponse *RPCResponse;
  103. __block NSError *RPCError;
  104. [FIRAuthBackend getProjectConfig:request
  105. callback:^(FIRGetProjectConfigResponse *_Nullable response,
  106. NSError *_Nullable error) {
  107. callbackInvoked = YES;
  108. RPCResponse = response;
  109. RPCError = error;
  110. }];
  111. [_RPCIssuer respondWithJSON:@{
  112. @"projectId" : kTestProjectID,
  113. @"authorizedDomains" : @[ kTestDomain1, kTestDomain2 ]
  114. }];
  115. XCTAssert(callbackInvoked);
  116. XCTAssertNil(RPCError);
  117. XCTAssertEqualObjects(kTestProjectID, RPCResponse.projectID);
  118. XCTAssertEqualObjects(kTestDomain1, RPCResponse.authorizedDomains[0]);
  119. XCTAssertEqualObjects(kTestDomain2, RPCResponse.authorizedDomains[1]);
  120. }
  121. @end