ToolJet/server/test/controllers/folder_apps.e2e-spec.ts
Ankush a07b8e3439
Fixes addition of application to folder more than once (#2961)
* fixed addition of application to folder more than once

* Update server/src/services/folder_apps.service.ts

Co-authored-by: Midhun G S <gsmithun4@gmail.com>

Co-authored-by: Midhun G S <gsmithun4@gmail.com>
2022-05-05 12:04:42 +05:30

83 lines
3.1 KiB
TypeScript

import { INestApplication } from '@nestjs/common';
import { authHeaderForUser, clearDB, createNestAppInstance, setupOrganization } from '../test.helper';
import * as request from 'supertest';
import { getManager } from 'typeorm';
import { Folder } from '../../src/entities/folder.entity';
import { FolderApp } from '../../src/entities/folder_app.entity';
describe('folder apps controller', () => {
let nestApp: INestApplication;
beforeEach(async () => {
await clearDB();
});
beforeAll(async () => {
nestApp = await createNestAppInstance();
});
describe('POST /api/folder_apps', () => {
it('should allow only authenticated users to add apps to folders', async () => {
await request(nestApp.getHttpServer()).post('/api/folder_apps').expect(401);
});
it('should add an app to a folder', async () => {
const { adminUser, app } = await setupOrganization(nestApp);
const manager = getManager();
// create a new folder
const folder = await manager.save(
manager.create(Folder, { name: 'folder', organizationId: adminUser.organizationId })
);
const response = await request(nestApp.getHttpServer())
.post(`/api/folder_apps`)
.set('Authorization', authHeaderForUser(adminUser))
.send({ folder_id: folder.id, app_id: app.id });
expect(response.statusCode).toBe(201);
const { id, app_id, folder_id } = response.body;
expect(id).toBeDefined();
expect(app_id).toBe(app.id);
expect(folder_id).toBe(folder.id);
});
it('should not add an app to a folder more than once', async () => {
const { adminUser, app } = await setupOrganization(nestApp);
const manager = getManager();
// create a new folder
const folder = await manager.save(
manager.create(Folder, { name: 'folder', organizationId: adminUser.organizationId })
);
await request(nestApp.getHttpServer())
.post(`/api/folder_apps`)
.set('Authorization', authHeaderForUser(adminUser))
.send({ folder_id: folder.id, app_id: app.id });
const response = await request(nestApp.getHttpServer())
.post(`/api/folder_apps`)
.set('Authorization', authHeaderForUser(adminUser))
.send({ folder_id: folder.id, app_id: app.id });
expect(response.statusCode).toBe(400);
expect(response.body.message).toBe('App has been already added to the folder');
});
it('should remove an app from a folder', async () => {
const { adminUser, app } = await setupOrganization(nestApp);
const manager = getManager();
// create a new folder
const folder = await manager.save(
manager.create(Folder, { name: 'folder', organizationId: adminUser.organizationId })
);
// add app to folder
const folderApp = await manager.save(manager.create(FolderApp, { folderId: folder.id, appId: app.id }));
const response = await request(nestApp.getHttpServer())
.put(`/api/folder_apps/${folderApp.folderId}`)
.set('Authorization', authHeaderForUser(adminUser))
.send({ app_id: folderApp.appId });
expect(response.statusCode).toBe(200);
});
});
});