2022-02-28 03:15:03 +00:00
|
|
|
import { INestApplication } from '@nestjs/common';
|
2023-04-06 11:12:58 +00:00
|
|
|
import { authenticateUser, clearDB, createNestAppInstance, setupOrganization } from '../test.helper';
|
2022-02-28 03:15:03 +00:00
|
|
|
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 })
|
|
|
|
|
);
|
2023-04-06 11:12:58 +00:00
|
|
|
|
|
|
|
|
const loggedUser = await authenticateUser(nestApp);
|
|
|
|
|
|
2022-02-28 03:15:03 +00:00
|
|
|
const response = await request(nestApp.getHttpServer())
|
|
|
|
|
.post(`/api/folder_apps`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', adminUser.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', loggedUser.tokenCookie)
|
2022-02-28 03:15:03 +00:00
|
|
|
.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);
|
|
|
|
|
});
|
|
|
|
|
|
2022-05-05 06:34:42 +00:00
|
|
|
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 })
|
|
|
|
|
);
|
|
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
const loggedUser = await authenticateUser(nestApp);
|
|
|
|
|
|
2022-05-05 06:34:42 +00:00
|
|
|
await request(nestApp.getHttpServer())
|
|
|
|
|
.post(`/api/folder_apps`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', adminUser.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', loggedUser.tokenCookie)
|
2022-05-05 06:34:42 +00:00
|
|
|
.send({ folder_id: folder.id, app_id: app.id });
|
|
|
|
|
|
|
|
|
|
const response = await request(nestApp.getHttpServer())
|
|
|
|
|
.post(`/api/folder_apps`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', adminUser.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', loggedUser.tokenCookie)
|
2022-05-05 06:34:42 +00:00
|
|
|
.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');
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-28 03:15:03 +00:00
|
|
|
it('should remove an app from a folder', async () => {
|
|
|
|
|
const { adminUser, app } = await setupOrganization(nestApp);
|
2023-04-06 11:12:58 +00:00
|
|
|
|
|
|
|
|
const loggedUser = await authenticateUser(nestApp);
|
|
|
|
|
|
2022-02-28 03:15:03 +00:00
|
|
|
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}`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', adminUser.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', loggedUser.tokenCookie)
|
2022-02-28 03:15:03 +00:00
|
|
|
.send({ app_id: folderApp.appId });
|
|
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-04-06 11:12:58 +00:00
|
|
|
afterAll(async () => {
|
|
|
|
|
await nestApp.close();
|
|
|
|
|
});
|
2022-02-28 03:15:03 +00:00
|
|
|
});
|