tests: vaults (#2365)

* tests: signature tests

* tests: asymmetric

* feat: delete contact use case

* chore: lint

* chore: lint
This commit is contained in:
Mo
2023-07-24 14:28:28 -05:00
committed by GitHub
parent d6bcc808d5
commit 98c0228139
40 changed files with 649 additions and 145 deletions

View File

@@ -1,3 +1,4 @@
import { SessionsClientInterface } from './../Session/SessionsClientInterface'
import { EncryptionProviderInterface } from './../Encryption/EncryptionProviderInterface'
import { GetUntrustedPayload } from './UseCase/GetUntrustedPayload'
import { GetInboundMessages } from './UseCase/GetInboundMessages'
@@ -31,6 +32,7 @@ describe('AsymmetricMessageService', () => {
let sync: jest.Mocked<SyncServiceInterface>
let mutator: jest.Mocked<MutatorClientInterface>
let encryption: jest.Mocked<EncryptionProviderInterface>
let sessions: jest.Mocked<SessionsClientInterface>
let service: AsymmetricMessageService
beforeEach(() => {
@@ -60,9 +62,10 @@ describe('AsymmetricMessageService', () => {
eventBus.addEventHandler = jest.fn()
service = new AsymmetricMessageService(
messageServer,
encryption,
mutator,
sessions,
messageServer,
createOrEditContact,
findContact,
getAllContacts,

View File

@@ -1,3 +1,4 @@
import { SessionsClientInterface } from './../Session/SessionsClientInterface'
import { MutatorClientInterface } from './../Mutator/MutatorClientInterface'
import { AsymmetricMessageServerHash, ClientDisplayableError, isClientDisplayableError } from '@standardnotes/responses'
import { SyncEvent, SyncEventReceivedAsymmetricMessagesData } from '../Event/SyncEvent'
@@ -39,9 +40,10 @@ export class AsymmetricMessageService
implements AsymmetricMessageServiceInterface, InternalEventHandlerInterface
{
constructor(
private messageServer: AsymmetricMessageServer,
private encryption: EncryptionProviderInterface,
private mutator: MutatorClientInterface,
private sessions: SessionsClientInterface,
private messageServer: AsymmetricMessageServer,
private _createOrEditContact: CreateOrEditContact,
private _findContact: FindContact,
private _getAllContacts: GetAllContacts,
@@ -184,10 +186,6 @@ export class AsymmetricMessageService
message: AsymmetricMessageServerHash,
payload: AsymmetricMessagePayload,
): Promise<void> {
if (payload.data.recipientUuid !== message.user_uuid) {
return
}
if (payload.type === AsymmetricMessagePayloadType.ContactShare) {
await this.handleTrustedContactShareMessage(message, payload)
} else if (payload.type === AsymmetricMessagePayloadType.SenderKeypairChanged) {
@@ -225,6 +223,7 @@ export class AsymmetricMessageService
const result = this._getTrustedPayload.execute({
privateKey: this.encryption.getKeyPair().privateKey,
sender: contact.getValue(),
ownUserUuid: this.sessions.userUuid,
message,
})

View File

@@ -0,0 +1,63 @@
import { AsymmetricMessageServerHash } from '@standardnotes/responses'
import { AsymmetricMessagePayload, TrustedContactInterface } from '@standardnotes/models'
import { DecryptMessage } from '../../Encryption/UseCase/Asymmetric/DecryptMessage'
import { Result } from '@standardnotes/domain-core'
import { GetTrustedPayload } from './GetTrustedPayload'
describe('GetTrustedPayload', () => {
let decryptMessage: jest.Mocked<DecryptMessage>
let getTrustedPayload: GetTrustedPayload
beforeEach(() => {
decryptMessage = {} as jest.Mocked<DecryptMessage>
decryptMessage.execute = jest.fn()
getTrustedPayload = new GetTrustedPayload(decryptMessage)
})
describe('execute', () => {
const mockDto = {
privateKey: 'test-private-key',
message: {} as AsymmetricMessageServerHash,
sender: {} as TrustedContactInterface,
ownUserUuid: 'test-user-uuid',
}
it('should return failure when decryption fails', () => {
decryptMessage.execute = jest.fn().mockReturnValue(Result.fail('Decryption failed'))
const result = getTrustedPayload.execute(mockDto)
expect(result.isFailed()).toBe(true)
expect(result.getError()).toBe('Decryption failed')
})
it('should return failure when recipientUuid is not equal to ownUserUuid', () => {
const mockPayload: AsymmetricMessagePayload = {
data: {
recipientUuid: 'another-user-uuid',
},
} as AsymmetricMessagePayload
decryptMessage.execute = jest.fn().mockReturnValue(Result.ok(mockPayload))
const result = getTrustedPayload.execute(mockDto)
expect(result.isFailed()).toBe(true)
expect(result.getError()).toBe('Message is not for this user')
})
it('should return success when recipientUuid is equal to ownUserUuid', () => {
const mockPayload: AsymmetricMessagePayload = {
data: {
recipientUuid: 'test-user-uuid',
},
} as AsymmetricMessagePayload
decryptMessage.execute = jest.fn().mockReturnValue(Result.ok(mockPayload))
const result = getTrustedPayload.execute(mockDto)
expect(result.isFailed()).toBe(false)
expect(result.getValue()).toBe(mockPayload)
})
})
})

View File

@@ -10,6 +10,7 @@ export class GetTrustedPayload implements SyncUseCaseInterface<AsymmetricMessage
privateKey: string
message: AsymmetricMessageServerHash
sender: TrustedContactInterface
ownUserUuid: string
}): Result<M> {
const result = this.decryptMessage.execute<M>({
message: dto.message.encrypted_message,
@@ -17,6 +18,16 @@ export class GetTrustedPayload implements SyncUseCaseInterface<AsymmetricMessage
privateKey: dto.privateKey,
})
if (result.isFailed()) {
return result
}
const recipientUuid = result.getValue().data.recipientUuid
if (recipientUuid !== dto.ownUserUuid) {
return Result.fail('Message is not for this user')
}
return result
}
}