Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
support saving --always-auth on adduser
Browse files Browse the repository at this point in the history
  • Loading branch information
othiym23 committed Sep 19, 2014
1 parent ea5b3d4 commit f2d2190
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 4 deletions.
17 changes: 16 additions & 1 deletion doc/cli/npm-adduser.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ npm-adduser(1) -- Add a registry user account

## SYNOPSIS

npm adduser [--registry=url] [--scope=@orgname]
npm adduser [--registry=url] [--scope=@orgname] [--always-auth]

## DESCRIPTION

Expand Down Expand Up @@ -45,6 +45,21 @@ e.g.
This will set a registry for the given scope and login or create a user for
that registry at the same time.

### always-auth

Default: false

If specified, save configuration indicating that all requests to the given
registry should include authorization information. Useful for private
registries. Can be used with `--registry` and / or `--scope`, e.g.

npm adduser --registry=http://private-registry.example.com --always-auth

This will ensure that all requests to that registry (including for tarballs)
include an authorization header. See `always-auth` in `npm-config(7)` for more
details on always-auth. Registry-specific configuaration of `always-auth` takes
precedence over any global configuration.

## SEE ALSO

* npm-registry(7)
Expand Down
7 changes: 4 additions & 3 deletions lib/adduser.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ function save (c, u, cb) {
}
else {
npm.config.setCredentialsByURI(uri, {
username : u.u,
password : u.p,
email : u.e
username : u.u,
password : u.p,
email : u.e,
alwaysAuth : npm.config.get("always-auth")
})
}

Expand Down
157 changes: 157 additions & 0 deletions test/tap/login-always-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
var fs = require("fs")
var path = require("path")
var spawn = require("child_process").spawn
var rimraf = require("rimraf")
var mr = require("npm-registry-mock")

var test = require("tap").test
var common = require("../common-tap.js")

var node = process.execPath
var npm = require.resolve("../../bin/npm-cli.js")
var opts = {cwd : __dirname}
var outfile = path.resolve(__dirname, "_npmrc")
var responses = {
"Username": "u\n",
"Password": "p\n",
"Email": "u@p.me\n"
}

function mocks(server) {
server.filteringRequestBody(function (r) {
if (r.match(/\"_id\":\"org\.couchdb\.user:u\"/)) {
return "auth"
}
})
server.put("/-/user/org.couchdb.user:u", "auth")
.reply(201, {username : "u", password : "p", email : "u@p.me"})
}

test("npm login", function (t) {
mr({port : common.port, mocks : mocks}, function (s) {
var runner = spawn(
node,
[
npm,
"login",
"--registry=" + common.registry,
"--loglevel=silent",
"--userconfig=" + outfile
],
opts
)

var o = "", e = "", remaining = Object.keys(responses).length
runner.stdout.on("data", function (chunk) {
remaining--
o += chunk

var label = chunk.toString("utf8").split(":")[0]
runner.stdin.write(responses[label])

if (remaining === 0) runner.stdin.end()
})
runner.stderr.on("data", function (chunk) { e += chunk })

runner.on("close", function (code) {
t.notOk(code, "exited OK")
t.notOk(e, "no error output")
var config = fs.readFileSync(outfile, "utf8")
t.like(config, /:always-auth=false/, "always-auth is scoped and false (by default)")
s.close()
rimraf(outfile, function (err) {
t.ifError(err, "removed config file OK")
t.end()
})
})
})
})

test("npm login --always-auth", function (t) {
mr({port : common.port, mocks : mocks}, function (s) {
var runner = spawn(
node,
[
npm,
"login",
"--registry=" + common.registry,
"--loglevel=silent",
"--userconfig=" + outfile,
"--always-auth"
],
opts
)

var o = "", e = "", remaining = Object.keys(responses).length
runner.stdout.on("data", function (chunk) {
remaining--
o += chunk

var label = chunk.toString("utf8").split(":")[0]
runner.stdin.write(responses[label])

if (remaining === 0) runner.stdin.end()
})
runner.stderr.on("data", function (chunk) { e += chunk })

runner.on("close", function (code) {
t.notOk(code, "exited OK")
t.notOk(e, "no error output")
var config = fs.readFileSync(outfile, "utf8")
t.like(config, /:always-auth=true/, "always-auth is scoped and true")
s.close()
rimraf(outfile, function (err) {
t.ifError(err, "removed config file OK")
t.end()
})
})
})
})

test("npm login --no-always-auth", function (t) {
mr({port : common.port, mocks : mocks}, function (s) {
var runner = spawn(
node,
[
npm,
"login",
"--registry=" + common.registry,
"--loglevel=silent",
"--userconfig=" + outfile,
"--no-always-auth"
],
opts
)

var o = "", e = "", remaining = Object.keys(responses).length
runner.stdout.on("data", function (chunk) {
remaining--
o += chunk

var label = chunk.toString("utf8").split(":")[0]
runner.stdin.write(responses[label])

if (remaining === 0) runner.stdin.end()
})
runner.stderr.on("data", function (chunk) { e += chunk })

runner.on("close", function (code) {
t.notOk(code, "exited OK")
t.notOk(e, "no error output")
var config = fs.readFileSync(outfile, "utf8")
t.like(config, /:always-auth=false/, "always-auth is scoped and false")
s.close()
rimraf(outfile, function (err) {
t.ifError(err, "removed config file OK")
t.end()
})
})
})
})


test("cleanup", function (t) {
rimraf.sync(outfile)
t.pass("cleaned up")
t.end()
})

0 comments on commit f2d2190

Please sign in to comment.