Skip to content

Commit 3272975

Browse files
authored
Merge 6ff6c69 into d0f23f5
2 parents d0f23f5 + 6ff6c69 commit 3272975

File tree

2 files changed

+126
-33
lines changed

2 files changed

+126
-33
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 {
22+
createUserWithEmailAndPassword,
23+
EmailAuthProvider,
24+
linkWithCredential,
25+
signInAnonymously,
26+
signInWithEmailAndPassword,
27+
updateEmail,
28+
updatePassword
29+
} from '@firebase/auth-exp/index.browser';
30+
import { OperationType } from '@firebase/auth-types-exp';
31+
import { FirebaseError } from '@firebase/util';
32+
33+
import {
34+
initIntegrationTestContext,
35+
randomEmail
36+
} from '../../helpers/integration/helpers';
37+
38+
use(chaiAsPromised);
39+
40+
describe('Integration test: anonymous auth', () => {
41+
const auth = initIntegrationTestContext();
42+
43+
it('signs in anonymously', async () => {
44+
const userCred = await signInAnonymously(auth);
45+
expect(auth.currentUser).to.eq(userCred.user);
46+
expect(userCred.operationType).to.eq(OperationType.SIGN_IN);
47+
48+
const user = userCred.user;
49+
expect(user.isAnonymous).to.be.true;
50+
expect(user.uid).to.be.a('string');
51+
});
52+
53+
it('second sign in on the same device yields same user', async () => {
54+
const { user: userA } = await signInAnonymously(auth);
55+
const { user: userB } = await signInAnonymously(auth);
56+
57+
expect(userA.uid).to.eq(userB.uid);
58+
});
59+
60+
context('email/password interaction', () => {
61+
let email: string;
62+
63+
beforeEach(() => {
64+
email = randomEmail();
65+
});
66+
67+
it('anonymous / email-password accounts remain independent', async () => {
68+
let anonCred = await signInAnonymously(auth);
69+
const emailCred = await createUserWithEmailAndPassword(
70+
auth,
71+
email,
72+
'password'
73+
);
74+
expect(emailCred.user.uid).not.to.eql(anonCred.user.uid);
75+
76+
await auth.signOut();
77+
anonCred = await signInAnonymously(auth);
78+
const emailSignIn = await signInWithEmailAndPassword(
79+
auth,
80+
email,
81+
'password'
82+
);
83+
expect(emailCred.user.uid).to.eql(emailSignIn.user.uid);
84+
expect(emailSignIn.user.uid).not.to.eql(anonCred.user.uid);
85+
});
86+
87+
it('account can be upgraded by setting email and password', async () => {
88+
const { user: anonUser } = await signInAnonymously(auth);
89+
await updateEmail(anonUser, email);
90+
await updatePassword(anonUser, 'password');
91+
92+
await auth.signOut();
93+
94+
const { user: emailPassUser } = await signInWithEmailAndPassword(
95+
auth,
96+
email,
97+
'password'
98+
);
99+
expect(emailPassUser.uid).to.eq(anonUser.uid);
100+
});
101+
102+
it('account can be linked using email and password', async () => {
103+
const { user: anonUser } = await signInAnonymously(auth);
104+
const cred = EmailAuthProvider.credential(email, 'password');
105+
await linkWithCredential(anonUser, cred);
106+
await auth.signOut();
107+
108+
const { user: emailPassUser } = await signInWithEmailAndPassword(
109+
auth,
110+
email,
111+
'password'
112+
);
113+
expect(emailPassUser.uid).to.eq(anonUser.uid);
114+
});
115+
116+
it('account cannot be linked with existing email/password', async () => {
117+
await createUserWithEmailAndPassword(auth, email, 'password');
118+
const { user: anonUser } = await signInAnonymously(auth);
119+
const cred = EmailAuthProvider.credential(email, 'password');
120+
await expect(linkWithCredential(anonUser, cred)).to.be.rejectedWith(
121+
FirebaseError,
122+
'auth/email-already-in-use'
123+
);
124+
});
125+
});
126+
});

packages-exp/auth-exp/test/integration/smoke_test.test.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)