Skip to content

[Auth] Add a bunch of integration tests for the various OOB Code flows #4578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { Auth } from '@firebase/auth-exp';
import { getApps } from '@firebase/app-exp';
import { FetchProvider } from '../../../src/core/util/fetch_provider';
import * as fetchImpl from 'node-fetch';

interface VerificationSession {
if (typeof document !== 'undefined') {
FetchProvider.initialize(fetch);
} else {
FetchProvider.initialize(
(fetchImpl.default as unknown) as typeof fetch,
(fetchImpl.Headers as unknown) as typeof Headers,
(fetchImpl.Response as unknown) as typeof Response
);
}

export interface VerificationSession {
code: string;
phoneNumber: string;
sessionInfo: string;
Expand All @@ -29,19 +41,47 @@ interface VerificationCodesResponse {
verificationCodes: VerificationSession[];
}

export interface OobCodeSession {
email: string;
requestType: string;
oobCode: string;
oobLink: string;
}

interface OobCodesResponse {
oobCodes: OobCodeSession[];
}

export async function getPhoneVerificationCodes(
auth: Auth
): Promise<Record<string, VerificationSession>> {
assertEmulator(auth);
const url = getEmulatorUrl(auth, 'verificationCodes');
const response: VerificationCodesResponse = await (await fetch(url)).json();
const response: VerificationCodesResponse = await (
await FetchProvider.fetch()(url)
).json();

return response.verificationCodes.reduce((accum, session) => {
accum[session.sessionInfo] = session;
return accum;
}, {} as Record<string, VerificationSession>);
}

export async function getOobCodes(
auth: Auth
): Promise<Record<string, OobCodeSession>> {
assertEmulator(auth);
const url = getEmulatorUrl(auth, 'oobCodes');
const response: OobCodesResponse = await (
await FetchProvider.fetch()(url)
).json();

return response.oobCodes.reduce((accum, session) => {
accum[session.email] = session;
return accum;
}, {} as Record<string, OobCodeSession>);
}

function getEmulatorUrl(auth: Auth, endpoint: string): string {
const { host, port, protocol } = auth.emulatorConfig!;
const projectId = getProjectId(auth);
Expand Down
5 changes: 4 additions & 1 deletion packages-exp/auth-exp/test/helpers/integration/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function randomEmail(): string {
return `${_generateEventId('test.email.')}@test.com`;
}

export function getTestInstance(): Auth {
export function getTestInstance(requireEmulator = false): Auth {
const app = initializeApp(getAppConfig());

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

auth.onAuthStateChanged(user => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,14 @@ describe('Integration test: custom auth', () => {
let uid: string;

beforeEach(() => {
auth = getTestInstance();
auth = getTestInstance(/* requireEmulator */ true);
uid = randomEmail();
customToken = JSON.stringify({
uid,
claims: {
customClaim: 'some-claim'
}
});

if (!auth.emulatorConfig) {
throw new Error('Test can only be run against the emulator!');
}
});

afterEach(async () => {
Expand Down
Loading