|
| 1 | +var test = require('tap').test |
| 2 | + |
| 3 | +var server = require('./lib/server.js') |
| 4 | +var common = require('./lib/common.js') |
| 5 | +var client = common.freshClient() |
| 6 | + |
| 7 | +function nop () {} |
| 8 | + |
| 9 | +var URI = 'http://localhost:1337' |
| 10 | +var PARAMS = { |
| 11 | + auth: { |
| 12 | + token: 'foo' |
| 13 | + }, |
| 14 | + scope: 'myorg', |
| 15 | + team: 'myteam' |
| 16 | +} |
| 17 | + |
| 18 | +var commands = ['create', 'destroy', 'add', 'rm', 'ls'] |
| 19 | + |
| 20 | +test('team create basic', function (t) { |
| 21 | + var teamData = { |
| 22 | + name: PARAMS.team, |
| 23 | + scope_id: 1234, |
| 24 | + created: '2015-07-23T18:07:49.959Z', |
| 25 | + updated: '2015-07-23T18:07:49.959Z', |
| 26 | + deleted: null |
| 27 | + } |
| 28 | + server.expect('PUT', '/-/org/myorg/team', function (req, res) { |
| 29 | + t.equal(req.method, 'PUT') |
| 30 | + onJsonReq(req, function (json) { |
| 31 | + t.same(json, { name: PARAMS.team }) |
| 32 | + res.statusCode = 200 |
| 33 | + res.json(teamData) |
| 34 | + }) |
| 35 | + }) |
| 36 | + client.team('create', URI, PARAMS, function (err, data) { |
| 37 | + t.ifError(err, 'no errors') |
| 38 | + t.same(data, teamData) |
| 39 | + t.end() |
| 40 | + }) |
| 41 | +}) |
| 42 | + |
| 43 | +test('team destroy', function (t) { |
| 44 | + var teamData = { |
| 45 | + name: 'myteam', |
| 46 | + scope_id: 1234, |
| 47 | + created: '2015-07-23T18:07:49.959Z', |
| 48 | + updated: '2015-07-23T18:07:49.959Z', |
| 49 | + deleted: '2015-07-23T18:27:27.178Z' |
| 50 | + } |
| 51 | + server.expect('DELETE', '/-/team/myorg/myteam', function (req, res) { |
| 52 | + t.equal(req.method, 'DELETE') |
| 53 | + onJsonReq(req, function (json) { |
| 54 | + t.same(json, undefined) |
| 55 | + res.statusCode = 200 |
| 56 | + res.json(teamData) |
| 57 | + }) |
| 58 | + }) |
| 59 | + client.team('destroy', URI, PARAMS, function (err, data) { |
| 60 | + t.ifError(err, 'no errors') |
| 61 | + t.same(data, teamData) |
| 62 | + t.end() |
| 63 | + }) |
| 64 | +}) |
| 65 | + |
| 66 | +test('team add basic', function (t) { |
| 67 | + var params = Object.create(PARAMS) |
| 68 | + params.user = 'zkat' |
| 69 | + server.expect('PUT', '/-/team/myorg/myteam/user', function (req, res) { |
| 70 | + t.equal(req.method, 'PUT') |
| 71 | + onJsonReq(req, function (json) { |
| 72 | + t.same(json, { user: params.user }) |
| 73 | + res.statusCode = 200 |
| 74 | + res.json(undefined) |
| 75 | + }) |
| 76 | + }) |
| 77 | + client.team('add', URI, params, function (err, data) { |
| 78 | + t.ifError(err, 'no errors') |
| 79 | + t.same(data, undefined) |
| 80 | + t.end() |
| 81 | + }) |
| 82 | +}) |
| 83 | + |
| 84 | +test('team add user not in org', function (t) { |
| 85 | + var params = Object.create(PARAMS) |
| 86 | + params.user = 'zkat' |
| 87 | + var errMsg = 'user is already in team' |
| 88 | + server.expect('PUT', '/-/team/myorg/myteam/user', function (req, res) { |
| 89 | + t.equal(req.method, 'PUT') |
| 90 | + res.statusCode = 400 |
| 91 | + res.json({ |
| 92 | + error: errMsg |
| 93 | + }) |
| 94 | + }) |
| 95 | + client.team('add', URI, params, function (err, data) { |
| 96 | + t.equal(err.message, errMsg + ' : ' + '-/team/myorg/myteam/user') |
| 97 | + t.same(data, {error: errMsg}) |
| 98 | + t.end() |
| 99 | + }) |
| 100 | +}) |
| 101 | + |
| 102 | +test('team rm basic', function (t) { |
| 103 | + var params = Object.create(PARAMS) |
| 104 | + params.user = 'bcoe' |
| 105 | + server.expect('DELETE', '/-/team/myorg/myteam/user', function (req, res) { |
| 106 | + t.equal(req.method, 'DELETE') |
| 107 | + onJsonReq(req, function (json) { |
| 108 | + t.same(json, params) |
| 109 | + res.statusCode = 200 |
| 110 | + res.json(undefined) |
| 111 | + }) |
| 112 | + }) |
| 113 | + client.team('rm', URI, params, function (err, data) { |
| 114 | + t.ifError(err, 'no errors') |
| 115 | + t.same(data, undefined) |
| 116 | + t.end() |
| 117 | + }) |
| 118 | +}) |
| 119 | + |
| 120 | +test('team ls (on org)', function (t) { |
| 121 | + var params = Object.create(PARAMS) |
| 122 | + params.team = null |
| 123 | + var teams = ['myorg:team1', 'myorg:team2', 'myorg:team3'] |
| 124 | + server.expect('GET', '/-/org/myorg/team?format=cli', function (req, res) { |
| 125 | + t.equal(req.method, 'GET') |
| 126 | + onJsonReq(req, function (json) { |
| 127 | + t.same(json, undefined) |
| 128 | + res.statusCode = 200 |
| 129 | + res.json(teams) |
| 130 | + }) |
| 131 | + }) |
| 132 | + client.team('ls', URI, params, function (err, data) { |
| 133 | + t.ifError(err, 'no errors') |
| 134 | + t.same(data, teams) |
| 135 | + t.end() |
| 136 | + }) |
| 137 | +}) |
| 138 | + |
| 139 | +test('team ls (on team)', function (t) { |
| 140 | + var uri = '/-/team/myorg/myteam/user?format=cli' |
| 141 | + var users = ['zkat', 'bcoe'] |
| 142 | + server.expect('GET', uri, function (req, res) { |
| 143 | + t.equal(req.method, 'GET') |
| 144 | + onJsonReq(req, function (json) { |
| 145 | + t.same(json, undefined) |
| 146 | + res.statusCode = 200 |
| 147 | + res.json(users) |
| 148 | + }) |
| 149 | + }) |
| 150 | + client.team('ls', URI, PARAMS, function (err, data) { |
| 151 | + t.ifError(err, 'no errors') |
| 152 | + t.same(data, users) |
| 153 | + t.end() |
| 154 | + }) |
| 155 | +}) |
| 156 | + |
| 157 | +// test('team edit', function (t) { |
| 158 | +// server.expect('PUT', '/-/org/myorg/team', function (req, res) { |
| 159 | +// t.equal(req.method, 'PUT') |
| 160 | +// res.statusCode = 201 |
| 161 | +// res.json({}) |
| 162 | +// }) |
| 163 | +// client.team('create', URI, PARAMS, function (err, data) { |
| 164 | +// t.ifError(err, 'no errors') |
| 165 | +// t.end() |
| 166 | +// }) |
| 167 | +// }) |
| 168 | + |
| 169 | +test('team command base validation', function (t) { |
| 170 | + t.throws(function () { |
| 171 | + client.team(undefined, URI, PARAMS, nop) |
| 172 | + }, 'command is required') |
| 173 | + commands.forEach(function (cmd) { |
| 174 | + t.throws(function () { |
| 175 | + client.team(cmd, undefined, PARAMS, nop) |
| 176 | + }, 'registry URI is required') |
| 177 | + t.throws(function () { |
| 178 | + client.team(cmd, URI, undefined, nop) |
| 179 | + }, 'params is required') |
| 180 | + t.throws(function () { |
| 181 | + client.team(cmd, URI, {scope: 'o', team: 't'}, nop) |
| 182 | + }, 'auth is required') |
| 183 | + t.throws(function () { |
| 184 | + client.team(cmd, URI, {auth: {token: 'f'}, team: 't'}, nop) |
| 185 | + }, 'scope is required') |
| 186 | + t.throws(function () { |
| 187 | + client.team(cmd, URI, PARAMS, {}) |
| 188 | + }, 'callback must be a function') |
| 189 | + if (cmd !== 'ls') { |
| 190 | + t.throws(function () { |
| 191 | + client.team( |
| 192 | + cmd, URI, {auth: {token: 'f'}, scope: 'o'}, nop) |
| 193 | + }, 'team name is required') |
| 194 | + } |
| 195 | + if (cmd === 'add' || cmd === 'rm') { |
| 196 | + t.throws(function () { |
| 197 | + client.team( |
| 198 | + cmd, URI, PARAMS, nop) |
| 199 | + }, 'user is required') |
| 200 | + } |
| 201 | + }) |
| 202 | + t.end() |
| 203 | +}) |
| 204 | + |
| 205 | +function onJsonReq (req, cb) { |
| 206 | + var buffer = '' |
| 207 | + req.setEncoding('utf8') |
| 208 | + req.on('data', function (data) { buffer += data }) |
| 209 | + req.on('end', function () { cb(buffer ? JSON.parse(buffer) : undefined) }) |
| 210 | +} |
0 commit comments