@@ -23,20 +23,26 @@ import {
23
23
ProviderId ,
24
24
SignInMethod
25
25
} from '@firebase/auth-types-exp' ;
26
+ import { FirebaseError } from '@firebase/util' ;
26
27
27
28
import { mockEndpoint } from '../../../test/api/helper' ;
28
- import { testAuth } from '../../../test/mock_auth' ;
29
+ import { testAuth , testUser } from '../../../test/mock_auth' ;
29
30
import { MockAuthCredential } from '../../../test/mock_auth_credential' ;
30
31
import * as mockFetch from '../../../test/mock_fetch' ;
31
32
import { Endpoint } from '../../api' ;
32
33
import { APIUserInfo } from '../../api/account_management/account' ;
33
34
import { Auth } from '../../model/auth' ;
34
35
import { IdTokenResponse } from '../../model/id_token' ;
35
- import { signInWithCredential } from './credential' ;
36
+ import { User } from '../../model/user' ;
37
+ import {
38
+ _assertLinkedStatus ,
39
+ linkWithCredential ,
40
+ signInWithCredential
41
+ } from './credential' ;
36
42
37
43
use ( chaiAsPromised ) ;
38
44
39
- describe ( 'core/strategies/signInWithCredential ' , ( ) => {
45
+ describe ( 'core/strategies/credential ' , ( ) => {
40
46
const serverUser : APIUserInfo = {
41
47
localId : 'local-id' ,
42
48
displayName : 'display-name' ,
@@ -63,32 +69,115 @@ describe('core/strategies/signInWithCredential', () => {
63
69
) ;
64
70
65
71
let auth : Auth ;
72
+ let getAccountInfoEndpoint : mockFetch . Route ;
73
+ let user : User ;
66
74
67
75
beforeEach ( async ( ) => {
68
76
auth = await testAuth ( ) ;
69
77
mockFetch . setUp ( ) ;
70
78
authCredential . _setIdTokenResponse ( idTokenResponse ) ;
71
- mockEndpoint ( Endpoint . GET_ACCOUNT_INFO , {
79
+ getAccountInfoEndpoint = mockEndpoint ( Endpoint . GET_ACCOUNT_INFO , {
72
80
users : [ serverUser ]
73
81
} ) ;
82
+
83
+ user = testUser ( auth , 'uid' , undefined , true ) ;
74
84
} ) ;
85
+
75
86
afterEach ( mockFetch . tearDown ) ;
76
87
77
- it ( 'should return a valid user credential' , async ( ) => {
78
- const { credential, user, operationType } = await signInWithCredential (
79
- auth ,
80
- authCredential
81
- ) ;
82
- expect ( credential ! . providerId ) . to . eq ( ProviderId . FIREBASE ) ;
83
- expect ( credential ! . signInMethod ) . to . eq ( SignInMethod . EMAIL_LINK ) ;
84
- expect ( user . uid ) . to . eq ( 'local-id' ) ;
85
- expect ( user . tenantId ) . to . eq ( 'tenant-id' ) ;
86
- expect ( user . displayName ) . to . eq ( 'display-name' ) ;
87
- expect ( operationType ) . to . eq ( OperationType . SIGN_IN ) ;
88
+ describe ( 'signInWithCredential' , ( ) => {
89
+ it ( 'should return a valid user credential' , async ( ) => {
90
+ const { credential, user, operationType } = await signInWithCredential (
91
+ auth ,
92
+ authCredential
93
+ ) ;
94
+ expect ( credential ! . providerId ) . to . eq ( ProviderId . FIREBASE ) ;
95
+ expect ( credential ! . signInMethod ) . to . eq ( SignInMethod . EMAIL_LINK ) ;
96
+ expect ( user . uid ) . to . eq ( 'local-id' ) ;
97
+ expect ( user . tenantId ) . to . eq ( 'tenant-id' ) ;
98
+ expect ( user . displayName ) . to . eq ( 'display-name' ) ;
99
+ expect ( operationType ) . to . eq ( OperationType . SIGN_IN ) ;
100
+ } ) ;
101
+
102
+ it ( 'should update the current user' , async ( ) => {
103
+ const { user } = await signInWithCredential ( auth , authCredential ) ;
104
+ expect ( auth . currentUser ) . to . eq ( user ) ;
105
+ } ) ;
106
+ } ) ;
107
+
108
+ describe ( 'linkWithCredential' , ( ) => {
109
+ it ( 'should throw an error if the provider is already linked' , async ( ) => {
110
+ getAccountInfoEndpoint . response = {
111
+ users : [
112
+ {
113
+ ...serverUser ,
114
+ providerUserInfo : [ { providerId : ProviderId . FIREBASE } ]
115
+ }
116
+ ]
117
+ } ;
118
+
119
+ await expect ( linkWithCredential ( user , authCredential ) ) . to . be . rejectedWith (
120
+ FirebaseError ,
121
+ 'Firebase: User can only be linked to one identity for the given provider. (auth/provider-already-linked).'
122
+ ) ;
123
+ } ) ;
124
+
125
+ it ( 'should return a valid user credential' , async ( ) => {
126
+ const {
127
+ credential,
128
+ user : newUser ,
129
+ operationType
130
+ } = await linkWithCredential ( user , authCredential ) ;
131
+ expect ( operationType ) . to . eq ( OperationType . LINK ) ;
132
+ expect ( newUser ) . to . eq ( user ) ;
133
+ expect ( credential ) . to . be . null ;
134
+ } ) ;
88
135
} ) ;
89
136
90
- it ( 'should update the current user' , async ( ) => {
91
- const { user } = await signInWithCredential ( auth , authCredential ) ;
92
- expect ( auth . currentUser ) . to . eq ( user ) ;
137
+ describe ( '_assertLinkedStatus' , ( ) => {
138
+ it ( 'should error with already linked if expectation is true' , async ( ) => {
139
+ getAccountInfoEndpoint . response = {
140
+ users : [
141
+ {
142
+ ...serverUser ,
143
+ providerUserInfo : [ { providerId : ProviderId . GOOGLE } ]
144
+ }
145
+ ]
146
+ } ;
147
+
148
+ await expect (
149
+ _assertLinkedStatus ( false , user , ProviderId . GOOGLE )
150
+ ) . to . be . rejectedWith (
151
+ FirebaseError ,
152
+ 'Firebase: User can only be linked to one identity for the given provider. (auth/provider-already-linked).'
153
+ ) ;
154
+ } ) ;
155
+
156
+ it ( 'should not error if provider is not linked' , async ( ) => {
157
+ await expect ( _assertLinkedStatus ( false , user , ProviderId . GOOGLE ) ) . not . to
158
+ . be . rejected ;
159
+ } ) ;
160
+
161
+ it ( 'should error if provider is not linked but it was expected to be' , async ( ) => {
162
+ await expect (
163
+ _assertLinkedStatus ( true , user , ProviderId . GOOGLE )
164
+ ) . to . be . rejectedWith (
165
+ FirebaseError ,
166
+ 'Firebase: User was not linked to an account with the given provider. (auth/no-such-provider).'
167
+ ) ;
168
+ } ) ;
169
+
170
+ it ( 'should not error if provider is linked and that is expected' , async ( ) => {
171
+ getAccountInfoEndpoint . response = {
172
+ users : [
173
+ {
174
+ ...serverUser ,
175
+ providerUserInfo : [ { providerId : ProviderId . GOOGLE } ]
176
+ }
177
+ ]
178
+ } ;
179
+ await expect ( _assertLinkedStatus ( true , user , ProviderId . GOOGLE ) ) . not . to . be
180
+ . rejected ;
181
+ } ) ;
93
182
} ) ;
94
183
} ) ;
0 commit comments