feat(admin): mail preview field with sandboxed HTML and test send
- Add MailPreviewField (POST /mail/preview + /mail/test-send, empty sandbox iframe) - Register mailpreview as valueless/group-labelled; SettingsFormView FORM_VALUES/FORM_LOCALE - Smoke tests for srcdoc, sequence guard, 422 keep-last, and 429 Retry-After Plan 35-03, task 1 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -108,6 +108,12 @@
|
||||
},
|
||||
"rowStateDisabled": {
|
||||
"other": "Inactive"
|
||||
},
|
||||
"rowStateCustomized": {
|
||||
"other": "Customized"
|
||||
},
|
||||
"rowStateOutdated": {
|
||||
"other": "Outdated"
|
||||
}
|
||||
},
|
||||
"meta": {
|
||||
|
||||
4
admin/tests/fixtures/roster.list-schema.json
vendored
4
admin/tests/fixtures/roster.list-schema.json
vendored
@@ -37,7 +37,9 @@
|
||||
"selected": { "other": ":count selected" },
|
||||
"rowStateDeleted": { "other": "Deleted" },
|
||||
"rowStateNegative": { "other": "Blocked" },
|
||||
"rowStateDisabled": { "other": "Not active" }
|
||||
"rowStateDisabled": { "other": "Not active" },
|
||||
"rowStateCustomized": { "other": "Customized" },
|
||||
"rowStateOutdated": { "other": "Outdated" }
|
||||
},
|
||||
"meta": { "locale": "en" }
|
||||
},
|
||||
|
||||
@@ -39,7 +39,9 @@
|
||||
"selected": { "other": "Selected :count" },
|
||||
"rowStateDeleted": { "other": "Deleted" },
|
||||
"rowStateNegative": { "other": "Blocked" },
|
||||
"rowStateDisabled": { "other": "Inactive" }
|
||||
"rowStateDisabled": { "other": "Inactive" },
|
||||
"rowStateCustomized": { "other": "Customized" },
|
||||
"rowStateOutdated": { "other": "Outdated" }
|
||||
},
|
||||
"meta": { "locale": "en" }
|
||||
},
|
||||
|
||||
211
admin/tests/form/mail-smoke.test.ts
Normal file
211
admin/tests/form/mail-smoke.test.ts
Normal file
@@ -0,0 +1,211 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils'
|
||||
import { computed, ref } from 'vue'
|
||||
import type { FormField } from '../../src/api/types'
|
||||
import { setBundle } from '../../src/app/i18n'
|
||||
import MailPreviewField from '../../src/components/form/fields/MailPreviewField.vue'
|
||||
import { FORM_VALUES } from '../../src/components/form/formContext'
|
||||
import { setCurrentUser } from '../../src/state/useAuth'
|
||||
import { clearToasts, toasts } from '../../src/state/useToasts'
|
||||
import { API, mockApi, profile, requestsTo, resetState, type Reply, wait } from '../helpers'
|
||||
import { langFixture } from '../fixtures/typed'
|
||||
|
||||
const mailPreviewBundle = {
|
||||
...langFixture.data,
|
||||
'backend::lang.mail.preview.html': { other: 'HTML' },
|
||||
'backend::lang.mail.preview.text': { other: 'Plain text' },
|
||||
'backend::lang.mail.preview.subject': { other: 'Subject' },
|
||||
'backend::lang.mail.preview.frame_title': { other: 'Preview of the HTML message' },
|
||||
'backend::lang.mail.preview.rendering': { other: 'Rendering preview…' },
|
||||
'backend::lang.mail.preview.empty': { other: 'Nothing to preview yet. Write the message first.' },
|
||||
'backend::lang.mail.preview.failed': {
|
||||
other: 'The draft cannot be rendered: :message The preview shows the last version that rendered.',
|
||||
},
|
||||
'backend::lang.mail.preview.failed_first': { other: 'The draft cannot be rendered: :message' },
|
||||
'backend::lang.mail.preview.unavailable': { other: 'Preview unavailable. It updates again with your next edit.' },
|
||||
'backend::lang.mail.preview.sample_note': {
|
||||
other: 'Variables are filled with sample values. A variable without a sample shows as [name].',
|
||||
},
|
||||
'backend::lang.mail.preview.sample_content_note': { other: 'Previewed with sample content.' },
|
||||
'backend::lang.mail.preview.test_send': { other: 'Send test to me' },
|
||||
'backend::lang.mail.preview.test_sending': { other: 'Sending…' },
|
||||
'backend::lang.mail.preview.test_hint': {
|
||||
other: 'Sends the current draft, unsaved changes included, to :email.',
|
||||
},
|
||||
'backend::lang.mail.preview.test_sent': { other: 'Test message sent to :email.' },
|
||||
'backend::lang.mail.preview.test_limited': {
|
||||
other: 'You have sent too many test messages. Try again in :seconds s.',
|
||||
},
|
||||
'backend::lang.mail.preview.test_invalid': { other: 'Fix the draft before sending a test: :message' },
|
||||
'backend::lang.mail.preview.test_failed': {
|
||||
other: 'The test message could not be sent. Check the mail settings and the application log.',
|
||||
},
|
||||
}
|
||||
|
||||
const PREVIEW = `${API}/mail/preview`
|
||||
const TEST_SEND = `${API}/mail/test-send`
|
||||
|
||||
function previewField(mode = 'template'): FormField {
|
||||
return { name: '_preview', label: 'Preview', type: 'mailpreview', mode } as FormField
|
||||
}
|
||||
|
||||
function okPreview(html = '<p>Hello</p>', subject = 'Welcome', text = 'Hello'): Reply {
|
||||
return { body: { data: { html, subject, text }, meta: {} } }
|
||||
}
|
||||
|
||||
function mountPreview(values: Record<string, unknown>, field: FormField = previewField(), recordId: number | null = 3) {
|
||||
const valuesRef = ref(values)
|
||||
return mount(MailPreviewField, {
|
||||
props: { field, modelValue: null, controlId: 'f-preview', recordId },
|
||||
global: {
|
||||
provide: {
|
||||
[FORM_VALUES as symbol]: computed(() => valuesRef.value),
|
||||
},
|
||||
},
|
||||
attachTo: document.body,
|
||||
})
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
resetState()
|
||||
setBundle(mailPreviewBundle, 'en')
|
||||
clearToasts()
|
||||
setCurrentUser(profile.data)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
document.body.innerHTML = ''
|
||||
clearToasts()
|
||||
})
|
||||
|
||||
enableAutoUnmount(afterEach)
|
||||
|
||||
describe('MailPreviewField', () => {
|
||||
it('binds a sandboxed srcdoc iframe to the server html only', async () => {
|
||||
const calls = mockApi({ [`POST ${PREVIEW}`]: okPreview('<p>Rendered</p>') })
|
||||
const wrapper = mountPreview({ body: 'hi' })
|
||||
await flushPromises()
|
||||
|
||||
const frame = wrapper.find('[data-mail-preview-frame]')
|
||||
expect(frame.exists()).toBe(true)
|
||||
expect(frame.attributes('sandbox')).toBe('')
|
||||
expect(frame.attributes('src')).toBeUndefined()
|
||||
expect(frame.attributes('srcdoc')).toBe('<p>Rendered</p>')
|
||||
expect(requestsTo(calls, 'POST', PREVIEW)).toHaveLength(1)
|
||||
const body = await requestsTo(calls, 'POST', PREVIEW)[0]!.clone().json()
|
||||
expect(body).toEqual({ mode: 'template', id: 3, values: { body: 'hi' } })
|
||||
})
|
||||
|
||||
it('drops a stale preview answer via the sequence guard', async () => {
|
||||
let resolveFirst!: (reply: Reply) => void
|
||||
const first = new Promise<Reply>((resolve) => {
|
||||
resolveFirst = resolve
|
||||
})
|
||||
let calls = 0
|
||||
mockApi({
|
||||
[`POST ${PREVIEW}`]: async () => {
|
||||
calls++
|
||||
if (calls === 1) {
|
||||
return first
|
||||
}
|
||||
return okPreview('<p>Second</p>', 'Second')
|
||||
},
|
||||
})
|
||||
const values = ref({ body: 'one' })
|
||||
const wrapper = mount(MailPreviewField, {
|
||||
props: { field: previewField(), modelValue: null, controlId: 'f-preview', recordId: 1 },
|
||||
global: { provide: { [FORM_VALUES as symbol]: computed(() => values.value) } },
|
||||
attachTo: document.body,
|
||||
})
|
||||
await flushPromises()
|
||||
values.value = { body: 'two' }
|
||||
await wait(520)
|
||||
await flushPromises()
|
||||
resolveFirst(okPreview('<p>First</p>', 'First'))
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.find('[data-mail-preview-frame]').attributes('srcdoc')).toBe('<p>Second</p>')
|
||||
expect(wrapper.find('[data-mail-preview-subject]').text()).toContain('Second')
|
||||
})
|
||||
|
||||
it('keeps the previous srcdoc after a 422 and shows the failed line', async () => {
|
||||
let n = 0
|
||||
mockApi({
|
||||
[`POST ${PREVIEW}`]: () => {
|
||||
n++
|
||||
if (n === 1) {
|
||||
return okPreview('<p>Good</p>')
|
||||
}
|
||||
return {
|
||||
status: 422,
|
||||
body: {
|
||||
error: {
|
||||
code: 'validation_failed',
|
||||
message: 'Validation failed',
|
||||
details: { field: 'body', rule: 'unsafe', message: 'script refused' },
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
})
|
||||
const values = ref({ body: 'ok' })
|
||||
const wrapper = mount(MailPreviewField, {
|
||||
props: { field: previewField(), modelValue: null, controlId: 'f-preview', recordId: 1 },
|
||||
global: { provide: { [FORM_VALUES as symbol]: computed(() => values.value) } },
|
||||
attachTo: document.body,
|
||||
})
|
||||
await flushPromises()
|
||||
expect(wrapper.find('[data-mail-preview-frame]').attributes('srcdoc')).toBe('<p>Good</p>')
|
||||
|
||||
values.value = { body: 'bad' }
|
||||
await wait(520)
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.find('[data-mail-preview-frame]').attributes('srcdoc')).toBe('<p>Good</p>')
|
||||
expect(wrapper.find('[data-mail-preview-error]').text()).toContain('script refused')
|
||||
})
|
||||
|
||||
it('disables the test-send button after a 429 with Retry-After', async () => {
|
||||
mockApi({
|
||||
[`POST ${PREVIEW}`]: okPreview(),
|
||||
[`POST ${TEST_SEND}`]: {
|
||||
status: 429,
|
||||
headers: { 'Retry-After': '30' },
|
||||
body: { error: { code: 'rate_limited', message: 'Too many', details: {} } },
|
||||
},
|
||||
})
|
||||
const wrapper = mountPreview({ body: 'hi' })
|
||||
await flushPromises()
|
||||
|
||||
await wrapper.find('[data-mail-preview-test-send]').trigger('click')
|
||||
await flushPromises()
|
||||
|
||||
const button = wrapper.find('[data-mail-preview-test-send]')
|
||||
expect(button.attributes('disabled')).toBeDefined()
|
||||
expect(wrapper.find('[data-mail-preview-test-error]').text()).toContain('30')
|
||||
})
|
||||
|
||||
it('sends nothing for an empty draft', async () => {
|
||||
const calls = mockApi({ [`POST ${PREVIEW}`]: okPreview() })
|
||||
const wrapper = mountPreview({ body: ' ' })
|
||||
await flushPromises()
|
||||
|
||||
expect(requestsTo(calls, 'POST', PREVIEW)).toHaveLength(0)
|
||||
expect(wrapper.find('[data-mail-preview-empty]').exists()).toBe(true)
|
||||
expect(wrapper.find('[data-mail-preview-frame]').exists()).toBe(false)
|
||||
})
|
||||
|
||||
it('toasts the returned email on a successful test send', async () => {
|
||||
mockApi({
|
||||
[`POST ${PREVIEW}`]: okPreview(),
|
||||
[`POST ${TEST_SEND}`]: { body: { data: { email: 'dev@example.test' }, meta: {} } },
|
||||
})
|
||||
const wrapper = mountPreview({ body: 'hi' })
|
||||
await flushPromises()
|
||||
await wrapper.find('[data-mail-preview-test-send]').trigger('click')
|
||||
await flushPromises()
|
||||
|
||||
expect(toasts.value[0]?.text).toContain('dev@example.test')
|
||||
})
|
||||
})
|
||||
@@ -37,12 +37,17 @@ export const profile = {
|
||||
export interface Reply {
|
||||
status?: number
|
||||
body: unknown
|
||||
/** Extra response headers (for example Retry-After on a 429). */
|
||||
headers?: Record<string, string>
|
||||
}
|
||||
|
||||
export type Route = Reply | ((request: Request) => Reply | Promise<Reply>)
|
||||
|
||||
export function json(status: number, body: unknown): Response {
|
||||
return new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } })
|
||||
export function json(status: number, body: unknown, headers: Record<string, string> = {}): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { 'Content-Type': 'application/json', ...headers },
|
||||
})
|
||||
}
|
||||
|
||||
export const notFound = { error: { code: 'not_found', message: 'Not found', details: {} } }
|
||||
@@ -58,7 +63,7 @@ export function mockApi(routes: Record<string, Route>): Request[] {
|
||||
return json(404, notFound)
|
||||
}
|
||||
const reply = typeof route === 'function' ? await route(request.clone()) : route
|
||||
return json(reply.status ?? 200, reply.body)
|
||||
return json(reply.status ?? 200, reply.body, reply.headers)
|
||||
})
|
||||
return calls
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user