FIRGetAccountInfoResponseTests.m 10 KB

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