Skip to content

Commit 5457992

Browse files
committed
all test cases working with added delay and pre-verified email
1 parent a412656 commit 5457992

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { Auth, User } from '../../../src/model/public_types';
2222
import { getAuth, connectAuthEmulator } from '../../../'; // Use browser OR node dist entrypoint depending on test env.
2323
import { _generateEventId } from '../../../src/core/util/event_id';
2424
import { getAppConfig, getEmulatorUrl } from './settings';
25-
import { resetEmulator } from './emulator_rest_helpers';
25+
import { getOobCodes, OobCodeSession, resetEmulator } from './emulator_rest_helpers';
2626
import { StartTotpMfaEnrollmentResponse } from '../../../src/api/account_management/mfa';
2727

2828
//import * as otpauth from "https://deno.land/x/[email protected]/dist/otpauth.esm.js";
@@ -62,6 +62,8 @@ export function getTestInstance(requireEmulator = false): Auth {
6262

6363
auth.cleanUp = async () => {
6464
// If we're in an emulated environment, the emulator will clean up for us
65+
66+
console.log('Auth cleanup should not be called');
6567
if (emulatorUrl) {
6668
await resetEmulator();
6769
} else {
@@ -83,7 +85,7 @@ export function getTestInstance(requireEmulator = false): Auth {
8385

8486
export async function cleanUpTestInstance(auth: Auth): Promise<void> {
8587
await auth.signOut();
86-
await (auth as IntegrationTestAuth).cleanUp();
88+
//await (auth as IntegrationTestAuth).cleanUp();
8789
}
8890

8991
function stubConsoleToSilenceEmulatorWarnings(): sinon.SinonStub {
@@ -99,17 +101,26 @@ function stubConsoleToSilenceEmulatorWarnings(): sinon.SinonStub {
99101
});
100102
}
101103

102-
export async function mockTotp(sharedSecretKey: string, periodSec: number, verificationCodeLength: number){
103-
console.log("**** starting to mock totp");
104+
export async function code(toEmail: string): Promise<OobCodeSession> {
105+
const codes = await getOobCodes();
106+
console.log('codes: ', codes);
107+
108+
return codes.reverse().find(({ email }) => email === toEmail)!;
109+
}
104110

105-
let digits = 9;
106-
let period = 30;
107-
let secret = "private";
108-
const headers = new Headers();
111+
112+
export function getTotpCode(sharedSecretKey: string, periodSec: number, verificationCodeLength: number, hashingAlgorithm: string){
113+
114+
let token = totp(sharedSecretKey, { period: periodSec, digits: verificationCodeLength, algorithm: 'SHA-1'});
109115

110-
let token = totp(sharedSecretKey, { period: periodSec, digits: verificationCodeLength });
111-
console.log("***"+token);
112116

113117
return token
114118

119+
}
120+
121+
export function delay(dt:number){
122+
123+
console.log('Delay called');
124+
125+
return new Promise(resolve => setTimeout(resolve, dt));
115126
}

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ describe(' Integration tests: Mfa TOTP', () => {
174174

175175
})
176176

177-
it('should allow sign-in for correct totp', async () => {
177+
it('should not allow sign-in with incorrect totp', async () => {
178178
let session;
179179
let cr;
180180
let resolver;
@@ -210,7 +210,7 @@ describe(' Integration tests: Mfa TOTP', () => {
210210
console.log(assertion);
211211

212212

213-
await expect(resolver.resolveSignIn(assertion)).to.be.rejectedWith('nothing');
213+
await expect(resolver.resolveSignIn(assertion)).to.be.rejectedWith('auth/invalid-verification-code');
214214

215215
await auth.signOut();
216216

@@ -221,5 +221,47 @@ describe(' Integration tests: Mfa TOTP', () => {
221221

222222
it('should allow sign-in with for correct totp and unenroll successfully', async() => {
223223

224+
let resolver;
225+
226+
console.log(email);
227+
console.log('session info for: ', getApp().options.projectId);
228+
229+
await delay(15*1000);
230+
try{
231+
232+
const userCredential = await signInWithEmailAndPassword(auth, email, 'password');
233+
234+
console.log('success: ', userCredential);
235+
236+
throw new Error('Signin should not have been successful');
237+
238+
} catch(error ){
239+
240+
241+
console.log('error occured: ', (error as any).code);
242+
expect((error as any).code).to.eql('auth/multi-factor-auth-required');
243+
244+
resolver = getMultiFactorResolver(auth,error as any);
245+
console.log(resolver.hints, totpSecret.secretKey);
246+
expect(resolver.hints).to.have.length(1);
247+
248+
const totpVerificationCode = getTotpCode(totpSecret.secretKey, totpSecret.codeIntervalSeconds, totpSecret.codeLength, totpSecret.hashingAlgorithm);
249+
console.log(totpVerificationCode, resolver.hints[0].uid )
250+
const assertion = TotpMultiFactorGenerator.assertionForSignIn(
251+
resolver.hints[0].uid,
252+
totpVerificationCode
253+
);
254+
255+
console.log(assertion);
256+
257+
258+
const userCredential = await resolver.resolveSignIn(assertion);
259+
260+
const mfaUser = multiFactor(userCredential.user);
261+
262+
await expect(mfaUser.unenroll(resolver.hints[0].uid)).to.be.fulfilled;
263+
await auth.signOut();
264+
265+
}
224266
})
225267
})

0 commit comments

Comments
 (0)