FIRAuthBackendCreateAuthURITests.m 4.0 KB

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