-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapi-get-layout.test.js
More file actions
154 lines (141 loc) · 6.39 KB
/
api-get-layout.test.js
File metadata and controls
154 lines (141 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
import { suite, test } from 'node:test';
import { OWNER_TEST_TOKEN, URL_ADDRESS } from '../config.js';
import request from 'supertest';
import { deepStrictEqual } from 'node:assert';
import { LAYOUT_MOCK_4, LAYOUT_MOCK_5, LAYOUT_MOCK_6 } from '../../demoData/layout/layout.mock.js';
import { addLabelsToLayout } from '../../../lib/utils/layout/addLabelsToLayout.js';
export const apiGetLayoutsTests = () => {
suite('GET /layouts', () => {
test('should return all layouts when no filters are provided', async () => {
await request(`${URL_ADDRESS}/api/layouts`)
.get(`?token=${OWNER_TEST_TOKEN}`)
.expect(200)
.expect((res) => {
if (!Array.isArray(res.body)) {
throw new Error('Expected array of layouts');
}
if (res.body.length < 2) {
throw new Error(`Expected at least 3 layouts ${res.body.length}`);
}
});
});
test('should return layouts filtered by owner_id', async () => {
const ownerId = 0;
await request(`${URL_ADDRESS}/api/layouts`)
.get(`?token=${OWNER_TEST_TOKEN}&owner_id=${ownerId}`)
.expect(200)
.expect((res) => {
if (!Array.isArray(res.body)) {
throw new Error('Expected array of layouts');
}
res.body.forEach((layout) => {
delete layout.labels; // remove labels for deep comparison
});
deepStrictEqual(res.body, [LAYOUT_MOCK_4, LAYOUT_MOCK_5], 'Unexpected Layout structure was returned');
});
});
test('should return specific fields when fields parameter is provided', async () => {
const fields = 'name,owner_id';
await request(`${URL_ADDRESS}/api/layouts`)
.get(`?token=${OWNER_TEST_TOKEN}&fields=${fields}`)
.expect(200)
.expect((res) => {
if (!Array.isArray(res.body)) {
throw new Error('Expected array of layouts');
}
res.body.forEach((layout) => {
const hasName = Object.prototype.hasOwnProperty.call(layout, 'name');
const hasOwnerId = Object.prototype.hasOwnProperty.call(layout, 'owner_id');
if (Object.keys(layout).length !== 2 || !hasName || !hasOwnerId) {
throw new Error(`Expected only name and owner_id fields but instead got: ${Object.keys(layout)}`);
}
});
});
});
test('should return 400 for invalid query parameters', async () => {
await request(`${URL_ADDRESS}/api/layouts`)
.get(`?token=${OWNER_TEST_TOKEN}&invalid_param=value`)
.expect(400, {
message: 'Invalid query parameters: "invalid_param" is not allowed',
status: 400,
title: 'Invalid Input' });
});
});
suite('GET /layout/:id', () => {
test('should return a single layout by id', async () => {
const layoutId = '671b8c22402408122e2f20dd';
const expectedLayout = addLabelsToLayout(LAYOUT_MOCK_6);
await request(`${URL_ADDRESS}/api/layout/${layoutId}`)
.get(`?token=${OWNER_TEST_TOKEN}`)
.expect(200)
.expect((res) => deepStrictEqual(res.body, expectedLayout, 'Unexpected Layout structure was returned'));
});
test('should return 400 when id parameter is an empty string', async () => {
await request(`${URL_ADDRESS}/api/layout/ `)
.get(`?token=${OWNER_TEST_TOKEN}`)
.expect(400, { message: 'Missing parameter "id" of layout', status: 400, title: 'Invalid Input' });
});
test('should return 404 when layout is not found', async () => {
const nonExistentId = 'nonexistent123';
await request(`${URL_ADDRESS}/api/layout/${nonExistentId}`)
.get(`?token=${OWNER_TEST_TOKEN}`)
.expect(404, { message: 'layout (nonexistent123) not found', status: 404, title: 'Not Found' });
});
});
suite('GET /layout?name=', () => {
test('should return layout by name', async () => {
const layoutName = 'a-test';
const expectedLayout = addLabelsToLayout(LAYOUT_MOCK_5);
await request(`${URL_ADDRESS}/api/layout`)
.get(`?token=${OWNER_TEST_TOKEN}&name=${layoutName}`)
.expect(200)
.expect((res) => deepStrictEqual(res.body, expectedLayout, 'Unexpected Layout structure was returned'));
});
test('should return layout by runDefinition', async () => {
const runDefinition = 'a-test';
const expectedLayout = addLabelsToLayout(LAYOUT_MOCK_5);
await request(`${URL_ADDRESS}/api/layout`)
.get(`?token=${OWNER_TEST_TOKEN}&runDefinition=${runDefinition}`)
.expect(200)
.expect((res) => deepStrictEqual(res.body, expectedLayout, 'Unexpected Layout structure was returned'));
});
test('should return layout by runDefinition and pdpBeamType combination', async () => {
const runDefinition = 'rundefinition';
const pdpBeamType = 'pdpBeamType';
await request(`${URL_ADDRESS}/api/layout`)
.get(`?token=${OWNER_TEST_TOKEN}&runDefinition=${runDefinition}&pdpBeamType=${pdpBeamType}`)
.expect(200)
.expect((res) => {
deepStrictEqual(
res.body.name,
`${runDefinition}_${pdpBeamType}`,
'Expected layout name to be combination of runDefinition and pdpBeamType',
);
});
});
test('should return 400 when no query parameters are provided', async () => {
await request(`${URL_ADDRESS}/api/layout`)
.get(`?token=${OWNER_TEST_TOKEN}`)
.expect(400, { message: 'Missing query parameters', status: 400, title: 'Invalid Input' });
});
test('should return 404 when layout is not found', async () => {
const nonExistentName = 'nonexistent-layout';
await request(`${URL_ADDRESS}/api/layout`)
.get(`?token=${OWNER_TEST_TOKEN}&name=${nonExistentName}`)
.expect(404, { message: `Layout (${nonExistentName}) not found`, status: 404, title: 'Not Found' });
});
});
};