Skip to content

Commit

Permalink
fix: test against actual api (#1438)
Browse files Browse the repository at this point in the history
Removes mocks from tests everywhere in w3up client and tests against
in-memory w3up service. This is aimed to help with #1425, which was
suffering from issues caused by mocks
  • Loading branch information
Gozala authored May 8, 2024
1 parent a22c0ff commit f8132ca
Show file tree
Hide file tree
Showing 26 changed files with 879 additions and 1,409 deletions.
4 changes: 2 additions & 2 deletions packages/upload-api/test/helpers/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export const createContext = async (
dealTrackerService: {
connection: dealTrackerConnection,
invocationConfig: {
issuer: id,
with: id.did(),
issuer: signer,
with: signer.did(),
audience: dealTrackerSigner,
},
},
Expand Down
5 changes: 3 additions & 2 deletions packages/w3up-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@
"build": "tsc --build",
"check": "tsc --build",
"prepare": "npm run build",
"test": "npm-run-all -p -r mock test:all",
"test": "npm-run-all -p -r mock:* test:all",
"test:all": "run-s test:node test:browser",
"test:node": "hundreds -r html -r text mocha 'test/**/!(*.browser).test.js' -n experimental-vm-modules -n no-warnings -n stack-trace-limit=1000",
"test:browser": "playwright-test --runner mocha 'test/**/!(*.node).test.js'",
"mock": "run-p mock:*",
"mock:bucket-200": "PORT=9200 STATUS=200 node test/helpers/bucket-server.js",
"mock:bucket-200": "PORT=8989 STATUS=200 node test/helpers/bucket-server.js",
"mock:receipts-server": "PORT=9201 node test/helpers/receipts-server.js",
"coverage": "c8 report -r html && open coverage/index.html",
"rc": "npm version prerelease --preid rc",
"docs": "npm run build && typedoc --out docs-generated"
},
Expand Down
1 change: 0 additions & 1 deletion packages/w3up-client/src/ability.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const setOfAbilities = new Set(abilitiesAsStrings)
* @param {string[]} abilities
* @returns {import('@web3-storage/capabilities/types').ServiceAbility[]}
*/
/* c8 ignore next */
export function asAbilities(abilities) {
for (const ability of abilities) {
if (
Expand Down
2 changes: 2 additions & 0 deletions packages/w3up-client/src/capability/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export class SubscriptionClient extends Base {
*
* @param {import('@web3-storage/access').AccountDID} account
*/
/* c8 ignore next */
async list(account) {
const out = await list({ agent: this.agent }, { account })
/* c8 ignore next 8 */
if (!out.ok) {
throw new Error(
`failed ${SubscriptionCapabilities.list.can} invocation`,
Expand Down
1 change: 1 addition & 0 deletions packages/w3up-client/src/capability/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class UsageClient extends Base {
*/
async report(space, period) {
const out = await report({ agent: this.agent }, { space, period })
/* c8 ignore next 7 */
if (!out.ok) {
throw new Error(`failed ${UsageCapabilities.report.can} invocation`, {
cause: out.error,
Expand Down
25 changes: 17 additions & 8 deletions packages/w3up-client/test/ability.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import assert from 'assert'
import { asAbilities } from '../src/ability.js'
import { asAbilities } from '@web3-storage/w3up-client'
import * as Test from './test.js'

describe('abilities', () => {
it('should return the passed argument if all abilities are valid', async () => {
/**
* @type {Test.Suite}
*/
export const testAbilities = {
'should return the passed argument if all abilities are valid': async (
assert
) => {
const abilities = ['store/add', 'upload/add']
assert.equal(asAbilities(abilities), abilities)
})
},

it('should throw an error if one of the abilities is not supported', async () => {
'should throw an error if one of the abilities is not supported': async (
assert
) => {
assert.throws(
() => {
asAbilities(['foo/bar'])
Expand All @@ -17,5 +24,7 @@ describe('abilities', () => {
message: 'foo/bar is not a supported capability',
}
)
})
})
},
}

Test.test({ Abilities: testAbilities })
7 changes: 2 additions & 5 deletions packages/w3up-client/test/access.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import * as Test from './test.js'
import * as Access from '../src/capability/access.js'
import * as Result from '../src/result.js'

/**
* @type {Test.Suite}
*/
export const testAccess = {
export const testAccess = Test.withContext({
'capability.access.request': async (
assert,
{ client, mail, grantAccess }
Expand All @@ -32,6 +29,6 @@ export const testAccess = {
await access.save()
assert.ok(client.proofs().length > 0)
},
}
})

Test.test({ Access: testAccess })
4 changes: 2 additions & 2 deletions packages/w3up-client/test/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as Result from '../src/result.js'
/**
* @type {Test.Suite}
*/
export const testAccount = {
export const testAccount = Test.withContext({
'list accounts': async (assert, { client, mail, grantAccess }) => {
const email = 'alice@web.mail'

Expand Down Expand Up @@ -286,6 +286,6 @@ export const testAccount = {

assert.deepEqual(client.currentSpace()?.did(), space.did())
},
}
})

Test.test({ Account: testAccount })
96 changes: 59 additions & 37 deletions packages/w3up-client/test/capability/access.test.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,71 @@
import assert from 'assert'
import { create as createServer, provide } from '@ucanto/server'
import * as CAR from '@ucanto/transport/car'
import * as Signer from '@ucanto/principal/ed25519'
import * as AccessCapabilities from '@web3-storage/capabilities/access'
import { AgentData } from '@web3-storage/access/agent'
import { mockService, mockServiceConf } from '../helpers/mocks.js'
import { Client } from '../../src/client.js'
import { validateAuthorization } from '../helpers/utils.js'

describe('AccessClient', () => {
describe('claim', () => {
it('should claim delegations', async () => {
const service = mockService({
access: {
claim: provide(AccessCapabilities.claim, ({ invocation }) => {
assert.equal(invocation.issuer.did(), alice.agent.did())
assert.equal(invocation.capabilities.length, 1)
const invCap = invocation.capabilities[0]
assert.equal(invCap.can, AccessCapabilities.claim.can)
return {
ok: {
delegations: {},
},
}
}),
import * as Upload from '@web3-storage/capabilities/upload'
import * as Test from '../test.js'

export const AccessClient = Test.withContext({
claim: {
'should claim delegations': async (assert, { connection }) => {
const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: {
access: connection,
upload: connection,
},
})

const server = createServer({
id: await Signer.generate(),
service,
codec: CAR.inbound,
validateAuthorization,
})
const delegations = await alice.capability.access.claim()

assert.deepEqual(delegations, [])
},
'should delegate and then claim': async (
assert,
{ connection, provisionsStorage }
) => {
const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: await mockServiceConf(server),
serviceConf: {
access: connection,
upload: connection,
},
})
const space = await alice.createSpace('upload-test')
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
await alice.setCurrentSpace(space.did())

const delegations = await alice.capability.access.claim()
// Then we setup a billing for this account
await provisionsStorage.put({
// @ts-expect-error
provider: connection.id.did(),
account: alice.agent.did(),
consumer: space.did(),
})

assert(service.access.claim.called)
assert.equal(service.access.claim.callCount, 1)
assert.deepEqual(delegations, [])
})
})
const bob = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: {
access: connection,
upload: connection,
},
})

const uploadList = await Upload.list.delegate({
issuer: alice.agent.issuer,
audience: bob,
with: space.did(),
})

const result = await alice.capability.access.delegate({
delegations: [uploadList],
})

assert.ok(result.ok)

const delegations = await bob.capability.access.claim()
assert.deepEqual(delegations, [uploadList])
},
},
})

Test.test({ AccessClient })
Loading

0 comments on commit f8132ca

Please sign in to comment.