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
|
/*******************************************************************************
* Copyright (c) 2016 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*eslint-env node, mocha*/
var chai = require('chai'),
express = require('express'),
nodePath = require('path'),
PrefsController = require('../lib/controllers/prefs').router,
Promise = require('bluebird'),
supertestAsPromised = require('supertest-as-promised'),
testData = require('./support/test_data');
var expect = chai.expect,
fs = Promise.promisifyAll(require('fs')),
mkdirpAsync = Promise.promisify(require('mkdirp'));
var CONTEXT_PATH = '';
var PREFS_PREFIX = CONTEXT_PATH + '/prefs';
var WORKSPACE_DIR = nodePath.join(__dirname, '.test_workspace');
var samplePrefData = {
user: {
foo: {
bar: 123,
qux: 'q'
},
foo2: false
},
zzz: 1
};
var app = express();
app.use(/* @callback */ function(req, res, next) {
req.user = { workspaceDir: WORKSPACE_DIR };
next();
})
.use(PREFS_PREFIX, PrefsController({
configParams: {
'orion.single.user': false, // use workspaceDir from req.user
},
ttl: 50, // flush after 50 ms
}));
var request = supertestAsPromised.bind(null, app);
function setupWorkspace(done) {
return Promise.fromCallback(testData.setUp.bind(null, WORKSPACE_DIR))
.asCallback(done);
}
function setupPrefs(done) {
var path = nodePath.join(WORKSPACE_DIR, '.orion', PrefsController.PREF_FILENAME);
return mkdirpAsync(nodePath.dirname(path))
.then(() => fs.writeFileAsync(path, JSON.stringify(samplePrefData)))
.asCallback(done);
}
describe('prefs', function() {
beforeEach(setupWorkspace);
describe('when NO prefs.json exists', function() {
describe('and we GET a nonexistent single key', function() {
it('should receive 404', function() {
return request().get(PREFS_PREFIX + '/user/a/b/c?key=jsjsijf')
.expect(404);
});
});
describe('and we GET a nonexistent node', function() {
it('should receive empty node', function() {
return request().get(PREFS_PREFIX + '/user/a/b/c')
.expect(200)
.then(function(res) {
expect(res.body).to.deep.equal({ });
});
});
});
});
describe('when prefs.json exists', function() {
beforeEach(setupPrefs);
describe('and we GET a nonexistent single key', function() {
it('should receive 404', function() {
return request().get(PREFS_PREFIX + '/user/a/b/c?key=jsjsijf')
.expect(404);
});
});
describe('and we GET a nonexistent node', function() {
it('should receive empty node', function() {
return request().get(PREFS_PREFIX + '/user/a/b/c')
.expect(200)
.then(function(res) {
expect(res.body).to.deep.equal({ });
});
});
});
describe('and we GET a single key', function() {
it('should receive just that property', function() {
return request().get(PREFS_PREFIX + '/user/foo?key=bar').expect(200)
.then(function(res) {
expect(res.body).to.deep.equal({ bar: 123 });
});
});
});
describe('and we GET a node', function() {
it('should receive the entire node', function() {
return request().get(PREFS_PREFIX + '/user/foo').expect(200)
.then(function(res) {
expect(res.body).to.deep.equal({ bar: 123, qux: 'q' });
});
});
});
describe('and we PUT a single key', function() {
beforeEach(function() {
return request().put(PREFS_PREFIX + '/user/foo')
.type('form')
.send({ key: 'bar' }).send({ value: 'modified' })
.expect(204);
});
it('should update the value', function() {
// Check the value
return request().get(PREFS_PREFIX + '/user/foo').expect(200)
.then(function(res) {
expect(res.body).to.deep.equal({ bar: 'modified', qux: 'q' });
});
});
});
describe('and we PUT an entire node', function() {
beforeEach(function() {
return request().put(PREFS_PREFIX + '/user/foo')
.type('json')
.send({ howdy: 'partner' })
.expect(204);
});
it('should update the value', function() {
return request().get(PREFS_PREFIX + '/user/foo').expect(200)
.then(function(res) {
expect(res.body).to.deep.equal({ howdy: 'partner' });
});
});
});
describe('and we DELETE a single key', function() {
beforeEach(function() {
return request().delete(PREFS_PREFIX + '/user/foo?key=bar')
.expect(204);
});
it('should not have the deleted key', function() {
return request().get(PREFS_PREFIX + '/user/foo?key=bar')
.expect(404);
});
});
describe('and we DELETE entire node', function() {
beforeEach(function() {
return request().delete(PREFS_PREFIX + '/user/foo')
.expect(204);
});
it('should be empty', function() {
// Node should be empty
return request().get(PREFS_PREFIX + '/user/foo').expect(200)
.then(function(res) {
expect(res.body).to.deep.equal({ });
});
});
});
describe('and we DELETE a non-existent node', function() {
beforeEach(function() {
return request().delete(PREFS_PREFIX + '/user/foo/asgjsgkjkjtiwujk')
.expect(204);
});
it('should have no effect', function() {
return request().get(PREFS_PREFIX + '/').expect(200)
.then(function(res) {
expect(res.body).to.deep.equal(samplePrefData);
});
});
});
});
});
|