|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack-ai/objectstack#29019 — a refused write says a sentence in the |
| 5 | + * reader's language, never the response body. |
| 6 | + * |
| 7 | + * A member with read but no create right opened a form link, pressed Submit, |
| 8 | + * and the banner and the toast both read the server's answer verbatim: |
| 9 | + * `Create failed (403): {"error":"You do not have permission…","code": |
| 10 | + * "PERMISSION_DENIED","object":"…"}` — English JSON, with the object's machine |
| 11 | + * name, under a page whose report twin says "You don't have access". |
| 12 | + * |
| 13 | + * The page now reads the refusal the way the record dialog does: a denial is |
| 14 | + * the localized `form.noPermissionToSave` line, a server fault (`5xx`) the |
| 15 | + * localized `form.submitFailed` line, a `4xx` the server's own sentence out of |
| 16 | + * the envelope, and the rows a refusal names are still marked. The fixtures |
| 17 | + * are the envelopes the server answers: the flat body the data routes answer, |
| 18 | + * the runtime dispatcher's wrapped one, and a proxy's markup page. |
| 19 | + */ |
| 20 | + |
| 21 | +import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; |
| 22 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 23 | +import userEvent from '@testing-library/user-event'; |
| 24 | +import { MemoryRouter, Route, Routes } from 'react-router-dom'; |
| 25 | +import { toast } from 'sonner'; |
| 26 | +import { createI18n } from '@object-ui/i18n'; |
| 27 | +import { FormPage } from './FormPage'; |
| 28 | + |
| 29 | +vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } })); |
| 30 | + |
| 31 | +const FIELDS = { |
| 32 | + name: { type: 'text', label: 'Name' }, |
| 33 | + email: { type: 'text', label: 'Email' }, |
| 34 | +}; |
| 35 | + |
| 36 | +/** The QA run's refusal, byte for byte: a `POST /data/:object` without create. */ |
| 37 | +const PERMISSION_DENIED = { |
| 38 | + status: 403, |
| 39 | + body: { |
| 40 | + error: 'You do not have permission to perform this action. Contact your administrator if you need access.', |
| 41 | + code: 'PERMISSION_DENIED', |
| 42 | + object: 'qar8_fsb', |
| 43 | + }, |
| 44 | +}; |
| 45 | + |
| 46 | +let posts = 0; |
| 47 | + |
| 48 | +type Refusal = { status: number; body: unknown }; |
| 49 | + |
| 50 | +/** One answer of the stubbed server; a string body goes out as it is. */ |
| 51 | +function answer(ok: boolean, status: number, body: unknown): Response { |
| 52 | + const text = typeof body === 'string' ? body : JSON.stringify(body); |
| 53 | + return { |
| 54 | + ok, |
| 55 | + status, |
| 56 | + statusText: ok ? 'OK' : 'Refused', |
| 57 | + json: async () => JSON.parse(text), |
| 58 | + text: async () => text, |
| 59 | + } as unknown as Response; |
| 60 | +} |
| 61 | + |
| 62 | +/** The internal route `/forms/:name`, refusing its `POST /data/:object` with `refusal`. */ |
| 63 | +function stubInternal(refusal: Refusal) { |
| 64 | + const view = { |
| 65 | + name: 'qar8_fsb.form', |
| 66 | + object: 'qar8_fsb', |
| 67 | + viewKind: 'form', |
| 68 | + label: 'Feedback', |
| 69 | + config: { type: 'simple', sections: [{ fields: ['name', 'email'] }] }, |
| 70 | + }; |
| 71 | + const object = { name: 'qar8_fsb', label: 'Feedback', fields: FIELDS }; |
| 72 | + vi.stubGlobal( |
| 73 | + 'fetch', |
| 74 | + vi.fn(async (url: string, init?: RequestInit) => { |
| 75 | + if (init?.method === 'POST') { |
| 76 | + posts += 1; |
| 77 | + return answer(false, refusal.status, refusal.body); |
| 78 | + } |
| 79 | + if (String(url).includes('/meta/view/')) return answer(true, 200, view); |
| 80 | + if (String(url).includes('/meta/object/')) return answer(true, 200, object); |
| 81 | + throw new Error(`unstubbed fetch: ${url}`); |
| 82 | + }), |
| 83 | + ); |
| 84 | + return render( |
| 85 | + <MemoryRouter initialEntries={['/forms/qar8_fsb.form']}> |
| 86 | + <Routes> |
| 87 | + <Route path="/forms/:name" element={<FormPage mode="internal" />} /> |
| 88 | + </Routes> |
| 89 | + </MemoryRouter>, |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +/** The public route `/f/:slug`, refusing its `POST /forms/:slug/submit` with `refusal`. */ |
| 94 | +function stubPublic(refusal: Refusal) { |
| 95 | + const payload = { |
| 96 | + slug: 'feedback', |
| 97 | + object: 'qar8_fsb', |
| 98 | + label: 'Feedback', |
| 99 | + form: { type: 'simple', sections: [{ fields: ['name', 'email'] }] }, |
| 100 | + objectSchema: { name: 'qar8_fsb', fields: FIELDS }, |
| 101 | + }; |
| 102 | + vi.stubGlobal( |
| 103 | + 'fetch', |
| 104 | + vi.fn(async (url: string, init?: RequestInit) => { |
| 105 | + if (init?.method === 'POST') { |
| 106 | + posts += 1; |
| 107 | + return answer(false, refusal.status, refusal.body); |
| 108 | + } |
| 109 | + if (!String(url).includes('/forms/')) throw new Error(`unstubbed fetch: ${url}`); |
| 110 | + return answer(true, 200, payload); |
| 111 | + }), |
| 112 | + ); |
| 113 | + return render( |
| 114 | + <MemoryRouter initialEntries={['/f/feedback']}> |
| 115 | + <Routes> |
| 116 | + <Route path="/f/:slug" element={<FormPage mode="public" />} /> |
| 117 | + </Routes> |
| 118 | + </MemoryRouter>, |
| 119 | + ); |
| 120 | +} |
| 121 | + |
| 122 | +async function fillAndSubmit(submitName: RegExp | string = /Submit/) { |
| 123 | + await userEvent.type(await screen.findByLabelText('Name'), 'Ada'); |
| 124 | + await userEvent.click(screen.getByRole('button', { name: submitName })); |
| 125 | +} |
| 126 | + |
| 127 | +/** The banner's text, once the refusal has drawn it. */ |
| 128 | +async function bannerText(): Promise<string> { |
| 129 | + await waitFor(() => expect(document.querySelector('[id^="form-error:"]')).not.toBeNull()); |
| 130 | + return document.querySelector('[id^="form-error:"]')?.textContent ?? ''; |
| 131 | +} |
| 132 | + |
| 133 | +/** The one refusal toast's content, as a string. */ |
| 134 | +function toastText(): string { |
| 135 | + expect(vi.mocked(toast.error)).toHaveBeenCalledTimes(1); |
| 136 | + return String(vi.mocked(toast.error).mock.calls[0][0]); |
| 137 | +} |
| 138 | + |
| 139 | +/** Neither the body nor the transport's prose reached the reader. */ |
| 140 | +function expectNoRawBody(text: string, status: number) { |
| 141 | + expect(text).not.toContain('{'); |
| 142 | + expect(text).not.toContain(String(status)); |
| 143 | + expect(text).not.toMatch(/failed \(/); |
| 144 | +} |
| 145 | + |
| 146 | +beforeEach(() => { |
| 147 | + posts = 0; |
| 148 | + vi.mocked(toast.error).mockClear(); |
| 149 | + createI18n({ defaultLanguage: 'en', detectBrowserLanguage: false }); |
| 150 | +}); |
| 151 | + |
| 152 | +afterEach(() => { |
| 153 | + vi.unstubAllGlobals(); |
| 154 | +}); |
| 155 | + |
| 156 | +describe('#29019 — a denied create reads the localized denial', () => { |
| 157 | + it('internal route: the 403 PERMISSION_DENIED banner and toast say the same localized sentence', async () => { |
| 158 | + stubInternal(PERMISSION_DENIED); |
| 159 | + await fillAndSubmit(); |
| 160 | + |
| 161 | + const banner = await bannerText(); |
| 162 | + expect(posts).toBe(1); |
| 163 | + expect(banner).toBe("You don't have permission to save this record."); |
| 164 | + expect(toastText()).toBe(banner); |
| 165 | + expectNoRawBody(banner, 403); |
| 166 | + // No row is to blame for a denial. |
| 167 | + expect(document.querySelectorAll('[id$="-error"]')).toHaveLength(0); |
| 168 | + }); |
| 169 | + |
| 170 | + it('zh: the same refusal reads in the reader\'s language', async () => { |
| 171 | + createI18n({ defaultLanguage: 'zh', detectBrowserLanguage: false }); |
| 172 | + stubInternal(PERMISSION_DENIED); |
| 173 | + await fillAndSubmit('提交'); |
| 174 | + |
| 175 | + const banner = await bannerText(); |
| 176 | + expect(banner).toBe('您没有权限保存这条记录。'); |
| 177 | + expect(toastText()).toBe(banner); |
| 178 | + expectNoRawBody(banner, 403); |
| 179 | + }); |
| 180 | + |
| 181 | + it('public route: the dispatcher\'s wrapped 403 envelope reads the same denial', async () => { |
| 182 | + stubPublic({ |
| 183 | + status: 403, |
| 184 | + body: { |
| 185 | + success: false, |
| 186 | + error: { code: 'PERMISSION_DENIED', message: 'Access denied for qar8_fsb', httpStatus: 403 }, |
| 187 | + }, |
| 188 | + }); |
| 189 | + await fillAndSubmit(); |
| 190 | + |
| 191 | + const banner = await bannerText(); |
| 192 | + expect(banner).toBe("You don't have permission to save this record."); |
| 193 | + expect(toastText()).toBe(banner); |
| 194 | + expectNoRawBody(banner, 403); |
| 195 | + }); |
| 196 | + |
| 197 | + it('a sentence the producer marked for the end user is said as written', async () => { |
| 198 | + stubInternal({ |
| 199 | + status: 403, |
| 200 | + body: { ...PERMISSION_DENIED.body, userMessage: 'Feedback closes on Fridays.' }, |
| 201 | + }); |
| 202 | + await fillAndSubmit(); |
| 203 | + |
| 204 | + expect(await bannerText()).toBe('Feedback closes on Fridays.'); |
| 205 | + expect(toastText()).toBe('Feedback closes on Fridays.'); |
| 206 | + }); |
| 207 | +}); |
| 208 | + |
| 209 | +describe('#29019 — every other refusal', () => { |
| 210 | + it('a 400 with field details still marks its fields, and says no body', async () => { |
| 211 | + stubInternal({ |
| 212 | + status: 400, |
| 213 | + body: { |
| 214 | + error: 'Validation failed', |
| 215 | + code: 'VALIDATION_FAILED', |
| 216 | + fields: [{ field: 'email', code: 'required', message: 'Email is required' }], |
| 217 | + }, |
| 218 | + }); |
| 219 | + await fillAndSubmit(); |
| 220 | + |
| 221 | + const email = screen.getByLabelText('Email'); |
| 222 | + await waitFor(() => expect(email).toHaveAttribute('aria-invalid', 'true')); |
| 223 | + expect(document.getElementById(email.getAttribute('aria-describedby') ?? '')?.textContent).toContain( |
| 224 | + 'Email is required', |
| 225 | + ); |
| 226 | + expect(screen.getByLabelText('Name')).not.toHaveAttribute('aria-invalid', 'true'); |
| 227 | + const banner = await bannerText(); |
| 228 | + expect(banner).toBe('Validation failed'); |
| 229 | + expect(toastText()).toBe(banner); |
| 230 | + expectNoRawBody(banner, 400); |
| 231 | + }); |
| 232 | + |
| 233 | + it('a 500 reads the localized generic sentence, not the server fault', async () => { |
| 234 | + stubInternal({ |
| 235 | + status: 500, |
| 236 | + body: { error: 'SQLITE_ERROR: no such table: qar8_fsb', code: 'INTERNAL_ERROR' }, |
| 237 | + }); |
| 238 | + await fillAndSubmit(); |
| 239 | + |
| 240 | + const banner = await bannerText(); |
| 241 | + expect(banner).toBe('Could not save. Please try again.'); |
| 242 | + expect(toastText()).toBe(banner); |
| 243 | + expectNoRawBody(banner, 500); |
| 244 | + expect(banner).not.toContain('SQLITE'); |
| 245 | + }); |
| 246 | + |
| 247 | + it('a proxy\'s markup page reads the localized generic sentence', async () => { |
| 248 | + stubPublic({ status: 502, body: '<html><body><h1>502 Bad Gateway</h1></body></html>' }); |
| 249 | + await fillAndSubmit(); |
| 250 | + |
| 251 | + const banner = await bannerText(); |
| 252 | + expect(banner).toBe('Could not save. Please try again.'); |
| 253 | + expect(toastText()).toBe(banner); |
| 254 | + expect(banner).not.toContain('<'); |
| 255 | + }); |
| 256 | +}); |
0 commit comments