Skip to content

Commit 7bf0871

Browse files
Enhancement/add mailchimp api client (#99)
Highlights - Add a FE Mailchimp client to Cypress tests to verify Mailchimp account interactions. Ensures successful form submission and full integration testing. - Use .env.test for env variables - Add health check for tests if Mailchimp client is unable to make requests - Modularize Cypress commands ‼️ Environment changes needed ‼️ - Need to generate an API key for the test Mailchimp user in the CI/CD pipeline. - Need to attach that API key to CYPRESS_MAILCHIMP_API_KEY as a Github secret - Need to attach the server prefix to CYPRESS_MAILCHIMP_API_SERVER_PREFIX as a Github secret Commits * Add Mailchimp API package * Add cypress-dotenv * Add Mailchimp API client to Cypress Necessary to interact with the test user account to verify contacts, make deletions, etc. * Update documentation for env setup * Remove outdated E2E notes * Remove log to fix bug. Skip unfinished tests. * Remove outdated readme note * Add test to verify lists in WP admin match lists in Mailchimp account * Add assertion that contacts in Mailchimp account match email submitted * Simplify mailchimp commands * Add mailchimp commands to command folder * Organize commands into modular files * Rename mailchimpApiConfig to more clearly state purpose * Fix grammar mistake * Add scaffolding for gmail api * Switch to gabi-script for FE requests * Update .env.test.sample with example of google creds * Remove gmail api client Too complicated to set up for our purposes. We're better off with Mailhog * Remove gapi-script
1 parent 7cb8ffd commit 7bf0871

16 files changed

+446
-143
lines changed

.env.test.sample

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CYPRESS_MAILCHIMP_USERNAME="your mailchimp username"
2+
CYPRESS_MAILCHIMP_PASSWORD="your mailchimp password"
3+
4+
# Generate API Key: https://mailchimp.com/help/about-api-keys/
5+
CYPRESS_MAILCHIMP_API_KEY="generate an API key from your mailchimp account and paste here" # e.g. "1234567890abcdef1234567890abcdef-us19"
6+
CYPRESS_MAILCHIMP_API_SERVER_PREFIX="" # e.g "us19" - your server prefix may be different

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dist/*
77
docs-built
88
.phpunit.result.cache
99
/.wp-env.override.json
10+
.env.test
1011

1112
tests/cypress/videos
1213
tests/cypress/screenshots

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,14 @@ The `tests` directory contains end-to-end tests for the project, utilizing Cypre
9999
1. Run `npm install`.
100100
2. Run `npm run build`.
101101
3. Run `npm run env:start`.
102-
4. Set Mailchimp credentials as environment variables:
103-
- run `export CYPRESS_MAILCHIMP_USERNAME="your mailchimp username"`
104-
- run `export CYPRESS_MAILCHIMP_PASSWORD="your mailchimp password"`
105-
- Do not forget to escape any special characters with `\`. `echo $CYPRESS_MAILCHIMP_PASSWORD` to ensure the password is set correctly.
102+
4. Duplicate the `.env.test.sample` file and rename `.env.test`. Populate this file with your own Mailchimp account credentials.
106103
- **NOTE:** Use a test account that does not require 2FA. 2FA will fail some of the tests.
107104
5. Set your Mailchimp account up
108105
- Name the audience in your Mailchimp test account "10up". Required for `settings.test.js`.
109106
- Enable all merge fields. From your Mailchimp account home page -> `/audience/settings/` -> Edit merge fields/tags -> Set all merge fields to "visible". Required for `settings.test.js`.
110107

111108
6. Run `npm run cypress:run`. You can also run `npm run cypress:open` to run tests in UI mode.
112109

113-
#### E2E tests notes
114-
- You must run `connect.test.js` before `settings.test.js` in order to log the user in. Otherwise, all settings tests will fail.
115-
116110
## Support Level
117111

118112
**Active:** Mailchimp is actively working on this, and we expect to continue work for the foreseeable future including keeping tested up to the most recent version of WordPress. Bug reports, feature requests, questions, and pull requests are welcome.

package-lock.json

Lines changed: 162 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
},
3333
"devDependencies": {
3434
"@10up/cypress-wp-utils": "^0.4.0",
35+
"@mailchimp/mailchimp_marketing": "^3.0.80",
3536
"@wordpress/env": "^10.2.0",
3637
"10up-toolkit": "^6.2.0",
3738
"cypress": "^13.13.2",
39+
"cypress-dotenv": "^3.0.1",
3840
"cypress-mochawesome-reporter": "^3.8.2",
3941
"mochawesome-json-to-md": "^1.3.5"
4042
},

tests/cypress/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { defineConfig } = require( 'cypress' );
22
const { loadConfig } = require( '@wordpress/env/lib/config' );
33
const getCacheDirectory = require( '@wordpress/env/lib/config/get-cache-directory' );
4+
const dotenvPlugin = require('cypress-dotenv');
45

56
module.exports = defineConfig( {
67
chromeWebSecurity: false,
@@ -19,6 +20,7 @@ module.exports = defineConfig( {
1920
},
2021
e2e: {
2122
setupNodeEvents( on, config ) {
23+
config = dotenvPlugin(config, { path: '../../.env.test' }); // Load .env variables into Cypress
2224
return setBaseUrl( on, config );
2325
},
2426
specPattern: 'tests/cypress/e2e/**/*.test.{js,jsx,ts,tsx}',

tests/cypress/e2e/settings.test.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,31 @@ describe('Admin can update plugin settings', () => {
1818

1919
cy.login(); // WP
2020
cy.mailchimpLoginIfNotAlreadyLoggedIn();
21+
22+
// Call mailchimpLists once and store the result in the alias 'mailchimpLists'
23+
cy.getMailchimpLists().then((mailchimpLists) => {
24+
Cypress.env('mailchimpLists', mailchimpLists); // Save globally
25+
});
26+
});
27+
28+
it('All lists from user\'s account populate the WP admin dropdown list', () => {
29+
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
30+
const $wpLists = cy.get('#mc_list_id'); // Lists from the WP admin dropdown
31+
const mailchimpLists = Cypress.env('mailchimpLists');
32+
33+
// Verify that the same number of lists exist in the dropdown as in the Mailchimp account
34+
$wpLists.should('have.length', mailchimpLists.length);
35+
36+
mailchimpLists.forEach((list) => {
37+
// Verify that all Mailchimp account lists exist in dropdown
38+
cy.get('#mc_list_id').should('contain', list.name);
39+
});
2140
});
2241

2342
it('Admin can see list and save it', () => {
2443
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
2544

45+
// Verify that list can be saved
2646
cy.get('.mc-h2').contains('Your Lists');
2747
cy.get('#mc_list_id').select('10up');
2848
cy.get('input[value="Update List"]').click();
@@ -56,7 +76,15 @@ describe('Admin can update plugin settings', () => {
5676
// Test here...
5777
});
5878

59-
it('Admin can create a Signup form using the shortcode', () => {
79+
/**
80+
* - Test form creation
81+
* - Test form display (basic)
82+
* - Test form error handling (basic)
83+
* - Test form submission
84+
* - Test that the contact was added to the Mailchimp account via the Mailchimp API
85+
*/
86+
it('Admin can create and submit a Signup form using the shortcode', () => {
87+
// Step 1: Set up the post with the shortcode
6088
const postTitle = 'Mailchimp signup form - shortcode';
6189
const beforeSave = () => {
6290
cy.insertBlock('core/shortcode').then((id) => {
@@ -66,18 +94,43 @@ describe('Admin can update plugin settings', () => {
6694
.type('[mailchimpsf_form]');
6795
});
6896
};
97+
98+
// Step 2: Create the post
6999
cy.createPost({ title: postTitle, content: '', beforeSave }).then((post) => {
70100
if (post) {
71101
shortcodePostURL = `/?p=${post.id}`;
72102
cy.visit(shortcodePostURL);
103+
104+
// Step 3: Verify the form is displayed
73105
cy.get('#mc_signup').should('exist');
74106
cy.get('#mc_mv_EMAIL').should('exist');
75107
cy.get('#mc_signup_submit').should('exist');
76108

77-
// Test error handling
109+
// Step 4: Test error handling
78110
cy.get('#mc_signup_submit').click();
79111
cy.get('.mc_error_msg').should('exist');
80112
cy.get('.mc_error_msg').contains('Email Address: This value should not be blank.');
113+
114+
// Step 5: Test that the form can be submitted
115+
// TODO: Is this email address name a security hazard? "@example.com" emails will not pass validation.
116+
const email = '[email protected]';
117+
cy.get('#mc_mv_EMAIL').type(email);
118+
cy.get('#mc_signup_submit').click();
119+
120+
// Step 6: Verify that the form was submitted successfully
121+
cy.get('.mc_success_msg').should('exist');
122+
123+
// // TODO: This is failing because we need to confirm the test email address subscription
124+
// // Step 7: Verify that the contact was added to the Mailchimp account via the Mailchimp API
125+
// const mailchimpLists = Cypress.env('mailchimpLists');
126+
// const listId = mailchimpLists.find((list) => list.name === '10up').id;
127+
// // Get the contacts from the list
128+
// cy.getContactsFromAList(listId).then((contacts) => {
129+
// console.log('Contacts:', contacts);
130+
// // Verify that the contact was added to the list
131+
// const contactJustRegistered = contacts.find((c) => c.email_address === email);
132+
// expect(contactJustRegistered).to.exist;
133+
// });
81134
}
82135
});
83136
});

0 commit comments

Comments
 (0)