Skip to content

Commit ea61b71

Browse files
committed
tests: add accessCounter test
1 parent a233565 commit ea61b71

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

src/handlers/handleWrite.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export async function handlePostOrPut(
126126
throw new WorkerError(400, `password should not contain newline`)
127127
}
128128
}
129+
const passwd = passwdFromForm || genRandStr(DEFAULT_PASSWD_LEN)
129130

130131
// check if name is legal
131132
if (nameFromForm !== undefined && !NAME_REGEX.test(nameFromForm)) {
@@ -186,10 +187,6 @@ export async function handlePostOrPut(
186187
pasteName = genRandStr(isPrivate ? PRIVATE_PASTE_NAME_LEN : PASTE_NAME_LEN)
187188
}
188189

189-
const passwd = passwdFromForm || genRandStr(DEFAULT_PASSWD_LEN)
190-
if (passwd.length === 0) {
191-
throw new WorkerError(400, "Empty passwd is not allowed")
192-
}
193190
await createPaste(env, pasteName, content, {
194191
expirationSeconds,
195192
now,

test/accessCounter.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { expect, it, beforeEach, vi, afterEach } from "vitest"
2+
import { genRandomBlob, upload, workerFetch } from "./testUtils.js"
3+
import { createExecutionContext, env, waitOnExecutionContext } from "cloudflare:test"
4+
import { PasteMetadata } from "../src/storage/storage"
5+
6+
beforeEach(() => {
7+
vi.spyOn(Math, "random").mockReturnValue(0)
8+
})
9+
10+
afterEach(() => {
11+
vi.restoreAllMocks()
12+
})
13+
14+
it("increase access counter", async () => {
15+
const ctx = createExecutionContext()
16+
const content = genRandomBlob(1024)
17+
const name = "abc"
18+
const url = (await upload(ctx, { c: content, n: name })).url
19+
20+
async function getCounter() {
21+
const paste = await env.PB.getWithMetadata<PasteMetadata>("~" + name)
22+
return paste?.metadata?.accessCounter
23+
}
24+
25+
expect(await getCounter()).toStrictEqual(0)
26+
27+
await workerFetch(ctx, url)
28+
await waitOnExecutionContext(ctx)
29+
30+
expect(await getCounter()).toStrictEqual(1)
31+
32+
await workerFetch(ctx, url)
33+
await waitOnExecutionContext(ctx)
34+
35+
expect(await getCounter()).toStrictEqual(2)
36+
})

test/roles.spec.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,59 @@ import { createExecutionContext } from "cloudflare:test"
44
import { MetaResponse } from "../src/shared"
55
import { genRandStr } from "../src/common"
66

7+
const testMd: string = `
8+
# Header 1
9+
10+
This is the content of \`test.md\`
11+
12+
<script>
13+
alert("Script should be removed")
14+
</script>
15+
16+
## Header 2
17+
18+
| abc | defghi |
19+
| :-: | -----: |
20+
| bar | baz |
21+
22+
**Bold**, \`Monospace\`, _Italics_, ~~Strikethrough~~, [URL](https://github.com)
23+
24+
- A
25+
- A1
26+
- A2
27+
- B
28+
29+
![Panty](https://shz.al/~panty.jpg)
30+
31+
1. first
32+
2. second
33+
34+
\`\`\`js
35+
console.log("hello world")
36+
\`\`\`
37+
38+
> Quotation
39+
40+
$$
41+
\\int_{-\\infty}^{\\infty} e^{-x^2} = \\sqrt{\\pi}
42+
$$
43+
`
44+
745
test("markdown with role a", async () => {
8-
const testMd = `# Hello` // TODO: use a stronger test file
946
const ctx = createExecutionContext()
1047
const url = (await upload(ctx, { c: testMd })).url
1148

1249
const revisitResponse = await workerFetch(ctx, addRole(url, "a"))
1350
expect(revisitResponse.status).toStrictEqual(200)
1451
expect(revisitResponse.headers.get("Content-Type")).toStrictEqual("text/html;charset=UTF-8")
1552
const responseHtml = await revisitResponse.text()
16-
expect(responseHtml.indexOf("<title>Hello</title>")).toBeGreaterThan(-1)
17-
expect(responseHtml.indexOf("<h1>Hello</h1>")).toBeGreaterThan(-1)
53+
expect(responseHtml.indexOf("<title>Header 1</title>")).toBeGreaterThan(-1)
54+
expect(responseHtml.indexOf('<code class="language-js">')).toBeGreaterThan(-1)
55+
56+
const bigMd = "1".repeat(1024 * 1024)
57+
const bigUrl = (await upload(ctx, { c: bigMd })).url
58+
const bigResp = await (await workerFetch(ctx, addRole(bigUrl, "a"))).text()
59+
expect(bigResp.indexOf("Untitled")).toBeGreaterThan(-1)
1860
})
1961

2062
test("meta with role m", async () => {

0 commit comments

Comments
 (0)