Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FORMS-16007 : rest endpoint url validation, to not allow relative url #1505

Merged
merged 4 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,18 @@
if(restEndPointSource.length == 0 || isPostUrlSelected){
Utils.showComponent(restEndPointUrlTextBox, 'div');
Utils.hideComponent(restEndpointConfigPath, 'div');
restEndPointUrlTextBox?.setAttribute("data-rest-endpoint-url-validation", "");
} else {
Utils.showComponent(restEndpointConfigPath, 'div');
Utils.hideComponent(restEndPointUrlTextBox, 'div');
restEndPointUrlTextBox?.removeAttribute("data-rest-endpoint-url-validation");
}
} else {
Utils.hideComponent(restEndPointSource, 'div');
Utils.hideComponent(restEndPointUrlTextBox, 'div');
Utils.hideComponent(restEndpointConfigPath, 'div');
restEndPointSource.parent('div').parent('div').hide();
restEndPointUrlTextBox?.removeAttribute("data-rest-endpoint-url-validation");
}
}

Expand Down Expand Up @@ -421,6 +424,7 @@
$(document).off('change' + REST_ENDPOINT).on('change' + REST_ENDPOINT, restCheckBox, function(){
showPostUrlTextField(dialog);
});
registerRestEndpointUrlValidator();
}
}

Expand Down Expand Up @@ -489,6 +493,22 @@
}
});

function registerRestEndpointUrlValidator() {
$(window).adaptTo("foundation-registry").register("foundation.validation.validator", {
selector: "[data-rest-endpoint-url-validation]",
validate: (el) => {
const url = el.value;
// Regex to validate absolute URLs starting with http:// or https:// only
const absoluteUrlPattern = /^https?:\/\/.+$/i;
if (!absoluteUrlPattern.test(url)) {
return Granite.I18n.getMessage(
"Please enter the absolute path of the REST endpoint."
);
}
}
});
}

Utils.initializeEditDialog(EDIT_DIALOG_FORM)(handleAsyncSubmissionAndThankYouOption, handleSubmitAction,
registerSubmitActionSubDialogClientLibs, registerRestEndPointDialogClientlibs, registerFDMDialogClientlibs, registerEmailDialogClientlibs, initialiseDataModel, registerAutoSaveDialogAction);

Expand Down
24 changes: 21 additions & 3 deletions ui.tests/test-module/specs/formcontainer.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@ describe('Page/Form Authoring', function () {
cy.get("coral-numberinput[name='./fd:autoSaveInterval']").should("exist");
}


const checkValidatorFunctioning = function(formContainerEditPathSelector) {
cy.openEditableToolbar(sitesSelectors.overlays.overlay.component + formContainerEditPathSelector);
cy.invokeEditableAction("[data-action='CONFIGURE']");
cy.get('.cmp-adaptiveform-container__editdialog').contains('Submission').click({force:true});
cy.get(".cmp-adaptiveform-container__submitaction").children('button[is="coral-button"][aria-haspopup="listbox"]').first().click({force: true});
cy.get('coral-selectlist-item[value="fd/af/components/guidesubmittype/restendpoint"]').should('be.visible').click();
cy.get("[name='./restEndpointPostUrl']").scrollIntoView().clear({force: true}).type("invalid-url", {force: true});
cy.get('.coral-Form-errorlabel').should('contain.text', "Please enter the absolute path of the REST endpoint.");
cy.get("[name='./restEndpointPostUrl']").clear({force: true}).type("http://localhost:4502/some/endpoint", {force: true});
cy.get('.coral-Form-errorlabel').should('not.exist');
cy.get('.cq-dialog-submit').click();
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test for that error should not exist in case where post check box is not checked

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented!


const checkAndSaveSubmitAction = function(formContainerEditPathSelector) {
// click configure action on adaptive form container component
Expand Down Expand Up @@ -107,11 +118,11 @@ describe('Page/Form Authoring', function () {
cy.get("[name='./restEndpointConfigPath']").should("exist").should("be.visible");
cy.get("coral-radio[name='./restEndPointSource'][value='posturl']").first().click();
cy.get("[name='./restEndpointPostUrl']").should("exist").should("be.visible");
cy.get("[name='./restEndpointPostUrl']").should("exist").clear().type("http://localhost:4502/some/endpoint");
cy.get("[name='./restEndpointConfigPath']").should("exist").should("not.be.visible");
}
cy.get("[name='./restEndpointPostUrl']").should("exist").type("http://localhost:4502/some/endpoint");

//save the configuration
cy.get("[name='./restEndpointPostUrl']").scrollIntoView().should("exist").clear().type("http://localhost:4502/some/endpoint");
cy.get('.cq-dialog-submit').click();
};

Expand Down Expand Up @@ -243,6 +254,9 @@ describe('Page/Form Authoring', function () {
}
});

it('check validator functioning for REST endpoint URL', function() {
checkValidatorFunctioning(formContainerEditPathSelector);
});
});

// commenting once we support adaptive form container in sites editor, uncomment this test
Expand Down Expand Up @@ -296,6 +310,10 @@ describe('Page/Form Authoring', function () {
checkEditDialog(formContainerEditPathSelector);
cy.get(sitesSelectors.confirmDialog.actions.first).click();
})

it('check validator functioning for REST endpoint URL', function() {
checkValidatorFunctioning(formContainerEditPathSelector);
});
});

context("Render Forms in Disabled mode", function () {
Expand Down