FIRVerifyCustomTokenResponseTests.m 11 KB

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