1 var test = require('tap').test
3 var server = require('./lib/server.js')
4 var common = require('./lib/common.js')
5 var client = common.freshClient()
9 var URI = 'http://localhost:1337'
18 var commands = ['create', 'destroy', 'add', 'rm', 'ls']
20 test('team create basic', function (t) {
24 created: '2015-07-23T18:07:49.959Z',
25 updated: '2015-07-23T18:07:49.959Z',
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 })
36 client.team('create', URI, PARAMS, function (err, data) {
37 t.ifError(err, 'no errors')
38 t.same(data, teamData)
43 test('team destroy', function (t) {
47 created: '2015-07-23T18:07:49.959Z',
48 updated: '2015-07-23T18:07:49.959Z',
49 deleted: '2015-07-23T18:27:27.178Z'
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)
59 client.team('destroy', URI, PARAMS, function (err, data) {
60 t.ifError(err, 'no errors')
61 t.same(data, teamData)
66 test('team add basic', function (t) {
67 var params = Object.create(PARAMS)
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 })
77 client.team('add', URI, params, function (err, data) {
78 t.ifError(err, 'no errors')
79 t.same(data, undefined)
84 test('team add user not in org', function (t) {
85 var params = Object.create(PARAMS)
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')
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})
102 test('team rm basic', function (t) {
103 var params = Object.create(PARAMS)
105 server.expect('DELETE', '/-/team/myorg/myteam/user', function (req, res) {
106 t.equal(req.method, 'DELETE')
107 onJsonReq(req, function (json) {
113 client.team('rm', URI, params, function (err, data) {
114 t.ifError(err, 'no errors')
115 t.same(data, undefined)
120 test('team ls (on org)', function (t) {
121 var params = Object.create(PARAMS)
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)
132 client.team('ls', URI, params, function (err, data) {
133 t.ifError(err, 'no errors')
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)
150 client.team('ls', URI, PARAMS, function (err, data) {
151 t.ifError(err, 'no errors')
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
163 // client.team('create', URI, PARAMS, function (err, data) {
164 // t.ifError(err, 'no errors')
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')
190 t.throws(function () {
192 cmd, URI, {auth: {token: 'f'}, scope: 'o'}, nop)
193 }, 'team name is required')
195 if (cmd === 'add' || cmd === 'rm') {
196 t.throws(function () {
198 cmd, URI, PARAMS, nop)
199 }, 'user is required')
205 test('cleanup', function (t) {
210 function onJsonReq (req, cb) {
212 req.setEncoding('utf8')
213 req.on('data', function (data) { buffer += data })
214 req.on('end', function () { cb(buffer ? JSON.parse(buffer) : undefined) })