Skip to content

Commit

Permalink
Merge pull request #2176 from opengovsg/test/more-e2e-test
Browse files Browse the repository at this point in the history
[Test] More E2E tests
  • Loading branch information
oversparkling authored Mar 15, 2023
2 parents e9dfec3 + eaf707f commit 4a8461e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
62 changes: 61 additions & 1 deletion test/end-to-end/UrlCreation.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Selector } from 'testcafe'
import { ClientFunction, Selector } from 'testcafe'
import * as fs from 'fs'
import { fetch } from 'cross-fetch'
import {
apiLocation,
circularRedirectUrl,
dummyBulkCsv,
dummyBulkCsvRelativePath,
dummyChangedFilePath,
dummyFilePath,
dummyMaliciousFilePath,
dummyMaliciousRelativePath,
dummyRelativeChangedFilePath,
dummyRelativePath,
invalidShortUrl,
largeFileSize,
Expand All @@ -20,6 +25,7 @@ import {
blacklistValidationError,
bulkTab,
circularRedirectValidationError,
closeDrawerButton,
createLinkButton,
createUrlModal,
csvOnlyError,
Expand All @@ -44,6 +50,7 @@ import {
tag3,
tagCloseButton1,
tagsAutocompleteInput,
unavailableShortLink,
uploadFile,
urlTable,
} from './util/helpers'
Expand All @@ -55,6 +62,7 @@ import {
createMaliciousFile,
deleteFile,
} from './util/fileHandle'
import { linkCreationProcedure } from './util/LinkCreationProcedure'

// eslint-disable-next-line no-undef
fixture(`URL Creation`)
Expand Down Expand Up @@ -311,3 +319,55 @@ 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, () => {})
})

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()
await t.navigateTo(`${apiLocation}/${generatedUrlInactive}`)
await t.expect(unavailableShortLink.exists).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()
})
2 changes: 2 additions & 0 deletions test/end-to-end/util/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/end-to-end/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4a8461e

Please sign in to comment.