FIRAdditionalUserInfoTests.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2017 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 "FIRAdditionalUserInfo_Internal.h"
  18. #import "FIRVerifyAssertionResponse.h"
  19. #import <OCMock/OCMock.h>
  20. NS_ASSUME_NONNULL_BEGIN
  21. /** @var kUserName
  22. @brief The fake user name.
  23. */
  24. static NSString *const kUserName = @"User Doe";
  25. /** @var kIsNewUser
  26. @brief The fake flag that indicates the user has signed in for the first time.
  27. */
  28. static BOOL kIsNewUser = YES;
  29. /** @var kProviderID
  30. @brief The fake Provider ID.
  31. */
  32. static NSString *const kProviderID = @"PROVIDER_ID";
  33. /** @class FIRAdditionalUserInfoTests
  34. @brief Tests for @c FIRAdditionalUserInfo .
  35. */
  36. @interface FIRAdditionalUserInfoTests : XCTestCase
  37. @end
  38. @implementation FIRAdditionalUserInfoTests
  39. /** @fn googleProfile
  40. @brief The fake user profile under additional user data in @c FIRVerifyAssertionResponse.
  41. */
  42. + (NSDictionary *)profile {
  43. static NSDictionary *kProfile = nil;
  44. static dispatch_once_t onceToken;
  45. dispatch_once(&onceToken, ^{
  46. kProfile = @{
  47. @"email": @"user@mail.com",
  48. @"given_name": @"User",
  49. @"family_name": @"Doe"
  50. };
  51. });
  52. return kProfile;
  53. }
  54. /** @fn testAditionalUserInfoCreation
  55. @brief Tests succuessful creation of @c FIRAdditionalUserInfo with
  56. @c initWithProviderID:profile:username: call.
  57. */
  58. - (void)testAditionalUserInfoCreation {
  59. FIRAdditionalUserInfo *userInfo =
  60. [[FIRAdditionalUserInfo alloc] initWithProviderID:kProviderID
  61. profile:[[self class] profile]
  62. username:kUserName
  63. isNewUser:kIsNewUser];
  64. XCTAssertEqualObjects(userInfo.providerID, kProviderID);
  65. XCTAssertEqualObjects(userInfo.profile, [[self class] profile]);
  66. XCTAssertEqualObjects(userInfo.username, kUserName);
  67. XCTAssertEqual(userInfo.isNewUser, kIsNewUser);
  68. }
  69. /** @fn testAditionalUserInfoCreationWithStaticInitializer
  70. @brief Tests succuessful creation of @c FIRAdditionalUserInfo with
  71. @c userInfoWithVerifyAssertionResponse call.
  72. */
  73. - (void)testAditionalUserInfoCreationWithStaticInitializer {
  74. id mockVeriyAssertionResponse = OCMClassMock([FIRVerifyAssertionResponse class]);
  75. OCMExpect([mockVeriyAssertionResponse providerID]).andReturn(kProviderID);
  76. OCMExpect([mockVeriyAssertionResponse profile]).andReturn([[self class] profile]);
  77. OCMExpect([mockVeriyAssertionResponse username]).andReturn(kUserName);
  78. OCMExpect([mockVeriyAssertionResponse isNewUser]).andReturn(kIsNewUser);
  79. FIRAdditionalUserInfo *userInfo =
  80. [FIRAdditionalUserInfo userInfoWithVerifyAssertionResponse:mockVeriyAssertionResponse];
  81. XCTAssertEqualObjects(userInfo.providerID, kProviderID);
  82. XCTAssertEqualObjects(userInfo.profile, [[self class] profile]);
  83. XCTAssertEqualObjects(userInfo.username, kUserName);
  84. XCTAssertEqual(userInfo.isNewUser, kIsNewUser);
  85. OCMVerifyAll(mockVeriyAssertionResponse);
  86. }
  87. /** @fn testAdditionalUserInfoCoding
  88. @brief Tests successful archiving and unarchiving of @c FIRAdditionalUserInfo.
  89. */
  90. - (void)testAdditionalUserInfoCoding {
  91. FIRAdditionalUserInfo *userInfo =
  92. [[FIRAdditionalUserInfo alloc] initWithProviderID:kProviderID
  93. profile:[[self class] profile]
  94. username:kUserName
  95. isNewUser:kIsNewUser];
  96. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:userInfo];
  97. XCTAssertNotNil(data, @"Should not be nil if archiving succeeded.");
  98. XCTAssertNoThrow([NSKeyedUnarchiver unarchiveObjectWithData:data],
  99. @"Unarchiving should not throw and exception.");
  100. FIRAdditionalUserInfo *unarchivedUserInfo = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  101. XCTAssertTrue([unarchivedUserInfo isKindOfClass:[FIRAdditionalUserInfo class]],
  102. @"Unarchived object must be of kind FIRAdditionalUserInfo class.");
  103. XCTAssertEqualObjects(unarchivedUserInfo.providerID, userInfo.providerID);
  104. XCTAssertEqualObjects(unarchivedUserInfo.profile, userInfo.profile);
  105. XCTAssertEqualObjects(unarchivedUserInfo.username, userInfo.username);
  106. XCTAssertEqual(unarchivedUserInfo.isNewUser, unarchivedUserInfo.isNewUser);
  107. }
  108. @end
  109. NS_ASSUME_NONNULL_END