Skip to content
This repository has been archived by the owner on Nov 5, 2018. It is now read-only.

Commit

Permalink
auth -> session.create, unauth -> session.delete
Browse files Browse the repository at this point in the history
  • Loading branch information
tarantoga committed Nov 1, 2011
1 parent ff9d559 commit fd66b21
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cfg/tests.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = exports = cfg = { url: 'http://testnano.iriscouch.com' };
module.exports = exports = cfg = { url: 'http://tarantoga:Jet0&kyms!@localhost:5984' };
20 changes: 12 additions & 8 deletions nano.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,29 +290,32 @@ module.exports = exports = nano = function database_module(cfg) {
return relax({db: "_replicate", body: body, method: "POST"},callback);
}

/****************************************************************************
* session *
****************************************************************************/
/*
* authenticates a user
* creates session
*
* e.g. nano.auth(user, password)
* e.g. nano.session.create(user, password)
*
* @param {user:string} user name
* @param {pass:string} password
*
* @see relax
*/
function auth_db(user, password, callback) {
function create_session(user, password, callback) {
var body = new Buffer("name=" + user + "&" + "password=" + password);
return relax({db: "_session", body:body, method: "POST", content_type: "application/x-www-form-urlencodeddata"}, callback);
}

/*
* ends auth session
* deletes session
*
* e.g. nano.unauth()
* e.g. nano.session.delete()
*
* @see relax
*/
function unauth_db(callback) {
function delete_session(callback) {
cfg.cookie = null; //make sure cookie gets destroyed also if error
return relax({db: "_session", method: "DELETE"}, callback);
}
Expand Down Expand Up @@ -541,12 +544,13 @@ module.exports = exports = nano = function database_module(cfg) {
, replicate: replicate_db
, changes: changes_db
}
, session: { create: create_session
, delete: delete_session
}
, use: document_module
, scope: document_module // alias
, request: relax
, config: cfg
, auth: auth_db
, unauth: unauth_db
, relax: relax // alias
, dinosaur: relax // alias
};
Expand Down
6 changes: 3 additions & 3 deletions tests/shared/auth.js → tests/session/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var ensure = require('ensure')
, nano = require('../../nano')(cfg)
, tests = exports;

tests.auth_user = function (callback) {
tests.create_session = function (callback) {
nano.use('_users').insert(
{ "_id" : "org.couchdb.user:pat"
, "type" : "user"
Expand All @@ -13,13 +13,13 @@ tests.auth_user = function (callback) {
, "password_sha" : "fdb2b8f8ae582440fbd11786fd9afd90920b42a1"
, "salt" : "659b9645544dfc82124b7fb07a0bd5f9"
}, function(err,user) {
nano.auth('pat', '123', function(e,b,h){
nano.session.create('pat', '123', function(e,b,h){
callback(e,user.rev,b,h);
});
});
};

tests.auth_user_ok = function (err,rev,response,headers) {
tests.create_session_ok = function (err,rev,response,headers) {
nano.use('_users').destroy('org.couchdb.user:pat',rev);
this.t.notOk(err);
this.t.equal(headers['status-code'], 200); // header tests go here
Expand Down
8 changes: 4 additions & 4 deletions tests/shared/unauth.js → tests/session/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var ensure = require('ensure')
, nano = require('../../nano')(cfg)
, tests = exports;

tests.auth_user = function (callback) {
tests.delete_session = function (callback) {
nano.use('_users').insert(
{ "_id" : "org.couchdb.user:pat"
, "type" : "user"
Expand All @@ -13,15 +13,15 @@ tests.auth_user = function (callback) {
, "password_sha" : "fdb2b8f8ae582440fbd11786fd9afd90920b42a1"
, "salt" : "659b9645544dfc82124b7fb07a0bd5f9"
}, function(err,user) {
nano.auth('pat', '123', function(){
nano.unauth(function(e,b,h){
nano.session.create('pat', '123', function(){
nano.session.delete(function(e,b,h){
callback(e,user.rev,b,h);
});
});
});
};

tests.auth_user_ok = function (err,rev,response,headers) {
tests.delete_session_ok = function (err,rev,response,headers) {
nano.use('_users').destroy('org.couchdb.user:pat',rev);
this.t.notOk(err);
this.t.equal(headers['status-code'], 200); // header tests go here
Expand Down

0 comments on commit fd66b21

Please sign in to comment.