|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#29180] `GET /meta/_drafts` refuses a `type` that names no metadata type. |
| 5 | + * |
| 6 | + * `?type=dashbord` (or `dashboardes`, `viewes`, `nosuchtype`) answered |
| 7 | + * `200 {"drafts":[]}`, a confident "nothing pending", where `GET /meta/dashbord` |
| 8 | + * refuses the same name `400`. The route now asks the list door's own verdict |
| 9 | + * (`refuseUnknownMetaListType`), so this file pins the drafts answer EQUAL to |
| 10 | + * the list door's answer for the same name on the same server — one code, one |
| 11 | + * sentence — and that `listDrafts` was never called. The controls: a known type |
| 12 | + * in either spelling still reaches `listDrafts` and answers `200` empty. |
| 13 | + * |
| 14 | + * The package half lives in `listDrafts` itself |
| 15 | + * (`@objectstack/metadata-protocol`'s `protocol-drafts-unknown-package.test.ts`); |
| 16 | + * the last case here pins that its classified refusal leaves this route as a |
| 17 | + * 400 with its code. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, it, expect, vi } from 'vitest'; |
| 21 | +import { RestServer } from './rest-server.js'; |
| 22 | + |
| 23 | +function mockRes() { |
| 24 | + const res: any = { |
| 25 | + statusCode: 200, |
| 26 | + json: vi.fn(function (this: any) { return this; }), |
| 27 | + status: vi.fn(function (this: any, code: number) { this.statusCode = code; return this; }), |
| 28 | + header: vi.fn(function (this: any) { return this; }), |
| 29 | + setHeader: vi.fn(function (this: any) { return this; }), |
| 30 | + send: vi.fn(function (this: any) { return this; }), |
| 31 | + end: vi.fn(function (this: any) { return this; }), |
| 32 | + }; |
| 33 | + return res; |
| 34 | +} |
| 35 | + |
| 36 | +function boot(listDrafts = vi.fn(async (_r: unknown) => ({ drafts: [] as unknown[] }))) { |
| 37 | + const protocol: any = { |
| 38 | + getDiscovery: vi.fn().mockResolvedValue({ version: 'v0', routes: { data: '', metadata: '', ui: '', auth: '/auth' } }), |
| 39 | + getMetaTypes: vi.fn().mockResolvedValue({ types: ['view', 'dashboard', 'object'] }), |
| 40 | + getMetaItems: vi.fn().mockResolvedValue({ items: [] }), |
| 41 | + listDrafts, |
| 42 | + }; |
| 43 | + const server = { get: vi.fn(), post: vi.fn(), put: vi.fn(), delete: vi.fn(), patch: vi.fn(), use: vi.fn() }; |
| 44 | + const rest = new RestServer(server as any, protocol as any, { api: { requireAuth: false } } as any); |
| 45 | + // `isSystem` clears the authoring gate that runs before the query is read. |
| 46 | + (rest as any).resolveExecCtx = async () => ({ isSystem: true, userId: 'u1' }); |
| 47 | + rest.registerRoutes(); |
| 48 | + const route = (path: string) => (rest as any).getRoutes().find((r: any) => r.method === 'GET' && r.path === path); |
| 49 | + const drive = async (path: string, params: Record<string, string>, query: Record<string, unknown>) => { |
| 50 | + const res = mockRes(); |
| 51 | + await route(path).handler({ method: 'GET', path, params, query, headers: {}, body: {} }, res); |
| 52 | + return { status: res.statusCode, body: res.json.mock.calls.at(-1)?.[0] }; |
| 53 | + }; |
| 54 | + return { protocol, drive }; |
| 55 | +} |
| 56 | + |
| 57 | +describe('[#29180] GET /meta/_drafts — a type filter that names no type', () => { |
| 58 | + it.each(['dashbord', 'dashboardes', 'viewes', 'nosuchtype'])( |
| 59 | + '`?type=%s` answers the list door\'s own 400, and listDrafts is never called', |
| 60 | + async (type) => { |
| 61 | + const { protocol, drive } = boot(); |
| 62 | + const drafts = await drive('/api/v1/meta/_drafts', {}, { type }); |
| 63 | + const list = await drive('/api/v1/meta/:type', { type }, {}); |
| 64 | + expect(drafts.status).toBe(400); |
| 65 | + expect(drafts.body).toEqual(list.body); |
| 66 | + expect(JSON.stringify(drafts.body)).toContain('INVALID_REQUEST'); |
| 67 | + expect(JSON.stringify(drafts.body)).toContain(`'${type}' is not a metadata type.`); |
| 68 | + expect(protocol.listDrafts).not.toHaveBeenCalled(); |
| 69 | + }, |
| 70 | + ); |
| 71 | + |
| 72 | + it.each(['dashboard', 'dashboards', 'view'])( |
| 73 | + 'a known type (`?type=%s`) still reaches listDrafts as sent and answers 200 empty', |
| 74 | + async (type) => { |
| 75 | + const { protocol, drive } = boot(); |
| 76 | + const answer = await drive('/api/v1/meta/_drafts', {}, { type }); |
| 77 | + expect(answer).toEqual({ status: 200, body: { drafts: [] } }); |
| 78 | + expect(protocol.listDrafts).toHaveBeenCalledWith({ packageId: undefined, type }); |
| 79 | + }, |
| 80 | + ); |
| 81 | + |
| 82 | + it('a known package with no drafts and no type answers 200 empty', async () => { |
| 83 | + const { protocol, drive } = boot(); |
| 84 | + expect(await drive('/api/v1/meta/_drafts', {}, { packageId: 'com.acme' })).toEqual({ status: 200, body: { drafts: [] } }); |
| 85 | + expect(protocol.listDrafts).toHaveBeenCalledWith({ packageId: 'com.acme', type: undefined }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('the package refusal listDrafts throws leaves the route a 400 INVALID_REQUEST', async () => { |
| 89 | + const refusal = Object.assign(new Error("'com.qa.nosuch' is not an installed package, and no pending draft is bound to it."), { |
| 90 | + code: 'INVALID_REQUEST', status: 400, |
| 91 | + }); |
| 92 | + const { drive } = boot(vi.fn(async () => { throw refusal; })); |
| 93 | + const answer = await drive('/api/v1/meta/_drafts', {}, { packageId: 'com.qa.nosuch' }); |
| 94 | + expect(answer.status).toBe(400); |
| 95 | + expect(JSON.stringify(answer.body)).toContain('INVALID_REQUEST'); |
| 96 | + expect(JSON.stringify(answer.body)).toContain('is not an installed package'); |
| 97 | + }); |
| 98 | +}); |
0 commit comments