FIRVerifyCustomTokenResponseTests.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /** @var _requestConfiguration
  101. @brief This is the request configuration used for testing.
  102. */
  103. FIRAuthRequestConfiguration *_requestConfiguration;
  104. }
  105. - (void)setUp {
  106. [super setUp];
  107. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  108. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  109. _RPCIssuer = RPCIssuer;
  110. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  111. }
  112. - (void)tearDown {
  113. _RPCIssuer = nil;
  114. _requestConfiguration = nil;
  115. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  116. [super tearDown];
  117. }
  118. /** @fn testInvalidCustomTokenError
  119. @brief This test simulates @c invalidCustomTokenError with @c
  120. FIRAuthErrorCodeINvalidCustomToken error code.
  121. */
  122. - (void)testInvalidCustomTokenError {
  123. FIRVerifyCustomTokenRequest *request =
  124. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken
  125. requestConfiguration:_requestConfiguration];
  126. __block BOOL callbackInvoked;
  127. __block FIRVerifyCustomTokenResponse *RPCResponse;
  128. __block NSError *RPCError;
  129. [FIRAuthBackend verifyCustomToken:request
  130. callback:^(FIRVerifyCustomTokenResponse*_Nullable response,
  131. NSError *_Nullable error) {
  132. callbackInvoked = YES;
  133. RPCResponse = response;
  134. RPCError = error;
  135. }];
  136. [_RPCIssuer respondWithServerErrorMessage:kInvalidCustomTokenErrorMessage];
  137. XCTAssert(callbackInvoked);
  138. XCTAssertNotNil(RPCError);
  139. XCTAssertNil(RPCResponse);
  140. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidCustomToken);
  141. }
  142. /** @fn testInvalidCustomTokenServerError
  143. @brief This test simulates @c invalidCustomTokenError with @c
  144. FIRAuthErrorCodeINvalidCustomToken error code, with a custom message from the server.
  145. */
  146. - (void)testInvalidCustomTokenServerError {
  147. FIRVerifyCustomTokenRequest *request =
  148. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken
  149. requestConfiguration:_requestConfiguration];
  150. __block BOOL callbackInvoked;
  151. __block FIRVerifyCustomTokenResponse *RPCResponse;
  152. __block NSError *RPCError;
  153. [FIRAuthBackend verifyCustomToken:request
  154. callback:^(FIRVerifyCustomTokenResponse*_Nullable response,
  155. NSError *_Nullable error) {
  156. callbackInvoked = YES;
  157. RPCResponse = response;
  158. RPCError = error;
  159. }];
  160. [_RPCIssuer respondWithServerErrorMessage:kInvalidCustomTokenServerErrorMessage];
  161. NSString *errorDescription = [RPCError.userInfo valueForKey:NSLocalizedDescriptionKey];
  162. XCTAssertTrue([errorDescription isEqualToString:kInvalidCustomTokenErrorDetails]);
  163. XCTAssert(callbackInvoked);
  164. XCTAssertNotNil(RPCError);
  165. XCTAssertNil(RPCResponse);
  166. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidCustomToken);
  167. }
  168. /** @fn testEmptyServerDetailMessage
  169. @brief This test simulates @c invalidCustomTokenError with @c
  170. FIRAuthErrorCodeINvalidCustomToken error code, with an empty custom message from the server.
  171. @remarks An empty error message is not valid and therefore should not be added as an error
  172. description.
  173. */
  174. - (void)testEmptyServerDetailMessage {
  175. FIRVerifyCustomTokenRequest *request =
  176. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken
  177. requestConfiguration:_requestConfiguration];
  178. __block BOOL callbackInvoked;
  179. __block FIRVerifyCustomTokenResponse *RPCResponse;
  180. __block NSError *RPCError;
  181. [FIRAuthBackend verifyCustomToken:request
  182. callback:^(FIRVerifyCustomTokenResponse*_Nullable response,
  183. NSError *_Nullable error) {
  184. callbackInvoked = YES;
  185. RPCResponse = response;
  186. RPCError = error;
  187. }];
  188. [_RPCIssuer respondWithServerErrorMessage:kInvalidCustomTokenEmptyServerErrorMessage];
  189. NSString *errorDescription = [RPCError.userInfo valueForKey:NSLocalizedDescriptionKey];
  190. XCTAssertFalse([errorDescription isEqualToString:@""]);
  191. XCTAssert(callbackInvoked);
  192. XCTAssertNotNil(RPCError);
  193. XCTAssertNil(RPCResponse);
  194. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidCustomToken);
  195. }
  196. /** @fn testInvalidCredentialMismatchError
  197. @brief This test simulates @c credentialMistmatchTokenError with @c
  198. FIRAuthErrorCodeCredetialMismatch error code.
  199. */
  200. - (void)testInvalidCredentialMismatchError {
  201. FIRVerifyCustomTokenRequest *request =
  202. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken
  203. requestConfiguration:_requestConfiguration];
  204. __block BOOL callbackInvoked;
  205. __block FIRVerifyCustomTokenResponse *RPCResponse;
  206. __block NSError *RPCError;
  207. [FIRAuthBackend verifyCustomToken:request
  208. callback:^(FIRVerifyCustomTokenResponse*_Nullable response,
  209. NSError *_Nullable error) {
  210. callbackInvoked = YES;
  211. RPCResponse = response;
  212. RPCError = error;
  213. }];
  214. [_RPCIssuer respondWithServerErrorMessage:kCredentialMismatchErrorMessage];
  215. XCTAssert(callbackInvoked);
  216. XCTAssertNotNil(RPCError);
  217. XCTAssertNil(RPCResponse);
  218. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeCustomTokenMismatch);
  219. }
  220. /** @fn testSuccessfulVerifyCustomTokenResponse
  221. @brief This test simulates a successful @c VerifyCustomToken flow.
  222. */
  223. - (void)testSuccessfulVerifyCustomTokenResponse {
  224. FIRVerifyCustomTokenRequest *request =
  225. [[FIRVerifyCustomTokenRequest alloc] initWithToken:kTestToken
  226. requestConfiguration:_requestConfiguration];
  227. __block BOOL callbackInvoked;
  228. __block FIRVerifyCustomTokenResponse *RPCResponse;
  229. __block NSError *RPCError;
  230. [FIRAuthBackend verifyCustomToken:request
  231. callback:^(FIRVerifyCustomTokenResponse*_Nullable response,
  232. NSError *_Nullable error) {
  233. callbackInvoked = YES;
  234. RPCResponse = response;
  235. RPCError = error;
  236. }];
  237. [_RPCIssuer respondWithJSON:@{
  238. kIDTokenKey : kTestIDToken,
  239. kExpiresInKey : kTestExpiresIn,
  240. kRefreshTokenKey : kTestRefreshToken,
  241. }];
  242. XCTAssert(callbackInvoked);
  243. XCTAssertNil(RPCError);
  244. XCTAssertNotNil(RPCResponse);
  245. XCTAssertEqualObjects(RPCResponse.IDToken, kTestIDToken);
  246. NSTimeInterval expiresIn = [RPCResponse.approximateExpirationDate timeIntervalSinceNow];
  247. XCTAssertEqualWithAccuracy(expiresIn, [kTestExpiresIn doubleValue], kAllowedTimeDifference);
  248. XCTAssertEqualObjects(RPCResponse.refreshToken, kTestRefreshToken);
  249. }
  250. @end