FIRSignInWithGameCenterTests.m 7.9 KB

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