Skip to content

Commit

Permalink
fix and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fforbeck committed Nov 29, 2024
1 parent 396d820 commit 9984a07
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 97 deletions.
11 changes: 0 additions & 11 deletions packages/filecoin-api/test/context/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import * as API from '../../src/types.js'

import { validateAuthorization } from '../utils.js'
import { mockService } from './mocks.js'
import { Access } from '@web3-storage/capabilities'

export { getStoreImplementations } from './store-implementations.js'
export { getQueueImplementations } from './queue-implementations.js'
Expand Down Expand Up @@ -241,16 +240,6 @@ export function getMockService() {
}
),
},
access: {
delegate: Server.provide(
Access.delegate,
async ({ capability, invocation }) => {
return {
ok: {},
}
}
),
},
})
}

Expand Down
12 changes: 6 additions & 6 deletions packages/w3up-client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { FilecoinClient } from './capability/filecoin.js'
import { CouponAPI } from './coupon.js'
export * as Access from './capability/access.js'
import * as Result from './result.js'
import { remove } from '@web3-storage/capabilities/store'

export {
AccessClient,
Expand Down Expand Up @@ -260,7 +259,7 @@ export class Client extends Base {
* @typedef {object} SpaceCreateOptions
* @property {boolean} [skipContentServeAuthorization] - Whether to skip the Content Serve authorization. It means that the content of the space will not be served by any Content Serve Service.
* @property {`did:${string}:${string}`[]} [authorizeContentServeServices] - The DID Key or DID Web of the Content Serve Service to authorize to serve content from the created space.
* @property {import('./types.js').ConnectionView<import('./types.js').ContentServeService>} [authorizeContentServeServices.connection] - The connection to the Content Serve Service that will handle, validate, and store the access/delegate UCAN invocation.
* @property {import('./types.js').ConnectionView<import('./types.js').ContentServeService>} [connection] - The connection to the Content Serve Service that will handle, validate, and store the access/delegate UCAN invocation.
* @property {Account.Account} [account] - The account configured as the recovery account for the space.
* @property {string} [name] - The name of the space to create.
*
Expand All @@ -269,10 +268,8 @@ export class Client extends Base {
* @returns {Promise<import("./space.js").OwnedSpace>} The created space owned by the agent.
*/
async createSpace(name, options) {
const space = await this._agent.createSpace(name)

// Save the space to authorize the client to use the space
await space.save()
const space = await this._agent.createSpace(name)

const account = options.account
if (account) {
Expand All @@ -285,6 +282,9 @@ export class Client extends Base {
)
}

// Save the space to authorize the client to use the space
await space.save()

// Create a recovery for the account
const recovery = await space.createRecovery(account.did())

Expand Down Expand Up @@ -380,6 +380,7 @@ export class Client extends Base {
})
.execute(options.connection)

/* c8 ignore next 8 - can't mock this error */
if (verificationResult.out.error) {
throw new Error(
`failed to publish delegation for audience ${options.audience}: ${verificationResult.out.error.message}`,
Expand All @@ -388,7 +389,6 @@ export class Client extends Base {
}
)
}

return { ok: { ...verificationResult.out.ok, delegation } }
} finally {
if (currentSpace) {
Expand Down
22 changes: 16 additions & 6 deletions packages/w3up-client/test/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ export const testAccount = Test.withContext({
assert,
{ client, mail, grantAccess }
) => {
const space = await client.createSpace('test')
const space = await client.createSpace('test', {
skipContentServeAuthorization: true,
})
const mnemonic = space.toMnemonic()
const { signer } = await Space.fromMnemonic(mnemonic, { name: 'import' })
assert.deepEqual(
Expand Down Expand Up @@ -147,7 +149,9 @@ export const testAccount = Test.withContext({

'multi device workflow': async (asserts, { connect, mail, grantAccess }) => {
const laptop = await connect()
const space = await laptop.createSpace('main')
const space = await laptop.createSpace('main', {
skipContentServeAuthorization: true,
})

// want to provision space ?
const email = '[email protected]'
Expand Down Expand Up @@ -183,7 +187,9 @@ export const testAccount = Test.withContext({
asserts.deepEqual(result.did, space.did())
},
'setup recovery': async (assert, { client, mail, grantAccess }) => {
const space = await client.createSpace('test')
const space = await client.createSpace('test', {
skipContentServeAuthorization: true,
})

const email = '[email protected]'
const login = Account.login(client, email)
Expand Down Expand Up @@ -280,7 +286,9 @@ export const testAccount = Test.withContext({
assert,
{ client, mail, grantAccess }
) => {
const space = await client.createSpace('test')
const space = await client.createSpace('test', {
skipContentServeAuthorization: true,
})

const email = '[email protected]'
const login = Account.login(client, email)
Expand All @@ -299,8 +307,10 @@ export const testAccount = Test.withContext({
assert.equal(typeof subs.results[0].subscription, 'string')
},

'space.save': async (assert, { client, mail, grantAccess }) => {
const space = await client.createSpace('test')
'space.save': async (assert, { client }) => {
const space = await client.createSpace('test', {
skipContentServeAuthorization: true,
})
assert.deepEqual(client.spaces(), [])

const result = await space.save()
Expand Down
8 changes: 6 additions & 2 deletions packages/w3up-client/test/capability/filecoin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import * as Test from '../test.js'
export const FilecoinClient = Test.withContext({
offer: {
'should send an offer': async (assert, { client: alice }) => {
const space = await alice.createSpace('test')
const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
await alice.setCurrentSpace(space.did())
Expand Down Expand Up @@ -36,7 +38,9 @@ export const FilecoinClient = Test.withContext({
throw new Error('could not compute proof')
}

const space = await alice.createSpace('test')
const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
await alice.setCurrentSpace(space.did())
Expand Down
4 changes: 3 additions & 1 deletion packages/w3up-client/test/capability/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export const IndexClient = Test.withContext({
) => {
const car = await randomCAR(128)

const space = await alice.createSpace('test')
const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
await alice.setCurrentSpace(space.did())
Expand Down
4 changes: 3 additions & 1 deletion packages/w3up-client/test/capability/subscription.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export const SubscriptionClient = Test.withContext({
assert,
{ client, connection, service, plansStorage, grantAccess, mail }
) => {
const space = await client.createSpace('test')
const space = await client.createSpace('test', {
skipContentServeAuthorization: true,
})
const email = '[email protected]'
const login = Account.login(client, email)
const message = await mail.take()
Expand Down
16 changes: 12 additions & 4 deletions packages/w3up-client/test/capability/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export const UploadClient = Test.withContext({
) => {
const car = await randomCAR(128)

const space = await alice.createSpace('test')
const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
await alice.setCurrentSpace(space.did())
Expand Down Expand Up @@ -38,7 +40,9 @@ export const UploadClient = Test.withContext({
) => {
const car = await randomCAR(128)

const space = await alice.createSpace('test')
const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
await alice.setCurrentSpace(space.did())
Expand Down Expand Up @@ -77,7 +81,9 @@ export const UploadClient = Test.withContext({
) => {
const car = await randomCAR(128)

const space = await alice.createSpace('test')
const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
await alice.setCurrentSpace(space.did())
Expand Down Expand Up @@ -109,7 +115,9 @@ export const UploadClient = Test.withContext({
) => {
const car = await randomCAR(128)

const space = await alice.createSpace('test')
const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
await alice.setCurrentSpace(space.did())
Expand Down
Loading

0 comments on commit 9984a07

Please sign in to comment.