Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: use torrent in fixtures #703

Merged
merged 9 commits into from
Dec 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading