GIDGoogleUserTest.m 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/GIDConfiguration.h"
  17. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h"
  18. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDToken.h"
  19. #import "GoogleSignIn/Sources/GIDAuthentication.h"
  20. #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
  21. #import "GoogleSignIn/Tests/Unit/GIDProfileData+Testing.h"
  22. #import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h"
  23. #import "GoogleSignIn/Tests/Unit/OIDAuthorizationRequest+Testing.h"
  24. #import "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h"
  25. #ifdef SWIFT_PACKAGE
  26. @import AppAuth;
  27. #else
  28. #import <AppAuth/OIDAuthState.h>
  29. #endif
  30. @interface GIDGoogleUserTest : XCTestCase
  31. @end
  32. @implementation GIDGoogleUserTest
  33. #pragma mark - Tests
  34. - (void)testInitWithAuthState {
  35. OIDAuthState *authState = [OIDAuthState testInstance];
  36. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:authState
  37. profileData:[GIDProfileData testInstance]];
  38. XCTAssertEqualObjects(user.grantedScopes, @[ OIDAuthorizationRequestTestingScope2 ]);
  39. XCTAssertEqualObjects(user.userID, kUserID);
  40. XCTAssertEqualObjects(user.configuration.hostedDomain, kHostedDomain);
  41. XCTAssertEqualObjects(user.configuration.clientID, OIDAuthorizationRequestTestingClientID);
  42. XCTAssertEqualObjects(user.profile, [GIDProfileData testInstance]);
  43. XCTAssertEqualObjects(user.accessToken.tokenString, kAccessToken);
  44. XCTAssertEqualObjects(user.refreshToken.tokenString, kRefreshToken);
  45. OIDIDToken *idToken = [[OIDIDToken alloc]
  46. initWithIDTokenString:authState.lastTokenResponse.idToken];
  47. XCTAssertEqualObjects(user.idToken.expirationDate, [idToken expiresAt]);
  48. }
  49. - (void)testCoding {
  50. if (@available(iOS 11, macOS 10.13, *)) {
  51. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:[OIDAuthState testInstance]
  52. profileData:[GIDProfileData testInstance]];
  53. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user
  54. requiringSecureCoding:YES
  55. error:nil];
  56. GIDGoogleUser *newUser = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDGoogleUser class]
  57. fromData:data
  58. error:nil];
  59. XCTAssertEqualObjects(user, newUser);
  60. XCTAssertTrue(GIDGoogleUser.supportsSecureCoding);
  61. } else {
  62. XCTSkip(@"Required API is not available for this test.");
  63. }
  64. }
  65. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  66. // Deprecated in iOS 13 and macOS 10.14
  67. - (void)testLegacyCoding {
  68. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:[OIDAuthState testInstance]
  69. profileData:[GIDProfileData testInstance]];
  70. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user];
  71. GIDGoogleUser *newUser = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  72. XCTAssertEqualObjects(user, newUser);
  73. XCTAssertTrue(GIDGoogleUser.supportsSecureCoding);
  74. }
  75. #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
  76. @end