Skip to content

Commit

Permalink
move capabilties e2e tests to members e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 committed Dec 18, 2023
1 parent d0e7723 commit de746f9
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 173 deletions.
23 changes: 18 additions & 5 deletions src/capabilities.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { currentSchemaVersions } from '@mapeo/schema'
import mapObject from 'map-obj'
import { kCreateWithDocId } from './datatype/index.js'
import { valueOf } from './utils.js'

// Randomly generated 8-byte encoded as hex
export const COORDINATOR_ROLE_ID = 'f7c150f5a3a9a855'
Expand Down Expand Up @@ -280,11 +281,23 @@ export class Capabilities {
if (!ownCapabilities.roleAssignment.includes(roleId)) {
throw new Error('No capability to assign role ' + roleId)
}
await this.#dataType[kCreateWithDocId](deviceId, {
schemaName: 'role',
roleId,
fromIndex,
})

const existingRoleDoc = await this.#dataType
.getByDocId(deviceId)
.catch(() => null)

if (existingRoleDoc) {
await this.#dataType.update(existingRoleDoc.versionId, {
...valueOf(existingRoleDoc),
roleId,
})
} else {
await this.#dataType[kCreateWithDocId](deviceId, {
schemaName: 'role',
roleId,
fromIndex,
})
}
}

async #isProjectCreator() {
Expand Down
8 changes: 0 additions & 8 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const CORESTORE_STORAGE_FOLDER_NAME = 'corestore'
const INDEXER_STORAGE_FOLDER_NAME = 'indexer'
export const kCoreManager = Symbol('coreManager')
export const kCoreOwnership = Symbol('coreOwnership')
export const kCapabilities = Symbol('capabilities')
export const kSetOwnDeviceInfo = Symbol('kSetOwnDeviceInfo')
export const kBlobStore = Symbol('blobStore')
export const kProjectReplicate = Symbol('replicate project')
Expand Down Expand Up @@ -348,13 +347,6 @@ export class MapeoProject extends TypedEmitter {
return this.#coreOwnership
}

/**
* Capabilities instance, used for tests
*/
get [kCapabilities]() {
return this.#capabilities
}

get [kBlobStore]() {
return this.#blobStore
}
Expand Down
159 changes: 0 additions & 159 deletions test-e2e/capabilities.js

This file was deleted.

126 changes: 125 additions & 1 deletion test-e2e/members.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { test } from 'brittle'
import { randomBytes } from 'crypto'

import {
COORDINATOR_ROLE_ID,
CREATOR_CAPABILITIES,
DEFAULT_CAPABILITIES,
MEMBER_ROLE_ID,
Expand Down Expand Up @@ -168,7 +169,7 @@ test('invite uses custom role name when provided', async (t) => {
invitee.invite.on('invite-received', ({ roleName }) => {
t.is(roleName, 'friend', 'roleName should be equal')
})

await invite({
invitor,
projectId,
Expand Down Expand Up @@ -206,3 +207,126 @@ test('invite uses default role name when not provided', async (t) => {

await disconnectPeers(managers)
})

test('capabilities - creator capabilities and role assignment', async (t) => {
const [manager] = await createManagers(1, t)

const projectId = await manager.createProject()
const project = await manager.getProject(projectId)
const ownCapabilities = await project.$getOwnCapabilities()

t.alike(
ownCapabilities,
CREATOR_CAPABILITIES,
'Project creator has creator capabilities'
)

const deviceId = randomBytes(32).toString('hex')
await project.$member.assignRole(deviceId, MEMBER_ROLE_ID)

const member = await project.$member.getById(deviceId)

t.alike(
member.capabilities,
DEFAULT_CAPABILITIES[MEMBER_ROLE_ID],
'Can assign capabilities to device'
)
})

test('capabilities - new device without capabilities', async (t) => {
const [manager] = await createManagers(1, t)

const projectId = await manager.addProject(
{
projectKey: randomBytes(32),
encryptionKeys: { auth: randomBytes(32) },
},
{ waitForSync: false }
)

const project = await manager.getProject(projectId)

const ownCapabilities = await project.$getOwnCapabilities()

t.alike(
ownCapabilities.sync,
{
auth: 'allowed',
config: 'allowed',
data: 'blocked',
blobIndex: 'blocked',
blob: 'blocked',
},
'A new device before sync can sync auth and config namespaces, but not other namespaces'
)
await t.exception(async () => {
const deviceId = randomBytes(32).toString('hex')
await project.$member.assignRole(deviceId, MEMBER_ROLE_ID)
}, 'Trying to assign a role without capabilities throws an error')
})

test('capabilities - getMany() on invitor device', async (t) => {
const [manager] = await createManagers(1, t)

const creatorDeviceId = manager.deviceId

const projectId = await manager.createProject()
const project = await manager.getProject(projectId)
const ownCapabilities = await project.$getOwnCapabilities()

t.alike(
ownCapabilities,
CREATOR_CAPABILITIES,
'Project creator has creator capabilities'
)

const deviceId1 = randomBytes(32).toString('hex')
const deviceId2 = randomBytes(32).toString('hex')
await project.$member.assignRole(deviceId1, MEMBER_ROLE_ID)
await project.$member.assignRole(deviceId2, COORDINATOR_ROLE_ID)

const expected = {
[deviceId1]: DEFAULT_CAPABILITIES[MEMBER_ROLE_ID],
[deviceId2]: DEFAULT_CAPABILITIES[COORDINATOR_ROLE_ID],
[creatorDeviceId]: CREATOR_CAPABILITIES,
}

const allMembers = await project.$member.getMany()

/** @type {Record<string, import('../src/capabilities.js').Capability>} */
const allMembersCapabilities = {}

for (const member of allMembers) {
allMembersCapabilities[member.deviceId] = member.capabilities
}

t.alike(allMembersCapabilities, expected, 'expected capabilities')
})

test('capabilities - getMany() on newly invited device before sync', async (t) => {
const [manager] = await createManagers(1, t)

const deviceId = manager.deviceId

const projectId = await manager.addProject(
{
projectKey: randomBytes(32),
encryptionKeys: { auth: randomBytes(32) },
},
{ waitForSync: false }
)
const project = await manager.getProject(projectId)

const expected = { [deviceId]: NO_ROLE_CAPABILITIES }

const allMembers = await project.$member.getMany()

/** @type {Record<string, import('../src/capabilities.js').Capability>} */
const allMembersCapabilities = {}

for (const member of allMembers) {
allMembersCapabilities[member.deviceId] = member.capabilities
}

t.alike(allMembersCapabilities, expected, 'expected capabilities')
})

0 comments on commit de746f9

Please sign in to comment.