summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/update-index.js
blob: 0586269722c8cdb95ff10d3c4f3aed0027b4aa65 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var common = require('../common-tap.js')
var test = require('tap').test
var npm = require('../../')
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var path = require('path')
var mr = require('npm-registry-mock')

var updateIndex = require('../../lib/cache/update-index.js')

var PKG_DIR = path.resolve(__dirname, 'get-basic')
var CACHE_DIR = path.resolve(PKG_DIR, 'cache')

var server

function setup (t, mock, extra) {
  mkdirp.sync(CACHE_DIR)
  mr({ port: common.port, plugin: mock }, function (er, s) {
    npm.load({ cache: CACHE_DIR, registry: common.registry }, function (err) {
      if (extra) {
        Object.keys(extra).forEach(function (k) {
          npm.config.set(k, extra[k], 'user')
        })
      }
      t.ifError(err, 'no error')
      server = s
      t.end()
    })
  })
}

function cleanup (t) {
  server.close(function () {
    rimraf.sync(PKG_DIR)

    t.end()
  })
}

test('setup basic', function (t) {
  setup(t, mocks.basic)
})

test('request basic', function (t) {
  updateIndex(0, function (er) {
    t.ifError(er, 'no error')
    t.end()
  })
})

test('cleanup basic', cleanup)

test('setup auth', function (t) {
  setup(t, mocks.auth)
})

test('request auth failure', function (t) {
  updateIndex(0, function (er) {
    t.equals(er.code, 'E401', 'gotta get that auth')
    t.ok(/^unauthorized/.test(er.message), 'unauthorized message')
    t.end()
  })
})

test('cleanup auth failure', cleanup)

test('setup auth', function (t) {
  // mimic as if alwaysAuth had been set
  setup(t, mocks.auth, {
    _auth: new Buffer('bobby:tables').toString('base64'),
    'always-auth': true
  })
})

test('request auth success', function (t) {
  updateIndex(0, function (er) {
    t.ifError(er, 'no error')
    t.end()
  })
})

test('cleanup auth', cleanup)

var mocks = {
  basic: function (mock) {
    mock.get('/-/all').reply(200, allMock)
  },
  auth: function (mock) {
    var littleBobbyTablesAuth = new Buffer('bobby:tables').toString('base64')
    var auth = 'Basic ' + littleBobbyTablesAuth
    mock.get('/-/all', { authorization: auth }).reply(200, allMock)
    mock.get('/-/all').reply(401, {
      error: 'unauthorized',
      reason: 'You are not authorized to access this db.'
    })
  }
}

var allMock = {
  '_updated': 1411727900 + 25,
  'generator-frontcow': {
      'name': 'generator-frontcow',
      'description': 'f36b6a6123da50959741e2ce4d634f96ec668c56 This is a fake description to ensure we do not accidentally search the real npm registry or use some kind of cache',
      'dist-tags': {
          'latest': '0.1.19'
      },
      'maintainers': [
          {
              'name': 'bcabanes',
              'email': 'contact@benjamincabanes.com'
          }
      ],
      'homepage': 'https://github.com/bcabanes/generator-frontcow',
      'keywords': [
          'sass',
          'frontend',
          'yeoman-generator',
          'atomic',
          'design',
          'sass',
          'foundation',
          'foundation5',
          'atomic design',
          'bourbon',
          'polyfill',
          'font awesome'
      ],
      'repository': {
          'type': 'git',
          'url': 'https://github.com/bcabanes/generator-frontcow'
      },
      'author': {
          'name': 'ben',
          'email': 'contact@benjamincabanes.com',
          'url': 'https://github.com/bcabanes'
      },
      'bugs': {
          'url': 'https://github.com/bcabanes/generator-frontcow/issues'
      },
      'license': 'MIT',
      'readmeFilename': 'README.md',
      'time': {
          'modified': '2014-10-03T02:26:18.406Z'
      },
      'versions': {
          '0.1.19': 'latest'
      }
  },
  'marko': {
      'name': 'marko',
      'description': 'Marko is an extensible, streaming, asynchronous, high performance, HTML-based templating language that can be used in Node.js or in the browser.',
      'dist-tags': {
          'latest': '1.2.16'
      },
      'maintainers': [
          {
              'name': 'pnidem',
              'email': 'pnidem@gmail.com'
          },
          {
              'name': 'philidem',
              'email': 'phillip.idem@gmail.com'
          }
      ],
      'homepage': 'https://github.com/raptorjs/marko',
      'keywords': [
          'templating',
          'template',
          'async',
          'streaming'
      ],
      'repository': {
          'type': 'git',
          'url': 'https://github.com/raptorjs/marko.git'
      },
      'author': {
          'name': 'Patrick Steele-Idem',
          'email': 'pnidem@gmail.com'
      },
      'bugs': {
          'url': 'https://github.com/raptorjs/marko/issues'
      },
      'license': 'Apache License v2.0',
      'readmeFilename': 'README.md',
      'users': {
          'pnidem': true
      },
      'time': {
          'modified': '2014-10-03T02:27:31.775Z'
      },
      'versions': {
          '1.2.16': 'latest'
      }
  }
}