Skip to content

Commit

Permalink
chore: move tests to use mock registry
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar authored and lukekarrys committed Dec 7, 2022
1 parent 280b7a4 commit 4a1ebeb
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 88 deletions.
6 changes: 4 additions & 2 deletions DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ graph LR;
libnpmaccess-->npm-package-arg;
libnpmaccess-->npm-registry-fetch;
libnpmaccess-->npmcli-eslint-config["@npmcli/eslint-config"];
libnpmaccess-->npmcli-mock-registry["@npmcli/mock-registry"];
libnpmaccess-->npmcli-template-oss["@npmcli/template-oss"];
libnpmdiff-->npm-package-arg;
libnpmdiff-->npmcli-arborist["@npmcli/arborist"];
Expand Down Expand Up @@ -336,6 +337,7 @@ graph LR;
libnpmaccess-->npm-package-arg;
libnpmaccess-->npm-registry-fetch;
libnpmaccess-->npmcli-eslint-config["@npmcli/eslint-config"];
libnpmaccess-->npmcli-mock-registry["@npmcli/mock-registry"];
libnpmaccess-->npmcli-template-oss["@npmcli/template-oss"];
libnpmaccess-->tap;
libnpmdiff-->binary-extensions;
Expand Down Expand Up @@ -776,11 +778,11 @@ Each group depends on packages lower down the chain, nothing depends on
packages higher up the chain.

- npm
- @npmcli/smoke-tests, libnpmexec, libnpmpublish
- @npmcli/smoke-tests, libnpmaccess, libnpmexec, libnpmpublish
- @npmcli/mock-registry, libnpmdiff, libnpmfund, libnpmpack
- @npmcli/arborist
- @npmcli/metavuln-calculator
- pacote, libnpmaccess, libnpmhook, libnpmorg, libnpmsearch, libnpmteam, npm-profile
- pacote, libnpmhook, libnpmorg, libnpmsearch, libnpmteam, npm-profile
- npm-registry-fetch, libnpmversion
- @npmcli/git, make-fetch-happen, @npmcli/config, init-package-json
- @npmcli/installed-package-contents, @npmcli/map-workspaces, cacache, npm-pick-manifest, @npmcli/run-script, read-package-json, promzard
Expand Down
22 changes: 13 additions & 9 deletions mock-registry/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,22 @@ class MockRegistry {
}

// team can be a team or a username
getPackages ({ team, packages = {}, times = 1 }) {
if (team.startsWith('@')) {
team = team.slice(1)
}
const [scope, teamName] = team.split(':').map(encodeURIComponent)
getPackages ({ user, team, packages = {}, times = 1, responseCode = 200 }) {
let uri
if (teamName) {
uri = `/-/team/${scope}/${teamName}/package`
if (user) {
uri = `/-/user/${user}/package`
} else {
uri = `/-/org/${scope}/package`
if (team.startsWith('@')) {
team = team.slice(1)
}
const [scope, teamName] = team.split(':').map(encodeURIComponent)
if (teamName) {
uri = `/-/team/${scope}/${teamName}/package`
} else {
uri = `/-/org/${scope}/package`
}
}
this.nock = this.nock.get(uri).times(times).reply(200, packages)
this.nock = this.nock.get(uri).times(times).reply(responseCode, packages)
}

getCollaborators ({ spec, collaborators = {} }) {
Expand Down
1 change: 1 addition & 0 deletions package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -15069,6 +15069,7 @@
},
"devDependencies": {
"@npmcli/eslint-config": "^4.0.0",
"@npmcli/mock-registry": "^1.0.0",
"@npmcli/template-oss": "4.10.0",
"nock": "^13.2.4",
"tap": "^16.0.1"
Expand Down
1 change: 1 addition & 0 deletions workspaces/libnpmaccess/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"devDependencies": {
"@npmcli/eslint-config": "^4.0.0",
"@npmcli/mock-registry": "^1.0.0",
"@npmcli/template-oss": "4.10.0",
"nock": "^13.2.4",
"tap": "^16.0.1"
Expand Down
12 changes: 0 additions & 12 deletions workspaces/libnpmaccess/test/fixtures/tnock.js

This file was deleted.

138 changes: 73 additions & 65 deletions workspaces/libnpmaccess/test/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
'use strict'

const t = require('tap')
const tnock = require('./fixtures/tnock.js')
const MockRegistry = require('@npmcli/mock-registry')

const access = require('../lib/index.js')

const REG = 'http://localhost:1337'
const OPTS = {
registry: REG,
const opts = {
registry: 'http://localhost:1337',
}

const pkg = '@npmcli/libnpmaccess-test'
const team = 'npm:test-team'
const orgUser = 'test-user'
const mockRegistry = (t) => {
return new MockRegistry({
tap: t,
strict: true,
registry: 'http://localhost:1337',
})
}

t.test('getCollaborators', t => {
t.test('success', async t => {
const registry = mockRegistry(t)
const collaborators = {
'npm:myteam': 'write',
'npm:anotherteam': 'read',
'npm:thirdteam': 'special-case',
}
tnock(t, REG).get('/-/package/@npmcli%2ftest-package/collaborators').reply(200, collaborators)
const data = await access.getCollaborators('@npmcli/test-package', OPTS)
t.same(data, collaborators)
registry.getCollaborators({ spec: pkg, collaborators })
await t.resolves(
access.getCollaborators(pkg, opts),
collaborators
)
})

t.test('non registry package', async t => {
await t.rejects(access.getCollaborators('./local', OPTS), /package name only/)
await t.rejects(access.getCollaborators('./local', opts), /package name only/)
})
t.end()
})
Expand All @@ -34,125 +48,119 @@ t.test('getPackages', t => {
'@npmcli/other': 'shrödinger',
}
t.test('team', async t => {
tnock(t, REG).get('/-/team/npm/myteam/package').reply(200, packages)
const data = await access.getPackages('npm:myteam', OPTS)
t.same(data, packages)
const registry = mockRegistry(t)
registry.getPackages({ team, packages })
await t.resolves(access.getPackages(team, opts), packages)
})
t.test('org', async t => {
tnock(t, REG).get('/-/org/npm/package').reply(200, packages)
const data = await access.getPackages('npm', OPTS)
t.same(data, packages)
const registry = mockRegistry(t)
registry.getPackages({ team: 'npm', packages })
await t.resolves(access.getPackages('npm', opts), packages)
})
t.test('user', async t => {
tnock(t, REG).get('/-/org/testuser/package').reply(404, {})
tnock(t, REG).get('/-/user/testuser/package').reply(200, packages)
const data = await access.getPackages('testuser', OPTS)
t.same(data, packages)
const registry = mockRegistry(t)
registry.getPackages({ team: orgUser, responseCode: 404 })
registry.getPackages({ user: orgUser, packages })
await t.resolves(access.getPackages(orgUser, opts), packages)
})
t.test('registry error', async t => {
tnock(t, REG).get('/-/org/npm/package').reply(500, {})
await t.rejects(access.getPackages('npm', OPTS), { code: 'E500' })
const registry = mockRegistry(t)
registry.getPackages({ team: orgUser, responseCode: 500 })
await t.rejects(access.getPackages(orgUser, opts), { code: 'E500' })
})
t.end()
})

t.test('getVisibility', t => {
t.test('success', async t => {
const registry = mockRegistry(t)
const visibility = { public: true }
tnock(t, REG).get('/-/package/@npmcli%2ftest-package/visibility').reply(200, visibility)
const data = await access.getVisibility('@npmcli/test-package', OPTS)
t.same(data, visibility)
registry.getVisibility({ spec: pkg, visibility })
await t.resolves(access.getVisibility(pkg, opts), visibility)
})
t.test('non registry package', async t => {
await t.rejects(access.getVisibility('./local', OPTS), /package name only/)
await t.rejects(access.getVisibility('./local', opts), /package name only/)
})
t.end()
})

t.test('removePermissions', t => {
t.test('success', async t => {
tnock(t, REG).delete('/-/team/npm/myteam/package', {
package: '@npmcli/test-package',
}).reply(200)
await t.resolves(access.removePermissions('npm:myteam', '@npmcli/test-package', OPTS))
const registry = mockRegistry(t)
registry.removePermissions({ spec: pkg, team })
await t.resolves(access.removePermissions(team, pkg, opts))
})
t.test('non registry spec', async t => {
await t.rejects(access.removePermissions('npm:myteam', './local', OPTS), /package name only/)
await t.rejects(access.removePermissions(team, './local', opts), /package name only/)
})
t.end()
})

t.test('setAccess', t => {
t.test('public', async t => {
tnock(t, REG).post(
'/-/package/@npmcli%2ftest-package/access', { access: 'public' }
).reply(200)
await t.resolves(access.setAccess('@npmcli/test-package', 'public', OPTS))
const body = { access: 'public' }
const registry = mockRegistry(t)
registry.setAccess({ spec: pkg, body })
await t.resolves(access.setAccess(pkg, 'public', opts))
})
t.test('restricted', async t => {
tnock(t, REG).post(
'/-/package/@npmcli%2ftest-package/access', { access: 'restricted' }
).reply(200)
await t.resolves(access.setAccess('@npmcli/test-package', 'restricted', OPTS))
const body = { access: 'restricted' }
const registry = mockRegistry(t)
registry.setAccess({ spec: pkg, body })
await t.resolves(access.setAccess(pkg, 'restricted', opts))
})
t.test('non registry package', async t => {
await t.rejects(access.setAccess('./local', 'public', OPTS), /package name only/)
await t.rejects(access.setAccess('./local', 'public', opts), /package name only/)
})
t.end()
})

t.test('setMfa', t => {
t.test('none', async t => {
tnock(t, REG).post('/-/package/@npmcli%2ftest-package/access', {
publish_requires_tfa: false,
}).reply(200)
await t.resolves(access.setMfa('@npmcli/test-package', 'none', OPTS))
const registry = mockRegistry(t)
const body = { publish_requires_tfa: false }
registry.setAccess({ spec: pkg, body })
await t.resolves(access.setMfa(pkg, 'none', opts))
})
t.test('publish', async t => {
tnock(t, REG).post('/-/package/@npmcli%2ftest-package/access', {
const registry = mockRegistry(t)
const body = {
publish_requires_tfa: true,
automation_token_overrides_tfa: false,
}).reply(200)
await t.resolves(access.setMfa('@npmcli/test-package', 'publish', OPTS))
}
registry.setAccess({ spec: pkg, body })
await t.resolves(access.setMfa(pkg, 'publish', opts))
})
t.test('automation', async t => {
tnock(t, REG).post('/-/package/@npmcli%2ftest-package/access', {
const registry = mockRegistry(t)
const body = {
publish_requires_tfa: true,
automation_token_overrides_tfa: true,
}).reply(200)
await t.resolves(access.setMfa('@npmcli/test-package', 'automation', OPTS))
}
registry.setAccess({ spec: pkg, body })
await t.resolves(access.setMfa(pkg, 'automation', opts))
})
t.test('invalid', async t => {
await t.rejects(access.setMfa('@npmcli/test-package', 'invalid', OPTS), /Invalid mfa setting/)
await t.rejects(access.setMfa(pkg, 'invalid', opts), /Invalid mfa setting/)
})
t.test('non registry spec', async t => {
await t.rejects(access.setMfa('./local', 'none', OPTS, /package name only/))
await t.rejects(access.setMfa('./local', 'none', opts, /package name only/))
})
t.end()
})

t.test('setPermissions', t => {
t.test('scope:team read-only', async t => {
tnock(t, REG).put('/-/team/npmcli/myteam/package', {
package: '@npmcli/test-package',
permissions: 'read-only',
}).reply(201)
await t.resolves(
access.setPermissions('npmcli:myteam', '@npmcli/test-package', 'read-only', OPTS)
)
const registry = mockRegistry(t)
registry.setPermissions({ spec: pkg, team, permissions: 'read-only' })
await t.resolves(access.setPermissions(team, pkg, 'read-only', opts))
})
t.test('scope only', async t => {
await t.rejects(
access.setPermissions('npmcli', '@npmcli/test-package', 'read-only', OPTS),
/scope:team/
)
await t.rejects(access.setPermissions('npmcli', pkg, 'read-only', opts), /scope:team/)
})

t.test('no scope or team', async t => {
await t.rejects(
access.setPermissions('@:myteam', '@npmcli/test-package', 'read-only', OPTS),
/scope:team/
)
await t.rejects(access.setPermissions('@:myteam', pkg, 'read-only', opts), /scope:team/)
})

t.end()
Expand Down

0 comments on commit 4a1ebeb

Please sign in to comment.