]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/test/tap/adduser-always-auth.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / test / tap / adduser-always-auth.js
1 var fs = require("fs")
2 var path = require("path")
3 var rimraf = require("rimraf")
4 var mr = require("npm-registry-mock")
5
6 var test = require("tap").test
7 var common = require("../common-tap.js")
8
9 var opts = {cwd : __dirname}
10 var outfile = path.resolve(__dirname, "_npmrc")
11 var responses = {
12   "Username" : "u\n",
13   "Password" : "p\n",
14   "Email"    : "u@p.me\n"
15 }
16
17 function verifyStdout (runner, successMessage, t) {
18   var remaining = Object.keys(responses).length
19   return function (chunk) {
20     if (remaining > 0) {
21       remaining--
22
23       var label = chunk.toString('utf8').split(':')[0]
24       runner.stdin.write(responses[label])
25
26       if (remaining === 0) runner.stdin.end()
27     } else {
28       var message = chunk.toString('utf8').trim()
29       t.equal(message, successMessage)
30     }
31   }
32 }
33
34 function mocks (server) {
35   server.filteringRequestBody(function (r) {
36     if (r.match(/\"_id\":\"org\.couchdb\.user:u\"/)) {
37       return "auth"
38     }
39   })
40   server.put("/-/user/org.couchdb.user:u", "auth")
41     .reply(201, {username : "u", password : "p", email : "u@p.me"})
42 }
43
44 test("npm login", function (t) {
45   mr({port : common.port, plugin : mocks}, function (er, s) {
46     var runner = common.npm(
47     [
48       "login",
49       "--registry", common.registry,
50       "--loglevel", "silent",
51       "--userconfig", outfile
52     ],
53     opts,
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)")
59       s.close()
60       rimraf(outfile, function (err) {
61         t.ifError(err, "removed config file OK")
62         t.end()
63       })
64     })
65
66     var message = 'Logged in as u on ' + common.registry + '/.'
67     runner.stdout.on('data', verifyStdout(runner, message, t))
68   })
69 })
70
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 + '/'
74   var scope = '@myco'
75   common.npm(
76     [
77       'config',
78       '--userconfig', outfile,
79       'set',
80       scope + ':registry',
81       uri
82     ],
83   opts,
84   function (err, code) {
85     t.notOk(code, 'exited OK')
86     t.notOk(err, 'no error output')
87
88     mr({ port: port, plugin: mocks }, function (er, s) {
89       var runner = common.npm(
90         [
91           'login',
92           '--loglevel', 'silent',
93           '--userconfig', outfile,
94           '--scope', scope
95         ],
96       opts,
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')
102         s.close()
103         rimraf(outfile, function (err) {
104           t.ifError(err, 'removed config file OK')
105           t.end()
106         })
107       })
108
109       var message = 'Logged in as u to scope ' + scope + ' on ' + uri + '.'
110       runner.stdout.on('data', verifyStdout(runner, message, t))
111     })
112   })
113 })
114
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 + '/'
118   var scope = 'myco'
119   var prefixedScope = '@' + scope
120   common.npm(
121     [
122       '--userconfig', outfile,
123       'config',
124       'set',
125       prefixedScope + ':registry',
126       uri
127     ],
128   opts,
129   function (err, code) {
130     t.notOk(code, 'exited OK')
131     t.notOk(err, 'no error output')
132
133     mr({ port: port, plugin: mocks }, function (er, s) {
134       var runner = common.npm(
135         [
136           'login',
137           '--loglevel', 'silent',
138           '--userconfig', outfile,
139           '--scope', scope
140         ],
141       opts,
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')
147         s.close()
148         rimraf(outfile, function (err) {
149           t.ifError(err, 'removed config file OK')
150           t.end()
151         })
152       })
153
154       var message = 'Logged in as u to scope ' + prefixedScope + ' on ' + uri + '.'
155       runner.stdout.on('data', verifyStdout(runner, message, t))
156     })
157   })
158 })
159
160 test('npm login --scope <scope> --registry <registry> uses <registry> as its URI', function (t) {
161   var scope = '@myco'
162   common.npm(
163     [
164       '--userconfig', outfile,
165       'config',
166       'set',
167       scope + ':registry',
168       'invalidurl'
169     ],
170   opts,
171   function (err, code) {
172     t.notOk(code, 'exited OK')
173     t.notOk(err, 'no error output')
174
175     mr({ port: common.port, plugin: mocks }, function (er, s) {
176       var runner = common.npm(
177         [
178           'login',
179           '--registry', common.registry,
180           '--loglevel', 'silent',
181           '--userconfig', outfile,
182           '--scope', scope
183         ],
184       opts,
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')
190         s.close()
191         rimraf(outfile, function (err) {
192           t.ifError(err, 'removed config file OK')
193           t.end()
194         })
195       })
196
197       var message = 'Logged in as u to scope ' + scope + ' on ' + common.registry + '/.'
198       runner.stdout.on('data', verifyStdout(runner, message, t))
199     })
200   })
201 })
202
203 test("npm login --always-auth", function (t) {
204   mr({port : common.port, plugin : mocks}, function (er, s) {
205     var runner = common.npm(
206     [
207       "login",
208       "--registry", common.registry,
209       "--loglevel", "silent",
210       "--userconfig", outfile,
211       "--always-auth"
212     ],
213     opts,
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")
219       s.close()
220       rimraf(outfile, function (err) {
221         t.ifError(err, "removed config file OK")
222         t.end()
223       })
224     })
225
226     var message = 'Logged in as u on ' + common.registry + '/.'
227     runner.stdout.on('data', verifyStdout(runner, message, t))
228   })
229 })
230
231 test("npm login --no-always-auth", function (t) {
232   mr({port : common.port, plugin : mocks}, function (er, s) {
233     var runner = common.npm(
234     [
235       "login",
236       "--registry", common.registry,
237       "--loglevel", "silent",
238       "--userconfig", outfile,
239       "--no-always-auth"
240     ],
241     opts,
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")
247       s.close()
248       rimraf(outfile, function (err) {
249         t.ifError(err, "removed config file OK")
250         t.end()
251       })
252     })
253
254     var message = 'Logged in as u on ' + common.registry + '/.'
255     runner.stdout.on('data', verifyStdout(runner, message, t))
256   })
257 })
258
259
260 test("cleanup", function (t) {
261   rimraf.sync(outfile)
262   t.pass("cleaned up")
263   t.end()
264 })