All files / realm/blong-party/test profile.play.ts

86.19% Statements 281/326
100% Branches 11/11
85.71% Functions 6/7
86.19% Lines 281/326

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 3271x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x             3x 3x 3x 3x 1x 1x 3x 3x             3x 3x 1x 1x 1x 1x 1x 1x 1x               1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                               1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x  
/**
 * Self-service user profile — full personal-details path.
 *
 * The blong-party suite has BOTH blong-access and blong-party, so testAdmin is
 * linked (dbTest seed `partyTestProfileMerge`) to a `party.person` via the
 * `hasProfile` edge → the profile page shows personal details and the avatar
 * shows the name initials ("TA").
 *
 * Screenshots (baseline via `npm run playwright:update`, verify via
 * `npm run playwright`):
 *   - `profile-menu.png`     — the account menu open (avatar visible)
 *   - `profile-open.png`     — the profile tab loaded (personal details visible)
 *   - `profile-edit.png`     — name + email + language edited (dirty)
 *   - `profile-saved.png`    — saved with the success message
 *   - `profile-password.png` — the change-password form filled in
 *
 * The test restores the password (testPassword), the name, the email and the
 * language at the end so the shared dev DB stays stable.
 */
import {expect, test} from '@feasibleone/blong-browser/playwright';
import type {Page} from '@playwright/test';
 
test.use({blongPermissions: true});
 
/**
 * A 1×1 red PNG — enough for the store to call it a picture, for `sharp` to derive the thumbnail the
 * widget asks for, and small enough to sit in the spec.
 */
const TINY_PNG = Buffer.from(
    'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
    'base64',
);
 
/** The rest of the profile, so a case can change the photograph and nothing else. */
const PERSONAL_DETAILS = {
    firstName: 'Test',
    lastName: 'Admin',
    emailAddress: 'testAdmin@example.com',
    preferredLanguage: 'en',
};
 
/** Set or clear the photograph without going through the form — the specs' own state control. */
async function storePhoto(page: Page, photoUrl: string | null): Promise<void> {
    await page.evaluate(
        async photo => {
            const handler = (window as unknown as {__blongHandler?: Record<string, unknown>})
                .__blongHandler;
            await (handler?.accessProfileEdit as (p: object, m: object) => Promise<unknown>)(
                {...photo.fields, photoUrl: photo.url},
                {},
            );
        },
        {fields: PERSONAL_DETAILS, url: photoUrl},
    );
}
 
/** The photograph the profile endpoint answers with, which is what the avatar draws. */
async function storedPhoto(page: Page): Promise<string | null> {
    return page.evaluate(async () => {
        const handler = (window as unknown as {__blongHandler?: Record<string, unknown>})
            .__blongHandler;
        const data = (await (
            handler?.accessProfileGet as (p: object, m: object) => Promise<unknown>
        )({}, {})) as {person?: {photoUrl?: string | null}};
        return data?.person?.photoUrl ?? null;
    });
}
 
/** Every record the store keeps in one namespace — the store's own answer, not the page's. */
async function blobRecords(
    page: Page,
    namespace: string,
): Promise<Array<{key: string; originalFilename?: string}>> {
    return page.evaluate(async ns => {
        const handler = (window as unknown as {__blongHandler?: Record<string, unknown>})
            .__blongHandler;
        const found = (await (handler?.blobBlobFind as (p: object, m: object) => Promise<unknown>)(
            {namespace: ns},
            {},
        )) as {blob?: Array<{key: string; originalFilename?: string}>};
        return found?.blob ?? [];
    }, namespace);
}
 
/** The avatar as a picture: an `<img>` whose bytes actually decoded. */
const avatarPicture = (page: Page) =>
    page.locator('.blong-account-menu__photo img.blong-blob-image');
 
test.describe('Party user profile', () => {
    test('shows personal details and allows editing the profile and password', async ({
        page,
        portal,
    }) => {
        test.setTimeout(120_000);
 
        // The portal fixture has logged in (as testAdmin) and rendered the menubar.
        await expect(portal.page.locator('.blong-portal-menubar')).toBeVisible();
 
        // Normalise the shared dev DB so screenshots are deterministic
        // regardless of leftover state from earlier runs.
        await page.evaluate(async () => {
            const handler = (window as unknown as {__blongHandler?: Record<string, unknown>})
                .__blongHandler;
            await (handler?.accessProfileEdit as (p: object, m: object) => Promise<unknown>)(
                {
                    emailAddress: 'testAdmin@example.com',
                    preferredLanguage: 'en',
                    firstName: 'Test',
                    lastName: 'Admin',
                },
                {},
            );
        });
 
        // The edit above reaches the database, not the page: the account menu rendered
        // before it and keeps the initials it was given, so the avatar has to be read from
        // a page that started after the edit. The leftover name is what a CI run showed —
        // the avatar read `TE` where this spec wants `TA`, was held for the full
        // expectation, and the *retry* passed because the call above had normalised the
        // row by then, which is the same state a reload produces here.
        await page.reload();
        await expect(portal.page.locator('.blong-portal-menubar')).toBeVisible();
 
        // The avatar shows the name initials (Test Admin → TA).
        const avatar = page.locator('.blong-account-menu__avatar');
        await expect(avatar).toBeVisible();
        await expect(avatar).toContainText('TA');
 
        // ── Open the account menu ───────────────────────────────────────────
        await avatar.click();
        const menu = page.locator('.blong-account-menu__menu');
        await expect(menu).toBeVisible();
        await expect(page).toHaveScreenshot('profile-menu.png');
 
        // ── Open the profile tab ────────────────────────────────────────────
        await menu.locator('.p-menuitem-link', {hasText: 'Profile'}).click();
        const profile = page.locator('.access-profile');
        await expect(profile).toBeVisible();
        const saveButton = profile.getByRole('button', {name: /save/i});
        await expect(saveButton).toBeEnabled();
 
        // Personal details from the linked party.person.
        await expect(profile.locator('input[name="firstName"]')).toHaveValue('Test');
        await expect(profile.locator('input[name="lastName"]')).toHaveValue('Admin');
        await expect(profile.locator('input[name="emailAddress"]')).toHaveValue(
            'testAdmin@example.com',
        );
        await expect(profile.getByText('Admin', {exact: true})).toBeVisible();
        await expect(page).toHaveScreenshot('profile-open.png');
 
        // ── Edit name + email + language, save ──────────────────────────────
        await profile.locator('input[name="firstName"]').fill('Test Edit');
        await profile.locator('input[name="lastName"]').fill('Admin Edited');
        await profile.locator('input[name="emailAddress"]').fill('profile@example.com');
        await profile.locator('.p-dropdown').click();
        await page.locator('.p-dropdown-panel .p-dropdown-item', {hasText: 'Български'}).click();
        await expect(page).toHaveScreenshot('profile-edit.png');
 
        await saveButton.click();
        await expect(profile.locator('.p-inline-message-success')).toBeVisible();
        await expect(page).toHaveScreenshot('profile-saved.png');
 
        // ── Change password (keep current session) ──────────────────────────
        const passwordCard = profile.locator('.p-card', {hasText: 'Change Password'});
        await passwordCard.locator('input[name="currentPassword"]').fill('testPassword');
        await passwordCard.locator('input[name="newPassword"]').fill('NewPass123!');
        await passwordCard.locator('input[name="confirmPassword"]').fill('NewPass123!');
        await expect(page).toHaveScreenshot('profile-password.png');
 
        await passwordCard.getByRole('button', {name: /change password/i}).click();
        await expect(passwordCard.locator('.p-inline-message-success')).toBeVisible();
 
        // ── Restore the original password ───────────────────────────────────
        await passwordCard.locator('input[name="currentPassword"]').fill('NewPass123!');
        await passwordCard.locator('input[name="newPassword"]').fill('testPassword');
        await passwordCard.locator('input[name="confirmPassword"]').fill('testPassword');
        await passwordCard.getByRole('button', {name: /change password/i}).click();
        await expect(passwordCard.locator('.p-inline-message-success')).toBeVisible();
        await expect(passwordCard.locator('.p-inline-message-error')).toHaveCount(0);
 
        // ── Restore name + email + language ─────────────────────────────────
        await profile.locator('input[name="firstName"]').fill('Test');
        await profile.locator('input[name="lastName"]').fill('Admin');
        await profile.locator('input[name="emailAddress"]').fill('testAdmin@example.com');
        await profile.locator('.p-dropdown').click();
        await page.locator('.p-dropdown-panel .p-dropdown-item', {hasText: 'English'}).click();
        await saveButton.click();
        await expect(profile.locator('.p-inline-message-success')).toBeVisible();
        await expect(profile.locator('.p-inline-message-error')).toHaveCount(0);
 
        // Sanity — the restored password still logs in.
        const loginCheck = await page.evaluate(async () => {
            const handler = (window as unknown as {__blongHandler?: Record<string, unknown>})
                .__blongHandler;
            try {
                const result = (await (
                    handler?.loginTokenCreate as (
                        p: object,
                        m: object,
                    ) => Promise<{access_token: string}>
                )({username: 'testAdmin', password: 'testPassword'}, {})) as {
                    access_token?: string;
                };
                return Boolean(result?.access_token);
            } catch {
                return false;
            }
        });
        expect(loginCheck, 'testPassword still logs testAdmin in').toBe(true);
    });
 
    /**
     * The photograph: chosen through the widget, saved with the form, drawn on the account menu.
     *
     * The record keeps a `blob://` reference rather than a copy of the bytes, so what is asserted is the
     * whole path — the three calls the widget makes, the reference the person row ends up holding, and
     * the avatar reading those bytes back out of the store.
     */
    test('stores a photograph and shows it on the avatar', async ({page, portal}) => {
        test.setTimeout(120_000);
 
        await expect(portal.page.locator('.blong-portal-menubar')).toBeVisible();
        await storePhoto(page, null);
        await page.reload();
        await expect(portal.page.locator('.blong-portal-menubar')).toBeVisible();
        await expect(page.locator('.blong-account-menu__avatar')).toContainText('TA');
 
        // ── Open the profile tab ────────────────────────────────────────────
        await page.locator('.blong-account-menu__avatar').click();
        await page
            .locator('.blong-account-menu__menu .p-menuitem-link', {hasText: 'Profile'})
            .click();
        const profile = page.locator('.access-profile');
        await expect(profile).toBeVisible();
 
        // ── Choose the picture through the widget ───────────────────────────
        // The input is hidden behind the FileUpload button; setting the files is what a person's
        // own choice does, and it is the only step a test can take for them.
        const photo = profile.locator('[data-testid="access-profile-photo"]');
        await photo.locator('input[type="file"]').setInputFiles({
            name: 'avatar.png',
            mimeType: 'image/png',
            buffer: TINY_PNG,
        });
        // The commit answers the reference, and the widget then holds a value instead of a picker.
        await expect(photo.locator('.blong-asset__current')).toBeVisible({timeout: 60_000});
        await expect(page).toHaveScreenshot('profile-photo-picked.png');
 
        // ── Save it with the form ───────────────────────────────────────────
        await profile.getByRole('button', {name: /save/i}).click();
        await expect(profile.locator('.p-inline-message-success')).toBeVisible();
        await expect(profile.locator('.p-inline-message-error')).toHaveCount(0);
 
        // The person row holds a reference, not bytes and not an external URL.
        await expect
            .poll(async () => storedPhoto(page), {timeout: 30_000})
            .toMatch(/^blob:\/\/party\//);
 
        // The menubar avatar redraws from the saved profile: a picture, decoded.
        const picture = avatarPicture(page);
        await expect(picture).toBeVisible({timeout: 30_000});
        await expect
            .poll(() => picture.evaluate((img: HTMLImageElement) => img.naturalWidth), {
                timeout: 30_000,
            })
            .toBeGreaterThan(0);
        await expect(page).toHaveScreenshot('profile-photo-saved.png');
 
        // A page that starts after the save draws it too — the reference is persisted, not carried.
        await page.reload();
        await expect(portal.page.locator('.blong-portal-menubar')).toBeVisible();
        await expect(avatarPicture(page)).toBeVisible({timeout: 30_000});
 
        // ── Restore: no photograph, initials again ──────────────────────────
        await storePhoto(page, null);
        await page.reload();
        await expect(portal.page.locator('.blong-portal-menubar')).toBeVisible();
        await expect(page.locator('.blong-account-menu__avatar')).toContainText('TA');
        await expect(avatarPicture(page)).toHaveCount(0);
        expect(await storedPhoto(page), 'the person row holds no photograph again').toBeNull();
    });
 
    /**
     * The store's refusal is the whole promise of the allow-list: a file the store will not keep must
     * leave nothing behind — no record, no reserved key, no photograph on the person.
     *
     * A `.png` declared `image/jpeg` is the cheapest refusal to reach from a browser: the extension is
     * known, the type is a real one, and the pair is not a pair (`blob.contentTypeMismatch`).
     */
    test('refuses a file the store will not keep, and leaves nothing behind', async ({
        page,
        portal,
    }) => {
        test.setTimeout(120_000);
 
        await expect(portal.page.locator('.blong-portal-menubar')).toBeVisible();
        await storePhoto(page, null);
        await page.reload();
        await expect(portal.page.locator('.blong-portal-menubar')).toBeVisible();
 
        await page.locator('.blong-account-menu__avatar').click();
        await page
            .locator('.blong-account-menu__menu .p-menuitem-link', {hasText: 'Profile'})
            .click();
        const profile = page.locator('.access-profile');
        await expect(profile).toBeVisible();
 
        const photo = profile.locator('[data-testid="access-profile-photo"]');
        await photo.locator('input[type="file"]').setInputFiles({
            name: 'refused.png',
            mimeType: 'image/jpeg',
            buffer: TINY_PNG,
        });
 
        // The widget says so where the person is looking, and keeps no value.
        await expect(photo.locator('.blong-asset__failure')).toBeVisible({timeout: 30_000});
        await expect(photo.locator('.blong-asset__current')).toHaveCount(0);
 
        // Nothing reached the store under that name.
        const kept = await blobRecords(page, 'party');
        expect(kept.map(record => record.originalFilename)).not.toContain('refused.png');
 
        // And the person still has no photograph.
        expect(await storedPhoto(page)).toBeNull();
    });
});