FIREmailLinkRequestTests.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "FIRAuthErrors.h"
  18. #import "FIRAuthBackend.h"
  19. #import "FIREmailLinkSignInRequest.h"
  20. #import "FIREmailLinkSignInResponse.h"
  21. #import "FIRFakeBackendRPCIssuer.h"
  22. /** @var kTestAPIKey
  23. @brief Fake API key used for testing.
  24. */
  25. static NSString *const kTestAPIKey = @"APIKey";
  26. /** @var kTestEmail
  27. @brief The key for the "email" value in the request.
  28. */
  29. static NSString *const kTestEmail = @"TestEmail@email.com";
  30. /** @var kTestOOBCode
  31. @brief The test value for the "oobCode" in the request.
  32. */
  33. static NSString *const kTestOOBCode = @"TestOOBCode";
  34. /** @var kTestIDToken
  35. @brief The test value for "idToken" in the request.
  36. */
  37. static NSString *const kTestIDToken = @"testIDToken";
  38. /** @var kEmailKey
  39. @brief The key for the "identifier" value in the request.
  40. */
  41. static NSString *const kEmailKey = @"email";
  42. /** @var kEmailLinkKey
  43. @brief The key for the "oobCode" value in the request.
  44. */
  45. static NSString *const kOOBCodeKey = @"oobCode";
  46. /** @var kIDTokenKey
  47. @brief The key for the "IDToken" value in the request.
  48. */
  49. static NSString *const kIDTokenKey = @"idToken";
  50. /** @var kExpectedAPIURL
  51. @brief The value of the expected URL (including the backend endpoint) in the request.
  52. */
  53. static NSString *const kExpectedAPIURL =
  54. @"https://www.googleapis.com/identitytoolkit/v3/relyingparty/emailLinkSignin?key=APIKey";
  55. /** @class FIREmailLinkRequestTests
  56. @brief Tests for @c FIREmailLinkRequests.
  57. */
  58. @interface FIREmailLinkRequestTests : XCTestCase
  59. @end
  60. @implementation FIREmailLinkRequestTests {
  61. /** @var _RPCIssuer
  62. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  63. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  64. */
  65. FIRFakeBackendRPCIssuer *_RPCIssuer;
  66. /** @var _requestConfiguration
  67. @brief This is the request configuration used for testing.
  68. */
  69. FIRAuthRequestConfiguration *_requestConfiguration;
  70. }
  71. - (void)setUp {
  72. [super setUp];
  73. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  74. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  75. _RPCIssuer = RPCIssuer;
  76. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  77. }
  78. - (void)tearDown {
  79. _RPCIssuer = nil;
  80. _requestConfiguration = nil;
  81. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  82. [super tearDown];
  83. }
  84. /** @fn testEmailLinkRequestCreation
  85. @brief Tests the email link sign-in request with mandatory parameters.
  86. */
  87. - (void)testEmailLinkRequest {
  88. FIREmailLinkSignInRequest *request =
  89. [[FIREmailLinkSignInRequest alloc] initWithEmail:kTestEmail
  90. oobCode:kTestOOBCode
  91. requestConfiguration:_requestConfiguration];
  92. [FIRAuthBackend emailLinkSignin:request callback:^(FIREmailLinkSignInResponse *_Nullable response,
  93. NSError *_Nullable error) {
  94. }];
  95. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  96. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  97. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  98. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kOOBCodeKey], kTestOOBCode);
  99. XCTAssertNil(_RPCIssuer.decodedRequest[kIDTokenKey]);
  100. }
  101. /** @fn testEmailLinkRequestCreationOptional
  102. @brief Tests the email link sign-in request with mandatory parameters and optional ID token.
  103. */
  104. - (void)testEmailLinkRequestCreationOptional {
  105. FIREmailLinkSignInRequest *request =
  106. [[FIREmailLinkSignInRequest alloc] initWithEmail:kTestEmail
  107. oobCode:kTestOOBCode
  108. requestConfiguration:_requestConfiguration];
  109. request.IDToken = kTestIDToken;
  110. [FIRAuthBackend emailLinkSignin:request callback:^(FIREmailLinkSignInResponse *_Nullable response,
  111. NSError *_Nullable error) {
  112. }];
  113. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  114. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  115. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  116. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kOOBCodeKey], kTestOOBCode);
  117. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kIDTokenKey], kTestIDToken);
  118. }
  119. @end