Skip to content

Commit 0acc039

Browse files
authored
Add the rest of the existing integration tests but modified to use the compatibility layer (#4644)
* Custom token tests * Add email tests * Add headless IdP tests * Add oob tests * Add phone auth tests * Remove unnecessary comment * Formatting
1 parent 4d7d2c4 commit 0acc039

File tree

5 files changed

+1235
-0
lines changed

5 files changed

+1235
-0
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { FirebaseError } from '@firebase/util';
19+
import { expect, use } from 'chai';
20+
import firebase from '@firebase/app-compat';
21+
import '@firebase/auth-compat';
22+
23+
import * as chaiAsPromised from 'chai-as-promised';
24+
import {
25+
cleanUpTestInstance,
26+
initializeTestInstance,
27+
randomEmail
28+
} from '../../helpers/helpers';
29+
30+
use(chaiAsPromised);
31+
32+
describe('Integration test: custom auth', () => {
33+
let customToken: string;
34+
let uid: string;
35+
36+
beforeEach(() => {
37+
initializeTestInstance();
38+
uid = randomEmail();
39+
customToken = JSON.stringify({
40+
uid,
41+
claims: {
42+
customClaim: 'some-claim'
43+
}
44+
});
45+
});
46+
47+
afterEach(async () => {
48+
await cleanUpTestInstance();
49+
});
50+
51+
it('signs in with custom token', async () => {
52+
const cred = await firebase.auth().signInWithCustomToken(customToken);
53+
expect(firebase.auth().currentUser).to.eq(cred.user);
54+
expect(cred.operationType).to.eq('signIn');
55+
56+
const { user } = cred;
57+
expect(user!.isAnonymous).to.be.false;
58+
expect(user!.uid).to.eq(uid);
59+
expect((await user!.getIdTokenResult(false)).claims.customClaim).to.eq(
60+
'some-claim'
61+
);
62+
expect(user!.providerId).to.eq('firebase');
63+
expect(cred.additionalUserInfo!.providerId).to.be.null;
64+
expect(cred.additionalUserInfo!.isNewUser).to.be.true;
65+
});
66+
67+
it('uid will overwrite existing user, joining accounts', async () => {
68+
const { user: anonUser } = await firebase.auth().signInAnonymously();
69+
const customCred = await firebase.auth().signInWithCustomToken(
70+
JSON.stringify({
71+
uid: anonUser!.uid
72+
})
73+
);
74+
75+
expect(firebase.auth().currentUser).to.eq(customCred.user);
76+
expect(customCred.user!.uid).to.eq(anonUser!.uid);
77+
expect(customCred.user!.isAnonymous).to.be.false;
78+
});
79+
80+
it('allows the user to delete the account', async () => {
81+
let { user } = await firebase.auth().signInWithCustomToken(customToken);
82+
await user!.updateProfile({ displayName: 'Display Name' });
83+
expect(user!.displayName).to.eq('Display Name');
84+
85+
await user!.delete();
86+
await expect(user!.reload()).to.be.rejectedWith(
87+
FirebaseError,
88+
'auth/user-token-expired'
89+
);
90+
expect(firebase.auth().currentUser).to.be.null;
91+
92+
({ user } = await firebase.auth().signInWithCustomToken(customToken));
93+
// New user in the system: the display name should be missing
94+
expect(user!.displayName).to.be.null;
95+
});
96+
97+
it('sign in can be called twice successively', async () => {
98+
const { user: userA } = await firebase
99+
.auth()
100+
.signInWithCustomToken(customToken);
101+
const { user: userB } = await firebase
102+
.auth()
103+
.signInWithCustomToken(customToken);
104+
expect(userA!.uid).to.eq(userB!.uid);
105+
});
106+
107+
it('allows user to update profile', async () => {
108+
let { user } = await firebase.auth().signInWithCustomToken(customToken);
109+
await user!.updateProfile({
110+
displayName: 'Display Name',
111+
photoURL: 'photo-url'
112+
});
113+
expect(user!.displayName).to.eq('Display Name');
114+
expect(user!.photoURL).to.eq('photo-url');
115+
116+
await firebase.auth().signOut();
117+
118+
user = (await firebase.auth().signInWithCustomToken(customToken)).user!;
119+
expect(user.displayName).to.eq('Display Name');
120+
expect(user.photoURL).to.eq('photo-url');
121+
});
122+
123+
it('token can be refreshed', async () => {
124+
const { user } = await firebase.auth().signInWithCustomToken(customToken);
125+
const origToken = await user!.getIdToken();
126+
await new Promise(resolve => setTimeout(resolve, 1000));
127+
expect(await user!.getIdToken(true)).not.to.eq(origToken);
128+
});
129+
130+
it('signing in will not override anonymous user', async () => {
131+
const { user: anonUser } = await firebase.auth().signInAnonymously();
132+
const { user: customUser } = await firebase
133+
.auth()
134+
.signInWithCustomToken(customToken);
135+
expect(firebase.auth().currentUser).to.eql(customUser);
136+
expect(customUser!.uid).not.to.eql(anonUser!.uid);
137+
});
138+
139+
context('email/password interaction', () => {
140+
let email: string;
141+
let customToken: string;
142+
143+
beforeEach(() => {
144+
email = randomEmail();
145+
customToken = JSON.stringify({
146+
uid: email
147+
});
148+
});
149+
150+
it('custom / email-password accounts remain independent', async () => {
151+
let customCred = await firebase.auth().signInWithCustomToken(customToken);
152+
const emailCred = await firebase
153+
.auth()
154+
.createUserWithEmailAndPassword(email, 'password');
155+
expect(emailCred.user!.uid).not.to.eql(customCred.user!.uid);
156+
157+
await firebase.auth().signOut();
158+
customCred = await firebase.auth().signInWithCustomToken(customToken);
159+
const emailSignIn = await firebase
160+
.auth()
161+
.signInWithEmailAndPassword(email, 'password');
162+
expect(emailCred.user!.uid).to.eql(emailSignIn.user!.uid);
163+
expect(emailSignIn.user!.uid).not.to.eql(customCred.user!.uid);
164+
});
165+
166+
it('account can have email / password attached', async () => {
167+
const { user: customUser } = await firebase
168+
.auth()
169+
.signInWithCustomToken(customToken);
170+
await customUser!.updateEmail(email);
171+
await customUser!.updatePassword('password');
172+
173+
await firebase.auth().signOut();
174+
175+
const {
176+
user: emailPassUser
177+
} = await firebase.auth().signInWithEmailAndPassword(email, 'password');
178+
expect(emailPassUser!.uid).to.eq(customUser!.uid);
179+
});
180+
181+
it('account can be linked using email and password', async () => {
182+
const { user: customUser } = await firebase
183+
.auth()
184+
.signInWithCustomToken(customToken);
185+
const cred = firebase.auth.EmailAuthProvider.credential(
186+
email,
187+
'password'
188+
);
189+
await customUser!.linkWithCredential(cred);
190+
await firebase.auth().signOut();
191+
192+
const {
193+
user: emailPassUser
194+
} = await firebase.auth().signInWithEmailAndPassword(email, 'password');
195+
expect(emailPassUser!.uid).to.eq(customUser!.uid);
196+
});
197+
198+
it('account cannot be linked with existing email/password', async () => {
199+
await firebase.auth().createUserWithEmailAndPassword(email, 'password');
200+
const { user: customUser } = await firebase
201+
.auth()
202+
.signInWithCustomToken(customToken);
203+
const cred = firebase.auth.EmailAuthProvider.credential(
204+
email,
205+
'password'
206+
);
207+
await expect(customUser!.linkWithCredential(cred)).to.be.rejectedWith(
208+
FirebaseError,
209+
'auth/email-already-in-use'
210+
);
211+
});
212+
});
213+
});
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { expect, use } from 'chai';
19+
import * as chaiAsPromised from 'chai-as-promised';
20+
21+
import { FirebaseError } from '@firebase/util';
22+
import firebase from '@firebase/app-compat';
23+
import {
24+
cleanUpTestInstance,
25+
initializeTestInstance,
26+
randomEmail
27+
} from '../../helpers/helpers';
28+
import { UserCredential } from '@firebase/auth-types';
29+
30+
use(chaiAsPromised);
31+
32+
describe('Integration test: email/password auth', () => {
33+
let email: string;
34+
beforeEach(() => {
35+
initializeTestInstance();
36+
email = randomEmail();
37+
});
38+
39+
afterEach(() => cleanUpTestInstance());
40+
41+
it('allows user to sign up', async () => {
42+
const userCred = await firebase
43+
.auth()
44+
.createUserWithEmailAndPassword(email, 'password');
45+
expect(firebase.auth().currentUser).to.eq(userCred.user);
46+
expect(userCred.operationType).to.eq('signIn');
47+
48+
const user = userCred.user!;
49+
expect(user.isAnonymous).to.be.false;
50+
expect(user.uid).to.be.a('string');
51+
expect(user.email).to.eq(email);
52+
expect(user.emailVerified).to.be.false;
53+
expect(user.providerData.length).to.eq(1);
54+
expect(user.providerData[0]!.providerId).to.eq('password');
55+
expect(user.providerData[0]!.email).to.eq(email);
56+
57+
const additionalUserInfo = userCred.additionalUserInfo!;
58+
expect(additionalUserInfo.isNewUser).to.be.true;
59+
expect(additionalUserInfo.providerId).to.eq('password');
60+
});
61+
62+
it('errors when createUser called twice', async () => {
63+
await firebase.auth().createUserWithEmailAndPassword(email, 'password');
64+
await expect(
65+
firebase.auth().createUserWithEmailAndPassword(email, 'password')
66+
).to.be.rejectedWith(FirebaseError, 'auth/email-already-in-use');
67+
});
68+
69+
context('with existing user', () => {
70+
let signUpCred: UserCredential;
71+
72+
beforeEach(async () => {
73+
signUpCred = await firebase
74+
.auth()
75+
.createUserWithEmailAndPassword(email, 'password');
76+
await firebase.auth().signOut();
77+
});
78+
79+
it('allows the user to sign in with signInWithEmailAndPassword', async () => {
80+
const signInCred = await firebase
81+
.auth()
82+
.signInWithEmailAndPassword(email, 'password');
83+
expect(firebase.auth().currentUser).to.eq(signInCred.user);
84+
85+
expect(signInCred.operationType).to.eq('signIn');
86+
expect(signInCred.user!.uid).to.eq(signUpCred.user!.uid);
87+
const additionalUserInfo = signInCred.additionalUserInfo!;
88+
expect(additionalUserInfo.isNewUser).to.be.false;
89+
expect(additionalUserInfo.providerId).to.eq('password');
90+
});
91+
92+
it('allows the user to sign in with signInWithCredential', async () => {
93+
const credential = firebase.auth.EmailAuthProvider.credential(
94+
email,
95+
'password'
96+
);
97+
const signInCred = await firebase.auth().signInWithCredential(credential);
98+
expect(firebase.auth().currentUser).to.eq(signInCred.user);
99+
100+
expect(signInCred.operationType).to.eq('signIn');
101+
expect(signInCred.user!.uid).to.eq(signUpCred.user!.uid);
102+
const additionalUserInfo = signInCred.additionalUserInfo!;
103+
expect(additionalUserInfo.isNewUser).to.be.false;
104+
expect(additionalUserInfo.providerId).to.eq('password');
105+
});
106+
107+
it('allows the user to update profile', async () => {
108+
let { user } = await firebase
109+
.auth()
110+
.signInWithEmailAndPassword(email, 'password');
111+
await user!.updateProfile({
112+
displayName: 'Display Name',
113+
photoURL: 'photo-url'
114+
});
115+
expect(user!.displayName).to.eq('Display Name');
116+
expect(user!.photoURL).to.eq('photo-url');
117+
118+
await firebase.auth().signOut();
119+
120+
user = (
121+
await firebase.auth().signInWithEmailAndPassword(email, 'password')
122+
).user;
123+
expect(user!.displayName).to.eq('Display Name');
124+
expect(user!.photoURL).to.eq('photo-url');
125+
});
126+
127+
it('allows the user to delete the account', async () => {
128+
const { user } = await firebase
129+
.auth()
130+
.signInWithEmailAndPassword(email, 'password');
131+
await user!.delete();
132+
133+
await expect(user!.reload()).to.be.rejectedWith(
134+
FirebaseError,
135+
'auth/user-token-expired'
136+
);
137+
138+
expect(firebase.auth().currentUser).to.be.null;
139+
await expect(
140+
firebase.auth().signInWithEmailAndPassword(email, 'password')
141+
).to.be.rejectedWith(FirebaseError, 'auth/user-not-found');
142+
});
143+
144+
it('sign in can be called twice successively', async () => {
145+
const { user: userA } = await firebase
146+
.auth()
147+
.signInWithEmailAndPassword(email, 'password');
148+
const { user: userB } = await firebase
149+
.auth()
150+
.signInWithEmailAndPassword(email, 'password');
151+
expect(userA!.uid).to.eq(userB!.uid);
152+
});
153+
});
154+
});

0 commit comments

Comments
 (0)