-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth, emulator): native implementation + e2e tests
phone flow still not working e2e without an OOB code fetch implementation
- Loading branch information
Showing
8 changed files
with
136 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,38 @@ | |
* | ||
*/ | ||
|
||
const TEST_EMAIL = '[email protected]'; | ||
const TEST_PASS = 'test1234'; | ||
|
||
const DISABLED_EMAIL = '[email protected]'; | ||
const DISABLED_PASS = 'test1234'; | ||
|
||
describe('auth()', () => { | ||
before(async () => { | ||
try { | ||
await firebase.auth().createUserWithEmailAndPassword(TEST_EMAIL, TEST_PASS); | ||
} catch (e) { | ||
// they may already exist, that's fine | ||
} | ||
// Need a way to create the user but have it disabled immediately | ||
// If we had the functions emulator running as well, it would work: | ||
// https://stackoverflow.com/questions/43140441/how-to-disable-user-accounts-on-firebase-authentication-automatically#comment73363725_43141780 | ||
// try { | ||
// const userCredential = await firebase | ||
// .auth() | ||
// .createUserWithEmailAndPassword(DISABLED_EMAIL, DISABLED_PASS); | ||
// } catch (e) { | ||
// // they may already exist, that's fine | ||
// } | ||
}); | ||
|
||
beforeEach(async () => { | ||
if (firebase.auth().currentUser) { | ||
await firebase.auth().signOut(); | ||
await Utils.sleep(50); | ||
} | ||
}); | ||
|
||
describe('namespace', () => { | ||
it('accessible from firebase.app()', () => { | ||
const app = firebase.app(); | ||
|
@@ -39,6 +70,7 @@ describe('auth()', () => { | |
}); | ||
|
||
describe('applyActionCode()', () => { | ||
// Needs a different setup to work against the auth emulator | ||
xit('works as expected', async () => { | ||
await firebase | ||
.auth() | ||
|
@@ -127,16 +159,14 @@ describe('auth()', () => { | |
}); | ||
|
||
describe('signInWithCustomToken()', () => { | ||
it('signs in with a admin sdk created custom auth token', async () => { | ||
const email = '[email protected]'; | ||
const pass = 'test1234'; | ||
|
||
// Needs a different setup when running against the emulator | ||
xit('signs in with a admin sdk created custom auth token', async () => { | ||
const successCb = currentUserCredential => { | ||
const currentUser = currentUserCredential.user; | ||
currentUser.should.be.an.Object(); | ||
currentUser.uid.should.be.a.String(); | ||
currentUser.toJSON().should.be.an.Object(); | ||
currentUser.toJSON().email.should.eql(email); | ||
currentUser.toJSON().email.should.eql(TEST_EMAIL); | ||
currentUser.isAnonymous.should.equal(false); | ||
currentUser.providerId.should.equal('firebase'); | ||
currentUser.should.equal(firebase.auth().currentUser); | ||
|
@@ -150,7 +180,7 @@ describe('auth()', () => { | |
|
||
const user = await firebase | ||
.auth() | ||
.signInWithEmailAndPassword(email, pass) | ||
.signInWithEmailAndPassword(TEST_EMAIL, TEST_PASS) | ||
.then(successCb); | ||
|
||
const IdToken = await firebase.auth().currentUser.getIdToken(); | ||
|
@@ -162,7 +192,7 @@ describe('auth()', () => { | |
|
||
await firebase.auth().signInWithCustomToken(token); | ||
|
||
firebase.auth().currentUser.email.should.equal('[email protected]'); | ||
firebase.auth().currentUser.email.should.equal(TEST_EMAIL); | ||
}); | ||
}); | ||
|
||
|
@@ -540,15 +570,12 @@ describe('auth()', () => { | |
|
||
describe('signInWithEmailAndPassword()', () => { | ||
it('it should login with email and password', () => { | ||
const email = '[email protected]'; | ||
const pass = 'test1234'; | ||
|
||
const successCb = currentUserCredential => { | ||
const currentUser = currentUserCredential.user; | ||
currentUser.should.be.an.Object(); | ||
currentUser.uid.should.be.a.String(); | ||
currentUser.toJSON().should.be.an.Object(); | ||
currentUser.toJSON().email.should.eql(email); | ||
currentUser.toJSON().email.should.eql(TEST_EMAIL); | ||
currentUser.isAnonymous.should.equal(false); | ||
currentUser.providerId.should.equal('firebase'); | ||
currentUser.should.equal(firebase.auth().currentUser); | ||
|
@@ -562,14 +589,12 @@ describe('auth()', () => { | |
|
||
return firebase | ||
.auth() | ||
.signInWithEmailAndPassword(email, pass) | ||
.signInWithEmailAndPassword(TEST_EMAIL, TEST_PASS) | ||
.then(successCb); | ||
}); | ||
|
||
it('it should error on login if user is disabled', () => { | ||
const email = '[email protected]'; | ||
const pass = 'test1234'; | ||
|
||
// Need to set these up differently to run against emulator | ||
xit('it should error on login if user is disabled', () => { | ||
const successCb = () => Promise.reject(new Error('Did not error.')); | ||
|
||
const failureCb = error => { | ||
|
@@ -580,15 +605,12 @@ describe('auth()', () => { | |
|
||
return firebase | ||
.auth() | ||
.signInWithEmailAndPassword(email, pass) | ||
.signInWithEmailAndPassword(DISABLED_EMAIL, DISABLED_PASS) | ||
.then(successCb) | ||
.catch(failureCb); | ||
}); | ||
|
||
it('it should error on login if password incorrect', () => { | ||
const email = '[email protected]'; | ||
const pass = 'test1234666'; | ||
|
||
const successCb = () => Promise.reject(new Error('Did not error.')); | ||
|
||
const failureCb = error => { | ||
|
@@ -601,7 +623,7 @@ describe('auth()', () => { | |
|
||
return firebase | ||
.auth() | ||
.signInWithEmailAndPassword(email, pass) | ||
.signInWithEmailAndPassword(TEST_EMAIL, TEST_PASS + '666') | ||
.then(successCb) | ||
.catch(failureCb); | ||
}); | ||
|
@@ -631,14 +653,14 @@ describe('auth()', () => { | |
|
||
describe('signInWithCredential()', () => { | ||
it('it should login with email and password', () => { | ||
const credential = firebase.auth.EmailAuthProvider.credential('[email protected]', 'test1234'); | ||
const credential = firebase.auth.EmailAuthProvider.credential(TEST_EMAIL, TEST_PASS); | ||
|
||
const successCb = currentUserCredential => { | ||
const currentUser = currentUserCredential.user; | ||
currentUser.should.be.an.Object(); | ||
currentUser.uid.should.be.a.String(); | ||
currentUser.toJSON().should.be.an.Object(); | ||
currentUser.toJSON().email.should.eql('[email protected]'); | ||
currentUser.toJSON().email.should.eql(TEST_EMAIL); | ||
currentUser.isAnonymous.should.equal(false); | ||
currentUser.providerId.should.equal('firebase'); | ||
currentUser.should.equal(firebase.auth().currentUser); | ||
|
@@ -656,11 +678,9 @@ describe('auth()', () => { | |
.then(successCb); | ||
}); | ||
|
||
it('it should error on login if user is disabled', () => { | ||
const credential = firebase.auth.EmailAuthProvider.credential( | ||
'[email protected]', | ||
'test1234', | ||
); | ||
// Need to set these up differently to run against emulator | ||
xit('it should error on login if user is disabled', () => { | ||
const credential = firebase.auth.EmailAuthProvider.credential(DISABLED_EMAIL, DISABLED_PASS); | ||
|
||
const successCb = () => Promise.reject(new Error('Did not error.')); | ||
|
||
|
@@ -678,7 +698,7 @@ describe('auth()', () => { | |
}); | ||
|
||
it('it should error on login if password incorrect', () => { | ||
const credential = firebase.auth.EmailAuthProvider.credential('[email protected]', 'test1234666'); | ||
const credential = firebase.auth.EmailAuthProvider.credential(TEST_EMAIL, TEST_PASS + '666'); | ||
|
||
const successCb = () => Promise.reject(new Error('Did not error.')); | ||
|
||
|
@@ -770,9 +790,6 @@ describe('auth()', () => { | |
}); | ||
|
||
it('it should error on create if email in use', () => { | ||
const email = '[email protected]'; | ||
const pass = 'test123456789'; | ||
|
||
const successCb = () => Promise.reject(new Error('Did not error.')); | ||
|
||
const failureCb = error => { | ||
|
@@ -783,7 +800,7 @@ describe('auth()', () => { | |
|
||
return firebase | ||
.auth() | ||
.createUserWithEmailAndPassword(email, pass) | ||
.createUserWithEmailAndPassword(TEST_EMAIL, TEST_PASS) | ||
.then(successCb) | ||
.catch(failureCb); | ||
}); | ||
|
@@ -824,7 +841,7 @@ describe('auth()', () => { | |
|
||
return firebase | ||
.auth() | ||
.fetchSignInMethodsForEmail('[email protected]') | ||
.fetchSignInMethodsForEmail(TEST_EMAIL) | ||
.then(successCb) | ||
.catch(failureCb); | ||
})); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,32 @@ | ||
// const TEST_EMAIL = '[email protected]'; | ||
// const TEST_PASS = 'test1234'; | ||
|
||
const TEST_PHONE_A = '+447445255123'; | ||
const TEST_CODE_A = '123456'; | ||
|
||
// const TEST_PHONE_B = '+447445123457'; | ||
// const TEST_CODE_B = '654321'; | ||
|
||
describe('auth() => Phone', () => { | ||
before(async () => { | ||
// Make sure we have a user connected with a phone number | ||
// let userCredential = null; | ||
// try { | ||
// userCredential = await firebase.auth().createUserWithEmailAndPassword(TEST_EMAIL, TEST_PASS); | ||
// } catch (e) { | ||
// // they may already exist, that's fine | ||
// } | ||
// if (!userCredential) { | ||
// userCredential = await firebase.auth().signInWithEmailAndPassword(TEST_EMAIL, TEST_PASS); | ||
// } | ||
// const phoneSignIn = await firebase.auth().signInWithPhoneNumber(TEST_PHONE_A); | ||
|
||
// // https://firebase.google.com/docs/reference/rest/auth#section-auth-emulator-smsverification | ||
// const smsCodeFromREST = 'not-implemented'; | ||
// const credential = firebase.auth.PhoneAuthProvider.credential( | ||
// phoneSignIn.verificationId, | ||
// smsCodeFromREST, | ||
// ); | ||
// disabled until code fetching from emulator REST API is finished | ||
//firebase.auth().currentUser.updatePhoneNumber(credential); | ||
|
||
firebase.auth().settings.appVerificationDisabledForTesting = true; | ||
await firebase.auth().settings.setAutoRetrievedSmsCodeForPhoneNumber(TEST_PHONE_A, TEST_CODE_A); | ||
await Utils.sleep(50); | ||
|
@@ -18,8 +39,10 @@ describe('auth() => Phone', () => { | |
} | ||
}); | ||
|
||
describe('signInWithPhoneNumber', () => { | ||
xit('signs in with a valid code', async () => { | ||
// Needs different set up to run against emulator - you can get OOB codes via emulator REST API: | ||
// https://firebase.google.com/docs/reference/rest/auth#section-auth-emulator-smsverification | ||
xdescribe('signInWithPhoneNumber', () => { | ||
it('signs in with a valid code', async () => { | ||
const confirmResult = await firebase.auth().signInWithPhoneNumber(TEST_PHONE_A); | ||
confirmResult.verificationId.should.be.a.String(); | ||
should.ok(confirmResult.verificationId.length, 'verificationId string should not be empty'); | ||
|
@@ -41,25 +64,23 @@ describe('auth() => Phone', () => { | |
}); | ||
}); | ||
|
||
describe('verifyPhoneNumber', async () => { | ||
// Needs different set up to run against emulator - these all require code fetching | ||
xdescribe('verifyPhoneNumber', async () => { | ||
it('successfully verifies', async () => { | ||
const TEST_PHONE_A = '+447445255123'; | ||
const confirmResult = await firebase.auth().signInWithPhoneNumber(TEST_PHONE_A); | ||
|
||
await confirmResult.confirm(TEST_CODE_A); | ||
await firebase.auth().verifyPhoneNumber(TEST_PHONE_A, false, false); | ||
}); | ||
|
||
it('uses the autoVerifyTimeout when a non boolean autoVerifyTimeoutOrForceResend is provided', async () => { | ||
const TEST_PHONE_A = '+447445255123'; | ||
const confirmResult = await firebase.auth().signInWithPhoneNumber(TEST_PHONE_A); | ||
|
||
await confirmResult.confirm(TEST_CODE_A); | ||
await firebase.auth().verifyPhoneNumber(TEST_PHONE_A, 0, false); | ||
}); | ||
|
||
it('throws an error with an invalid on event', async () => { | ||
const TEST_PHONE_A = '+447445255123'; | ||
const confirmResult = await firebase.auth().signInWithPhoneNumber(TEST_PHONE_A); | ||
|
||
await confirmResult.confirm(TEST_CODE_A); | ||
|
@@ -80,7 +101,6 @@ describe('auth() => Phone', () => { | |
}); | ||
|
||
it('throws an error with an invalid observer event', async () => { | ||
const TEST_PHONE_A = '+447445255123'; | ||
const confirmResult = await firebase.auth().signInWithPhoneNumber(TEST_PHONE_A); | ||
|
||
await confirmResult.confirm(TEST_CODE_A); | ||
|
@@ -101,7 +121,6 @@ describe('auth() => Phone', () => { | |
}); | ||
|
||
it('successfully runs verification complete handler', async () => { | ||
const TEST_PHONE_A = '+447445255123'; | ||
const confirmResult = await firebase.auth().signInWithPhoneNumber(TEST_PHONE_A); | ||
|
||
await confirmResult.confirm(TEST_CODE_A); | ||
|
@@ -114,8 +133,7 @@ describe('auth() => Phone', () => { | |
return Promise.resolve(); | ||
}); | ||
|
||
it('successfully runs and adds emiters', async () => { | ||
const TEST_PHONE_A = '+447445255123'; | ||
it('successfully runs and adds emitters', async () => { | ||
const confirmResult = await firebase.auth().signInWithPhoneNumber(TEST_PHONE_A); | ||
|
||
await confirmResult.confirm(TEST_CODE_A); | ||
|
@@ -135,7 +153,6 @@ describe('auth() => Phone', () => { | |
}); | ||
|
||
it('catches an error and emits an error event', async () => { | ||
const TEST_PHONE_A = '+447445255123'; | ||
const confirmResult = await firebase.auth().signInWithPhoneNumber(TEST_PHONE_A); | ||
|
||
await confirmResult.confirm(TEST_CODE_A); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,15 @@ | ||
const TEST_EMAIL = '[email protected]'; | ||
const TEST_PASS = 'test1234'; | ||
|
||
describe('auth()', () => { | ||
before(async () => { | ||
try { | ||
await firebase.auth().createUserWithEmailAndPassword(TEST_EMAIL, TEST_PASS); | ||
} catch (e) { | ||
// they may already exist, that's fine | ||
} | ||
}); | ||
|
||
beforeEach(async () => { | ||
if (firebase.auth().currentUser) { | ||
await firebase.auth().signOut(); | ||
|
@@ -39,15 +50,13 @@ describe('auth()', () => { | |
// in with a different user then reloading | ||
await firebase.auth().signOut(); | ||
|
||
const email = '[email protected]'; | ||
const pass = 'test1234'; | ||
await firebase.auth().signInWithEmailAndPassword(email, pass); | ||
await firebase.auth().signInWithEmailAndPassword(TEST_EMAIL, TEST_PASS); | ||
|
||
({ currentUser } = firebase.auth()); | ||
currentUser.should.be.an.Object(); | ||
currentUser.uid.should.be.a.String(); | ||
currentUser.toJSON().should.be.an.Object(); | ||
currentUser.toJSON().email.should.eql(email); | ||
currentUser.toJSON().email.should.eql(TEST_EMAIL); | ||
currentUser.isAnonymous.should.equal(false); | ||
currentUser.providerId.should.equal('firebase'); | ||
currentUser.should.equal(firebase.auth().currentUser); | ||
|
@@ -60,7 +69,7 @@ describe('auth()', () => { | |
currentUser.should.be.an.Object(); | ||
currentUser.uid.should.be.a.String(); | ||
currentUser.toJSON().should.be.an.Object(); | ||
currentUser.toJSON().email.should.eql(email); | ||
currentUser.toJSON().email.should.eql(TEST_EMAIL); | ||
currentUser.isAnonymous.should.equal(false); | ||
currentUser.providerId.should.equal('firebase'); | ||
currentUser.should.equal(firebase.auth().currentUser); | ||
|
Oops, something went wrong.