|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { WorkspaceAttribute } from 'src/core/types'; |
| 7 | +import * as osdTestServer from '../../../../core/test_helpers/osd_server'; |
| 8 | + |
| 9 | +const omitId = <T extends { id?: string }>(object: T): Omit<T, 'id'> => { |
| 10 | + const { id, ...others } = object; |
| 11 | + return others; |
| 12 | +}; |
| 13 | + |
| 14 | +const testWorkspace: WorkspaceAttribute = { |
| 15 | + id: 'fake_id', |
| 16 | + name: 'test_workspace', |
| 17 | + description: 'test_workspace_description', |
| 18 | +}; |
| 19 | + |
| 20 | +describe('workspace service', () => { |
| 21 | + let root: ReturnType<typeof osdTestServer.createRoot>; |
| 22 | + let opensearchServer: osdTestServer.TestOpenSearchUtils; |
| 23 | + beforeAll(async () => { |
| 24 | + const { startOpenSearch, startOpenSearchDashboards } = osdTestServer.createTestServers({ |
| 25 | + adjustTimeout: (t: number) => jest.setTimeout(t), |
| 26 | + settings: { |
| 27 | + osd: { |
| 28 | + workspace: { |
| 29 | + enabled: true, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }); |
| 34 | + opensearchServer = await startOpenSearch(); |
| 35 | + const startOSDResp = await startOpenSearchDashboards(); |
| 36 | + root = startOSDResp.root; |
| 37 | + }); |
| 38 | + afterAll(async () => { |
| 39 | + await root.shutdown(); |
| 40 | + await opensearchServer.stop(); |
| 41 | + }); |
| 42 | + describe('Workspace CRUD APIs', () => { |
| 43 | + afterEach(async () => { |
| 44 | + const listResult = await osdTestServer.request |
| 45 | + .post(root, `/api/workspaces/_list`) |
| 46 | + .send({ |
| 47 | + page: 1, |
| 48 | + }) |
| 49 | + .expect(200); |
| 50 | + await Promise.all( |
| 51 | + listResult.body.result.workspaces.map((item: WorkspaceAttribute) => |
| 52 | + osdTestServer.request.delete(root, `/api/workspaces/${item.id}`).expect(200) |
| 53 | + ) |
| 54 | + ); |
| 55 | + }); |
| 56 | + it('create', async () => { |
| 57 | + await osdTestServer.request |
| 58 | + .post(root, `/api/workspaces`) |
| 59 | + .send({ |
| 60 | + attributes: testWorkspace, |
| 61 | + }) |
| 62 | + .expect(400); |
| 63 | + |
| 64 | + const result: any = await osdTestServer.request |
| 65 | + .post(root, `/api/workspaces`) |
| 66 | + .send({ |
| 67 | + attributes: omitId(testWorkspace), |
| 68 | + }) |
| 69 | + .expect(200); |
| 70 | + |
| 71 | + expect(result.body.success).toEqual(true); |
| 72 | + expect(typeof result.body.result.id).toBe('string'); |
| 73 | + }); |
| 74 | + it('get', async () => { |
| 75 | + const result = await osdTestServer.request |
| 76 | + .post(root, `/api/workspaces`) |
| 77 | + .send({ |
| 78 | + attributes: omitId(testWorkspace), |
| 79 | + }) |
| 80 | + .expect(200); |
| 81 | + |
| 82 | + const getResult = await osdTestServer.request.get( |
| 83 | + root, |
| 84 | + `/api/workspaces/${result.body.result.id}` |
| 85 | + ); |
| 86 | + expect(getResult.body.result.name).toEqual(testWorkspace.name); |
| 87 | + }); |
| 88 | + it('update', async () => { |
| 89 | + const result: any = await osdTestServer.request |
| 90 | + .post(root, `/api/workspaces`) |
| 91 | + .send({ |
| 92 | + attributes: omitId(testWorkspace), |
| 93 | + }) |
| 94 | + .expect(200); |
| 95 | + |
| 96 | + await osdTestServer.request |
| 97 | + .put(root, `/api/workspaces/${result.body.result.id}`) |
| 98 | + .send({ |
| 99 | + attributes: { |
| 100 | + ...omitId(testWorkspace), |
| 101 | + name: 'updated', |
| 102 | + }, |
| 103 | + }) |
| 104 | + .expect(200); |
| 105 | + |
| 106 | + const getResult = await osdTestServer.request.get( |
| 107 | + root, |
| 108 | + `/api/workspaces/${result.body.result.id}` |
| 109 | + ); |
| 110 | + |
| 111 | + expect(getResult.body.success).toEqual(true); |
| 112 | + expect(getResult.body.result.name).toEqual('updated'); |
| 113 | + }); |
| 114 | + it('delete', async () => { |
| 115 | + const result: any = await osdTestServer.request |
| 116 | + .post(root, `/api/workspaces`) |
| 117 | + .send({ |
| 118 | + attributes: omitId(testWorkspace), |
| 119 | + }) |
| 120 | + .expect(200); |
| 121 | + |
| 122 | + await osdTestServer.request |
| 123 | + .delete(root, `/api/workspaces/${result.body.result.id}`) |
| 124 | + .expect(200); |
| 125 | + |
| 126 | + const getResult = await osdTestServer.request.get( |
| 127 | + root, |
| 128 | + `/api/workspaces/${result.body.result.id}` |
| 129 | + ); |
| 130 | + |
| 131 | + expect(getResult.body.success).toEqual(false); |
| 132 | + }); |
| 133 | + it('list', async () => { |
| 134 | + await osdTestServer.request |
| 135 | + .post(root, `/api/workspaces`) |
| 136 | + .send({ |
| 137 | + attributes: omitId(testWorkspace), |
| 138 | + }) |
| 139 | + .expect(200); |
| 140 | + |
| 141 | + await osdTestServer.request |
| 142 | + .post(root, `/api/workspaces`) |
| 143 | + .send({ |
| 144 | + attributes: { |
| 145 | + ...omitId(testWorkspace), |
| 146 | + name: 'another test workspace', |
| 147 | + }, |
| 148 | + }) |
| 149 | + .expect(200); |
| 150 | + |
| 151 | + const listResult = await osdTestServer.request |
| 152 | + .post(root, `/api/workspaces/_list`) |
| 153 | + .send({ |
| 154 | + page: 1, |
| 155 | + }) |
| 156 | + .expect(200); |
| 157 | + expect(listResult.body.result.total).toEqual(2); |
| 158 | + }); |
| 159 | + it('unable to perform operations on workspace by calling saved objects APIs', async () => { |
| 160 | + const result = await osdTestServer.request |
| 161 | + .post(root, `/api/workspaces`) |
| 162 | + .send({ |
| 163 | + attributes: omitId(testWorkspace), |
| 164 | + }) |
| 165 | + .expect(200); |
| 166 | + |
| 167 | + /** |
| 168 | + * Can not create workspace by saved objects API |
| 169 | + */ |
| 170 | + await osdTestServer.request |
| 171 | + .post(root, `/api/saved_objects/workspace`) |
| 172 | + .send({ |
| 173 | + attributes: { |
| 174 | + ...omitId(testWorkspace), |
| 175 | + name: 'another test workspace', |
| 176 | + }, |
| 177 | + }) |
| 178 | + .expect(400); |
| 179 | + |
| 180 | + /** |
| 181 | + * Can not get workspace by saved objects API |
| 182 | + */ |
| 183 | + await osdTestServer.request |
| 184 | + .get(root, `/api/saved_objects/workspace/${result.body.result.id}`) |
| 185 | + .expect(404); |
| 186 | + |
| 187 | + /** |
| 188 | + * Can not update workspace by saved objects API |
| 189 | + */ |
| 190 | + await osdTestServer.request |
| 191 | + .put(root, `/api/saved_objects/workspace/${result.body.result.id}`) |
| 192 | + .send({ |
| 193 | + attributes: { |
| 194 | + name: 'another test workspace', |
| 195 | + }, |
| 196 | + }) |
| 197 | + .expect(404); |
| 198 | + |
| 199 | + /** |
| 200 | + * Can not delete workspace by saved objects API |
| 201 | + */ |
| 202 | + await osdTestServer.request |
| 203 | + .delete(root, `/api/saved_objects/workspace/${result.body.result.id}`) |
| 204 | + .expect(404); |
| 205 | + |
| 206 | + /** |
| 207 | + * Can not find workspace by saved objects API |
| 208 | + */ |
| 209 | + const findResult = await osdTestServer.request |
| 210 | + .get(root, `/api/saved_objects/_find?type=workspace`) |
| 211 | + .expect(200); |
| 212 | + const listResult = await osdTestServer.request |
| 213 | + .post(root, `/api/workspaces/_list`) |
| 214 | + .send({ |
| 215 | + page: 1, |
| 216 | + }) |
| 217 | + .expect(200); |
| 218 | + expect(findResult.body.total).toEqual(0); |
| 219 | + expect(listResult.body.result.total).toEqual(1); |
| 220 | + }); |
| 221 | + }); |
| 222 | +}); |
0 commit comments