server: tests: mock response to torrents fetch

This commit is contained in:
Jesse Chan
2021-11-08 23:18:44 -08:00
parent 5dc4e8a119
commit da8f8b4d24
4 changed files with 116 additions and 22 deletions
+73
View File
@@ -68,6 +68,7 @@
"@vercel/ncc": "^0.31.1",
"autoprefixer": "^10.4.0",
"axios": "^0.24.0",
"axios-mock-adapter": "^1.20.0",
"babel-loader": "^8.2.3",
"bencode": "^2.0.2",
"body-parser": "^1.19.0",
@@ -4699,6 +4700,20 @@
"follow-redirects": "^1.14.4"
}
},
"node_modules/axios-mock-adapter": {
"version": "1.20.0",
"resolved": "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-1.20.0.tgz",
"integrity": "sha512-shZRhTjLP0WWdcvHKf3rH3iW9deb3UdKbdnKUoHmmsnBhVXN3sjPJM6ZvQ2r/ywgvBVQrMnjrSyQab60G1sr2w==",
"dev": true,
"dependencies": {
"fast-deep-equal": "^3.1.3",
"is-blob": "^2.1.0",
"is-buffer": "^2.0.5"
},
"peerDependencies": {
"axios": ">= 0.9.0"
}
},
"node_modules/axobject-query": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.2.0.tgz",
@@ -9998,6 +10013,18 @@
"node": ">=8"
}
},
"node_modules/is-blob": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/is-blob/-/is-blob-2.1.0.tgz",
"integrity": "sha512-SZ/fTft5eUhQM6oF/ZaASFDEdbFVe89Imltn9uZr03wdKMcWNVYSMjQPFtg05QuNkt5l5c135ElvXEQG0rk4tw==",
"dev": true,
"engines": {
"node": ">=6"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/is-boolean-object": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/is-boolean-object/-/is-boolean-object-1.1.2.tgz",
@@ -10014,6 +10041,29 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/is-buffer": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.5.tgz",
"integrity": "sha512-i2R6zNFDwgEHJyQUtJEk0XFi1i0dPFn/oqjK3/vPCcDeJvW5NQ83V8QbicfF1SupOaB0h8ntgBC2YiE7dfyctQ==",
"dev": true,
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"engines": {
"node": ">=4"
}
},
"node_modules/is-callable": {
"version": "1.2.4",
"resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.4.tgz",
@@ -21684,6 +21734,17 @@
"follow-redirects": "^1.14.4"
}
},
"axios-mock-adapter": {
"version": "1.20.0",
"resolved": "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-1.20.0.tgz",
"integrity": "sha512-shZRhTjLP0WWdcvHKf3rH3iW9deb3UdKbdnKUoHmmsnBhVXN3sjPJM6ZvQ2r/ywgvBVQrMnjrSyQab60G1sr2w==",
"dev": true,
"requires": {
"fast-deep-equal": "^3.1.3",
"is-blob": "^2.1.0",
"is-buffer": "^2.0.5"
}
},
"axobject-query": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.2.0.tgz",
@@ -25736,6 +25797,12 @@
"binary-extensions": "^2.0.0"
}
},
"is-blob": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/is-blob/-/is-blob-2.1.0.tgz",
"integrity": "sha512-SZ/fTft5eUhQM6oF/ZaASFDEdbFVe89Imltn9uZr03wdKMcWNVYSMjQPFtg05QuNkt5l5c135ElvXEQG0rk4tw==",
"dev": true
},
"is-boolean-object": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/is-boolean-object/-/is-boolean-object-1.1.2.tgz",
@@ -25746,6 +25813,12 @@
"has-tostringtag": "^1.0.0"
}
},
"is-buffer": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.5.tgz",
"integrity": "sha512-i2R6zNFDwgEHJyQUtJEk0XFi1i0dPFn/oqjK3/vPCcDeJvW5NQ83V8QbicfF1SupOaB0h8ntgBC2YiE7dfyctQ==",
"dev": true
},
"is-callable": {
"version": "1.2.4",
"resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.4.tgz",
+1
View File
@@ -119,6 +119,7 @@
"@vercel/ncc": "^0.31.1",
"autoprefixer": "^10.4.0",
"axios": "^0.24.0",
"axios-mock-adapter": "^1.20.0",
"babel-loader": "^8.2.3",
"bencode": "^2.0.2",
"body-parser": "^1.19.0",
+40 -12
View File
@@ -1,5 +1,7 @@
import axios from 'axios';
import crypto from 'crypto';
import fs from 'fs';
import MockAdapter from 'axios-mock-adapter';
import os from 'os';
import path from 'path';
import readline from 'readline';
@@ -31,9 +33,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://webtorrent.io/torrents/big-buck-bunny.torrent',
'https://flood.js.org/api/test-cookie',
'https://www.torrents/single.torrent',
'https://www.torrents/multi.torrent',
];
const torrentCookies = {
@@ -46,8 +58,8 @@ const testTrackers = [
`http://${crypto.randomBytes(8).toString('hex')}.com/announce.php?key=test`,
];
let torrentHash = '';
let createdTorrentHash = '';
const torrentHashes: string[] = [];
const createdTorrentHashes: string[] = [];
const activityStream = new stream.PassThrough();
const rl = readline.createInterface({input: activityStream});
@@ -170,7 +182,7 @@ describe('POST /api/torrents/add-urls', () => {
: ['stopped', 'inactive'];
expect(torrent.status).toEqual(expect.arrayContaining(expectedStatuses));
torrentHash = torrent.hash;
torrentHashes.push(torrent.hash);
}),
);
@@ -179,6 +191,22 @@ describe('POST /api/torrents/add-urls', () => {
});
});
describe('POST /api/torrents/delete', () => {
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);
done();
});
});
});
describe('POST /api/torrents/add-files', () => {
const addTorrentByFileOptions: AddTorrentByFileOptions = {
files: torrentFiles,
@@ -361,7 +389,7 @@ describe('POST /api/torrents/create', () => {
await Promise.all(
addedTorrents.map(async (torrent) => {
createdTorrentHash = torrent.hash;
createdTorrentHashes.push(torrent.hash);
expect(torrent.isPrivate).toBe(false);
if (process.argv.includes('--trurl')) {
@@ -381,7 +409,7 @@ describe('POST /api/torrents/create', () => {
describe('PATCH /api/torrents/trackers', () => {
it('Sets single tracker', (done) => {
const setTrackersOptions: SetTorrentsTrackersOptions = {
hashes: [torrentHash],
hashes: [torrentHashes[0]],
trackers: [testTrackers[0]],
};
@@ -401,7 +429,7 @@ describe('PATCH /api/torrents/trackers', () => {
it('Sets multiple trackers', (done) => {
const setTrackersOptions: SetTorrentsTrackersOptions = {
hashes: [torrentHash],
hashes: [torrentHashes[0]],
trackers: testTrackers,
};
@@ -421,7 +449,7 @@ describe('PATCH /api/torrents/trackers', () => {
it('GET /api/torrents/{hash}/trackers', (done) => {
request
.get(`/api/torrents/${torrentHash}/trackers`)
.get(`/api/torrents/${torrentHashes[0]}/trackers`)
.send()
.set('Cookie', [authToken])
.set('Accept', 'application/json')
@@ -443,7 +471,7 @@ describe('PATCH /api/torrents/trackers', () => {
describe('GET /api/torrents/{hash}/contents', () => {
it('Gets contents of torrents', (done) => {
request
.get(`/api/torrents/${torrentHash}/contents`)
.get(`/api/torrents/${torrentHashes[0]}/contents`)
.send()
.set('Cookie', [authToken])
.set('Accept', 'application/json')
@@ -466,7 +494,7 @@ describe('POST /api/torrents/move', () => {
it('Moves torrent', (done) => {
const moveTorrentsOptions: MoveTorrentsOptions = {
hashes: [createdTorrentHash],
hashes: [createdTorrentHashes[createdTorrentHashes.length - 1]],
destination: destDirectory,
moveFiles: true,
isBasePath: true,
@@ -504,7 +532,7 @@ describe('POST /api/torrents/move', () => {
expect(res.body.torrents == null).toBe(false);
const torrentList: TorrentList = res.body.torrents;
const torrent = torrentList[createdTorrentHash];
const torrent = torrentList[createdTorrentHashes[createdTorrentHashes.length - 1]];
expect(torrent).not.toBe(null);
expect(torrent.directory.startsWith(destDirectory)).toBe(true);
@@ -424,20 +424,12 @@ class RTorrentClientGatewayService extends ClientGatewayService {
// Delete contents of torrents
for await (const contentPath of contentPaths) {
try {
await fs.promises.unlink(contentPath);
} catch (error) {
console.error(`Error deleting file: ${contentPath}\n${error}`);
}
await fs.promises.unlink(contentPath).catch(() => undefined);
}
// Try to remove empty directories
for await (const directoryPath of directoryPaths) {
try {
await fs.promises.rmdir(directoryPath);
} catch (error) {
console.error(`Error removing directory: ${directoryPath}\n${error}`);
}
await fs.promises.rmdir(directoryPath).catch(() => undefined);
}
}