Skip to content

Commit

Permalink
fix: run format for upload-api (#825)
Browse files Browse the repository at this point in the history
Noticed while working on aggregate-api and client that format/lint is
failing for `upload-api`. So ran format for it in different branch to
put fix it in main without creating noise in other PR
  • Loading branch information
vasco-santos authored Jun 29, 2023
1 parent 3a75655 commit 59dc765
Show file tree
Hide file tree
Showing 18 changed files with 553 additions and 515 deletions.
4 changes: 2 additions & 2 deletions packages/upload-api/src/access/claim.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export const claim = async (
error: {
name: 'AccessClaimFailure',
message: 'error finding delegations',
cause: claimedResult.error
}
cause: claimedResult.error,
},
}
}
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/upload-api/src/access/confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function confirm({ capability, invocation }, ctx) {
audience: account.did(),
})

if (delegationsResult.error){
if (delegationsResult.error) {
return delegationsResult
}

Expand Down
4 changes: 3 additions & 1 deletion packages/upload-api/src/access/delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export const delegate = async ({ capability, invocation }, context) => {
)

if (result.ok) {
await context.delegationsStorage.putMany(delegated.ok, { cause: invocation.link() })
await context.delegationsStorage.putMany(delegated.ok, {
cause: invocation.link(),
})
return { ok: {} }
} else {
return result
Expand Down
5 changes: 4 additions & 1 deletion packages/upload-api/src/customer/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ const get = async ({ capability }, context) => {
return { error: new UnknownProvider(capability.with) }
}

const customer = await context.provisionsStorage.getCustomer(capability.with, capability.nb.customer)
const customer = await context.provisionsStorage.getCustomer(
capability.with,
capability.nb.customer
)
return { ok: { customer: customer.ok ? { did: customer.ok.did } : null } }
}

Expand Down
6 changes: 3 additions & 3 deletions packages/upload-api/src/provider-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ export const add = async (
},
}
}
if ((await provisions.hasStorageProvider(consumer)).ok){
if ((await provisions.hasStorageProvider(consumer)).ok) {
return {
error: {
name: 'SpaceAlreadyProvisioned',
message: `${consumer} already has a storage provider`
}
message: `${consumer} already has a storage provider`,
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/upload-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { DelegationsStorage as Delegations } from './types/delegations'
import { ProvisionsStorage as Provisions } from './types/provisions'

export type ValidationEmailSend = {
to: string,
to: string
url: string
}

Expand Down
9 changes: 6 additions & 3 deletions packages/upload-api/src/types/delegations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface DelegationsStorage<
> {
/**
* Write several items into storage.
*
*
* Options accepts an optional `cause` that MUST be the CID of the invocation
* that contains the given delegations. Implementations MAY choose
* to avoid storing delegations as long as they can reliably
Expand All @@ -29,6 +29,9 @@ export interface DelegationsStorage<
/**
* find all items that match the query
*/
find: (query: Query) =>
Promise<Ucanto.Result<Ucanto.Delegation<Ucanto.Tuple<Cap>>[], Ucanto.Failure>>
find: (
query: Query
) => Promise<
Ucanto.Result<Ucanto.Delegation<Ucanto.Tuple<Cap>>[], Ucanto.Failure>
>
}
14 changes: 5 additions & 9 deletions packages/upload-api/src/types/provisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export interface Customer {
/**
* stores instances of a storage provider being consumed by a consumer
*/
export interface ProvisionsStorage<
ProviderDID = Ucanto.DID<'web'>
> {
export interface ProvisionsStorage<ProviderDID = Ucanto.DID<'web'>> {
services: ProviderDID[]
hasStorageProvider: (
consumer: Ucanto.DID<'key'>
Expand All @@ -31,16 +29,14 @@ export interface ProvisionsStorage<
*
* @param item - provision to store
*/
put: (
item: Provision
) => Promise<Ucanto.Result<{}, Ucanto.Failure>>
put: (item: Provision) => Promise<Ucanto.Result<{}, Ucanto.Failure>>

/**
* Returns information about a customer related to the given provider.
*
* TODO: this should probably be moved to its own Storage interface, but
*
* TODO: this should probably be moved to its own Storage interface, but
* I'd like to tackle that once we've finished consolidating the access and upload services.
*
*
* @param provider DID of the provider we care about
* @param customer DID of the customer
* @returns record for the specified customer, if it is in our system
Expand Down
9 changes: 4 additions & 5 deletions packages/upload-api/src/utils/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ export class DebugEmail {
}
}
/**
*
* @param {number} retries
*
* @param {number} retries
*/
async takeWithRetries(retries = MAX_TAKE_RETRIES) {
const self = this
if ((this.emails.length > 0) || (retries <= 0)){
if (this.emails.length > 0 || retries <= 0) {
return this.emails.shift()
} else {
return new Promise(resolve => {
return new Promise((resolve) => {
setTimeout(() => resolve(self.takeWithRetries(retries - 1)), 100)
})
}
Expand All @@ -46,4 +46,3 @@ export class DebugEmail {
return this.takeWithRetries()
}
}

13 changes: 9 additions & 4 deletions packages/upload-api/test/delegations-storage-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export const test = {
Array.from({ length: count }).map(() => createSampleDelegation())
)
await delegationsStorage.putMany(delegations)
assert.deepEqual(await delegationsStorage.count(), BigInt(delegations.length))
assert.deepEqual(
await delegationsStorage.count(),
BigInt(delegations.length)
)
},
'can retrieve delegations by audience': async (assert, context) => {
const delegations = await context.delegationsStorage
Expand Down Expand Up @@ -71,14 +74,16 @@ export const test = {

await delegations.putMany([...delegationsForAlice, ...delegationsForBob])

const aliceDelegations = (await delegations.find({ audience: alice.did() })).ok
const aliceDelegations = (await delegations.find({ audience: alice.did() }))
.ok
assert.deepEqual(aliceDelegations?.length, delegationsForAlice.length)

const bobDelegations = (await delegations.find({ audience: bob.did() })).ok
assert.deepEqual(bobDelegations?.length, delegationsForBob.length)

const carol = await principal.ed25519.generate()
const carolDelegations = (await delegations.find({ audience: carol.did() })).ok
const carolDelegations = (await delegations.find({ audience: carol.did() }))
.ok
assert.deepEqual(carolDelegations?.length, 0)
}
},
}
2 changes: 1 addition & 1 deletion packages/upload-api/test/delegations-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class DelegationsStorage {
}
}
return {
ok: delegations
ok: delegations,
}
}
}
4 changes: 2 additions & 2 deletions packages/upload-api/test/delegations-storage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ describe('in memory delegations storage', async () => {
const define = name.startsWith('only ')
? it.only
: name.startsWith('skip ')
? it.skip
: it
? it.skip
: it

define(name, async () => {
const context = await createContext()
Expand Down
6 changes: 5 additions & 1 deletion packages/upload-api/test/helpers/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,9 @@ export const createContext = async (options = {}) => {
* @param {Types.UcantoServerTestContext} context
*/
export const cleanupContext = async (context) => {
/** @type {CarStoreBucket & { deactivate: () => Promise<void> }}} */(await context.carStoreBucket).deactivate()
/** @type {CarStoreBucket & { deactivate: () => Promise<void> }}} */
// @ts-ignore type misses S3 bucket properties like accessKey
const store = context.carStoreBucket

await store.deactivate()
}
2 changes: 1 addition & 1 deletion packages/upload-api/test/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export {
Upload,
delegationsStorageTests,
provisionsStorageTests,
DebugEmail
DebugEmail,
}
127 changes: 68 additions & 59 deletions packages/upload-api/test/provisions-storage-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,83 @@ import { Provider } from '@web3-storage/capabilities'
* @type {API.Tests}
*/
export const test = {
'should persist provisions':
async (assert, context) => {
const storage = context.provisionsStorage
const spaceA = await principal.ed25519.generate()
const spaceB = await principal.ed25519.generate()
const issuerKey = await principal.ed25519.generate()
const issuer = issuerKey.withDID('did:mailto:example.com:foo')
const provider = 'did:web:web3.storage:providers:w3up-alpha'
const invocation = await Provider.add
'should persist provisions': async (assert, context) => {
const storage = context.provisionsStorage
const spaceA = await principal.ed25519.generate()
const spaceB = await principal.ed25519.generate()
const issuerKey = await principal.ed25519.generate()
const issuer = issuerKey.withDID('did:mailto:example.com:foo')
const provider = 'did:web:web3.storage:providers:w3up-alpha'
const invocation = await Provider.add
.invoke({
issuer,
audience: issuer,
with: issuer.did(),
nb: {
consumer: spaceA.did(),
provider,
},
})
.delegate()
/** @type {Types.Provision} */
const provision = {
cause: invocation,
consumer: spaceA.did(),
provider,
customer: issuer.did(),
}

assert.deepEqual(await storage.count(), BigInt(0))

const result = await storage.put(provision)
assert.ok(!result.error, 'adding a provision failed')
assert.deepEqual(await storage.count(), BigInt(1))

const spaceHasStorageProvider = await storage.hasStorageProvider(
spaceA.did()
)
assert.deepEqual(spaceHasStorageProvider?.ok, true)
// ensure no error if we try to store same provision twice
const dupeResult = await storage.put(provision)
assert.ok(
!dupeResult.error,
'putting the same provision twice did not succeed'
)
assert.deepEqual(await storage.count(), BigInt(1))

const modifiedProvision = {
...provision,
cause: await Provider.add
.invoke({
issuer,
audience: issuer,
with: issuer.did(),
nb: {
consumer: spaceA.did(),
consumer: spaceB.did(),
provider,
},
})
.delegate()
/** @type {Types.Provision} */
const provision = {
cause: invocation,
consumer: spaceA.did(),
provider,
customer: issuer.did(),
}

assert.deepEqual(await storage.count(), BigInt(0))

const result = await storage.put(provision)
assert.ok(!result.error, 'adding a provision failed')
assert.deepEqual(await storage.count(), BigInt(1))

const spaceHasStorageProvider = await storage.hasStorageProvider(
spaceA.did()
)
assert.deepEqual(spaceHasStorageProvider?.ok, true)
// ensure no error if we try to store same provision twice
const dupeResult = await storage.put(provision)
assert.ok(!dupeResult.error, 'putting the same provision twice did not succeed')
assert.deepEqual(await storage.count(), BigInt(1))

const modifiedProvision = {
...provision,
cause: await Provider.add
.invoke({
issuer,
audience: issuer,
with: issuer.did(),
nb: {
consumer: spaceB.did(),
provider,
},
}).delegate(),
}
.delegate(),
}

// ensure error if we try to store a provision with the same (consumer, provider, customer) but a different cause
const modifiedResult = await storage.put(modifiedProvision)
assert.ok(modifiedResult.error, 'provisioning with duplicate (consumer, provider, customer) but a different cause succeeded and should not have!')
assert.deepEqual(await storage.count(), BigInt(1))
// ensure error if we try to store a provision with the same (consumer, provider, customer) but a different cause
const modifiedResult = await storage.put(modifiedProvision)
assert.ok(
modifiedResult.error,
'provisioning with duplicate (consumer, provider, customer) but a different cause succeeded and should not have!'
)
assert.deepEqual(await storage.count(), BigInt(1))

// verify that provisions are returned as part of customer record
const customerResult = await storage.getCustomer(provider, issuer.did())
assert.ok(!customerResult.error, 'error getting customer record')
assert.deepEqual(customerResult.ok, { did: issuer.did() })
// verify that provisions are returned as part of customer record
const customerResult = await storage.getCustomer(provider, issuer.did())
assert.ok(!customerResult.error, 'error getting customer record')
assert.deepEqual(customerResult.ok, { did: issuer.did() })

const fakeCustomerResult = await storage.getCustomer(provider, 'did:mailto:example.com:travis')
assert.ok(!fakeCustomerResult.error, 'error getting fake customer record')
assert.equal(fakeCustomerResult.ok, null)
}
const fakeCustomerResult = await storage.getCustomer(
provider,
'did:mailto:example.com:travis'
)
assert.ok(!fakeCustomerResult.error, 'error getting fake customer record')
assert.equal(fakeCustomerResult.ok, null)
},
}
Loading

0 comments on commit 59dc765

Please sign in to comment.