1'use strict' 2 3var fs = require('fs') 4var path = require('path') 5var writeFileSync = require('graceful-fs').writeFileSync 6 7var mkdirp = require('mkdirp') 8var mr = require('npm-registry-mock') 9var ssri = require('ssri') 10var test = require('tap').test 11 12var common = require('../common-tap.js') 13 14var pkg = common.pkg 15var outfile = path.resolve(pkg, '_npmrc') 16 17var modules = path.resolve(pkg, 'node_modules') 18var tarballPath = '/scoped-underscore/-/scoped-underscore-1.3.1.tgz' 19var tarballURL = common.registry + tarballPath 20var tarball = path.resolve(__dirname, '../fixtures/scoped-underscore-1.3.1.tgz') 21var tarballIntegrity = ssri.fromData(fs.readFileSync(tarball)).toString() 22 23var contents = 'registry=' + common.registry + '\n' + 24 '_authToken=0xabad1dea\n' + 25 '\'always-auth\'=true\n' 26 27var json = { 28 name: 'test-package-install', 29 version: '1.0.0', 30 dependencies: { 31 '@scoped/underscore': '1.0.0' 32 } 33} 34 35var shrinkwrap = { 36 name: 'test-package-install', 37 version: '1.0.0', 38 lockfileVersion: 1, 39 dependencies: { 40 '@scoped/underscore': { 41 resolved: tarballURL, 42 integrity: tarballIntegrity, 43 version: '1.3.1' 44 } 45 } 46} 47 48function mocks (server) { 49 var auth = 'Bearer 0xabad1dea' 50 server.get(tarballPath, { authorization: auth }).replyWithFile(200, tarball) 51 server.get(tarballPath).reply(401, { 52 error: 'unauthorized', 53 reason: 'You are not authorized to access this db.' 54 }) 55} 56 57test('setup', function (t) { 58 mkdirp.sync(modules) 59 writeFileSync(path.resolve(pkg, 'package.json'), JSON.stringify(json, null, 2) + '\n') 60 writeFileSync(outfile, contents) 61 writeFileSync( 62 path.resolve(pkg, 'npm-shrinkwrap.json'), 63 JSON.stringify(shrinkwrap, null, 2) + '\n' 64 ) 65 mr({ port: common.port, plugin: mocks }, function (er, s) { 66 t.parent.teardown(() => s.close()) 67 t.ok(s, 'set up mock registry') 68 t.end() 69 }) 70}) 71 72test('authed npm install with shrinkwrapped global package', function (t) { 73 common.npm( 74 [ 75 'install', 76 '--loglevel', 'error', 77 '--json', 78 '--fetch-retries', 0, 79 '--userconfig', outfile, 80 '--registry', common.registry 81 ], 82 {cwd: pkg, stdio: [0, 'pipe', 2]}, 83 function (err, code, stdout) { 84 if (err) throw err 85 t.equal(code, 0, 'npm install exited OK') 86 try { 87 var results = JSON.parse(stdout) 88 t.match(results, {added: [{name: '@scoped/underscore', version: '1.3.1'}]}, '@scoped/underscore installed') 89 } catch (ex) { 90 console.error('#', ex) 91 t.ifError(ex, 'stdout was valid JSON') 92 } 93 94 t.end() 95 } 96 ) 97}) 98