Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/command/access/grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class Grant extends AccessCommand implements LeafCommand {
const response = await this.bee.patchGrantees(stampId, granteeListRef, historyAddress, { add: this.grantees })

if (response.status === 200) {
this.console.log(successText(`Access granted to ${this.grantees.join(', ')}!`))
this.console.log(successText(`Access granted to ${this.grantees.join(', ')}`))
}

accessHistory.addEvent(this.listName, {
Expand Down
3 changes: 2 additions & 1 deletion src/command/access/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { GroupCommand } from 'furious-commander'
import { Grant } from './grant'
import { Init } from './init'
import { Revoke } from './revoke'

export class Access implements GroupCommand {
public readonly name = 'access'

public readonly description = 'Share access to your uploaded files/folders'

public subCommandClasses = [Init, Grant]
public subCommandClasses = [Init, Grant, Revoke]
}
70 changes: 70 additions & 0 deletions src/command/access/revoke.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { LeafCommand, Option } from 'furious-commander'
import { exit } from 'process'
import { AccessHistory } from '../../service/access'
import { AccessHistoryOperation } from '../../service/access/types/history-event'
import { createKeyValue, errorText, successText } from '../../utils/text'
import { AccessCommand } from './access-command'

export class Revoke extends AccessCommand implements LeafCommand {
public readonly name = 'revoke'

public readonly description = 'Remove grantees from an existing grantee list'

@Option({
key: 'list-name',
alias: 'n',
description: 'Name of the grantee list',
required: true,
type: 'string',
})
public listName!: string

@Option({
key: 'grantee',
alias: 'g',
description: 'Public address of the grantee(s)',
type: 'string',
array: true,
})
public grantees!: string[]

public async run(): Promise<void> {
super.init()

if (this.grantees.length === 0) {
this.console.error(errorText('At least one grantee must be specified!'))

exit(1)
}

const accessHistory = new AccessHistory(this.commandConfig, this.console)
const lastHistoryEvent = accessHistory.getEvents(this.listName).sort((a, b) => b.createdAt - a.createdAt)[0]

if (!lastHistoryEvent) {
this.console.error(errorText(`Grantee list with name '${this.listName}' does not exist!`))

exit(1)
}
const stampId = lastHistoryEvent.stampId
const granteeListRef = lastHistoryEvent.granteeListRef
const historyAddress = lastHistoryEvent.historyAddress
const response = await this.bee.patchGrantees(stampId, granteeListRef, historyAddress, { revoke: this.grantees })

if (response.status === 200) {
this.console.log(successText(`Access revoked from ${this.grantees.join(', ')}`))
}

accessHistory.addEvent(this.listName, {
stampId: stampId,
historyAddress: response.historyref.toHex(),
granteeListRef: response.ref.toHex(),
operation: AccessHistoryOperation.Revoke,
createdAt: Date.now(),
})

if (this.verbose) {
this.console.log(createKeyValue('Grantee list reference', response.ref.toHex()))
this.console.log(createKeyValue('History address', response.historyref.toHex()))
}
}
}
67 changes: 66 additions & 1 deletion test/command/access.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describeCommand(
await System.sleepMillis(1000)
const pssAddress = await getPssAddress('http://localhost:21633')
await invokeTestCli(['access', 'grant', '--list-name', 'test-access', '--grantee', pssAddress.toHex()])
expect(getLastMessage()).toContain(`Access granted to ${pssAddress.toHex()}!`)
expect(getLastMessage()).toContain(`Access granted to ${pssAddress.toHex()}`)
})

describe('when grantee list does not exist', () => {
Expand Down Expand Up @@ -97,6 +97,71 @@ describeCommand(
})
})
})

describe('revoke', () => {
it('should revoke access from a grantee', async () => {
const pssAddress = await getPssAddress('http://localhost:21633')
await invokeTestCli([
'access',
'init',
...getStampOption(),
'-n',
'test-access',
'--grantee',
pssAddress.toHex(),
])
await System.sleepMillis(1000)
await invokeTestCli(['access', 'revoke', '--list-name', 'test-access', '--grantee', pssAddress.toHex()])
expect(getLastMessage()).toContain(`Access revoked from ${pssAddress.toHex()}`)
})

describe('when grantee list does not exist', () => {
it('should show error message', async () => {
await invokeTestCli(['access', 'revoke', '-n', 'nonexistent-list', '-g', '0x123'])
expect(consoleMessages[0]).toContain("Grantee list with name 'nonexistent-list' does not exist!")
expect(consoleMessages[1]).toContain('process.exit() was called with code 1')
})
})

describe('when no grantees are specified', () => {
it('should show error message', async () => {
await invokeTestCli(['access', 'init', ...getStampOption(), '-n', 'test-access'])
await System.sleepMillis(1000)
await invokeTestCli(['access', 'revoke', '--list-name', 'test-access'])
expect(consoleMessages[1]).toContain('At least one grantee must be specified!')
expect(consoleMessages[2]).toContain('process.exit() was called with code 1')
})
})

describe('when verbose option is used', () => {
it('should show grantee list reference and history address', async () => {
const pssAddress = await getPssAddress('http://localhost:21633')
await invokeTestCli([
'access',
'init',
...getStampOption(),
'-n',
'test-access',
'--grantee',
pssAddress.toHex(),
])
await System.sleepMillis(1000)
await invokeTestCli([
'access',
'revoke',
'--list-name',
'test-access',
'--grantee',
pssAddress.toHex(),
'--verbose',
])
expect(getNthLastMessage(2)).toContain('Grantee list reference')
expect(getNthLastMessage(2)).toMatch(/[a-f0-9]{64}/g)
expect(getLastMessage()).toContain('History address')
expect(getLastMessage()).toMatch(/[a-f0-9]{64}/g)
})
})
})
},
{ configFileName: 'access' },
)
Loading