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

BACKLOG-22218: Replace selenium HtmlFilteringTest test scenarios #12

Merged
merged 1 commit into from
Jan 18, 2024
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
56 changes: 47 additions & 9 deletions tests/cypress/e2e/filtering.cy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {addNode, createSite, deleteSite} from '@jahia/cypress';
import {
addConfig,
disableHtmlFiltering,
enableHtmlFiltering,
getContent,
installConfig,
Expand All @@ -11,6 +13,8 @@
const textName = 'myText';
const path = `/sites/${siteKey}/contents/${textName}`;

// TODO Need to clean up configuration manually (delete in file system) for local re-runs

Check warning on line 16 in tests/cypress/e2e/filtering.cy.ts

View workflow job for this annotation

GitHub Actions / Static Analysis (linting, vulns)

Unexpected 'todo' comment: 'TODO Need to clean up configuration...'

before(() => {
createSite(siteKey);
addNode({
Expand All @@ -25,40 +29,55 @@
deleteSite(siteKey);
});

it('does not apply html filtering when disabled (default)', () => {
modifyContent(path, '<iframe title="My iframe"></iframe>');
it('does not apply html filtering to tag when disabled (default)', () => {
modifyContent(path, '<iframe title="My iframe"></iframe><script> var today= new Date(); </script>');
getContent(path).then(result => {
expect(result.data.jcr.nodeByPath.property.value).to.contain('iframe');
expect(result.data.jcr.nodeByPath.property.value).to.contain('<script>');
});
});

it('does not apply html filtering to attribute when disabled (default)', () => {
modifyContent(path, '<img src="stub.jpg" loading="lazy">');
getContent(path).then(result => {
expect(result.data.jcr.nodeByPath.property.value).to.contain('img');
expect(result.data.jcr.nodeByPath.property.value).to.contain('<img');
expect(result.data.jcr.nodeByPath.property.value).to.contain('src');
expect(result.data.jcr.nodeByPath.property.value).to.contain('loading');
});
});

it('applies default html filtering when enabled', () => {
enableHtmlFiltering(siteKey);
modifyContent(path, '<iframe title="My iframe"></iframe>');
modifyContent(path, '<iframe title="My iframe"></iframe><a title="My link"></a><script> var today= new Date(); </script>');
getContent(path).then(result => {
expect(result.data.jcr.nodeByPath.property.value).to.not.contain('iframe');
expect(result.data.jcr.nodeByPath.property.value).to.not.contain('script');
expect(result.data.jcr.nodeByPath.property.value).to.contain('<a');
});
});

it('applies default html filtering when enabled', () => {
enableHtmlFiltering(siteKey);
modifyContent(path, '<a title="My iframe"></a>');
getContent(path).then(result => {
expect(result.data.jcr.nodeByPath.property.value).to.contain('a');
});
});

it('applies default filtering to html element when enabled', () => {
modifyContent(path, '<iframe title="My iframe"></iframe>');
modifyContent(path, '<iframe title="My iframe"></iframe><p>This is a <u>nested</u>tag</p>');
getContent(path).then(result => {
expect(result.data.jcr.nodeByPath.property.value).to.not.contain('iframe');
expect(result.data.jcr.nodeByPath.property.value).to.not.contain('<iframe>');
expect(result.data.jcr.nodeByPath.property.value).to.contain('<p>');
expect(result.data.jcr.nodeByPath.property.value).to.contain('<u>');
});
});

it('applies default filtering to html attributes when enabled', () => {
modifyContent(path, '<img src="stub.jpg" loading="lazy">');
modifyContent(path, '<img src="stub.jpg" loading="lazy"><a href="https://localhost:8080">');
getContent(path).then(result => {
expect(result.data.jcr.nodeByPath.property.value).to.contain('a');
expect(result.data.jcr.nodeByPath.property.value).to.contain('href');
expect(result.data.jcr.nodeByPath.property.value).to.contain('img');
expect(result.data.jcr.nodeByPath.property.value).to.contain('src');
expect(result.data.jcr.nodeByPath.property.value).to.not.contain('loading', 'filtered attribute');
Expand All @@ -75,11 +94,30 @@

it('can override filtering config for specified site', () => {
installConfig('configs/org.jahia.modules.richtext.config-filteringSite.yml');
modifyContent(path, '<strong>This text is important!</strong>');
modifyContent(path, '<p>This text<strong>is important!</strong> but not this one</p>');
getContent(path).then(result => {
expect(result.data.jcr.nodeByPath.property.value).to.contain('<p>');
expect(result.data.jcr.nodeByPath.property.value).to.not.contain('strong');
expect(result.data.jcr.nodeByPath.property.value).to.contain('important', 'Tag removed but data remains');
});
});

it('can update config and filter using updated rules', () => {
addConfig('htmlFiltering.disallow.elements[1].name', 'i', 'filteringSite');
modifyContent(path, '<p>This text<i>is important!</i> but not this one</p>');
getContent(path).then(result => {
expect(result.data.jcr.nodeByPath.property.value).to.contain('<p>');
expect(result.data.jcr.nodeByPath.property.value).to.not.contain('<i>');
expect(result.data.jcr.nodeByPath.property.value).to.contain('important', 'Tag removed but data remains');
});
});

// TODO Need to clean up configuration manually (delete in file system) for re-runs
it('does not override filtering config for specified site when filtering is disabled', () => {
disableHtmlFiltering(siteKey);
installConfig('configs/org.jahia.modules.richtext.config-filteringSite.yml');
modifyContent(path, '<p>This text<strong>is important!</strong> but not this one</p>');
getContent(path).then(result => {
expect(result.data.jcr.nodeByPath.property.value).to.contain('strong');
});
});
});
21 changes: 20 additions & 1 deletion tests/cypress/fixtures/utils/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import gql from 'graphql-tag';

const pid = 'org.jahia.modules.richtext.config';

export const installConfig = configFilePath => {
return cy.runProvisioningScript(
{fileContent: `- installConfiguration: "${configFilePath}"`, type: 'application/yaml'},
Expand All @@ -8,7 +10,6 @@ export const installConfig = configFilePath => {
};

export const getConfig = siteKey => {
const pid = 'org.jahia.modules.richtext.config';
const getConfigGql = gql`
query getConfig($pid: String!, $siteKey: String!) {
admin {
Expand All @@ -25,3 +26,21 @@ export const getConfig = siteKey => {
variables: {pid, siteKey}
});
};

export const addConfig = (key, value, siteKey = 'default') => {
const addConfigGql = gql`
mutation addConfig($pid: String!, $identifier: String!, $key: String!, $value: String!) {
admin {
jahia {
configuration(pid: $pid, identifier: $identifier) {
value(name: $key value: $value)
}
}
}
}
`;
return cy.apollo({
mutation: addConfigGql,
variables: {pid, identifier: siteKey, key, value}
});
};
Loading