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')
10 var URI = 'https://npm.registry:8043/rewrite'
12 var USERNAME = 'username'
13 var PASSWORD = '%1234@asdf%'
14 var EMAIL = 'i@izs.me'
25 test('star call contract', function (t) {
26 t.throws(function () {
27 client.star(undefined, PARAMS, nop)
30 t.throws(function () {
31 client.star([], PARAMS, nop)
32 }, 'requires URI to be a string')
34 t.throws(function () {
35 client.star(URI, undefined, nop)
36 }, 'requires params object')
38 t.throws(function () {
39 client.star(URI, '', nop)
40 }, 'params must be object')
42 t.throws(function () {
43 client.star(URI, PARAMS, undefined)
44 }, 'requires callback')
46 t.throws(function () {
47 client.star(URI, PARAMS, 'callback')
48 }, 'callback must be function')
52 var params = { starred: STARRED }
53 client.star(URI, params, nop)
55 { name: 'AssertionError', message: 'must pass auth to star' },
56 'params must include auth'
62 test('star a package', function (t) {
63 server.expect('GET', '/underscore?write=true', function (req, res) {
64 t.equal(req.method, 'GET')
69 server.expect('PUT', '/underscore', function (req, res) {
70 t.equal(req.method, 'PUT')
73 req.setEncoding('utf8')
74 req.on('data', function (d) {
78 req.on('end', function () {
79 var updated = JSON.parse(b)
82 'vesln', 'mvolkmann', 'lancehunt', 'mikl', 'linus', 'vasc', 'bat',
83 'dmalam', 'mbrevoort', 'danielr', 'rsimoes', 'thlorenz'
85 for (var i = 0; i < already.length; i++) {
86 var current = already[i]
88 updated.users[current],
89 current + ' still likes this package'
92 t.ok(updated.users[USERNAME], 'user is in the starred list')
95 res.json({ starred: true })
99 var params = { starred: STARRED, auth: AUTH }
101 client.star('http://localhost:1337/underscore', params, function (er, data) {
102 t.ifError(er, 'no errors')
103 t.ok(data.starred, 'was starred')
109 test('if password auth, only sets authorization on put', function (t) {
110 var starGet = nock('http://localhost:1010')
111 .get('/underscore?write=true')
114 var starPut = nock('http://localhost:1010', {
116 authorization: 'Basic ' + new Buffer(AUTH.username + ':' +
117 AUTH.password).toString('base64')
123 var params = { starred: STARRED, auth: AUTH }
125 client.star('http://localhost:1010/underscore', params, function (er) {
126 t.ifError(er, 'starred without issues')
133 test('if token auth, sets bearer on get and put', function (t) {
134 var starGet = nock('http://localhost:1010', {
136 authorization: 'Bearer foo'
139 .get('/underscore?write=true')
142 var getUser = nock('http://localhost:1010', {
144 authorization: 'Bearer foo'
148 .reply(200, { username: 'bcoe' })
150 var starPut = nock('http://localhost:1010', {
152 authorization: 'Bearer foo'
164 client.star('http://localhost:1010/underscore', params, function (er) {
165 t.ifError(er, 'starred without error')
173 test('cleanup', function (t) {