1var resolve = require('path').resolve 2var writeFileSync = require('graceful-fs').writeFileSync 3 4var fs = require('fs') 5var mkdirp = require('mkdirp') 6var http = require('http') 7const t = require('tap') 8 9var common = require('../common-tap.js') 10var toNerfDart = require('../../lib/config/nerf-dart.js') 11 12var pkg = common.pkg 13var outfile = resolve(pkg, '_npmrc') 14var modules = resolve(pkg, 'node_modules') 15var tarballPath = '/scoped-underscore/-/scoped-underscore-1.3.1.tgz' 16// needs to be a different hostname to verify tokens (not) being sent correctly 17var tarballURL = 'http://127.0.0.1:' + common.port + tarballPath 18var tarball = resolve(__dirname, '../fixtures/scoped-underscore-1.3.1.tgz') 19 20var EXEC_OPTS = { cwd: pkg, stdio: [0, 'pipe', 2] } 21 22var auth = 'Bearer 0xabad1dea' 23var server = http.createServer() 24server.on('request', (req, res) => { 25 if (req.method === 'GET' && req.url === tarballPath) { 26 if (req.headers.authorization === auth) { 27 res.writeHead(403, 'this token should not be sent') 28 res.end() 29 } else { 30 res.writeHead(200, 'ok') 31 res.end(fs.readFileSync(tarball)) 32 } 33 } else { 34 res.writeHead(500) 35 res.end() 36 } 37}) 38 39var contents = '@scoped:registry=' + common.registry + '\n' + 40 toNerfDart(common.registry) + ':_authToken=0xabad1dea\n' 41 42var json = { 43 name: 'test-package-install', 44 version: '1.0.0', 45 dependencies: { 46 '@scoped/underscore': '1.3.1' 47 } 48} 49 50var shrinkwrap = { 51 name: 'test-package-install', 52 version: '1.0.0', 53 dependencies: { 54 '@scoped/underscore': { 55 resolved: tarballURL, 56 version: '1.3.1' 57 } 58 } 59} 60 61t.teardown(() => server.close()) 62 63t.test('setup', function (t) { 64 mkdirp.sync(modules) 65 writeFileSync(resolve(pkg, 'package.json'), JSON.stringify(json, null, 2) + '\n') 66 writeFileSync(outfile, contents) 67 writeFileSync( 68 resolve(pkg, 'npm-shrinkwrap.json'), 69 JSON.stringify(shrinkwrap, null, 2) + '\n' 70 ) 71 server.listen(common.port, t.end) 72}) 73 74t.test('authed npm install with tarball not on registry', function (t) { 75 common.npm( 76 [ 77 'install', 78 '--json', 79 '--fetch-retries', 0, 80 '--registry', common.registry, 81 '--userconfig', outfile 82 ], 83 EXEC_OPTS, 84 function (err, code, stdout, stderr) { 85 if (err) throw err 86 t.equal(code, 0, 'npm install exited OK') 87 t.comment(stdout.trim()) 88 t.comment(stderr.trim()) 89 t.notOk(stderr, 'no output on stderr') 90 try { 91 var results = JSON.parse(stdout) 92 } catch (ex) { 93 t.ifError(ex, 'stdout was valid JSON') 94 } 95 96 if (results) { 97 var installedversion = [ 98 { 99 'name': '@scoped/underscore', 100 'version': '1.3.1' 101 } 102 ] 103 t.match(results.added, installedversion, '@scoped/underscore installed') 104 } 105 106 t.end() 107 } 108 ) 109}) 110