AFFiNE/packages/backend/server/tests/mailer.spec.ts

69 lines
1.6 KiB
TypeScript
Raw Normal View History

import type { INestApplication } from '@nestjs/common';
import type { TestFn } from 'ava';
import ava from 'ava';
import Sinon from 'sinon';
import { AppModule } from '../src/app.module';
import { FeatureManagementService } from '../src/core/features';
import { MailService } from '../src/fundamentals/mailer';
import { createTestingApp, createWorkspace, inviteUser, signUp } from './utils';
const test = ava as TestFn<{
app: INestApplication;
mail: MailService;
}>;
test.beforeEach(async t => {
const { module, app } = await createTestingApp({
2023-09-01 22:41:29 +03:00
imports: [AppModule],
tapModule: module => {
module.overrideProvider(FeatureManagementService).useValue({
hasWorkspaceFeature() {
return false;
},
});
},
});
const mail = module.get(MailService);
t.context.app = app;
t.context.mail = mail;
2023-09-01 22:41:29 +03:00
});
test.afterEach.always(async t => {
await t.context.app.close();
2023-09-01 22:41:29 +03:00
});
2023-09-01 22:41:29 +03:00
test('should send invite email', async t => {
const { mail, app } = t.context;
2023-09-01 22:41:29 +03:00
if (mail.hasConfigured()) {
const u1 = await signUp(app, 'u1', 'u1@affine.pro', '1');
const u2 = await signUp(app, 'u2', 'u2@affine.pro', '1');
2023-09-01 22:41:29 +03:00
const workspace = await createWorkspace(app, u1.token.token);
const stub = Sinon.stub(mail, 'sendMail');
await inviteUser(
2023-09-01 22:41:29 +03:00
app,
u1.token.token,
workspace.id,
u2.email,
'Admin',
true
2023-09-01 22:41:29 +03:00
);
t.true(stub.calledOnce);
const args = stub.args[0][0];
t.is(args.to, u2.email);
t.true(
args.subject!.startsWith(
`${u1.name} invited you to join` /* we don't know the name of mocked workspace */
)
);
2023-09-01 22:41:29 +03:00
}
t.pass();
});