aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Auth/Tests/FIRAdditionalUserInfoTests.m
blob: d50380e475faa2a00deebd1e08c82f4afed2c364 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import <XCTest/XCTest.h>

#import "FIRAdditionalUserInfo_Internal.h"
#import "FIRVerifyAssertionResponse.h"
#import <OCMock/OCMock.h>

NS_ASSUME_NONNULL_BEGIN

/** @var kUserName
    @brief The fake user name.
 */
static NSString *const kUserName = @"User Doe";

/** @var kIsNewUser
    @brief The fake flag that indicates the user has signed in for the first time.
 */
static BOOL kIsNewUser = YES;

/** @var kProviderID
    @brief The fake Provider ID.
 */
static NSString *const kProviderID = @"PROVIDER_ID";

/** @class FIRAdditionalUserInfoTests
    @brief Tests for @c FIRAdditionalUserInfo .
 */
@interface FIRAdditionalUserInfoTests : XCTestCase
@end

@implementation FIRAdditionalUserInfoTests

/** @fn googleProfile
    @brief The fake user profile under additional user data in @c FIRVerifyAssertionResponse.
 */
+ (NSDictionary *)profile {
  static NSDictionary *kProfile = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    kProfile = @{
      @"email": @"user@mail.com",
      @"given_name": @"User",
      @"family_name": @"Doe"
    };
  });
  return kProfile;
}

/** @fn testAditionalUserInfoCreation
    @brief Tests succuessful creation of @c FIRAdditionalUserInfo with
        @c initWithProviderID:profile:username: call.
 */
- (void)testAditionalUserInfoCreation {
  FIRAdditionalUserInfo *userInfo =
      [[FIRAdditionalUserInfo alloc] initWithProviderID:kProviderID
                                                profile:[[self class] profile]
                                               username:kUserName
                                              isNewUser:kIsNewUser];
  XCTAssertEqualObjects(userInfo.providerID, kProviderID);
  XCTAssertEqualObjects(userInfo.profile, [[self class] profile]);
  XCTAssertEqualObjects(userInfo.username, kUserName);
  XCTAssertEqual(userInfo.isNewUser, kIsNewUser);
}

/** @fn testAditionalUserInfoCreationWithStaticInitializer
    @brief Tests succuessful creation of @c FIRAdditionalUserInfo with
        @c userInfoWithVerifyAssertionResponse call.
 */
- (void)testAditionalUserInfoCreationWithStaticInitializer {
  id mockVeriyAssertionResponse = OCMClassMock([FIRVerifyAssertionResponse class]);
  OCMExpect([mockVeriyAssertionResponse providerID]).andReturn(kProviderID);
  OCMExpect([mockVeriyAssertionResponse profile]).andReturn([[self class] profile]);
  OCMExpect([mockVeriyAssertionResponse username]).andReturn(kUserName);
  OCMExpect([mockVeriyAssertionResponse isNewUser]).andReturn(kIsNewUser);

  FIRAdditionalUserInfo *userInfo =
      [FIRAdditionalUserInfo userInfoWithVerifyAssertionResponse:mockVeriyAssertionResponse];
  XCTAssertEqualObjects(userInfo.providerID, kProviderID);
  XCTAssertEqualObjects(userInfo.profile, [[self class] profile]);
  XCTAssertEqualObjects(userInfo.username, kUserName);
  XCTAssertEqual(userInfo.isNewUser, kIsNewUser);
  OCMVerifyAll(mockVeriyAssertionResponse);
}

/** @fn testAdditionalUserInfoCoding
    @brief Tests successful archiving and unarchiving of @c FIRAdditionalUserInfo.
 */
- (void)testAdditionalUserInfoCoding {
  FIRAdditionalUserInfo *userInfo =
      [[FIRAdditionalUserInfo alloc] initWithProviderID:kProviderID
                                                profile:[[self class] profile]
                                               username:kUserName
                                              isNewUser:kIsNewUser];
  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:userInfo];
  XCTAssertNotNil(data, @"Should not be nil if archiving succeeded.");
  XCTAssertNoThrow([NSKeyedUnarchiver unarchiveObjectWithData:data],
                   @"Unarchiving should not throw and exception.");
  FIRAdditionalUserInfo *unarchivedUserInfo = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  XCTAssertTrue([unarchivedUserInfo isKindOfClass:[FIRAdditionalUserInfo class]],
                @"Unarchived object must be of kind FIRAdditionalUserInfo class.");
  XCTAssertEqualObjects(unarchivedUserInfo.providerID, userInfo.providerID);
  XCTAssertEqualObjects(unarchivedUserInfo.profile, userInfo.profile);
  XCTAssertEqualObjects(unarchivedUserInfo.username, userInfo.username);
  XCTAssertEqual(unarchivedUserInfo.isNewUser, unarchivedUserInfo.isNewUser);
}

@end

NS_ASSUME_NONNULL_END