-
Notifications
You must be signed in to change notification settings - Fork 502
Expand file tree
/
Copy pathparse-options-test.js
More file actions
327 lines (270 loc) · 8.89 KB
/
Copy pathparse-options-test.js
File metadata and controls
327 lines (270 loc) · 8.89 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
var proxyquire = require('proxyquire').noCallThru()
var test = require('tap').test
function getDefaultsMock () {
return {
hoodie: {
name: 'foo',
paths: {
data: 'data path',
public: 'public path'
},
db: {},
client: {}
}
}
}
var logMock = {
info: function () {},
warn: function () {}
}
var parseOptions = proxyquire('../../../cli/parse-options', {
npmlog: logMock,
'./defaults': getDefaultsMock
})
test('parse options', function (group) {
group.test('unset keys', function (t) {
var config = parseOptions({
data: '.hoodie',
dbAdapter: 'pouchdb-adapter-fs'
})
t.notOk(config.hasOwnProperty('url'), 'does not set config.url by default')
t.is(config.inMemory, false, 'sets config.inMemory to false by default')
t.end()
})
group.test('wiring', function (t) {
var config = parseOptions({
data: 'options.data',
public: 'options.public',
url: 'options.url',
dbUrl: 'http://foo:bar@baz.com',
dbUrlPassword: 'I@mApassword',
dbUrlUsername: 'I@mAusername4db',
inMemory: 'aNonNullString',
adminPassword: 'secret'
})
t.is(config.paths.data, 'options.data', 'uses data option as data path')
t.is(config.url, 'options.url', 'sets config.url from options.url')
t.is(config.paths.public, 'options.public', 'uses public option as public path')
t.is(config.inMemory, true, 'Sets config.inMemory to boolean equivalent of options.inMemory')
t.is(config.adminPassword, 'secret', 'Sets config.adminPassword')
t.end()
})
// *************
// dbURL TESTS
// tests that PASS
group.test('dbUrl http with normal user/pass in url', function (t) {
var config = parseOptions({
public: 'public',
data: 'data',
dbUrl: 'http://foo:bar@baz.com'
})
var defaults = config.PouchDB('hack', { skip_setup: true }).__opts
t.is(defaults.prefix, 'http://foo:bar@baz.com', 'Sets config.db.url')
t.end()
})
group.test('dbUrl https with normal user/pass in url', function (t) {
var config = parseOptions({
public: 'public',
data: 'data',
dbUrl: 'https://foo:bar@baz.com'
})
var defaults = config.PouchDB('hack', { skip_setup: true }).__opts
t.is(defaults.prefix, 'https://foo:bar@baz.com', 'Sets config.db.url')
t.end()
})
// https://foo@baz.com passing
group.test('dbUrl https with user/pass in url, username: same as the start of the url', function (t) {
var config = parseOptions({
public: 'public',
data: 'data',
dbUrl: 'https://https://foo@baz.com'
})
var defaults = config.PouchDB('hack', { skip_setup: true }).__opts
t.is(defaults.prefix, 'https://https:%2F%2Ffoo@baz.com', 'Sets config.db.url')
t.end()
})
// @@:@@@baz.com passing
group.test('dbUrl http with user/pass in url, user/pass only with @', function (t) {
var config = parseOptions({
public: 'public',
data: 'data',
dbUrl: 'http://@@:@@@baz.com'
})
var defaults = config.PouchDB('hack', { skip_setup: true }).__opts
t.is(defaults.prefix, 'http://%40%40:%40%40@baz.com', 'Sets config.db.url')
t.end()
})
// dasds@dasdsa.com:@dsadas@dasdas@@baz.com passing
group.test('dbUrl http with user/pass in url, tricky user/pass', function (t) {
var config = parseOptions({
public: 'public',
data: 'data',
dbUrl: 'http://das/^$ds@das!dsa.com:@dsadas@dasdas@@baz.com'
})
var defaults = config.PouchDB('hack', { skip_setup: true }).__opts
t.is(defaults.prefix, 'http://das%2F%5E%24ds%40das!dsa.com:%40dsadas%40dasdas%40@baz.com', 'Sets config.db.url')
t.end()
})
group.test('dbUrl http with user/pass in params', function (t) {
var config = parseOptions({
public: 'public',
data: 'data',
dbUrlUsername: 'john@doe.com',
dbUrlPassword: 'password',
dbUrl: 'http://baz.com'
})
var defaults = config.PouchDB('hack', { skip_setup: true }).__opts
t.is(defaults.prefix, 'http://john%40doe.com:password@baz.com', 'Sets config.db.url')
t.end()
})
group.test('dbUrl http with user in url, password provided via params', function (t) {
var config = parseOptions({
public: 'public',
data: 'data',
dbUrlPassword: 'password',
dbUrl: 'http://john@doe.com@baz.com'
})
var defaults = config.PouchDB('hack', { skip_setup: true }).__opts
t.is(defaults.prefix, 'http://john%40doe.com:password@baz.com', 'Sets config.db.url')
t.end()
})
group.test('dbUrl http with user/pass in url and username in params, params override url details', function (t) {
var config = parseOptions({
public: 'public',
data: 'data',
dbUrlUsername: 'john',
dbUrl: 'http://test:anotherpass@baz.com'
})
var defaults = config.PouchDB('hack', { skip_setup: true }).__opts
t.is(defaults.prefix, 'http://john:anotherpass@baz.com', 'Sets config.db.url')
t.end()
})
group.test('dbUrl http with user/pass in url and pass in params, params override url details', function (t) {
var config = parseOptions({
public: 'public',
data: 'data',
dbUrlPassword: 'password',
dbUrl: 'http://test:anotherpass@baz.com'
})
var defaults = config.PouchDB('hack', { skip_setup: true }).__opts
t.is(defaults.prefix, 'http://test:password@baz.com', 'Sets config.db.url')
t.end()
})
group.test('dbUrl http with user/pass both in params and in url, params override url details', function (t) {
var config = parseOptions({
public: 'public',
data: 'data',
dbUrlUsername: 'john',
dbUrlPassword: 'password',
dbUrl: 'http://test:anotherpass@baz.com'
})
var defaults = config.PouchDB('hack', { skip_setup: true }).__opts
t.is(defaults.prefix, 'http://john:password@baz.com', 'Sets config.db.url')
t.end()
})
group.test('dbUrl no url is passed, with user/pass in params. Test passes and local db initialized', function (t) {
var config = parseOptions({
public: 'public',
data: 'data',
dbUrlUsername: 'john@doe.com',
dbUrlPassword: 'pass',
dbAdapter: 'pouchdb-adapter-fs'
})
t.is(config.PouchDB.preferredAdapters[0], 'fs')
t.end()
})
// tests that will throw an ERROR
group.test('dbUrl http with user/pass in url, cannot separate username and pass credentials, ":" exists more than once', function (t) {
t.throws(parseOptions.bind(null, {
public: 'public',
data: 'data',
dbUrl: 'http://jo:hn:deow::pass@baz.com'
}))
t.end()
})
group.test('dbUrl lacking auth', function (t) {
t.throws(parseOptions.bind(null, {
public: 'public',
data: 'data',
dbUrl: 'http://baz.com'
}))
t.end()
})
group.test('dbUrl lacking auth (@ is included in dbUrl)', function (t) {
t.throws(parseOptions.bind(null, {
public: 'public',
data: 'data',
dbUrl: 'http://@baz.com'
}))
t.end()
})
group.test('dbUrl lacking username (password and symbol "@" are included in dbUrl)', function (t) {
t.throws(parseOptions.bind(null, {
public: 'public',
data: 'data',
dbUrl: 'http://:dsadsa@baz.com'
}))
t.end()
})
group.test('dbUrl lacking username (password is included in provided params)', function (t) {
t.throws(parseOptions.bind(null, {
public: 'public',
data: 'data',
dbUrlPassword: 'asdasda',
dbUrl: 'http://baz.com'
}))
t.end()
})
group.test('dbUrl lacking password (username and symbol "@" are included in dbUrl)', function (t) {
t.throws(parseOptions.bind(null, {
public: 'public',
data: 'data',
dbUrl: 'http://dasdad:@baz.com'
}))
t.end()
})
group.test('dbUrl lacking password (username is included in provided params)', function (t) {
t.throws(parseOptions.bind(null, {
public: 'public',
data: 'data',
dbUrlUsername: 'asdasda',
dbUrl: 'http://baz.com'
}))
t.end()
})
group.test('dbUrl lacking pass (username is included in provided params)', function (t) {
t.throws(parseOptions.bind(null, {
public: 'public',
data: 'data',
dbUrlUsername: 'asdasda',
dbUrl: 'http://baz.com'
}))
t.end()
})
group.test('dbUrl lacking pass from parameters (username is included in url)', function (t) {
t.throws(parseOptions.bind(null, {
public: 'public',
data: 'data',
dbUrl: 'http://john@doe.com@baz.com'
}))
t.end()
})
// end of dbURL TESTS
// ****************
test('throws when dbAdapter is missing and neither dbUrl nor inMemory is set', function (t) {
t.plan(1)
t.throws(function () {
parseOptions({})
}, new Error('Missing required option: dbAdapter'))
})
group.test('inMemory', function (t) {
var config = parseOptions({
public: 'public',
data: 'data',
inMemory: true
})
t.is(config.inMemory, true, 'Sets config.inMemory to true')
t.end()
})
group.end()
})