FIRSignInWithGameCenterTests.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 kApproximateExpirationDateKey
  68. @brief The approximate expiration date key.
  69. */
  70. static NSString *const kApproximateExpirationDateKey = @"expiresIn";
  71. /** @var kApproximateExpirationDate
  72. @brief The testing approximate expration date.
  73. */
  74. static NSString *const kApproximateExpirationDate = @"3600";
  75. /** @var kIsNewUserKey
  76. @brief The key of whether the user is new user.
  77. */
  78. static NSString *const kIsNewUserKey = @"isNewUser";
  79. /** @var kIsNewUser
  80. @brief The testing isNewUser.
  81. */
  82. static BOOL const kIsNewUser = YES;
  83. /** @var kDisplayNameKey
  84. @brief The key of display name.
  85. */
  86. static NSString *const kDisplayNameKey = @"displayName";
  87. /** @var kDisplayName
  88. @brief The testing display name.
  89. */
  90. static NSString *const kDisplayName = @"DISPLAYNAME";
  91. /** @var kPublicKeyURLKey
  92. @brief The key of public key url.
  93. */
  94. static NSString *const kPublicKeyURLKey = @"publicKeyUrl";
  95. /** @var kPublicKeyURL
  96. @brief The testing public key url.
  97. */
  98. static NSString *const kPublicKeyURL = @"PUBLICKEYURL";
  99. /** @var kSignatureKey
  100. @brief The key of the signature.
  101. */
  102. static NSString *const kSignatureKey = @"signature";
  103. /** @var kSignature
  104. @brief The testing signature.
  105. */
  106. static NSString *const kSignature = @"AAAABBBBCCCC";
  107. /** @var kSaltKey
  108. @brief The key of the salt.
  109. */
  110. static NSString *const kSaltKey = @"salt";
  111. /** @var kSalt
  112. @brief The testing salt.
  113. */
  114. static NSString *const kSalt = @"AAAA";
  115. /** @var kTimestampKey
  116. @brief The key of the timestamp.
  117. */
  118. static NSString *const kTimestampKey = @"timestamp";
  119. /** @var kTimestamp
  120. @brief The testing timestamp.
  121. */
  122. static uint64_t const kTimestamp = 12345678;
  123. /** @var kAccessTokenKey
  124. @brief The key of the access token.
  125. */
  126. static NSString *const kAccessTokenKey = @"idToken";
  127. /** @var kAccessToken
  128. @brief The testing access token.
  129. */
  130. static NSString *const kAccessToken = @"ACCESSTOKEN";
  131. @interface FIRSignInWithGameCenterTests : XCTestCase
  132. /** @property RPCIssuer
  133. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  134. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  135. */
  136. @property(nonatomic, strong) FIRFakeBackendRPCIssuer *RPCIssuer;
  137. /** @property requestConfiguration
  138. @brief This is the request configuration used for testing.
  139. */
  140. @property(nonatomic, strong) FIRAuthRequestConfiguration *requestConfiguration;
  141. @end
  142. @implementation FIRSignInWithGameCenterTests
  143. - (void)setUp {
  144. [super setUp];
  145. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  146. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  147. self.RPCIssuer = RPCIssuer;
  148. self.requestConfiguration =
  149. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey appID:kTestFirebaseAppID];
  150. }
  151. - (void)tearDown {
  152. self.requestConfiguration = nil;
  153. self.RPCIssuer = nil;
  154. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  155. [super tearDown];
  156. }
  157. - (void)testRequestResponseEncoding {
  158. NSData *signature = [[NSData alloc] initWithBase64EncodedString:kSignature options:0];
  159. NSData *salt = [[NSData alloc] initWithBase64EncodedString:kSalt options:0];
  160. FIRSignInWithGameCenterRequest *request =
  161. [[FIRSignInWithGameCenterRequest alloc] initWithPlayerID:kPlayerID
  162. publicKeyURL:[NSURL URLWithString:kPublicKeyURL]
  163. signature:signature
  164. salt:salt
  165. timestamp:kTimestamp
  166. displayName:kDisplayName
  167. requestConfiguration:self.requestConfiguration];
  168. request.accessToken = kAccessToken;
  169. __block BOOL callbackInvoked;
  170. __block FIRSignInWithGameCenterResponse *RPCResponse;
  171. __block NSError *RPCError;
  172. [FIRAuthBackend signInWithGameCenter:request
  173. callback:^(FIRSignInWithGameCenterResponse *_Nullable response,
  174. NSError *_Nullable error) {
  175. RPCResponse = response;
  176. RPCError = error;
  177. callbackInvoked = YES;
  178. }];
  179. XCTAssertEqualObjects(self.RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  180. XCTAssertNotNil(self.RPCIssuer.decodedRequest);
  181. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kPlayerIDKey], kPlayerID);
  182. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kPublicKeyURLKey], kPublicKeyURL);
  183. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kSignatureKey], kSignature);
  184. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kSaltKey], kSalt);
  185. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kTimestampKey],
  186. [NSNumber numberWithInteger:kTimestamp]);
  187. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kAccessTokenKey], kAccessToken);
  188. XCTAssertEqualObjects(self.RPCIssuer.decodedRequest[kDisplayNameKey], kDisplayName);
  189. NSDictionary *jsonDictionary = @{
  190. @"idToken" : kIDToken,
  191. @"refreshToken" : kRefreshToken,
  192. @"localId" : kLocalID,
  193. @"playerId" : kPlayerID,
  194. @"expiresIn" : kApproximateExpirationDate,
  195. @"isNewUser" : [NSNumber numberWithBool:kIsNewUser],
  196. @"displayName" : kDisplayName,
  197. };
  198. [self.RPCIssuer respondWithJSON:jsonDictionary];
  199. XCTAssertTrue(callbackInvoked);
  200. XCTAssertNotNil(RPCResponse);
  201. XCTAssertEqualObjects(RPCResponse.IDToken, kIDToken);
  202. XCTAssertEqualObjects(RPCResponse.refreshToken, kRefreshToken);
  203. XCTAssertEqualObjects(RPCResponse.localID, kLocalID);
  204. XCTAssertEqualObjects(RPCResponse.playerID, kPlayerID);
  205. XCTAssertEqual(RPCResponse.isNewUser, kIsNewUser);
  206. XCTAssertEqualObjects(RPCResponse.displayName, kDisplayName);
  207. }
  208. @end