File size: 4,365 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @group jetpack-wpcom-integration
 */

import {
	getTestAccountByFeature,
	envToFeatureKey,
	envVariables,
	DataHelper,
	TestAccount,
	RestAPIClient,
	PostResponse,
	PublishedPostPage,
	EmailClient,
	SecretsManager,
	SubscribersPage,
} from '@automattic/calypso-e2e';
import { Page, Browser } from 'playwright';
import { skipDescribeIf } from '../../jest-helpers';

declare const browser: Browser;

/**
 * Walks through the newsletter lifecycle of a user signing up for newsletters
 * from a published post, then being removed from subscribers.
 *
 * Keywords: Newsletters, Jetpack, Email Subscribers, Email Followers
 */
skipDescribeIf( envVariables.ATOMIC_VARIATION === 'private' )(
	DataHelper.createSuiteTitle( 'Newsletter: Subscribe and Remove' ),
	function () {
		// Subscribing "user" setup.
		const inboxID = SecretsManager.secrets.mailosaur.manualTesting;
		const postTitle = DataHelper.getDateString( 'ISO-8601' ) as string;
		const emailClient = new EmailClient();
		const testEmail = emailClient.getTestEmailAddress( inboxID );

		let page: Page;
		let testAccount: TestAccount;
		let restAPIClient: RestAPIClient;
		let newPostDetails: PostResponse;
		let subscribersPage: SubscribersPage;

		beforeAll( async function () {
			page = await browser.newPage();

			const features = envToFeatureKey( envVariables );
			const accountName = getTestAccountByFeature( features );

			// Initizlie the TestAccount object, but do not authenticate
			// to Calypso.
			testAccount = new TestAccount( accountName );

			// Create a new post on the site with the Subscribe block.
			restAPIClient = new RestAPIClient( testAccount.credentials );
			newPostDetails = await restAPIClient.createPost(
				testAccount.credentials.testSites?.primary.id as number,
				{
					title: postTitle,
					content: '<!-- wp:jetpack/subscriptions /-->',
				}
			);
		} );

		describe( 'As subscribing user', function () {
			it( 'Navigate to published post page', async function () {
				await page.goto( newPostDetails.URL );
			} );

			it( 'Subscribe to the site', async function () {
				const publishedPostPage = new PublishedPostPage( page );
				await publishedPostPage.subscribe( testEmail );
			} );

			it( 'Confirm email subscription', async function () {
				const message = await emailClient.getLastMatchingMessage( {
					inboxId: inboxID,
					subject: 'Confirm your subscription',
					sentTo: testEmail,
				} );

				const confirmationURL =
					emailClient.getLinkFromMessageByKey( message, 'Confirm email' ) ??
					emailClient.getLinkFromMessageByKey( message, 'Confirm now' );
				expect( confirmationURL ).not.toBe( null );

				// Now, when you subscribe from a post, it uses a magic link login and redirects you to the post you subscribed from.
				// All of this takes a good bit longer due to some behinds-the-scenes stuff, so we need a longer timeout.
				// We also should make sure we have a fresh page so we know for sure when the redirect happens to the original post.
				const pageForConfirming = await browser.newPage();
				const waitForRedirectToOriginalPost = pageForConfirming.waitForURL(
					( url ) => url.href.includes( newPostDetails.URL ),
					{
						timeout: 45 * 1000,
					}
				);
				await pageForConfirming.goto( confirmationURL as string );
				await waitForRedirectToOriginalPost;
			} );
		} );

		describe( 'As publishing user', function () {
			beforeAll( async function () {
				// Authenticate as the publishing user.
				await testAccount.authenticate( page );

				subscribersPage = new SubscribersPage( page );
			} );

			it( 'Navigate to the Subscribers page', async function () {
				await subscribersPage.visit( testAccount.getSiteURL( { protocol: false } ) );
			} );

			it( 'Search for subscribed user', async function () {
				await subscribersPage.validateSubscriber( testEmail );
			} );

			it( 'Remove subscribed user', async function () {
				await subscribersPage.removeSubscriber( testEmail );
			} );
		} );

		afterAll( async function () {
			try {
				// Clean up the subscriber and the post.
				await restAPIClient.deleteSubscriber(
					testAccount.credentials.testSites?.primary.id as number,
					testEmail
				);

				await restAPIClient.deletePost(
					testAccount.credentials.testSites?.primary.id as number,
					newPostDetails.ID
				);
			} catch {
				// noop
			}
		} );
	}
);