@@ -24,13 +24,13 @@ import { UserInfo } from '@firebase/auth-types-exp';
24
24
25
25
// import { UserInfo } from '@firebase/auth-types-exp';
26
26
import { mockEndpoint } from '../../../test/api/helper' ;
27
- import { testUser } from '../../../test/mock_auth' ;
27
+ import { testPersistence , testUser } from '../../../test/mock_auth' ;
28
28
import * as fetch from '../../../test/mock_fetch' ;
29
29
import { Endpoint } from '../../api' ;
30
+ import { Auth } from '../../model/auth' ;
30
31
import { User } from '../../model/user' ;
31
32
import { ProviderId } from '../providers' ;
32
- // import { ProviderId } from '../providers';
33
- import { updateProfile } from './account_info' ;
33
+ import { updateEmail , updatePassword , updateProfile } from './account_info' ;
34
34
35
35
use ( chaiAsPromised ) ;
36
36
use ( sinonChai ) ;
@@ -49,83 +49,194 @@ describe('core/user/profile', () => {
49
49
50
50
beforeEach ( ( ) => {
51
51
user = testUser ( 'uid' , '' , true ) ;
52
+ fetch . setUp ( ) ;
52
53
} ) ;
53
54
54
55
afterEach ( ( ) => {
55
56
sinon . restore ( ) ;
57
+ fetch . tearDown ( ) ;
56
58
} ) ;
57
59
58
- beforeEach ( fetch . setUp ) ;
59
- afterEach ( fetch . tearDown ) ;
60
-
61
- it ( 'returns immediately if profile object is empty' , async ( ) => {
62
- const ep = mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , { } ) ;
63
- await updateProfile ( user , { } ) ;
64
- expect ( ep . calls ) . to . be . empty ;
65
- } ) ;
60
+ describe ( '#updateProfile' , ( ) => {
61
+ it ( 'returns immediately if profile object is empty' , async ( ) => {
62
+ const ep = mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , { } ) ;
63
+ await updateProfile ( user , { } ) ;
64
+ expect ( ep . calls ) . to . be . empty ;
65
+ } ) ;
66
66
67
- it ( 'calls the setAccountInfo endpoint' , async ( ) => {
68
- const ep = mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , { } ) ;
67
+ it ( 'calls the setAccountInfo endpoint' , async ( ) => {
68
+ const ep = mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , { } ) ;
69
69
70
- await updateProfile ( user , { displayName : 'displayname' , photoURL : 'photo' } ) ;
71
- expect ( ep . calls [ 0 ] . request ) . to . eql ( {
72
- idToken : 'access-token' ,
73
- displayName : 'displayname' ,
74
- photoUrl : 'photo' ,
70
+ await updateProfile ( user , { displayName : 'displayname' , photoURL : 'photo' } ) ;
71
+ expect ( ep . calls [ 0 ] . request ) . to . eql ( {
72
+ idToken : 'access-token' ,
73
+ displayName : 'displayname' ,
74
+ photoUrl : 'photo' ,
75
+ } ) ;
75
76
} ) ;
76
- } ) ;
77
77
78
- it ( 'sets the fields on the user based on the response' , async ( ) => {
79
- mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , {
80
- displayName : 'response-name' ,
81
- photoUrl : 'response-photo' ,
78
+ it ( 'sets the fields on the user based on the response' , async ( ) => {
79
+ mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , {
80
+ displayName : 'response-name' ,
81
+ photoUrl : 'response-photo' ,
82
+ } ) ;
83
+
84
+ await updateProfile ( user , { displayName : 'displayname' , photoURL : 'photo' } ) ;
85
+ expect ( user . displayName ) . to . eq ( 'response-name' ) ;
86
+ expect ( user . photoURL ) . to . eq ( 'response-photo' ) ;
82
87
} ) ;
83
88
84
- await updateProfile ( user , { displayName : 'displayname' , photoURL : 'photo' } ) ;
85
- expect ( user . displayName ) . to . eq ( 'response-name' ) ;
86
- expect ( user . photoURL ) . to . eq ( 'response-photo' ) ;
89
+ it ( 'sets the fields on the password provider' , async ( ) => {
90
+ mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , {
91
+ displayName : 'response-name' ,
92
+ photoUrl : 'response-photo' ,
93
+ } ) ;
94
+ user . providerData = [ { ...PASSWORD_PROVIDER } ] ;
95
+
96
+ await updateProfile ( user , { displayName : 'displayname' , photoURL : 'photo' } ) ;
97
+ const provider = user . providerData [ 0 ] ;
98
+ expect ( provider . displayName ) . to . eq ( 'response-name' ) ;
99
+ expect ( provider . photoURL ) . to . eq ( 'response-photo' ) ;
100
+ } ) ;
87
101
} ) ;
88
102
89
- it ( 'sets the fields on the passwd provider' , async ( ) => {
90
- mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , {
91
- displayName : 'response-name' ,
92
- photoUrl : 'response-photo' ,
103
+ describe ( '#updateEmail' , ( ) => {
104
+ it ( 'calls the setAccountInfo endpoint and reloads the user' , async ( ) => {
105
+ const set = mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , { } ) ;
106
+ mockEndpoint ( Endpoint . GET_ACCOUNT_INFO , { users : [
107
+ { localId : 'new-uid-to-prove-refresh-got-called' } ,
108
+ ] } ) ;
109
+
110
+ await updateEmail ( user , '[email protected] ' ) ;
111
+ expect ( set . calls [ 0 ] . request ) . to . eql ( {
112
+ idToken : 'access-token' ,
113
+
114
+ } ) ;
115
+
116
+ expect ( user . uid ) . to . eq ( 'new-uid-to-prove-refresh-got-called' ) ;
93
117
} ) ;
94
- user . providerData = [ { ...PASSWORD_PROVIDER } ] ;
118
+ } ) ;
119
+
120
+ describe ( '#updatePassword' , ( ) => {
121
+ it ( 'calls the setAccountInfo endpoint and reloads the user' , async ( ) => {
122
+ const set = mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , { } ) ;
123
+ mockEndpoint ( Endpoint . GET_ACCOUNT_INFO , { users : [
124
+ { localId : 'new-uid-to-prove-refresh-got-called' } ,
125
+ ] } ) ;
126
+
127
+ await updatePassword ( user , 'pass' ) ;
128
+ expect ( set . calls [ 0 ] . request ) . to . eql ( {
129
+ idToken : 'access-token' ,
130
+ password : 'pass' ,
131
+ } ) ;
95
132
96
- await updateProfile ( user , { displayName : 'displayname' , photoURL : 'photo' } ) ;
97
- const provider = user . providerData [ 0 ] ;
98
- expect ( provider . displayName ) . to . eq ( 'response-name' ) ;
99
- expect ( provider . photoURL ) . to . eq ( 'response-photo' ) ;
133
+ expect ( user . uid ) . to . eq ( 'new-uid-to-prove-refresh-got-called' ) ;
134
+ } ) ;
100
135
} ) ;
101
136
102
137
describe ( 'notifications' , ( ) => {
103
- beforeEach ( ( ) => {
104
- user . auth . currentUser = user ;
138
+ let auth : Auth ;
139
+ let idTokenChange : sinon . SinonStub ;
140
+
141
+ beforeEach ( async ( ) => {
142
+ auth = user . auth ;
143
+ idTokenChange = sinon . stub ( ) ;
144
+ auth . onIdTokenChanged ( idTokenChange ) ;
145
+
146
+ // Flush token change promises which are floating
147
+ await auth . updateCurrentUser ( user ) ;
148
+ auth . _isInitialized = true ;
149
+ idTokenChange . resetHistory ( ) ;
105
150
} ) ;
106
151
107
- it ( 'triggers a token update if necessary' , async ( ) => {
108
- mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , {
109
- idToken : 'new-id-token' ,
110
- refreshToken : 'new-refresh-token' ,
111
- expiresIn : 300 ,
152
+ describe ( '#updateProfile' , ( ) => {
153
+ it ( 'triggers a token update if necessary' , async ( ) => {
154
+ mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , {
155
+ idToken : 'new-id-token' ,
156
+ refreshToken : 'new-refresh-token' ,
157
+ expiresIn : 300 ,
158
+ } ) ;
159
+
160
+ await updateProfile ( user , { displayName : 'd' } ) ;
161
+ expect ( idTokenChange ) . to . have . been . called ;
162
+ expect ( testPersistence . lastPersistedBlob ) . to . eql ( user . toPlainObject ( ) ) ;
112
163
} ) ;
113
164
114
- const notifySpy = sinon . stub ( user . auth , '_notifyStateListeners' ) ;
115
- await updateProfile ( user , { displayName : 'd' } ) ;
116
- expect ( notifySpy ) . to . have . been . called ;
165
+ it ( 'does NOT trigger a token update if unnecessary' , async ( ) => {
166
+ mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , {
167
+ idToken : 'access-token' ,
168
+ refreshToken : 'refresh-token' ,
169
+ expiresIn : 300 ,
170
+ } ) ;
171
+
172
+ await updateProfile ( user , { displayName : 'd' } ) ;
173
+ expect ( idTokenChange ) . not . to . have . been . called ;
174
+ expect ( testPersistence . lastPersistedBlob ) . to . eql ( user . toPlainObject ( ) ) ;
175
+ } ) ;
117
176
} ) ;
118
177
119
- it ( 'does NOT trigger a token update if unnecessary' , async ( ) => {
120
- mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , {
121
- idToken : 'access-token' ,
122
- refreshToken : 'refresh-token' ,
123
- expiresIn : 300 ,
178
+ describe ( '#updateEmail' , ( ) => {
179
+ beforeEach ( ( ) => {
180
+ // This is necessary because this method calls reload; we don't care about that though ,
181
+ // for these tests we're looking at the change listeners
182
+ mockEndpoint ( Endpoint . GET_ACCOUNT_INFO , { users : [ { } ] } ) ;
124
183
} ) ;
125
184
126
- const notifySpy = sinon . stub ( user . auth , '_notifyStateListeners' ) ;
127
- await updateProfile ( user , { displayName : 'd' } ) ;
128
- expect ( notifySpy ) . not . to . have . been . called ;
185
+ it ( 'triggers a token update if necessary' , async ( ) => {
186
+ mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , {
187
+ idToken : 'new-id-token' ,
188
+ refreshToken : 'new-refresh-token' ,
189
+ expiresIn : 300 ,
190
+ } ) ;
191
+
192
+ await updatePassword ( user , '[email protected] ' ) ;
193
+ expect ( idTokenChange ) . to . have . been . called ;
194
+ expect ( testPersistence . lastPersistedBlob ) . to . eql ( user . toPlainObject ( ) ) ;
195
+ } ) ;
196
+
197
+ it ( 'does NOT trigger a token update if unnecessary' , async ( ) => {
198
+ mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , {
199
+ idToken : 'access-token' ,
200
+ refreshToken : 'refresh-token' ,
201
+ expiresIn : 300 ,
202
+ } ) ;
203
+
204
+ await updateEmail ( user , '[email protected] ' ) ;
205
+ expect ( idTokenChange ) . not . to . have . been . called ;
206
+ expect ( testPersistence . lastPersistedBlob ) . to . eql ( user . toPlainObject ( ) ) ;
207
+ } ) ;
208
+ } ) ;
209
+
210
+ describe ( '#updatePassword' , ( ) => {
211
+ beforeEach ( ( ) => {
212
+ // This is necessary because this method calls reload; we don't care about that though,
213
+ // for these tests we're looking at the change listeners
214
+ mockEndpoint ( Endpoint . GET_ACCOUNT_INFO , { users : [ { } ] } ) ;
215
+ } ) ;
216
+
217
+ it ( 'triggers a token update if necessary' , async ( ) => {
218
+ mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , {
219
+ idToken : 'new-id-token' ,
220
+ refreshToken : 'new-refresh-token' ,
221
+ expiresIn : 300 ,
222
+ } ) ;
223
+
224
+ await updatePassword ( user , 'pass' ) ;
225
+ expect ( idTokenChange ) . to . have . been . called ;
226
+ expect ( testPersistence . lastPersistedBlob ) . to . eql ( user . toPlainObject ( ) ) ;
227
+ } ) ;
228
+
229
+ it ( 'does NOT trigger a token update if unnecessary' , async ( ) => {
230
+ mockEndpoint ( Endpoint . SET_ACCOUNT_INFO , {
231
+ idToken : 'access-token' ,
232
+ refreshToken : 'refresh-token' ,
233
+ expiresIn : 300 ,
234
+ } ) ;
235
+
236
+ await updatePassword ( user , 'pass' ) ;
237
+ expect ( idTokenChange ) . not . to . have . been . called ;
238
+ expect ( testPersistence . lastPersistedBlob ) . to . eql ( user . toPlainObject ( ) ) ;
239
+ } ) ;
129
240
} ) ;
130
241
} ) ;
131
242
} ) ;
0 commit comments