From 5eb31edb0b886cf79b0036c6d1c67c4d07ff5836 Mon Sep 17 00:00:00 2001 From: bcoe Date: Tue, 13 Jan 2015 18:20:46 -0800 Subject: [PATCH] lookup a user. --- lib/star.js | 53 ++++++++++++++++++++++++++++------------------------ test/star.js | 9 +++++++++ 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/lib/star.js b/lib/star.js index f43a82e..df17e2c 100644 --- a/lib/star.js +++ b/lib/star.js @@ -1,6 +1,7 @@ module.exports = star -var assert = require("assert") +var assert = require("assert"), + url = require("url") function star (uri, params, cb) { assert(typeof uri === "string", "must pass registry URI to star") @@ -21,28 +22,32 @@ function star (uri, params, cb) { var client = this this.request(uri+"?write=true", { auth : auth }, function (er, fullData) { if (er) return cb(er) - - fullData = { - _id : fullData._id, - _rev : fullData._rev, - users : fullData.users || {} - } - - if (starred) { - client.log.info("starring", fullData._id) - fullData.users[auth.username] = true - client.log.verbose("starring", fullData) - } else { - delete fullData.users[auth.username] - client.log.info("unstarring", fullData._id) - client.log.verbose("unstarring", fullData) - } - - var options = { - method : "PUT", - body : fullData, - auth : auth - } - return client.request(uri, options, cb) + + client.whoami(uri, params, function(err, username) { + if (er) return cb(er) + + fullData = { + _id : fullData._id, + _rev : fullData._rev, + users : fullData.users || {} + } + + if (starred) { + client.log.info("starring", fullData._id) + fullData.users[username] = true + client.log.verbose("starring", fullData) + } else { + delete fullData.users[username] + client.log.info("unstarring", fullData._id) + client.log.verbose("unstarring", fullData) + } + + var options = { + method : "PUT", + body : fullData, + auth : auth + } + return client.request(uri, options, cb) + }) }) } diff --git a/test/star.js b/test/star.js index 932882d..8b9ef38 100644 --- a/test/star.js +++ b/test/star.js @@ -143,6 +143,14 @@ test("if token auth, sets bearer on get and put", function(t) { .get('/underscore?write=true') .reply(200, {}) + var getUser = nock('http://localhost:1010', { + reqheaders: { + authorization: 'Bearer foo' + } + }) + .get('/-/whoami') + .reply(200) + var starPut = nock('http://localhost:1010', { reqheaders: { authorization: 'Bearer foo' @@ -160,6 +168,7 @@ test("if token auth, sets bearer on get and put", function(t) { client.star("http://localhost:1010/underscore", params, function (error, data) { starGet.done() starPut.done() + getUser.done() t.end() }) })