Skip to content

Commit

Permalink
fix: restrict owner redirect test
Browse files Browse the repository at this point in the history
  • Loading branch information
kielllll committed Nov 25, 2024
1 parent 2e3bdeb commit d8553a6
Show file tree
Hide file tree
Showing 2 changed files with 367 additions and 201 deletions.
126 changes: 66 additions & 60 deletions tests/e2e/advanced-checks/pdf-restriction.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { RequestLogger, Role } from 'testcafe';
import { RequestLogger, Role, Selector, t } from 'testcafe';
import { admin, baseURL, editor } from '../auth';
import AdvancedCheck from '../utilities/page-model/helpers/advanced-check';
import Page from '../utilities/page-model/helpers/page';
import PageModel from '../models/Page';
import Pdf from '../utilities/page-model/helpers/pdf';
import { getQueryParam } from '../utilities/page-model/helpers/search-params';

Expand All @@ -10,118 +11,123 @@ let downloadUrl;
const advancedCheck = new AdvancedCheck();
const page = new Page();
const pdf = new Pdf();
const downloadLogger = RequestLogger( downloadUrl, {
const downloadLogger = RequestLogger(downloadUrl, {
logResponseBody: true,
logResponseHeaders: true,
} );
});

fixture`PDF Administrator & Non-Administrator - Restriction Test`;

test( 'should throw an error when a non-administrator user try to access a PDF generated by an admin', async ( t ) => {
test('should throw an error when a non-administrator user try to access a PDF generated by an admin', async (t) => {
// Actions & Assertions
await advancedCheck.navigateSection(
'gf_edit_forms&view=settings&subview=PDF&id=4'
);
pdfId = await advancedCheck.shortcodeBox.getAttribute(
'data-clipboard-text'
);
pdfId = pdfId.substring( 30, 43 );
pdfId = pdfId.substring(30, 43);
await t
.useRole( editor )
.navigateTo( `${ baseURL }?gpdf=1&pid=${ pdfId }&lid=4` )
.expect( advancedCheck.pdfRestrictionErrorMessage.exists )
.useRole(editor)
.navigateTo(`${baseURL}?gpdf=1&pid=${pdfId}&lid=4`)
.expect(advancedCheck.pdfRestrictionErrorMessage.exists)
.ok();
} );

test( "should redirect to WP login page if 'Restrict Owner' is enabled", async ( t ) => {
// Actions & Assertions
await advancedCheck.toggleRestrictOwnerCheckbox(
'gf_edit_forms&view=settings&subview=PDF&id=4'
);
await page.navigatePage();
await page.addNewPage();
await page.navigatePage();
});

test("should redirect to WP login page if 'Restrict Owner' is enabled", async (t) => {
// Actions
// Toggle restrict owner checkbox
await pdf.navigatePdf('4');
// Navigate to pdf info
await t.click(Selector('td.name').find('a').withText('Sample'));
// check if it's publicly accessed then turn off
if (pdf.enablePdfSecurityCheckbox.value === 'Yes') {
await t.click(pdf.enablePublicAccessCheckbox);
}
await t.click(pdf.restrictOwnerCheckbox).click(pdf.saveSettings);

// Create a new page
await t
.click( page.testPageLink )
.click( page.addBlockIcon )
.typeText( page.searchBlock.filterVisible(), 'shortcode', {
paste: true,
} )
.click( page.shortcodeLink )
.typeText(
page.shortcodeTextarea,
.setNativeDialogHandler(() => true)
.navigateTo(`${baseURL}/wp-admin/edit.php?post_type=page`);
const name = 'Restrict Owner Enabled';
const pageModel = new PageModel(name);
await pageModel.add();
await pageModel.insertBlock('shortcode', {
target: '.block-editor-plain-text',
content:
'[gravityform id="4" title="false" description="false" ajax="true" tabindex="49"]',
{ paste: true }
)
.click( page.updateButton );
});
await pageModel.saveChanges();
await pageModel.publishChanges();

pdfId = await getQueryParam( 'post' );
// Visit the page as anon user
pdfId = await getQueryParam('post');

await t
.useRole( Role.anonymous() )
.navigateTo( `${ baseURL }/?page_id=${ pdfId }` )
.typeText( advancedCheck.textInputField, 'texttest', { paste: true } )
.click( advancedCheck.submitButton )
.useRole( admin );
.useRole(Role.anonymous())
.navigateTo(`${baseURL}/?page_id=${pdfId}`)
.typeText(advancedCheck.textInputField, 'texttest', { paste: true })
.click(advancedCheck.submitButton)
.useRole(admin);

await pdf.navigate( 'gf_entries&id=4' );
downloadUrl = await advancedCheck.viewEntryLink.getAttribute( 'href' );
await pdf.navigate('gf_entries&id=4');
downloadUrl = await advancedCheck.viewEntryLink.getAttribute('href');

downloadLogger.clear();

await t
.addRequestHooks( downloadLogger )
.useRole( Role.anonymous() )
.navigateTo( downloadUrl )
.removeRequestHooks( downloadLogger );
.addRequestHooks(downloadLogger)
.useRole(Role.anonymous())
.navigateTo(downloadUrl)
.removeRequestHooks(downloadLogger);

// Assertions
await t
.expect(
downloadLogger.contains(
( r ) =>
r.response.headers[ 'content-type' ] === 'application/pdf'
(r) => r.response.headers['content-type'] === 'application/pdf'
)
)
.notOk()
.expect( advancedCheck.pdfRestrictionErrorMessage.exists )
.expect(advancedCheck.pdfRestrictionErrorMessage.exists)
.notOk()
.expect( advancedCheck.wpLoginForm.exists )
.expect(advancedCheck.wpLoginForm.exists)
.ok();
} );
});

test( 'reset/clean previous tests saved data and ensure PDF can be viewed by default', async ( t ) => {
test('reset/clean previous tests saved data and ensure PDF can be viewed by default', async (t) => {
// Actions & Assertions
await advancedCheck.toggleRestrictOwnerCheckbox(
'gf_edit_forms&view=settings&subview=PDF&id=4'
);

await t
.useRole( Role.anonymous() )
.navigateTo( `${ baseURL }/?page_id=${ pdfId }` );
.useRole(Role.anonymous())
.navigateTo(`${baseURL}/?page_id=${pdfId}`);
await advancedCheck.submitNewPdfEntry();

await t.useRole( admin );
await pdf.navigate( 'gf_entries&id=4' );
await t.useRole(admin);
await pdf.navigate('gf_entries&id=4');

downloadUrl = await advancedCheck.viewEntryLink.getAttribute( 'href' );
downloadUrl = await advancedCheck.viewEntryLink.getAttribute('href');
await page.deleteTestPage();
await t.useRole( Role.anonymous() );
await t.useRole(Role.anonymous());

downloadLogger.clear();

await t
.addRequestHooks( downloadLogger )
.navigateTo( downloadUrl )
.wait( 500 )
.removeRequestHooks( downloadLogger );
.addRequestHooks(downloadLogger)
.navigateTo(downloadUrl)
.wait(500)
.removeRequestHooks(downloadLogger);

// Assertions
await t
.expect(
downloadLogger.contains(
( r ) =>
r.response.headers[ 'content-type' ] === 'application/pdf'
(r) => r.response.headers['content-type'] === 'application/pdf'
)
)
.ok();
} );
});
Loading

0 comments on commit d8553a6

Please sign in to comment.