1
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
2
// use this file except in compliance with the License. You may obtain a copy of
5
// http://www.apache.org/licenses/LICENSE-2.0
7
// Unless required by applicable law or agreed to in writing, software
8
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
// License for the specific language governing permissions and limitations under
13
// Specs for jquery_couch.js lines 48-156 and 415-448
15
describe 'jQuery couchdb'
24
describe 'activeTasks'
26
db = $.couch.db("spec_db");
34
it 'should return an empty array when there are no active tasks'
36
success: function(resp){
39
error: function(status, error, reason){errorCallback(status, error, reason)}
43
it 'should return an active task'
44
// doing a bit of stuff here so compaction has something to do and takes a while
45
var battlestar, civillian;
46
db.saveDoc({"type":"Battlestar", "name":"Galactica"}, {
47
success: function(resp){
49
success: function(resp2){
52
error: function(status, error, reason){errorCallback(status, error, reason)}
55
error: function(status, error, reason){errorCallback(status, error, reason)}
57
battlestar.name = "Pegasus";
58
db.saveDoc(battlestar);
60
db.saveDoc({"type":"Civillian", "name":"Cloud 9"}, {
61
success: function(resp){
63
success: function(resp2){
66
error: function(status, error, reason){errorCallback(status, error, reason)}
69
error: function(status, error, reason){errorCallback(status, error, reason)}
71
civillian.name = "Olympic Carrier";
72
db.saveDoc(civillian);
73
db.removeDoc(civillian);
76
ajaxStart: function(resp){
78
success: function(resp2){
79
resp2[0].type.should.eql "Database Compaction"
80
resp2[0].task.should.eql "spec_db"
81
resp2[0].should.have_prop "status"
82
resp2[0].should.include "pid"
84
error: function(status, error, reason){errorCallback(status, error, reason)}
92
it 'should return an array that includes a created database'
93
temp_db = new CouchDB("temp_spec_db", {"X-Couch-Full-Commit":"false"});
96
success: function(resp){
97
resp.should.include "temp_spec_db"
99
error: function(status, error, reason){errorCallback(status, error, reason)}
104
it 'should return an array that does not include a database that does not exist'
106
success: function(resp){
107
resp.should.not.include("not_existing_temp_spec_db");
109
error: function(status, error, reason){errorCallback(status, error, reason)}
115
it 'should get the config settings'
117
success: function(resp){
118
resp.httpd.port.should.eql window.location.port
119
resp.stats.samples.should.match /\[.*\]/
120
resp.native_query_servers.should.have_prop "erlang"
122
error: function(status, error, reason){errorCallback(status, error, reason)}
126
it 'should get a specific config setting'
128
success: function(resp){
129
parseInt(resp.max_document_size).should.be_a Number
130
resp.delayed_commits.should.be_a String
131
resp.database_dir.should.be_a String
133
error: function(status, error, reason){errorCallback(status, error, reason)}
137
it 'should update a config setting'
139
success: function(resp){
142
error: function(status, error, reason){errorCallback(status, error, reason)}
143
}, "test", "colony", "Caprica");
146
success: function(resp){
147
resp.colony.should.eql "Caprica"
149
error: function(status, error, reason){errorCallback(status, error, reason)}
152
$.couch.config({}, "test", "colony", null);
155
it 'should delete a config setting'
156
$.couch.config({}, "test", "colony", "Caprica");
159
success: function(resp){
160
resp.should.eql "Caprica"
162
error: function(status, error, reason){errorCallback(status, error, reason)}
163
}, "test", "colony", null);
166
success: function(resp){
169
error: function(status, error, reason){errorCallback(status, error, reason)}
173
it 'should alert with an error message prefix'
174
$.couch.config("asdf", "asdf", "asdf");
175
alert_msg.should.match /An error occurred retrieving\/updating the server configuration/
180
it 'should return information about the session'
182
success: function(resp){
183
resp.info.should.have_prop 'authentication_db'
184
resp.userCtx.should.include 'name'
185
resp.userCtx.roles.should.be_an Array
187
error: function(status, error, reason){errorCallback(status, error, reason)}
193
it 'should return the userDb'
194
var authentication_db;
196
success: function(resp){
197
authentication_db = resp.info.authentication_db;
199
error: function(status, error, reason){errorCallback(status, error, reason)}
202
$.couch.userDb(function(resp){
203
resp.name.should.eql authentication_db
207
it 'should return a db instance'
208
$.couch.userDb(function(resp){
209
resp.should.respond_to 'allDocs'
210
resp.should.respond_to 'bulkSave'
215
describe 'user_db stuff'
225
it 'should return a saved user'
227
{name: "Tom Zarek"}, "secretpass", {
228
success: function(resp){
229
resp.id.should.eql "org.couchdb.user:Tom Zarek"
230
resp.rev.length.should.be_at_least 30
231
resp.ok.should.be_true
232
users_db.deleteDoc({_id : resp.id, _rev : resp.rev})
234
error: function(status, error, reason){errorCallback(status, error, reason)}
238
it 'should create a userDoc in the user db'
240
{name: "Tom Zarek"}, "secretpass", {
241
success: function(resp){
242
var user = users_db.open(resp.id);
243
user.name.should.eql "Tom Zarek"
244
user._id.should.eql "org.couchdb.user:Tom Zarek"
245
user.roles.should.eql []
246
user.password_sha.length.should.be_at_least 30
247
user.password_sha.should.be_a String
248
users_db.deleteDoc({_id : resp.id, _rev : resp.rev})
250
error: function(status, error, reason){errorCallback(status, error, reason)}
254
it 'should create a userDoc with roles when specified'
256
{name: "Tom Zarek", roles: ["vice_president", "activist"]}, "secretpass", {
257
success: function(resp){
258
var user = users_db.open(resp.id);
259
user.roles.should.eql ["vice_president", "activist"]
260
users_db.deleteDoc({_id : resp.id, _rev : resp.rev})
262
error: function(status, error, reason){errorCallback(status, error, reason)}
270
$.couch.signup({name: "Tom Zarek", roles: ["vice_president", "activist"]}, "secretpass", {
271
success: function(resp){
275
error: function(status, error, reason){errorCallback(status, error, reason)}
280
users_db.deleteDoc({_id : user.id, _rev : user.rev})
283
it 'should return the logged in user'
286
password: "secretpass",
287
success: function(resp){
288
resp.name.should.eql "Tom Zarek"
289
resp.ok.should.be_true
290
resp.roles.should.eql ["vice_president", "activist"]
292
error: function(status, error, reason){errorCallback(status, error, reason)}
296
it 'should result in a session for the logged in user'
299
password: "secretpass"
302
success: function(resp){
303
resp.info.authentication_db.should.eql "spec_users_db"
304
resp.userCtx.name.should.eql "Tom Zarek"
305
resp.userCtx.roles.should.eql ["vice_president", "activist"]
307
error: function(status, error, reason){errorCallback(status, error, reason)}
311
it 'should return a 404 when password is wrong'
314
password: "wrongpass",
315
error: function(status, error, reason){
316
status.should.eql 401
317
error.should.eql "unauthorized"
318
reason.should.eql "Name or password is incorrect."
320
success: function(resp){successCallback(resp)}
324
it 'should return a 404 when the user doesnt exist in the users db'
326
name: "Number Three",
327
password: "secretpass",
328
error: function(status, error, reason){
329
status.should.eql 401
330
error.should.eql "unauthorized"
331
reason.should.eql "Name or password is incorrect."
333
success: function(resp){successCallback(resp)}
337
it 'should alert with an error message prefix'
338
$.couch.login("asdf");
339
alert_msg.should.match /An error occurred logging in/
346
$.couch.signup({name: "Tom Zarek", roles: ["vice_president", "activist"]}, "secretpass", {
347
success: function(resp){
351
error: function(status, error, reason){errorCallback(status, error, reason)}
353
$.couch.login({name: "Tom Zarek", password: "secretpass"});
357
users_db.deleteDoc({_id : user.id, _rev : user.rev})
360
it 'should return ok true'
362
success: function(resp){
363
resp.ok.should.be_true
365
error: function(status, error, reason){errorCallback(status, error, reason)}
369
it 'should result in an empty session'
372
success: function(resp){
373
resp.userCtx.name.should.be_null
374
resp.userCtx.roles.should.not.include ["vice_president"]
376
error: function(status, error, reason){errorCallback(status, error, reason)}
382
describe 'encodeDocId'
383
it 'should return the encoded docID when it is not a design document'
384
$.couch.encodeDocId("viper").should.eql(encodeURIComponent("viper"))
387
it 'should encode only the name of the design document'
388
$.couch.encodeDocId("_design/raptor").should.eql("_design/" + encodeURIComponent("raptor"))
391
it 'should also work when the name of the des'
392
$.couch.encodeDocId("_design/battlestar/_view/crew").should.eql("_design/" + encodeURIComponent("battlestar/_view/crew"))
397
it 'should return the CouchDB version'
399
success: function(resp){
400
resp.couchdb.should.eql "Welcome"
401
resp.version.should_match /^\d\d?\.\d\d?\.\d\d?.*/
403
error: function(status, error, reason){errorCallback(status, error, reason)}
410
db = $.couch.db("spec_db");
412
db2 = $.couch.db("spec_db_2");
414
host = window.location.protocol + "//" + window.location.host ;
422
it 'should return no_changes true when there are no changes between the dbs'
423
$.couch.replicate(host + db.uri, host + db2.uri, {
424
success: function(resp){
425
resp.ok.should.be_true
426
resp.no_changes.should.be_true
428
error: function(status, error, reason){errorCallback(status, error, reason)}
432
it 'should return the session ID'
433
db.saveDoc({'type':'battlestar', 'name':'galactica'});
434
$.couch.replicate(host + db.uri, host + db2.uri, {
435
success: function(resp){
436
resp.session_id.length.should.be_at_least 30
438
error: function(status, error, reason){errorCallback(status, error, reason)}
442
it 'should return source_last_seq'
443
db.saveDoc({'type':'battlestar', 'name':'galactica'});
444
db.saveDoc({'type':'battlestar', 'name':'pegasus'});
446
$.couch.replicate(host + db.uri, host + db2.uri, {
447
success: function(resp){
448
resp.source_last_seq.should.eql 2
450
error: function(status, error, reason){errorCallback(status, error, reason)}
454
it 'should return the replication history'
455
db.saveDoc({'type':'battlestar', 'name':'galactica'});
456
db.saveDoc({'type':'battlestar', 'name':'pegasus'});
458
$.couch.replicate(host + db.uri, host + db2.uri, {
459
success: function(resp){
460
resp.history[0].docs_written.should.eql 2
461
resp.history[0].start_last_seq.should.eql 0
463
error: function(status, error, reason){errorCallback(status, error, reason)}
467
it 'should pass through replication options'
468
db.saveDoc({'type':'battlestar', 'name':'galactica'});
470
$.couch.replicate(host + db.uri, host + db2.uri, {
471
error: function(status, error, reason){
472
status.should.eql 500
473
reason.should.match /db_not_found/
475
success: function(resp){successCallback(resp)}
478
$.couch.replicate(host + db.uri, host + db2.uri, {
479
success: function(resp){
480
resp.ok.should.eql true
481
resp.history[0].docs_written.should.eql 1
483
error: function(status, error, reason){errorCallback(status, error, reason)}
489
success: function(resp){
490
resp.db_name.should.eql "spec_db_2"
492
error: function(status, error, reason){errorCallback(status, error, reason)}
496
it 'should alert with an error message prefix'
497
$.couch.replicate("asdf");
498
alert_msg.should.match /Replication failed/
503
it 'should return a new UUID'
504
var new_uuid = $.couch.newUUID(1);
505
new_uuid.should.be_a String
506
new_uuid.should.have_length 32
509
it 'should fill the uuidCache with the specified number minus 1'
510
// we can't reach the uuidCache from here, so we mock the next request
511
// to test that the next uuid is not coming from the request, but from the cache.
513
mock_request().and_return({'uuids':['a_sample_uuid']})
514
$.couch.newUUID(1).should.not.eql 'a_sample_uuid'
515
$.couch.newUUID(1).should.eql 'a_sample_uuid'
518
it 'should alert with an error message prefix'
519
$.couch.newUUID("asdf");
520
alert_msg.should.match /Failed to retrieve UUID batch/
b'\\ No newline at end of file'