Skip to content

Commit

Permalink
tests: use torrent in fixtures (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 authored Dec 11, 2023
1 parent eaefd4c commit b9680f1
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions server/routes/api/torrents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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 '..';
import {getAuthToken} from '../../util/authUtil';
Expand Down Expand Up @@ -43,11 +45,23 @@ 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 torrentHashes: string[] = [];

const torrentCookies = {
'flood.js.org': ['test=test'],
};
Expand Down Expand Up @@ -178,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;
}),
);
Expand All @@ -187,6 +202,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,
Expand Down Expand Up @@ -241,7 +281,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 ${JSON.stringify(res.body)}`);
}
})
.end((err, _res) => {
Expand Down

0 comments on commit b9680f1

Please sign in to comment.