-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
167 lines (136 loc) · 3.67 KB
/
test.js
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
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
import API from './api';
import fs from 'fs';
import test from 'ava';
const ALIAS = process.env.ALIAS;
const TOKEN = process.env.TOKEN || getHomeToken();
const testIfAlias = ALIAS ? test : test.skip;
test.before(t => {
console.log("Starting tests...");
t.truthy(TOKEN, "Zeit token is missing. Make sure you run `TOKEN=[token] npm test`");
});
test.serial('deploy', t => {
t.plan(3);
return deploy().then(data => {
t.truthy(data.uid);
t.truthy(data.host);
t.truthy(data.state);
}).catch(e => error(e, t));
});
testIfAlias.serial('alias', t => {
t.plan(2);
return API.alias(DEPLOY_ID, ALIAS)
.then(data => {
t.truthy(data.uid);
t.truthy(data.created);
})
.catch(e => error(e, t));
});
test('aliases', t => {
t.plan(1);
return API.aliases()
.then(data => t.true(Array.isArray(data)))
.catch(e => error(e, t));
});
test('deployments', t => {
t.plan(1);
return API.deployments()
.then(data => t.true(data.length > 0))
.catch(e => error(e, t));
});
test('deployment', t => {
t.plan(1);
return API.deployment(DEPLOY_ID)
.then(data => t.is(data.uid, DEPLOY_ID))
.catch(e => error(e, t));
});
test('files', t => {
t.plan(1);
return API.files(DEPLOY_ID)
.then(data => t.is(data.length, 2))
.catch(e => error(e, t));
});
test('file', t => {
t.plan(1);
return getFileIdByName('package.json')
.then(fileId => API.setToken(TOKEN).file(DEPLOY_ID, fileId))
.then(data => t.is(data.name, DEPLOY_NAME))
.catch(e => error(e, t));
});
testIfAlias('delete alias', t => {
t.plan(1);
return API.aliases()
.then(data => data.find(a => a.alias === ALIAS))
.then(alias => API.setToken(TOKEN).deleteAlias(alias.uid))
.then(data => t.is(data.status, API.RESPONSE_STATUS.SUCCESS))
.catch(e => error(e, t));
});
test.after('delete deployment', t => {
t.plan(1);
return API.deleteDeployment(DEPLOY_ID)
.then(data => t.is(data.state, API.DEPLOY_STATUS.DELETED))
.catch(e => error(e, t));
});
test('ok', t => {
t.plan(1);
return API.ok()
.then(ok => t.true(ok))
.catch(e => error(e, t));
});
/**
* ----- HELPERS -----
*/
const DEPLOY_NAME = "zeit-client-engine-test";
const DEPLOY_CONTENTS = {
package: {
name: DEPLOY_NAME,
scripts: { start: 'node index' }
},
'index.js':
'require("http").Server((req, res) => {' +
'res.end("zeit-client-engine-test");' +
'}).listen();'
};
let DEPLOY_ID = null;
function deploy() {
return API.setToken(TOKEN)
.deploy(DEPLOY_CONTENTS)
.then(data => {
DEPLOY_ID = data && data.uid || null;
return data;
});
}
/**
* @param {Error} e
* @param {Object} t test
*/
function error(e, t) {
t.fail(e.toString());
}
/**
* @param {String} fileName
* @return {Promise}
*/
function getFileIdByName(fileName) {
return API.setToken(TOKEN)
.files(DEPLOY_ID)
.then(files => getFileByName(files, fileName).uid);
}
/**
* @param {Array.<Object>} files
* @param {String} fileName
* @return {Promise}
*/
function getFileByName(files, fileName) {
if (!files || !files.length) return null;
return files.filter(file => file.name === fileName)[0];
}
/**
* @return {String} the token from ~/.now.json
*/
function getHomeToken() {
let homedir = process.env[(process.platform == 'win32')
? 'USERPROFILE'
: 'HOME'];
return JSON.parse(fs.readFileSync(`${homedir}/.now.json`, 'utf8')).token;
}