|
| 1 | +import { mount } from '@vue/test-utils'; |
| 2 | + |
| 3 | +import DetailText from '@shell/components/DetailText.vue'; |
| 4 | + |
| 5 | +jest.mock('@shell/utils/clipboard', () => ({ copyTextToClipboard: jest.fn() })); |
| 6 | + |
| 7 | +describe('component: DetailText', () => { |
| 8 | + const defaultMocks = { |
| 9 | + $store: { |
| 10 | + getters: { |
| 11 | + 'i18n/t': jest.fn((key: string) => `%${ key }%`), |
| 12 | + 'prefs/get': jest.fn(() => true), |
| 13 | + } |
| 14 | + } |
| 15 | + }; |
| 16 | + |
| 17 | + describe('concealment', () => { |
| 18 | + it('should not render the actual secret value in the content area when concealed', () => { |
| 19 | + const secretValue = 'super-secret-password-xyz'; |
| 20 | + const wrapper = mount(DetailText, { |
| 21 | + props: { |
| 22 | + value: secretValue, |
| 23 | + conceal: true, |
| 24 | + label: 'Password', |
| 25 | + }, |
| 26 | + |
| 27 | + global: { |
| 28 | + mocks: defaultMocks, |
| 29 | + directives: { |
| 30 | + 'clean-html': () => {}, |
| 31 | + 'clean-tooltip': () => {}, |
| 32 | + t: () => {}, |
| 33 | + }, |
| 34 | + stubs: { |
| 35 | + CopyToClipboard: true, |
| 36 | + CodeMirror: true, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + const concealedSpan = wrapper.find('[data-testid="detail-top_html"]'); |
| 42 | + |
| 43 | + expect(concealedSpan.exists()).toBe(true); |
| 44 | + expect(concealedSpan.classes()).toContain('conceal'); |
| 45 | + expect(concealedSpan.text()).not.toContain(secretValue); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should render the actual value when not concealed', () => { |
| 49 | + const visibleValue = 'visible-value-123'; |
| 50 | + const wrapper = mount(DetailText, { |
| 51 | + props: { |
| 52 | + value: visibleValue, |
| 53 | + conceal: false, |
| 54 | + label: 'Data', |
| 55 | + }, |
| 56 | + |
| 57 | + global: { |
| 58 | + mocks: defaultMocks, |
| 59 | + directives: { |
| 60 | + 'clean-html': (el: HTMLElement, binding: { value: string }) => { |
| 61 | + el.innerHTML = binding.value; |
| 62 | + }, |
| 63 | + 'clean-tooltip': () => {}, |
| 64 | + t: () => {}, |
| 65 | + }, |
| 66 | + stubs: { |
| 67 | + CopyToClipboard: true, |
| 68 | + CodeMirror: true, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + const contentSpan = wrapper.find('[data-testid="detail-top_html"]'); |
| 74 | + |
| 75 | + expect(contentSpan.exists()).toBe(true); |
| 76 | + expect(contentSpan.classes()).not.toContain('conceal'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should not render JSON secret values in CodeMirror when concealed', () => { |
| 80 | + const jsonSecret = '{"api_key": "secret-key-123"}'; |
| 81 | + const wrapper = mount(DetailText, { |
| 82 | + props: { |
| 83 | + value: jsonSecret, |
| 84 | + conceal: true, |
| 85 | + label: 'Config', |
| 86 | + }, |
| 87 | + |
| 88 | + global: { |
| 89 | + mocks: defaultMocks, |
| 90 | + directives: { |
| 91 | + 'clean-html': () => {}, |
| 92 | + 'clean-tooltip': () => {}, |
| 93 | + t: () => {}, |
| 94 | + }, |
| 95 | + stubs: { |
| 96 | + CopyToClipboard: true, |
| 97 | + CodeMirror: true, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + const codeMirror = wrapper.findComponent({ name: 'CodeMirror' }); |
| 103 | + |
| 104 | + expect(codeMirror.exists()).toBe(false); |
| 105 | + |
| 106 | + const concealedSpan = wrapper.find('[data-testid="detail-top_html"]'); |
| 107 | + |
| 108 | + expect(concealedSpan.exists()).toBe(true); |
| 109 | + expect(concealedSpan.classes()).toContain('conceal'); |
| 110 | + expect(concealedSpan.text()).not.toContain('secret-key-123'); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments