Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): update agent label and imageUrl plus per connection label and imageUrl #516

Merged
merged 3 commits into from
Nov 3, 2021
Merged
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
4 changes: 4 additions & 0 deletions packages/core/src/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,8 @@ export class Agent {
public get injectionContainer() {
return this.container
}

public get config() {
return this.agentConfig
}
}
6 changes: 2 additions & 4 deletions packages/core/src/agent/AgentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { DidCommMimeType } from '../types'

export class AgentConfig {
private initConfig: InitConfig
public label: string
public logger: Logger
public readonly agentDependencies: AgentDependencies
public readonly fileSystem: FileSystem
Expand All @@ -24,6 +25,7 @@ export class AgentConfig {

public constructor(initConfig: InitConfig, agentDependencies: AgentDependencies) {
this.initConfig = initConfig
this.label = initConfig.label
this.logger = initConfig.logger ?? new ConsoleLogger(LogLevel.off)
this.agentDependencies = agentDependencies
this.fileSystem = new agentDependencies.FileSystem()
Expand All @@ -38,10 +40,6 @@ export class AgentConfig {
}
}

public get label() {
return this.initConfig.label
}

public get publicDidSeed() {
return this.initConfig.publicDidSeed
}
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/agent/__tests__/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ describe('Agent', () => {
})
})

describe('Change label', () => {
let agent: Agent

it('should return new label after setter is called', async () => {
expect.assertions(2)
const newLabel = 'Agent: Agent Class Test 2'

agent = new Agent(config, dependencies)
expect(agent.config.label).toBe(config.label)

agent.config.label = newLabel
expect(agent.config.label).toBe(newLabel)
})
})

describe('Dependency Injection', () => {
it('should be able to resolve registered instances', () => {
const agent = new Agent(config, dependencies)
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/modules/connections/ConnectionsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export class ConnectionsModule {
alias?: string
mediatorId?: string
multiUseInvitation?: boolean
myLabel?: string
myImageUrl?: string
}): Promise<{
invitation: ConnectionInvitationMessage
connectionRecord: ConnectionRecord
Expand All @@ -60,6 +62,8 @@ export class ConnectionsModule {
alias: config?.alias,
routing: myRouting,
multiUseInvitation: config?.multiUseInvitation,
myLabel: config?.myLabel,
myImageUrl: config?.myImageUrl,
})

return { connectionRecord, invitation }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,43 @@ describe('ConnectionService', () => {
// Defaults to false
expect(multiUseUndefined.multiUseInvitation).toBe(false)
})

it('returns a connection record with the custom label from the config', async () => {
expect.assertions(1)

const { message: invitation } = await connectionService.createInvitation({
routing: myRouting,
myLabel: 'custom-label',
})

expect(invitation).toEqual(
expect.objectContaining({
label: 'custom-label',
recipientKeys: [expect.any(String)],
routingKeys: [],
serviceEndpoint: config.endpoints[0],
})
)
})

it('returns a connection record with the custom image url from the config', async () => {
expect.assertions(1)

const { message: invitation } = await connectionService.createInvitation({
routing: myRouting,
myImageUrl: 'custom-image-url',
})

expect(invitation).toEqual(
expect.objectContaining({
label: config.label,
imageUrl: 'custom-image-url',
recipientKeys: [expect.any(String)],
routingKeys: [],
serviceEndpoint: config.endpoints[0],
})
)
})
})

describe('processInvitation', () => {
Expand Down Expand Up @@ -248,6 +285,28 @@ describe('ConnectionService', () => {
expect(message.imageUrl).toBe(connectionImageUrl)
})

it('returns a connection request message containing a custom label', async () => {
expect.assertions(1)

const connection = getMockConnection()
mockFunction(connectionRepository.getById).mockReturnValue(Promise.resolve(connection))

const { message } = await connectionService.createRequest('test', { myLabel: 'custom-label' })

expect(message.label).toBe('custom-label')
})

it('returns a connection request message containing a custom image url', async () => {
expect.assertions(1)

const connection = getMockConnection()
mockFunction(connectionRepository.getById).mockReturnValue(Promise.resolve(connection))

const { message } = await connectionService.createRequest('test', { myImageUrl: 'custom-image-url' })

expect(message.imageUrl).toBe('custom-image-url')
})

it(`throws an error when connection role is ${ConnectionRole.Inviter} and not ${ConnectionRole.Invitee}`, async () => {
expect.assertions(1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export class ConnectionService {
autoAcceptConnection?: boolean
alias?: string
multiUseInvitation?: boolean
myLabel?: string
myImageUrl?: string
}): Promise<ConnectionProtocolMsgReturnType<ConnectionInvitationMessage>> {
// TODO: public did
const connectionRecord = await this.createConnection({
Expand All @@ -83,11 +85,11 @@ export class ConnectionService {
const { didDoc } = connectionRecord
const [service] = didDoc.didCommServices
const invitation = new ConnectionInvitationMessage({
label: this.config.label,
label: config?.myLabel ?? this.config.label,
recipientKeys: service.recipientKeys,
serviceEndpoint: service.serviceEndpoint,
routingKeys: service.routingKeys,
imageUrl: this.config.connectionImageUrl,
imageUrl: config?.myImageUrl ?? this.config.connectionImageUrl,
})

connectionRecord.invitation = invitation
Expand Down Expand Up @@ -152,19 +154,26 @@ export class ConnectionService {
* Create a connection request message for the connection with the specified connection id.
*
* @param connectionId the id of the connection for which to create a connection request
* @param config config for creation of connection request
* @returns outbound message containing connection request
*/
public async createRequest(connectionId: string): Promise<ConnectionProtocolMsgReturnType<ConnectionRequestMessage>> {
public async createRequest(
connectionId: string,
config?: {
myLabel?: string
myImageUrl?: string
}
): Promise<ConnectionProtocolMsgReturnType<ConnectionRequestMessage>> {
const connectionRecord = await this.connectionRepository.getById(connectionId)

connectionRecord.assertState(ConnectionState.Invited)
connectionRecord.assertRole(ConnectionRole.Invitee)

const connectionRequest = new ConnectionRequestMessage({
label: this.config.label,
label: config?.myLabel ?? this.config.label,
did: connectionRecord.did,
didDoc: connectionRecord.didDoc,
imageUrl: this.config.connectionImageUrl,
imageUrl: config?.myImageUrl ?? this.config.connectionImageUrl,
})

await this.updateState(connectionRecord, ConnectionState.Requested)
Expand Down