GIDGoogleUserTest.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h"
  15. #import <XCTest/XCTest.h>
  16. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDAuthentication.h"
  17. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
  18. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h"
  19. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDToken.h"
  20. #import "GoogleSignIn/Sources/GIDAuthentication_Private.h"
  21. #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
  22. #import "GoogleSignIn/Tests/Unit/GIDProfileData+Testing.h"
  23. #import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h"
  24. #import "GoogleSignIn/Tests/Unit/OIDAuthorizationRequest+Testing.h"
  25. #import "GoogleSignIn/Tests/Unit/OIDTokenRequest+Testing.h"
  26. #import "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h"
  27. #ifdef SWIFT_PACKAGE
  28. @import AppAuth;
  29. #else
  30. #import <AppAuth/OIDAuthState.h>
  31. #endif
  32. static NSString *const kNewAccessToken = @"new_access_token";
  33. static NSString *const kNewRefreshToken = @"new_refresh_token";
  34. static NSTimeInterval const kTimeAccuracy = 10;
  35. static NSTimeInterval const kIDTokenExpiresIn = 100;
  36. static NSTimeInterval const kNewIDTokenExpiresIn = 200;
  37. @interface GIDGoogleUserTest : XCTestCase
  38. @end
  39. @implementation GIDGoogleUserTest
  40. #pragma mark - Tests
  41. - (void)testInitWithAuthState {
  42. OIDAuthState *authState = [OIDAuthState testInstance];
  43. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:authState
  44. profileData:[GIDProfileData testInstance]];
  45. GIDAuthentication *authentication =
  46. [[GIDAuthentication alloc] initWithAuthState:authState];
  47. XCTAssertEqualObjects(user.authentication, authentication);
  48. XCTAssertEqualObjects(user.grantedScopes, @[ OIDAuthorizationRequestTestingScope2 ]);
  49. XCTAssertEqualObjects(user.userID, kUserID);
  50. XCTAssertEqualObjects(user.configuration.hostedDomain, kHostedDomain);
  51. XCTAssertEqualObjects(user.configuration.clientID, OIDAuthorizationRequestTestingClientID);
  52. XCTAssertEqualObjects(user.profile, [GIDProfileData testInstance]);
  53. XCTAssertEqualObjects(user.accessToken.tokenString, kAccessToken);
  54. XCTAssertEqualObjects(user.refreshToken.tokenString, kRefreshToken);
  55. OIDIDToken *idToken = [[OIDIDToken alloc]
  56. initWithIDTokenString:authState.lastTokenResponse.idToken];
  57. XCTAssertEqualObjects(user.idToken.expirationDate, [idToken expiresAt]);
  58. }
  59. - (void)testCoding {
  60. if (@available(iOS 11, macOS 10.13, *)) {
  61. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:[OIDAuthState testInstance]
  62. profileData:[GIDProfileData testInstance]];
  63. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user
  64. requiringSecureCoding:YES
  65. error:nil];
  66. GIDGoogleUser *newUser = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDGoogleUser class]
  67. fromData:data
  68. error:nil];
  69. XCTAssertEqualObjects(user, newUser);
  70. XCTAssertTrue(GIDGoogleUser.supportsSecureCoding);
  71. } else {
  72. XCTSkip(@"Required API is not available for this test.");
  73. }
  74. }
  75. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  76. // Deprecated in iOS 13 and macOS 10.14
  77. - (void)testLegacyCoding {
  78. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:[OIDAuthState testInstance]
  79. profileData:[GIDProfileData testInstance]];
  80. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user];
  81. GIDGoogleUser *newUser = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  82. XCTAssertEqualObjects(user, newUser);
  83. XCTAssertTrue(GIDGoogleUser.supportsSecureCoding);
  84. }
  85. #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
  86. - (void)testUpdateAuthState {
  87. GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:kAccessTokenExpiresIn
  88. idTokenExpiresIn:kIDTokenExpiresIn];
  89. NSString *updatedIDToken = [self idTokenWithExpiresIn:kNewIDTokenExpiresIn];
  90. OIDAuthState *updatedAuthState = [OIDAuthState testInstanceWithIDToken:updatedIDToken
  91. accessToken:kNewAccessToken
  92. accessTokenExpiresIn:kAccessTokenExpiresIn
  93. refreshToken:kNewRefreshToken];
  94. GIDProfileData *updatedProfileData = [GIDProfileData testInstance];
  95. [user updateAuthState:updatedAuthState profileData:updatedProfileData];
  96. XCTAssertEqualObjects(user.accessToken.tokenString, kNewAccessToken);
  97. NSDate *expectedAccessTokenExpirationDate = [[NSDate date] dateByAddingTimeInterval:kAccessTokenExpiresIn];
  98. XCTAssertEqualWithAccuracy([user.accessToken.expirationDate timeIntervalSince1970],
  99. [expectedAccessTokenExpirationDate timeIntervalSince1970], kTimeAccuracy);
  100. XCTAssertEqualObjects(user.idToken.tokenString, updatedIDToken);
  101. NSDate *expectedIDTokenExpirationDate = [[NSDate date] dateByAddingTimeInterval:kNewIDTokenExpiresIn];
  102. XCTAssertEqualWithAccuracy([user.idToken.expirationDate timeIntervalSince1970],
  103. [expectedIDTokenExpirationDate timeIntervalSince1970], kTimeAccuracy);
  104. XCTAssertEqualObjects(user.refreshToken.tokenString, kNewRefreshToken);
  105. XCTAssertEqual(user.profile, updatedProfileData);
  106. }
  107. // When updating with a new OIDAuthState in which token information is not changed, the token objects
  108. // should remain the same.
  109. - (void)testUpdateAuthState_tokensAreNotChanged {
  110. NSString *idToken = [self idTokenWithExpiresIn:kIDTokenExpiresIn];
  111. OIDAuthState *authState = [OIDAuthState testInstanceWithIDToken:idToken
  112. accessToken:kAccessToken
  113. accessTokenExpiresIn:kAccessTokenExpiresIn
  114. refreshToken:kRefreshToken];
  115. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:authState profileData:nil];
  116. GIDToken *accessTokenBeforeUpdate = user.accessToken;
  117. GIDToken *refreshTokenBeforeUpdate = user.refreshToken;
  118. GIDToken *idTokenBeforeUpdate = user.idToken;
  119. [user updateAuthState:authState profileData:nil];
  120. XCTAssertIdentical(user.accessToken, accessTokenBeforeUpdate);
  121. XCTAssertIdentical(user.idToken, idTokenBeforeUpdate);
  122. XCTAssertIdentical(user.refreshToken, refreshTokenBeforeUpdate);
  123. }
  124. #pragma mark - Helpers
  125. - (GIDGoogleUser *)googleUserWithAccessTokenExpiresIn:(NSTimeInterval)accessTokenExpiresIn
  126. idTokenExpiresIn:(NSTimeInterval)idTokenExpiresIn {
  127. NSString *idToken = [self idTokenWithExpiresIn:idTokenExpiresIn];
  128. OIDAuthState *authState = [OIDAuthState testInstanceWithIDToken:idToken
  129. accessToken:kAccessToken
  130. accessTokenExpiresIn:accessTokenExpiresIn
  131. refreshToken:kRefreshToken];
  132. return [[GIDGoogleUser alloc] initWithAuthState:authState profileData:nil];
  133. }
  134. - (NSString *)idTokenWithExpiresIn:(NSTimeInterval)expiresIn {
  135. // The expireTime should be based on 1970.
  136. NSTimeInterval expireTime = [[NSDate date] timeIntervalSince1970] + expiresIn;
  137. return [OIDTokenResponse idTokenWithSub:kUserID exp:@(expireTime)];
  138. }
  139. @end