-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ui: ui: create unique component only for private key editing
Signed-off-by: Leonardo R.S. Joao <leonardo.joao@ossystems.com.br>
- Loading branch information
1 parent
9d59e4b
commit 256ab3f
Showing
4 changed files
with
369 additions
and
6 deletions.
There are no files selected for viewing
181 changes: 181 additions & 0 deletions
181
ui/src/components/private_key/PrivateKeyFormDialogEdit.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,181 @@ | ||
<template> | ||
<fragment> | ||
<v-list-item-icon class="mr-0"> | ||
<v-icon | ||
left | ||
data-test="edit-icon" | ||
v-text="'edit'" | ||
/> | ||
</v-list-item-icon> | ||
|
||
<v-list-item-content> | ||
<v-list-item-title | ||
class="text-left" | ||
data-test="edit-title" | ||
v-text="'Edit'" | ||
/> | ||
</v-list-item-content> | ||
|
||
<v-dialog | ||
v-model="showDialog" | ||
max-width="400" | ||
@click:outside="close" | ||
> | ||
<v-card data-test="privateKeyFormDialog-card"> | ||
<v-card-title | ||
class="headline primary" | ||
data-test="text-title" | ||
v-text="'Edit Private Key'" | ||
/> | ||
|
||
<ValidationObserver | ||
ref="obs" | ||
v-slot="{ passes }" | ||
> | ||
<v-card-text> | ||
<ValidationProvider | ||
v-slot="{ errors }" | ||
ref="providerName" | ||
vid="name" | ||
name="Name" | ||
rules="required" | ||
> | ||
<v-text-field | ||
v-model="privateKey.name" | ||
label="Name" | ||
:error-messages="errors" | ||
required | ||
data-test="name-field" | ||
/> | ||
</ValidationProvider> | ||
|
||
<ValidationProvider | ||
v-slot="{ errors }" | ||
ref="providerData" | ||
vid="key" | ||
name="Data" | ||
:rules="'required|parseKey:private'" | ||
:disabled="true" | ||
> | ||
<v-textarea | ||
v-model="privateKey.data" | ||
label="Data" | ||
:error-messages="errors" | ||
required | ||
:messages="supportedKeys" | ||
:disabled="true" | ||
data-test="data-field" | ||
/> | ||
</ValidationProvider> | ||
</v-card-text> | ||
|
||
<v-card-actions> | ||
<v-spacer /> | ||
|
||
<v-btn | ||
text | ||
data-test="cancel-btn" | ||
@click="close" | ||
v-text="'Cancel'" | ||
/> | ||
|
||
<v-btn | ||
text | ||
data-test="edit-btn" | ||
@click="passes(edit)" | ||
v-text="'Edit'" | ||
/> | ||
</v-card-actions> | ||
</ValidationObserver> | ||
</v-card> | ||
</v-dialog> | ||
</fragment> | ||
</template> | ||
|
||
<script> | ||
import { | ||
ValidationObserver, | ||
ValidationProvider, | ||
} from 'vee-validate'; | ||
export default { | ||
name: 'PublicKeyFormDialogAdd', | ||
components: { | ||
ValidationProvider, | ||
ValidationObserver, | ||
}, | ||
props: { | ||
keyObject: { | ||
type: Object, | ||
required: true, | ||
default: Object, | ||
}, | ||
show: { | ||
type: Boolean, | ||
required: true, | ||
}, | ||
}, | ||
data() { | ||
return { | ||
privateKey: { | ||
name: '', | ||
data: '', | ||
}, | ||
supportedKeys: 'Supports RSA, DSA, ECDSA (nistp-*) and ED25519 key types, in PEM (PKCS#1, PKCS#8) and OpenSSH formats.', | ||
}; | ||
}, | ||
computed: { | ||
showDialog: { | ||
get() { | ||
return this.show; | ||
}, | ||
set(value) { | ||
this.$emit('update:show', value); | ||
}, | ||
}, | ||
}, | ||
async updated() { | ||
await this.setLocalVariable(); | ||
}, | ||
methods: { | ||
setLocalVariable() { | ||
this.privateKey = { ...this.keyObject }; | ||
}, | ||
async edit() { | ||
try { | ||
await this.$store.dispatch('privatekeys/edit', this.privateKey); | ||
this.$store.dispatch('snackbar/showSnackbarSuccessNotRequest', this.$success.privateKeyEditing); | ||
this.update(); | ||
} catch (error) { | ||
if (error.message === 'name') { | ||
this.$refs.obs.setErrors({ | ||
name: ['The name already exists'], | ||
}); | ||
} else { | ||
this.$store.dispatch('snackbar/showSnackbarErrorNotRequest', this.$errors.snackbar.privateKeyEditing); | ||
} | ||
} | ||
}, | ||
update() { | ||
this.$emit('update'); | ||
this.close(); | ||
}, | ||
close() { | ||
this.$emit('update:show', false); | ||
this.$refs.obs.reset(); | ||
}, | ||
}, | ||
}; | ||
</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
161 changes: 161 additions & 0 deletions
161
ui/tests/unit/components/private_key/PrivateKeyFormDialogEdit.spec.js
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,161 @@ | ||
import Vuex from 'vuex'; | ||
import { mount, createLocalVue } from '@vue/test-utils'; | ||
import { ValidationProvider, ValidationObserver } from 'vee-validate'; | ||
import flushPromises from 'flush-promises'; | ||
import Vuetify from 'vuetify'; | ||
import PrivateKeyFormDialogEdit from '@/components/private_key/PrivateKeyFormDialogEdit'; | ||
import '@/vee-validate'; | ||
|
||
describe('PrivateKeyFormDialogEdit', () => { | ||
const localVue = createLocalVue(); | ||
const vuetify = new Vuetify(); | ||
localVue.use(Vuex); | ||
localVue.component('ValidationProvider', ValidationProvider); | ||
localVue.component('ValidationObserver', ValidationObserver); | ||
|
||
document.body.setAttribute('data-app', true); | ||
|
||
let wrapper; | ||
|
||
const keyObject = { | ||
name: 'ShellHub', | ||
data: '', | ||
}; | ||
|
||
const privateKey = { | ||
name: '', | ||
data: '', | ||
}; | ||
|
||
const tests = [ | ||
{ | ||
description: 'Dialog closed', | ||
props: { | ||
keyObject, | ||
show: false, | ||
}, | ||
data: { | ||
privateKey, | ||
supportedKeys: 'Supports RSA, DSA, ECDSA (nistp-*) and ED25519 key types, in PEM (PKCS#1, PKCS#8) and OpenSSH formats.', | ||
}, | ||
template: { | ||
'edit-icon': true, | ||
'edit-title': true, | ||
'privateKeyFormDialog-card': false, | ||
}, | ||
templateText: { | ||
'edit-title': 'Edit', | ||
}, | ||
}, | ||
{ | ||
description: 'Dialog opened', | ||
props: { | ||
keyObject, | ||
show: true, | ||
}, | ||
data: { | ||
privateKey, | ||
supportedKeys: 'Supports RSA, DSA, ECDSA (nistp-*) and ED25519 key types, in PEM (PKCS#1, PKCS#8) and OpenSSH formats.', | ||
}, | ||
template: { | ||
'edit-icon': true, | ||
'edit-title': true, | ||
'privateKeyFormDialog-card': true, | ||
'text-title': true, | ||
'name-field': true, | ||
'data-field': true, | ||
'cancel-btn': true, | ||
'edit-btn': true, | ||
}, | ||
templateText: { | ||
'edit-title': 'Edit', | ||
'cancel-btn': 'Cancel', | ||
'edit-btn': 'Edit', | ||
}, | ||
}, | ||
]; | ||
|
||
const storeVuex = () => new Vuex.Store({ | ||
namespaced: true, | ||
state: { }, | ||
getters: { }, | ||
actions: { | ||
'privatekeys/set': () => {}, | ||
'snackbar/showSnackbarSuccessNotRequest': () => {}, | ||
'snackbar/showSnackbarErrorNotRequest': () => {}, | ||
}, | ||
}); | ||
|
||
tests.forEach((test) => { | ||
describe(`${test.description}`, () => { | ||
beforeEach(() => { | ||
wrapper = mount(PrivateKeyFormDialogEdit, { | ||
store: storeVuex(), | ||
localVue, | ||
stubs: ['fragment'], | ||
propsData: { | ||
keyObject: test.props.keyObject, | ||
show: test.props.show, | ||
}, | ||
vuetify, | ||
}); | ||
}); | ||
|
||
/////// | ||
// Component Rendering | ||
////// | ||
|
||
it('Is a Vue instance', () => { | ||
expect(wrapper).toBeTruthy(); | ||
}); | ||
it('Renders the component', () => { | ||
expect(wrapper.html()).toMatchSnapshot(); | ||
}); | ||
|
||
/////// | ||
// Data checking | ||
////// | ||
|
||
it('Receive data in props', () => { | ||
Object.keys(test.props).forEach((item) => { | ||
expect(wrapper.vm[item]).toEqual(test.props[item]); | ||
}); | ||
}); | ||
it('Compare data with default value', () => { | ||
Object.keys(test.data).forEach((item) => { | ||
expect(wrapper.vm[item]).toEqual(test.data[item]); | ||
}); | ||
}); | ||
|
||
////// | ||
// HTML validation | ||
////// | ||
|
||
it('Renders the template with data', () => { | ||
Object.keys(test.template).forEach((item) => { | ||
expect(wrapper.find(`[data-test="${item}"]`).exists()).toBe(test.template[item]); | ||
}); | ||
}); | ||
it('Renders template with expected text', () => { | ||
Object.keys(test.templateText).forEach((item) => { | ||
expect(wrapper.find(`[data-test="${item}"]`).text()).toContain(test.templateText[item]); | ||
}); | ||
}); | ||
if (test.props.show) { | ||
it('Show validation messages', async () => { | ||
////// | ||
// In this case, the empty fields are validated. | ||
////// | ||
|
||
wrapper.setData({ privateKey: { name: '' } }); | ||
await flushPromises(); | ||
|
||
const validatorName = wrapper.vm.$refs.providerName; | ||
|
||
await validatorName.validate(); | ||
expect(validatorName.errors[0]).toBe('This field is required'); | ||
}); | ||
} | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.