Repository navigation
Expand file tree
/
Copy pathpublishing.test.ts
More file actions
517 lines (420 loc) · 19.5 KB
/
Copy pathpublishing.test.ts
File metadata and controls
517 lines (420 loc) · 19.5 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import { APIRequestContext, Page } from '@playwright/test';
import { LoginPage, PostEditorPage, PostsPage } from '@/admin-pages';
import {
PostFactory,
buildLexicalParagraph,
createMemberFactory,
createPostFactory,
} from '@/data-factory';
import { PostPage } from '@/helpers/pages';
import { SettingsService } from '@/helpers/services/settings/settings-service';
import { expect, test, withIsolatedPage } from '@/helpers/playwright';
/**
* The publishing journeys through the post editor. Each case pins the
* state the server ends up holding, not the layout the editor reaches it
* through. The draft autosave journeys stay in `editor.test.ts`.
*/
const POSTS_API = '/ghost/api/admin/posts/';
const PAGES_API = '/ghost/api/admin/pages/';
async function readPost(page: Page, postId: string) {
const response = await page.request.get(
`${POSTS_API}${postId}/?formats=lexical&include=email,newsletter`,
);
expect(response.status()).toBe(200);
const {
posts: [post],
} = await response.json();
return post;
}
function waitForPostSave(page: Page, postId: string, api = POSTS_API) {
return page.waitForResponse(
(response) =>
response.request().method() === 'PUT' &&
response.url().includes(`${api}${postId}/`) &&
response.status() === 200,
);
}
async function readPage(page: Page, pageId: string) {
const response = await page.request.get(`${PAGES_API}${pageId}/`);
expect(response.status()).toBe(200);
const {
pages: [found],
} = await response.json();
return found;
}
/** A draft page, opened in the editor. */
async function openDraftPage(page: Page, title: string) {
const response = await page.request.post(PAGES_API, {
data: { pages: [{ title, status: 'draft' }] },
});
expect(response.status()).toBe(201);
const {
pages: [draft],
} = await response.json();
const editor = new PostEditorPage(page);
await page.goto(`/ghost/#/editor/page/${draft.id}`);
await editor.titleInput.waitFor({ state: 'visible' });
return { editor, draft: draft as { id: string; slug: string } };
}
async function getNewsletters(request: APIRequestContext): Promise<{ id: string }[]> {
const response = await request.get('/ghost/api/admin/newsletters/?status=active&limit=all');
const data = await response.json();
return data.newsletters.map((newsletter: { id: string }) => ({ id: newsletter.id }));
}
/** A member on every active newsletter, so email is on offer in the publish flow. */
async function addSubscribedMember(page: Page, email: string) {
const memberFactory = createMemberFactory(page.request);
const newsletters = await getNewsletters(page.request);
await memberFactory.create({ email, name: 'React publishing member', newsletters });
}
async function startDraft(page: Page, { title, body }: { title: string; body: string }) {
const postsPage = new PostsPage(page);
await postsPage.goto();
await postsPage.newPostButton.click();
const editor = new PostEditorPage(page);
await Promise.all([
page.waitForResponse(
(response) =>
response.request().method() === 'POST' &&
response.url().includes(POSTS_API) &&
response.status() === 201,
),
editor.createDraft({ title, body }),
]);
return { editor, postId: await editor.getPostId(), postsPage };
}
test.describe('Ghost Admin - Publishing', () => {
let postFactory: PostFactory;
test.beforeEach(async ({ page }) => {
postFactory = createPostFactory(page.request);
});
test('draft - schedules, lists as scheduled, and unschedules back to a draft', async ({
page,
}) => {
// A schedule, a trip through the list and a revert do not fit the default
// budget
test.setTimeout(90000);
const title = `react-schedule-${Date.now()}`;
const body = 'This is my scheduled post body.';
const { editor, postId, postsPage } = await startDraft(page, { title, body });
await editor.publishFlow.open();
await expect(editor.publishFlow.optionsStep).toBeVisible();
// No date or time: the default schedule is ten minutes out
await editor.publishFlow.schedule({});
await Promise.all([waitForPostSave(page, postId), editor.publishFlow.confirm()]);
await expect(page).toHaveURL('/ghost/#/posts');
const scheduled = await readPost(page, postId);
expect(scheduled.status).toBe('scheduled');
// The list consumes the handoff and opens its celebration over itself.
await expect(postsPage.publishCelebration).toBeVisible();
await postsPage.closePublishCelebration();
await postsPage.waitForPageToFullyLoad();
await expect(postsPage.getPostByTitle(title)).toContainText('Scheduled');
await postsPage.getPostByTitle(title).click();
await expect(editor.postStatus).toContainText('Scheduled');
await editor.revertToDraft();
await expect(editor.postStatus).toContainText('Draft');
const reverted = await readPost(page, postId);
expect(reverted.status).toBe('draft');
expect(reverted.published_at).toBeNull();
});
test('published - an edit reaches the server and the site when Update is clicked', async ({
page,
}) => {
const created = await postFactory.create({
title: `react-update-${Date.now()}`,
status: 'published',
// One paragraph: the site's `articleBody` locator matches each paragraph
lexical: buildLexicalParagraph('Published before the edit.'),
});
const addition = 'Edited after publishing.';
const editor = new PostEditorPage(page);
await editor.gotoPost(created.id);
await expect(editor.lexicalEditor).toBeVisible();
await editor.appendToBody(` ${addition}`);
// A published post never autosaves, so this click is the only save
await Promise.all([waitForPostSave(page, created.id), editor.publishSaveButton.click()]);
const updated = await readPost(page, created.id);
expect(updated.status).toBe('published');
expect(updated.lexical).toContain(addition);
const frontendPage = await page.context().newPage();
const publicPage = new PostPage(frontendPage);
await publicPage.gotoPost(created.slug);
await expect(publicPage.articleBody).toContainText(addition);
});
test('published - unpublishing takes the post off the site', async ({ page }) => {
const title = `react-unpublish-${Date.now()}`;
const created = await postFactory.create({ title, status: 'published' });
const editor = new PostEditorPage(page);
await editor.gotoPost(created.id);
await expect(editor.postStatus).toContainText('Published');
await editor.revertToDraft();
await expect(editor.postStatus).toContainText('Draft');
const reverted = await readPost(page, created.id);
expect(reverted.status).toBe('draft');
const frontendPage = await page.context().newPage();
await expect
.poll(async () => (await frontendPage.request.get(`/${created.slug}/`)).status(), {
timeout: 20000,
})
.toBe(404);
});
test('page - publishing puts the page on the site', async ({ page }) => {
const title = `react-page-publish-${Date.now()}`;
const { editor, draft } = await openDraftPage(page, title);
await editor.publishFlow.open();
await expect(editor.publishFlow.optionsStep).toBeVisible();
await Promise.all([waitForPostSave(page, draft.id, PAGES_API), editor.publishFlow.confirm()]);
await expect(page).toHaveURL('/ghost/#/pages');
const published = await readPage(page, draft.id);
expect(published.status).toBe('published');
const frontendPage = await page.context().newPage();
const publicPage = new PostPage(frontendPage);
await publicPage.gotoPost(draft.slug);
await expect(publicPage.articleTitle).toHaveText(title);
});
test('page - scheduling keeps the page off the site until its time', async ({ page }) => {
const { editor, draft } = await openDraftPage(page, `react-page-schedule-${Date.now()}`);
await editor.publishFlow.open();
await expect(editor.publishFlow.optionsStep).toBeVisible();
await editor.publishFlow.schedule({});
await Promise.all([waitForPostSave(page, draft.id, PAGES_API), editor.publishFlow.confirm()]);
await expect(page).toHaveURL('/ghost/#/pages');
const scheduled = await readPage(page, draft.id);
expect(scheduled.status).toBe('scheduled');
expect(Date.parse(scheduled.published_at)).toBeGreaterThan(Date.now());
expect((await page.request.get(`/${draft.slug}/`)).status()).toBe(404);
});
test('draft - previewing saves the pending edit before it renders', async ({ page }) => {
// The save, the modal and the frontend render of the preview do not fit the
// default budget
test.setTimeout(60000);
const created = await postFactory.create({
title: `react-preview-${Date.now()}`,
status: 'draft',
featured: false,
});
const addition = 'Typed but not yet saved.';
const editor = new PostEditorPage(page);
await editor.gotoPost(created.id);
await expect(editor.lexicalEditor).toBeVisible();
await editor.appendToBody(` ${addition}`);
await editor.previewButton.click();
await expect(editor.previewModal.modal).toBeVisible();
const preview = editor.previewModal.desktopPreview;
// The frame is rendered only once the save the preview waited on has landed
await expect(preview.frameElement).toBeVisible();
await preview.waitForPreviewModalFrame();
const post = await readPost(page, created.id);
expect(post.lexical).toContain(addition);
await expect(preview.frameElement).toHaveAttribute('src', new RegExp(`/p/${post.uuid}/`));
await expect(preview.desktopPreviewFrame.getByRole('article').first()).toContainText(addition);
});
test('contributor - gets Save and Preview and no way to publish', async ({
browser,
baseURL,
page,
ghostAccountContributor,
}) => {
// Inviting the contributor and signing them in does not fit the default
// budget
test.setTimeout(90000);
const title = `react-contributor-${Date.now()}`;
// Authored through the API rather than the editor: the editor's
// create payload omits `authors`, which a contributor's add permission needs
const created = await page.request.post(POSTS_API, {
data: {
posts: [{ title, status: 'draft', authors: [{ email: ghostAccountContributor.email }] }],
},
});
expect(created.status()).toBe(201);
const {
posts: [draft],
} = await created.json();
await withIsolatedPage(browser, { baseURL }, async ({ page: contributorPage }) => {
const loginPage = new LoginPage(contributorPage);
await loginPage.goto();
await loginPage.signIn(ghostAccountContributor.email, ghostAccountContributor.password);
const postsPage = new PostsPage(contributorPage);
await postsPage.waitForPageToFullyLoad();
const editor = new PostEditorPage(contributorPage);
await editor.gotoPost(draft.id);
await expect(editor.lexicalEditor).toBeVisible();
await expect(editor.publishSaveButton).toBeVisible();
await expect(editor.previewButton).toBeVisible();
await expect(editor.publishFlow.publishButton).toHaveCount(0);
await expect(editor.updateFlowButton).toHaveCount(0);
});
});
// Both cases need email genuinely on offer, so the publish type is a real
// choice rather than the only one left.
test.describe('with a newsletter', () => {
test.use({ mailgunEnabled: true });
test('draft - publish only puts the post on the site and sends no email', async ({ page }) => {
// A member, a draft and the three publish steps do not fit the default
// budget
test.setTimeout(90000);
const title = `react-publish-only-${Date.now()}`;
const body = 'This is my published post body.';
await addSubscribedMember(page, 'react-publish-only@example.com');
const { editor, postId } = await startDraft(page, { title, body });
await editor.publishFlow.open();
await expect(editor.publishFlow.optionsStep).toBeVisible();
await editor.publishFlow.selectPublishType('publish');
await editor.publishFlow.confirm();
await expect(page).toHaveURL('/ghost/#/posts');
const post = await readPost(page, postId);
expect(post.status).toBe('published');
// The send was on offer and the publish type declined it
expect(post.email).toBeNull();
const frontendPage = await page.context().newPage();
const publicPage = new PostPage(frontendPage);
await publicPage.gotoPost(post.slug);
await expect(publicPage.articleTitle).toHaveText(title);
await expect(publicPage.articleBody).toHaveText(body);
});
test('draft - publish and send delivers the email to the member', async ({
emailClient,
page,
}) => {
// A member, a publish flow and the send do not fit the default budget
test.setTimeout(90000);
const title = `react-publish-send-${Date.now()}`;
const body = 'This is my emailed post body.';
const memberEmail = 'react-publish-send@example.com';
await addSubscribedMember(page, memberEmail);
const { editor, postId } = await startDraft(page, { title, body });
await editor.publishFlow.open();
await expect(editor.publishFlow.optionsStep).toBeVisible();
await editor.publishFlow.selectPublishType('publish+send');
await editor.publishFlow.confirm();
await expect(page).toHaveURL(`/ghost/#/posts/analytics/${postId}`);
const post = await readPost(page, postId);
expect(post.status).toBe('published');
expect(post.email).not.toBeNull();
const delivered = await emailClient.search(
{ to: memberEmail, subject: title },
{ timeoutMs: 30_000 },
);
expect(delivered.length).toBeGreaterThanOrEqual(1);
const detail = await emailClient.getMessageDetailed(delivered[0]);
expect(detail.HTML).toContain(body);
});
test('draft - email only delivers the email and keeps the post off the site', async ({
emailClient,
page,
}) => {
// A member, a publish flow and the send do not fit the default budget
test.setTimeout(90000);
const title = `react-email-only-${Date.now()}`;
const body = 'This is my email-only post body.';
const memberEmail = 'react-email-only@example.com';
await addSubscribedMember(page, memberEmail);
const { editor, postId } = await startDraft(page, { title, body });
await editor.publishFlow.open();
await expect(editor.publishFlow.optionsStep).toBeVisible();
await editor.publishFlow.selectPublishType('send');
await editor.publishFlow.confirm();
await expect(page).toHaveURL(`/ghost/#/posts/analytics/${postId}`);
const post = await readPost(page, postId);
expect(post.status).toBe('sent');
expect(post.email_only).toBe(true);
expect(post.email).not.toBeNull();
expect((await page.request.get(`/${post.slug}/`)).status()).toBe(404);
const delivered = await emailClient.search(
{ to: memberEmail, subject: title },
{ timeoutMs: 30_000 },
);
expect(delivered.length).toBeGreaterThanOrEqual(1);
const detail = await emailClient.getMessageDetailed(delivered[0]);
expect(detail.HTML).toContain(body);
});
test('draft - scheduled publish and send holds the email and lists as scheduled', async ({
page,
}) => {
// A member, a scheduled flow and a trip through the list do not fit the
// default budget
test.setTimeout(90000);
const title = `react-scheduled-publish-send-${Date.now()}`;
await addSubscribedMember(page, 'react-scheduled-publish-send@example.com');
const { editor, postId, postsPage } = await startDraft(page, {
title,
body: 'This is my scheduled publish and send post body.',
});
await editor.publishFlow.open();
await expect(editor.publishFlow.optionsStep).toBeVisible();
await editor.publishFlow.selectPublishType('publish+send');
await editor.publishFlow.schedule({ date: '2050-01-01' });
await Promise.all([waitForPostSave(page, postId), editor.publishFlow.confirm()]);
await expect(page).toHaveURL('/ghost/#/posts');
const post = await readPost(page, postId);
expect(post.status).toBe('scheduled');
expect(post.email_only).toBe(false);
expect(post.published_at).toMatch(/^2050-01-01T/);
// The newsletter rides on the post; the email is only created at publish time
expect(post.newsletter?.slug).toBe('default-newsletter');
expect(post.email).toBeNull();
expect((await page.request.get(`/${post.slug}/`)).status()).toBe(404);
// The list consumes the handoff and opens its celebration over itself
await expect(postsPage.publishCelebration).toBeVisible();
await postsPage.closePublishCelebration();
await postsPage.waitForPageToFullyLoad();
const row = postsPage.getPostByTitle(title);
await expect(row).toContainText('Scheduled');
await postsPage.hoverPost(title);
await expect(row).toContainText(/to be published and sent at .*2050/);
await row.click();
await expect(editor.postStatus).toContainText('Scheduled');
await editor.postStatus.hover();
await expect(editor.header.scheduleCountdown).toContainText(
/to be published and sent to .*2050/,
);
});
test('draft - scheduled email only holds the send and keeps the post off the site', async ({
page,
}) => {
// A member, a draft and a scheduled flow do not fit the default budget
test.setTimeout(90000);
const title = `react-scheduled-email-only-${Date.now()}`;
await addSubscribedMember(page, 'react-scheduled-email-only@example.com');
const { editor, postId } = await startDraft(page, {
title,
body: 'This is my scheduled email-only post body.',
});
await editor.publishFlow.open();
await expect(editor.publishFlow.optionsStep).toBeVisible();
await editor.publishFlow.selectPublishType('send');
await editor.publishFlow.schedule({});
await Promise.all([waitForPostSave(page, postId), editor.publishFlow.confirm()]);
// A scheduled send has nothing to report yet, so the flow returns to the list
await expect(page).toHaveURL('/ghost/#/posts');
const post = await readPost(page, postId);
expect(post.status).toBe('scheduled');
expect(post.email_only).toBe(true);
expect(Date.parse(post.published_at)).toBeGreaterThan(Date.now());
expect(post.email).toBeNull();
expect((await page.request.get(`/${post.slug}/`)).status()).toBe(404);
});
});
test.describe('with member signup disabled', () => {
// The settings change must not leak into the file's shared environment
test.use({ isolation: 'per-test' });
test('disabled subscription access leaves the publish flow without email', async ({ page }) => {
const settingsService = new SettingsService(page.request);
await settingsService.setMembersSignupAccess('none');
// Admin reads settings as it boots, so only a reload picks the change up
await page.reload();
const postsPage = new PostsPage(page);
await postsPage.goto();
await postsPage.newPostButton.click();
const editor = new PostEditorPage(page);
await editor.createDraft({ title: 'Test post' });
await expect(editor.postStatus).toContainText('Draft - Saved');
await editor.publishFlow.open();
await expect(editor.publishFlow.optionsStep).toBeVisible();
await expect(editor.publishFlow.publishTypeButton).toBeDisabled();
await expect(editor.publishFlow.publishTypeButton).toContainText('Publish on site');
await expect(editor.publishFlow.emailRecipientsSetting).toHaveCount(0);
});
});
});