Fixed sending emails when using MembersAgent.loginAs (#15791)

fixes https://github.com/TryGhost/Team/issues/1903

MembersAgent.loginAs sends email, asynchronously via events. Which
conflicts with tests that also test emails. We cannot properly await
these events, so this is currently fixed with a timeout of 200ms. But
this was too random and unreliable.
This commit is contained in:
Simon Backx 2022-11-09 16:25:42 +01:00 committed by GitHub
parent d542fadd63
commit f4660b440b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 5 deletions

View File

@ -292,10 +292,6 @@ describe('Comments API', function () {
await membersAgent.loginAs('member@example.com'); await membersAgent.loginAs('member@example.com');
member = await models.Member.findOne({email: 'member@example.com'}, {require: true}); member = await models.Member.findOne({email: 'member@example.com'}, {require: true});
await membersAgent2.loginAs('member2@example.com'); await membersAgent2.loginAs('member2@example.com');
// Wait before we mock emails from newly created members
// todo: in the future we need a way to wait for DomainEvents to be fired and handled correctly
await sleep(200);
}); });
beforeEach(function () { beforeEach(function () {

View File

@ -18,7 +18,16 @@ class MembersAPITestAgent extends TestAgent {
async loginAs(email) { async loginAs(email) {
const membersService = require('../../../core/server/services/members'); const membersService = require('../../../core/server/services/members');
const magicLink = await membersService.api.getMagicLink(email, 'signup'); const memberRepository = membersService.api.members;
const member = await memberRepository.get({email});
if (!member) {
// Create the member first with context internal if it doesn't exist to prevent sending a signup email
await memberRepository.create({name: '', email}, {context: {internal: true}});
}
const magicLink = await membersService.api.getMagicLink(email, 'signin');
const magicLinkUrl = new URL(magicLink); const magicLinkUrl = new URL(magicLink);
const token = magicLinkUrl.searchParams.get('token'); const token = magicLinkUrl.searchParams.get('token');