FIRVerifyCustomTokenResponseTests.m 9.8 KB

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