Skip to content

Instantly share code, notes, and snippets.

@ebeloded
Last active March 31, 2022 06:40
Show Gist options
  • Save ebeloded/0991c4d7bfb6aed3f98414616ef3d569 to your computer and use it in GitHub Desktop.
Save ebeloded/0991c4d7bfb6aed3f98414616ef3d569 to your computer and use it in GitHub Desktop.
Playwright auth
import esbuild from 'esbuild'
import { firebaseConfig } from '@lumiere/config'
import { initializeApp } from 'firebase-admin/app'
import { getAuth } from 'firebase-admin/auth'
import { expect } from '@playwright/test'
import { test } from './setup'
const app = initializeApp()
const auth = getAuth(app)
const bundleAuthScript = async () => {
return esbuild
.build({
entryPoints: ['./tests/setup/prepareAuth.ts'],
write: false,
bundle: true,
define: {
'process.env.FIREBASE_CONFIG': JSON.stringify(firebaseConfig.admin),
'process.env.FIREBASE_AUTH_EMULATOR_HOST': `"${process.env.FIREBASE_AUTH_EMULATOR_HOST}"`,
},
})
.then(({ outputFiles }) => outputFiles[0].text)
}
test('redirects to login page', async ({ page }) => {
await page.goto('.')
expect(new URL(page.url()).pathname).toEqual('/admin')
await page.waitForNavigation()
expect(new URL(page.url()).pathname).toEqual('/admin/login')
})
test.describe('authentication', () => {
test('authenticate', async ({ page }) => {
const uid = 'test-user'
await auth.createUser({ uid }).catch(() => {})
const token = await auth.createCustomToken(uid)
await page.goto('./void.html')
await page.addScriptTag({
content: await bundleAuthScript(),
})
await page.evaluate(
(token) => __TEST_ENV__.signInWithCustomToken(token),
token
)
await page.goto('.')
})
test('authenticate 2', async ({ page }) => {
await page.goto('.')
})
})
import { initializeApp } from 'firebase/app'
import {
connectAuthEmulator,
getAuth,
signInWithCustomToken,
} from 'firebase/auth'
console.log('init TEST_ENV')
const app = initializeApp(process.env.FIREBASE_CONFIG)
const auth = getAuth(app)
connectAuthEmulator(auth, `http://${process.env.FIREBASE_AUTH_EMULATOR_HOST}`)
window.__TEST_ENV__ = {
signInWithCustomToken: (token: string) => signInWithCustomToken(auth, token),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment