FIRVerifyCustomTokenResponseTests.m 11 KB

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