1var common = require('../common-tap.js') 2 3var fs = require('fs') 4var path = require('path') 5var createServer = require('http').createServer 6 7var test = require('tap').test 8var rimraf = require('rimraf') 9 10var opts = { cwd: __dirname } 11 12var FIXTURE_PATH = path.resolve(common.pkg, 'fixture_npmrc') 13 14test('npm whoami with basic auth', function (t) { 15 var s = '//registry.lvh.me/:username = wombat\n' + 16 '//registry.lvh.me/:_password = YmFkIHBhc3N3b3Jk\n' + 17 '//registry.lvh.me/:email = lindsay@wdu.org.au\n' 18 fs.writeFileSync(FIXTURE_PATH, s, 'ascii') 19 fs.chmodSync(FIXTURE_PATH, 0o644) 20 21 common.npm( 22 [ 23 'whoami', 24 '--userconfig=' + FIXTURE_PATH, 25 '--registry=http://registry.lvh.me/' 26 ], 27 opts, 28 function (err, code, stdout, stderr) { 29 t.ifError(err) 30 31 t.equal(stderr, '', 'got nothing on stderr') 32 t.equal(code, 0, 'exit ok') 33 t.equal(stdout, 'wombat\n', 'got username') 34 t.end() 35 } 36 ) 37}) 38 39test('npm whoami with bearer auth', { timeout: 8000 }, function (t) { 40 var s = '//localhost:' + common.port + 41 '/:_authToken = wombat-developers-union\n' 42 fs.writeFileSync(FIXTURE_PATH, s, 'ascii') 43 fs.chmodSync(FIXTURE_PATH, 0o644) 44 45 function verify (req, res) { 46 t.equal(req.method, 'GET') 47 t.equal(req.url, '/-/whoami') 48 49 res.setHeader('content-type', 'application/json') 50 res.writeHead(200) 51 res.end(JSON.stringify({ username: 'wombat' }), 'utf8') 52 } 53 54 var server = createServer(verify) 55 56 server.listen(common.port, function () { 57 common.npm( 58 [ 59 'whoami', 60 '--userconfig=' + FIXTURE_PATH, 61 '--registry=http://localhost:' + common.port + '/' 62 ], 63 opts, 64 function (err, code, stdout, stderr) { 65 t.ifError(err) 66 67 t.equal(stderr, '', 'got nothing on stderr') 68 t.equal(code, 0, 'exit ok') 69 t.equal(stdout, 'wombat\n', 'got username') 70 rimraf.sync(FIXTURE_PATH) 71 server.close() 72 t.end() 73 } 74 ) 75 }) 76}) 77