Skip to content

Commit

Permalink
chore: fix adduser tests
Browse files Browse the repository at this point in the history
They mock real registry calls now
  • Loading branch information
wraithgar authored and fritzy committed Apr 13, 2022
1 parent 98377d1 commit 71ab00a
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 171 deletions.
18 changes: 17 additions & 1 deletion test/fixtures/mock-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const LoadMockNpm = async (t, {
init = true,
load = init,
prefixDir = {},
homeDir = {},
cacheDir = {},
globalPrefixDir = {},
config = {},
Expand All @@ -70,13 +71,20 @@ const LoadMockNpm = async (t, {
// Mock some globals with their original values so they get torn down
// back to the original at the end of the test since they are manipulated
// by npm itself
const npmConfigEnv = {}
for (const key in process.env) {
if (key.startsWith('npm_config_')) {
npmConfigEnv[key] = undefined
}
}
mockGlobals(t, {
process: {
title: process.title,
execPath: process.execPath,
env: {
npm_command: process.env.npm_command,
COLOR: process.env.COLOR,
...npmConfigEnv,
},
},
})
Expand All @@ -94,14 +102,21 @@ const LoadMockNpm = async (t, {
// Set log level as early as possible since
setLoglevel(t, config.loglevel)

const dir = t.testdir({ prefix: prefixDir, cache: cacheDir, global: globalPrefixDir })
const dir = t.testdir({
home: homeDir,
prefix: prefixDir,
cache: cacheDir,
global: globalPrefixDir,
})
const prefix = path.join(dir, 'prefix')
const cache = path.join(dir, 'cache')
const globalPrefix = path.join(dir, 'global')
const home = path.join(dir, 'home')

// Set cache to testdir via env var so it is available when load is run
// XXX: remove this for a solution where cache argv is passed in
mockGlobals(t, {
'process.env.HOME': home,
'process.env.npm_config_cache': cache,
...(globals ? result(globals, { prefix, cache }) : {}),
// Some configs don't work because they can't be set via npm.config.set until
Expand Down Expand Up @@ -140,6 +155,7 @@ const LoadMockNpm = async (t, {
...rest,
Npm,
npm,
home,
prefix,
globalPrefix,
testdir: dir,
Expand Down
39 changes: 33 additions & 6 deletions test/fixtures/mock-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MockRegistry {
}

whoami ({ username }) {
this.nock.get('/-/whoami').reply(200, { username })
this.nock = this.nock.get('/-/whoami').reply(200, { username })
}

access ({ spec, access, publishRequires2fa }) {
Expand All @@ -52,7 +52,7 @@ class MockRegistry {
if (publishRequires2fa !== undefined) {
body.publish_requires_tfa = publishRequires2fa
}
this.nock.post(
this.nock = this.nock.post(
`/-/package/${encodeURIComponent(spec)}/access`,
body
).reply(200)
Expand All @@ -63,7 +63,7 @@ class MockRegistry {
team = team.slice(1)
}
const [scope, teamName] = team.split(':')
this.nock.put(
this.nock = this.nock.put(
`/-/team/${encodeURIComponent(scope)}/${encodeURIComponent(teamName)}/package`,
{ package: spec, permissions }
).reply(200)
Expand All @@ -74,12 +74,39 @@ class MockRegistry {
team = team.slice(1)
}
const [scope, teamName] = team.split(':')
this.nock.delete(
this.nock = this.nock.delete(
`/-/team/${encodeURIComponent(scope)}/${encodeURIComponent(teamName)}/package`,
{ package: spec }
).reply(200)
}

couchlogin ({ username, password, email, otp, token = 'npm_default-test-token' }) {
this.nock = this.nock
.post('/-/v1/login').reply(401, { error: 'You must be logged in to publish packages.' })
if (otp) {
// TODO otp failure results in a 401 with
// {"ok":false,"error":"failed to authenticate: Could not authenticate ${username}: bad otp"}
}
this.nock = this.nock.put(`/-/user/org.couchdb.user:${username}`, body => {
this.#tap.match(body, {
_id: `org.couchdb.user:${username}`,
name: username,
password,
type: 'user',
roles: [],
})
if (!body.date) {
return false
}
return true
}).reply(201, {
ok: true,
id: 'org.couchdb.user:undefined',
rev: '_we_dont_use_revs_any_more',
token,
})
}

// team can be a team or a username
lsPackages ({ team, packages = {} }) {
if (team.startsWith('@')) {
Expand All @@ -92,15 +119,15 @@ class MockRegistry {
} else {
uri = `/-/org/${encodeURIComponent(scope)}/package`
}
this.nock.get(uri).query({ format: 'cli' }).reply(200, packages)
this.nock = this.nock.get(uri).query({ format: 'cli' }).reply(200, packages)
}

lsCollaborators ({ spec, user, collaborators = {} }) {
const query = { format: 'cli' }
if (user) {
query.user = user
}
this.nock.get(`/-/package/${encodeURIComponent(spec)}/collaborators`)
this.nock = this.nock.get(`/-/package/${encodeURIComponent(spec)}/collaborators`)
.query(query)
.reply(200, collaborators)
}
Expand Down
Loading

0 comments on commit 71ab00a

Please sign in to comment.