• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3var test = require('tap').test
4var common = require('../common-tap.js')
5
6var mr = common.fakeRegistry.compat
7
8var server
9
10test('setup', function (t) {
11  mr({port: common.port}, function (err, s) {
12    t.ifError(err, 'registry mocked successfully')
13    server = s
14    t.end()
15  })
16})
17
18const names = ['add', 'set']
19const roles = ['developer', 'admin', 'owner']
20
21names.forEach(function (name) {
22  test('org ' + name + ' [orgname] [username]: defaults to developer', function (t) {
23    const membershipData = {
24      org: {
25        name: 'myorg',
26        size: 1
27      },
28      user: 'myuser',
29      role: 'developer'
30    }
31    server.put('/-/org/myorg/user', JSON.stringify({
32      user: 'myuser'
33    })).reply(200, membershipData)
34    common.npm([
35      'org', 'add', 'myorg', 'myuser',
36      '--json',
37      '--registry', common.registry,
38      '--loglevel', 'silent'
39    ], {}, function (err, code, stdout, stderr) {
40      t.ifError(err, 'npm org')
41
42      t.equal(code, 0, 'exited OK')
43      t.equal(stderr, '', 'no error output')
44
45      t.same(JSON.parse(stdout), membershipData)
46      t.end()
47    })
48  })
49
50  roles.forEach(function (role) {
51    test('org ' + name + ' [orgname] [username]: accepts role ' + role, function (t) {
52      const membershipData = {
53        org: {
54          name: 'myorg',
55          size: 1
56        },
57        user: 'myuser',
58        role: role
59      }
60      server.put('/-/org/myorg/user', JSON.stringify({
61        user: 'myuser'
62      })).reply(200, membershipData)
63      common.npm([
64        'org', name, 'myorg', 'myuser',
65        '--json',
66        '--registry', common.registry,
67        '--loglevel', 'silent'
68      ], {}, function (err, code, stdout, stderr) {
69        t.ifError(err, 'npm org')
70
71        t.equal(code, 0, 'exited OK')
72        t.equal(stderr, '', 'no error output')
73
74        t.same(JSON.parse(stdout), membershipData)
75        t.end()
76      })
77    })
78  })
79})
80
81test('org rm [orgname] [username]', function (t) {
82  const membershipData = {
83    otheruser: 'admin'
84  }
85  server.delete('/-/org/myorg/user', JSON.stringify({
86    user: 'myuser'
87  })).reply(204, {})
88  server.get('/-/org/myorg/user')
89    .reply(200, membershipData)
90  common.npm([
91    'org', 'rm', 'myorg', 'myuser',
92    '--json',
93    '--registry', common.registry,
94    '--loglevel', 'silent'
95  ], {}, function (err, code, stdout, stderr) {
96    t.ifError(err, 'npm org')
97
98    t.equal(code, 0, 'exited OK')
99    t.equal(stderr, '', 'no error output')
100    t.deepEqual(JSON.parse(stdout), {
101      user: 'myuser',
102      org: 'myorg',
103      deleted: true,
104      userCount: 1
105    }, 'got useful info')
106    t.end()
107  })
108})
109
110test('org ls [orgname]', function (t) {
111  const membershipData = {
112    username: 'admin',
113    username2: 'foo'
114  }
115  server.get('/-/org/myorg/user')
116    .reply(200, membershipData)
117  common.npm([
118    'org', 'ls', 'myorg',
119    '--json',
120    '--registry', common.registry,
121    '--loglevel', 'silent'
122  ], {}, function (err, code, stdout, stderr) {
123    t.ifError(err, 'npm org')
124    t.equal(code, 0, 'exited OK')
125    t.equal(stderr, '', 'no error output')
126    t.same(JSON.parse(stdout), membershipData, 'outputs members')
127    t.end()
128  })
129})
130
131test('cleanup', function (t) {
132  t.pass('cleaned up')
133  server.done()
134  server.close()
135  t.end()
136})
137