FIRGetAccountInfoResponseTests.m 8.9 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 "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. /** @class FIRGetAccountInfoResponseTests
  105. @brief Tests for @c FIRGetAccountInfoResponse.
  106. */
  107. @interface FIRGetAccountInfoResponseTests : XCTestCase
  108. @end
  109. @implementation FIRGetAccountInfoResponseTests {
  110. /** @var _RPCIssuer
  111. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  112. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  113. */
  114. FIRFakeBackendRPCIssuer *_RPCIssuer;
  115. }
  116. - (void)setUp {
  117. [super setUp];
  118. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  119. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  120. _RPCIssuer = RPCIssuer;
  121. }
  122. - (void)tearDown {
  123. _RPCIssuer = nil;
  124. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  125. [super tearDown];
  126. }
  127. /** @fn testGetAccountInfoUnexpectedResponseError
  128. @brief This test simulates an unexpected response returned from server in @c GetAccountInfo
  129. flow.
  130. */
  131. - (void)testGetAccountInfoUnexpectedResponseError {
  132. FIRAuthRequestConfiguration *requestConfiguration =
  133. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey appID:kTestFirebaseAppID];
  134. FIRGetAccountInfoRequest *request =
  135. [[FIRGetAccountInfoRequest alloc] initWithAccessToken:kTestAccessToken
  136. requestConfiguration:requestConfiguration];
  137. __block BOOL callbackInvoked;
  138. __block FIRGetAccountInfoResponse *RPCResponse;
  139. __block NSError *RPCError;
  140. [FIRAuthBackend
  141. getAccountInfo:request
  142. callback:^(FIRGetAccountInfoResponse *_Nullable response, NSError *_Nullable error) {
  143. callbackInvoked = YES;
  144. RPCResponse = response;
  145. RPCError = error;
  146. }];
  147. NSArray *erroneousUserData = @[ @"user1Data", @"user2Data" ];
  148. [_RPCIssuer respondWithJSON:@{kUsersKey : erroneousUserData}];
  149. XCTAssert(callbackInvoked);
  150. XCTAssertNotNil(RPCError);
  151. XCTAssertEqualObjects(RPCError.domain, FIRAuthErrorDomain);
  152. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInternalError);
  153. XCTAssertNotNil(RPCError.userInfo[NSUnderlyingErrorKey]);
  154. NSError *underlyingError = RPCError.userInfo[NSUnderlyingErrorKey];
  155. XCTAssertNotNil(underlyingError);
  156. XCTAssertNotNil(underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey]);
  157. XCTAssertNil(RPCResponse);
  158. }
  159. /** @fn testSuccessfulGetAccountInfoResponse
  160. @brief This test simulates a successful @c GetAccountInfo flow.
  161. */
  162. - (void)testSuccessfulGetAccountInfoResponse {
  163. FIRAuthRequestConfiguration *requestConfiguration =
  164. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey appID:kTestFirebaseAppID];
  165. FIRGetAccountInfoRequest *request =
  166. [[FIRGetAccountInfoRequest alloc] initWithAccessToken:kTestAccessToken
  167. requestConfiguration:requestConfiguration];
  168. __block BOOL callbackInvoked;
  169. __block FIRGetAccountInfoResponse *RPCResponse;
  170. __block NSError *RPCError;
  171. [FIRAuthBackend
  172. getAccountInfo:request
  173. callback:^(FIRGetAccountInfoResponse *_Nullable response, NSError *_Nullable error) {
  174. callbackInvoked = YES;
  175. RPCResponse = response;
  176. RPCError = error;
  177. }];
  178. NSArray *users = @[ @{
  179. kProviderUserInfoKey : @[ @{
  180. kProviderIDkey : kTestProviderID,
  181. kDisplayNameKey : kTestDisplayName,
  182. kPhotoUrlKey : kTestPhotoURL,
  183. kFederatedIDKey : kTestFederatedID,
  184. kEmailKey : kTestEmail,
  185. } ],
  186. kLocalIDKey : kTestLocalID,
  187. kDisplayNameKey : kTestDisplayName,
  188. kEmailKey : kTestEmail,
  189. kPhotoUrlKey : kTestPhotoURL,
  190. kEmailVerifiedKey : @YES,
  191. kPasswordHashKey : kTestPasswordHash
  192. } ];
  193. [_RPCIssuer respondWithJSON:@{
  194. @"users" : users,
  195. }];
  196. XCTAssert(callbackInvoked);
  197. XCTAssertNil(RPCError);
  198. XCTAssertNotNil(RPCResponse);
  199. XCTAssertNotNil(RPCResponse.users);
  200. if ([RPCResponse.users count]) {
  201. NSURL *responsePhotoUrl = RPCResponse.users[0].photoURL;
  202. XCTAssertEqualObjects(responsePhotoUrl.absoluteString, kTestPhotoURL);
  203. XCTAssertEqualObjects(RPCResponse.users[0].displayName, kTestDisplayName);
  204. XCTAssertEqualObjects(RPCResponse.users[0].email, kTestEmail);
  205. XCTAssertEqualObjects(RPCResponse.users[0].localID, kTestLocalID);
  206. XCTAssertEqual(RPCResponse.users[0].emailVerified, YES);
  207. XCTAssertEqualObjects(RPCResponse.users[0].passwordHash, kTestPasswordHash);
  208. NSArray<FIRGetAccountInfoResponseProviderUserInfo *> *providerUserInfo =
  209. RPCResponse.users[0].providerUserInfo;
  210. if ([providerUserInfo count]) {
  211. NSURL *providerInfoPhotoUrl = providerUserInfo[0].photoURL;
  212. XCTAssertEqualObjects(providerInfoPhotoUrl.absoluteString, kTestPhotoURL);
  213. XCTAssertEqualObjects(providerUserInfo[0].providerID, kTestProviderID);
  214. XCTAssertEqualObjects(providerUserInfo[0].displayName, kTestDisplayName);
  215. XCTAssertEqualObjects(providerUserInfo[0].federatedID, kTestFederatedID);
  216. XCTAssertEqualObjects(providerUserInfo[0].email, kTestEmail);
  217. }
  218. }
  219. }
  220. @end