2018-03-24 05:53:49 +00:00
|
|
|
describe('.auth()', () => {
|
|
|
|
describe('.signInAnonymously()', () => {
|
|
|
|
it('it should sign in anonymously', () => {
|
|
|
|
const successCb = currentUser => {
|
|
|
|
currentUser.should.be.an.Object();
|
|
|
|
currentUser.uid.should.be.a.String();
|
|
|
|
currentUser.toJSON().should.be.an.Object();
|
|
|
|
should.equal(currentUser.toJSON().email, null);
|
|
|
|
currentUser.isAnonymous.should.equal(true);
|
|
|
|
currentUser.providerId.should.equal('firebase');
|
2018-03-25 05:52:30 +00:00
|
|
|
currentUser.should.equal(firebase.auth().currentUser);
|
|
|
|
return firebase.auth().signOut();
|
2018-03-24 05:53:49 +00:00
|
|
|
};
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
return firebase
|
2018-03-24 05:53:49 +00:00
|
|
|
.auth()
|
|
|
|
.signInAnonymously()
|
|
|
|
.then(successCb);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('.signInAnonymouslyAndRetrieveData()', () => {
|
|
|
|
it('it should sign in anonymously', () => {
|
|
|
|
const successCb = currentUserCredential => {
|
|
|
|
const currentUser = currentUserCredential.user;
|
|
|
|
currentUser.should.be.an.Object();
|
|
|
|
currentUser.uid.should.be.a.String();
|
|
|
|
currentUser.toJSON().should.be.an.Object();
|
|
|
|
should.equal(currentUser.toJSON().email, null);
|
|
|
|
currentUser.isAnonymous.should.equal(true);
|
|
|
|
currentUser.providerId.should.equal('firebase');
|
2018-03-25 05:52:30 +00:00
|
|
|
currentUser.should.equal(firebase.auth().currentUser);
|
2018-03-24 05:53:49 +00:00
|
|
|
|
|
|
|
const { additionalUserInfo } = currentUserCredential;
|
|
|
|
additionalUserInfo.should.be.an.Object();
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
return firebase.auth().signOut();
|
2018-03-24 05:53:49 +00:00
|
|
|
};
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
return firebase
|
2018-03-24 05:53:49 +00:00
|
|
|
.auth()
|
|
|
|
.signInAnonymouslyAndRetrieveData()
|
|
|
|
.then(successCb);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('.signInWithEmailAndPassword()', () => {
|
|
|
|
it('it should login with email and password', () => {
|
|
|
|
const email = 'test@test.com';
|
|
|
|
const pass = 'test1234';
|
|
|
|
|
|
|
|
const successCb = currentUser => {
|
|
|
|
currentUser.should.be.an.Object();
|
|
|
|
currentUser.uid.should.be.a.String();
|
|
|
|
currentUser.toJSON().should.be.an.Object();
|
|
|
|
currentUser.toJSON().email.should.eql('test@test.com');
|
|
|
|
currentUser.isAnonymous.should.equal(false);
|
|
|
|
currentUser.providerId.should.equal('firebase');
|
2018-03-25 05:52:30 +00:00
|
|
|
currentUser.should.equal(firebase.auth().currentUser);
|
2018-03-24 05:53:49 +00:00
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
return firebase.auth().signOut();
|
2018-03-24 05:53:49 +00:00
|
|
|
};
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
return firebase
|
2018-03-24 05:53:49 +00:00
|
|
|
.auth()
|
|
|
|
.signInWithEmailAndPassword(email, pass)
|
|
|
|
.then(successCb);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('it should error on login if user is disabled', () => {
|
|
|
|
const email = 'disabled@account.com';
|
|
|
|
const pass = 'test1234';
|
|
|
|
|
|
|
|
const successCb = () => Promise.reject(new Error('Did not error.'));
|
|
|
|
|
|
|
|
const failureCb = error => {
|
|
|
|
error.code.should.equal('auth/user-disabled');
|
|
|
|
error.message.should.equal(
|
|
|
|
'The user account has been disabled by an administrator.'
|
|
|
|
);
|
|
|
|
return Promise.resolve();
|
|
|
|
};
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
return firebase
|
2018-03-24 05:53:49 +00:00
|
|
|
.auth()
|
|
|
|
.signInWithEmailAndPassword(email, pass)
|
|
|
|
.then(successCb)
|
|
|
|
.catch(failureCb);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('it should error on login if password incorrect', () => {
|
|
|
|
const email = 'test@test.com';
|
|
|
|
const pass = 'test1234666';
|
|
|
|
|
|
|
|
const successCb = () => Promise.reject(new Error('Did not error.'));
|
|
|
|
|
|
|
|
const failureCb = error => {
|
|
|
|
error.code.should.equal('auth/wrong-password');
|
|
|
|
error.message.should.equal(
|
|
|
|
'The password is invalid or the user does not have a password.'
|
|
|
|
);
|
|
|
|
return Promise.resolve();
|
|
|
|
};
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
return firebase
|
2018-03-24 05:53:49 +00:00
|
|
|
.auth()
|
|
|
|
.signInWithEmailAndPassword(email, pass)
|
|
|
|
.then(successCb)
|
|
|
|
.catch(failureCb);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('it should error on login if user not found', () => {
|
|
|
|
const email = 'randomSomeone@fourOhFour.com';
|
|
|
|
const pass = 'test1234';
|
|
|
|
|
|
|
|
const successCb = () => Promise.reject(new Error('Did not error.'));
|
|
|
|
|
|
|
|
const failureCb = error => {
|
|
|
|
error.code.should.equal('auth/user-not-found');
|
|
|
|
error.message.should.equal(
|
|
|
|
'There is no user record corresponding to this identifier. The user may have been deleted.'
|
|
|
|
);
|
|
|
|
return Promise.resolve();
|
|
|
|
};
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
return firebase
|
2018-03-24 05:53:49 +00:00
|
|
|
.auth()
|
|
|
|
.signInWithEmailAndPassword(email, pass)
|
|
|
|
.then(successCb)
|
|
|
|
.catch(failureCb);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('.onAuthStateChanged()', () => {
|
|
|
|
it('calls callback with the current user and when auth state changes', async () => {
|
2018-03-25 05:52:30 +00:00
|
|
|
await firebase.auth().signInAnonymouslyAndRetrieveData();
|
2018-03-24 05:53:49 +00:00
|
|
|
|
|
|
|
// Test
|
|
|
|
const callback = sinon.spy();
|
|
|
|
|
|
|
|
let unsubscribe;
|
|
|
|
await new Promise(resolve => {
|
2018-03-25 05:52:30 +00:00
|
|
|
unsubscribe = firebase.auth().onAuthStateChanged(user => {
|
2018-03-24 05:53:49 +00:00
|
|
|
callback(user);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
callback.should.be.calledWith(firebase.auth().currentUser);
|
2018-03-24 05:53:49 +00:00
|
|
|
callback.should.be.calledOnce();
|
|
|
|
|
|
|
|
// Sign out
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
await firebase.auth().signOut();
|
2018-03-24 05:53:49 +00:00
|
|
|
|
|
|
|
await new Promise(resolve => {
|
|
|
|
setTimeout(() => resolve(), 100);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Assertions
|
|
|
|
|
|
|
|
callback.should.be.calledWith(null);
|
|
|
|
callback.should.be.calledTwice();
|
|
|
|
|
|
|
|
// Tear down
|
|
|
|
|
|
|
|
unsubscribe();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('stops listening when unsubscribe called', async () => {
|
2018-03-25 05:52:30 +00:00
|
|
|
await firebase.auth().signInAnonymouslyAndRetrieveData();
|
2018-03-24 05:53:49 +00:00
|
|
|
|
|
|
|
// Test
|
|
|
|
const callback = sinon.spy();
|
|
|
|
|
|
|
|
let unsubscribe;
|
|
|
|
await new Promise(resolve => {
|
2018-03-25 05:52:30 +00:00
|
|
|
unsubscribe = firebase.auth().onAuthStateChanged(user => {
|
2018-03-24 05:53:49 +00:00
|
|
|
callback(user);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
callback.should.be.calledWith(firebase.auth().currentUser);
|
2018-03-24 05:53:49 +00:00
|
|
|
callback.should.be.calledOnce();
|
|
|
|
|
|
|
|
// Sign out
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
await firebase.auth().signOut();
|
2018-03-24 05:53:49 +00:00
|
|
|
|
|
|
|
await new Promise(resolve => {
|
|
|
|
setTimeout(() => resolve(), 100);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Assertions
|
|
|
|
|
|
|
|
// callback.should.be.calledWith(null);
|
|
|
|
callback.should.be.calledTwice();
|
|
|
|
|
|
|
|
// Unsubscribe
|
|
|
|
|
|
|
|
unsubscribe();
|
|
|
|
|
|
|
|
// Sign back in
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
await firebase.auth().signInAnonymouslyAndRetrieveData();
|
2018-03-24 05:53:49 +00:00
|
|
|
|
|
|
|
// Assertions
|
|
|
|
|
|
|
|
callback.should.be.calledTwice();
|
|
|
|
|
|
|
|
// Tear down
|
|
|
|
|
2018-03-25 05:52:30 +00:00
|
|
|
await firebase.auth().signOut();
|
2018-03-24 05:53:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|