GIDGoogleUserTest.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/GIDAuthentication_Private.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. GIDAuthentication *authentication =
  39. [[GIDAuthentication alloc] initWithAuthState:authState];
  40. XCTAssertEqualObjects(user.authentication, authentication);
  41. XCTAssertEqualObjects(user.grantedScopes, @[ OIDAuthorizationRequestTestingScope2 ]);
  42. XCTAssertEqualObjects(user.userID, kUserID);
  43. XCTAssertEqualObjects(user.configuration.hostedDomain, kHostedDomain);
  44. XCTAssertEqualObjects(user.configuration.clientID, OIDAuthorizationRequestTestingClientID);
  45. XCTAssertEqualObjects(user.profile, [GIDProfileData testInstance]);
  46. }
  47. - (void)testCoding {
  48. if (@available(iOS 11, macOS 10.13, *)) {
  49. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:[OIDAuthState testInstance]
  50. profileData:[GIDProfileData testInstance]];
  51. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user
  52. requiringSecureCoding:YES
  53. error:nil];
  54. GIDGoogleUser *newUser = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDGoogleUser class]
  55. fromData:data
  56. error:nil];
  57. XCTAssertEqualObjects(user, newUser);
  58. XCTAssertTrue(GIDGoogleUser.supportsSecureCoding);
  59. } else {
  60. XCTSkip(@"Required API is not available for this test.");
  61. }
  62. }
  63. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  64. // Deprecated in iOS 13 and macOS 10.14
  65. - (void)testLegacyCoding {
  66. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:[OIDAuthState testInstance]
  67. profileData:[GIDProfileData testInstance]];
  68. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user];
  69. GIDGoogleUser *newUser = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  70. XCTAssertEqualObjects(user, newUser);
  71. XCTAssertTrue(GIDGoogleUser.supportsSecureCoding);
  72. }
  73. #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
  74. @end