• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const common = require('../common-tap.js')
4const getStream = require('get-stream')
5const npm = require('../../')
6const test = require('tap').test
7const mkdirp = require('mkdirp')
8const mr = require('npm-registry-mock')
9
10var _createEntryUpdateStream = require('../../lib/search/all-package-metadata.js')._createEntryUpdateStream
11
12var server
13
14// this test uses a fresh cache for each test block
15// create them all in common.cache so that we can verify
16// them for root-owned files in sudotest
17let CACHE_DIR
18let cacheCounter = 1
19function setup () {
20  CACHE_DIR = common.cache + '/' + cacheCounter++
21  mkdirp.sync(CACHE_DIR)
22  fixOwner(CACHE_DIR)
23}
24
25const chownr = require('chownr')
26
27const fixOwner = (
28  process.getuid && process.getuid() === 0 &&
29  process.env.SUDO_UID && process.env.SUDO_GID
30) ? (path) => chownr.sync(path, +process.env.SUDO_UID, +process.env.SUDO_GID)
31  : () => {}
32
33test('setup', function (t) {
34  mr({port: common.port, throwOnUnmatched: true}, function (err, s) {
35    t.ifError(err, 'registry mocked successfully')
36    npm.load({ cache: CACHE_DIR, registry: common.registry }, function (err) {
37      t.ifError(err, 'npm loaded successfully')
38      server = s
39      t.pass('all set up')
40      t.done()
41    })
42  })
43})
44
45test('createEntryUpdateStream full request', function (t) {
46  setup()
47  server.get('/-/all').once().reply(200, {
48    '_updated': 1234,
49    'bar': { name: 'bar', version: '1.0.0' },
50    'foo': { name: 'foo', version: '1.0.0' }
51  }, {
52    date: Date.now() // should never be used.
53  })
54  return _createEntryUpdateStream(600, 0, {
55    registry: common.registry
56  }).then(({
57    updateStream: stream,
58    updatedLatest: latest
59  }) => {
60    t.equals(latest, 1234, '`latest` correctly extracted')
61    t.ok(stream, 'returned a stream')
62    return getStream.array(stream)
63  }).then(results => {
64    t.deepEquals(results, [{
65      name: 'bar',
66      version: '1.0.0'
67    }, {
68      name: 'foo',
69      version: '1.0.0'
70    }])
71    server.done()
72  })
73})
74
75test('createEntryUpdateStream partial update', function (t) {
76  setup()
77  var now = Date.now()
78  server.get('/-/all/since?stale=update_after&startkey=1234').once().reply(200, {
79    'bar': { name: 'bar', version: '1.0.0' },
80    'foo': { name: 'foo', version: '1.0.0' }
81  }, {
82    date: (new Date(now)).toISOString()
83  })
84  return _createEntryUpdateStream(600, 1234, {
85    registry: common.registry
86  }).then(({
87    updateStream: stream,
88    updatedLatest: latest
89  }) => {
90    t.equals(latest, now, '`latest` correctly extracted from header')
91    t.ok(stream, 'returned a stream')
92    return getStream.array(stream)
93  }).then(results => {
94    t.deepEquals(results, [{
95      name: 'bar',
96      version: '1.0.0'
97    }, {
98      name: 'foo',
99      version: '1.0.0'
100    }])
101    server.done()
102  })
103})
104
105test('createEntryUpdateStream authed request', function (t) {
106  setup()
107  var token = 'thisisanauthtoken'
108  server.get('/-/all', { authorization: 'Bearer ' + token }).once().reply(200, {
109    '_updated': 1234,
110    'bar': { name: 'bar', version: '1.0.0' },
111    'foo': { name: 'foo', version: '1.0.0' }
112  }, {
113    date: Date.now() // should never be used.
114  })
115  return _createEntryUpdateStream(600, 0, {
116    registry: common.registry,
117    token
118  }).then(({
119    updateStream: stream,
120    updatedLatest: latest
121  }) => {
122    t.equals(latest, 1234, '`latest` correctly extracted')
123    t.ok(stream, 'returned a stream')
124    return getStream.array(stream)
125  }).then(results => {
126    t.deepEquals(results, [{
127      name: 'bar',
128      version: '1.0.0'
129    }, {
130      name: 'foo',
131      version: '1.0.0'
132    }])
133    server.done()
134  })
135})
136
137test('createEntryUpdateStream bad auth', function (t) {
138  setup()
139  var token = 'thisisanauthtoken'
140  server.get('/-/all', { authorization: 'Bearer ' + token }).once().reply(401, {
141    error: 'unauthorized search request'
142  })
143  return _createEntryUpdateStream(600, 0, {
144    registry: common.registry,
145    token
146  }).then(() => {
147    throw new Error('should not succeed')
148  }, err => {
149    t.ok(err, 'got an error from auth failure')
150    t.match(err, /unauthorized/, 'failure message from request used')
151  }).then(() => {
152    server.done()
153  })
154})
155
156test('createEntryUpdateStream not stale', function (t) {
157  setup()
158  var now = Date.now()
159  var staleness = 600
160  return _createEntryUpdateStream(staleness, now, {
161    registry: common.registry
162  }).then(({
163    updateStream: stream,
164    updatedLatest: latest
165  }) => {
166    t.notOk(stream, 'no stream returned')
167    t.notOk(latest, 'no latest returned')
168    server.done()
169    t.end()
170  })
171})
172
173test('cleanup', function (t) {
174  server.close()
175  t.done()
176})
177