|
| 1 | +import request from 'supertest'; |
| 2 | + |
| 3 | +import {routes} from '../../../../routes'; |
| 4 | +import {app, auth} from '../../auth'; |
| 5 | +import {createMockWorkbook, createMockWorkbookEntry} from '../../helpers'; |
| 6 | +import {OpensourceRole} from '../../roles'; |
| 7 | + |
| 8 | +let workbookId: string; |
| 9 | +let workbookEntryId: string; |
| 10 | + |
| 11 | +const notExistingEntryId = 'fvsb9zbfkqos2'; |
| 12 | + |
| 13 | +describe('Delete entry', () => { |
| 14 | + test('[Setup test data] Create workbook and entry for deletion', async () => { |
| 15 | + const workbook = await createMockWorkbook({title: 'Workbook for deletion'}); |
| 16 | + workbookId = workbook.workbookId; |
| 17 | + |
| 18 | + const workbookEntry = await createMockWorkbookEntry({ |
| 19 | + name: 'Entry for deletion', |
| 20 | + workbookId: workbook.workbookId, |
| 21 | + scope: 'dash', |
| 22 | + type: 'wizard-widget', |
| 23 | + }); |
| 24 | + |
| 25 | + workbookEntryId = workbookEntry.entryId; |
| 26 | + }); |
| 27 | + |
| 28 | + test('Delete entry without auth error', async () => { |
| 29 | + await request(app).delete(`${routes.entries}/${workbookEntryId}`).expect(401); |
| 30 | + }); |
| 31 | + |
| 32 | + test('Delete non-existing entry error', async () => { |
| 33 | + await auth(request(app).delete(`${routes.entries}/${notExistingEntryId}`), { |
| 34 | + role: OpensourceRole.Editor, |
| 35 | + }).expect(404); |
| 36 | + }); |
| 37 | + |
| 38 | + test('Delete entry with read-only permissions error', async () => { |
| 39 | + await auth(request(app).delete(`${routes.entries}/${workbookEntryId}`), { |
| 40 | + role: OpensourceRole.Viewer, |
| 41 | + }).expect(403); |
| 42 | + }); |
| 43 | + |
| 44 | + test('Delete entry with wrong filter by scope. Entry is not found', async () => { |
| 45 | + await auth( |
| 46 | + request(app).delete(`${routes.entries}/${workbookEntryId}`).query({ |
| 47 | + scope: 'dataset', |
| 48 | + }), |
| 49 | + { |
| 50 | + role: OpensourceRole.Editor, |
| 51 | + }, |
| 52 | + ).expect(404); |
| 53 | + }); |
| 54 | + |
| 55 | + test('Delete entry with wrong filter by types. Entry is not found', async () => { |
| 56 | + await auth( |
| 57 | + request(app) |
| 58 | + .delete(`${routes.entries}/${workbookEntryId}`) |
| 59 | + .query({ |
| 60 | + types: ['wizard-chart', 'wizard-node'], |
| 61 | + }), |
| 62 | + { |
| 63 | + role: OpensourceRole.Editor, |
| 64 | + }, |
| 65 | + ).expect(404); |
| 66 | + }); |
| 67 | + |
| 68 | + test('Successfully delete entry with filters by scope and types', async () => { |
| 69 | + const newEntry = await createMockWorkbookEntry({ |
| 70 | + name: 'Entry for scope and types test', |
| 71 | + workbookId: workbookId, |
| 72 | + scope: 'dash', |
| 73 | + type: 'wizard-widget', |
| 74 | + }); |
| 75 | + |
| 76 | + const response = await auth( |
| 77 | + request(app) |
| 78 | + .delete(`${routes.entries}/${newEntry.entryId}`) |
| 79 | + .query({ |
| 80 | + scope: 'dash', |
| 81 | + types: ['wizard-widget'], |
| 82 | + }), |
| 83 | + { |
| 84 | + role: OpensourceRole.Editor, |
| 85 | + }, |
| 86 | + ).expect(200); |
| 87 | + |
| 88 | + expect(response.body.isDeleted).toBe(true); |
| 89 | + }); |
| 90 | + |
| 91 | + test('Successfully delete entry', async () => { |
| 92 | + const response = await auth(request(app).delete(`${routes.entries}/${workbookEntryId}`), { |
| 93 | + role: OpensourceRole.Editor, |
| 94 | + }).expect(200); |
| 95 | + |
| 96 | + expect(response.body.isDeleted).toBe(true); |
| 97 | + |
| 98 | + await auth(request(app).get(`${routes.entries}/${workbookEntryId}`), { |
| 99 | + role: OpensourceRole.Editor, |
| 100 | + }).expect(404); |
| 101 | + }); |
| 102 | + |
| 103 | + test('Recover deleted entry', async () => { |
| 104 | + const response = await auth(request(app).post(`${routes.entries}/${workbookEntryId}`), { |
| 105 | + role: OpensourceRole.Editor, |
| 106 | + }) |
| 107 | + .send({ |
| 108 | + mode: 'recover', |
| 109 | + }) |
| 110 | + .expect(200); |
| 111 | + |
| 112 | + expect(response.body.isDeleted).toBeFalsy(); |
| 113 | + |
| 114 | + await auth(request(app).get(`${routes.entries}/${workbookEntryId}`), { |
| 115 | + role: OpensourceRole.Editor, |
| 116 | + }).expect(200); |
| 117 | + }); |
| 118 | +}); |
0 commit comments