test: new suite "after-login"

Massimo Melina committed May 22, 2022 at 12:38 UTC 1734404ca9479ef4320b3d1c076cc710e042be14
4 files changed +27 -15
server/package.json
+3
@@ -43,6 +43,9 @@
43 "@types/node": "^16.11.12",
44 "@types/unzipper": "^0.10.5",
45 "axios": "^0.24.0",
46 + "axios-cookiejar-support": "^4.0.1",
47 + "tough-cookie": "^4.0.0",
48 + "@types/tough-cookie": "^4.0.2",
49 "koa-better-http-proxy": "^0.2.9",
50 "mocha": "^9.1.3",
51 "nm-prune": "^5.0.0",
shared/src/index.ts
+1
@@ -34,6 +34,7 @@ export function wait(ms: number) {
34
35 export function getCookie(name: string) {
36 const pre = name + '='
37 + //@ts-ignore necessary to import this file in tests.ts (node doesn't have document)
38 let decodedCookie = decodeURIComponent(document.cookie)
39 let ca = decodedCookie.split(';')
40 for (let c of ca) {
tests/test.ts
+23 -14
@@ -1,5 +1,8 @@
1 import axios 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/src'
6 /*
7 import { PORT, srv } from '../src'
8
@@ -11,6 +14,10 @@ const appStarted = new Promise(resolve =>
14 const username = 'rejetto'
15 const password = 'password'
16 const API = '/~/api/'
17 +const BASE_URL = 'http://localhost'
18 +
19 +const jar = new CookieJar()
20 +const client = wrapper(axios.create({ jar }))
21
22 describe('basics', () => {
23 //before(async () => appStarted)
@@ -67,19 +74,13 @@ describe('basics', () => {
74 }))
75 })
76
70 -/*
71 -let cookie:any
77 describe('after-login', () => {
73 - before(req(API+'login', (data, res) => Boolean(cookie = res.headers['set-cookie']), {
74 - data: { username, password }
75 - }))
76 - it('list protected', done => // defer execution of req() to have cookie set
77 - req(API+'file_list', data => isInList(data, 'alfa.txt'), {
78 - data: { path:'/for-admins/' },
79 - headers: { cookie },
80 - })(done))
78 + before(() =>
79 + srpSequence(username, password, (cmd: string, params: any) =>
80 + client.post(API+cmd, params).then(x => x.data))
81 + )
82 + it('list protected', reqList('/for-admins/', { inList:['alfa.txt'] }))
83 })
82 -*/
84
85 type Tester = number
86 | ((data: any, fullResponse: any) => boolean | Error)
@@ -96,17 +97,21 @@ type Tester = number
97
98 function req(methodUrl: string, test:Tester, requestOptions?:any) {
99 return (done:Done) => {
100 + const csrf = getCookie('csrf')
101 + if (csrf)
102 + Object.assign(requestOptions.data, { csrf })
103 +
104 const i = methodUrl.indexOf('/')
105 const method = methodUrl.slice(0,i) || requestOptions?.data && 'POST' || 'GET'
101 - const url = 'http://localhost'+methodUrl.slice(i)
102 - axios.request({ method, url, ...requestOptions })
106 + const url = BASE_URL+methodUrl.slice(i)
107 + client.request({ method, url, ...requestOptions })
108 .then(fun, fun)
109 .catch(err => {
110 done(err)
111 })
112
113 function fun(res:any) {
109 - console.debug('sent', requestOptions, 'got', res instanceof Error ? String(res) : [res.status, res.data])
114 + //console.debug('sent', requestOptions, 'got', res instanceof Error ? String(res) : [res.status])
115 if (test && test instanceof RegExp)
116 test = { re:test }
117 if (typeof test === 'number')
@@ -132,6 +137,10 @@ function req(methodUrl: string, test:Tester, requestOptions?:any) {
137 }
138 }
139
140 +function getCookie(k: string) {
141 + return jar.getCookiesSync(BASE_URL).find(c => c.key === k)?.value
142 +}
143 +
144 function reqApi(api: string, params: object, test:Tester) {
145 return req(API+api, test, { data: params })
146 }
todo.md
-1
@@ -31,7 +31,6 @@
31 - https://api.github.com/repos/rejetto/hfs/releases/latest
32 - check if 'created_at' is after exe's timestamp
33 - if positive search 'assets' array for 'name' matching exe, and download 'browser_download_url', and replace exe
34 -- update tests to SRP login
34 - upload
35 - upload unzipping (while streaming?)
36 - plugin to automatic generate letsencrypt cert?