FIRSignInWithGameCenterTests.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright 2018 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/Backend/FIRAuthBackend.h"
  18. #import "FirebaseAuth/Sources/Backend/RPC/FIRSignInWithGameCenterRequest.h"
  19. #import "FirebaseAuth/Sources/Backend/RPC/FIRSignInWithGameCenterResponse.h"
  20. #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
  21. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  22. /** @var kTestAPIKey
  23. @brief Fake API key used for testing.
  24. */
  25. static NSString *const kTestAPIKey = @"APIKEY";
  26. /** @var kTestFirebaseAppID
  27. @brief Fake Firebase app ID used for testing.
  28. */
  29. static NSString *const kTestFirebaseAppID = @"APPID";
  30. /** @var kExpectedAPIURL
  31. @brief The expected URL for the test calls.
  32. */
  33. static NSString *const kExpectedAPIURL =
  34. @"https://www.googleapis.com/identitytoolkit/v3/relyingparty/signInWithGameCenter?key=APIKEY";
  35. /** @var kIDTokenKey
  36. @brief The key of the id token.
  37. */
  38. static NSString *const kIDTokenKey = @"idToken";
  39. /** @var kIDToken
  40. @brief The testing id token.
  41. */
  42. static NSString *const kIDToken = @"IDTOKEN";
  43. /** @var kRefreshTokenKey
  44. @brief The key of the refresh token.
  45. */
  46. static NSString *const kRefreshTokenKey = @"refreshToken";
  47. /** @var kRefreshToken
  48. @brief The testing refresh token.
  49. */
  50. static NSString *const kRefreshToken = @"PUBLICKEYURL";
  51. /** @var kLocalIDKey
  52. @brief The key of local id.
  53. */
  54. static NSString *const kLocalIDKey = @"localId";
  55. /** @var kLocalID
  56. @brief The testing local id.
  57. */
  58. static NSString *const kLocalID = @"LOCALID";
  59. /** @var kPlayerIDKey
  60. @brief The key of player id.
  61. */
  62. static NSString *const kPlayerIDKey = @"playerId";
  63. /** @var kPlayerID
  64. @brief The testing player id.
  65. */
  66. static NSString *const kPlayerID = @"PLAYERID";
  67. /** @var kTeamPlayerIDKey
  68. @brief The key of team player id.
  69. */
  70. static NSString *const kTeamPlayerIDKey = @"teamPlayerId";
  71. /** @var kPlayerID
  72. @brief The testing player id.
  73. */
  74. static NSString *const kTeamPlayerID = @"TEAMPLAYERID";
  75. /** @var kGamePlayerIDKey
  76. @brief The key of game player id.
  77. */
  78. static NSString *const kGamePlayerIDKey = @"gamePlayerId";
  79. /** @var kGamePlayerID
  80. @brief The testing game player id.
  81. */
  82. static NSString *const kGamePlayerID = @"GAMEPLAYERID";
  83. /** @var kApproximateExpirationDateKey
  84. @brief The approximate expiration date key.
  85. */
  86. static NSString *const kApproximateExpirationDateKey = @"expiresIn";
  87. /** @var kApproximateExpirationDate
  88. @brief The testing approximate expration date.
  89. */
  90. static NSString *const kApproximateExpirationDate = @"3600";
  91. /** @var kIsNewUserKey
  92. @brief The key of whether the user is new user.
  93. */
  94. static NSString *const kIsNewUserKey = @"isNewUser";
  95. /** @var kIsNewUser
  96. @brief The testing isNewUser.
  97. */
  98. static BOOL const kIsNewUser = YES;
  99. /** @var kDisplayNameKey
  100. @brief The key of display name.
  101. */
  102. static NSString *const kDisplayNameKey = @"displayName";
  103. /** @var kDisplayName
  104. @brief The testing display name.
  105. */
  106. static NSString *const kDisplayName = @"DISPLAYNAME";
  107. /** @var kPublicKeyURLKey
  108. @brief The key of public key url.
  109. */
  110. static NSString *const kPublicKeyURLKey = @"publicKeyUrl";
  111. /** @var kPublicKeyURL
  112. @brief The testing public key url.
  113. */
  114. static NSString *const kPublicKeyURL = @"PUBLICKEYURL";
  115. /** @var kSignatureKey
  116. @brief The key of the signature.
  117. */
  118. static NSString *const kSignatureKey = @"signature";
  119. /** @var kSignature
  120. @brief The testing signature.
  121. */
  122. static NSString *const kSignature = @"AAAABBBBCCCC";
  123. /** @var kSaltKey
  124. @brief The key of the salt.
  125. */
  126. static NSString *const kSaltKey = @"salt";
  127. /** @var kSalt
  128. @brief The testing salt.
  129. */
  130. static NSString *const kSalt = @"AAAA";
  131. /** @var kTimestampKey
  132. @brief The key of the timestamp.
  133. */
  134. static NSString *const kTimestampKey = @"timestamp";
  135. /** @var kTimestamp
  136. @brief The testing timestamp.
  137. */
  138. static uint64_t const kTimestamp = 12345678;
  139. /** @var kAccessTokenKey
  140. @brief The key of the access token.
  141. */
  142. static NSString *const kAccessTokenKey = @"idToken";
  143. /** @var kAccessToken
  144. @brief The testing access token.
  145. */
  146. static NSString *const kAccessToken = @"ACCESSTOKEN";
  147. @interface FIRSignInWithGameCenterTests : XCTestCase
  148. /** @property RPCIssuer
  149. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  150. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  151. */
  152. @property(nonatomic, strong) FIRFakeBackendRPCIssuer *RPCIssuer;
  153. /** @property requestConfiguration
  154. @brief This is the request configuration used for testing.
  155. */
  156. @property(nonatomic, strong) FIRAuthRequestConfiguration *requestConfiguration;
  157. @end
  158. @implementation FIRSignInWithGameCenterTests
  159. - (void)setUp {
  160. [super setUp];
  161. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  162. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  163. self.RPCIssuer = RPCIssuer;
  164. self.requestConfiguration =
  165. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey appID:kTestFirebaseAppID];
  166. }
  167. - (void)tearDown {
  168. self.requestConfiguration = nil;
  169. self.RPCIssuer = nil;
  170. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  171. [super tearDown];
  172. }
  173. - (void)testRequestResponseEncoding {
  174. NSData *signature = [[NSData alloc] initWithBase64EncodedString:kSignature options:0];
  175. NSData *salt = [[NSData alloc] initWithBase64EncodedString:kSalt options:0];
  176. FIRSignInWithGameCenterRequest *request =
  177. [[FIRSignInWithGameCenterRequest alloc] initWithPlayerID:kPlayerID
  178. teamPlayerID:kTeamPlayerID
  179. gamePlayerID:kGamePlayerID
  180. publicKeyURL:[NSURL URLWithString:kPublicKeyURL]
  181. signature:signature
  182. salt:salt
  183. timestamp:kTimestamp
  184. displayName:kDisplayName
  185. requestConfiguration:self.requestConfiguration];
  186. request.accessToken = kAccessToken;
  187. __block BOOL callbackInvoked;
  188. __block FIRSignInWithGameCenterResponse *RPCResponse;
  189. __block NSError *RPCError;
  190. [FIRAuthBackend signInWithGameCenter:request
  191. callback:^(FIRSignInWithGameCenterResponse *_Nullable response,
  192. NSError *_Nullable error) {
  193. RPCResponse = response;
  194. RPCError = error;
  195. callbackInvoked = YES;
  196. }];
  197. XCTAssertEqualObjects(self.RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  198. XCTAssertNotNil(self.RPCIssuer.decodedRequest);
  199. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kPlayerIDKey], kPlayerID);
  200. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kTeamPlayerIDKey], kTeamPlayerID);
  201. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kGamePlayerIDKey], kGamePlayerID);
  202. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kPublicKeyURLKey], kPublicKeyURL);
  203. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kSignatureKey], kSignature);
  204. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kSaltKey], kSalt);
  205. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kTimestampKey],
  206. [NSNumber numberWithInteger:kTimestamp]);
  207. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kAccessTokenKey], kAccessToken);
  208. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kDisplayNameKey], kDisplayName);
  209. NSDictionary *jsonDictionary = @{
  210. @"idToken" : kIDToken,
  211. @"refreshToken" : kRefreshToken,
  212. @"localId" : kLocalID,
  213. @"playerId" : kPlayerID,
  214. @"teamPlayerId" : kTeamPlayerID,
  215. @"gamePlayerId" : kGamePlayerID,
  216. @"expiresIn" : kApproximateExpirationDate,
  217. @"isNewUser" : [NSNumber numberWithBool:kIsNewUser],
  218. @"displayName" : kDisplayName,
  219. };
  220. [self.RPCIssuer respondWithJSON:jsonDictionary];
  221. XCTAssertTrue(callbackInvoked);
  222. XCTAssertNotNil(RPCResponse);
  223. XCTAssertEqualObjects(RPCResponse.IDToken, kIDToken);
  224. XCTAssertEqualObjects(RPCResponse.refreshToken, kRefreshToken);
  225. XCTAssertEqualObjects(RPCResponse.localID, kLocalID);
  226. XCTAssertEqualObjects(RPCResponse.playerID, kPlayerID);
  227. XCTAssertEqualObjects(RPCResponse.teamPlayerID, kTeamPlayerID);
  228. XCTAssertEqualObjects(RPCResponse.gamePlayerID, kGamePlayerID);
  229. XCTAssertEqual(RPCResponse.isNewUser, kIsNewUser);
  230. XCTAssertEqualObjects(RPCResponse.displayName, kDisplayName);
  231. }
  232. @end