FIRAuthBackendCreateAuthURITests.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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/Backend/FIRAuthBackend.h"
  18. #import "FirebaseAuth/Sources/Backend/RPC/FIRCreateAuthURIRequest.h"
  19. #import "FirebaseAuth/Sources/Backend/RPC/FIRCreateAuthURIResponse.h"
  20. #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
  21. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  22. /** @var kTestIdentifier
  23. @brief A test value for @c FIRCreateAuthURIRequest.identifier
  24. */
  25. static NSString *const kTestIdentifier = @"identifier_value";
  26. /** @var kTestContinueURI
  27. @brief A test value for @c FIRCreateAuthURIRequest.continueURI
  28. */
  29. static NSString *const kTestContinueURI = @"https://www.example.com/";
  30. /** @var kTestAPIKey
  31. @brief A test value for @c FIRCreateAuthURIRequest.APIKey
  32. */
  33. static NSString *const kTestAPIKey = @"apikey_value";
  34. /** @var kTestFirebaseAppID
  35. @brief A test value for the Firebase app ID
  36. */
  37. static NSString *const kTestFirebaseAppID = @"appID";
  38. /** @var kTestExpectedRequestURL
  39. @brief The URL we are expecting should be requested by valid requests.
  40. */
  41. static NSString *const kTestExpectedRequestURL =
  42. @"https://www.googleapis.com/identitytoolkit/v3/relyingparty/createAuthUri?key=apikey_value";
  43. /** @var kTestExpectedKind
  44. @brief The expected value for the "kind" parameter of a successful response.
  45. */
  46. static NSString *const kTestExpectedKind = @"identitytoolkit#CreateAuthUriResponse";
  47. /** @var kTestProviderID1
  48. @brief A valid value for a provider ID in the @c FIRCreateAuthURIResponse.allProviders array.
  49. */
  50. static NSString *const kTestProviderID1 = @"google.com";
  51. /** @var kTestProviderID2
  52. @brief A valid value for a provider ID in the @c FIRCreateAuthURIResponse.allProviders array.
  53. */
  54. static NSString *const kTestProviderID2 = @"facebook.com";
  55. /** @class FIRAuthBackendCreateAuthURITests
  56. @brief Unit tests for createAuthURI.
  57. */
  58. @interface FIRAuthBackendCreateAuthURITests : XCTestCase
  59. @end
  60. @implementation FIRAuthBackendCreateAuthURITests
  61. - (void)testRequestAndResponseEncoding {
  62. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  63. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  64. FIRAuthRequestConfiguration *requestConfiguration =
  65. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey appID:kTestFirebaseAppID];
  66. FIRCreateAuthURIRequest *request =
  67. [[FIRCreateAuthURIRequest alloc] initWithIdentifier:kTestIdentifier
  68. continueURI:kTestContinueURI
  69. requestConfiguration:requestConfiguration];
  70. __block FIRCreateAuthURIResponse *createAuthURIResponse;
  71. __block NSError *createAuthURIError;
  72. __block BOOL callbackInvoked;
  73. [FIRAuthBackend
  74. createAuthURI:request
  75. callback:^(FIRCreateAuthURIResponse *_Nullable response, NSError *_Nullable error) {
  76. callbackInvoked = YES;
  77. createAuthURIResponse = response;
  78. createAuthURIError = error;
  79. }];
  80. XCTAssertEqualObjects(RPCIssuer.requestURL.absoluteString, kTestExpectedRequestURL);
  81. XCTAssertEqualObjects(RPCIssuer.decodedRequest[@"identifier"], kTestIdentifier);
  82. XCTAssertEqualObjects(RPCIssuer.decodedRequest[@"continueUri"], kTestContinueURI);
  83. [RPCIssuer respondWithJSON:@{
  84. @"kind" : kTestExpectedKind,
  85. @"allProviders" : @[ kTestProviderID1, kTestProviderID2 ]
  86. }];
  87. XCTAssert(callbackInvoked);
  88. XCTAssertNil(createAuthURIError);
  89. XCTAssertEqual(createAuthURIResponse.allProviders.count, 2);
  90. XCTAssertEqualObjects(createAuthURIResponse.allProviders[0], kTestProviderID1);
  91. XCTAssertEqualObjects(createAuthURIResponse.allProviders[1], kTestProviderID2);
  92. }
  93. @end