Skip to content

Commit

Permalink
logout: Fix npm-logout to remove scope config if specified
Browse files Browse the repository at this point in the history
Fixes: #10529
Credit: @wyze
Reviewed-By: @iarna
PR-URL: npm/npm#11417
  • Loading branch information
wyze authored and iarna committed Mar 10, 2016
1 parent d1d0233 commit 460ed21
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ var mapToRegistry = require('./utils/map-to-registry.js')

logout.usage = 'npm logout [--registry=<url>] [--scope=<@scope>]'

function afterLogout (normalized, cb) {
var scope = npm.config.get('scope')

if (scope) npm.config.del(scope + ':registry')

npm.config.clearCredentialsByURI(normalized)
npm.config.save('user', cb)
}

function logout (args, cb) {
cb = dezalgo(cb)

Expand All @@ -19,13 +28,12 @@ function logout (args, cb) {
npm.registry.logout(normalized, { auth: auth }, function (err) {
if (err) return cb(err)

npm.config.clearCredentialsByURI(normalized)
npm.config.save('user', cb)
afterLogout(normalized, cb)
})
} else if (auth.username || auth.password) {
log.verbose('logout', 'clearing user credentials for', normalized)
npm.config.clearCredentialsByURI(normalized)
npm.config.save('user', cb)

afterLogout(normalized, cb)
} else {
cb(new Error(
'Not logged in to', normalized + ',', "so can't log out."
Expand Down
70 changes: 70 additions & 0 deletions test/tap/logout-scoped.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
var fs = require('fs')
var path = require('path')

var mkdirp = require('mkdirp')
var mr = require('npm-registry-mock')
var rimraf = require('rimraf')
var test = require('tap').test

var common = require('../common-tap.js')

var pkg = path.resolve(__dirname, 'logout')
var outfile = path.join(pkg, '_npmrc')
var opts = { cwd: pkg }

var contents = function () {/*
foo=boo
@bar:registry=http://localhost:1337
//localhost:1337/:_authToken=glarb
*/}.toString().split('\n').slice(1, -1).join('\n')

function mocks (server) {
server.delete('/-/user/token/glarb')
.reply(200, {})
}

test('setup', function (t) {
cleanup()
setup()
t.end()
})

test('npm logout', function (t) {
mr({ port: common.port, plugin: mocks }, function (err, s) {
if (err) throw err

common.npm(
[
'logout',
'--registry', common.registry,
'--scope', '@bar',
'--loglevel', 'silent',
'--userconfig', outfile
],
opts,
function (err, code) {
t.ifError(err, 'no error output')
t.notOk(code, 'exited OK')

var config = fs.readFileSync(outfile, 'utf8')
t.equal(config, 'foo=boo\n', 'creds gone')
s.close()
t.end()
}
)
})
})

test('cleanup', function (t) {
cleanup()
t.end()
})

function setup () {
mkdirp.sync(pkg)
fs.writeFileSync(outfile, contents)
}

function cleanup () {
rimraf.sync(pkg)
}

0 comments on commit 460ed21

Please sign in to comment.