GIDGoogleUserTest.m 3.5 KB

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