FIRGetAccountInfoResponseTests.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 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
  137. getAccountInfo:request
  138. callback:^(FIRGetAccountInfoResponse *_Nullable response, NSError *_Nullable error) {
  139. callbackInvoked = YES;
  140. RPCResponse = response;
  141. RPCError = error;
  142. }];
  143. NSArray *erroneousUserData = @[ @"user1Data", @"user2Data" ];
  144. [_RPCIssuer respondWithJSON:@{kUsersKey : erroneousUserData}];
  145. XCTAssert(callbackInvoked);
  146. XCTAssertNotNil(RPCError);
  147. XCTAssertEqualObjects(RPCError.domain, FIRAuthErrorDomain);
  148. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInternalError);
  149. XCTAssertNotNil(RPCError.userInfo[NSUnderlyingErrorKey]);
  150. NSError *underlyingError = RPCError.userInfo[NSUnderlyingErrorKey];
  151. XCTAssertNotNil(underlyingError);
  152. XCTAssertNotNil(underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey]);
  153. XCTAssertNil(RPCResponse);
  154. }
  155. /** @fn testSuccessfulGetAccountInfoResponse
  156. @brief This test simulates a successful @c GetAccountInfo flow.
  157. */
  158. - (void)testSuccessfulGetAccountInfoResponse {
  159. FIRAuthRequestConfiguration *requestConfiguration =
  160. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  161. FIRGetAccountInfoRequest *request =
  162. [[FIRGetAccountInfoRequest alloc] initWithAccessToken:kTestAccessToken
  163. requestConfiguration:requestConfiguration];
  164. __block BOOL callbackInvoked;
  165. __block FIRGetAccountInfoResponse *RPCResponse;
  166. __block NSError *RPCError;
  167. [FIRAuthBackend
  168. getAccountInfo:request
  169. callback:^(FIRGetAccountInfoResponse *_Nullable response, NSError *_Nullable error) {
  170. callbackInvoked = YES;
  171. RPCResponse = response;
  172. RPCError = error;
  173. }];
  174. NSArray *users = @[ @{
  175. kProviderUserInfoKey : @[ @{
  176. kProviderIDkey : kTestProviderID,
  177. kDisplayNameKey : kTestDisplayName,
  178. kPhotoUrlKey : kTestPhotoURL,
  179. kFederatedIDKey : kTestFederatedID,
  180. kEmailKey : kTestEmail,
  181. } ],
  182. kLocalIDKey : kTestLocalID,
  183. kDisplayNameKey : kTestDisplayName,
  184. kEmailKey : kTestEmail,
  185. kPhotoUrlKey : kTestPhotoURL,
  186. kEmailVerifiedKey : @YES,
  187. kPasswordHashKey : kTestPasswordHash
  188. } ];
  189. [_RPCIssuer respondWithJSON:@{
  190. @"users" : users,
  191. }];
  192. XCTAssert(callbackInvoked);
  193. XCTAssertNil(RPCError);
  194. XCTAssertNotNil(RPCResponse);
  195. XCTAssertNotNil(RPCResponse.users);
  196. if ([RPCResponse.users count]) {
  197. NSURL *responsePhotoUrl = RPCResponse.users[0].photoURL;
  198. XCTAssertEqualObjects(responsePhotoUrl.absoluteString, kTestPhotoURL);
  199. XCTAssertEqualObjects(RPCResponse.users[0].displayName, kTestDisplayName);
  200. XCTAssertEqualObjects(RPCResponse.users[0].email, kTestEmail);
  201. XCTAssertEqualObjects(RPCResponse.users[0].localID, kTestLocalID);
  202. XCTAssertEqual(RPCResponse.users[0].emailVerified, YES);
  203. XCTAssertEqualObjects(RPCResponse.users[0].passwordHash, kTestPasswordHash);
  204. NSArray<FIRGetAccountInfoResponseProviderUserInfo *> *providerUserInfo =
  205. RPCResponse.users[0].providerUserInfo;
  206. if ([providerUserInfo count]) {
  207. NSURL *providerInfoPhotoUrl = providerUserInfo[0].photoURL;
  208. XCTAssertEqualObjects(providerInfoPhotoUrl.absoluteString, kTestPhotoURL);
  209. XCTAssertEqualObjects(providerUserInfo[0].providerID, kTestProviderID);
  210. XCTAssertEqualObjects(providerUserInfo[0].displayName, kTestDisplayName);
  211. XCTAssertEqualObjects(providerUserInfo[0].federatedID, kTestFederatedID);
  212. XCTAssertEqualObjects(providerUserInfo[0].email, kTestEmail);
  213. }
  214. }
  215. }
  216. @end