]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/npm-registry-client/test/star.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / npm-registry-client / test / star.js
1 var test = require('tap').test
2 var server = require('./lib/server.js')
3 var common = require('./lib/common.js')
4 var client = common.freshClient()
5 var cache = require('./fixtures/underscore/cache.json')
6 var nock = require('nock')
7
8 function nop () {}
9
10 var URI = 'https://npm.registry:8043/rewrite'
11 var STARRED = true
12 var USERNAME = 'username'
13 var PASSWORD = '%1234@asdf%'
14 var EMAIL = 'i@izs.me'
15 var AUTH = {
16   username: USERNAME,
17   password: PASSWORD,
18   email: EMAIL
19 }
20 var PARAMS = {
21   starred: STARRED,
22   auth: AUTH
23 }
24
25 test('star call contract', function (t) {
26   t.throws(function () {
27     client.star(undefined, PARAMS, nop)
28   }, 'requires a URI')
29
30   t.throws(function () {
31     client.star([], PARAMS, nop)
32   }, 'requires URI to be a string')
33
34   t.throws(function () {
35     client.star(URI, undefined, nop)
36   }, 'requires params object')
37
38   t.throws(function () {
39     client.star(URI, '', nop)
40   }, 'params must be object')
41
42   t.throws(function () {
43     client.star(URI, PARAMS, undefined)
44   }, 'requires callback')
45
46   t.throws(function () {
47     client.star(URI, PARAMS, 'callback')
48   }, 'callback must be function')
49
50   t.throws(
51     function () {
52       var params = { starred: STARRED }
53       client.star(URI, params, nop)
54     },
55     { name: 'AssertionError', message: 'must pass auth to star' },
56     'params must include auth'
57   )
58
59   t.end()
60 })
61
62 test('star a package', function (t) {
63   server.expect('GET', '/underscore?write=true', function (req, res) {
64     t.equal(req.method, 'GET')
65
66     res.json(cache)
67   })
68
69   server.expect('PUT', '/underscore', function (req, res) {
70     t.equal(req.method, 'PUT')
71
72     var b = ''
73     req.setEncoding('utf8')
74     req.on('data', function (d) {
75       b += d
76     })
77
78     req.on('end', function () {
79       var updated = JSON.parse(b)
80
81       var already = [
82         'vesln', 'mvolkmann', 'lancehunt', 'mikl', 'linus', 'vasc', 'bat',
83         'dmalam', 'mbrevoort', 'danielr', 'rsimoes', 'thlorenz'
84       ]
85       for (var i = 0; i < already.length; i++) {
86         var current = already[i]
87         t.ok(
88           updated.users[current],
89           current + ' still likes this package'
90         )
91       }
92       t.ok(updated.users[USERNAME], 'user is in the starred list')
93
94       res.statusCode = 201
95       res.json({ starred: true })
96     })
97   })
98
99   var params = { starred: STARRED, auth: AUTH }
100
101   client.star('http://localhost:1337/underscore', params, function (er, data) {
102     t.ifError(er, 'no errors')
103     t.ok(data.starred, 'was starred')
104
105     t.end()
106   })
107 })
108
109 test('if password auth, only sets authorization on put', function (t) {
110   var starGet = nock('http://localhost:1010')
111     .get('/underscore?write=true')
112     .reply(200, {})
113
114   var starPut = nock('http://localhost:1010', {
115     reqheaders: {
116       authorization: 'Basic ' + new Buffer(AUTH.username + ':' +
117                                            AUTH.password).toString('base64')
118     }
119   })
120   .put('/underscore')
121   .reply(200)
122
123   var params = { starred: STARRED, auth: AUTH }
124
125   client.star('http://localhost:1010/underscore', params, function (er) {
126     t.ifError(er, 'starred without issues')
127     starGet.done()
128     starPut.done()
129     t.end()
130   })
131 })
132
133 test('if token auth, sets bearer on get and put', function (t) {
134   var starGet = nock('http://localhost:1010', {
135     reqheaders: {
136       authorization: 'Bearer foo'
137     }
138   })
139   .get('/underscore?write=true')
140   .reply(200, {})
141
142   var getUser = nock('http://localhost:1010', {
143     reqheaders: {
144       authorization: 'Bearer foo'
145     }
146   })
147   .get('/-/whoami')
148   .reply(200, { username: 'bcoe' })
149
150   var starPut = nock('http://localhost:1010', {
151     reqheaders: {
152       authorization: 'Bearer foo'
153     }
154   })
155   .put('/underscore')
156   .reply(200)
157
158   var params = {
159     starred: STARRED,
160     auth: {
161       token: 'foo'
162     }
163   }
164   client.star('http://localhost:1010/underscore', params, function (er) {
165     t.ifError(er, 'starred without error')
166     starGet.done()
167     starPut.done()
168     getUser.done()
169     t.end()
170   })
171 })
172
173 test('cleanup', function (t) {
174   server.close()
175   t.end()
176 })