diff --git a/ghost/core/test/e2e-api/members/send-magic-link.test.js b/ghost/core/test/e2e-api/members/send-magic-link.test.js index b490573397..9fba77554b 100644 --- a/ghost/core/test/e2e-api/members/send-magic-link.test.js +++ b/ghost/core/test/e2e-api/members/send-magic-link.test.js @@ -44,7 +44,7 @@ describe('sendMagicLink', function () { // Get data const data = await membersService.api.getTokenDataFromMagicLinkToken(token); - + should(data).match({ email, attribution: { @@ -55,6 +55,42 @@ describe('sendMagicLink', function () { }); }); + it('triggers email alert for free member signup', async function () { + const email = 'newly-created-user-magic-link-test@test.com'; + await membersAgent.post('/api/send-magic-link') + .body({ + email, + emailType: 'signup' + }) + .expectEmptyBody() + .expectStatus(201); + + // Check email is sent + const mail = mockManager.assert.sentEmail({ + to: email, + subject: /Complete your sign up to Ghost!/ + }); + + // Get link from email + const [url] = mail.text.match(/https?:\/\/[^\s]+/); + const parsed = new URL(url); + const token = parsed.searchParams.get('token'); + + // Get member data from token + const data = await membersService.api.getMemberDataFromMagicLinkToken(token); + + // Check member alert is sent to site owners + mockManager.assert.sentEmail({ + to: 'jbloggs@example.com', + subject: /🥳 Free member signup: newly-created-user-magic-link-test@test.com/ + }); + + // Check member data is returned + should(data).match({ + email + }); + }); + it('Converts the urlHistory to the attribution and stores it in the token', async function () { const email = 'newly-created-user-magic-link-test-2@test.com'; await membersAgent.post('/api/send-magic-link') @@ -84,7 +120,7 @@ describe('sendMagicLink', function () { // Get data const data = await membersService.api.getTokenDataFromMagicLinkToken(token); - + should(data).match({ email, attribution: { diff --git a/ghost/members-api/lib/MembersAPI.js b/ghost/members-api/lib/MembersAPI.js index 61e779734d..c94fd3832a 100644 --- a/ghost/members-api/lib/MembersAPI.js +++ b/ghost/members-api/lib/MembersAPI.js @@ -60,6 +60,7 @@ module.exports = function MembersAPI({ }, stripeAPIService, offersAPI, + staffService, labsService, newslettersService, memberAttributionService @@ -85,6 +86,7 @@ module.exports = function MembersAPI({ stripeAPIService, tokenService, newslettersService, + staffService, labsService, productRepository, Member, @@ -149,6 +151,7 @@ module.exports = function MembersAPI({ productRepository, StripePrice, tokenService, + staffService, sendEmailWithMagicLink }); @@ -218,6 +221,12 @@ module.exports = function MembersAPI({ return member; } const newMember = await users.create({name, email, labels, newsletters, attribution}); + + // Notify staff users of new free member signup + if (labsService.isSet('emailAlerts')) { + await staffService.notifyFreeMemberSignup(newMember.toJSON()); + } + await MemberLoginEvent.add({member_id: newMember.id}); return getMemberIdentityData(email); } @@ -324,7 +333,7 @@ module.exports = function MembersAPI({ memberBREADService, events: eventRepository, productRepository, - + // Test helpers getTokenDataFromMagicLinkToken };