Compare commits

..

No commits in common. "a5809c216c2b401f7ae0aed28b1d6bef0abca072" and "fdc2722fb96cc85a902677d2d074af8377076ccd" have entirely different histories.

18 changed files with 98 additions and 283 deletions

View File

@ -7,7 +7,7 @@
"docker:up": "docker compose -f docker-compose.dev.yaml up",
"docker:build": "docker compose -f docker-compose.dev.yaml build",
"test": "playwright test --project chrome firefox safari",
"test:local": "playwright test --project chrome",
"test:local": "playwright test --project local",
"test:server": "run-s docker:up",
"test:prepare": "run-p build docker:build",
"build": "pnpm run --recursive --filter=!@cryptgeon/backend build"

View File

@ -7,7 +7,7 @@ import pretty from 'pretty-bytes'
import { exit } from './utils'
export async function download(url: URL, all: boolean, suggestedPassword?: string) {
export async function download(url: URL) {
setBase(url.origin)
const id = url.pathname.split('/')[2]
const preview = await info(id).catch(() => exit('Note does not exist or is expired'))
@ -16,9 +16,6 @@ export async function download(url: URL, all: boolean, suggestedPassword?: strin
let password: string
const derivation = preview?.meta.derivation
if (derivation) {
if (suggestedPassword) {
password = suggestedPassword
} else {
const response = await inquirer.prompt([
{
type: 'password',
@ -27,7 +24,6 @@ export async function download(url: URL, all: boolean, suggestedPassword?: strin
},
])
password = response.password
}
} else {
password = url.hash.slice(1)
}
@ -43,11 +39,6 @@ export async function download(url: URL, all: boolean, suggestedPassword?: strin
exit('No files found in note')
return
}
let selected: typeof files
if (all) {
selected = files
} else {
const { names } = await inquirer.prompt([
{
type: 'checkbox',
@ -60,12 +51,13 @@ export async function download(url: URL, all: boolean, suggestedPassword?: strin
})),
},
])
selected = files.filter((file) => names.includes(file.name))
}
const selected = files.filter((file) => names.includes(file.name))
if (!selected.length) exit('No files selected')
await Promise.all(
selected.map(async (file) => {
files.map(async (file) => {
let filename = resolve(file.name)
try {
// If exists -> prepend timestamp to not overwrite the current file
@ -76,7 +68,6 @@ export async function download(url: URL, all: boolean, suggestedPassword?: strin
console.log(`Saved: ${basename(filename)}`)
})
)
break
case 'text':
const plaintext = await Adapters.Text.decrypt(note.contents, key).catch(couldNotDecrypt)

View File

@ -15,7 +15,6 @@ const server = new Option('-s --server <url>', 'the cryptgeon server to use').de
const files = new Argument('<file...>', 'Files to be sent').argParser(parseFile)
const text = new Argument('<text>', 'Text content of the note')
const password = new Option('-p --password <string>', 'manually set a password')
const all = new Option('-a --all', 'Save all files without prompt').default(false)
const url = new Argument('<url>', 'The url to open')
const views = new Option('-v --views <number>', 'Amount of views before getting destroyed').argParser(parseNumber)
const minutes = new Option('-m --minutes <number>', 'Minutes before the note expires').argParser(parseNumber)
@ -87,13 +86,10 @@ send
program
.command('open')
.addArgument(url)
.addOption(password)
.addOption(all)
.action(async (note, options) => {
try {
const url = new URL(note)
options.password ||= await getStdin()
await download(url, options.all, options.password)
await download(url)
} catch {
exit('Invalid URL')
}

View File

@ -2,23 +2,19 @@ export function getStdin(timeout: number = 10): Promise<string> {
return new Promise<string>((resolve, reject) => {
// Store the data from stdin in a buffer
let buffer = ''
let t: NodeJS.Timeout
const dataHandler = (d: Buffer) => (buffer += d.toString())
const endHandler = () => {
clearTimeout(t)
resolve(buffer.trim())
}
process.stdin.on('data', (d) => (buffer += d.toString()))
// Stop listening for data after the timeout, otherwise hangs indefinitely
t = setTimeout(() => {
process.stdin.removeListener('data', dataHandler)
process.stdin.removeListener('end', endHandler)
process.stdin.pause()
const t = setTimeout(() => {
process.stdin.destroy()
resolve('')
}, timeout)
process.stdin.on('data', dataHandler)
process.stdin.on('end', endHandler)
// Listen for end and error events
process.stdin.on('end', () => {
clearTimeout(t)
resolve(buffer.trim())
})
process.stdin.on('error', reject)
})
}

View File

@ -47,13 +47,8 @@
/>
</div>
<div class="flex">
<Switch
data-testid="custom-password"
bind:value={customPassword}
label={$t('home.advanced.custom_password')}
/>
<Switch bind:value={customPassword} label={$t('home.advanced.custom_password')} />
<TextInput
data-testid="password"
type="password"
bind:value={note.password}
label={$t('common.password')}

View File

@ -1,33 +0,0 @@
import { test } from '@playwright/test'
import { basename } from 'node:path'
import { Files, getFileChecksum, rm, tmpFile } from '../../files'
import { CLI, getLinkFromCLI } from '../../utils'
test.describe('file @cli', () => {
test('simple', async ({ page }) => {
const file = await tmpFile(Files.Image)
const checksum = await getFileChecksum(file)
const note = await CLI('send', 'file', file)
const link = getLinkFromCLI(note.stdout)
await rm(file)
await CLI('open', link, '--all')
const c = await getFileChecksum(basename(file))
await rm(basename(file))
test.expect(checksum).toBe(c)
})
test('simple with password', async ({ page }) => {
const file = await tmpFile(Files.Image)
const password = 'password'
const checksum = await getFileChecksum(file)
const note = await CLI('send', 'file', file, '--password', password)
const link = getLinkFromCLI(note.stdout)
await rm(file)
await CLI('open', link, '--all', '--password', password)
const c = await getFileChecksum(basename(file))
await rm(basename(file))
test.expect(checksum).toBe(c)
})
})

View File

@ -1,23 +1,13 @@
import { test } from '@playwright/test'
import { CLI, getLinkFromCLI } from '../../utils'
import { CLI } from '../../utils'
test.describe('text @cli', () => {
test('simple', async ({ page }) => {
const text = `Endless prejudice endless play derive joy eternal-return selfish burying. Of decieve play pinnacle faith disgust. Spirit reason salvation burying strong of joy ascetic selfish against merciful sea truth. Ubermensch moral prejudice derive chaos mountains ubermensch justice philosophy justice ultimate joy ultimate transvaluation. Virtues convictions war ascetic eternal-return spirit. Ubermensch transvaluation noble revaluation sexuality intentions salvation endless decrepit hope noble fearful. Justice ideal ultimate snare god joy evil sexuality insofar gains oneself ideal.`
const note = await CLI('send', 'text', text)
const link = getLinkFromCLI(note.stdout)
const link = note.stdout.trim().replace(/(.|\s)*http/g, 'http')
const retrieved = await CLI('open', link)
test.expect(retrieved.stdout.trim()).toBe(text)
})
test('simple with password', async ({ page }) => {
const text = `Endless prejudice endless play derive joy eternal-return selfish burying.`
const password = 'password'
const note = await CLI('send', 'text', text, '--password', password)
const link = getLinkFromCLI(note.stdout)
const retrieved = await CLI('open', link, '--password', password)
test.expect(retrieved.stdout.trim()).toBe(text)
})
})

View File

@ -1,53 +0,0 @@
import { test } from '@playwright/test'
import { CLI, checkLinkForDownload, checkLinkForText, createNote, getLinkFromCLI } from '../../utils'
import { Files, getFileChecksum, rm, tmpFile } from '../../files'
import { basename } from 'path'
const text = `Endless prejudice endless play derive joy eternal-return selfish burying. Of decieve play pinnacle faith disgust. Spirit reason salvation burying strong of joy ascetic selfish against merciful sea truth. Ubermensch moral prejudice derive chaos mountains ubermensch justice philosophy justice ultimate joy ultimate transvaluation. Virtues convictions war ascetic eternal-return spirit. Ubermensch transvaluation noble revaluation sexuality intentions salvation endless decrepit hope noble fearful. Justice ideal ultimate snare god joy evil sexuality insofar gains oneself ideal.`
const password = 'password'
test.describe('text @cross', () => {
test('cli to web', async ({ page }) => {
const file = await tmpFile(Files.Image)
const checksum = await getFileChecksum(file)
const note = await CLI('send', 'file', file)
const link = getLinkFromCLI(note.stdout)
await rm(file)
await checkLinkForDownload(page, { link, text: basename(file), checksum })
})
test('cli to web with password', async ({ page }) => {
const file = await tmpFile(Files.Image)
const checksum = await getFileChecksum(file)
const note = await CLI('send', 'file', file, '--password', password)
const link = getLinkFromCLI(note.stdout)
await rm(file)
await checkLinkForDownload(page, { link, text: basename(file), checksum, password })
})
test('web to cli', async ({ page }) => {
const files = [Files.Image]
const checksum = await getFileChecksum(files[0])
const link = await createNote(page, { files })
const filename = basename(files[0])
await CLI('open', link, '--all')
const c = await getFileChecksum(filename)
await rm(basename(filename))
test.expect(checksum).toBe(c)
})
test('web to cli with password', async ({ page }) => {
const files = [Files.Image]
const checksum = await getFileChecksum(files[0])
const link = await createNote(page, { files, password })
const filename = basename(files[0])
await CLI('open', link, '--all', '--password', password)
const c = await getFileChecksum(filename)
await rm(basename(filename))
test.expect(checksum).toBe(c)
})
})

View File

@ -1,15 +1,14 @@
import { test } from '@playwright/test'
import { CLI, checkLinkForText, createNote, getLinkFromCLI } from '../../utils'
import { CLI, checkLinkForText, createNote } from '../../utils'
const text = `Endless prejudice endless play derive joy eternal-return selfish burying. Of decieve play pinnacle faith disgust. Spirit reason salvation burying strong of joy ascetic selfish against merciful sea truth. Ubermensch moral prejudice derive chaos mountains ubermensch justice philosophy justice ultimate joy ultimate transvaluation. Virtues convictions war ascetic eternal-return spirit. Ubermensch transvaluation noble revaluation sexuality intentions salvation endless decrepit hope noble fearful. Justice ideal ultimate snare god joy evil sexuality insofar gains oneself ideal.`
const password = 'password'
test.describe('text @cross', () => {
test('cli to web', async ({ page }) => {
const note = await CLI('send', 'text', text)
const link = getLinkFromCLI(note.stdout)
const link = note.stdout.trim().replace(/(.|\s)*http/g, 'http')
await checkLinkForText(page, { link, text })
await checkLinkForText(page, link, text)
})
test('web to cli', async ({ page }) => {
@ -17,16 +16,4 @@ test.describe('text @cross', () => {
const retrieved = await CLI('open', link)
test.expect(retrieved.stdout.trim()).toBe(text)
})
test('cli to web with password', async ({ page }) => {
const note = await CLI('send', 'text', text, '--password', password)
const link = getLinkFromCLI(note.stdout)
await checkLinkForText(page, { link, text, password })
})
test('web to cli with password', async ({ page }) => {
const link = await createNote(page, { text, password })
const retrieved = await CLI('open', link, '--password', password)
test.expect(retrieved.stdout.trim()).toBe(text)
})
})

View File

@ -1,25 +0,0 @@
import { createHash } from 'crypto'
import { cp as cpFN, rm as rmFN } from 'fs'
import { readFile } from 'fs/promises'
import { promisify } from 'util'
export const cp = promisify(cpFN)
export const rm = promisify(rmFN)
export const Files = {
PDF: 'test/assets/AES.pdf',
Image: 'test/assets/image.jpg',
Zip: 'test/assets/Pigeons.zip',
}
export async function getFileChecksum(file: string) {
const buffer = await readFile(file)
const hash = createHash('sha3-256').update(buffer).digest('hex')
return hash
}
export async function tmpFile(file: string) {
const name = `./tmp/${Math.random().toString(36).substring(7)}`
await cp(file, name)
return name
}

View File

@ -1,25 +1,19 @@
import { expect, type Page } from '@playwright/test'
import { createHash } from 'crypto'
import { readFile } from 'fs/promises'
import { execFile } from 'node:child_process'
import { promisify } from 'node:util'
import { getFileChecksum } from './files'
const exec = promisify(execFile)
type CreatePage = {
text?: string
files?: string[]
views?: number
expiration?: number
error?: string
password?: string
}
type CreatePage = { text?: string; files?: string[]; views?: number; expiration?: number; error?: string }
export async function createNote(page: Page, options: CreatePage): Promise<string> {
await page.goto('/')
if (options.text) {
await page.getByTestId('text-field').fill(options.text)
await page.locator('[data-testid="text-field"]').fill(options.text)
} else if (options.files) {
await page.getByTestId('switch-file').click()
await page.locator('[data-testid="switch-file"]').click()
const [fileChooser] = await Promise.all([
page.waitForEvent('filechooser'),
@ -28,16 +22,13 @@ export async function createNote(page: Page, options: CreatePage): Promise<strin
await fileChooser.setFiles(options.files)
}
if (options.views || options.expiration || options.password) await page.getByTestId('switch-advanced').click()
if (options.views) {
await page.getByTestId('field-views').fill(options.views.toString())
await page.locator('[data-testid="switch-advanced"]').click()
await page.locator('[data-testid="field-views"]').fill(options.views.toString())
} else if (options.expiration) {
await page.getByTestId('switch-advanced-toggle').click()
await page.getByTestId('field-expiration').fill(options.expiration.toString())
}
if (options.password) {
await page.getByTestId('custom-password').click()
await page.getByTestId('password').fill(options.password)
await page.locator('[data-testid="switch-advanced"]').click()
await page.locator('[data-testid="switch-advanced-toggle"]').click()
await page.locator('[data-testid="field-expiration"]').fill(options.expiration.toString())
}
await page.locator('button:has-text("create")').click()
@ -46,39 +37,29 @@ export async function createNote(page: Page, options: CreatePage): Promise<strin
await expect(page.locator('.error-text')).toContainText(options.error, { timeout: 60_000 })
}
// Return share link
return await page.getByTestId('share-link').inputValue()
const shareLink = await page.locator('[data-testid="share-link"]').inputValue()
return shareLink
}
type CheckLinkBase = {
link: string
text: string
password?: string
}
export async function checkLinkForDownload(page: Page, options: CheckLinkBase & { checksum: string }) {
export async function checkLinkForDownload(page: Page, link: string, text: string, checksum: string) {
await page.goto('/')
await page.goto(options.link)
if (options.password) await page.getByTestId('show-note-password').fill(options.password)
await page.getByTestId('show-note-button').click()
await page.goto(link)
await page.locator('[data-testid="show-note-button"]').click()
const [download] = await Promise.all([
page.waitForEvent('download'),
page.getByTestId(`result`).locator(`text=${options.text}`).click(),
page.locator(`[data-testid="result"] >> text=${text}`).click(),
])
const path = await download.path()
if (!path) throw new Error('Download failed')
const cs = await getFileChecksum(path)
await expect(cs).toBe(options.checksum)
await expect(cs).toBe(checksum)
}
export async function checkLinkForText(page: Page, options: CheckLinkBase) {
export async function checkLinkForText(page: Page, link: string, text: string) {
await page.goto('/')
await page.goto(options.link)
if (options.password) await page.getByTestId('show-note-password').fill(options.password)
await page.getByTestId('show-note-button').click()
const text = await page.getByTestId('result').locator('.note').innerText()
await expect(text).toContain(options.text)
await page.goto(link)
await page.locator('[data-testid="show-note-button"]').click()
await expect(await page.locator('[data-testid="result"] >> .note').innerText()).toContain(text)
}
export async function checkLinkDoesNotExist(page: Page, link: string) {
@ -87,6 +68,12 @@ export async function checkLinkDoesNotExist(page: Page, link: string) {
await expect(page.locator('main')).toContainText('note was not found or was already deleted')
}
export async function getFileChecksum(file: string) {
const buffer = await readFile(file)
const hash = createHash('sha3-256').update(buffer).digest('hex')
return hash
}
export async function CLI(...args: string[]) {
return await exec('./packages/cli/dist/index.cjs', args, {
env: {
@ -95,9 +82,3 @@ export async function CLI(...args: string[]) {
},
})
}
export function getLinkFromCLI(output: string): string {
const match = output.match(/(https?:\/\/[^\s]+)/)
if (!match) throw new Error('No link found in CLI output')
return match[0]
}

5
test/web/file/files.ts Normal file
View File

@ -0,0 +1,5 @@
export default {
PDF: 'test/assets/AES.pdf',
Image: 'test/assets/image.jpg',
Zip: 'test/assets/Pigeons.zip',
}

View File

@ -1,13 +1,13 @@
import { test } from '@playwright/test'
import { Files, getFileChecksum } from '../../files'
import { checkLinkForDownload, createNote } from '../../utils'
import { checkLinkForDownload, createNote, getFileChecksum } from '../../utils'
import Files from './files'
test.describe('@web', () => {
test('multiple', async ({ page }) => {
const files = [Files.PDF, Files.Image]
const checksums = await Promise.all(files.map(getFileChecksum))
const link = await createNote(page, { files, views: 2 })
await checkLinkForDownload(page, { link, text: 'image.jpg', checksum: checksums[1] })
await checkLinkForDownload(page, { link, text: 'AES.pdf', checksum: checksums[0] })
await checkLinkForDownload(page, link, 'image.jpg', checksums[1])
await checkLinkForDownload(page, link, 'AES.pdf', checksums[0])
})
})

View File

@ -1,12 +1,12 @@
import { test } from '@playwright/test'
import { Files, getFileChecksum } from '../../files'
import { checkLinkDoesNotExist, checkLinkForDownload, checkLinkForText, createNote } from '../../utils'
import { checkLinkDoesNotExist, checkLinkForDownload, checkLinkForText, createNote, getFileChecksum } from '../../utils'
import Files from './files'
test.describe('@web', () => {
test('simple pdf', async ({ page }) => {
const files = [Files.PDF]
const link = await createNote(page, { files })
await checkLinkForText(page, { link, text: 'AES.pdf' })
await checkLinkForText(page, link, 'AES.pdf')
await checkLinkDoesNotExist(page, link)
})
@ -14,21 +14,13 @@ test.describe('@web', () => {
const files = [Files.PDF]
const checksum = await getFileChecksum(files[0])
const link = await createNote(page, { files })
await checkLinkForDownload(page, { link, text: 'AES.pdf', checksum })
await checkLinkForDownload(page, link, 'AES.pdf', checksum)
})
test('image content', async ({ page }) => {
const files = [Files.Image]
const checksum = await getFileChecksum(files[0])
const link = await createNote(page, { files })
await checkLinkForDownload(page, { link, text: 'image.jpg', checksum })
})
test('simple pdf with password', async ({ page }) => {
const files = [Files.PDF]
const password = 'password'
const link = await createNote(page, { files, password })
await checkLinkForText(page, { link, text: 'AES.pdf', password })
await checkLinkDoesNotExist(page, link)
await checkLinkForDownload(page, link, 'image.jpg', checksum)
})
})

View File

@ -1,6 +1,6 @@
import { test } from '@playwright/test'
import { createNote } from '../../utils'
import { Files } from '../../files'
import Files from './files'
test.describe('@web', () => {
test.skip('to big zip', async ({ page }) => {

View File

@ -7,10 +7,10 @@ test.describe('@web', () => {
const minutes = 1
const timeout = minutes * 60_000
test.setTimeout(timeout * 2)
const link = await createNote(page, { text, expiration: minutes })
await checkLinkForText(page, { link, text })
await checkLinkForText(page, { link, text })
const shareLink = await createNote(page, { text, expiration: minutes })
await checkLinkForText(page, shareLink, text)
await checkLinkForText(page, shareLink, text)
await page.waitForTimeout(timeout)
await checkLinkDoesNotExist(page, link)
await checkLinkDoesNotExist(page, shareLink)
})
})

View File

@ -3,15 +3,8 @@ import { checkLinkForText, createNote } from '../../utils'
test.describe('@web', () => {
test('simple', async ({ page }) => {
const text = `Endless prejudice endless play derive joy eternal-return selfish burying. Of deceive play pinnacle faith disgust. Spirit reason salvation burying strong of joy ascetic selfish against merciful sea truth. Ubermensch moral prejudice derive chaos mountains ubermensch justice philosophy justice ultimate joy ultimate transvaluation. Virtues convictions war ascetic eternal-return spirit. Ubermensch transvaluation noble revaluation sexuality intentions salvation endless decrepit hope noble fearful. Justice ideal ultimate snare god joy evil sexuality insofar gains oneself ideal.`
const link = await createNote(page, { text })
await checkLinkForText(page, { link, text })
})
test('simple with password', async ({ page }) => {
const text = 'Foo bar'
const password = '123'
const shareLink = await createNote(page, { text, password })
await checkLinkForText(page, { link: shareLink, text, password })
const text = `Endless prejudice endless play derive joy eternal-return selfish burying. Of decieve play pinnacle faith disgust. Spirit reason salvation burying strong of joy ascetic selfish against merciful sea truth. Ubermensch moral prejudice derive chaos mountains ubermensch justice philosophy justice ultimate joy ultimate transvaluation. Virtues convictions war ascetic eternal-return spirit. Ubermensch transvaluation noble revaluation sexuality intentions salvation endless decrepit hope noble fearful. Justice ideal ultimate snare god joy evil sexuality insofar gains oneself ideal.`
const shareLink = await createNote(page, { text })
await checkLinkForText(page, shareLink, text)
})
})

View File

@ -4,17 +4,17 @@ import { checkLinkDoesNotExist, checkLinkForText, createNote } from '../../utils
test.describe('@web', () => {
test('only shown once', async ({ page }) => {
const text = `Christian victorious reason suicide dead. Right ultimate gains god hope truth burying selfish society joy. Ultimate.`
const link = await createNote(page, { text })
await checkLinkForText(page, { link, text })
await checkLinkDoesNotExist(page, link)
const shareLink = await createNote(page, { text })
await checkLinkForText(page, shareLink, text)
await checkLinkDoesNotExist(page, shareLink)
})
test('view 3 times', async ({ page }) => {
const text = `Justice holiest overcome fearful strong ultimate holiest christianity.`
const link = await createNote(page, { text, views: 3 })
await checkLinkForText(page, { link, text })
await checkLinkForText(page, { link, text })
await checkLinkForText(page, { link, text })
await checkLinkDoesNotExist(page, link)
const shareLink = await createNote(page, { text, views: 3 })
await checkLinkForText(page, shareLink, text)
await checkLinkForText(page, shareLink, text)
await checkLinkForText(page, shareLink, text)
await checkLinkDoesNotExist(page, shareLink)
})
})