FIRGetAccountInfoResponseTests.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 "FIRAuthInternalErrors.h"
  19. #import "FIRAuthBackend.h"
  20. #import "FIRGetOOBConfirmationCodeResponse.h"
  21. #import "FIRGetAccountInfoRequest.h"
  22. #import "FIRGetAccountInfoResponse.h"
  23. #import "FIRFakeBackendRPCIssuer.h"
  24. /** @var kTestAPIKey
  25. @brief Fake API key used for testing.
  26. */
  27. static NSString *const kTestAPIKey = @"APIKey";
  28. /** @var kUsersKey
  29. @brief the name of the "users" property in the response.
  30. */
  31. static NSString *const kUsersKey = @"users";
  32. /** @var kVerifiedProviderKey
  33. @brief The name of the "VerifiedProvider" property in the response.
  34. */
  35. static NSString *const kProviderUserInfoKey = @"providerUserInfo";
  36. /** @var kPhotoUrlKey
  37. @brief The name of the "photoURL" property in the response.
  38. */
  39. static NSString *const kPhotoUrlKey = @"photoUrl";
  40. /** @var kTestPhotoURL
  41. @brief The fake photoUrl property value in the response.
  42. */
  43. static NSString *const kTestPhotoURL = @"testPhotoURL";
  44. /** @var kTestAccessToken
  45. @brief testing token.
  46. */
  47. static NSString *const kTestAccessToken = @"testAccessToken";
  48. /** @var kProviderIDkey
  49. @brief The name of the "provider ID" property in the response.
  50. */
  51. static NSString *const kProviderIDkey = @"providerId";
  52. /** @var kTestProviderID
  53. @brief The fake providerID property value in the response.
  54. */
  55. static NSString *const kTestProviderID = @"testProviderID";
  56. /** @var kDisplayNameKey
  57. @brief The name of the "Display Name" property in the response.
  58. */
  59. static NSString *const kDisplayNameKey = @"displayName";
  60. /** @var kTestDisplayName
  61. @brief The fake DisplayName property value in the response.
  62. */
  63. static NSString *const kTestDisplayName = @"DisplayName";
  64. /** @var kFederatedIDKey
  65. @brief The name of the "federated Id" property in the response.
  66. */
  67. static NSString *const kFederatedIDKey = @"federatedId";
  68. /** @var kTestFederatedID
  69. @brief The fake federated Id property value in the response.
  70. */
  71. static NSString *const kTestFederatedID = @"testFederatedId";
  72. /** @var kEmailKey
  73. @brief The name of the "Email" property in the response.
  74. */
  75. static NSString *const kEmailKey = @"email";
  76. /** @var kTestEmail
  77. @brief The fake email property value in the response.
  78. */
  79. static NSString *const kTestEmail = @"testEmail";
  80. /** @var kPasswordHashKey
  81. @brief The name of the "password hash" property in the response.
  82. */
  83. static NSString *const kPasswordHashKey = @"passwordHash";
  84. /** @var kTestPasswordHash
  85. @brief The fake password hash property value in the response.
  86. */
  87. static NSString *const kTestPasswordHash = @"testPasswordHash";
  88. /** @var kLocalIDKey
  89. @brief The key for the "localID" value in the response.
  90. */
  91. static NSString *const kLocalIDKey = @"localId";
  92. /** @var kTestLocalID
  93. @brief The fake @c localID for testing in the response.
  94. */
  95. static NSString *const kTestLocalID = @"testLocalId";
  96. /** @var kEmailVerifiedKey
  97. @brief The key for the "emailVerified" value in the response.
  98. */
  99. static NSString *const kEmailVerifiedKey = @"emailVerified";
  100. /** @class FIRGetAccountInfoResponseTests
  101. @brief Tests for @c FIRGetAccountInfoResponse.
  102. */
  103. @interface FIRGetAccountInfoResponseTests : XCTestCase
  104. @end
  105. @implementation FIRGetAccountInfoResponseTests {
  106. /** @var _RPCIssuer
  107. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  108. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  109. */
  110. FIRFakeBackendRPCIssuer *_RPCIssuer;
  111. }
  112. - (void)setUp {
  113. [super setUp];
  114. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  115. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  116. _RPCIssuer = RPCIssuer;
  117. }
  118. - (void)tearDown {
  119. _RPCIssuer = nil;
  120. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  121. [super tearDown];
  122. }
  123. /** @fn testGetAccountInfoUnexpectedResponseError
  124. @brief This test simulates an unexpected response returned from server in @c GetAccountInfo
  125. flow.
  126. */
  127. - (void)testGetAccountInfoUnexpectedResponseError {
  128. FIRAuthRequestConfiguration *requestConfiguration =
  129. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  130. FIRGetAccountInfoRequest *request =
  131. [[FIRGetAccountInfoRequest alloc] initWithAccessToken:kTestAccessToken
  132. requestConfiguration:requestConfiguration];
  133. __block BOOL callbackInvoked;
  134. __block FIRGetAccountInfoResponse *RPCResponse;
  135. __block NSError *RPCError;
  136. [FIRAuthBackend getAccountInfo:request
  137. callback:^(FIRGetAccountInfoResponse *_Nullable response,
  138. NSError *_Nullable error) {
  139. callbackInvoked = YES;
  140. RPCResponse = response;
  141. RPCError = error;
  142. }];
  143. NSArray *erroneousUserData = @[@"user1Data", @"user2Data"];
  144. [_RPCIssuer respondWithJSON:@{
  145. kUsersKey : erroneousUserData
  146. }];
  147. XCTAssert(callbackInvoked);
  148. XCTAssertNotNil(RPCError);
  149. XCTAssertEqualObjects(RPCError.domain, FIRAuthErrorDomain);
  150. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInternalError);
  151. XCTAssertNotNil(RPCError.userInfo[NSUnderlyingErrorKey]);
  152. NSError *underlyingError = RPCError.userInfo[NSUnderlyingErrorKey];
  153. XCTAssertNotNil(underlyingError);
  154. XCTAssertNotNil(underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey]);
  155. XCTAssertNil(RPCResponse);
  156. }
  157. /** @fn testSuccessfulGetAccountInfoResponse
  158. @brief This test simulates a successful @c GetAccountInfo flow.
  159. */
  160. - (void)testSuccessfulGetAccountInfoResponse {
  161. FIRAuthRequestConfiguration *requestConfiguration =
  162. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  163. FIRGetAccountInfoRequest *request =
  164. [[FIRGetAccountInfoRequest alloc] initWithAccessToken:kTestAccessToken
  165. requestConfiguration:requestConfiguration];
  166. __block BOOL callbackInvoked;
  167. __block FIRGetAccountInfoResponse *RPCResponse;
  168. __block NSError *RPCError;
  169. [FIRAuthBackend getAccountInfo:request
  170. callback:^(FIRGetAccountInfoResponse *_Nullable response,
  171. NSError *_Nullable error) {
  172. callbackInvoked = YES;
  173. RPCResponse = response;
  174. RPCError = error;
  175. }];
  176. NSArray *users = @[
  177. @{
  178. kProviderUserInfoKey:@[
  179. @{
  180. kProviderIDkey : kTestProviderID,
  181. kDisplayNameKey: kTestDisplayName,
  182. kPhotoUrlKey : kTestPhotoURL,
  183. kFederatedIDKey : kTestFederatedID,
  184. kEmailKey : kTestEmail,
  185. }
  186. ],
  187. kLocalIDKey : kTestLocalID,
  188. kDisplayNameKey : kTestDisplayName,
  189. kEmailKey : kTestEmail,
  190. kPhotoUrlKey : kTestPhotoURL,
  191. kEmailVerifiedKey : @YES,
  192. kPasswordHashKey : kTestPasswordHash
  193. }
  194. ];
  195. [_RPCIssuer respondWithJSON:@{
  196. @"users" : users,
  197. }];
  198. XCTAssert(callbackInvoked);
  199. XCTAssertNil(RPCError);
  200. XCTAssertNotNil(RPCResponse);
  201. XCTAssertNotNil(RPCResponse.users);
  202. if ([RPCResponse.users count]) {
  203. NSURL *responsePhotoUrl = RPCResponse.users[0].photoURL;
  204. XCTAssertEqualObjects(responsePhotoUrl.absoluteString, kTestPhotoURL);
  205. XCTAssertEqualObjects(RPCResponse.users[0].displayName, kTestDisplayName);
  206. XCTAssertEqualObjects(RPCResponse.users[0].email, kTestEmail);
  207. XCTAssertEqualObjects(RPCResponse.users[0].localID, kTestLocalID);
  208. XCTAssertEqual(RPCResponse.users[0].emailVerified, YES);
  209. XCTAssertEqualObjects(RPCResponse.users[0].passwordHash, kTestPasswordHash);
  210. NSArray<FIRGetAccountInfoResponseProviderUserInfo *> *providerUserInfo =
  211. RPCResponse.users[0].providerUserInfo;
  212. if ([providerUserInfo count]) {
  213. NSURL *providerInfoPhotoUrl = providerUserInfo[0].photoURL;
  214. XCTAssertEqualObjects(providerInfoPhotoUrl.absoluteString, kTestPhotoURL);
  215. XCTAssertEqualObjects(providerUserInfo[0].providerID, kTestProviderID);
  216. XCTAssertEqualObjects(providerUserInfo[0].displayName, kTestDisplayName);
  217. XCTAssertEqualObjects(providerUserInfo[0].federatedID, kTestFederatedID);
  218. XCTAssertEqualObjects(providerUserInfo[0].email, kTestEmail);
  219. }
  220. }
  221. }
  222. @end