forked from deployd/deployd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresources.unit.js
197 lines (164 loc) · 7.43 KB
/
resources.unit.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var InternalResources = require('../lib/resources/internal-resources')
, Files = require('../lib/resources/files')
, config = require('../lib/config-loader')
, sh = require('shelljs')
, fs = require('fs')
, path = require('path')
, testCollection = {type: 'Collection', path: '/my-objects', properties: {title: {type: 'string'}}}
, Collection = require('../lib/resources/collection')
, ClientLib = require('../lib/resources/client-lib')
, configPath = './test/support/proj'
, Dashboard = require('../lib/resources/dashboard');
describe('InternalResources', function() {
describe('.handle(ctx)', function() {
beforeEach(function() {
// reset
sh.rm('-rf', __dirname + '/support/proj');
if(!sh.test('-d', __dirname + '/support/proj')) {
sh.mkdir(__dirname + '/support/proj');
sh.mkdir('-p', __dirname + '/support/proj/resources');
}
this.ir = new InternalResources('__resources', {config: {configPath: configPath}});
});
it('should require root access', function(done) {
var r = {type: 'Bar'}
, created = false;
this.ir.handle({req: {method: 'POST', url: '/__resources/foo'}, body: r, done: function(err, resource) {
expect(resource).to.not.exist;
expect(err).to.exist;
expect(err.statusCode).to.equal(401);
done();
}});
});
it('should not allow a generic POST', function(done) {
var r = {type: 'Bar'}
, created = false;
this.ir.handle({req: {method: 'POST', url: '/__resources', isRoot: true}, body: r, done: function(err, resource) {
expect(resource).to.not.exist;
expect(err).to.exist;
expect(err.statusCode).to.equal(400);
done();
}});
});
it('should create a resource when handling a POST request', function(done) {
var r = {type: 'Bar'}
, created = false;
this.ir.handle({req: {method: 'POST', url: '/__resources/foo', isRoot: true}, url: '/foo', body: r, done: function(err, resource) {
if (err) return done(err);
expect(resource.type).to.equal('Bar');
var file = path.join(configPath, '/resources/foo/config.json');
expect(sh.test('-f', file)).to.be.ok;
expect(JSON.parse(sh.cat(file)).type).to.equal('Bar');
done();
}});
});
it('should save a file when handling a POST request', function(done) {
var r = {type: 'Bar'}
, created = false;
sh.mkdir('-p', path.join(configPath, 'resources/foo'));
JSON.stringify(r).to(path.join(configPath, 'resources/foo/config.json'));
this.ir.handle({req: {method: 'POST', url: '/__resources/foo/get.js', isRoot: true}, url: '/foo/get.js', body: {value: "this.foo = 'bar';"}, done: function(err, resource) {
if (err) return done(err);
var fileVal = sh.cat(path.join(configPath, 'resources/foo/get.js'));
expect(fileVal).to.exist.and.to.equal("this.foo = 'bar';");
done();
}});
});
it('should update a resource when handling a PUT request', function(done) {
var r = {type: 'Bar', val: 1};
var test = this;
sh.mkdir('-p', path.join(configPath, 'resources/foo'));
JSON.stringify(r).to(path.join(configPath, 'resources/foo/config.json'));
r.val = 2;
test.ir.handle({req: {method: 'PUT', url: '/__resources/foo', isRoot: true}, url: '/foo', body: r, done: function() {
var file = path.join(configPath, '/resources/foo/config.json');
expect(JSON.parse(sh.cat(file)).val).to.equal(2);
done();
}}, function() {
throw Error("next called");
});
});
it('should partially update a resource when handing a PUT request', function(done) {
var r = {type: 'Bar', val: 1, other: 'test'};
var test = this;
sh.mkdir('-p', path.join(configPath, 'resources/foo'));
JSON.stringify(r).to(path.join(configPath, 'resources/foo/config.json'));
test.ir.handle({req: {method: 'PUT', url: '/__resources/foo', isRoot: true}, url: '/foo', body: {val: 2}, done: function() {
var file = path.join(configPath, '/resources/foo/config.json');
var json = JSON.parse(sh.cat(file));
expect(json.val).to.equal(2);
expect(json.other).to.equal('test');
done();
}}, function() {
throw Error("next called");
});
});
it('should update a resource when handing a PUT request with $setAll', function(done) {
var r = {type: 'Bar', val: 1, other: 'test'};
var test = this;
sh.mkdir('-p', path.join(configPath, 'resources/foo'));
JSON.stringify(r).to(path.join(configPath, 'resources/foo/config.json'));
test.ir.handle({req: {method: 'PUT', url: '/__resources/foo', isRoot: true}, url: '/foo', body: {type: 'Bar', val: 2, $setAll: true}, done: function() {
var file = path.join(configPath, '/resources/foo/config.json');
var json = JSON.parse(sh.cat(file));
expect(json.val).to.equal(2);
expect(json.other).to.not.exist;
done();
}}, function() {
throw Error("next called");
});
});
it('should find all resources when handling a GET request', function(done) {
var q = {type: 'Bar'}
, q2 = {type: 'Bar'}
, test = this;
sh.mkdir('-p', path.join(configPath, 'resources/foo'));
sh.mkdir('-p', path.join(configPath, 'resources/bar'));
JSON.stringify(q).to(path.join(configPath, 'resources/foo/config.json'));
JSON.stringify(q2).to(path.join(configPath, 'resources/bar/config.json'));
test.ir.handle({req: {method: 'GET', url: '/__resources', isRoot: true}, url: '/', done: function(err, result) {
if (err) return done(err);
expect(result).to.have.length(2);
result.forEach(function(r) {
expect(r.id).to.exist;
});
done();
}}, function() {
throw Error("next called");
});
});
it('should find a single resource when handling a GET request', function(done) {
var q = {type: 'Bar'}
, q2 = {type: 'Bar'}
, test = this;
sh.mkdir('-p', path.join(configPath, 'resources/foo'));
sh.mkdir('-p', path.join(configPath, 'resources/bar'));
JSON.stringify(q).to(path.join(configPath, 'resources/foo/config.json'));
JSON.stringify(q2).to(path.join(configPath, 'resources/bar/config.json'));
test.ir.handle({req: {method: 'GET', url: '/__resources/bar', isRoot: true}, url: '/bar', done: function(err, result) {
if (err) return done(err);
expect(result).to.exist;
expect(result.id).to.equal('bar');
expect(result.type).to.equal('Bar');
done();
}}, function() {
throw Error("next called");
});
});
it('should delete a resource when handling a DELETE request', function(done) {
var q = {path: '/foo', type: 'Bar'}
, q2 = {path: '/bar', type: 'Bar'}
, test = this;
sh.mkdir('-p', path.join(configPath, 'resources/foo'));
sh.mkdir('-p', path.join(configPath, 'resources/bar'));
JSON.stringify(q).to(path.join(configPath, 'resources/foo/config.json'));
JSON.stringify(q2).to(path.join(configPath, 'resources/bar/config.json'));
test.ir.handle({req: {method: 'DELETE', url: '/__resources/bar', isRoot: true}, url: '/bar', done: function() {
expect(sh.test('-d', path.join(configPath, 'resources/bar'))).to.not.be.ok;
done();
}}, function() {
throw Error("next called");
});
});
});
});