From 74b84a42663cfaee733950b9e6e302a9e14222dc Mon Sep 17 00:00:00 2001 From: Trim21 Date: Mon, 11 Dec 2023 22:14:37 +0800 Subject: [PATCH 1/8] tests: use torrent in fixtures --- server/routes/api/torrents.test.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/server/routes/api/torrents.test.ts b/server/routes/api/torrents.test.ts index b9c848378..5953d5cac 100644 --- a/server/routes/api/torrents.test.ts +++ b/server/routes/api/torrents.test.ts @@ -6,6 +6,7 @@ import path from 'path'; import readline from 'readline'; import stream from 'stream'; import supertest from 'supertest'; +import MockAdapter from 'axios-mock-adapter'; import constructRoutes from '..'; import {getAuthToken} from '../../util/authUtil'; @@ -43,9 +44,19 @@ const torrentFiles = [ path.join(paths.appSrc, 'fixtures/multi.torrent'), ].map((torrentPath) => Buffer.from(fs.readFileSync(torrentPath)).toString('base64')) as [string, ...string[]]; +const mock = new MockAdapter(axios, {onNoMatch: 'passthrough'}); + +mock + .onGet('https://www.torrents/single.torrent') + .reply(200, fs.readFileSync(path.join(paths.appSrc, 'fixtures/single.torrent'))); + +mock + .onGet('https://www.torrents/multi.torrent') + .reply(200, fs.readFileSync(path.join(paths.appSrc, 'fixtures/multi.torrent'))); + const torrentURLs: [string, ...string[]] = [ - 'https://releases.ubuntu.com/focal/ubuntu-20.04.6-live-server-amd64.iso.torrent', - 'https://flood.js.org/api/test-cookie', + 'https://www.torrents/single.torrent', + 'https://www.torrents/multi.torrent', ]; const torrentCookies = { From 53b3a6cd15de98c221208076598e3a1f0b842b41 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Mon, 11 Dec 2023 22:16:42 +0800 Subject: [PATCH 2/8] fix --- server/routes/api/torrents.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/server/routes/api/torrents.test.ts b/server/routes/api/torrents.test.ts index 5953d5cac..7f92e9439 100644 --- a/server/routes/api/torrents.test.ts +++ b/server/routes/api/torrents.test.ts @@ -19,6 +19,7 @@ import type {TorrentContent} from '../../../shared/types/TorrentContent'; import type {TorrentList} from '../../../shared/types/Torrent'; import type {TorrentStatus} from '../../../shared/constants/torrentStatusMap'; import type {TorrentTracker} from '../../../shared/types/TorrentTracker'; +import axios from "axios"; const app = fastify({bodyLimit: 100 * 1024 * 1024 * 1024, disableRequestLogging: true, forceCloseConnections: true}); let request: supertest.SuperTest; From 668d6fd99947ff897023c058e7161ff04b891f25 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Mon, 11 Dec 2023 22:18:21 +0800 Subject: [PATCH 3/8] fix --- server/routes/api/torrents.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/routes/api/torrents.test.ts b/server/routes/api/torrents.test.ts index 7f92e9439..9fdb636bd 100644 --- a/server/routes/api/torrents.test.ts +++ b/server/routes/api/torrents.test.ts @@ -6,6 +6,7 @@ import path from 'path'; import readline from 'readline'; import stream from 'stream'; import supertest from 'supertest'; +import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import constructRoutes from '..'; @@ -19,7 +20,6 @@ import type {TorrentContent} from '../../../shared/types/TorrentContent'; import type {TorrentList} from '../../../shared/types/Torrent'; import type {TorrentStatus} from '../../../shared/constants/torrentStatusMap'; import type {TorrentTracker} from '../../../shared/types/TorrentTracker'; -import axios from "axios"; const app = fastify({bodyLimit: 100 * 1024 * 1024 * 1024, disableRequestLogging: true, forceCloseConnections: true}); let request: supertest.SuperTest; From 1964027629251c93fbc0f7584697cd18cab2e4ec Mon Sep 17 00:00:00 2001 From: Trim21 Date: Mon, 11 Dec 2023 23:14:20 +0800 Subject: [PATCH 4/8] debug --- server/routes/api/torrents.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/routes/api/torrents.test.ts b/server/routes/api/torrents.test.ts index 9fdb636bd..83835f8d7 100644 --- a/server/routes/api/torrents.test.ts +++ b/server/routes/api/torrents.test.ts @@ -253,7 +253,7 @@ describe('POST /api/torrents/add-files', () => { .expect('Content-Type', /json/) .expect((res) => { if (res.status !== 200 && res.status !== 202) { - throw new Error('Failed to add torrents'); + throw new Error(`Failed to add torrents ${res.body}`); } }) .end((err, _res) => { From 5f8ae6f73cf35dcf6eaa1f10cea04707fa2a7dfc Mon Sep 17 00:00:00 2001 From: Trim21 Date: Mon, 11 Dec 2023 23:17:23 +0800 Subject: [PATCH 5/8] debug --- server/routes/api/torrents.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/routes/api/torrents.test.ts b/server/routes/api/torrents.test.ts index 83835f8d7..68f459262 100644 --- a/server/routes/api/torrents.test.ts +++ b/server/routes/api/torrents.test.ts @@ -253,7 +253,7 @@ describe('POST /api/torrents/add-files', () => { .expect('Content-Type', /json/) .expect((res) => { if (res.status !== 200 && res.status !== 202) { - throw new Error(`Failed to add torrents ${res.body}`); + throw new Error(`Failed to add torrents ${JSON.stringify(res.body)}`); } }) .end((err, _res) => { From a2053ad82904e683368b22421d4d627241f7eb19 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Mon, 11 Dec 2023 23:19:41 +0800 Subject: [PATCH 6/8] debug --- server/routes/api/torrents.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/server/routes/api/torrents.test.ts b/server/routes/api/torrents.test.ts index 68f459262..de367a556 100644 --- a/server/routes/api/torrents.test.ts +++ b/server/routes/api/torrents.test.ts @@ -199,6 +199,31 @@ describe('POST /api/torrents/add-urls', () => { }); }); +describe('POST /api/torrents/delete', () => { + const torrentDeleted = watchTorrentList('remove'); + it('Deletes added torrents', (done) => { + request + .post('/api/torrents/delete') + .send({hashes: torrentHashes, deleteData: true}) + .set('Cookie', [authToken]) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end((err, _res) => { + if (err) done(err); + + Promise.race([torrentDeleted, new Promise((r) => setTimeout(r, 1000 * 15))]) + .then(async () => { + // Wait a while + await new Promise((r) => setTimeout(r, 1000 * 3)); + }) + .then(() => { + done(); + }); + }); + }); +}); + describe('POST /api/torrents/add-files', () => { const addTorrentByFileOptions: AddTorrentByFileOptions = { files: torrentFiles, From 06c7413e936b97131b6c0e6dcbf8b6ee6e6a38c5 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Mon, 11 Dec 2023 23:20:52 +0800 Subject: [PATCH 7/8] debug --- server/routes/api/torrents.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server/routes/api/torrents.test.ts b/server/routes/api/torrents.test.ts index de367a556..29508dc97 100644 --- a/server/routes/api/torrents.test.ts +++ b/server/routes/api/torrents.test.ts @@ -60,6 +60,8 @@ const torrentURLs: [string, ...string[]] = [ 'https://www.torrents/multi.torrent', ]; +const torrentHashes: string[] = []; + const torrentCookies = { 'flood.js.org': ['test=test'], }; @@ -190,6 +192,7 @@ describe('POST /api/torrents/add-urls', () => { : ['stopped', 'inactive']; expect(torrent.status).toEqual(expect.arrayContaining(expectedStatuses)); + torrentHashes.push(torrent.hash) torrentHash = torrent.hash; }), ); From 9242cfc71b5eaf629ee64b819eb07a6d0b17b805 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Mon, 11 Dec 2023 23:22:58 +0800 Subject: [PATCH 8/8] format --- server/routes/api/torrents.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/routes/api/torrents.test.ts b/server/routes/api/torrents.test.ts index 29508dc97..646857a8e 100644 --- a/server/routes/api/torrents.test.ts +++ b/server/routes/api/torrents.test.ts @@ -192,7 +192,7 @@ describe('POST /api/torrents/add-urls', () => { : ['stopped', 'inactive']; expect(torrent.status).toEqual(expect.arrayContaining(expectedStatuses)); - torrentHashes.push(torrent.hash) + torrentHashes.push(torrent.hash); torrentHash = torrent.hash; }), );