FIRGetOOBConfirmationCodeRequestTests.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "FIRGetOOBConfirmationCodeRequest.h"
  20. #import "FIRGetOOBConfirmationCodeResponse.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 kExpectedAPIURL
  27. @brief The expected URL for the test calls.
  28. */
  29. static NSString *const kExpectedAPIURL =
  30. @"https://www.googleapis.com/identitytoolkit/v3/relyingparty/getOobConfirmationCode?key=APIKey";
  31. /** @var kRequestTypeKey
  32. @brief The name of the required "requestType" property in the request.
  33. */
  34. static NSString *const kRequestTypeKey = @"requestType";
  35. /** @var kPasswordResetRequestTypeValue
  36. @brief The value for the "PASSWORD_RESET" request type.
  37. */
  38. static NSString *const kPasswordResetRequestTypeValue = @"PASSWORD_RESET";
  39. /** @var kVerifyEmailRequestTypeValue
  40. @brief The value for the "VERIFY_EMAIL" request type.
  41. */
  42. static NSString *const kVerifyEmailRequestTypeValue = @"VERIFY_EMAIL";
  43. /** @var kEmailKey
  44. @brief The name of the "email" property in the request.
  45. */
  46. static NSString *const kEmailKey = @"email";
  47. /** @var kTestEmail
  48. @brief Testing user email adadress.
  49. */
  50. static NSString *const kTestEmail = @"test@gmail.com";
  51. /** @var kAccessTokenKey
  52. @brief The name of the "accessToken" property in the request.
  53. */
  54. static NSString *const kAccessTokenKey = @"idToken";
  55. /** @var kTestAccessToken
  56. @brief Testing access token.
  57. */
  58. static NSString *const kTestAccessToken = @"ACCESS_TOKEN";
  59. /** @class FIRGetOOBConfirmationCodeRequestTests
  60. @brief Tests for @c FIRGetOOBConfirmationCodeRequest.
  61. */
  62. @interface FIRGetOOBConfirmationCodeRequestTests : XCTestCase
  63. @end
  64. @implementation FIRGetOOBConfirmationCodeRequestTests {
  65. /** @var _RPCIssuer
  66. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  67. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  68. */
  69. FIRFakeBackendRPCIssuer *_RPCIssuer;
  70. }
  71. - (void)setUp {
  72. [super setUp];
  73. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  74. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  75. _RPCIssuer = RPCIssuer;
  76. }
  77. - (void)tearDown {
  78. _RPCIssuer = nil;
  79. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  80. [super tearDown];
  81. }
  82. /** @fn testPasswordResetRequest
  83. @brief Tests the encoding of a password reset request.
  84. */
  85. - (void)testPasswordResetRequest {
  86. FIRGetOOBConfirmationCodeRequest *request =
  87. [FIRGetOOBConfirmationCodeRequest passwordResetRequestWithEmail:kTestEmail
  88. APIKey:kTestAPIKey];
  89. __block BOOL callbackInvoked;
  90. __block FIRGetOOBConfirmationCodeResponse *RPCResponse;
  91. __block NSError *RPCError;
  92. [FIRAuthBackend getOOBConfirmationCode:request
  93. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  94. NSError *_Nullable error) {
  95. callbackInvoked = YES;
  96. RPCResponse = response;
  97. RPCError = error;
  98. }];
  99. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  100. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  101. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  102. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  103. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kRequestTypeKey], kPasswordResetRequestTypeValue);
  104. }
  105. /** @fn testEmailVerificationRequest
  106. @brief Tests the encoding of an email verification request.
  107. */
  108. - (void)testEmailVerificationRequest {
  109. FIRGetOOBConfirmationCodeRequest *request =
  110. [FIRGetOOBConfirmationCodeRequest verifyEmailRequestWithAccessToken:kTestAccessToken
  111. APIKey:kTestAPIKey];
  112. __block BOOL callbackInvoked;
  113. __block FIRGetOOBConfirmationCodeResponse *RPCResponse;
  114. __block NSError *RPCError;
  115. [FIRAuthBackend getOOBConfirmationCode:request
  116. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  117. NSError *_Nullable error) {
  118. callbackInvoked = YES;
  119. RPCResponse = response;
  120. RPCError = error;
  121. }];
  122. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  123. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  124. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  125. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kAccessTokenKey], kTestAccessToken);
  126. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kRequestTypeKey], kVerifyEmailRequestTypeValue);
  127. }
  128. @end