diff options
Diffstat (limited to 'spec/frontend/issuable/issuable_form_spec.js')
-rw-r--r-- | spec/frontend/issuable/issuable_form_spec.js | 69 |
1 files changed, 52 insertions, 17 deletions
diff --git a/spec/frontend/issuable/issuable_form_spec.js b/spec/frontend/issuable/issuable_form_spec.js index 3e778e50fb8..d7e5f9083b0 100644 --- a/spec/frontend/issuable/issuable_form_spec.js +++ b/spec/frontend/issuable/issuable_form_spec.js @@ -3,11 +3,18 @@ import Autosave from '~/autosave'; import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures'; import IssuableForm from '~/issuable/issuable_form'; import setWindowLocation from 'helpers/set_window_location_helper'; - +import { confirmSensitiveAction, i18n } from '~/lib/utils/secret_detection'; import { getSaveableFormChildren } from './helpers'; jest.mock('~/autosave'); +jest.mock('~/lib/utils/secret_detection', () => { + return { + ...jest.requireActual('~/lib/utils/secret_detection'), + confirmSensitiveAction: jest.fn(() => Promise.resolve(false)), + }; +}); + const createIssuable = (form) => { return new IssuableForm(form); }; @@ -35,16 +42,13 @@ describe('IssuableForm', () => { describe('autosave', () => { let $title; - let $description; beforeEach(() => { $title = $form.find('input[name*="[title]"]').get(0); - $description = $form.find('textarea[name*="[description]"]').get(0); }); afterEach(() => { $title = null; - $description = null; }); describe('initAutosave', () => { @@ -64,11 +68,6 @@ describe('IssuableForm', () => { ['/foo', 'bar=true', 'title'], 'autosave//foo/bar=true=title', ); - expect(Autosave).toHaveBeenCalledWith( - $description, - ['/foo', 'bar=true', 'description'], - 'autosave//foo/bar=true=description', - ); }); it("creates autosave fields without the searchTerm if it's an issue new form", () => { @@ -81,11 +80,6 @@ describe('IssuableForm', () => { ['/issues/new', '', 'title'], 'autosave//issues/new/bar=true=title', ); - expect(Autosave).toHaveBeenCalledWith( - $description, - ['/issues/new', '', 'description'], - 'autosave//issues/new/bar=true=description', - ); }); it.each([ @@ -106,7 +100,9 @@ describe('IssuableForm', () => { const children = getSaveableFormChildren($form[0]); - expect(Autosave).toHaveBeenCalledTimes(children.length); + // description autosave is being handled separately + // hence we're using children.length - 1 + expect(Autosave).toHaveBeenCalledTimes(children.length - 1); expect(Autosave).toHaveBeenLastCalledWith( $input.get(0), ['/', '', id], @@ -116,13 +112,12 @@ describe('IssuableForm', () => { }); describe('resetAutosave', () => { - it('calls reset on title and description', () => { + it('calls reset on title', () => { instance = createIssuable($form); instance.resetAutosave(); expect(instance.autosaves.get('title').reset).toHaveBeenCalledTimes(1); - expect(instance.autosaves.get('description').reset).toHaveBeenCalledTimes(1); }); it('resets autosave when submit', () => { @@ -245,4 +240,44 @@ describe('IssuableForm', () => { ); }); }); + + describe('Checks for sensitive token', () => { + let issueDescription; + const sensitiveMessage = 'token: glpat-1234567890abcdefghij'; + + beforeEach(() => { + issueDescription = $form.find('textarea[name*="[description]"]').get(0); + }); + + afterEach(() => { + issueDescription = null; + }); + + it('submits the form when no token is present', () => { + issueDescription.value = 'sample message'; + + const handleSubmit = jest.spyOn(IssuableForm.prototype, 'handleSubmit'); + const resetAutosave = jest.spyOn(IssuableForm.prototype, 'resetAutosave'); + createIssuable($form); + + $form.submit(); + + expect(handleSubmit).toHaveBeenCalled(); + expect(resetAutosave).toHaveBeenCalled(); + }); + + it('prevents form submission when token is present', () => { + issueDescription.value = sensitiveMessage; + + const handleSubmit = jest.spyOn(IssuableForm.prototype, 'handleSubmit'); + const resetAutosave = jest.spyOn(IssuableForm.prototype, 'resetAutosave'); + createIssuable($form); + + $form.submit(); + + expect(handleSubmit).toHaveBeenCalled(); + expect(confirmSensitiveAction).toHaveBeenCalledWith(i18n.descriptionPrompt); + expect(resetAutosave).not.toHaveBeenCalled(); + }); + }); }); |