dev: upload tests

Massimo Melina committed Jan 15, 2023 at 16:52 UTC 1cd43e853d88aa7be0d32cdf23bdc0524a36180d
2 files changed +21 -3
tests/config.yaml
+4 -1
@@ -30,7 +30,10 @@ vfs:
30 can_read:
31 - admins
32 children:
33 - - name: asd
33 + - name: upload
34 + source: tests
35 + can_upload:
36 + - admins
37 - source: tests/alfa.txt
38 - source: tests
39 - name: renameChild
tests/test.ts
+17 -2
@@ -1,8 +1,10 @@
1 -import axios from 'axios'
1 +import axios, { AxiosRequestConfig } from 'axios'
2 import { wrapper } from 'axios-cookiejar-support'
3 import { CookieJar } from 'tough-cookie'
4 import { Done } from 'mocha'
5 import { srpSequence } from '@hfs/shared/srp'
6 +import { createReadStream, rmSync, unlinkSync } from 'fs'
7 +import { join } from 'path'
8 /*
9 import { PORT, srv } from '../src'
10
@@ -78,6 +80,8 @@ describe('basics', () => {
80 it('referer', req('/f1/page/gpl.png', 403, {
81 headers: { Referer: 'https://some-website.com/try-to-trick/x.com/' }
82 }))
83 +
84 + testUpload('upload.missing perm', 401)
85 })
86
87 describe('accounts', () => {
@@ -93,8 +97,18 @@ describe('after-login', () => {
97 client.post(API+cmd, params).then(x => x.data))
98 )
99 it('list protected', reqList('/for-admins/', { inList:['alfa.txt'] }))
100 + testUpload('upload', 200)
101 + testUpload('upload.bad path', 418, '../../')
102 + after(() =>
103 + rmSync(join(__dirname, 'temp'), { recursive: true}))
104 })
105
106 +function testUpload(name: string, tester: Tester, path = 'temp/') {
107 + it(name, req('PUT/for-admins/upload/'+join(path+'gpl.png'), tester, {
108 + data: createReadStream(join(__dirname, 'page/gpl.png'))
109 + }))
110 +}
111 +
112 type Tester = number
113 | ((data: any, fullResponse: any) => boolean | Error)
114 | RegExp
@@ -108,12 +122,13 @@ type Tester = number
122 length?: number
123 }
124
111 -function req(methodUrl: string, test:Tester, requestOptions?:any) {
125 +function req(methodUrl: string, test:Tester, requestOptions: AxiosRequestConfig<any>={}) {
126 return (done:Done) => {
127 const csrf = getCookie('csrf')
128 if (csrf)
129 Object.assign(requestOptions.data, { csrf })
130
131 + // all url starts with /, so if one doesn't it's because the method is prefixed
132 const i = methodUrl.indexOf('/')
133 const method = methodUrl.slice(0,i) || requestOptions?.data && 'POST' || 'GET'
134 const url = BASE_URL+methodUrl.slice(i)