-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove password logic form modal implementation
Removes the password logic from the modal implementation and uses a custom component instead.
- Loading branch information
1 parent
82ed990
commit 0b88637
Showing
8 changed files
with
188 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/web-app-files/src/components/Modals/SetLinkPasswordModal.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<template> | ||
<oc-text-input | ||
:model-value="password" | ||
:label="$gettext('Password')" | ||
type="password" | ||
:password-policy="inputPasswordPolicy" | ||
:generate-password-method="inputGeneratePasswordMethod" | ||
:fix-message-line="true" | ||
:placeholder="link.password ? '●●●●●●●●' : null" | ||
:error-message="errorMessage" | ||
class="oc-modal-body-input" | ||
@password-challenge-completed="confirmDisabled = false" | ||
@password-challenge-failed="confirmDisabled = true" | ||
@keydown.enter.prevent="onConfirm" | ||
@update:model-value="onInput" | ||
/> | ||
<div class="oc-flex oc-flex-right oc-flex-middle oc-mt-m"> | ||
<oc-button | ||
class="oc-modal-body-actions-cancel oc-ml-s" | ||
appearance="outline" | ||
variation="passive" | ||
@click="onCancel" | ||
>{{ $gettext('Cancel') }} | ||
</oc-button> | ||
<oc-button | ||
class="oc-modal-body-actions-confirm oc-ml-s" | ||
appearance="filled" | ||
variation="primary" | ||
:disabled="confirmDisabled" | ||
@click="onConfirm" | ||
>{{ link.password ? $gettext('Apply') : $gettext('Set') }} | ||
</oc-button> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, ref, unref, PropType } from 'vue' | ||
import { useGettext } from 'vue3-gettext' | ||
import { useClientService, usePasswordPolicyService, useStore } from '@ownclouders/web-pkg' | ||
import { Share } from '@ownclouders/web-client/src/helpers' | ||
export default defineComponent({ | ||
name: 'SetLinkPasswordModal', | ||
props: { | ||
link: { type: Object as PropType<Share>, required: true } | ||
}, | ||
setup(props, { expose }) { | ||
const store = useStore() | ||
const clientService = useClientService() | ||
const passwordPolicyService = usePasswordPolicyService() | ||
const { $gettext } = useGettext() | ||
const password = ref('') | ||
const errorMessage = ref<string>() | ||
const confirmDisabled = ref(true) | ||
const onInput = (value: string) => { | ||
password.value = value | ||
errorMessage.value = undefined | ||
} | ||
const onConfirm = async () => { | ||
try { | ||
await store.dispatch('Files/updateLink', { | ||
id: props.link.id, | ||
client: clientService.owncloudSdk, | ||
params: { ...props.link, password: unref(password) } | ||
}) | ||
store.dispatch('hideModal') | ||
store.dispatch('showMessage', { | ||
title: $gettext('Link was updated successfully') | ||
}) | ||
} catch (e) { | ||
// Human-readable error message is provided, for example when password is on banned list | ||
if (e.statusCode === 400) { | ||
errorMessage.value = $gettext(e.message) | ||
return | ||
} | ||
store.dispatch('showErrorMessage', { | ||
title: $gettext('Failed to update link'), | ||
error: e | ||
}) | ||
} | ||
} | ||
const onCancel = () => { | ||
store.dispatch('hideModal') | ||
} | ||
expose({ onConfirm, onCancel }) | ||
return { | ||
password, | ||
confirmDisabled, | ||
onInput, | ||
onConfirm, | ||
onCancel, | ||
errorMessage, | ||
passwordPolicyService, | ||
inputPasswordPolicy: passwordPolicyService.getPolicy(), | ||
inputGeneratePasswordMethod: () => passwordPolicyService.generatePassword() | ||
} | ||
} | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/web-app-files/tests/unit/components/Modals/SetLinkPasswordModal.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import SetLinkPasswordModal from '../../../../src/components/Modals/SetLinkPasswordModal.vue' | ||
import { | ||
createStore, | ||
defaultComponentMocks, | ||
defaultPlugins, | ||
defaultStoreMockOptions, | ||
shallowMount | ||
} from 'web-test-helpers' | ||
|
||
describe('SetLinkPasswordModal', () => { | ||
it('should render a text input field for the password', () => { | ||
const { wrapper } = getWrapper() | ||
|
||
expect(wrapper.find('oc-text-input-stub').exists()).toBeTruthy() | ||
}) | ||
describe('method "onConfirm"', () => { | ||
it('updates the link', async () => { | ||
const { wrapper, storeOptions } = getWrapper() | ||
await wrapper.vm.onConfirm() | ||
|
||
expect(storeOptions.modules.Files.actions.updateLink).toHaveBeenCalled() | ||
expect(storeOptions.actions.showMessage).toHaveBeenCalled() | ||
}) | ||
it('shows an error message on error', async () => { | ||
const { wrapper, storeOptions } = getWrapper() | ||
storeOptions.modules.Files.actions.updateLink.mockRejectedValue(new Error('')) | ||
await wrapper.vm.onConfirm() | ||
|
||
expect(storeOptions.actions.showErrorMessage).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) | ||
|
||
function getWrapper({ link = {} } = {}) { | ||
const mocks = { ...defaultComponentMocks() } | ||
|
||
const storeOptions = defaultStoreMockOptions | ||
const store = createStore(storeOptions) | ||
|
||
return { | ||
mocks, | ||
storeOptions, | ||
wrapper: shallowMount(SetLinkPasswordModal, { | ||
props: { | ||
link | ||
}, | ||
global: { | ||
plugins: [...defaultPlugins(), store], | ||
mocks, | ||
provide: mocks | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.