1+ // Copyright 2019, Google Inc. All rights reserved.
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+
15+ using System ;
16+ using System . Collections . Generic ;
17+ using Xunit ;
18+
19+ namespace FirebaseAdmin . Auth . Tests
20+ {
21+ public class UserRecordTest
22+ {
23+ [ Fact ]
24+ public void NullResponse ( )
25+ {
26+ Assert . Throws < ArgumentException > ( ( ) => new UserRecord ( null ) ) ;
27+ }
28+
29+ [ Fact ]
30+ public void NullUid ( )
31+ {
32+ Assert . Throws < ArgumentException > ( ( ) => new UserRecord ( new GetAccountInfoResponse . User ( )
33+ {
34+ UserId = null ,
35+ } ) ) ;
36+ }
37+
38+ [ Fact ]
39+ public void EmptyUid ( )
40+ {
41+ Assert . Throws < ArgumentException > ( ( ) => new UserRecord ( new GetAccountInfoResponse . User ( )
42+ {
43+ UserId = string . Empty ,
44+ } ) ) ;
45+ }
46+
47+ [ Fact ]
48+ public void UidOnly ( )
49+ {
50+ var user = new UserRecord ( new GetAccountInfoResponse . User ( )
51+ {
52+ UserId = "user1" ,
53+ } ) ;
54+
55+ Assert . Equal ( "user1" , user . Uid ) ;
56+ Assert . Null ( user . DisplayName ) ;
57+ Assert . Null ( user . Email ) ;
58+ Assert . Null ( user . PhoneNumber ) ;
59+ Assert . Null ( user . PhotoUrl ) ;
60+ Assert . Equal ( "firebase" , user . ProviderId ) ;
61+ Assert . False ( user . Disabled ) ;
62+ Assert . False ( user . EmailVerified ) ;
63+ Assert . Equal ( UserRecord . UnixEpoch , user . TokensValidAfterTimestamp ) ;
64+ Assert . Empty ( user . CustomClaims ) ;
65+ Assert . Empty ( user . ProviderData ) ;
66+ Assert . NotNull ( user . UserMetaData ) ;
67+ Assert . Equal ( DateTime . MinValue , user . UserMetaData . CreationTimestamp ) ;
68+ Assert . Equal ( DateTime . MinValue , user . UserMetaData . LastSignInTimestamp ) ;
69+ }
70+
71+ [ Fact ]
72+ public void AllProperties ( )
73+ {
74+ var response = new GetAccountInfoResponse . User ( )
75+ {
76+ UserId = "user1" ,
77+ DisplayName = "Test User" ,
78+ 79+ PhoneNumber = "+11234567890" ,
80+ PhotoUrl = "https://domain.com/user.png" ,
81+ Disabled = true ,
82+ EmailVerified = true ,
83+ ValidSince = 3600 ,
84+ CreatedAt = 100 ,
85+ LastLoginAt = 150 ,
86+ CustomClaims = @"{""admin"": true, ""level"": 10}" ,
87+ Providers = new List < GetAccountInfoResponse . Provider > ( )
88+ {
89+ new GetAccountInfoResponse . Provider ( )
90+ {
91+ ProviderID = "google.com" ,
92+ UserId = "googleuid" ,
93+ } ,
94+ new GetAccountInfoResponse . Provider ( )
95+ {
96+ ProviderID = "other.com" ,
97+ UserId = "otheruid" ,
98+ DisplayName = "Other Name" ,
99+ 100+ PhotoUrl = "https://other.com/user.png" ,
101+ PhoneNumber = "+10987654321" ,
102+ } ,
103+ } ,
104+ } ;
105+ var user = new UserRecord ( response ) ;
106+
107+ Assert . Equal ( "user1" , user . Uid ) ;
108+ Assert . Equal ( "Test User" , user . DisplayName ) ;
109+ Assert . Equal ( "[email protected] " , user . Email ) ; 110+ Assert . Equal ( "+11234567890" , user . PhoneNumber ) ;
111+ Assert . Equal ( "https://domain.com/user.png" , user . PhotoUrl ) ;
112+ Assert . Equal ( "firebase" , user . ProviderId ) ;
113+ Assert . True ( user . Disabled ) ;
114+ Assert . True ( user . EmailVerified ) ;
115+ Assert . Equal ( UserRecord . UnixEpoch . AddSeconds ( 3600 ) , user . TokensValidAfterTimestamp ) ;
116+
117+ var claims = new Dictionary < string , object > ( )
118+ {
119+ { "admin" , true } ,
120+ { "level" , 10L } ,
121+ } ;
122+ Assert . Equal ( claims , user . CustomClaims ) ;
123+
124+ Assert . Equal ( 2 , user . ProviderData . Length ) ;
125+ var provider = user . ProviderData [ 0 ] ;
126+ Assert . Equal ( "google.com" , provider . ProviderId ) ;
127+ Assert . Equal ( "googleuid" , provider . Uid ) ;
128+ Assert . Null ( provider . DisplayName ) ;
129+ Assert . Null ( provider . Email ) ;
130+ Assert . Null ( provider . PhoneNumber ) ;
131+ Assert . Null ( provider . PhotoUrl ) ;
132+
133+ provider = user . ProviderData [ 1 ] ;
134+ Assert . Equal ( "other.com" , provider . ProviderId ) ;
135+ Assert . Equal ( "otheruid" , provider . Uid ) ;
136+ Assert . Equal ( "Other Name" , provider . DisplayName ) ;
137+ Assert . Equal ( "[email protected] " , provider . Email ) ; 138+ Assert . Equal ( "+10987654321" , provider . PhoneNumber ) ;
139+ Assert . Equal ( "https://other.com/user.png" , provider . PhotoUrl ) ;
140+
141+ var metadata = user . UserMetaData ;
142+ Assert . NotNull ( metadata ) ;
143+ Assert . Equal ( UserRecord . UnixEpoch . AddMilliseconds ( 100 ) , metadata . CreationTimestamp ) ;
144+ Assert . Equal ( UserRecord . UnixEpoch . AddMilliseconds ( 150 ) , metadata . LastSignInTimestamp ) ;
145+ }
146+ }
147+ }
0 commit comments