-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds auth hooks headless tests. Refactors headless tests a bit.
- Loading branch information
Showing
9 changed files
with
153 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...-test/examples/todoApp/migrations/20240508125445_add_headless_test_property/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "isOnAfterSignupHookCalled" BOOLEAN NOT NULL DEFAULT false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { HttpError } from 'wasp/server' | ||
import type { | ||
OnAfterSignupHookFn, | ||
OnBeforeSignupHookFn, | ||
} from 'wasp/server/auth' | ||
|
||
export const onBeforeSignup: OnBeforeSignupHookFn = async (args) => { | ||
if (args.providerId.providerUserId === 'notallowed@email.com') { | ||
throw new HttpError(403, 'On Before Signup Hook disallows this email.') | ||
} | ||
} | ||
|
||
export const onAfterSignup: OnAfterSignupHookFn = async (args) => { | ||
await args.prisma.user.update({ | ||
where: { id: args.user.id }, | ||
data: { | ||
isOnAfterSignupHookCalled: true, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { test, expect } from '@playwright/test' | ||
import { | ||
generateRandomCredentials, | ||
performLogin, | ||
performSignup, | ||
} from './helpers' | ||
|
||
test.describe('auth hooks', () => { | ||
test.describe.configure({ mode: 'serial' }) | ||
|
||
/* | ||
We set up the "before signup hook" to throw an error for a specific email address. | ||
*/ | ||
test('before signup hook works', async ({ page }) => { | ||
const emailThatThrowsError = 'notallowed@email.com' | ||
const password = '12345678' | ||
|
||
await performSignup(page, { | ||
email: emailThatThrowsError, | ||
password, | ||
}) | ||
|
||
await expect(page.locator('body')).toContainText( | ||
'On Before Signup Hook disallows this email.', | ||
) | ||
}) | ||
|
||
/* | ||
We set up the "after signup hook" to set a value in the user object. | ||
*/ | ||
test('after signup hook works', async ({ page }) => { | ||
const { email, password } = generateRandomCredentials() | ||
|
||
await performSignup(page, { | ||
email, | ||
password, | ||
}) | ||
|
||
await performLogin(page, { | ||
email, | ||
password, | ||
}) | ||
|
||
await expect(page).toHaveURL('/profile') | ||
|
||
await expect(page.locator('body')).toContainText( | ||
'Value of user.isOnAfterSignupHookCalled is true.', | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { Page } from '@playwright/test' | ||
|
||
export async function performSignup( | ||
page: Page, | ||
{ email, password }: { email: string; password: string }, | ||
) { | ||
await page.goto('/signup') | ||
|
||
await page.waitForSelector('text=Create a new account') | ||
|
||
await page.locator("input[type='email']").fill(email) | ||
await page.locator("input[type='password']").fill(password) | ||
await page.locator('button').click() | ||
} | ||
|
||
export async function performLogin( | ||
page: Page, | ||
{ | ||
email, | ||
password, | ||
}: { | ||
email: string | ||
password: string | ||
}, | ||
) { | ||
await page.goto('/login') | ||
|
||
await page.waitForSelector('text=Log in to your account') | ||
|
||
await page.locator("input[type='email']").fill(email) | ||
await page.locator("input[type='password']").fill(password) | ||
await page.getByRole('button', { name: 'Log in' }).click() | ||
} | ||
|
||
export function generateRandomCredentials(): { | ||
email: string | ||
password: string | ||
} { | ||
return { | ||
email: `test${Math.random().toString(36).substring(7)}@test.com`, | ||
password: '12345678', | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,35 @@ | ||
import { test, expect } from '@playwright/test' | ||
import { generateRandomCredentials, performSignup } from './helpers' | ||
|
||
test.describe('user API', () => { | ||
const randomEmail = `test${Math.random().toString(36).substring(7)}@test.com` | ||
const password = '12345678' | ||
const { email, password } = generateRandomCredentials() | ||
|
||
test.describe.configure({ mode: 'serial' }) | ||
|
||
test.beforeAll(async ({ browser }) => { | ||
const page = await browser.newPage() | ||
|
||
// Sign up | ||
await page.goto('/signup') | ||
|
||
await page.waitForSelector('text=Create a new account') | ||
|
||
await page.locator("input[type='email']").fill(randomEmail) | ||
await page.locator("input[type='password']").fill(password) | ||
await page.locator('button').click() | ||
await performSignup(page, { | ||
email, | ||
password, | ||
}) | ||
}) | ||
|
||
test('user API works on the client', async ({ page }) => { | ||
await page.goto('/login') | ||
|
||
await page.waitForSelector('text=Log in to your account') | ||
|
||
await page.locator("input[type='email']").fill(randomEmail) | ||
await page.locator("input[type='email']").fill(email) | ||
await page.locator("input[type='password']").fill(password) | ||
await page.getByRole('button', { name: 'Log in' }).click() | ||
|
||
await page.waitForSelector('text=Profile page') | ||
|
||
await expect(page.locator('body')).toContainText( | ||
`Hello ${randomEmail}! Your status is verfied`, | ||
`Hello ${email}! Your status is verfied`, | ||
) | ||
|
||
await expect(page.locator('a[href="/profile"]')).toContainText(randomEmail) | ||
await expect(page.locator('a[href="/profile"]')).toContainText(email) | ||
}) | ||
}) |