Skip to content

Commit

Permalink
feat: allow customers to create more than one space (#246)
Browse files Browse the repository at this point in the history
this is just a matter of changing how we generate subscription IDs. they
are now generated from customer AND consumer, which means they will be
unique for a given customer/consumer pair - in other words, a given
customer will only be able to provision a given space one time.
  • Loading branch information
travis authored Oct 26, 2023
1 parent 8579f5b commit fe3e3c6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions test/helpers/up-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function getAuthLinkFromEmail (email, accessServiceUrl) {
return link.replace(process.env.ACCESS_SERVICE_URL, accessServiceUrl)
}

async function createMailSlurpInbox() {
export async function createMailSlurpInbox() {
const apiKey = process.env.MAILSLURP_API_KEY
const mailslurp = new MailSlurp({ apiKey })
const inbox = await mailslurp.inboxController.createInbox({})
Expand All @@ -46,7 +46,7 @@ export async function createNewClient(uploadServiceUrl) {

export async function setupNewClient (uploadServiceUrl, options = {}) {
// create an inbox
const { mailslurp, id: inboxId, email } = await createMailSlurpInbox()
const { mailslurp, id: inboxId, email } = options.inbox || await createMailSlurpInbox()
const client = await createNewClient(uploadServiceUrl)

const timeoutMs = process.env.MAILSLURP_TIMEOUT ? parseInt(process.env.MAILSLURP_TIMEOUT) : 60_000
Expand Down
10 changes: 8 additions & 2 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
getCarparkBucketInfo,
getDynamoDb
} from './helpers/deployment.js'
import { createNewClient, setupNewClient } from './helpers/up-client.js'
import { createMailSlurpInbox, createNewClient, setupNewClient } from './helpers/up-client.js'
import { randomFile } from './helpers/random.js'
import { getTableItem, getAllTableRows, pollQueryTable } from './helpers/table.js'

Expand Down Expand Up @@ -116,12 +116,18 @@ test('authorizations can be blocked by email or domain', async t => {

// Integration test for all flow from uploading a file to Kinesis events consumers and replicator
test('w3infra integration flow', async t => {
const client = await setupNewClient(t.context.apiEndpoint)
const inbox = await createMailSlurpInbox()
const client = await setupNewClient(t.context.apiEndpoint, { inbox })
const spaceDid = client.currentSpace()?.did()
if (!spaceDid) {
throw new Error('Testing space DID must be set')
}

// it should be possible to create more than one space
const space = await client.createSpace("2nd space")
await client.setCurrentSpace(space.did())
await client.registerSpace(inbox.email)

// Get space metrics before upload
const spaceBeforeUploadAddMetrics = await getSpaceMetrics(t, spaceDid, SPACE_METRICS_NAMES.UPLOAD_ADD_TOTAL)
const spaceBeforeStoreAddMetrics = await getSpaceMetrics(t, spaceDid, SPACE_METRICS_NAMES.STORE_ADD_TOTAL)
Expand Down
10 changes: 4 additions & 6 deletions upload-api/stores/provisions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import { CBOR, Failure } from '@ucanto/server'

/**
* Create a subscription ID for a given provision. Currently
* uses the CID of `customer` which ensures each customer
* will get at most one subscription. This can be relaxed (ie,
* by deriving subscription ID from customer AND consumer) in the future
* or by other providers for flexibility.
* uses a CID generated from `consumer` which ensures a space
* can be provisioned at most once.
*
* @param {import('@web3-storage/upload-api').Provision} item
* @returns string
*/
export const createProvisionSubscriptionId = async ({ customer }) =>
(await CBOR.write({ customer })).cid.toString()
export const createProvisionSubscriptionId = async ({ customer, consumer }) =>
(await CBOR.write({ consumer })).cid.toString()

/**
* @param {import('../types').SubscriptionTable} subscriptionTable
Expand Down

0 comments on commit fe3e3c6

Please sign in to comment.