FIRVerifyCustomTokenRequestTests.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/Public/FirebaseAuth/FIRAuthErrors.h"
  18. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  19. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetOOBConfirmationCodeResponse.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyCustomTokenRequest.h"
  21. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyCustomTokenResponse.h"
  22. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  23. /** @var kTestAPIKey
  24. @brief Fake API key used for testing.
  25. */
  26. static NSString *const kTestAPIKey = @"APIKey";
  27. /** @var kTestFirebaseAppID
  28. @brief Fake Firebase app ID used for testing.
  29. */
  30. static NSString *const kTestFirebaseAppID = @"appID";
  31. /** @var kTestTokenKey
  32. @brief The name of the "token" property in the response.
  33. */
  34. static NSString *const kTestTokenKey = @"token";
  35. /** @var kTestToken
  36. @brief testing token.
  37. */
  38. static NSString *const kTestToken = @"test token";
  39. /** @var kReturnSecureTokenKey
  40. @brief The key for the "returnSecureToken" value in the request.
  41. */
  42. static NSString *const kReturnSecureTokenKey = @"returnSecureToken";
  43. /** @var kExpectedAPIURL
  44. @brief The expected URL for test calls.
  45. */
  46. static NSString *const kExpectedAPIURL =
  47. @"https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=APIKey";
  48. @interface FIRVerifyCustomTokenRequestTests : XCTestCase
  49. @end
  50. @implementation FIRVerifyCustomTokenRequestTests {
  51. /** @var _RPCIssuer
  52. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  53. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  54. */
  55. FIRFakeBackendRPCIssuer *_RPCIssuer;
  56. /** @var _requestConfiguration
  57. @brief This is the request configuration used for testing.
  58. */
  59. FIRAuthRequestConfiguration *_requestConfiguration;
  60. }
  61. - (void)setUp {
  62. [super setUp];
  63. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  64. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  65. _RPCIssuer = RPCIssuer;
  66. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
  67. appID:kTestFirebaseAppID];
  68. }
  69. - (void)tearDown {
  70. _RPCIssuer = nil;
  71. _requestConfiguration = nil;
  72. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  73. [super tearDown];
  74. }
  75. /** @fn testVerifyCustomTokenRequest
  76. @brief Tests the verify custom token request.
  77. */
  78. - (void)testVerifyCustomTokenRequest {
  79. FIRVerifyCustomTokenRequest *request =
  80. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken
  81. requestConfiguration:_requestConfiguration];
  82. request.returnSecureToken = NO;
  83. [FIRAuthBackend verifyCustomToken:request
  84. callback:^(FIRVerifyCustomTokenResponse *_Nullable response,
  85. NSError *_Nullable error){
  86. }];
  87. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  88. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  89. XCTAssertNotNil(_RPCIssuer.decodedRequest[kTestTokenKey]);
  90. XCTAssertNil(_RPCIssuer.decodedRequest[kReturnSecureTokenKey]);
  91. }
  92. /** @fn testVerifyCustomTokenRequestOptionalFields
  93. @brief Tests the verify custom token request with optional fields.
  94. */
  95. - (void)testVerifyCustomTokenRequestOptionalFields {
  96. FIRVerifyCustomTokenRequest *request =
  97. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken
  98. requestConfiguration:_requestConfiguration];
  99. [FIRAuthBackend verifyCustomToken:request
  100. callback:^(FIRVerifyCustomTokenResponse *_Nullable response,
  101. NSError *_Nullable error){
  102. }];
  103. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  104. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  105. XCTAssertNotNil(_RPCIssuer.decodedRequest[kTestTokenKey]);
  106. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  107. }
  108. @end