FIRVerifyCustomTokenRequestTests.m 4.4 KB

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