1
+ import { expect , use } from 'chai' ;
2
+ import * as chaiAsPromised from 'chai-as-promised' ;
3
+
4
+ import {
5
+ createUserWithEmailAndPassword , EmailAuthProvider , linkWithCredential , signInAnonymously ,
6
+ signInWithEmailAndPassword , updateEmail , updatePassword
7
+ } from '@firebase/auth-exp/index.browser' ;
8
+ import { OperationType } from '@firebase/auth-types-exp' ;
9
+ import { FirebaseError } from '@firebase/util' ;
10
+
11
+ import { describeIntegration , randomEmail } from '../../helpers/integration/with_test_instance' ;
12
+
13
+ use ( chaiAsPromised ) ;
14
+
15
+ describeIntegration ( 'anonymous auth' , auth => {
16
+ it ( 'signs in anonymously' , async ( ) => {
17
+ const userCred = await signInAnonymously ( auth ) ;
18
+ expect ( auth . currentUser ) . to . eq ( userCred . user ) ;
19
+ expect ( userCred . operationType ) . to . eq ( OperationType . SIGN_IN ) ;
20
+
21
+ const user = userCred . user ;
22
+ expect ( user . isAnonymous ) . to . be . true ;
23
+ expect ( typeof user . uid ) . to . eq ( 'string' ) ;
24
+ } ) ;
25
+
26
+ it ( 'second sign in on the same device yields same user' , async ( ) => {
27
+ const { user : userA } = await signInAnonymously ( auth ) ;
28
+ const { user : userB } = await signInAnonymously ( auth ) ;
29
+
30
+ expect ( userA . uid ) . to . eq ( userB . uid ) ;
31
+ } ) ;
32
+
33
+ context ( 'email/password interaction' , ( ) => {
34
+ let email : string ;
35
+
36
+ beforeEach ( ( ) => {
37
+ email = randomEmail ( ) ;
38
+ } ) ;
39
+
40
+ it ( 'anonymous / email-password accounts remain independent' , async ( ) => {
41
+ let anonCred = await signInAnonymously ( auth ) ;
42
+ const emailCred = await createUserWithEmailAndPassword ( auth , email , 'password' ) ;
43
+ expect ( emailCred . user . uid ) . not . to . eql ( anonCred . user . uid ) ;
44
+
45
+ await auth . signOut ( ) ;
46
+ anonCred = await signInAnonymously ( auth ) ;
47
+ const emailSignIn = await signInWithEmailAndPassword ( auth , email , 'password' ) ;
48
+ expect ( emailCred . user . uid ) . to . eql ( emailSignIn . user . uid ) ;
49
+ expect ( emailSignIn . user . uid ) . not . to . eql ( anonCred . user . uid ) ;
50
+ } ) ;
51
+
52
+ it ( 'account can be upgraded by setting email and password' , async ( ) => {
53
+ const { user} = await signInAnonymously ( auth ) ;
54
+ await updateEmail ( user , email ) ;
55
+ await updatePassword ( user , 'password' ) ;
56
+
57
+ const anonId = user . uid ;
58
+ await auth . signOut ( ) ;
59
+ expect ( ( await signInWithEmailAndPassword ( auth , email , 'password' ) ) . user . uid ) . to . eq ( anonId ) ;
60
+ } ) ;
61
+
62
+ it ( 'account can be linked using email and password' , async ( ) => {
63
+ const { user} = await signInAnonymously ( auth ) ;
64
+ const cred = EmailAuthProvider . credential ( email , 'password' ) ;
65
+ const id = user . uid ;
66
+ await linkWithCredential ( user , cred ) ;
67
+ await auth . signOut ( ) ;
68
+
69
+ expect ( ( await signInWithEmailAndPassword ( auth , email , 'password' ) ) . user . uid ) . to . eq ( id ) ;
70
+ } ) ;
71
+
72
+ it ( 'account cannot be linked with existing email/password' , async ( ) => {
73
+ await createUserWithEmailAndPassword ( auth , email , 'password' ) ;
74
+ const { user : anonUser } = await signInAnonymously ( auth ) ;
75
+ const cred = EmailAuthProvider . credential ( email , 'password' ) ;
76
+ await expect ( linkWithCredential ( anonUser , cred ) ) . to . be . rejectedWith ( FirebaseError , 'auth/email-already-in-use' ) ;
77
+ } ) ;
78
+ } ) ;
79
+ } ) ;
0 commit comments