summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/whoami.js
blob: 268e0f94feb095c234a54aab486da8229c7bdecc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var common = require('../common-tap.js')

var fs = require('fs')
var path = require('path')
var createServer = require('http').createServer

var test = require('tap').test
var rimraf = require('rimraf')

var opts = { cwd: __dirname }

var FIXTURE_PATH = path.resolve(__dirname, 'fixture_npmrc')

test('npm whoami with basic auth', function (t) {
  var s = '//registry.lvh.me/:username = wombat\n' +
          '//registry.lvh.me/:_password = YmFkIHBhc3N3b3Jk\n' +
          '//registry.lvh.me/:email = lindsay@wdu.org.au\n'
  fs.writeFileSync(FIXTURE_PATH, s, 'ascii')
  fs.chmodSync(FIXTURE_PATH, '0444')

  common.npm(
    [
      'whoami',
      '--userconfig=' + FIXTURE_PATH,
      '--registry=http://registry.lvh.me/'
    ],
    opts,
    function (err, code, stdout, stderr) {
      t.ifError(err)

      t.equal(stderr, '', 'got nothing on stderr')
      t.equal(code, 0, 'exit ok')
      t.equal(stdout, 'wombat\n', 'got username')
      rimraf.sync(FIXTURE_PATH)
      t.end()
    }
  )
})

test('npm whoami with bearer auth', { timeout: 2 * 1000 }, function (t) {
  var s = '//localhost:' + common.port +
          '/:_authToken = wombat-developers-union\n'
  fs.writeFileSync(FIXTURE_PATH, s, 'ascii')
  fs.chmodSync(FIXTURE_PATH, '0444')

  function verify (req, res) {
    t.equal(req.method, 'GET')
    t.equal(req.url, '/-/whoami')

    res.setHeader('content-type', 'application/json')
    res.writeHeader(200)
    res.end(JSON.stringify({ username: 'wombat' }), 'utf8')
  }

  var server = createServer(verify)

  server.listen(common.port, function () {
    common.npm(
      [
        'whoami',
        '--userconfig=' + FIXTURE_PATH,
        '--registry=http://localhost:' + common.port + '/'
      ],
      opts,
      function (err, code, stdout, stderr) {
        t.ifError(err)

        t.equal(stderr, '', 'got nothing on stderr')
        t.equal(code, 0, 'exit ok')
        t.equal(stdout, 'wombat\n', 'got username')
        rimraf.sync(FIXTURE_PATH)
        server.close()
        t.end()
      }
    )
  })
})