FIRAdditionalUserInfoTests.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <OCMock/OCMock.h>
  18. #import "FIRAdditionalUserInfo_Internal.h"
  19. #import "FIRVerifyAssertionResponse.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 = @{@"email" : @"user@mail.com", @"given_name" : @"User", @"family_name" : @"Doe"};
  47. });
  48. return kProfile;
  49. }
  50. /** @fn testAditionalUserInfoCreation
  51. @brief Tests succuessful creation of @c FIRAdditionalUserInfo with
  52. @c initWithProviderID:profile:username: call.
  53. */
  54. - (void)testAditionalUserInfoCreation {
  55. FIRAdditionalUserInfo *userInfo =
  56. [[FIRAdditionalUserInfo alloc] initWithProviderID:kProviderID
  57. profile:[[self class] profile]
  58. username:kUserName
  59. isNewUser:kIsNewUser];
  60. XCTAssertEqualObjects(userInfo.providerID, kProviderID);
  61. XCTAssertEqualObjects(userInfo.profile, [[self class] profile]);
  62. XCTAssertEqualObjects(userInfo.username, kUserName);
  63. XCTAssertEqual(userInfo.isNewUser, kIsNewUser);
  64. }
  65. /** @fn testAditionalUserInfoCreationWithStaticInitializer
  66. @brief Tests succuessful creation of @c FIRAdditionalUserInfo with
  67. @c userInfoWithVerifyAssertionResponse call.
  68. */
  69. - (void)testAditionalUserInfoCreationWithStaticInitializer {
  70. id mockVeriyAssertionResponse = OCMClassMock([FIRVerifyAssertionResponse class]);
  71. OCMExpect([mockVeriyAssertionResponse providerID]).andReturn(kProviderID);
  72. OCMExpect([mockVeriyAssertionResponse profile]).andReturn([[self class] profile]);
  73. OCMExpect([mockVeriyAssertionResponse username]).andReturn(kUserName);
  74. OCMExpect([mockVeriyAssertionResponse isNewUser]).andReturn(kIsNewUser);
  75. FIRAdditionalUserInfo *userInfo =
  76. [FIRAdditionalUserInfo userInfoWithVerifyAssertionResponse:mockVeriyAssertionResponse];
  77. XCTAssertEqualObjects(userInfo.providerID, kProviderID);
  78. XCTAssertEqualObjects(userInfo.profile, [[self class] profile]);
  79. XCTAssertEqualObjects(userInfo.username, kUserName);
  80. XCTAssertEqual(userInfo.isNewUser, kIsNewUser);
  81. OCMVerifyAll(mockVeriyAssertionResponse);
  82. }
  83. /** @fn testAdditionalUserInfoCoding
  84. @brief Tests successful archiving and unarchiving of @c FIRAdditionalUserInfo.
  85. */
  86. - (void)testAdditionalUserInfoCoding {
  87. FIRAdditionalUserInfo *userInfo =
  88. [[FIRAdditionalUserInfo alloc] initWithProviderID:kProviderID
  89. profile:[[self class] profile]
  90. username:kUserName
  91. isNewUser:kIsNewUser];
  92. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:userInfo];
  93. XCTAssertNotNil(data, @"Should not be nil if archiving succeeded.");
  94. XCTAssertNoThrow([NSKeyedUnarchiver unarchiveObjectWithData:data],
  95. @"Unarchiving should not throw and exception.");
  96. FIRAdditionalUserInfo *unarchivedUserInfo = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  97. XCTAssertTrue([unarchivedUserInfo isKindOfClass:[FIRAdditionalUserInfo class]],
  98. @"Unarchived object must be of kind FIRAdditionalUserInfo class.");
  99. XCTAssertEqualObjects(unarchivedUserInfo.providerID, userInfo.providerID);
  100. XCTAssertEqualObjects(unarchivedUserInfo.profile, userInfo.profile);
  101. XCTAssertEqualObjects(unarchivedUserInfo.username, userInfo.username);
  102. XCTAssertEqual(unarchivedUserInfo.isNewUser, unarchivedUserInfo.isNewUser);
  103. }
  104. @end
  105. NS_ASSUME_NONNULL_END