From 624b2d1687f4de21e72196454018b077e9f56a04 Mon Sep 17 00:00:00 2001 From: Nicholas Ong Date: Tue, 14 Mar 2023 14:26:55 +0800 Subject: [PATCH 1/4] test: redirect to amended file immediately when file is replaced --- test/end-to-end/UrlCreation.test.ts | 42 +++++++++++++++++++++++++++++ test/end-to-end/util/config.ts | 2 ++ 2 files changed, 44 insertions(+) diff --git a/test/end-to-end/UrlCreation.test.ts b/test/end-to-end/UrlCreation.test.ts index 4b827c196..1812106bd 100644 --- a/test/end-to-end/UrlCreation.test.ts +++ b/test/end-to-end/UrlCreation.test.ts @@ -1,11 +1,15 @@ import { Selector } from 'testcafe' +import * as fs from 'fs' import { + apiLocation, circularRedirectUrl, dummyBulkCsv, dummyBulkCsvRelativePath, + dummyChangedFilePath, dummyFilePath, dummyMaliciousFilePath, dummyMaliciousRelativePath, + dummyRelativeChangedFilePath, dummyRelativePath, invalidShortUrl, largeFileSize, @@ -20,6 +24,7 @@ import { blacklistValidationError, bulkTab, circularRedirectValidationError, + closeDrawerButton, createLinkButton, createUrlModal, csvOnlyError, @@ -311,3 +316,40 @@ test('The malicious file test.', async (t) => { await t.expect(linkRow.exists).notOk() }) + +test('The update file test', async (t) => { + await t.click(createLinkButton.nth(0)).click(generateUrlImage) + + const generatedfileUrl = await shortUrlTextField.value + const fileRow = Selector(`h6[title="${generatedfileUrl}"]`) + const directoryPath = `${process.env.HOME}/Downloads/${generatedfileUrl}.pdf` + // Generate 1mb file + await createEmptyFileOfSize(dummyFilePath, smallFileSize) + + await t + .click(fileTab) + .setFilesToUpload(uploadFile, dummyRelativePath) + .click(tagsAutocompleteInput) + .typeText(tagsAutocompleteInput, tagText1) + .pressKey('enter') + .click(createLinkButton.nth(2)) + + await createEmptyFileOfSize(dummyChangedFilePath, smallFileSize) + await t + .click(fileRow) + .setFilesToUpload(uploadFile, dummyRelativeChangedFilePath) + .click(closeDrawerButton) + + await t.navigateTo(`${apiLocation}/${generatedfileUrl}`) + await t.wait(7000) + + await t.expect(fs.existsSync(directoryPath)).ok() + + await deleteFile(dummyFilePath) + await deleteFile(dummyChangedFilePath) + + // Delete downloaded file + fs.unlink(directoryPath, (err) => { + if (err) throw err + }) +}) diff --git a/test/end-to-end/util/config.ts b/test/end-to-end/util/config.ts index 4a1899179..ba84dc9b5 100644 --- a/test/end-to-end/util/config.ts +++ b/test/end-to-end/util/config.ts @@ -16,6 +16,8 @@ export const dummyMaliciousFilePath = './test/end-to-end/eicar.com.txt' export const dummyMaliciousRelativePath = './eicar.com.txt' export const dummyFilePath = './test/end-to-end/anotherDummy.txt' export const dummyRelativePath = './anotherDummy.txt' +export const dummyChangedFilePath = './test/end-to-end/changedDummy.pdf' +export const dummyRelativeChangedFilePath = './changedDummy.pdf' export const dummyBulkCsv = './test/end-to-end/bulkCsv.csv' export const dummyBulkCsvRelativePath = './bulkCsv.csv' export const smallFileSize = 1024 * 1024 * 1 From cf4dabd591adce77b021edd1263ef36844acb875 Mon Sep 17 00:00:00 2001 From: Nicholas Ong Date: Tue, 14 Mar 2023 14:33:47 +0800 Subject: [PATCH 2/4] fix: lint --- test/end-to-end/UrlCreation.test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/end-to-end/UrlCreation.test.ts b/test/end-to-end/UrlCreation.test.ts index 1812106bd..d86743c0b 100644 --- a/test/end-to-end/UrlCreation.test.ts +++ b/test/end-to-end/UrlCreation.test.ts @@ -349,7 +349,5 @@ test('The update file test', async (t) => { await deleteFile(dummyChangedFilePath) // Delete downloaded file - fs.unlink(directoryPath, (err) => { - if (err) throw err - }) + fs.unlink(directoryPath, () => {}) }) From 48ad29c873a93469433e68e63addb17752f979e9 Mon Sep 17 00:00:00 2001 From: Nicholas Ong Date: Tue, 14 Mar 2023 16:13:43 +0800 Subject: [PATCH 3/4] test: inactive and active link redirect --- test/end-to-end/UrlCreation.test.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/test/end-to-end/UrlCreation.test.ts b/test/end-to-end/UrlCreation.test.ts index d86743c0b..48662797c 100644 --- a/test/end-to-end/UrlCreation.test.ts +++ b/test/end-to-end/UrlCreation.test.ts @@ -1,5 +1,6 @@ -import { Selector } from 'testcafe' +import { ClientFunction, Selector } from 'testcafe' import * as fs from 'fs' +import { fetch } from 'cross-fetch' import { apiLocation, circularRedirectUrl, @@ -60,6 +61,7 @@ import { createMaliciousFile, deleteFile, } from './util/fileHandle' +import { linkCreationProcedure } from './util/LinkCreationProcedure' // eslint-disable-next-line no-undef fixture(`URL Creation`) @@ -351,3 +353,18 @@ test('The update file test', async (t) => { // Delete downloaded file fs.unlink(directoryPath, () => {}) }) + +test('Test active and inactive link redirects', async (t) => { + const { generatedUrlActive, generatedUrlInactive } = + await linkCreationProcedure(t) + + // Check inactive link + const inactiveResult = await fetch(`${apiLocation}/${generatedUrlInactive}`) + await t.expect(inactiveResult.status === 404).ok() + + // Check active link redirect + await t.navigateTo(`${apiLocation}/${generatedUrlActive}`) + await t.wait(7000) + const getCurrentUrl = ClientFunction(() => window.location) + await t.expect((await getCurrentUrl()).host === 'www.google.com').ok() +}) From eaf707f8a8ee154726c28ebbdfe9cc584ac18893 Mon Sep 17 00:00:00 2001 From: Nicholas Ong Date: Wed, 15 Mar 2023 10:59:14 +0800 Subject: [PATCH 4/4] test: more robust test for inactive links --- test/end-to-end/UrlCreation.test.ts | 3 +++ test/end-to-end/util/helpers.ts | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/test/end-to-end/UrlCreation.test.ts b/test/end-to-end/UrlCreation.test.ts index 48662797c..43cbe0a5e 100644 --- a/test/end-to-end/UrlCreation.test.ts +++ b/test/end-to-end/UrlCreation.test.ts @@ -50,6 +50,7 @@ import { tag3, tagCloseButton1, tagsAutocompleteInput, + unavailableShortLink, uploadFile, urlTable, } from './util/helpers' @@ -361,6 +362,8 @@ test('Test active and inactive link redirects', async (t) => { // Check inactive link const inactiveResult = await fetch(`${apiLocation}/${generatedUrlInactive}`) await t.expect(inactiveResult.status === 404).ok() + await t.navigateTo(`${apiLocation}/${generatedUrlInactive}`) + await t.expect(unavailableShortLink.exists).ok() // Check active link redirect await t.navigateTo(`${apiLocation}/${generatedUrlActive}`) diff --git a/test/end-to-end/util/helpers.ts b/test/end-to-end/util/helpers.ts index 4222bf599..11369cb88 100644 --- a/test/end-to-end/util/helpers.ts +++ b/test/end-to-end/util/helpers.ts @@ -71,6 +71,11 @@ export const successBulkCreation = Selector('div').withText( 'links have been created', ) +// Unavailable Short Link Page +export const unavailableShortLink = Selector('h3').withText( + 'This short link is not available.', +) + export const urlTable = Selector('tbody') export const urlTableRowUrlText = (index: number) => // eslint-disable-next-line newline-per-chained-call