Skip to content

Commit 2301aee

Browse files
authored
[Auth] Add a bunch of integration tests for the various OOB Code flows (#4578)
* Add tests for oob flows * Formatting, license * Fix linter problems * PR feedback * Formatting
1 parent 14fbc8e commit 2301aee

File tree

4 files changed

+399
-8
lines changed

4 files changed

+399
-8
lines changed

packages-exp/auth-exp/test/helpers/integration/emulator_rest_helpers.ts

+36-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,20 @@
1818
// eslint-disable-next-line import/no-extraneous-dependencies
1919
import { Auth } from '@firebase/auth-exp';
2020
import { getApps } from '@firebase/app-exp';
21+
import { FetchProvider } from '../../../src/core/util/fetch_provider';
22+
import * as fetchImpl from 'node-fetch';
2123

22-
interface VerificationSession {
24+
if (typeof document !== 'undefined') {
25+
FetchProvider.initialize(fetch);
26+
} else {
27+
FetchProvider.initialize(
28+
(fetchImpl.default as unknown) as typeof fetch,
29+
(fetchImpl.Headers as unknown) as typeof Headers,
30+
(fetchImpl.Response as unknown) as typeof Response
31+
);
32+
}
33+
34+
export interface VerificationSession {
2335
code: string;
2436
phoneNumber: string;
2537
sessionInfo: string;
@@ -29,19 +41,41 @@ interface VerificationCodesResponse {
2941
verificationCodes: VerificationSession[];
3042
}
3143

44+
export interface OobCodeSession {
45+
email: string;
46+
requestType: string;
47+
oobCode: string;
48+
oobLink: string;
49+
}
50+
51+
interface OobCodesResponse {
52+
oobCodes: OobCodeSession[];
53+
}
54+
3255
export async function getPhoneVerificationCodes(
3356
auth: Auth
3457
): Promise<Record<string, VerificationSession>> {
3558
assertEmulator(auth);
3659
const url = getEmulatorUrl(auth, 'verificationCodes');
37-
const response: VerificationCodesResponse = await (await fetch(url)).json();
60+
const response: VerificationCodesResponse = await (
61+
await FetchProvider.fetch()(url)
62+
).json();
3863

3964
return response.verificationCodes.reduce((accum, session) => {
4065
accum[session.sessionInfo] = session;
4166
return accum;
4267
}, {} as Record<string, VerificationSession>);
4368
}
4469

70+
export async function getOobCodes(auth: Auth): Promise<OobCodeSession[]> {
71+
assertEmulator(auth);
72+
const url = getEmulatorUrl(auth, 'oobCodes');
73+
const response: OobCodesResponse = await (
74+
await FetchProvider.fetch()(url)
75+
).json();
76+
return response.oobCodes;
77+
}
78+
4579
function getEmulatorUrl(auth: Auth, endpoint: string): string {
4680
const { host, port, protocol } = auth.emulatorConfig!;
4781
const projectId = getProjectId(auth);

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function randomEmail(): string {
3131
return `${_generateEventId('test.email.')}@test.com`;
3232
}
3333

34-
export function getTestInstance(): Auth {
34+
export function getTestInstance(requireEmulator = false): Auth {
3535
const app = initializeApp(getAppConfig());
3636

3737
const createdUsers: User[] = [];
@@ -43,6 +43,9 @@ export function getTestInstance(): Auth {
4343
const stub = stubConsoleToSilenceEmulatorWarnings();
4444
useAuthEmulator(auth, emulatorUrl, { disableWarnings: true });
4545
stub.restore();
46+
} else if (requireEmulator) {
47+
/* Emulator wasn't configured but test must use emulator */
48+
throw new Error('Test may only be run using the Auth Emulator!');
4649
}
4750

4851
auth.onAuthStateChanged(user => {

packages-exp/auth-exp/test/integration/flows/custom.local.test.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,14 @@ describe('Integration test: custom auth', () => {
4747
let uid: string;
4848

4949
beforeEach(() => {
50-
auth = getTestInstance();
50+
auth = getTestInstance(/* requireEmulator */ true);
5151
uid = randomEmail();
5252
customToken = JSON.stringify({
5353
uid,
5454
claims: {
5555
customClaim: 'some-claim'
5656
}
5757
});
58-
59-
if (!auth.emulatorConfig) {
60-
throw new Error('Test can only be run against the emulator!');
61-
}
6258
});
6359

6460
afterEach(async () => {

0 commit comments

Comments
 (0)