diff --git a/src/routes/(server)/api/checkauth/+server.ts b/src/routes/(server)/api/checkauth/+server.ts new file mode 100644 index 0000000..ddef114 --- /dev/null +++ b/src/routes/(server)/api/checkauth/+server.ts @@ -0,0 +1,14 @@ +import { auth } from '$lib/server/db/auth'; +import { json } from '@sveltejs/kit'; + +export const GET = async ({ request }) => { + const session = await auth.api.getSession({ + headers: request.headers, + }); + + if (session) { + return json({ status: 200 }); + } else { + return json({ status: 401 }, { status: 401 }); + } +}; diff --git a/tests/signout.spec.ts b/tests/signout.spec.ts new file mode 100644 index 0000000..d77ba26 --- /dev/null +++ b/tests/signout.spec.ts @@ -0,0 +1,39 @@ +import { test, expect, type Page, type Locator } from '@playwright/test'; +import { login } from './utils'; + +test.describe('Sign Out Button', () => { + let page: Page; + let button: Locator; + + test.beforeEach(async ({ browser }) => { + page = await browser.newPage(); + + // Login and navigate + await login(page); + await page.goto('/account', { timeout: 30000, waitUntil: 'domcontentloaded' }); + // Initialize locators + button = page.getByRole('button', { name: 'Sign Out' }); + }); + + test('sign out button signs user out', async ({ request }) => { + // Get cookies from the browser context + const cookies = await page.context().cookies(); + const cookieHeader = cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join('; '); + + const initalfetch = await request.get('/api/checkauth', { + headers: { + Cookie: cookieHeader, + }, + }); + expect(initalfetch.status()).toEqual(200); + + await button.click(); + + const finalfetch = await request.get('/api/checkauth', { + headers: { + Cookie: cookieHeader, + }, + }); + expect(finalfetch.status()).toEqual(401); + }); +}); diff --git a/tests/updatePassword.spec.ts b/tests/updatePassword.spec.ts index d601198..3d09699 100644 --- a/tests/updatePassword.spec.ts +++ b/tests/updatePassword.spec.ts @@ -65,8 +65,8 @@ test.describe('Password Update Form', () => { await expectError('Password must not be empty.', page); }); - // Test update functionalityz - test('should successfully update usser password', async () => { + // Test update functionality + test('should successfully update user password', async () => { await currentPasswordInput.fill(currentPassword); await newPasswordInput.fill('newPassword123!'); await submitButton.click();