FIRVerifyCustomTokenResponseTests.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "FIRVerifyCustomTokenRequest.h"
  20. #import "FIRVerifyCustomTokenResponse.h"
  21. #import "FIRFakeBackendRPCIssuer.h"
  22. /** @var kTestToken
  23. @brief testing token.
  24. */
  25. static NSString *const kTestToken = @"test token";
  26. /** @var kTestAPIKey
  27. @brief Fake API key used for testing.
  28. */
  29. static NSString *const kTestAPIKey = @"APIKey";
  30. /** @var kIDTokenKey
  31. @brief The name of the "IDToken" property in the response.
  32. */
  33. static NSString *const kIDTokenKey = @"idToken";
  34. /** @var kExpiresInKey
  35. @brief The name of the "expiresIn" property in the response.
  36. */
  37. static NSString *const kExpiresInKey = @"expiresIn";
  38. /** @var kRefreshTokenKey
  39. @brief The name of the "refreshToken" property in the response.
  40. */
  41. static NSString *const kRefreshTokenKey = @"refreshToken";
  42. /** @var kTestIDToken
  43. @brief Testing ID token for verifying assertion.
  44. */
  45. static NSString *const kTestIDToken = @"ID_TOKEN";
  46. /** @var kTestExpiresIn
  47. @brief Fake token expiration time.
  48. */
  49. static NSString *const kTestExpiresIn = @"12345";
  50. /** @var kTestRefreshToken
  51. @brief Fake refresh token.
  52. */
  53. static NSString *const kTestRefreshToken = @"REFRESH_TOKEN";
  54. /** @var kMissingTokenCustomErrorMessage
  55. @brief This is the error message the server will respond with if token field is missing in
  56. request.
  57. */
  58. static NSString *const kMissingCustomTokenErrorMessage = @"MISSING_CUSTOM_TOKEN";
  59. /** @var kInvalidTokenCustomErrorMessage
  60. @brief This is the error message the server will respond with if there is a validation error
  61. with the custom token.
  62. */
  63. static NSString *const kInvalidCustomTokenErrorMessage = @"INVALID_CUSTOM_TOKEN";
  64. /** @var kInvalidCustomTokenServerErrorMessage
  65. @brief This is the error message the server will respond with if there is a validation error
  66. with the custom token. This message contains error details from the server.
  67. */
  68. static NSString *const kInvalidCustomTokenServerErrorMessage =
  69. @"INVALID_CUSTOM_TOKEN : Detailed Error";
  70. /** @var kInvalidCustomTokenEmptyServerErrorMessage
  71. @brief This is the error message the server will respond with if there is a validation error
  72. with the custom token.
  73. @remarks This message deliberately has no content where it should contain
  74. error details.
  75. */
  76. static NSString *const kInvalidCustomTokenEmptyServerErrorMessage =
  77. @"INVALID_CUSTOM_TOKEN :";
  78. /** @var kInvalidCustomTokenErrorDetails
  79. @brief This is the test detailed error message that could be returned by the backend.
  80. */
  81. static NSString *const kInvalidCustomTokenErrorDetails = @"Detailed Error";
  82. /** @var kCredentialMismatchErrorMessage
  83. @brief This is the error message the server will respond with if the service API key belongs to
  84. different projects.
  85. */
  86. static NSString *const kCredentialMismatchErrorMessage = @"CREDENTIAL_MISMATCH:";
  87. /** @var kAllowedTimeDifference
  88. @brief Allowed difference when comparing times because of execution time and floating point
  89. error.
  90. */
  91. static const double kAllowedTimeDifference = 0.1;
  92. @interface FIRVerifyCustomTokenResponseTests : XCTestCase
  93. @end
  94. @implementation FIRVerifyCustomTokenResponseTests {
  95. /** @var _RPCIssuer
  96. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  97. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  98. */
  99. FIRFakeBackendRPCIssuer *_RPCIssuer;
  100. }
  101. - (void)setUp {
  102. [super setUp];
  103. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  104. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  105. _RPCIssuer = RPCIssuer;
  106. }
  107. - (void)tearDown {
  108. _RPCIssuer = nil;
  109. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  110. [super tearDown];
  111. }
  112. /** @fn testInvalidCustomTokenError
  113. @brief This test simulates @c invalidCustomTokenError with @c
  114. FIRAuthErrorCodeINvalidCustomToken error code.
  115. */
  116. - (void)testInvalidCustomTokenError {
  117. FIRVerifyCustomTokenRequest *request =
  118. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken APIKey:kTestAPIKey];
  119. __block BOOL callbackInvoked;
  120. __block FIRVerifyCustomTokenResponse *RPCResponse;
  121. __block NSError *RPCError;
  122. [FIRAuthBackend verifyCustomToken:request
  123. callback:^(FIRVerifyCustomTokenResponse*_Nullable response,
  124. NSError *_Nullable error) {
  125. callbackInvoked = YES;
  126. RPCResponse = response;
  127. RPCError = error;
  128. }];
  129. [_RPCIssuer respondWithServerErrorMessage:kInvalidCustomTokenErrorMessage];
  130. XCTAssert(callbackInvoked);
  131. XCTAssertNotNil(RPCError);
  132. XCTAssertNil(RPCResponse);
  133. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidCustomToken);
  134. }
  135. /** @fn testInvalidCustomTokenServerError
  136. @brief This test simulates @c invalidCustomTokenError with @c
  137. FIRAuthErrorCodeINvalidCustomToken error code, with a custom message from the server.
  138. */
  139. - (void)testInvalidCustomTokenServerError {
  140. FIRVerifyCustomTokenRequest *request =
  141. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken APIKey:kTestAPIKey];
  142. __block BOOL callbackInvoked;
  143. __block FIRVerifyCustomTokenResponse *RPCResponse;
  144. __block NSError *RPCError;
  145. [FIRAuthBackend verifyCustomToken:request
  146. callback:^(FIRVerifyCustomTokenResponse*_Nullable response,
  147. NSError *_Nullable error) {
  148. callbackInvoked = YES;
  149. RPCResponse = response;
  150. RPCError = error;
  151. }];
  152. [_RPCIssuer respondWithServerErrorMessage:kInvalidCustomTokenServerErrorMessage];
  153. NSString *errorDescription = [RPCError.userInfo valueForKey:NSLocalizedDescriptionKey];
  154. XCTAssertTrue([errorDescription isEqualToString:kInvalidCustomTokenErrorDetails]);
  155. XCTAssert(callbackInvoked);
  156. XCTAssertNotNil(RPCError);
  157. XCTAssertNil(RPCResponse);
  158. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidCustomToken);
  159. }
  160. /** @fn testEmptyServerDetailMessage
  161. @brief This test simulates @c invalidCustomTokenError with @c
  162. FIRAuthErrorCodeINvalidCustomToken error code, with an empty custom message from the server.
  163. @remarks An empty error message is not valid and therefore should not be added as an error
  164. description.
  165. */
  166. - (void)testEmptyServerDetailMessage {
  167. FIRVerifyCustomTokenRequest *request =
  168. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken APIKey:kTestAPIKey];
  169. __block BOOL callbackInvoked;
  170. __block FIRVerifyCustomTokenResponse *RPCResponse;
  171. __block NSError *RPCError;
  172. [FIRAuthBackend verifyCustomToken:request
  173. callback:^(FIRVerifyCustomTokenResponse*_Nullable response,
  174. NSError *_Nullable error) {
  175. callbackInvoked = YES;
  176. RPCResponse = response;
  177. RPCError = error;
  178. }];
  179. [_RPCIssuer respondWithServerErrorMessage:kInvalidCustomTokenEmptyServerErrorMessage];
  180. NSString *errorDescription = [RPCError.userInfo valueForKey:NSLocalizedDescriptionKey];
  181. XCTAssertFalse([errorDescription isEqualToString:@""]);
  182. XCTAssert(callbackInvoked);
  183. XCTAssertNotNil(RPCError);
  184. XCTAssertNil(RPCResponse);
  185. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidCustomToken);
  186. }
  187. /** @fn testInvalidCredentialMismatchError
  188. @brief This test simulates @c credentialMistmatchTokenError with @c
  189. FIRAuthErrorCodeCredetialMismatch error code.
  190. */
  191. - (void)testInvalidCredentialMismatchError {
  192. FIRVerifyCustomTokenRequest *request =
  193. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken APIKey:kTestAPIKey];
  194. __block BOOL callbackInvoked;
  195. __block FIRVerifyCustomTokenResponse *RPCResponse;
  196. __block NSError *RPCError;
  197. [FIRAuthBackend verifyCustomToken:request
  198. callback:^(FIRVerifyCustomTokenResponse*_Nullable response,
  199. NSError *_Nullable error) {
  200. callbackInvoked = YES;
  201. RPCResponse = response;
  202. RPCError = error;
  203. }];
  204. [_RPCIssuer respondWithServerErrorMessage:kCredentialMismatchErrorMessage];
  205. XCTAssert(callbackInvoked);
  206. XCTAssertNotNil(RPCError);
  207. XCTAssertNil(RPCResponse);
  208. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeCustomTokenMismatch);
  209. }
  210. /** @fn testSuccessfulVerifyCustomTokenResponse
  211. @brief This test simulates a successful @c VerifyCustomToken flow.
  212. */
  213. - (void)testSuccessfulVerifyCustomTokenResponse {
  214. FIRVerifyCustomTokenRequest *request =
  215. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken APIKey:kTestAPIKey];
  216. __block BOOL callbackInvoked;
  217. __block FIRVerifyCustomTokenResponse *RPCResponse;
  218. __block NSError *RPCError;
  219. [FIRAuthBackend verifyCustomToken:request
  220. callback:^(FIRVerifyCustomTokenResponse*_Nullable response,
  221. NSError *_Nullable error) {
  222. callbackInvoked = YES;
  223. RPCResponse = response;
  224. RPCError = error;
  225. }];
  226. [_RPCIssuer respondWithJSON:@{
  227. kIDTokenKey : kTestIDToken,
  228. kExpiresInKey : kTestExpiresIn,
  229. kRefreshTokenKey : kTestRefreshToken,
  230. }];
  231. XCTAssert(callbackInvoked);
  232. XCTAssertNil(RPCError);
  233. XCTAssertNotNil(RPCResponse);
  234. XCTAssertEqualObjects(RPCResponse.IDToken, kTestIDToken);
  235. NSTimeInterval expiresIn = [RPCResponse.approximateExpirationDate timeIntervalSinceNow];
  236. XCTAssertEqualWithAccuracy(expiresIn, [kTestExpiresIn doubleValue], kAllowedTimeDifference);
  237. XCTAssertEqualObjects(RPCResponse.refreshToken, kTestRefreshToken);
  238. }
  239. @end