-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrest.test.ts
More file actions
91 lines (70 loc) · 2.51 KB
/
rest.test.ts
File metadata and controls
91 lines (70 loc) · 2.51 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
// Tests of the REST API
import test from 'ava';
import { setup } from './util';
import { DEFAULT_PROJECT_ID } from '../src/api-rest';
// @ts-ignore
let server: any;
let close: any;
const port = 3334;
const endpoint = `http://localhost:${port}`;
test.before(async () => ({ server, close } = await setup(port)));
test.after(async () => {
await close();
});
test.serial('should pull a project', async (t) => {
const response = await fetch(
`${endpoint}/api/provision/${DEFAULT_PROJECT_ID}`
);
t.is(response.status, 200);
const { data: proj } = await response.json();
t.is(proj.id, DEFAULT_PROJECT_ID);
t.is(proj.name, 'aaa');
t.truthy(proj.workflows);
});
test.serial("should return 404 if a project isn't found", async (t) => {
const response = await fetch(`${endpoint}/api/provision/nah`);
t.is(response.status, 404);
});
test.serial('should pull a project as yaml', async (t) => {
const response = await fetch(`${endpoint}/api/provision/yaml?id=123`);
const proj = await response.text();
t.regex(proj, /name: aaa/);
t.regex(proj, /name: wf1/);
});
test.serial('should deploy a project and fetch it back', async (t) => {
const response = await fetch(`${endpoint}/api/provision`, {
method: 'POST',
body: JSON.stringify({
id: 'abc',
name: 'my project',
}),
headers: {
'content-type': 'application/json',
},
});
t.is(response.status, 200);
const res2 = await fetch(`${endpoint}/api/provision/abc`);
const { data: proj } = await res2.json();
t.is(proj.id, 'abc');
t.is(proj.name, 'my project');
});
test.serial('should fetch many items from a collection', async (t) => {
server.collections.createCollection('stuff');
server.collections.upsert('stuff', 'x', { id: 'x' });
const response = await fetch(`${endpoint}/collections/stuff?query=*`);
const { items } = await response.json();
t.is(items.length, 1);
t.deepEqual(items[0], { key: 'x', value: { id: 'x' } });
});
test.serial('should fetch a single item from a collection', async (t) => {
server.collections.createCollection('stuff');
server.collections.upsert('stuff', 'x', { id: 'x' });
const response = await fetch(`${endpoint}/collections/stuff/x`);
const result = await response.json();
t.deepEqual(result, { key: 'x', value: { id: 'x' } });
});
test.serial("should return 404 if a collection isn't found", async (t) => {
const response = await fetch(`${endpoint}/collections/nope/*`);
t.is(response.status, 404);
});
test.todo("should return 403 if a collection isn't authorized");