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-01 22:41:29 +03:00
|
|
|
import test from 'ava';
|
2023-08-29 13:07:05 +03:00
|
|
|
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
|
|
|
|
import request from 'supertest';
|
|
|
|
|
2023-09-15 10:34:14 +03:00
|
|
|
import { AppModule } from '../src/app';
|
2023-08-29 13:07:05 +03:00
|
|
|
import { currentUser, signUp } from './utils';
|
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
let app: INestApplication;
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
// cleanup database before each test
|
|
|
|
test.beforeEach(async () => {
|
|
|
|
const client = new PrismaClient();
|
|
|
|
await client.$connect();
|
|
|
|
await client.user.deleteMany({});
|
|
|
|
await client.$disconnect();
|
|
|
|
});
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test.beforeEach(async () => {
|
|
|
|
const module = await Test.createTestingModule({
|
|
|
|
imports: [AppModule],
|
|
|
|
}).compile();
|
|
|
|
app = module.createNestApplication();
|
|
|
|
app.use(
|
|
|
|
graphqlUploadExpress({
|
|
|
|
maxFileSize: 10 * 1024 * 1024,
|
|
|
|
maxFiles: 5,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
await app.init();
|
|
|
|
});
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-11 12:30:39 +03:00
|
|
|
test.afterEach.always(async () => {
|
2023-09-01 22:41:29 +03:00
|
|
|
await app.close();
|
|
|
|
});
|
2023-08-29 13:07:05 +03:00
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test('should register a user', async t => {
|
|
|
|
const user = await signUp(app, 'u1', 'u1@affine.pro', '123456');
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(typeof user.id, 'string', 'user.id is not a string');
|
|
|
|
t.is(user.name, 'u1', 'user.name is not valid');
|
|
|
|
t.is(user.email, 'u1@affine.pro', 'user.email is not valid');
|
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 get current user', async t => {
|
|
|
|
const user = await signUp(app, 'u1', 'u1@affine.pro', '123456');
|
|
|
|
const currUser = await currentUser(app, user.token.token);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(currUser.id, user.id, 'user.id is not valid');
|
|
|
|
t.is(currUser.name, user.name, 'user.name is not valid');
|
|
|
|
t.is(currUser.email, user.email, 'user.email is not valid');
|
|
|
|
t.true(currUser.hasPassword, 'currUser.hasPassword is not valid');
|
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 be able to delete user', async t => {
|
|
|
|
const user = await signUp(app, 'u1', 'u1@affine.pro', '123456');
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.post('/graphql')
|
|
|
|
.auth(user.token.token, { type: 'bearer' })
|
|
|
|
.send({
|
|
|
|
query: `
|
2023-08-29 13:07:05 +03:00
|
|
|
mutation {
|
|
|
|
deleteAccount {
|
|
|
|
success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
2023-09-01 22:41:29 +03:00
|
|
|
})
|
|
|
|
.expect(200);
|
2023-10-19 05:06:34 +03:00
|
|
|
t.is(await currentUser(app, user.token.token), null);
|
2023-09-01 22:41:29 +03:00
|
|
|
t.pass();
|
2023-08-29 13:07:05 +03:00
|
|
|
});
|