2023-08-29 13:07:05 +03:00
|
|
|
import type { INestApplication } from '@nestjs/common';
|
|
|
|
import { Test } from '@nestjs/testing';
|
|
|
|
import { PrismaClient } from '@prisma/client';
|
2023-09-09 02:02:01 +03:00
|
|
|
import ava, { TestFn } from 'ava';
|
2023-08-29 13:07:05 +03:00
|
|
|
// @ts-expect-error graphql-upload is not typed
|
|
|
|
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
|
|
|
|
|
|
|
|
import { AppModule } from '../app';
|
|
|
|
import { MailService } from '../modules/auth/mailer';
|
|
|
|
import { AuthService } from '../modules/auth/service';
|
|
|
|
import {
|
|
|
|
acceptInvite,
|
|
|
|
acceptInviteById,
|
|
|
|
createWorkspace,
|
2023-09-07 08:08:23 +03:00
|
|
|
getCurrentMailMessageCount,
|
|
|
|
getLatestMailMessage,
|
2023-08-29 13:07:05 +03:00
|
|
|
getWorkspace,
|
|
|
|
inviteUser,
|
|
|
|
leaveWorkspace,
|
|
|
|
revokeUser,
|
|
|
|
signUp,
|
|
|
|
} from './utils';
|
|
|
|
|
2023-09-09 02:02:01 +03:00
|
|
|
const test = ava as TestFn<{
|
|
|
|
app: INestApplication;
|
|
|
|
client: PrismaClient;
|
|
|
|
auth: AuthService;
|
|
|
|
mail: MailService;
|
|
|
|
}>;
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-09 02:02:01 +03:00
|
|
|
test.beforeEach(async t => {
|
|
|
|
const client = new PrismaClient();
|
|
|
|
t.context.client = client;
|
2023-09-01 22:41:29 +03:00
|
|
|
await client.$connect();
|
|
|
|
await client.user.deleteMany({});
|
|
|
|
await client.snapshot.deleteMany({});
|
|
|
|
await client.update.deleteMany({});
|
|
|
|
await client.workspace.deleteMany({});
|
|
|
|
await client.$disconnect();
|
|
|
|
const module = await Test.createTestingModule({
|
|
|
|
imports: [AppModule],
|
|
|
|
}).compile();
|
2023-09-09 02:02:01 +03:00
|
|
|
const app = module.createNestApplication();
|
2023-09-01 22:41:29 +03:00
|
|
|
app.use(
|
|
|
|
graphqlUploadExpress({
|
|
|
|
maxFileSize: 10 * 1024 * 1024,
|
|
|
|
maxFiles: 5,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
await app.init();
|
|
|
|
|
2023-09-09 02:02:01 +03:00
|
|
|
const auth = module.get(AuthService);
|
|
|
|
const mail = module.get(MailService);
|
|
|
|
t.context.app = app;
|
|
|
|
t.context.auth = auth;
|
|
|
|
t.context.mail = mail;
|
2023-09-01 22:41:29 +03:00
|
|
|
});
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-09 02:02:01 +03:00
|
|
|
test.afterEach(async t => {
|
|
|
|
await t.context.app.close();
|
2023-09-01 22:41:29 +03:00
|
|
|
});
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test('should invite a user', async t => {
|
2023-09-09 02:02:01 +03:00
|
|
|
const { app } = t.context;
|
2023-09-01 22:41:29 +03:00
|
|
|
const u1 = await signUp(app, 'u1', 'u1@affine.pro', '1');
|
|
|
|
const u2 = await signUp(app, 'u2', 'u2@affine.pro', '1');
|
|
|
|
|
|
|
|
const workspace = await createWorkspace(app, u1.token.token);
|
|
|
|
|
|
|
|
const invite = await inviteUser(
|
|
|
|
app,
|
|
|
|
u1.token.token,
|
|
|
|
workspace.id,
|
|
|
|
u2.email,
|
|
|
|
'Admin'
|
|
|
|
);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.truthy(invite, 'failed to invite user');
|
2023-09-01 22:41:29 +03:00
|
|
|
});
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test('should accept an invite', async t => {
|
2023-09-09 02:02:01 +03:00
|
|
|
const { app } = t.context;
|
2023-09-01 22:41:29 +03:00
|
|
|
const u1 = await signUp(app, 'u1', 'u1@affine.pro', '1');
|
|
|
|
const u2 = await signUp(app, 'u2', 'u2@affine.pro', '1');
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
const workspace = await createWorkspace(app, u1.token.token);
|
|
|
|
await inviteUser(app, u1.token.token, workspace.id, u2.email, 'Admin');
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
const accept = await acceptInvite(app, u2.token.token, workspace.id);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(accept, true, 'failed to accept invite');
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
const currWorkspace = await getWorkspace(app, u1.token.token, workspace.id);
|
|
|
|
const currMember = currWorkspace.members.find(u => u.email === u2.email);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.not(currMember, undefined, 'failed to invite user');
|
|
|
|
t.is(currMember!.id, u2.id, 'failed to invite user');
|
|
|
|
t.true(!currMember!.accepted, 'failed to invite user');
|
2023-09-01 22:41:29 +03:00
|
|
|
t.pass();
|
|
|
|
});
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test('should leave a workspace', async t => {
|
2023-09-09 02:02:01 +03:00
|
|
|
const { app } = t.context;
|
2023-09-01 22:41:29 +03:00
|
|
|
const u1 = await signUp(app, 'u1', 'u1@affine.pro', '1');
|
|
|
|
const u2 = await signUp(app, 'u2', 'u2@affine.pro', '1');
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
const workspace = await createWorkspace(app, u1.token.token);
|
|
|
|
await inviteUser(app, u1.token.token, workspace.id, u2.email, 'Admin');
|
|
|
|
await acceptInvite(app, u2.token.token, workspace.id);
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
const leave = await leaveWorkspace(app, u2.token.token, workspace.id);
|
2023-09-07 08:08:23 +03:00
|
|
|
|
|
|
|
t.pass();
|
2023-09-05 11:01:45 +03:00
|
|
|
t.true(leave, 'failed to leave workspace');
|
2023-09-01 22:41:29 +03:00
|
|
|
});
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test('should revoke a user', async t => {
|
2023-09-09 02:02:01 +03:00
|
|
|
const { app } = t.context;
|
2023-09-01 22:41:29 +03:00
|
|
|
const u1 = await signUp(app, 'u1', 'u1@affine.pro', '1');
|
|
|
|
const u2 = await signUp(app, 'u2', 'u2@affine.pro', '1');
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
const workspace = await createWorkspace(app, u1.token.token);
|
|
|
|
await inviteUser(app, u1.token.token, workspace.id, u2.email, 'Admin');
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
const currWorkspace = await getWorkspace(app, u1.token.token, workspace.id);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(currWorkspace.members.length, 2, 'failed to invite user');
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
const revoke = await revokeUser(app, u1.token.token, workspace.id, u2.id);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.true(revoke, 'failed to revoke user');
|
2023-09-01 22:41:29 +03:00
|
|
|
});
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test('should create user if not exist', async t => {
|
2023-09-09 02:02:01 +03:00
|
|
|
const { app, auth } = t.context;
|
2023-09-01 22:41:29 +03:00
|
|
|
const u1 = await signUp(app, 'u1', 'u1@affine.pro', '1');
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
const workspace = await createWorkspace(app, u1.token.token);
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
await inviteUser(app, u1.token.token, workspace.id, 'u2@affine.pro', 'Admin');
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
const user = await auth.getUserByEmail('u2@affine.pro');
|
2023-09-05 11:01:45 +03:00
|
|
|
t.not(user, undefined, 'failed to create user');
|
|
|
|
t.is(user?.name, 'Unnamed', 'failed to create user');
|
2023-09-01 22:41:29 +03:00
|
|
|
});
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test('should invite a user by link', async t => {
|
2023-09-09 02:02:01 +03:00
|
|
|
const { app } = t.context;
|
2023-09-01 22:41:29 +03:00
|
|
|
const u1 = await signUp(app, 'u1', 'u1@affine.pro', '1');
|
|
|
|
const u2 = await signUp(app, 'u2', 'u2@affine.pro', '1');
|
|
|
|
|
|
|
|
const workspace = await createWorkspace(app, u1.token.token);
|
|
|
|
|
|
|
|
const invite = await inviteUser(
|
|
|
|
app,
|
|
|
|
u1.token.token,
|
|
|
|
workspace.id,
|
|
|
|
u2.email,
|
|
|
|
'Admin'
|
|
|
|
);
|
|
|
|
|
|
|
|
const accept = await acceptInviteById(app, workspace.id, invite);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.true(accept, 'failed to accept invite');
|
2023-09-01 22:41:29 +03:00
|
|
|
|
|
|
|
const invite1 = await inviteUser(
|
|
|
|
app,
|
|
|
|
u1.token.token,
|
|
|
|
workspace.id,
|
|
|
|
u2.email,
|
|
|
|
'Admin'
|
|
|
|
);
|
|
|
|
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(invite, invite1, 'repeat the invitation must return same id');
|
2023-09-01 22:41:29 +03:00
|
|
|
|
|
|
|
const currWorkspace = await getWorkspace(app, u1.token.token, workspace.id);
|
|
|
|
const currMember = currWorkspace.members.find(u => u.email === u2.email);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.not(currMember, undefined, 'failed to invite user');
|
|
|
|
t.is(currMember?.inviteId, invite, 'failed to check invite id');
|
2023-09-01 22:41:29 +03:00
|
|
|
});
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-07 08:08:23 +03:00
|
|
|
test('should send email', async t => {
|
2023-09-09 02:02:01 +03:00
|
|
|
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, 'test', 'production@toeverything.info', '1');
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
const workspace = await createWorkspace(app, u1.token.token);
|
2023-09-07 08:08:23 +03:00
|
|
|
const primitiveMailCount = await getCurrentMailMessageCount();
|
|
|
|
|
|
|
|
const invite = await inviteUser(
|
2023-08-29 13:07:05 +03:00
|
|
|
app,
|
|
|
|
u1.token.token,
|
|
|
|
workspace.id,
|
|
|
|
u2.email,
|
2023-09-01 22:41:29 +03:00
|
|
|
'Admin',
|
|
|
|
true
|
2023-08-29 13:07:05 +03:00
|
|
|
);
|
2023-09-07 08:08:23 +03:00
|
|
|
|
|
|
|
const afterInviteMailCount = await getCurrentMailMessageCount();
|
|
|
|
t.is(
|
|
|
|
primitiveMailCount + 1,
|
|
|
|
afterInviteMailCount,
|
|
|
|
'failed to send invite email'
|
|
|
|
);
|
|
|
|
const inviteEmailContent = await getLatestMailMessage();
|
|
|
|
|
|
|
|
t.not(
|
|
|
|
// @ts-expect-error Third part library type mismatch
|
|
|
|
inviteEmailContent.To.find(item => {
|
|
|
|
return item.Mailbox === 'production';
|
|
|
|
}),
|
|
|
|
undefined,
|
|
|
|
'invite email address was incorrectly sent'
|
|
|
|
);
|
|
|
|
|
|
|
|
const accept = await acceptInviteById(app, workspace.id, invite, true);
|
|
|
|
t.true(accept, 'failed to accept invite');
|
|
|
|
|
|
|
|
const afterAcceptMailCount = await getCurrentMailMessageCount();
|
|
|
|
t.is(
|
|
|
|
afterInviteMailCount + 1,
|
|
|
|
afterAcceptMailCount,
|
|
|
|
'failed to send accepted email to owner'
|
|
|
|
);
|
|
|
|
const acceptEmailContent = await getLatestMailMessage();
|
|
|
|
t.not(
|
|
|
|
// @ts-expect-error Third part library type mismatch
|
|
|
|
acceptEmailContent.To.find(item => {
|
|
|
|
return item.Mailbox === 'u1';
|
|
|
|
}),
|
|
|
|
undefined,
|
|
|
|
'accept email address was incorrectly sent'
|
|
|
|
);
|
|
|
|
|
|
|
|
await leaveWorkspace(app, u2.token.token, workspace.id, true);
|
|
|
|
|
|
|
|
const afterLeaveMailCount = await getCurrentMailMessageCount();
|
|
|
|
t.is(
|
|
|
|
afterAcceptMailCount + 1,
|
|
|
|
afterLeaveMailCount,
|
|
|
|
'failed to send leave email to owner'
|
|
|
|
);
|
|
|
|
const leaveEmailContent = await getLatestMailMessage();
|
|
|
|
t.not(
|
|
|
|
// @ts-expect-error Third part library type mismatch
|
|
|
|
leaveEmailContent.To.find(item => {
|
|
|
|
return item.Mailbox === 'u1';
|
|
|
|
}),
|
|
|
|
undefined,
|
|
|
|
'leave email address was incorrectly sent'
|
|
|
|
);
|
2023-09-01 22:41:29 +03:00
|
|
|
}
|
|
|
|
t.pass();
|
2023-08-29 13:07:05 +03:00
|
|
|
});
|