Skip to content

Commit

Permalink
BACKLOG-22218: Add more test scenarios (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
gflores-jahia authored Jan 18, 2024
1 parent 7759f0d commit 8f8bd1a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
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 @@ describe('HTML rich text filtering', () => {
const textName = 'myText';
const path = `/sites/${siteKey}/contents/${textName}`;

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

before(() => {
createSite(siteKey);
addNode({
Expand All @@ -25,40 +29,55 @@ describe('HTML rich text filtering', () => {
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 @@ describe('HTML rich text filtering', () => {

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}
});
};

0 comments on commit 8f8bd1a

Please sign in to comment.