FIRGetProjectConfigResponseTests.m 5.0 KB

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