File size: 8,861 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
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
/* eslint-disable @typescript-eslint/no-explicit-any */ // We have to do some dynamic parsing and iteration as we validate JSON.
import fs from 'fs';
import path from 'path';
import type { Secrets } from '.';

export const TEST_ACCOUNT_NAMES = [
	'defaultUser',
	'atomicUser',
	'eCommerceUser',
	'simpleSiteFreePlanUser',
	'simpleSitePersonalPlanUser',
	'gutenbergSimpleSiteUser',
	'gutenbergSimpleSiteEdgeUser',
	'gutenbergSimpleSiteBlockUpgradeUser',
	'gutenbergAtomicSiteUser',
	'gutenbergAtomicSiteEdgeUser',
	'gutenbergAtomicSiteEdgeNightliesUser',
	'coBlocksSimpleSiteEdgeUser',
	'coBlocksAtomicSiteEdgeUser',
	'siteEditorSimpleSiteUser',
	'siteEditorSimpleSiteEdgeUser',
	'siteEditorAtomicSiteUser',
	'siteEditorAtomicSiteEdgeUser',
	'martechTosUser',
	'calypsoPreReleaseUser',
	'i18nUser',
	'p2User',
	'totpUser',
	'smsUser',
	'jetpackRemoteSiteUser',
	'jetpackStagingUser',
	'jetpackStagingFseUser',
	'jetpackUserPREMIUM',
	'jetpackUserJN',
	'desktopAppUser',
	'commentingUser',
	'notificationsUser',
	'googleLoginUser',
	'appleLoginUser',
	'gitHubLoginUser',
	'jetpackAtomicDefaultUser',
	'jetpackAtomicPhpOldUser',
	'jetpackAtomicPhpNewUser',
	'jetpackAtomicEcommPlanUser',
	'jetpackAtomicPrivateUser',
	'jetpackAtomicWpBetaUser',
	'jetpackAtomicWpPreviousUser',
	'automatticForAgenciesUser',
] as const;

/**
 * Static class that gives you access to secrets needed for E2E testing.
 * You must first decrypt the encrypted secrets for tests to be able to access them.
 * All checking happens at runtime.
 */
export class SecretsManager {
	private static _secretsCache: Secrets;

	/**
	 * Get the secrets. If this is the first time accessed in the node process, it will initialize and cache the secrets.
	 */
	public static get secrets(): Secrets {
		if ( ! this._secretsCache ) {
			this._secretsCache = this.initializeSecrets();
		}

		return this._secretsCache;
	}

	/**
	 * Initializes the secrets needed for E2E testing.
	 */
	private static initializeSecrets(): Secrets {
		const SECRETS_FILE_NAME = 'decrypted-secrets.json';
		const parsedSecrets = this.parseSecretsFile( path.join( __dirname, SECRETS_FILE_NAME ) );
		const fakeReferenceSecrets = this.createFakeSecretsForValidation();
		this.validateSecrets( parsedSecrets, fakeReferenceSecrets );
		return parsedSecrets as Secrets;
	}

	/**
	 * Read and JSON parse the decrypted secrets file.
	 *
	 * @param {string} filePath Full path to the expected decrypted secrets file.
	 * @returns A parsed JSON object.
	 */
	private static parseSecretsFile( filePath: string ): any {
		try {
			// Normally, any fs sync operations are a big no-no.
			// However, this only runs once on module load, and should be safe to do.
			// This is a more runtime deferred version of just having a '.json' module.
			// Once we're on ES2022, this would be a perfect use case for a top-level-await.
			const secretsJson = fs.readFileSync( filePath, {
				encoding: 'utf-8',
			} );
			return JSON.parse( secretsJson );
		} catch ( err ) {
			const error: Error = err as Error;
			throw new Error(
				'Failed to initialize the E2E secrets: Could not find and parse the secrets file.\n' +
					'Have you decrypted the secrets file yet?\n' +
					'Export the decryption key to E2E_SECRETS_KEY and run "yarn decrypt-secrets".\n' +
					'Then, please build the package by running "yarn workspace @automattic/calypso-e2e build".\n' +
					`Original error message: ${ error.message }`
			);
		}
	}

	/**
	 * Validates a parsed secrets object against a fake set of reference secrets.
	 * This ensures we have all the keys we expect and they are of the right type.
	 *
	 * @param {any} parsedSecrets The parsed JSON object from the local secrets file.
	 * @param {Secrets} fakeReferenceSecrets A fake secrets object that can be used for reference validation.
	 * @throws If any expected key is missing or the wrong type.
	 */
	private static validateSecrets( parsedSecrets: any, fakeReferenceSecrets: Secrets ): void {
		const compareRecursive = ( target: any, reference: any, keyPath: string[] ) => {
			for ( const key in reference ) {
				if ( ! target?.[ key ] || typeof reference[ key ] !== typeof target[ key ] ) {
					const fullKeyPath = [ ...keyPath, key ].join( '.' );
					throw new Error(
						'Failed to initialize the E2E secrets: Invalid or missing key found in the decrypted secrets.\n' +
							'Make sure you have decrypted the most recent version of the encrypted secrets.\n' +
							'This also may mean the typings for the secrets are stale and need updating.\n\n' +
							'Details:\n' +
							`\tInvalid or missing key: ${ fullKeyPath }\n` +
							`\tExpected type: ${ typeof reference[ key ] }, got ${ typeof target[ key ] }`
					);
				}

				if ( typeof reference[ key ] === 'object' ) {
					compareRecursive( target[ key ], reference[ key ], [ ...keyPath, key ] );
				}
			}
		};

		compareRecursive( parsedSecrets, fakeReferenceSecrets, [] );
	}

	/**
	 * Creates a fake set of secrets that can be used for comparative validation.
	 *
	 * @returns A fake secret object with all expcted required properties.
	 */
	private static createFakeSecretsForValidation(): Secrets {
		const fakeAccount = {
			username: 'FAKE_VALUE',
			password: 'FAKE_VALUE',
		};

		const fakeFullAccount = {
			...fakeAccount,
			userID: 0,
			email: 'FAKE_VALUE',
			testSites: { primary: { id: 0, url: 'FAKE_VALUE' } },
		};

		return {
			storeSandboxCookieValue: 'FAKE_VALUE',
			testCouponCode: 'FAKE_VALUE',
			wpccAuthPath: 'FAKE_VALUE',
			wooSignupPath: 'FAKE_VALUE',
			wooLoginPath: 'FAKE_VALUE',
			calypsoOauthApplication: {
				client_id: 'FAKE_VALUE',
				client_secret: 'FAKE_VALUE',
			},
			martechTosUploadCredentials: {
				bearer_token: 'FAKE_VALUE',
			},
			socialAccounts: {
				tumblr: {
					username: 'FAKE_VALUE',
					password: 'FAKE_VALUE',
				},
			},
			mailosaur: {
				apiKey: 'FAKE_VALUE',
				inviteInboxId: 'FAKE_VALUE',
				signupInboxId: 'FAKE_VALUE',
				domainsInboxId: 'FAKE_VALUE',
				defaultUserInboxId: 'FAKE_VALUE',
				totpUserInboxId: 'FAKE_VALUE',
				manualTesting: 'FAKE_VALUE',
			},
			testAccounts: {
				defaultUser: {
					...fakeFullAccount,
				},
				atomicUser: {
					...fakeFullAccount,
				},
				eCommerceUser: { ...fakeAccount },
				simpleSiteFreePlanUser: { ...fakeAccount },
				simpleSitePersonalPlanUser: { ...fakeAccount },
				gutenbergSimpleSiteUser: { ...fakeAccount },
				gutenbergSimpleSiteEdgeUser: { ...fakeAccount },
				gutenbergSimpleSiteBlockUpgradeUser: { ...fakeFullAccount },
				gutenbergAtomicSiteUser: { ...fakeAccount },
				gutenbergAtomicSiteEdgeUser: { ...fakeAccount },
				gutenbergAtomicSiteEdgeNightliesUser: { ...fakeAccount },
				coBlocksSimpleSiteEdgeUser: { ...fakeAccount },
				coBlocksAtomicSiteEdgeUser: { ...fakeAccount },
				siteEditorSimpleSiteUser: { ...fakeAccount },
				siteEditorSimpleSiteEdgeUser: { ...fakeAccount },
				siteEditorAtomicSiteUser: { ...fakeAccount },
				siteEditorAtomicSiteEdgeUser: { ...fakeAccount },
				martechTosUser: { ...fakeAccount },
				calypsoPreReleaseUser: { ...fakeAccount },
				i18nUser: { ...fakeAccount },
				// The following two needs a totpKey
				p2User: { ...fakeAccount, totpKey: 'FAKE_VALUE' },
				totpUser: { ...fakeAccount, totpKey: 'FAKE_VALUE' },
				// The following user needs smsNumber
				smsUser: { ...fakeAccount, smsNumber: { code: 'FAKE_VALUE', number: 'FAKE_VALUE' } },
				jetpackRemoteSiteUser: {
					...fakeAccount,
					userID: 0,
					testSites: { primary: { id: 0, url: 'FAKE_VALUE', remotePassword: 'FAKE_VALUE' } },
				},
				jetpackStagingUser: {
					...fakeAccount,
					userID: 0,
					testSites: { primary: { id: 0, url: 'FAKE_VALUE' } },
				},
				jetpackStagingFseUser: {
					...fakeAccount,
					userID: 0,
					testSites: { primary: { id: 0, url: 'FAKE_VALUE' } },
				},
				jetpackUserPREMIUM: { ...fakeAccount },
				jetpackUserJN: { ...fakeAccount },
				desktopAppUser: { ...fakeAccount },
				commentingUser: { ...fakeAccount },
				notificationsUser: { ...fakeAccount },
				googleLoginUser: {
					...fakeAccount,
					smsNumber: { code: 'FAKE_VALUE', number: 'FAKE_VALUE' },
					totpKey: 'FAKE_VALUE',
				},
				appleLoginUser: {
					...fakeAccount,
				},
				gitHubLoginUser: {
					...fakeAccount,
				},
				jetpackAtomicDefaultUser: {
					...fakeFullAccount,
				},
				jetpackAtomicPhpOldUser: {
					...fakeFullAccount,
				},
				jetpackAtomicPhpNewUser: {
					...fakeFullAccount,
				},
				jetpackAtomicEcommPlanUser: {
					...fakeFullAccount,
				},
				jetpackAtomicPrivateUser: {
					...fakeFullAccount,
				},
				jetpackAtomicWpBetaUser: {
					...fakeFullAccount,
				},
				jetpackAtomicWpPreviousUser: {
					...fakeFullAccount,
				},
				automatticForAgenciesUser: {
					...fakeFullAccount,
				},
			},
			otherTestSites: {
				notifications: 'FAKE_VALUE',
			},
		};
	}
}