Welcome to mirror list, hosted at ThFree Co, Russian Federation.

adduser.js « commands « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 04fe8dc8c3147420a3c44fdeede78fa2a8d507be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const t = require('tap')
const fs = require('fs')
const path = require('path')
const ini = require('ini')

const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
const mockGlobals = require('../../fixtures/mock-globals.js')
const MockRegistry = require('../../fixtures/mock-registry.js')
const stream = require('stream')

t.test('usage', async t => {
  const { npm } = await loadMockNpm(t)
  const adduser = await npm.cmd('adduser')
  t.match(adduser.usage, 'adduser', 'usage has command name in it')
})

t.test('legacy', async t => {
  t.test('simple adduser', async t => {
    const stdin = new stream.PassThrough()
    stdin.write('test-user\n')
    stdin.write('test-password\n')
    stdin.write('test-email@npmjs.org\n')
    mockGlobals(t, {
      'process.stdin': stdin,
      'process.stdout': new stream.PassThrough(), // to quiet readline
    }, { replace: true })
    const { npm, home } = await loadMockNpm(t, {
      config: { 'auth-type': 'legacy' },
      homeDir: {
        // These all get cleaned up by config.setCredentialsByURI
        '.npmrc': [
          '_token=user',
          '_password=user',
          'username=user',
          '_auth=user',
          '_authtoken=user',
          '-authtoken=user',
          '_authToken=user',
          '//registry.npmjs.org/:_authToken=user',
          '//registry.npmjs.org/:always-auth=user',
          '//registry.npmjs.org/:email=test-email-old@npmjs.org',
        ].join('\n'),
      },
    })
    const registry = new MockRegistry({
      tap: t,
      registry: npm.config.get('registry'),
    })
    registry.couchadduser({
      username: 'test-user',
      password: 'test-password',
      email: 'test-email@npmjs.org',
      token: 'npm_test-token',
    })
    await npm.exec('adduser', [])
    t.same(npm.config.get('email'), 'test-email-old@npmjs.org')
    t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token')
    const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8'))
    t.same(rc, {
      '//registry.npmjs.org/:_authToken': 'npm_test-token',
      email: 'test-email-old@npmjs.org',
    }, 'should only have token and un-nerfed old email')
  })

  t.test('scoped adduser', async t => {
    const stdin = new stream.PassThrough()
    stdin.write('test-user\n')
    stdin.write('test-password\n')
    stdin.write('test-email@npmjs.org\n')
    mockGlobals(t, {
      'process.stdin': stdin,
      'process.stdout': new stream.PassThrough(), // to quiet readline
    }, { replace: true })
    const { npm, home } = await loadMockNpm(t, {
      config: {
        'auth-type': 'legacy',
        scope: '@myscope',
      },
    })
    const registry = new MockRegistry({
      tap: t,
      registry: npm.config.get('registry'),
    })
    registry.couchadduser({
      username: 'test-user',
      password: 'test-password',
      email: 'test-email@npmjs.org',
      token: 'npm_test-token',
    })
    await npm.exec('adduser', [])
    t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token')
    t.same(npm.config.get('@myscope:registry'), 'https://registry.npmjs.org/')
    const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8'))
    t.same(rc, {
      '//registry.npmjs.org/:_authToken': 'npm_test-token',
      '@myscope:registry': 'https://registry.npmjs.org/',
    }, 'should only have token and scope:registry')
  })

  t.test('scoped adduser with valid scoped registry config', async t => {
    const stdin = new stream.PassThrough()
    stdin.write('test-user\n')
    stdin.write('test-password\n')
    stdin.write('test-email@npmjs.org\n')
    mockGlobals(t, {
      'process.stdin': stdin,
      'process.stdout': new stream.PassThrough(), // to quiet readline
    }, { replace: true })
    const { npm, home } = await loadMockNpm(t, {
      homeDir: {
        '.npmrc': '@myscope:registry=https://diff-registry.npmjs.org',
      },
      config: {
        'auth-type': 'legacy',
        scope: '@myscope',
      },
    })
    const registry = new MockRegistry({
      tap: t,
      registry: 'https://diff-registry.npmjs.org',
    })
    registry.couchadduser({
      username: 'test-user',
      password: 'test-password',
      email: 'test-email@npmjs.org',
      token: 'npm_test-token',
    })
    await npm.exec('adduser', [])
    t.same(npm.config.get('//diff-registry.npmjs.org/:_authToken'), 'npm_test-token')
    t.same(npm.config.get('@myscope:registry'), 'https://diff-registry.npmjs.org')
    const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8'))
    t.same(rc, {
      '@myscope:registry': 'https://diff-registry.npmjs.org',
      '//diff-registry.npmjs.org/:_authToken': 'npm_test-token',
    }, 'should only have token and scope:registry')
  })

  t.test('save config failure', async t => {
    const stdin = new stream.PassThrough()
    stdin.write('test-user\n')
    stdin.write('test-password\n')
    stdin.write('test-email@npmjs.org\n')
    mockGlobals(t, {
      'process.stdin': stdin,
      'process.stdout': new stream.PassThrough(), // to quiet readline
    }, { replace: true })
    const { npm } = await loadMockNpm(t, {
      config: { 'auth-type': 'legacy' },
      homeDir: {
        '.npmrc': {},
      },
    })
    const registry = new MockRegistry({
      tap: t,
      registry: npm.config.get('registry'),
    })
    registry.couchadduser({
      username: 'test-user',
      password: 'test-password',
      email: 'test-email@npmjs.org',
      token: 'npm_test-token',
    })
    await t.rejects(npm.exec('adduser', []))
  })
  t.end()
})

t.test('web', t => {
  t.test('basic adduser', async t => {
    const { npm, home } = await loadMockNpm(t, {
      config: { 'auth-type': 'web' },
    })
    const registry = new MockRegistry({
      tap: t,
      registry: npm.config.get('registry'),
    })
    registry.webadduser({ token: 'npm_test-token' })
    await npm.exec('adduser', [])
    t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token')
    const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8'))
    t.same(rc, {
      '//registry.npmjs.org/:_authToken': 'npm_test-token',
    })
  })
  t.end()
})