Skip to content

Commit

Permalink
feat(auth, emulator): native implementation + e2e tests
Browse files Browse the repository at this point in the history
phone flow still not working e2e without an OOB code fetch implementation
  • Loading branch information
mikehardy committed Nov 15, 2020
1 parent eec98ed commit 4713832
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,16 @@ public void verifyPasswordResetCode(String appName, String code, final Promise p
});
}

@ReactMethod
public void useEmulator(String appName, String host, int port) {
Log.d(TAG, "useEmulator");

FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);

firebaseAuth.useEmulator(host, port);
}

/* ------------------
* INTERNAL HELPERS
* ---------------- */
Expand Down
85 changes: 51 additions & 34 deletions packages/auth/e2e/auth.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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);
});
});

Expand Down Expand Up @@ -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);
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -601,7 +623,7 @@ describe('auth()', () => {

return firebase
.auth()
.signInWithEmailAndPassword(email, pass)
.signInWithEmailAndPassword(TEST_EMAIL, TEST_PASS + '666')
.then(successCb)
.catch(failureCb);
});
Expand Down Expand Up @@ -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);
Expand All @@ -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.'));

Expand All @@ -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.'));

Expand Down Expand Up @@ -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 => {
Expand All @@ -783,7 +800,7 @@ describe('auth()', () => {

return firebase
.auth()
.createUserWithEmailAndPassword(email, pass)
.createUserWithEmailAndPassword(TEST_EMAIL, TEST_PASS)
.then(successCb)
.catch(failureCb);
});
Expand Down Expand Up @@ -824,7 +841,7 @@ describe('auth()', () => {

return firebase
.auth()
.fetchSignInMethodsForEmail('[email protected]')
.fetchSignInMethodsForEmail(TEST_EMAIL)
.then(successCb)
.catch(failureCb);
}));
Expand Down
45 changes: 31 additions & 14 deletions packages/auth/e2e/phone.e2e.js
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);
Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
19 changes: 14 additions & 5 deletions packages/auth/e2e/rnReload.e2e.js
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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 4713832

Please sign in to comment.