• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3var fs = require('fs')
4var path = require('path')
5var writeFileSync = require('graceful-fs').writeFileSync
6
7var mkdirp = require('mkdirp')
8var http = require('http')
9var ssri = require('ssri')
10var t = require('tap')
11var test = t.test
12
13var common = require('../common-tap.js')
14
15var pkg = common.pkg
16var outfile = path.resolve(pkg, '_npmrc')
17
18var modules = path.resolve(pkg, 'node_modules')
19var tarballPath = '/scoped-underscore/-/scoped-underscore-1.3.1.tgz'
20var tarballURL = common.registry + tarballPath
21var tarball = path.resolve(__dirname, '../fixtures/scoped-underscore-1.3.1.tgz')
22var tarballIntegrity = ssri.fromData(fs.readFileSync(tarball)).toString()
23
24var _auth = '0xabad1dea'
25
26var contents = '_auth=' + _auth + '\n' +
27               '\'always-auth\'=true\n'
28
29var json = {
30  name: 'test-package-install',
31  version: '1.0.0',
32  dependencies: {
33    '@scoped/underscore': '1.0.0'
34  }
35}
36
37var shrinkwrap = {
38  name: 'test-package-install',
39  version: '1.0.0',
40  lockfileVersion: 1,
41  dependencies: {
42    '@scoped/underscore': {
43      resolved: tarballURL,
44      integrity: tarballIntegrity,
45      version: '1.3.1'
46    }
47  }
48}
49
50var server = http.createServer()
51t.teardown(() => server.close())
52
53const errors = []
54server.on('request', (req, res) => {
55  const auth = 'Basic ' + _auth
56  if (req.method === 'GET' && req.url === tarballPath) {
57    if (req.headers.authorization === auth) {
58      res.writeHead(200, 'ok')
59      res.end(fs.readFileSync(tarball))
60    } else {
61      res.writeHead(403, 'unauthorized')
62      errors.push("Got authorization of '" + req.headers.authorization + "' expected '" + auth + "'")
63      res.end()
64    }
65  } else {
66    res.writeHead(500)
67    errors.push('Unknown request: ' + req.method + ' ' + req.url)
68    res.end()
69  }
70})
71
72test('setup', function (t) {
73  mkdirp.sync(modules)
74  writeFileSync(path.resolve(pkg, 'package.json'), JSON.stringify(json, null, 2) + '\n')
75  writeFileSync(outfile, contents)
76  writeFileSync(
77    path.resolve(pkg, 'npm-shrinkwrap.json'),
78    JSON.stringify(shrinkwrap, null, 2) + '\n'
79  )
80  server.listen(common.port, t.end)
81})
82
83test('authed npm install with shrinkwrapped global package using _auth', function (t) {
84  common.npm(
85    [
86      'install',
87      '--loglevel', 'error',
88      '--json',
89      '--fetch-retries', 0,
90      '--registry', common.registry,
91      '--userconfig', outfile
92    ],
93    {cwd: pkg, stdio: [0, 'pipe', 2]},
94    function (err, code, stdout) {
95      if (err) throw err
96      t.equal(code, 0, 'npm install exited OK')
97      errors.forEach((err) => t.comment('Error: ' + err))
98      try {
99        var results = JSON.parse(stdout)
100        t.match(results, {added: [{name: '@scoped/underscore', version: '1.3.1'}]}, '@scoped/underscore installed')
101      } catch (ex) {
102        console.error('#', ex)
103        t.ifError(ex, 'stdout was valid JSON')
104      }
105
106      t.end()
107    }
108  )
109})
110