FIRVerifyCustomTokenResponseTests.m 12 KB

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