FIRVerifyCustomTokenRequestTests.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "FIRGetOOBConfirmationCodeResponse.h"
  20. #import "FIRVerifyCustomTokenRequest.h"
  21. #import "FIRVerifyCustomTokenResponse.h"
  22. #import "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. }
  53. - (void)setUp {
  54. [super setUp];
  55. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  56. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  57. _RPCIssuer = RPCIssuer;
  58. }
  59. - (void)tearDown {
  60. _RPCIssuer = nil;
  61. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  62. [super tearDown];
  63. }
  64. /** @fn testVerifyCustomTokenRequest
  65. @brief Tests the verify custom token request.
  66. */
  67. - (void)testVerifyCustomTokenRequest {
  68. FIRVerifyCustomTokenRequest *request =
  69. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken APIKey:kTestAPIKey];
  70. request.returnSecureToken = NO;
  71. [FIRAuthBackend verifyCustomToken:request
  72. callback:^(FIRVerifyCustomTokenResponse *_Nullable response,
  73. NSError *_Nullable error) {
  74. }];
  75. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  76. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  77. XCTAssertNotNil(_RPCIssuer.decodedRequest[kTestTokenKey]);
  78. XCTAssertNil(_RPCIssuer.decodedRequest[kReturnSecureTokenKey]);
  79. }
  80. /** @fn testVerifyCustomTokenRequestOptionalFields
  81. @brief Tests the verify custom token request with optional fields.
  82. */
  83. - (void)testVerifyCustomTokenRequestOptionalFields {
  84. FIRVerifyCustomTokenRequest *request =
  85. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken APIKey:kTestAPIKey];
  86. [FIRAuthBackend verifyCustomToken:request
  87. callback:^(FIRVerifyCustomTokenResponse *_Nullable response,
  88. NSError *_Nullable error) {
  89. }];
  90. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  91. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  92. XCTAssertNotNil(_RPCIssuer.decodedRequest[kTestTokenKey]);
  93. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  94. }
  95. @end