~ubuntu-branches/ubuntu/maverick/couchdb/maverick

« back to all changes in this revision

Viewing changes to share/www/spec/jquery_couch_js_class_methods_spec.js

  • Committer: Bazaar Package Importer
  • Author(s): Elliot Murphy
  • Date: 2010-08-16 19:45:48 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20100816194548-cdm0mjqnbxjt5wa4
Tags: 1.0.1-0ubuntu1
* Upstream microrelease to address data loss (LP: #615224)
  - Fix data corruption bug COUCHDB-844. Please see
    http://couchdb.apache.org/notice/1.0.1.html for details.
  - Added support for replication via an HTTP/HTTP proxy.
  - Fixed various replicator bugs for interop with older
    CouchDB versions.
  - Show fields saved along with _deleted=true.
    Allows for auditing of deletes.
  - Enable basic-auth popup when required to access the server,
    to prevent people from getting locked out.
  - User interface element for querying stale (cached) views.
* debian/patches/auth_patch.patch dropped, included upstream.
* debian/patches/remove_users.patch dropped, included upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
3
// the License at
 
4
//
 
5
//   http://www.apache.org/licenses/LICENSE-2.0
 
6
//
 
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
 
11
// the License.
 
12
 
 
13
// Specs for jquery_couch.js lines 48-156 and 415-448
 
14
 
 
15
describe 'jQuery couchdb'
 
16
  before
 
17
    stubAlert();
 
18
  end
 
19
  
 
20
  after
 
21
    destubAlert();
 
22
  end
 
23
  
 
24
  describe 'activeTasks'
 
25
    before_each
 
26
      db = $.couch.db("spec_db");
 
27
      db.create();
 
28
    end
 
29
    
 
30
    after_each
 
31
      db.drop();
 
32
    end
 
33
    
 
34
    it 'should return an empty array when there are no active tasks'
 
35
      $.couch.activeTasks({
 
36
        success: function(resp){
 
37
          resp.should.eql []
 
38
        },
 
39
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
40
      });
 
41
    end
 
42
    
 
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){
 
48
          db.openDoc(resp.id, {
 
49
            success: function(resp2){
 
50
              battlestar = resp2;
 
51
            },
 
52
            error: function(status, error, reason){errorCallback(status, error, reason)}
 
53
          });
 
54
        },
 
55
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
56
      });
 
57
      battlestar.name = "Pegasus";
 
58
      db.saveDoc(battlestar);
 
59
      
 
60
      db.saveDoc({"type":"Civillian", "name":"Cloud 9"}, {
 
61
        success: function(resp){
 
62
          db.openDoc(resp.id, {
 
63
            success: function(resp2){
 
64
              civillian = resp2;
 
65
            },
 
66
            error: function(status, error, reason){errorCallback(status, error, reason)}
 
67
          });
 
68
        },
 
69
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
70
      });
 
71
      civillian.name = "Olympic Carrier";
 
72
      db.saveDoc(civillian);
 
73
      db.removeDoc(civillian);
 
74
      
 
75
      db.compact({
 
76
        ajaxStart: function(resp){
 
77
          $.couch.activeTasks({
 
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"
 
83
            },
 
84
            error: function(status, error, reason){errorCallback(status, error, reason)}
 
85
          });
 
86
        }
 
87
      });
 
88
    end
 
89
  end
 
90
  
 
91
  describe 'allDbs'
 
92
    it 'should return an array that includes a created database'
 
93
      temp_db = new CouchDB("temp_spec_db", {"X-Couch-Full-Commit":"false"});
 
94
      temp_db.createDb();
 
95
      $.couch.allDbs({
 
96
        success: function(resp){
 
97
          resp.should.include "temp_spec_db"
 
98
        },
 
99
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
100
      });
 
101
      temp_db.deleteDb();
 
102
    end
 
103
    
 
104
    it 'should return an array that does not include a database that does not exist'
 
105
      $.couch.allDbs({
 
106
        success: function(resp){
 
107
          resp.should.not.include("not_existing_temp_spec_db");
 
108
        },
 
109
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
110
      });
 
111
    end
 
112
  end
 
113
  
 
114
  describe 'config'
 
115
    it 'should get the config settings'
 
116
      $.couch.config({
 
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"
 
121
        },
 
122
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
123
      });
 
124
    end
 
125
    
 
126
    it 'should get a specific config setting'
 
127
      $.couch.config({
 
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
 
132
        },
 
133
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
134
      }, "couchdb");
 
135
    end
 
136
    
 
137
    it 'should update a config setting'
 
138
      $.couch.config({
 
139
        success: function(resp){
 
140
          resp.should.eql ""
 
141
        },
 
142
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
143
      }, "test", "colony", "Caprica");
 
144
      
 
145
      $.couch.config({
 
146
        success: function(resp){
 
147
          resp.colony.should.eql "Caprica"
 
148
        },
 
149
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
150
      }, "test");
 
151
      
 
152
      $.couch.config({}, "test", "colony", null);
 
153
    end
 
154
    
 
155
    it 'should delete a config setting'
 
156
      $.couch.config({}, "test", "colony", "Caprica");
 
157
      
 
158
      $.couch.config({
 
159
        success: function(resp){
 
160
          resp.should.eql "Caprica"
 
161
        },
 
162
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
163
      }, "test", "colony", null);
 
164
      
 
165
      $.couch.config({
 
166
        success: function(resp){
 
167
          resp.should.eql {}
 
168
        },
 
169
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
170
      }, "test");
 
171
    end
 
172
  
 
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/
 
176
    end
 
177
  end
 
178
  
 
179
  describe 'session'
 
180
    it 'should return information about the session'
 
181
      $.couch.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
 
186
        },
 
187
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
188
      });
 
189
    end
 
190
  end
 
191
  
 
192
  describe 'userDb'
 
193
    it 'should return the userDb'
 
194
      var authentication_db;
 
195
      $.couch.session({
 
196
        success: function(resp){
 
197
          authentication_db = resp.info.authentication_db;
 
198
        },
 
199
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
200
      });
 
201
      
 
202
      $.couch.userDb(function(resp){
 
203
        resp.name.should.eql authentication_db
 
204
      });
 
205
    end
 
206
    
 
207
    it 'should return a db instance'
 
208
      $.couch.userDb(function(resp){
 
209
        resp.should.respond_to 'allDocs'
 
210
        resp.should.respond_to 'bulkSave'
 
211
      });
 
212
    end
 
213
  end
 
214
  
 
215
  describe 'user_db stuff'
 
216
    before
 
217
      useTestUserDb();
 
218
    end
 
219
  
 
220
    after
 
221
      useOldUserDb();
 
222
    end
 
223
    
 
224
    describe 'signup'
 
225
      it 'should return a saved user'
 
226
        $.couch.signup(
 
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})
 
233
          },
 
234
          error: function(status, error, reason){errorCallback(status, error, reason)}
 
235
        });
 
236
      end
 
237
    
 
238
      it 'should create a userDoc in the user db'
 
239
        $.couch.signup(
 
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})
 
249
          },
 
250
          error: function(status, error, reason){errorCallback(status, error, reason)}
 
251
        });
 
252
      end
 
253
    
 
254
      it 'should create a userDoc with roles when specified'
 
255
        $.couch.signup(
 
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})
 
261
          },
 
262
          error: function(status, error, reason){errorCallback(status, error, reason)}
 
263
        });
 
264
      end
 
265
    end
 
266
    
 
267
    describe 'login'
 
268
      before_each
 
269
        user = {};
 
270
        $.couch.signup({name: "Tom Zarek", roles: ["vice_president", "activist"]}, "secretpass", {
 
271
          success: function(resp){
 
272
            user.id  = resp.id;
 
273
            user.rev = resp.rev;
 
274
          },
 
275
          error: function(status, error, reason){errorCallback(status, error, reason)}
 
276
        });
 
277
      end
 
278
      
 
279
      after_each
 
280
        users_db.deleteDoc({_id : user.id, _rev : user.rev})
 
281
      end
 
282
      
 
283
      it 'should return the logged in user'
 
284
        $.couch.login({
 
285
          name: "Tom Zarek",
 
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"]
 
291
          },
 
292
          error: function(status, error, reason){errorCallback(status, error, reason)}
 
293
        });
 
294
      end
 
295
      
 
296
      it 'should result in a session for the logged in user'
 
297
        $.couch.login({
 
298
          name: "Tom Zarek",
 
299
          password: "secretpass"
 
300
        });
 
301
        $.couch.session({
 
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"]
 
306
          },
 
307
          error: function(status, error, reason){errorCallback(status, error, reason)}
 
308
        });
 
309
      end
 
310
      
 
311
      it 'should return a 404 when password is wrong'
 
312
        $.couch.login({
 
313
          name: "Tom Zarek",
 
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."
 
319
          },
 
320
          success: function(resp){successCallback(resp)}
 
321
        });
 
322
      end
 
323
      
 
324
      it 'should return a 404 when the user doesnt exist in the users db'
 
325
        $.couch.login({
 
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."
 
332
          },
 
333
          success: function(resp){successCallback(resp)}
 
334
        });
 
335
      end
 
336
  
 
337
      it 'should alert with an error message prefix'
 
338
        $.couch.login("asdf");
 
339
        alert_msg.should.match /An error occurred logging in/
 
340
      end
 
341
    end
 
342
  
 
343
    describe 'logout'
 
344
      before_each
 
345
        user = {};
 
346
        $.couch.signup({name: "Tom Zarek", roles: ["vice_president", "activist"]}, "secretpass", {
 
347
          success: function(resp){
 
348
            user.id  = resp.id;
 
349
            user.rev = resp.rev;
 
350
          },
 
351
          error: function(status, error, reason){errorCallback(status, error, reason)}
 
352
        });
 
353
        $.couch.login({name: "Tom Zarek", password: "secretpass"});
 
354
      end
 
355
      
 
356
      after_each
 
357
        users_db.deleteDoc({_id : user.id, _rev : user.rev})
 
358
      end
 
359
      
 
360
      it 'should return ok true'
 
361
        $.couch.logout({
 
362
          success: function(resp){
 
363
            resp.ok.should.be_true
 
364
          },
 
365
          error: function(status, error, reason){errorCallback(status, error, reason)}
 
366
        });
 
367
      end
 
368
      
 
369
      it 'should result in an empty session'
 
370
        $.couch.logout();
 
371
        $.couch.session({
 
372
          success: function(resp){
 
373
            resp.userCtx.name.should.be_null
 
374
            resp.userCtx.roles.should.not.include ["vice_president"]
 
375
          },
 
376
          error: function(status, error, reason){errorCallback(status, error, reason)}
 
377
        });
 
378
      end
 
379
    end
 
380
  end
 
381
  
 
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"))
 
385
    end
 
386
    
 
387
    it 'should encode only the name of the design document'
 
388
      $.couch.encodeDocId("_design/raptor").should.eql("_design/" + encodeURIComponent("raptor"))
 
389
    end
 
390
    
 
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"))
 
393
    end
 
394
  end
 
395
    
 
396
  describe 'info'
 
397
    it 'should return the CouchDB version'
 
398
      $.couch.info({
 
399
        success: function(resp){
 
400
          resp.couchdb.should.eql "Welcome"
 
401
          resp.version.should_match /^\d\d?\.\d\d?\.\d\d?.*/
 
402
        },
 
403
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
404
      });
 
405
    end
 
406
  end
 
407
  
 
408
  describe 'replicate'
 
409
    before_each
 
410
      db = $.couch.db("spec_db");
 
411
      db.create();
 
412
      db2 = $.couch.db("spec_db_2");
 
413
      db2.create();
 
414
      host = window.location.protocol + "//" + window.location.host ;
 
415
    end
 
416
    
 
417
    after_each
 
418
      db.drop();
 
419
      db2.drop();
 
420
    end
 
421
  
 
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
 
427
        },
 
428
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
429
      });
 
430
    end
 
431
    
 
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
 
437
        },
 
438
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
439
      });
 
440
    end
 
441
    
 
442
    it 'should return source_last_seq'
 
443
      db.saveDoc({'type':'battlestar', 'name':'galactica'});
 
444
      db.saveDoc({'type':'battlestar', 'name':'pegasus'});
 
445
      
 
446
      $.couch.replicate(host + db.uri, host + db2.uri, {
 
447
        success: function(resp){
 
448
          resp.source_last_seq.should.eql 2
 
449
        },
 
450
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
451
      });
 
452
    end
 
453
    
 
454
    it 'should return the replication history'
 
455
      db.saveDoc({'type':'battlestar', 'name':'galactica'});
 
456
      db.saveDoc({'type':'battlestar', 'name':'pegasus'});
 
457
      
 
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
 
462
        },
 
463
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
464
      });
 
465
    end
 
466
    
 
467
    it 'should pass through replication options'
 
468
      db.saveDoc({'type':'battlestar', 'name':'galactica'});
 
469
      db2.drop();
 
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/
 
474
        },
 
475
        success: function(resp){successCallback(resp)}
 
476
      });
 
477
      
 
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
 
482
        },
 
483
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
484
      }, {
 
485
        "create_target":true
 
486
      });
 
487
      
 
488
      db2.info({
 
489
        success: function(resp){
 
490
          resp.db_name.should.eql "spec_db_2"
 
491
        },
 
492
        error: function(status, error, reason){errorCallback(status, error, reason)}
 
493
      });
 
494
    end
 
495
    
 
496
    it 'should alert with an error message prefix'
 
497
      $.couch.replicate("asdf");
 
498
      alert_msg.should.match /Replication failed/
 
499
    end
 
500
  end
 
501
  
 
502
  describe 'newUUID'
 
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
 
507
    end
 
508
    
 
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.
 
512
      $.couch.newUUID(2);
 
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'
 
516
    end
 
517
    
 
518
    it 'should alert with an error message prefix'
 
519
      $.couch.newUUID("asdf");
 
520
      alert_msg.should.match /Failed to retrieve UUID batch/
 
521
    end
 
522
  end
 
523
end
 
 
b'\\ No newline at end of file'