Permalink
Browse files

add security method to Database.prototype

  • Loading branch information...
1 parent 58d783d commit 7eda3570058056e7ffa30916bb420557818ca2d5 @nrstott committed Aug 11, 2010
Showing with 47 additions and 15 deletions.
  1. +21 −6 lib/couchdb.js
  2. +26 −9 tests/node/couchclient.js
View
@@ -160,15 +160,15 @@
* @return {Promise} a promise
* @api public
*/
- Database.prototype.remove = function(cb) {
+ Database.prototype.remove = function() {
return this.request({
method: httpMethod.DELETE
- }, cb);
+ });
};
Database.prototype.allDocs = function() {
return this.request({
- pathInfo: "/_all_docs"
+ path: "/_all_docs"
});
};
@@ -182,7 +182,7 @@
Database.prototype.getDoc = Database.prototype.openDoc = function(id, cb) {
return this.request({
path: "/"+id
- }, cb);
+ });
};
Database.prototype.saveDoc = function(doc, opts) {
@@ -203,12 +203,27 @@
});
};
- Database.prototype.removeDoc = function(id, rev, cb) {
+ Database.prototype.removeDoc = function(id, rev) {
return this.request({
method: httpMethod.DELETE,
path: "/"+id,
query: {rev:rev}
- }, cb);
+ });
+ };
+
+ Database.prototype.security = function(obj) {
+ if (obj === undefined || obj === null) {
+ return this.request({
+ method: httpMethod.GET,
+ path: "/_security"
+ });
+ }
+
+ return this.request({
+ method: httpMethod.PUT,
+ path: "/_security",
+ body: [ JSON.stringify(obj) ]
+ });
};
/**
View
@@ -105,7 +105,6 @@ exports["should replicate"] = function() {
function before() {
db = client.db(DB_NAME);
return when(db.exists(), function(exists) {
- console.log("exists:"+exists);
if (exists) {
return when(db.remove(), function() {
return db.create();
@@ -122,10 +121,9 @@ exports["should replicate"] = function() {
var tests = {
"should have no documents": function() {
- console.log("allDocs");
return when(db.allDocs(), function(resp) {
- assert.ok(Array.isArray(resp.rows));
- assert.equal(0, resp.rows.length);
+ assert.ok(Array.isArray(resp.rows), "Expected resp.rows to be an array but it is '"+typeof(resp.rows)+"'");
+ assert.equal(0, resp.rows.length, "Expected 0 rows, found '"+resp.rows.length+"'");
});
},
"should create document with id": function() {
@@ -137,28 +135,47 @@ exports["should replicate"] = function() {
"should create document without id": function() {
return when(db.saveDoc({ hello: "world" }),
function(resp) {
- console.log(JSON.stringify(resp));
assert.ok(resp.ok);
});
+ },
+ "should get security object": function() {
+ return when(db.security(), function(resp) {
+ console.log("Secuirty:"+JSON.stringify(resp));
+ assert.deepEqual({}, resp);
+ });
+ },
+ "should set security object": function() {
+ var securityObj = { readers: { names: ["tester"], roles: ["test_reader"] } };
+ return when(db.security(securityObj), function(resp) {
+ assert.ok(resp.ok);
+ return when(db.security(), function(resp) {
+ assert.deepEqual(resp, securityObj);
+ })
+ });
}
};
exports["database tests"] = {};
- for (var test in tests) {
+ var keys= [];
+ for (var k in tests) {
+ keys.push(k);
+ }
+
+ keys.forEach(function(test) {
exports["database tests"][test] = function() {
return when(before(), function() {
return when(tests[test](),
function() {
return after();
},
function(err) {
- assert.ok(false, err);
- after();
+ assert.ok(false, err, JSON.stringify(err));
+ return after();
});
});
};
- }
+ });
})();
if (require.main == module) {

0 comments on commit 7eda357

Please sign in to comment.