feat(snjs): add getting recovery codes (#2132)
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { AuthClientInterface } from '@standardnotes/services'
|
||||
import { SettingsClientInterface } from '@Lib/Services/Settings/SettingsClientInterface'
|
||||
|
||||
import { GetRecoveryCodes } from './GetRecoveryCodes'
|
||||
|
||||
describe('GetRecoveryCodes', () => {
|
||||
let authClient: AuthClientInterface
|
||||
let settingsClient: SettingsClientInterface
|
||||
|
||||
const createUseCase = () => new GetRecoveryCodes(authClient, settingsClient)
|
||||
|
||||
beforeEach(() => {
|
||||
authClient = {} as jest.Mocked<AuthClientInterface>
|
||||
authClient.generateRecoveryCodes = jest.fn().mockResolvedValue('recovery-codes')
|
||||
|
||||
settingsClient = {} as jest.Mocked<SettingsClientInterface>
|
||||
settingsClient.getSetting = jest.fn().mockResolvedValue('existing-recovery-codes')
|
||||
})
|
||||
|
||||
it('should return existing recovery codes if they exist', async () => {
|
||||
const useCase = createUseCase()
|
||||
|
||||
const result = await useCase.execute()
|
||||
|
||||
expect(result.getValue()).toBe('existing-recovery-codes')
|
||||
})
|
||||
|
||||
it('should generate recovery codes if they do not exist', async () => {
|
||||
settingsClient.getSetting = jest.fn().mockResolvedValue(undefined)
|
||||
|
||||
const useCase = createUseCase()
|
||||
|
||||
const result = await useCase.execute()
|
||||
|
||||
expect(result.getValue()).toBe('recovery-codes')
|
||||
})
|
||||
|
||||
it('should return error if recovery codes could not be generated', async () => {
|
||||
settingsClient.getSetting = jest.fn().mockResolvedValue(undefined)
|
||||
authClient.generateRecoveryCodes = jest.fn().mockResolvedValue(false)
|
||||
|
||||
const useCase = createUseCase()
|
||||
|
||||
const result = await useCase.execute()
|
||||
|
||||
expect(result.isFailed()).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,23 @@
|
||||
import { AuthClientInterface } from '@standardnotes/services'
|
||||
import { Result, UseCaseInterface } from '@standardnotes/domain-core'
|
||||
import { SettingName } from '@standardnotes/settings'
|
||||
|
||||
import { SettingsClientInterface } from '@Lib/Services/Settings/SettingsClientInterface'
|
||||
|
||||
export class GetRecoveryCodes implements UseCaseInterface<string> {
|
||||
constructor(private authClient: AuthClientInterface, private settingsClient: SettingsClientInterface) {}
|
||||
|
||||
async execute(): Promise<Result<string>> {
|
||||
const existingRecoveryCodes = await this.settingsClient.getSetting(SettingName.RecoveryCodes)
|
||||
if (existingRecoveryCodes !== undefined) {
|
||||
return Result.ok(existingRecoveryCodes)
|
||||
}
|
||||
|
||||
const generatedRecoveryCodes = await this.authClient.generateRecoveryCodes()
|
||||
if (generatedRecoveryCodes === false) {
|
||||
return Result.fail('Could not generate recovery codes')
|
||||
}
|
||||
|
||||
return Result.ok(generatedRecoveryCodes)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user