Skip to content

Commit 42d7a40

Browse files
committed
adding skippig of totp tests if emulator is being used
1 parent 5696b84 commit 42d7a40

File tree

1 file changed

+96
-80
lines changed

1 file changed

+96
-80
lines changed

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

Lines changed: 96 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import {
3737
TotpMultiFactorGenerator,
3838
TotpSecret
3939
} from '../../../src/mfa/assertions/totp';
40+
import { _emulatorUrl } from '../../../src/core/util/emulator';
41+
import { getEmulatorUrl } from '../../helpers/integration/settings';
4042

4143
use(chaiAsPromised);
4244
use(sinonChai);
@@ -46,116 +48,130 @@ describe(' Integration tests: Mfa TOTP', () => {
4648
let totpSecret: TotpSecret;
4749
let displayName: string;
4850
let totpTimestamp: Date;
51+
let emulatorUrl: string | null;
4952
beforeEach(async () => {
50-
auth = getTestInstance();
51-
displayName = 'totp-integration-test';
53+
emulatorUrl = getEmulatorUrl();
54+
if (!emulatorUrl) {
55+
auth = getTestInstance();
56+
displayName = 'totp-integration-test';
57+
}
5258
});
5359

5460
afterEach(async () => {
55-
await cleanUpTestInstance(auth);
61+
if (!emulatorUrl) {
62+
await cleanUpTestInstance(auth);
63+
}
5664
});
5765

5866
it('should not enroll if incorrect totp supplied', async () => {
59-
const cr = await signInWithEmailAndPassword(auth, email, 'password');
60-
const mfaUser = multiFactor(cr.user);
61-
const session = await mfaUser.getSession();
62-
totpSecret = await TotpMultiFactorGenerator.generateSecret(session);
63-
const multiFactorAssertion =
64-
TotpMultiFactorGenerator.assertionForEnrollment(
65-
totpSecret,
66-
incorrectTotpCode
67-
);
68-
69-
await expect(
70-
mfaUser.enroll(multiFactorAssertion, displayName)
71-
).to.be.rejectedWith('auth/invalid-verification-code');
67+
if (!emulatorUrl) {
68+
const cr = await signInWithEmailAndPassword(auth, email, 'password');
69+
const mfaUser = multiFactor(cr.user);
70+
const session = await mfaUser.getSession();
71+
totpSecret = await TotpMultiFactorGenerator.generateSecret(session);
72+
const multiFactorAssertion =
73+
TotpMultiFactorGenerator.assertionForEnrollment(
74+
totpSecret,
75+
incorrectTotpCode
76+
);
77+
78+
await expect(
79+
mfaUser.enroll(multiFactorAssertion, displayName)
80+
).to.be.rejectedWith('auth/invalid-verification-code');
81+
}
7282
});
7383

7484
it('should enroll using correct otp', async () => {
75-
const cr = await signInWithEmailAndPassword(auth, email, 'password');
76-
77-
const mfaUser = multiFactor(cr.user);
85+
if (!emulatorUrl) {
86+
const cr = await signInWithEmailAndPassword(auth, email, 'password');
7887

79-
const session = await mfaUser.getSession();
88+
const mfaUser = multiFactor(cr.user);
8089

81-
totpSecret = await TotpMultiFactorGenerator.generateSecret(session);
90+
const session = await mfaUser.getSession();
8291

83-
totpTimestamp = new Date();
92+
totpSecret = await TotpMultiFactorGenerator.generateSecret(session);
8493

85-
const totpVerificationCode = getTotpCode(
86-
totpSecret.secretKey,
87-
totpSecret.codeIntervalSeconds,
88-
totpSecret.codeLength,
89-
totpTimestamp
90-
);
94+
totpTimestamp = new Date();
9195

92-
const multiFactorAssertion =
93-
TotpMultiFactorGenerator.assertionForEnrollment(
94-
totpSecret,
95-
totpVerificationCode
96+
const totpVerificationCode = getTotpCode(
97+
totpSecret.secretKey,
98+
totpSecret.codeIntervalSeconds,
99+
totpSecret.codeLength,
100+
totpTimestamp
96101
);
97-
await expect(mfaUser.enroll(multiFactorAssertion, displayName)).to.be
98-
.fulfilled;
102+
103+
const multiFactorAssertion =
104+
TotpMultiFactorGenerator.assertionForEnrollment(
105+
totpSecret,
106+
totpVerificationCode
107+
);
108+
await expect(mfaUser.enroll(multiFactorAssertion, displayName)).to.be
109+
.fulfilled;
110+
}
99111
});
100112

101113
it('should not allow sign-in with incorrect totp', async () => {
102114
let resolver;
103115

104-
try {
105-
await signInWithEmailAndPassword(auth, email, 'password');
116+
if (!emulatorUrl) {
117+
try {
118+
await signInWithEmailAndPassword(auth, email, 'password');
106119

107-
throw new Error('Signin should not have been successful');
108-
} catch (error) {
109-
expect(error).to.be.an.instanceOf(FirebaseError);
110-
expect((error as any).code).to.eql('auth/multi-factor-auth-required');
120+
throw new Error('Signin should not have been successful');
121+
} catch (error) {
122+
expect(error).to.be.an.instanceOf(FirebaseError);
123+
expect((error as any).code).to.eql('auth/multi-factor-auth-required');
111124

112-
resolver = getMultiFactorResolver(auth, error as any);
113-
expect(resolver.hints).to.have.length(1);
125+
resolver = getMultiFactorResolver(auth, error as any);
126+
expect(resolver.hints).to.have.length(1);
114127

115-
const assertion = TotpMultiFactorGenerator.assertionForSignIn(
116-
resolver.hints[0].uid,
117-
incorrectTotpCode
118-
);
128+
const assertion = TotpMultiFactorGenerator.assertionForSignIn(
129+
resolver.hints[0].uid,
130+
incorrectTotpCode
131+
);
119132

120-
await expect(resolver.resolveSignIn(assertion)).to.be.rejectedWith(
121-
'auth/invalid-verification-code'
122-
);
133+
await expect(resolver.resolveSignIn(assertion)).to.be.rejectedWith(
134+
'auth/invalid-verification-code'
135+
);
136+
}
123137
}
124138
});
125139

126140
it('should allow sign-in with for correct totp and unenroll successfully', async () => {
127141
let resolver;
128-
try {
129-
await signInWithEmailAndPassword(auth, email, 'password');
130-
131-
throw new Error('Signin should not have been successful');
132-
} catch (error) {
133-
expect(error).to.be.an.instanceOf(FirebaseError);
134-
expect((error as any).code).to.eql('auth/multi-factor-auth-required');
135-
136-
resolver = getMultiFactorResolver(auth, error as any);
137-
expect(resolver.hints).to.have.length(1);
138-
139-
totpTimestamp.setSeconds(totpTimestamp.getSeconds() + 30);
140-
141-
const totpVerificationCode = getTotpCode(
142-
totpSecret.secretKey,
143-
totpSecret.codeIntervalSeconds,
144-
totpSecret.codeLength,
145-
totpTimestamp
146-
);
147-
148-
const assertion = TotpMultiFactorGenerator.assertionForSignIn(
149-
resolver.hints[0].uid,
150-
totpVerificationCode
151-
);
152-
const userCredential = await resolver.resolveSignIn(assertion);
153-
154-
const mfaUser = multiFactor(userCredential.user);
155-
156-
await expect(mfaUser.unenroll(resolver.hints[0].uid)).to.be.fulfilled;
157-
await expect(signInWithEmailAndPassword(auth, email, 'password')).to.be
158-
.fulfilled;
142+
if (!emulatorUrl) {
143+
try {
144+
await signInWithEmailAndPassword(auth, email, 'password');
145+
146+
throw new Error('Signin should not have been successful');
147+
} catch (error) {
148+
expect(error).to.be.an.instanceOf(FirebaseError);
149+
expect((error as any).code).to.eql('auth/multi-factor-auth-required');
150+
151+
resolver = getMultiFactorResolver(auth, error as any);
152+
expect(resolver.hints).to.have.length(1);
153+
154+
totpTimestamp.setSeconds(totpTimestamp.getSeconds() + 30);
155+
156+
const totpVerificationCode = getTotpCode(
157+
totpSecret.secretKey,
158+
totpSecret.codeIntervalSeconds,
159+
totpSecret.codeLength,
160+
totpTimestamp
161+
);
162+
163+
const assertion = TotpMultiFactorGenerator.assertionForSignIn(
164+
resolver.hints[0].uid,
165+
totpVerificationCode
166+
);
167+
const userCredential = await resolver.resolveSignIn(assertion);
168+
169+
const mfaUser = multiFactor(userCredential.user);
170+
171+
await expect(mfaUser.unenroll(resolver.hints[0].uid)).to.be.fulfilled;
172+
await expect(signInWithEmailAndPassword(auth, email, 'password')).to.be
173+
.fulfilled;
174+
}
159175
}
160176
});
161177
});

0 commit comments

Comments
 (0)