Permalink
Browse files

[cookies] fixes #77 #73 and #71

  • Loading branch information...
1 parent 2f88c16 commit 003127da80c99b4338dbd93f7d08344081bf9164 @dscape committed May 23, 2012
Showing with 61 additions and 25 deletions.
  1. +1 −1 nano.js
  2. +1 −1 package.json
  3. +1 −0 tests/fixtures/cfg.json
  4. +9 −0 tests/fixtures/shared/cookie.json
  5. +11 −5 tests/helpers.js
  6. +38 −18 tests/shared/cookie.js
View
@@ -801,7 +801,7 @@ module.exports = exports = nano = function database_module(cfg) {
}
}
}
-
+
if(!(cfg && cfg.url)) {
throw errs.create(
{ "scope" : "init"
View
@@ -2,7 +2,7 @@
, "description" : "minimalistic couchdb driver for node.js"
, "homepage" : "http://github.com/dscape/nano"
, "repository" : "git://github.com/dscape/nano"
-, "version" : "3.0.3"
+, "version" : "3.0.4"
, "author" : "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)"
, "contributors" :
[ "Thiago Arrais <thiago.arrais@gmail.com> (http://thiagoarrais.com)"
@@ -1,3 +1,4 @@
{ "timeout" : 20000
, "couch" : "http://localhost:5984"
+, "admin" : "http://admin:password@localhost:5984"
}
@@ -4,6 +4,11 @@
, "status" : 201
, "response" : "{ \"ok\": true }"
}
+, { "path" : "/_config/admins/admin"
+ , "method" : "put"
+ , "body" : "*"
+ , "response" : "{\"ok\": true}"
+ }
, { "method" : "post"
, "path" : "/_session"
, "body" : "name=admin&password=password"
@@ -17,6 +22,10 @@
, "body" : "{\"foo\":\"baz\"}"
, "response" : "{\"ok\":true,\"id\":\"234\",\"rev\":\"1-333231\"}"
}
+, { "path" : "/_config/admins/admin"
+ , "method" : "delete"
+ , "response" : "{\"ok\": true}"
+ }
, { "method" : "delete"
, "path" : "/shared_cookie"
, "response" : "{ \"ok\": true }"
View
@@ -31,11 +31,17 @@ function fake_chain() {
}
helpers.timeout = cfg.timeout;
-helpers.nano = nano(cfg.couch);
-helpers.Nano = nano;
-helpers.couch = cfg.couch;
-helpers.pixel = "Qk06AAAAAAAAADYAAAAoAAAAAQAAAP////8BABgAAAAA" +
- "AAAAAAATCwAAEwsAAAAAAAAAAAAAWm2CAA==";
+helpers.nano = nano(cfg.couch);
+helpers.Nano = nano;
+helpers.couch = cfg.couch;
+helpers.admin = cfg.admin;
+helpers.pixel = "Qk06AAAAAAAAADYAAAAoAAAAAQAAAP////8BABgAAAAA" +
+ "AAAAAAATCwAAEwsAAAAAAAAAAAAAWm2CAA==";
+
+var auth = require("url").parse(cfg.admin).auth.split(":");
+
+helpers.username = auth[0];
+helpers.password = auth[1];
helpers.loadFixture = function helpersLoadFixture(filename, json) {
var contents = fs.readFileSync(
@@ -6,42 +6,62 @@ var specify = require("specify")
, nock = helpers.nock
;
-var mock = nock(helpers.couch, "shared/cookie")
- , cookie
- , cookie_nano
- , nano_admin = new Nano('http://admin:password@localhost:5984')
+var mock = nock(helpers.couch, "shared/cookie")
+ , admin = Nano(helpers.admin)
+ , cookie
+ , cookie_nano
;
specify("shared_cookie:setup", timeout, function (assert) {
- nano_admin.db.create("shared_cookie", function (err, response) {
+ // creates a db in admin party mode
+ nano.db.create("shared_cookie", function (err, response) {
assert.equal(err, undefined, "Failed to create database");
- nano.request({
- method: "POST",
- db: "_session",
- form: { "name" : "admin", "password" : "password" },
- content_type: "application/x-www-form-urlencoded; charset=utf-8"
- }, function (err, response, headers) {
- assert.equal(err, undefined, "Should have logged in successfully");
- assert.ok(headers['set-cookie'], "Response should have a set-cookie header");
- cookie = headers['set-cookie'];
- });
+ // creates a admin user, leaves admin party mode
+ nano.relax(
+ { method : "PUT"
+ , path : "_config/admins/" + helpers.username
+ , body : helpers.password
+ }, function (err, response, headers) {
+ assert.equal(err, undefined, "Failed to create admin user");
+ // authenticate
+ nano.relax(
+ { method : "POST"
+ , db : "_session"
+ , form : { "name" : "admin", "password" : "password" }
+ , content_type : "application/x-www-form-urlencoded; charset=utf-8"
+ }, function (err, response, headers) {
+ assert.equal(err, undefined, "Should have logged in successfully");
+ assert.ok(headers['set-cookie'],
+ "Response should have a set-cookie header");
+ cookie = headers['set-cookie'];
+ });
+ });
});
});
specify("shared_cookie:test", timeout, function (assert) {
- Nano({ url : "http://localhost:5984", cookie: cookie }).use("shared_cookie")
- .insert({"foo": "baz"}, null, function (error, response) {
+ var server = Nano({ url : helpers.couch, cookie: cookie });
+ var db = server.use("shared_cookie");
+ // insert with a shared cookie
+ db.insert({"foo": "baz"}, null, function (error, response) {
assert.equal(error, undefined, "Should have stored value");
assert.equal(response.ok, true, "Response should be ok");
assert.ok(response.rev, "Response should have rev");
});
});
specify("shared_cookie:teardown", timeout, function (assert) {
- nano_admin.db.destroy("shared_cookie", function (err) {
+ // back to admin party mode
+ admin.relax(
+ { method : "DELETE"
+ , path : "_config/admins/" + helpers.username
+ }, function (err, response, headers) {
+ // delete the database that we created
+ nano.db.destroy("shared_cookie", function (err) {
assert.equal(err, undefined, "Failed to destroy database");
assert.ok(mock.isDone(), "Some mocks didn't run");
});
+ });
});
specify.run(process.argv.slice(2));

0 comments on commit 003127d

Please sign in to comment.