From af14ae62faa97f9a868cbb713cce46bcbcba7dbd Mon Sep 17 00:00:00 2001 From: April Hall Date: Wed, 26 Feb 2025 16:54:51 -0500 Subject: [PATCH] feat: Tests for profile image changer --- src/lib/components/user.svelte | 2 +- {tests => static}/freakybear.jpg | Bin tests/profileImage.spec.ts | 62 ++++++++++++++++++++++++++++++ tests/signout.spec.ts | 64 +++++++++++++++---------------- 4 files changed, 95 insertions(+), 33 deletions(-) rename {tests => static}/freakybear.jpg (100%) create mode 100644 tests/profileImage.spec.ts diff --git a/src/lib/components/user.svelte b/src/lib/components/user.svelte index ecee8d6..37d6559 100644 --- a/src/lib/components/user.svelte +++ b/src/lib/components/user.svelte @@ -12,7 +12,7 @@
- Profile image for {data.user.username} + Profile image for {data.user.username}
diff --git a/tests/freakybear.jpg b/static/freakybear.jpg similarity index 100% rename from tests/freakybear.jpg rename to static/freakybear.jpg diff --git a/tests/profileImage.spec.ts b/tests/profileImage.spec.ts new file mode 100644 index 0000000..af46120 --- /dev/null +++ b/tests/profileImage.spec.ts @@ -0,0 +1,62 @@ +import { test, expect, type Page, type Locator } from '@playwright/test'; +import { login } from './utils'; + +async function getImgSrc(image: Locator) { + return await image.getAttribute('src'); +} + +test.describe('Profile Photo Update', () => { + let page: Page; + let fileInput: Locator; + let profileImage: Locator; + let submitButton: 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 + submitButton = page.getByRole('button', { name: 'Update Profile Photo' }); + profileImage = page.locator('img#userimage'); + fileInput = page.locator('input[type="file"]'); + }); + + test('successfully update profile image', async () => { + await page.waitForTimeout(1000); + + // Get the inital image src + const initalSrc = await getImgSrc(profileImage); + + // Upload the new image + await fileInput.setInputFiles(['./static/freakybear.jpg']); + await submitButton.click(); + + // Wait for upload to complete + const response = await page.waitForResponse((response) => response.request().method() === 'POST', { timeout: 30000 }); + expect(response.status()).toBe(200); + + // Make sure the src is not the same as the original + expect(await getImgSrc(profileImage)).not.toEqual(initalSrc); + }); + + test("shouldn't accept non-images", async () => { + await page.waitForTimeout(1000); + + // Get the inital image src + const initalSrc = await getImgSrc(profileImage); + + // Upload the new image + await fileInput.setInputFiles(['./README.md']); + await submitButton.click(); + + // Wait for upload to complete + const response = await page.waitForResponse((response) => response.request().method() === 'POST', { timeout: 30000 }); + expect(response.status()).toBe(500); + + // Make sure the src is the same as the original + expect(await getImgSrc(profileImage)).toEqual(initalSrc); + }); +}); diff --git a/tests/signout.spec.ts b/tests/signout.spec.ts index dc9bd66..d77ba26 100644 --- a/tests/signout.spec.ts +++ b/tests/signout.spec.ts @@ -1,39 +1,39 @@ -// import { test, expect, type Page, type Locator } from '@playwright/test'; -// import { login } from './utils'; +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.describe('Sign Out Button', () => { + let page: Page; + let button: Locator; -// test.beforeEach(async ({ browser }) => { -// page = await browser.newPage(); + 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' }); -// }); + // 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('; '); + 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); + const initalfetch = await request.get('/api/checkauth', { + headers: { + Cookie: cookieHeader, + }, + }); + expect(initalfetch.status()).toEqual(200); -// await button.click(); + await button.click(); -// const finalfetch = await request.get('/api/checkauth', { -// headers: { -// Cookie: cookieHeader, -// }, -// }); -// expect(finalfetch.status()).toEqual(401); -// }); -// }); + const finalfetch = await request.get('/api/checkauth', { + headers: { + Cookie: cookieHeader, + }, + }); + expect(finalfetch.status()).toEqual(401); + }); +});