FIRVerifyCustomTokenRequestTests.m 4.3 KB

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