2 var path = require("path")
3 var rimraf = require("rimraf")
4 var mr = require("npm-registry-mock")
6 var test = require("tap").test
7 var common = require("../common-tap.js")
9 var opts = {cwd : __dirname}
10 var outfile = path.resolve(__dirname, "_npmrc")
17 function verifyStdout (runner, successMessage, t) {
18 var remaining = Object.keys(responses).length
19 return function (chunk) {
23 var label = chunk.toString('utf8').split(':')[0]
24 runner.stdin.write(responses[label])
26 if (remaining === 0) runner.stdin.end()
28 var message = chunk.toString('utf8').trim()
29 t.equal(message, successMessage)
34 function mocks (server) {
35 server.filteringRequestBody(function (r) {
36 if (r.match(/\"_id\":\"org\.couchdb\.user:u\"/)) {
40 server.put("/-/user/org.couchdb.user:u", "auth")
41 .reply(201, {username : "u", password : "p", email : "u@p.me"})
44 test("npm login", function (t) {
45 mr({port : common.port, plugin : mocks}, function (er, s) {
46 var runner = common.npm(
49 "--registry", common.registry,
50 "--loglevel", "silent",
51 "--userconfig", outfile
54 function (err, code) {
55 t.notOk(code, "exited OK")
56 t.notOk(err, "no error output")
57 var config = fs.readFileSync(outfile, "utf8")
58 t.like(config, /:always-auth=false/, "always-auth is scoped and false (by default)")
60 rimraf(outfile, function (err) {
61 t.ifError(err, "removed config file OK")
66 var message = 'Logged in as u on ' + common.registry + '/.'
67 runner.stdout.on('data', verifyStdout(runner, message, t))
71 test('npm login --scope <scope> uses <scope>:registry as its URI', function (t) {
72 var port = common.port + 1
73 var uri = 'http://localhost:' + port + '/'
78 '--userconfig', outfile,
84 function (err, code) {
85 t.notOk(code, 'exited OK')
86 t.notOk(err, 'no error output')
88 mr({ port: port, plugin: mocks }, function (er, s) {
89 var runner = common.npm(
92 '--loglevel', 'silent',
93 '--userconfig', outfile,
97 function (err, code) {
98 t.notOk(code, 'exited OK')
99 t.notOk(err, 'no error output')
100 var config = fs.readFileSync(outfile, 'utf8')
101 t.like(config, new RegExp(scope + ':registry=' + uri), 'scope:registry is set')
103 rimraf(outfile, function (err) {
104 t.ifError(err, 'removed config file OK')
109 var message = 'Logged in as u to scope ' + scope + ' on ' + uri + '.'
110 runner.stdout.on('data', verifyStdout(runner, message, t))
115 test('npm login --scope <scope> makes sure <scope> is prefixed by an @', function (t) {
116 var port = common.port + 1
117 var uri = 'http://localhost:' + port + '/'
119 var prefixedScope = '@' + scope
122 '--userconfig', outfile,
125 prefixedScope + ':registry',
129 function (err, code) {
130 t.notOk(code, 'exited OK')
131 t.notOk(err, 'no error output')
133 mr({ port: port, plugin: mocks }, function (er, s) {
134 var runner = common.npm(
137 '--loglevel', 'silent',
138 '--userconfig', outfile,
142 function (err, code) {
143 t.notOk(code, 'exited OK')
144 t.notOk(err, 'no error output')
145 var config = fs.readFileSync(outfile, 'utf8')
146 t.like(config, new RegExp(prefixedScope + ':registry=' + uri), 'scope:registry is set')
148 rimraf(outfile, function (err) {
149 t.ifError(err, 'removed config file OK')
154 var message = 'Logged in as u to scope ' + prefixedScope + ' on ' + uri + '.'
155 runner.stdout.on('data', verifyStdout(runner, message, t))
160 test('npm login --scope <scope> --registry <registry> uses <registry> as its URI', function (t) {
164 '--userconfig', outfile,
171 function (err, code) {
172 t.notOk(code, 'exited OK')
173 t.notOk(err, 'no error output')
175 mr({ port: common.port, plugin: mocks }, function (er, s) {
176 var runner = common.npm(
179 '--registry', common.registry,
180 '--loglevel', 'silent',
181 '--userconfig', outfile,
185 function (err, code) {
186 t.notOk(code, 'exited OK')
187 t.notOk(err, 'no error output')
188 var config = fs.readFileSync(outfile, 'utf8')
189 t.like(config, new RegExp(scope + ':registry=' + common.registry), 'scope:registry is set')
191 rimraf(outfile, function (err) {
192 t.ifError(err, 'removed config file OK')
197 var message = 'Logged in as u to scope ' + scope + ' on ' + common.registry + '/.'
198 runner.stdout.on('data', verifyStdout(runner, message, t))
203 test("npm login --always-auth", function (t) {
204 mr({port : common.port, plugin : mocks}, function (er, s) {
205 var runner = common.npm(
208 "--registry", common.registry,
209 "--loglevel", "silent",
210 "--userconfig", outfile,
214 function (err, code) {
215 t.notOk(code, "exited OK")
216 t.notOk(err, "no error output")
217 var config = fs.readFileSync(outfile, "utf8")
218 t.like(config, /:always-auth=true/, "always-auth is scoped and true")
220 rimraf(outfile, function (err) {
221 t.ifError(err, "removed config file OK")
226 var message = 'Logged in as u on ' + common.registry + '/.'
227 runner.stdout.on('data', verifyStdout(runner, message, t))
231 test("npm login --no-always-auth", function (t) {
232 mr({port : common.port, plugin : mocks}, function (er, s) {
233 var runner = common.npm(
236 "--registry", common.registry,
237 "--loglevel", "silent",
238 "--userconfig", outfile,
242 function (err, code) {
243 t.notOk(code, "exited OK")
244 t.notOk(err, "no error output")
245 var config = fs.readFileSync(outfile, "utf8")
246 t.like(config, /:always-auth=false/, "always-auth is scoped and false")
248 rimraf(outfile, function (err) {
249 t.ifError(err, "removed config file OK")
254 var message = 'Logged in as u on ' + common.registry + '/.'
255 runner.stdout.on('data', verifyStdout(runner, message, t))
260 test("cleanup", function (t) {