summaryrefslogtreecommitdiff
path: root/rel/overlay/share/www/spec/couch_js_class_methods_spec.js
blob: 7eac2348ca40259a3d160c30a6633269372db03a (plain)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// Specs for couch.js lines 313-470

describe 'CouchDB class'
  describe 'session stuff'
    before
      useTestUserDb();
    end
  
    after
      useOldUserDb();
    end
    
    before_each
      userDoc = users_db.save(CouchDB.prepareUserDoc({name: "Gaius Baltar", roles: ["president"]}, "secretpass"));
    end
  
    after_each
      users_db.deleteDoc({_id : userDoc.id, _rev : userDoc.rev})
    end
    
    describe '.login'
      it 'should return ok true'
        CouchDB.login("Gaius Baltar", "secretpass").ok.should.be_true
      end
          
      it 'should return the name of the logged in user'
        CouchDB.login("Gaius Baltar", "secretpass").name.should.eql "Gaius Baltar"
      end
          
      it 'should return the roles of the logged in user'
        CouchDB.login("Gaius Baltar", "secretpass").roles.should.eql ["president"]
      end
      
      it 'should post _session'
        CouchDB.should.receive("request", "once").with_args("POST", "/_session")
        CouchDB.login("Gaius Baltar", "secretpass");
      end
      
      it 'should create a session'
        CouchDB.login("Gaius Baltar", "secretpass");
        CouchDB.session().userCtx.name.should.eql "Gaius Baltar"
      end
    end
      
    describe '.logout'
      before_each
        CouchDB.login("Gaius Baltar", "secretpass");
      end
    
      it 'should return ok true'
        CouchDB.logout().ok.should.be_true
      end
    
      it 'should delete _session'
        CouchDB.should.receive("request", "once").with_args("DELETE", "/_session")
        CouchDB.logout();
      end
      
      it 'should result in an invalid session'
        CouchDB.logout();
        CouchDB.session().name.should.be_null
      end
    end
  
    describe '.session'
      before_each
        CouchDB.login("Gaius Baltar", "secretpass");
      end
    
      it 'should return ok true'
        CouchDB.session().ok.should.be_true
      end
    
      it 'should return the users name'
        CouchDB.session().userCtx.name.should.eql "Gaius Baltar"
      end
    
      it 'should return the users roles'
        CouchDB.session().userCtx.roles.should.eql ["president"]
      end
    
      it 'should return the name of the authentication db'
        CouchDB.session().info.authentication_db.should.eql "spec_users_db"
      end
    
      it 'should return the active authentication handler'
        CouchDB.session().info.authenticated.should.eql "cookie"
      end
    end
  end
  
  describe 'db stuff'
    before_each
      db = new CouchDB("spec_db", {"X-Couch-Full-Commit":"false"});
      db.createDb();
    end
  
    after_each
      db.deleteDb();
    end
  
    describe '.prepareUserDoc'
      before_each
        userDoc = CouchDB.prepareUserDoc({name: "Laura Roslin"}, "secretpass");
      end
      
      it 'should return the users name'
        userDoc.name.should.eql "Laura Roslin"
      end
      
      it 'should prefix the id with the CouchDB user_prefix'
        userDoc._id.should.eql "org.couchdb.user:Laura Roslin"
      end
      
      it 'should return the users roles'
        var userDocWithRoles = CouchDB.prepareUserDoc({name: "William Adama", roles: ["admiral", "commander"]}, "secretpass")
        userDocWithRoles.roles.should.eql ["admiral", "commander"]
      end
      
      it 'should return the hashed password'
        userDoc.password_sha.length.should.be_at_least 30
        userDoc.password_sha.should.be_a String
      end
    end
      
    describe '.allDbs'
      it 'should get _all_dbs'
        CouchDB.should.receive("request", "once").with_args("GET", "/_all_dbs");
        CouchDB.allDbs();
      end
      
      it 'should return an array that includes a created database'
        temp_db = new CouchDB("temp_spec_db", {"X-Couch-Full-Commit":"false"});
        temp_db.createDb();
        CouchDB.allDbs().should.include("temp_spec_db");
        temp_db.deleteDb();
      end
      
      it 'should return an array that does not include a database that does not exist'
        CouchDB.allDbs().should.not.include("not_existing_temp_spec_db");
      end
    end
    
    describe '.allDesignDocs'
      it 'should return the total number of documents'
        CouchDB.allDesignDocs().spec_db.total_rows.should.eql 0
        db.save({'type':'battlestar', 'name':'galactica'});
        CouchDB.allDesignDocs().spec_db.total_rows.should.eql 1
      end
      
      it 'should return undefined when the db does not exist'
        CouchDB.allDesignDocs().non_existing_db.should.be_undefined
      end
      
      it 'should return no documents when there are no design documents'
        CouchDB.allDesignDocs().spec_db.rows.should.eql []
      end
      
      it 'should return all design documents'
        var designDoc = {
          "views" : {
            "people" : {
              "map" : "function(doc) { emit(doc._id, doc); }"
            }
          },
          "_id" : "_design/spec_db"
        };
        db.save(designDoc);
        
        var allDesignDocs = CouchDB.allDesignDocs();
        allDesignDocs.spec_db.rows[0].id.should.eql "_design/spec_db"
        allDesignDocs.spec_db.rows[0].key.should.eql "_design/spec_db"
        allDesignDocs.spec_db.rows[0].value.rev.length.should.be_at_least 30
      end
    end
    
    describe '.getVersion'
      it 'should get the CouchDB version'
        CouchDB.should.receive("request", "once").with_args("GET", "/")
        CouchDB.getVersion();
      end
      
      it 'should return the CouchDB version'
        CouchDB.getVersion().should_match /^\d\d?\.\d\d?\.\d\d?.*/
      end
    end
    
    describe '.replicate'
      before_each
        db2 = new CouchDB("spec_db_2", {"X-Couch-Full-Commit":"false"});
        db2.createDb();
        host = window.location.protocol + "//" + window.location.host ;
      end
      
      after_each
        db2.deleteDb();
      end
      
      it 'should return no_changes true when there are no changes between the dbs'
        CouchDB.replicate(host + db.uri, host + db2.uri).no_changes.should.be_true
      end
      
      it 'should return the session ID'
        db.save({'type':'battlestar', 'name':'galactica'});
        CouchDB.replicate(host + db.uri, host + db2.uri).session_id.length.should.be_at_least 30
      end
      
      it 'should return source_last_seq'
        db.save({'type':'battlestar', 'name':'galactica'});
        db.save({'type':'battlestar', 'name':'pegasus'});
        
        CouchDB.replicate(host + db.uri, host + db2.uri).source_last_seq.should.eql 2
      end
      
      it 'should return the replication history'
        db.save({'type':'battlestar', 'name':'galactica'});
        db.save({'type':'battlestar', 'name':'pegasus'});
        
        var result = CouchDB.replicate(host + db.uri, host + db2.uri);
        result.history[0].docs_written.should.eql 2
        result.history[0].start_last_seq.should.eql 0
      end
      
      it 'should pass through replication options'
        db.save({'type':'battlestar', 'name':'galactica'});
        db2.deleteDb();
        -{CouchDB.replicate(host + db.uri, host + db2.uri)}.should.throw_error
        var result = CouchDB.replicate(host + db.uri, host + db2.uri, {"body" : {"create_target":true}});
    
        result.ok.should.eql true
        result.history[0].docs_written.should.eql 1
        db2.info().db_name.should.eql "spec_db_2"
      end
    end
    
    describe '.newXhr'
      it 'should return a XMLHTTPRequest'
        CouchDB.newXhr().should.have_prop 'readyState'
        CouchDB.newXhr().should.have_prop 'responseText'
        CouchDB.newXhr().should.have_prop 'status'
      end
    end
    
    describe '.request'
      it 'should return a XMLHttpRequest'
        var req = CouchDB.request("GET", '/');
        req.should.include "readyState"
        req.should.include "responseText"
        req.should.include "statusText"
      end
      
      it 'should pass through the options headers'
        var xhr = CouchDB.newXhr();
        stub(CouchDB, 'newXhr').and_return(xhr);
        
        xhr.should.receive("setRequestHeader", "once").with_args("X-Couch-Full-Commit", "true")
        CouchDB.request("GET", "/", {'headers': {"X-Couch-Full-Commit":"true"}});
      end
      
      it 'should pass through the options body'
        var xhr = CouchDB.newXhr();
        stub(CouchDB, 'newXhr').and_return(xhr);
       
        xhr.should.receive("send", "once").with_args({"body_key":"body_value"})
        CouchDB.request("GET", "/", {'body': {"body_key":"body_value"}});
      end
      
      it 'should prepend the urlPrefix to the uri'
        var oldPrefix = CouchDB.urlPrefix;
        CouchDB.urlPrefix = "/_utils";
       
        var xhr = CouchDB.newXhr();
        stub(CouchDB, 'newXhr').and_return(xhr);
        
        xhr.should.receive("open", "once").with_args("GET", "/_utils/", false)
        CouchDB.request("GET", "/", {'headers': {"X-Couch-Full-Commit":"true"}});
        
        CouchDB.urlPrefix = oldPrefix;
      end
    end
    
    describe '.requestStats'
      it 'should get the stats for specified module and key'
        var stats = CouchDB.requestStats('couchdb', 'open_databases', null);
        stats.description.should.eql 'number of open databases'
        stats.current.should.be_a Number
      end
      
      it 'should add flush true to the request when there is a test argument'
        CouchDB.should.receive("request", "once").with_args("GET", "/_stats/httpd/requests?flush=true")
        CouchDB.requestStats('httpd', 'requests', 'test');
      end
      
      it 'should still work when there is a test argument'
        var stats = CouchDB.requestStats('httpd_status_codes', '200', 'test');
        stats.description.should.eql 'number of HTTP 200 OK responses'
        stats.sum.should.be_a Number
      end
    end
    
    describe '.newUuids'
      after_each
        CouchDB.uuids_cache = [];
      end
      
      it 'should return the specified amount of uuids'
        var uuids = CouchDB.newUuids(45);
        uuids.should.have_length 45
      end
          
      it 'should return an array with uuids'
        var uuids = CouchDB.newUuids(1);
        uuids[0].should.be_a String
        uuids[0].should.have_length 32
      end
      
      it 'should leave the uuids_cache with 100 uuids when theres no buffer size specified'
        CouchDB.newUuids(23);
        CouchDB.uuids_cache.should.have_length 100
      end
      
      it 'should leave the uuids_cache with the specified buffer size'
        CouchDB.newUuids(23, 150);
        CouchDB.uuids_cache.should.have_length 150
      end
      
      it 'should get the uuids from the uuids_cache when there are enough uuids in there'
        CouchDB.newUuids(10);
        CouchDB.newUuids(25);
        CouchDB.uuids_cache.should.have_length 75
      end
      
      it 'should create new uuids and add as many as specified to the uuids_cache when there are not enough uuids in the cache'
        CouchDB.newUuids(10);
        CouchDB.newUuids(125, 60);
        CouchDB.uuids_cache.should.have_length 160
      end
    end
    
    describe '.maybeThrowError'
      it 'should throw an error when the request has status 404'
        var req = CouchDB.request("GET", "/nonexisting_db");
        -{CouchDB.maybeThrowError(req)}.should.throw_error
      end
    
      it 'should throw an error when the request has status 412'
        var req = CouchDB.request("PUT", "/spec_db");
        -{CouchDB.maybeThrowError(req)}.should.throw_error
      end
    
      it 'should throw an error when the request has status 405'
        var req = CouchDB.request("DELETE", "/_utils");
        -{CouchDB.maybeThrowError(req)}.should.throw_error
      end
    
      it 'should throw the responseText of the request'
        var req = CouchDB.request("GET", "/nonexisting_db");
        try {
          CouchDB.maybeThrowError(req)
        } catch(e) {
          e.error.should.eql JSON.parse(req.responseText).error
          e.reason.should.eql JSON.parse(req.responseText).reason
        }
      end
    
      it 'should throw an unknown error when the responseText is invalid json'
        mock_request().and_return("invalid json...", "application/json", 404, {})
        try {
          CouchDB.maybeThrowError(CouchDB.newXhr())
        } catch(e) {
          e.error.should.eql "unknown"
          e.reason.should.eql "invalid json..."
        }
      end
    end
    
    describe '.params'
      it 'should turn a json object into a http params string'
        var params = CouchDB.params({"president":"laura", "cag":"lee"})
        params.should.eql "president=laura&cag=lee"
      end
    
      it 'should return a blank string when the object is empty'
        var params = CouchDB.params({})
        params.should.eql ""
      end
    end
  end
end