feat: refactor settings (#890)

* feat: refactor settings

* fix verify mfa specs and data source metadata

* fix: compound index field names

* fix metadata binding for typeorm repository

* fix responses to preserve e2e

* fixes for e2e

* fix recovery codes e2e

* add missing specs
This commit is contained in:
Karol Sójko
2023-10-26 12:42:30 +02:00
committed by GitHub
parent a5e019e290
commit 1b5078eb96
175 changed files with 4155 additions and 7701 deletions
@@ -1,20 +1,20 @@
import { Result } from '@standardnotes/domain-core'
import { Setting } from '../../Setting/Setting'
import { SettingServiceInterface } from '../../Setting/SettingServiceInterface'
import { KeyParamsFactoryInterface } from '../../User/KeyParamsFactoryInterface'
import { PKCERepositoryInterface } from '../../User/PKCERepositoryInterface'
import { User } from '../../User/User'
import { UserRepositoryInterface } from '../../User/UserRepositoryInterface'
import { GetSetting } from '../GetSetting/GetSetting'
import { GetUserKeyParamsRecovery } from './GetUserKeyParamsRecovery'
describe('GetUserKeyParamsRecovery', () => {
let keyParamsFactory: KeyParamsFactoryInterface
let userRepository: UserRepositoryInterface
let settingService: SettingServiceInterface
let getSetting: GetSetting
let user: User
let pkceRepository: PKCERepositoryInterface
const createUseCase = () =>
new GetUserKeyParamsRecovery(keyParamsFactory, userRepository, pkceRepository, settingService)
const createUseCase = () => new GetUserKeyParamsRecovery(keyParamsFactory, userRepository, pkceRepository, getSetting)
beforeEach(() => {
keyParamsFactory = {} as jest.Mocked<KeyParamsFactoryInterface>
@@ -26,8 +26,10 @@ describe('GetUserKeyParamsRecovery', () => {
userRepository = {} as jest.Mocked<UserRepositoryInterface>
userRepository.findOneByUsernameOrEmail = jest.fn().mockReturnValue(user)
settingService = {} as jest.Mocked<SettingServiceInterface>
settingService.findSettingWithDecryptedValue = jest.fn().mockReturnValue({ value: 'foo' } as Setting)
getSetting = {} as jest.Mocked<GetSetting>
getSetting.execute = jest
.fn()
.mockReturnValue(Result.ok({ setting: {} as jest.Mocked<Setting>, decryptedValue: 'foo' }))
pkceRepository = {} as jest.Mocked<PKCERepositoryInterface>
pkceRepository.storeCodeChallenge = jest.fn()
@@ -80,7 +82,7 @@ describe('GetUserKeyParamsRecovery', () => {
})
it('should return error if user has no recovery codes generated', async () => {
settingService.findSettingWithDecryptedValue = jest.fn().mockReturnValue(null)
getSetting.execute = jest.fn().mockReturnValue(Result.fail('not found'))
const result = await createUseCase().execute({
username: 'username',
@@ -7,14 +7,14 @@ import { UserRepositoryInterface } from '../../User/UserRepositoryInterface'
import { GetUserKeyParamsRecoveryDTO } from './GetUserKeyParamsRecoveryDTO'
import { User } from '../../User/User'
import { PKCERepositoryInterface } from '../../User/PKCERepositoryInterface'
import { SettingServiceInterface } from '../../Setting/SettingServiceInterface'
import { GetSetting } from '../GetSetting/GetSetting'
export class GetUserKeyParamsRecovery implements UseCaseInterface<KeyParamsData> {
constructor(
private keyParamsFactory: KeyParamsFactoryInterface,
private userRepository: UserRepositoryInterface,
private pkceRepository: PKCERepositoryInterface,
private settingService: SettingServiceInterface,
private getSetting: GetSetting,
) {}
async execute(dto: GetUserKeyParamsRecoveryDTO): Promise<Result<KeyParamsData>> {
@@ -39,15 +39,18 @@ export class GetUserKeyParamsRecovery implements UseCaseInterface<KeyParamsData>
return Result.ok(this.keyParamsFactory.createPseudoParams(username.value))
}
const recoveryCodesSetting = await this.settingService.findSettingWithDecryptedValue({
settingName: SettingName.create(SettingName.NAMES.RecoveryCodes).getValue(),
const recoveryCodesSettingOrError = await this.getSetting.execute({
settingName: SettingName.NAMES.RecoveryCodes,
userUuid: user.uuid,
allowSensitiveRetrieval: true,
decrypted: true,
})
if (!recoveryCodesSetting) {
if (recoveryCodesSettingOrError.isFailed()) {
return Result.fail('User does not have recovery codes generated')
}
if (recoveryCodesSetting.value !== dto.recoveryCodes) {
const recoveryCodesSetting = recoveryCodesSettingOrError.getValue()
if (recoveryCodesSetting.decryptedValue !== dto.recoveryCodes) {
return Result.fail('Invalid recovery codes')
}