import type { INestApplication } from '@nestjs/common'; import { api, createApp, insertTiles, resetDatabase, signIn, signInAdmin } from './helpers.js'; const slugs = (body: { data: { slug: string }[] }) => body.data.map((tile) => tile.slug); describe('Admin-only tiles (e2e)', () => { let app: INestApplication; let client: Record; let admin: Record; beforeAll(async () => { app = await createApp(); }); afterAll(() => app.close()); beforeEach(async () => { await resetDatabase(); client = await signIn('user_client'); admin = await signInAdmin('user_admin', app); await insertTiles( { slug: 'demo', name: 'Demo Oak', surfaces: ['floor'], collection: 'Demo' }, { slug: 'emb-1', name: 'Tulip Garden', surfaces: ['walls'], collection: 'Embellence', visibility: 'admin', brand: 'embellence', organization: 'embellence-group' }, { slug: 'emb-2', name: 'Dawn Forest', surfaces: ['walls'], collection: 'Embellence', visibility: 'admin', brand: 'embellence', organization: 'embellence-group' }, { slug: 'gone', name: 'Gone', surfaces: ['walls'], collection: 'Old', deleted: true }, ); }); it('hides admin tiles from clients in lists, totals and search', async () => { const list = await api(app).get('/api/tiles').set(client).expect(200); expect(slugs(list.body)).toEqual(['demo']); expect(list.body.meta.total).toBe(1); expect((await api(app).get('/api/tiles').query({ q: 'tulip' }).set(client)).body.meta.total).toBe(0); expect((await api(app).get('/api/tiles').query({ collection: 'Embellence' }).set(client)).body.meta.total).toBe(0); expect((await api(app).get('/api/tiles').query({ surface: 'walls' }).set(client)).body.meta.total).toBe(0); }); it('hides admin tiles and deleted tiles from client facets', async () => { const facets = await api(app).get('/api/tiles/facets').set(client).expect(200); expect(facets.body.data).toEqual({ total: 1, surfaces: [{ value: 'floor', count: 1 }], collections: [{ value: 'Demo', count: 1 }] }); }); it('answers a client lookup of an admin tile like a missing tile', async () => { const response = await api(app).get('/api/tiles/emb-1').set(client).expect(404); expect(response.body.error.code).toBe('TILE_NOT_FOUND'); }); it('shows admins every tile with its brand, organization and visibility', async () => { const list = await api(app).get('/api/tiles').set(admin).expect(200); expect(slugs(list.body).sort()).toEqual(['demo', 'emb-1', 'emb-2']); const tile = (await api(app).get('/api/tiles/emb-1').set(admin).expect(200)).body.data; expect(tile).toMatchObject({ brand: 'embellence', organization: 'embellence-group', visibility: 'admin' }); const facets = (await api(app).get('/api/tiles/facets').set(admin)).body.data; expect(facets.total).toBe(3); expect(facets.collections).toEqual([ { value: 'Demo', count: 1 }, { value: 'Embellence', count: 2 }, ]); }); it('hides admin tiles again when an admin is demoted', async () => { const { sql } = await import('./helpers.js'); await sql("UPDATE users SET role = 'user' WHERE clerk_user_id = 'user_admin'"); expect(slugs((await api(app).get('/api/tiles').set(admin)).body)).toEqual(['demo']); }); });