feat: implement e2e test for CompanyResolver (#944)
* feat: wip e2e server test * feat: use github action postgres & use infra for local * feat: company e2e test * feat: add company e2e test for permissions * Simplify server e2e test run * Fix lint --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -1,24 +1,31 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import * as request from 'supertest';
|
||||
import { AppModule } from './../src/app.module';
|
||||
|
||||
import request from 'supertest';
|
||||
|
||||
import { createApp } from './utils/create-app';
|
||||
|
||||
describe('AppController (e2e)', () => {
|
||||
let app: INestApplication;
|
||||
|
||||
beforeEach(async () => {
|
||||
const moduleFixture: TestingModule = await Test.createTestingModule({
|
||||
imports: [AppModule],
|
||||
}).compile();
|
||||
|
||||
app = moduleFixture.createNestApplication();
|
||||
await app.init();
|
||||
[app] = await createApp();
|
||||
});
|
||||
|
||||
it('/ (GET)', () => {
|
||||
afterEach(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('/healthz (GET)', () => {
|
||||
return request(app.getHttpServer())
|
||||
.get('/')
|
||||
.get('/healthz')
|
||||
.expect(200)
|
||||
.expect('Hello World!');
|
||||
.expect((response) => {
|
||||
expect(response.body).toEqual({
|
||||
status: 'ok',
|
||||
info: { database: { status: 'up' } },
|
||||
error: {},
|
||||
details: { database: { status: 'up' } },
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
298
server/test/company.e2e-spec.ts
Normal file
298
server/test/company.e2e-spec.ts
Normal file
@ -0,0 +1,298 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
|
||||
import request from 'supertest';
|
||||
|
||||
import { JwtAuthGuard } from 'src/guards/jwt.auth.guard';
|
||||
|
||||
import { createApp } from './utils/create-app';
|
||||
|
||||
describe('CompanyResolver (e2e)', () => {
|
||||
let app: INestApplication;
|
||||
let companyId: string | undefined;
|
||||
|
||||
const authGuardMock = { canActivate: (): any => true };
|
||||
|
||||
beforeEach(async () => {
|
||||
[app] = await createApp({
|
||||
moduleBuilderHook: (moduleBuilder) =>
|
||||
moduleBuilder.overrideGuard(JwtAuthGuard).useValue(authGuardMock),
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('should create a company', () => {
|
||||
const queryData = {
|
||||
query: `
|
||||
mutation CreateOneCompany($data: CompanyCreateInput!) {
|
||||
createOneCompany(data: $data) {
|
||||
id
|
||||
name
|
||||
domainName
|
||||
address
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
data: {
|
||||
name: 'New Company',
|
||||
domainName: 'new-company.com',
|
||||
address: 'New Address',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return request(app.getHttpServer())
|
||||
.post('/graphql')
|
||||
.send(queryData)
|
||||
.expect(200)
|
||||
.expect((res) => {
|
||||
const data = res.body.data.createOneCompany;
|
||||
|
||||
companyId = data.id;
|
||||
|
||||
expect(data).toBeDefined();
|
||||
expect(data).toHaveProperty('id');
|
||||
expect(data).toHaveProperty('name', 'New Company');
|
||||
expect(data).toHaveProperty('domainName', 'new-company.com');
|
||||
expect(data).toHaveProperty('address', 'New Address');
|
||||
});
|
||||
});
|
||||
|
||||
it('should find many companies', () => {
|
||||
const queryData = {
|
||||
query: `
|
||||
query FindManyCompany {
|
||||
findManyCompany {
|
||||
id
|
||||
name
|
||||
domainName
|
||||
address
|
||||
}
|
||||
}
|
||||
`,
|
||||
};
|
||||
|
||||
return request(app.getHttpServer())
|
||||
.post('/graphql')
|
||||
.send(queryData)
|
||||
.expect(200)
|
||||
.expect((res) => {
|
||||
const data = res.body.data.findManyCompany;
|
||||
expect(data).toBeDefined();
|
||||
expect(Array.isArray(data)).toBe(true);
|
||||
expect(data.length).toBeGreaterThan(0);
|
||||
|
||||
const company = data.find((c) => c.id === companyId);
|
||||
expect(company).toBeDefined();
|
||||
expect(company).toHaveProperty('id');
|
||||
expect(company).toHaveProperty('name', 'New Company');
|
||||
expect(company).toHaveProperty('domainName', 'new-company.com');
|
||||
expect(company).toHaveProperty('address', 'New Address');
|
||||
|
||||
// Check if we have access to ressources outside of our workspace
|
||||
const instagramCompany = data.find((c) => c.name === 'Instagram');
|
||||
expect(instagramCompany).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
it('should find unique company', () => {
|
||||
const queryData = {
|
||||
query: `
|
||||
query FindUniqueCompany($where: CompanyWhereUniqueInput!) {
|
||||
findUniqueCompany(where: $where) {
|
||||
id
|
||||
name
|
||||
domainName
|
||||
address
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
where: {
|
||||
id: companyId,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return request(app.getHttpServer())
|
||||
.post('/graphql')
|
||||
.send(queryData)
|
||||
.expect(200)
|
||||
.expect((res) => {
|
||||
const data = res.body.data.findUniqueCompany;
|
||||
|
||||
expect(data).toBeDefined();
|
||||
expect(data).toHaveProperty('id');
|
||||
expect(data).toHaveProperty('name', 'New Company');
|
||||
expect(data).toHaveProperty('domainName', 'new-company.com');
|
||||
expect(data).toHaveProperty('address', 'New Address');
|
||||
});
|
||||
});
|
||||
|
||||
it('should not find unique company (forbidden because outside workspace)', () => {
|
||||
const queryData = {
|
||||
query: `
|
||||
query FindUniqueCompany($where: CompanyWhereUniqueInput!) {
|
||||
findUniqueCompany(where: $where) {
|
||||
id
|
||||
name
|
||||
domainName
|
||||
address
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
where: {
|
||||
id: 'twenty-dev-a674fa6c-1455-4c57-afaf-dd5dc086361e',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return request(app.getHttpServer())
|
||||
.post('/graphql')
|
||||
.send(queryData)
|
||||
.expect(200)
|
||||
.expect((res) => {
|
||||
const errors = res.body.errors;
|
||||
const error = errors?.[0];
|
||||
|
||||
expect(error).toBeDefined();
|
||||
expect(error.extensions.code).toBe('FORBIDDEN');
|
||||
expect(error.extensions.originalError.statusCode).toBe(403);
|
||||
});
|
||||
});
|
||||
|
||||
it('should update a company', () => {
|
||||
const queryData = {
|
||||
query: `
|
||||
mutation UpdateOneCompany($where: CompanyWhereUniqueInput!, $data: CompanyUpdateInput!) {
|
||||
updateOneCompany(data: $data, where: $where) {
|
||||
id
|
||||
name
|
||||
domainName
|
||||
address
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
where: {
|
||||
id: companyId,
|
||||
},
|
||||
data: {
|
||||
name: 'Updated Company',
|
||||
domainName: 'updated-company.com',
|
||||
address: 'Updated Address',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return request(app.getHttpServer())
|
||||
.post('/graphql')
|
||||
.send(queryData)
|
||||
.expect(200)
|
||||
.expect((res) => {
|
||||
const data = res.body.data.updateOneCompany;
|
||||
|
||||
expect(data).toBeDefined();
|
||||
expect(data).toHaveProperty('id');
|
||||
expect(data).toHaveProperty('name', 'Updated Company');
|
||||
expect(data).toHaveProperty('domainName', 'updated-company.com');
|
||||
expect(data).toHaveProperty('address', 'Updated Address');
|
||||
});
|
||||
});
|
||||
|
||||
it('should not update a company (forbidden because outside workspace)', () => {
|
||||
const queryData = {
|
||||
query: `
|
||||
mutation UpdateOneCompany($where: CompanyWhereUniqueInput!, $data: CompanyUpdateInput!) {
|
||||
updateOneCompany(data: $data, where: $where) {
|
||||
id
|
||||
name
|
||||
domainName
|
||||
address
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
where: {
|
||||
id: 'twenty-dev-a674fa6c-1455-4c57-afaf-dd5dc086361e',
|
||||
},
|
||||
data: {
|
||||
name: 'Updated Instagram',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return request(app.getHttpServer())
|
||||
.post('/graphql')
|
||||
.send(queryData)
|
||||
.expect(200)
|
||||
.expect((res) => {
|
||||
const errors = res.body.errors;
|
||||
const error = errors?.[0];
|
||||
|
||||
expect(error).toBeDefined();
|
||||
expect(error.extensions.code).toBe('FORBIDDEN');
|
||||
expect(error.extensions.originalError.statusCode).toBe(403);
|
||||
});
|
||||
});
|
||||
|
||||
it('should delete a company', () => {
|
||||
const queryData = {
|
||||
query: `
|
||||
mutation DeleteManyCompany($ids: [String!]) {
|
||||
deleteManyCompany(where: {id: {in: $ids}}) {
|
||||
count
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
ids: [companyId],
|
||||
},
|
||||
};
|
||||
|
||||
return request(app.getHttpServer())
|
||||
.post('/graphql')
|
||||
.send(queryData)
|
||||
.expect(200)
|
||||
.expect((res) => {
|
||||
const data = res.body.data.deleteManyCompany;
|
||||
|
||||
companyId = undefined;
|
||||
|
||||
expect(data).toBeDefined();
|
||||
expect(data).toHaveProperty('count', 1);
|
||||
});
|
||||
});
|
||||
|
||||
it('should not delete a company (forbidden because outside workspace)', () => {
|
||||
const queryData = {
|
||||
query: `
|
||||
mutation DeleteManyCompany($ids: [String!]) {
|
||||
deleteManyCompany(where: {id: {in: $ids}}) {
|
||||
count
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
ids: ['twenty-dev-a674fa6c-1455-4c57-afaf-dd5dc086361e'],
|
||||
},
|
||||
};
|
||||
|
||||
return request(app.getHttpServer())
|
||||
.post('/graphql')
|
||||
.send(queryData)
|
||||
.expect(200)
|
||||
.expect((res) => {
|
||||
const errors = res.body.errors;
|
||||
const error = errors?.[0];
|
||||
|
||||
expect(error).toBeDefined();
|
||||
expect(error.extensions.code).toBe('FORBIDDEN');
|
||||
expect(error.extensions.originalError.statusCode).toBe(403);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -3,6 +3,11 @@
|
||||
"rootDir": ".",
|
||||
"testEnvironment": "node",
|
||||
"testRegex": ".e2e-spec.ts$",
|
||||
"setupFilesAfterEnv": ["<rootDir>/utils/setup-tests.ts"],
|
||||
"moduleNameMapper": {
|
||||
"^src/(.*)": "<rootDir>/../src/$1",
|
||||
"^test/(.*)": "<rootDir>/$1"
|
||||
},
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
}
|
||||
|
||||
9
server/test/mock-data/user.json
Normal file
9
server/test/mock-data/user.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"id": "twenty-ge256b39-3ec3-4fe3-8997-b76aa0bfc102",
|
||||
"firstName": "Tim",
|
||||
"lastName": "Apple",
|
||||
"email": "tim@apple.dev",
|
||||
"locale": "en",
|
||||
"passwordHash": "$2b$10$66d.6DuQExxnrfI9rMqOg.U1XIYpagr6Lv05uoWLYbYmtK0HDIvS6",
|
||||
"avatarUrl": null
|
||||
}
|
||||
7
server/test/mock-data/workspace.json
Normal file
7
server/test/mock-data/workspace.json
Normal file
File diff suppressed because one or more lines are too long
41
server/test/utils/check-db.ts
Normal file
41
server/test/utils/check-db.ts
Normal file
@ -0,0 +1,41 @@
|
||||
// check-db.ts
|
||||
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
const schemaDatabaseExists = async (databaseName: string) => {
|
||||
try {
|
||||
const result = await prisma.$queryRawUnsafe<[any]>(
|
||||
`SELECT 1 FROM pg_database WHERE datname = '${databaseName}';`,
|
||||
);
|
||||
|
||||
return result.length > 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
async function main() {
|
||||
const databaseName = 'tests';
|
||||
// Check if schema exists
|
||||
const databaseExistsResult = await schemaDatabaseExists(databaseName);
|
||||
|
||||
if (!databaseExistsResult) {
|
||||
throw new Error(`Schema ${databaseName} does not exist`);
|
||||
}
|
||||
|
||||
// Check if database is initialized
|
||||
await prisma.$queryRaw`SELECT 1 FROM pg_tables WHERE tablename='_prisma_migrations';`;
|
||||
}
|
||||
|
||||
main()
|
||||
.then(() => {
|
||||
process.exit(0);
|
||||
})
|
||||
.catch(() => {
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
56
server/test/utils/create-app.ts
Normal file
56
server/test/utils/create-app.ts
Normal file
@ -0,0 +1,56 @@
|
||||
import { NestExpressApplication } from '@nestjs/platform-express';
|
||||
import { Test, TestingModule, TestingModuleBuilder } from '@nestjs/testing';
|
||||
|
||||
import mockUser from 'test/mock-data/user.json';
|
||||
import mockWorkspace from 'test/mock-data/workspace.json';
|
||||
import { RequestHandler } from 'express';
|
||||
|
||||
import { AppModule } from 'src/app.module';
|
||||
|
||||
interface TestingModuleCreatePreHook {
|
||||
(moduleBuilder: TestingModuleBuilder): TestingModuleBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for adding items to nest application
|
||||
*/
|
||||
export type TestingAppCreatePreHook = (
|
||||
app: NestExpressApplication,
|
||||
) => Promise<void>;
|
||||
|
||||
/**
|
||||
* Sets basic e2e testing module of app
|
||||
*/
|
||||
export async function createApp(
|
||||
config: {
|
||||
moduleBuilderHook?: TestingModuleCreatePreHook;
|
||||
appInitHook?: TestingAppCreatePreHook;
|
||||
} = {},
|
||||
): Promise<[NestExpressApplication, TestingModule]> {
|
||||
let moduleBuilder: TestingModuleBuilder = Test.createTestingModule({
|
||||
imports: [AppModule],
|
||||
});
|
||||
|
||||
if (!!config.moduleBuilderHook) {
|
||||
moduleBuilder = config.moduleBuilderHook(moduleBuilder);
|
||||
}
|
||||
|
||||
const moduleFixture: TestingModule = await moduleBuilder.compile();
|
||||
const app = moduleFixture.createNestApplication<NestExpressApplication>();
|
||||
|
||||
if (config.appInitHook) {
|
||||
await config.appInitHook(app);
|
||||
}
|
||||
|
||||
const mockAuthHandler: RequestHandler = (req, _res, next) => {
|
||||
req.user = {
|
||||
user: mockUser,
|
||||
workspace: mockWorkspace,
|
||||
};
|
||||
next();
|
||||
};
|
||||
|
||||
app.use(mockAuthHandler);
|
||||
|
||||
return [await app.init(), moduleFixture];
|
||||
}
|
||||
18
server/test/utils/reset-db.ts
Normal file
18
server/test/utils/reset-db.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { PrismaClient, Prisma } from '@prisma/client';
|
||||
|
||||
import { camelCase } from 'src/utils/camel-case';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export default async () => {
|
||||
const models = Prisma.dmmf.datamodel.models;
|
||||
const modelNames = models.map((model) => model.name);
|
||||
const entities = modelNames.map((modelName) => camelCase(modelName));
|
||||
|
||||
await prisma.$transaction(
|
||||
entities.map((entity) => {
|
||||
console.log('entity: ', entity);
|
||||
return prisma[entity].deleteMany();
|
||||
}),
|
||||
);
|
||||
};
|
||||
5
server/test/utils/setup-tests.ts
Normal file
5
server/test/utils/setup-tests.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import resetDb from './reset-db';
|
||||
|
||||
global.beforeEach(() => {
|
||||
// resetDb();
|
||||
});
|
||||
Reference in New Issue
Block a user