From 3d26dab4ed7c5ce15cc45a5ca479affc84612b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Wed, 5 Aug 2015 18:29:10 -0700 Subject: test: wrote full tests for npm access PR-URL: https://github.com/npm/npm/pull/9011 --- test/tap/access.js | 315 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 275 insertions(+), 40 deletions(-) (limited to 'test') diff --git a/test/tap/access.js b/test/tap/access.js index 82a5b79ea..5e13a23cd 100644 --- a/test/tap/access.js +++ b/test/tap/access.js @@ -15,22 +15,12 @@ var scoped = { version: '1.1.1' } -var body = { - access: 'public' -} - -function mocks (server) { - server.post('/-/package/@scoped%2fpkg/access', JSON.stringify(body)) - .reply(200, { 'access': 'public' }) - server.post('/-/package/@scoped%2fanother/access', JSON.stringify(body)) - .reply(200, { 'access': 'public' }) -} - test('setup', function (t) { mkdirp(pkg, function (er) { t.ifError(er, pkg + ' made successfully') - mr({port: common.port, plugin: mocks}, function (err, s) { + mr({port: common.port}, function (err, s) { + t.ifError(err, 'registry mocked successfully') server = s fs.writeFile( @@ -45,103 +35,347 @@ test('setup', function (t) { }) }) -test('npm access on current package', function (t) { +test('npm access public on current package', function (t) { + server.post('/-/package/%40scoped%2Fpkg/access', JSON.stringify({ + access: 'public' + })).reply(200, { + accessChanged: true + }) + common.npm([ + 'access', + 'public', + '--registry', common.registry, + '--loglevel', 'silent' + ], { + cwd: pkg + }, + function (er, code, stdout, stderr) { + t.ifError(er, 'npm access') + t.equal(code, 0, 'exited OK') + t.equal(stderr, '', 'no error output') + t.end() + }) +}) + +test('npm access restricted on current package', function (t) { + server.post('/-/package/%40scoped%2Fpkg/access', JSON.stringify({ + access: 'restricted' + })).reply(200, { + accessChanged: true + }) + common.npm([ + 'access', + 'restricted', + '--registry', common.registry, + '--loglevel', 'silent' + ], { + cwd: pkg + }, + function (er, code, stdout, stderr) { + t.ifError(er, 'npm access') + t.equal(code, 0, 'exited OK') + t.equal(stderr, '', 'no error output') + t.end() + }) +}) + +test('npm access on named package', function (t) { + server.post('/-/package/%40scoped%2Fanother/access', { + access: 'public' + }).reply(200, { + accessChaged: true + }) common.npm( [ 'access', - 'public', + 'public', '@scoped/another', '--registry', common.registry, '--loglevel', 'silent' ], { cwd: pkg }, function (er, code, stdout, stderr) { t.ifError(er, 'npm access') - t.notOk(code, 'exited OK') - t.notOk(stderr, 'no error output') + t.equal(code, 0, 'exited OK') + t.equal(stderr, '', 'no error output') t.end() } ) }) -test('npm access on named package', function (t) { +test('npm change access on unscoped package', function (t) { common.npm( [ 'access', - 'public', '@scoped/another', - '--registry', common.registry, - '--loglevel', 'silent' + 'restricted', 'yargs', + '--registry', common.registry ], { cwd: pkg }, function (er, code, stdout, stderr) { - t.ifError(er, 'npm access') - t.notOk(code, 'exited OK') - t.notOk(stderr, 'no error output') + t.ok(code, 'exited with Error') + t.matches( + stderr, /access commands are only accessible for scoped packages/) + t.end() + } + ) +}) +test('npm access grant read-only', function (t) { + server.put('/-/team/myorg/myteam/package', { + permissions: 'read-only', + package: '@scoped/another' + }).reply(201, { + accessChaged: true + }) + common.npm( + [ + 'access', + 'grant', 'read-only', + 'myorg:myteam', + '@scoped/another', + '--registry', common.registry + ], + { cwd: pkg }, + function (er, code, stdout, stderr) { + t.ifError(er, 'npm access grant') + t.equal(code, 0, 'exited with Error') t.end() } ) }) -test('npm change access on unscoped package', function (t) { +test('npm access grant read-write', function (t) { + server.put('/-/team/myorg/myteam/package', { + permissions: 'read-write', + package: '@scoped/another' + }).reply(201, { + accessChaged: true + }) common.npm( [ 'access', - 'restricted', 'yargs', + 'grant', 'read-write', + 'myorg:myteam', + '@scoped/another', '--registry', common.registry ], { cwd: pkg }, function (er, code, stdout, stderr) { - t.ok(code, 'exited with Error') - t.ok(stderr.match(/you can't change the access level of unscoped packages/)) + t.ifError(er, 'npm access grant') + t.equal(code, 0, 'exited with Error') t.end() } ) }) -test('npm access add', function (t) { +test('npm access grant others', function (t) { common.npm( [ 'access', - 'add', '@scoped/another', + 'grant', 'rerere', + 'myorg:myteam', + '@scoped/another', '--registry', common.registry ], { cwd: pkg }, function (er, code, stdout, stderr) { t.ok(code, 'exited with Error') - t.ok(stderr.match(/npm access add isn't implemented yet!/)) + t.matches(stderr, /read-only/) + t.matches(stderr, /read-write/) t.end() } ) }) -test('npm access rm', function (t) { +test('npm access revoke', function (t) { + server.delete('/-/team/myorg/myteam/package', { + package: '@scoped/another' + }).reply(200, { + accessChaged: true + }) common.npm( [ 'access', - 'rm', '@scoped/another', + 'revoke', + 'myorg:myteam', + '@scoped/another', '--registry', common.registry ], { cwd: pkg }, function (er, code, stdout, stderr) { - t.ok(code, 'exited with Error') - t.ok(stderr.match(/npm access rm isn't implemented yet!/)) + t.ifError(er, 'npm access grant') + t.equal(code, 0, 'exited with Error') t.end() } ) }) -test('npm access ls', function (t) { +test('npm access ls-packages on team', function (t) { + var serverPackages = { + '@foo/bar': 'write', + '@foo/util': 'read' + } + var clientPackages = { + '@foo/bar': 'read-write', + '@foo/util': 'read-only' + } + server.get( + '/-/team/myorg/myteam/package?format=cli' + ).reply(200, serverPackages) common.npm( [ 'access', - 'ls', '@scoped/another', + 'ls-packages', + 'myorg:myteam', '--registry', common.registry ], { cwd: pkg }, function (er, code, stdout, stderr) { - t.ok(code, 'exited with Error') - t.ok(stderr.match(/npm access ls isn't implemented yet!/)) + t.ifError(er, 'npm access ls-packages') + t.same(JSON.parse(stdout), clientPackages) + t.end() + } + ) +}) + +test('npm access ls-packages on org', function (t) { + var serverPackages = { + '@foo/bar': 'write', + '@foo/util': 'read' + } + var clientPackages = { + '@foo/bar': 'read-write', + '@foo/util': 'read-only' + } + server.get( + '/-/org/myorg/package?format=cli' + ).reply(200, serverPackages) + common.npm( + [ + 'access', + 'ls-packages', + 'myorg', + '--registry', common.registry + ], + { cwd: pkg }, + function (er, code, stdout, stderr) { + t.ifError(er, 'npm access ls-packages') + t.same(JSON.parse(stdout), clientPackages) + t.end() + } + ) +}) + +test('npm access ls-packages on user', function (t) { + var serverPackages = { + '@foo/bar': 'write', + '@foo/util': 'read' + } + var clientPackages = { + '@foo/bar': 'read-write', + '@foo/util': 'read-only' + } + server.get( + '/-/org/myorg/package?format=cli' + ).reply(404, {error: 'nope'}) + server.get( + '/-/user/myorg/package?format=cli' + ).reply(200, serverPackages) + common.npm( + [ + 'access', + 'ls-packages', + 'myorg', + '--registry', common.registry + ], + { cwd: pkg }, + function (er, code, stdout, stderr) { + t.ifError(er, 'npm access ls-packages') + t.same(JSON.parse(stdout), clientPackages) + t.end() + } + ) +}) + +test('npm access ls-collaborators on current', function (t) { + var serverCollaborators = { + 'myorg:myteam': 'write', + 'myorg:anotherteam': 'read' + } + var clientCollaborators = { + 'myorg:myteam': 'read-write', + 'myorg:anotherteam': 'read-only' + } + server.get( + '/-/package/%40scoped%2Fpkg/collaborators?format=cli' + ).reply(200, serverCollaborators) + common.npm( + [ + 'access', + 'ls-collaborators', + '--registry', common.registry + ], + { cwd: pkg }, + function (er, code, stdout, stderr) { + t.ifError(er, 'npm access ls-collaborators') + t.same(JSON.parse(stdout), clientCollaborators) + t.end() + } + ) +}) + +test('npm access ls-collaborators on package', function (t) { + var serverCollaborators = { + 'myorg:myteam': 'write', + 'myorg:anotherteam': 'read' + } + var clientCollaborators = { + 'myorg:myteam': 'read-write', + 'myorg:anotherteam': 'read-only' + } + server.get( + '/-/package/%40scoped%2Fanother/collaborators?format=cli' + ).reply(200, serverCollaborators) + common.npm( + [ + 'access', + 'ls-collaborators', + '@scoped/another', + '--registry', common.registry + ], + { cwd: pkg }, + function (er, code, stdout, stderr) { + t.ifError(er, 'npm access ls-collaborators') + t.same(JSON.parse(stdout), clientCollaborators) + t.end() + } + ) +}) + +test('npm access ls-collaborators on current w/user filter', function (t) { + var serverCollaborators = { + 'myorg:myteam': 'write', + 'myorg:anotherteam': 'read' + } + var clientCollaborators = { + 'myorg:myteam': 'read-write', + 'myorg:anotherteam': 'read-only' + } + server.get( + '/-/package/%40scoped%2Fanother/collaborators?format=cli&user=zkat' + ).reply(200, serverCollaborators) + common.npm( + [ + 'access', + 'ls-collaborators', + '@scoped/another', + 'zkat', + '--registry', common.registry + ], + { cwd: pkg }, + function (er, code, stdout, stderr) { + t.ifError(er, 'npm access ls-collaborators') + t.same(JSON.parse(stdout), clientCollaborators) t.end() } ) @@ -157,7 +391,7 @@ test('npm access edit', function (t) { { cwd: pkg }, function (er, code, stdout, stderr) { t.ok(code, 'exited with Error') - t.ok(stderr.match(/npm access edit isn't implemented yet!/)) + t.match(stderr, /edit subcommand is not implemented yet/) t.end() } ) @@ -173,7 +407,7 @@ test('npm access blerg', function (t) { { cwd: pkg }, function (er, code, stdout, stderr) { t.ok(code, 'exited with Error') - t.ok(stderr.match(/Usage:/)) + t.matches(stderr, /Usage:/) t.end() } ) @@ -182,6 +416,7 @@ test('npm access blerg', function (t) { test('cleanup', function (t) { t.pass('cleaned up') rimraf.sync(pkg) + server.done() server.close() t.end() }) -- cgit v1.2.3