Skip to content

Commit 713363d

Browse files
reorganized mfa totp integration tests (#7352)
* reorganized mfa totp integration tests * formatted the code * added await for enroll function calls * created fakePassword to store password * fixed typo in mfaUser * moved fakePassword into integration helpers file
1 parent f949ee9 commit 713363d

File tree

2 files changed

+71
-20
lines changed

2 files changed

+71
-20
lines changed

packages/auth/test/helpers/integration/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,6 @@ export function getTotpCode(
113113
return token;
114114
}
115115
export const email = 'totpuser-donotdelete@test.com';
116+
export const fakePassword = 'password';
116117
//1000000 is always incorrect since it has 7 digits and we expect 6.
117118
export const incorrectTotpCode = '1000000';

packages/auth/test/integration/flows/totp.test.ts

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import sinonChai from 'sinon-chai';
2121
import {
2222
Auth,
2323
multiFactor,
24+
MultiFactorUser,
2425
signInWithEmailAndPassword,
2526
getMultiFactorResolver
2627
} from '@firebase/auth';
@@ -30,6 +31,7 @@ import {
3031
getTestInstance,
3132
getTotpCode,
3233
email,
34+
fakePassword,
3335
incorrectTotpCode
3436
} from '../../helpers/integration/helpers';
3537

@@ -42,22 +44,30 @@ import { getEmulatorUrl } from '../../helpers/integration/settings';
4244
use(chaiAsPromised);
4345
use(sinonChai);
4446

45-
describe(' Integration tests: Mfa TOTP', () => {
46-
let auth: Auth;
47-
let totpSecret: TotpSecret;
48-
let displayName: string;
49-
let totpTimestamp: Date;
50-
let emulatorUrl: string | null;
47+
let auth: Auth;
48+
let totpSecret: TotpSecret;
49+
let displayName: string;
50+
let totpTimestamp: Date;
51+
let emulatorUrl: string | null;
52+
let mfaUser: MultiFactorUser | null;
53+
54+
describe(' Integration tests: Mfa enrollement using totp', () => {
5155
beforeEach(async () => {
5256
emulatorUrl = getEmulatorUrl();
5357
if (!emulatorUrl) {
58+
mfaUser = null;
5459
auth = getTestInstance();
5560
displayName = 'totp-integration-test';
5661
}
5762
});
5863

5964
afterEach(async () => {
6065
if (!emulatorUrl) {
66+
if (mfaUser && mfaUser.enrolledFactors.length > 0) {
67+
for (let i = 0; i < mfaUser.enrolledFactors.length; i++) {
68+
await mfaUser.unenroll(mfaUser.enrolledFactors[i]);
69+
}
70+
}
6171
await cleanUpTestInstance(auth);
6272
}
6373
});
@@ -66,10 +76,12 @@ describe(' Integration tests: Mfa TOTP', () => {
6676
if (emulatorUrl) {
6777
this.skip();
6878
}
69-
const cr = await signInWithEmailAndPassword(auth, email, 'password');
70-
const mfaUser = multiFactor(cr.user);
79+
80+
const cr = await signInWithEmailAndPassword(auth, email, fakePassword);
81+
mfaUser = multiFactor(cr.user);
7182
const session = await mfaUser.getSession();
7283
totpSecret = await TotpMultiFactorGenerator.generateSecret(session);
84+
7385
const multiFactorAssertion =
7486
TotpMultiFactorGenerator.assertionForEnrollment(
7587
totpSecret,
@@ -85,16 +97,12 @@ describe(' Integration tests: Mfa TOTP', () => {
8597
if (emulatorUrl) {
8698
this.skip();
8799
}
88-
const cr = await signInWithEmailAndPassword(auth, email, 'password');
89-
90-
const mfaUser = multiFactor(cr.user);
91100

101+
const cr = await signInWithEmailAndPassword(auth, email, fakePassword);
102+
mfaUser = multiFactor(cr.user);
92103
const session = await mfaUser.getSession();
93-
94104
totpSecret = await TotpMultiFactorGenerator.generateSecret(session);
95-
96105
totpTimestamp = new Date();
97-
98106
const totpVerificationCode = getTotpCode(
99107
totpSecret.secretKey,
100108
totpSecret.codeIntervalSeconds,
@@ -107,18 +115,61 @@ describe(' Integration tests: Mfa TOTP', () => {
107115
totpSecret,
108116
totpVerificationCode
109117
);
118+
110119
await expect(mfaUser.enroll(multiFactorAssertion, displayName)).to.be
111120
.fulfilled;
112121
});
122+
});
123+
124+
describe('Integration tests: sign-in for mfa-enrolled users', () => {
125+
beforeEach(async () => {
126+
emulatorUrl = getEmulatorUrl();
127+
mfaUser = null;
128+
129+
if (!emulatorUrl) {
130+
auth = getTestInstance();
131+
displayName = 'totp-integration-test';
132+
133+
const cr = await signInWithEmailAndPassword(auth, email, fakePassword);
134+
mfaUser = multiFactor(cr.user);
135+
const session = await mfaUser.getSession();
136+
totpSecret = await TotpMultiFactorGenerator.generateSecret(session);
137+
totpTimestamp = new Date();
138+
const totpVerificationCode = getTotpCode(
139+
totpSecret.secretKey,
140+
totpSecret.codeIntervalSeconds,
141+
totpSecret.codeLength,
142+
totpTimestamp
143+
);
144+
145+
const multiFactorAssertion =
146+
TotpMultiFactorGenerator.assertionForEnrollment(
147+
totpSecret,
148+
totpVerificationCode
149+
);
150+
151+
await mfaUser.enroll(multiFactorAssertion, displayName);
152+
}
153+
});
154+
155+
afterEach(async () => {
156+
if (!emulatorUrl) {
157+
if (mfaUser && mfaUser.enrolledFactors.length > 0) {
158+
for (let i = 0; i < mfaUser.enrolledFactors.length; i++) {
159+
await mfaUser.unenroll(mfaUser.enrolledFactors[i]);
160+
}
161+
}
162+
await cleanUpTestInstance(auth);
163+
}
164+
});
113165

114166
it('should not allow sign-in with incorrect totp', async function () {
115167
let resolver: any;
116-
117168
if (emulatorUrl) {
118169
this.skip();
119170
}
120171
try {
121-
await signInWithEmailAndPassword(auth, email, 'password');
172+
await signInWithEmailAndPassword(auth, email, fakePassword);
122173

123174
throw new Error('Signin should not have been successful');
124175
} catch (error) {
@@ -145,7 +196,7 @@ describe(' Integration tests: Mfa TOTP', () => {
145196
this.skip();
146197
}
147198
try {
148-
await signInWithEmailAndPassword(auth, email, 'password');
199+
await signInWithEmailAndPassword(auth, email, fakePassword);
149200

150201
throw new Error('Signin should not have been successful');
151202
} catch (error) {
@@ -169,11 +220,10 @@ describe(' Integration tests: Mfa TOTP', () => {
169220
totpVerificationCode
170221
);
171222
const userCredential = await resolver.resolveSignIn(assertion);
172-
173-
const mfaUser = multiFactor(userCredential.user);
223+
mfaUser = multiFactor(userCredential.user);
174224

175225
await expect(mfaUser.unenroll(resolver.hints[0].uid)).to.be.fulfilled;
176-
await expect(signInWithEmailAndPassword(auth, email, 'password')).to.be
226+
await expect(signInWithEmailAndPassword(auth, email, fakePassword)).to.be
177227
.fulfilled;
178228
}
179229
});

0 commit comments

Comments
 (0)