feat(snjs): add authenticator use cases (#2145)

* feat(snjs): add authenticator use case

* feat(snjs): add use cases for listing, deleting and verifying authenticators

* fix(snjs): spec for deleting authenticator
This commit is contained in:
Karol Sójko
2023-01-11 11:30:42 +01:00
committed by GitHub
parent 8fe78ecf5f
commit 5864ea84e7
16 changed files with 531 additions and 29 deletions

View File

@@ -0,0 +1,41 @@
import { AuthenticatorClientInterface } from '@standardnotes/services'
import { DeleteAuthenticator } from './DeleteAuthenticator'
describe('DeleteAuthenticator', () => {
let authenticatorClient: AuthenticatorClientInterface
const createUseCase = () => new DeleteAuthenticator(authenticatorClient)
beforeEach(() => {
authenticatorClient = {} as jest.Mocked<AuthenticatorClientInterface>
authenticatorClient.delete = jest.fn().mockReturnValue(true)
})
it('should delete authenticator', async () => {
const useCase = createUseCase()
const result = await useCase.execute({ authenticatorId: '00000000-0000-0000-0000-000000000000' })
expect(result.isFailed()).toBe(false)
})
it('should fail if authenticator id is invalid', async () => {
const useCase = createUseCase()
const result = await useCase.execute({ authenticatorId: 'invalid' })
expect(result.isFailed()).toBe(true)
expect(result.getError()).toBe('Could not delete authenticator: Given value is not a valid uuid: invalid')
})
it('should fail if authenticator client fails to delete authenticator', async () => {
authenticatorClient.delete = jest.fn().mockReturnValue(false)
const useCase = createUseCase()
const result = await useCase.execute({ authenticatorId: '00000000-0000-0000-0000-000000000000' })
expect(result.isFailed()).toBe(true)
expect(result.getError()).toBe('Could not delete authenticator')
})
})

View File

@@ -0,0 +1,23 @@
import { AuthenticatorClientInterface } from '@standardnotes/services'
import { Result, UseCaseInterface, Uuid } from '@standardnotes/domain-core'
import { DeleteAuthenticatorDTO } from './DeleteAuthenticatorDTO'
export class DeleteAuthenticator implements UseCaseInterface<void> {
constructor(private authenticatorClient: AuthenticatorClientInterface) {}
async execute(dto: DeleteAuthenticatorDTO): Promise<Result<void>> {
const authenticatorIdOrError = Uuid.create(dto.authenticatorId)
if (authenticatorIdOrError.isFailed()) {
return Result.fail(`Could not delete authenticator: ${authenticatorIdOrError.getError()}`)
}
const authenticatorId = authenticatorIdOrError.getValue()
const result = await this.authenticatorClient.delete(authenticatorId)
if (!result) {
return Result.fail('Could not delete authenticator')
}
return Result.ok()
}
}

View File

@@ -0,0 +1,3 @@
export interface DeleteAuthenticatorDTO {
authenticatorId: string
}