Skip to content

Commit 9ad8b23

Browse files
committed
Totp integration test fix (#6814)
* removed delay function and used timestamp for totp generator * updated yarn.lock to show totp-generator * added check for signin after unenroll * adding skippig of totp tests if emulator is being used * using this.skip to skip tests
1 parent cee33b9 commit 9ad8b23

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,18 @@ function stubConsoleToSilenceEmulatorWarnings(): sinon.SinonStub {
9999
export function getTotpCode(
100100
sharedSecretKey: string,
101101
periodSec: number,
102-
verificationCodeLength: number
102+
verificationCodeLength: number,
103+
timestamp: Date
103104
): string {
104105
const token = totp(sharedSecretKey, {
105106
period: periodSec,
106107
digits: verificationCodeLength,
107-
algorithm: 'SHA-1'
108+
algorithm: 'SHA-1',
109+
timestamp
108110
});
109111

110112
return token;
111113
}
112-
113-
export function delay(dt: number): Promise<void> {
114-
return new Promise(resolve => setTimeout(resolve, dt));
115-
}
116-
117114
export const email = '[email protected]';
118115
//1000000 is always incorrect since it has 7 digits and we expect 6.
119116
export const incorrectTotpCode = '1000000';

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

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
cleanUpTestInstance,
3030
getTestInstance,
3131
getTotpCode,
32-
delay,
3332
email,
3433
incorrectTotpCode
3534
} from '../../helpers/integration/helpers';
@@ -38,6 +37,7 @@ import {
3837
TotpMultiFactorGenerator,
3938
TotpSecret
4039
} from '../../../src/mfa/assertions/totp';
40+
import { getEmulatorUrl } from '../../helpers/integration/settings';
4141

4242
use(chaiAsPromised);
4343
use(sinonChai);
@@ -46,16 +46,26 @@ describe(' Integration tests: Mfa TOTP', () => {
4646
let auth: Auth;
4747
let totpSecret: TotpSecret;
4848
let displayName: string;
49+
let totpTimestamp: Date;
50+
let emulatorUrl: string | null;
4951
beforeEach(async () => {
50-
auth = getTestInstance();
51-
displayName = 'totp-integration-test';
52+
emulatorUrl = getEmulatorUrl();
53+
if (!emulatorUrl) {
54+
auth = getTestInstance();
55+
displayName = 'totp-integration-test';
56+
}
5257
});
5358

5459
afterEach(async () => {
55-
await cleanUpTestInstance(auth);
60+
if (!emulatorUrl) {
61+
await cleanUpTestInstance(auth);
62+
}
5663
});
5764

58-
it('should not enroll if incorrect totp supplied', async () => {
65+
it('should not enroll if incorrect totp supplied', async function () {
66+
if (emulatorUrl) {
67+
this.skip();
68+
}
5969
const cr = await signInWithEmailAndPassword(auth, email, 'password');
6070
const mfaUser = multiFactor(cr.user);
6171
const session = await mfaUser.getSession();
@@ -71,7 +81,10 @@ describe(' Integration tests: Mfa TOTP', () => {
7181
).to.be.rejectedWith('auth/invalid-verification-code');
7282
});
7383

74-
it('should enroll using correct otp', async () => {
84+
it('should enroll using correct otp', async function () {
85+
if (emulatorUrl) {
86+
this.skip();
87+
}
7588
const cr = await signInWithEmailAndPassword(auth, email, 'password');
7689

7790
const mfaUser = multiFactor(cr.user);
@@ -80,10 +93,13 @@ describe(' Integration tests: Mfa TOTP', () => {
8093

8194
totpSecret = await TotpMultiFactorGenerator.generateSecret(session);
8295

96+
totpTimestamp = new Date();
97+
8398
const totpVerificationCode = getTotpCode(
8499
totpSecret.secretKey,
85100
totpSecret.codeIntervalSeconds,
86-
totpSecret.codeLength
101+
totpSecret.codeLength,
102+
totpTimestamp
87103
);
88104

89105
const multiFactorAssertion =
@@ -95,9 +111,12 @@ describe(' Integration tests: Mfa TOTP', () => {
95111
.fulfilled;
96112
});
97113

98-
it('should not allow sign-in with incorrect totp', async () => {
99-
let resolver;
114+
it('should not allow sign-in with incorrect totp', async function () {
115+
let resolver: any;
100116

117+
if (emulatorUrl) {
118+
this.skip();
119+
}
101120
try {
102121
await signInWithEmailAndPassword(auth, email, 'password');
103122

@@ -120,13 +139,11 @@ describe(' Integration tests: Mfa TOTP', () => {
120139
}
121140
});
122141

123-
it('should allow sign-in with for correct totp and unenroll successfully', async () => {
124-
let resolver;
125-
126-
await delay(30 * 1000);
127-
//TODO(bhparijat) generate the otp code for the next time window by passing the appropriate
128-
//timestamp to avoid the 30s delay. The delay is needed because the otp code used for enrollment
129-
//cannot be reused for signing in.
142+
it('should allow sign-in with for correct totp and unenroll successfully', async function () {
143+
let resolver: any;
144+
if (emulatorUrl) {
145+
this.skip();
146+
}
130147
try {
131148
await signInWithEmailAndPassword(auth, email, 'password');
132149

@@ -138,11 +155,15 @@ describe(' Integration tests: Mfa TOTP', () => {
138155
resolver = getMultiFactorResolver(auth, error as any);
139156
expect(resolver.hints).to.have.length(1);
140157

158+
totpTimestamp.setSeconds(totpTimestamp.getSeconds() + 30);
159+
141160
const totpVerificationCode = getTotpCode(
142161
totpSecret.secretKey,
143162
totpSecret.codeIntervalSeconds,
144-
totpSecret.codeLength
163+
totpSecret.codeLength,
164+
totpTimestamp
145165
);
166+
146167
const assertion = TotpMultiFactorGenerator.assertionForSignIn(
147168
resolver.hints[0].uid,
148169
totpVerificationCode
@@ -152,6 +173,8 @@ describe(' Integration tests: Mfa TOTP', () => {
152173
const mfaUser = multiFactor(userCredential.user);
153174

154175
await expect(mfaUser.unenroll(resolver.hints[0].uid)).to.be.fulfilled;
176+
await expect(signInWithEmailAndPassword(auth, email, 'password')).to.be
177+
.fulfilled;
155178
}
156-
}).timeout(32000);
179+
});
157180
});

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11105,6 +11105,11 @@ jsprim@^1.2.2:
1110511105
json-schema "0.2.3"
1110611106
verror "1.10.0"
1110711107

11108+
jssha@^3.1.2:
11109+
version "3.3.0"
11110+
resolved "https://registry.npmjs.org/jssha/-/jssha-3.3.0.tgz#44b5531bcf55a12f4a388476c647a9a1cca92839"
11111+
integrity sha512-w9OtT4ALL+fbbwG3gw7erAO0jvS5nfvrukGPMWIAoea359B26ALXGpzy4YJSp9yGnpUvuvOw1nSjSoHDfWSr1w==
11112+
1110811113
jszip@^3.1.3, jszip@^3.6.0:
1110911114
version "3.7.1"
1111011115
resolved "https://registry.npmjs.org/jszip/-/jszip-3.7.1.tgz#bd63401221c15625a1228c556ca8a68da6fda3d9"
@@ -16773,6 +16778,13 @@ [email protected]:
1677316778
resolved "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz#3be34321a88a820ed1bd80dfaa33e479fbb8dd35"
1677416779
integrity sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==
1677516780

16781+
16782+
version "0.0.14"
16783+
resolved "https://registry.npmjs.org/totp-generator/-/totp-generator-0.0.14.tgz#c136bcb4030f9cab5b10ecd82d15cd2d5518234a"
16784+
integrity sha512-vFZ8N2TdF4mCj8bUW460jI73LqS+JKccsZ8cttQSuXa3dkTmZo8q81Pq2yAuiPxCI5fPfUrfaKuU+7adjx5s4w==
16785+
dependencies:
16786+
jssha "^3.1.2"
16787+
1677616788
tough-cookie@~2.5.0:
1677716789
version "2.5.0"
1677816790
resolved "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.5.0.tgz#cd9fb2a0aa1d5a12b473bd9fb96fa3dcff65ade2"

0 commit comments

Comments
 (0)