FIREmailLinkRequestTests.m 5.1 KB

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