diff options
-rw-r--r-- | lib/config/clear-credentials-by-uri.js | 16 | ||||
-rw-r--r-- | lib/config/core.js | 1 | ||||
-rw-r--r-- | test/tap/config-credentials.js | 55 |
3 files changed, 72 insertions, 0 deletions
diff --git a/lib/config/clear-credentials-by-uri.js b/lib/config/clear-credentials-by-uri.js new file mode 100644 index 000000000..88131f7ad --- /dev/null +++ b/lib/config/clear-credentials-by-uri.js @@ -0,0 +1,16 @@ +var assert = require("assert") + +var toNerfDart = require("./nerf-dart.js") + +module.exports = clearCredentialsByURI + +function clearCredentialsByURI (uri) { + assert(uri && typeof uri === "string", "registry URL is required") + + var nerfed = toNerfDart(uri) + + this.del(nerfed + ":_authToken", "user") + this.del(nerfed + ":_password", "user") + this.del(nerfed + ":username", "user") + this.del(nerfed + ":email", "user") +} diff --git a/lib/config/core.js b/lib/config/core.js index ddd851cf7..97c17919d 100644 --- a/lib/config/core.js +++ b/lib/config/core.js @@ -227,6 +227,7 @@ Conf.prototype.setUser = require("./set-user.js") Conf.prototype.findPrefix = require("./find-prefix.js") Conf.prototype.getCredentialsByURI = require("./get-credentials-by-uri.js") Conf.prototype.setCredentialsByURI = require("./set-credentials-by-uri.js") +Conf.prototype.clearCredentialsByURI = require("./clear-credentials-by-uri.js") Conf.prototype.loadExtras = function(cb) { this.setUser(function(er) { diff --git a/test/tap/config-credentials.js b/test/tap/config-credentials.js index c24bb7e1b..1cb2a7eb4 100644 --- a/test/tap/config-credentials.js +++ b/test/tap/config-credentials.js @@ -28,6 +28,18 @@ test("trying to set credentials with no URI", function (t) { }) }) +test("trying to clear credentials with no URI", function (t) { + npmconf.load(common.builtin, function (er, conf) { + t.ifError(er, "configuration loaded") + + t.throws(function () { + conf.clearCredentialsByURI() + }, "enforced missing URI") + + t.end() + }) +}) + test("set with missing credentials object", function (t) { npmconf.load(common.builtin, function (er, conf) { t.ifError(er, "configuration loaded") @@ -76,6 +88,24 @@ test("set with token", function (t) { }) }) +test("clear with token", function (t) { + npmconf.load(common.builtin, function (er, conf) { + t.ifError(er, "configuration loaded") + + t.doesNotThrow(function () { + conf.setCredentialsByURI(URI, {token : "simple-token"}) + }, "needs only token") + + t.doesNotThrow(function () { + conf.clearCredentialsByURI(URI) + }, "needs only URI") + + t.notOk(conf.getCredentialsByURI(URI).token, "token all gone") + + t.end() + }) +}) + test("set with missing username", function (t) { npmconf.load(common.builtin, function (er, conf) { t.ifError(er, "configuration loaded") @@ -157,6 +187,31 @@ test("set with old-style credentials", function (t) { }) }) +test("clear with old-style credentials", function (t) { + npmconf.load(common.builtin, function (er, conf) { + t.ifError(er, "configuration loaded") + + var credentials = { + username : "username", + password : "password", + email : "ogd@aoaioxxysz.net" + } + + t.doesNotThrow(function () { + conf.setCredentialsByURI(URI, credentials) + }, "requires all of username, password, and email") + + t.doesNotThrow(function () { + conf.clearCredentialsByURI(URI) + }, "clearing only required URI") + + t.notOk(conf.getCredentialsByURI(URI).username, "username cleared") + t.notOk(conf.getCredentialsByURI(URI).password, "password cleared") + + t.end() + }) +}) + test("get old-style credentials for default registry", function (t) { npmconf.load(common.builtin, function (er, conf) { var actual = conf.getCredentialsByURI(conf.get("registry")) |