1
1
import crypto from 'crypto' ;
2
2
import fs from 'fs' ;
3
+ import path from 'path' ;
3
4
import readline from 'readline' ;
4
5
import stream from 'stream' ;
5
6
import supertest from 'supertest' ;
6
7
7
8
import app from '../../app' ;
8
9
import { getAuthToken } from './auth' ;
9
10
import { getTempPath } from '../../models/TemporaryStorage' ;
11
+ import paths from '../../../shared/config/paths' ;
10
12
11
- import type { AddTorrentByURLOptions , SetTorrentsTrackersOptions } from '../../../shared/types/api/torrents' ;
13
+ import type {
14
+ AddTorrentByFileOptions ,
15
+ AddTorrentByURLOptions ,
16
+ SetTorrentsTrackersOptions ,
17
+ } from '../../../shared/types/api/torrents' ;
12
18
import type { TorrentContent } from '../../../shared/types/TorrentContent' ;
13
19
import type { TorrentList , TorrentProperties } from '../../../shared/types/Torrent' ;
14
20
import type { TorrentStatus } from '../../../shared/constants/torrentStatusMap' ;
@@ -24,6 +30,13 @@ fs.mkdirSync(tempDirectory, {recursive: true});
24
30
25
31
jest . setTimeout ( 20000 ) ;
26
32
33
+ const torrentFiles = [
34
+ path . join ( paths . appSrc , 'fixtures/single.torrent' ) ,
35
+ path . join ( paths . appSrc , 'fixtures/multi.torrent' ) ,
36
+ ] . map ( ( torrentPath ) => Buffer . from ( fs . readFileSync ( torrentPath ) ) . toString ( 'base64' ) ) ;
37
+
38
+ const torrentURLs = [ 'https://releases.ubuntu.com/20.04/ubuntu-20.04.1-live-server-amd64.iso.torrent' ] ;
39
+
27
40
let torrentHash = '' ;
28
41
29
42
const activityStream = new stream . PassThrough ( ) ;
@@ -32,7 +45,7 @@ request.get('/api/activity-stream').send().set('Cookie', [authToken]).pipe(activ
32
45
33
46
describe ( 'POST /api/torrents/add-urls' , ( ) => {
34
47
const addTorrentByURLOptions : AddTorrentByURLOptions = {
35
- urls : [ 'https://releases.ubuntu.com/20.04/ubuntu-20.04.1-live-server-amd64.iso.torrent' ] ,
48
+ urls : torrentURLs ,
36
49
destination : tempDirectory ,
37
50
tags : [ 'test' ] ,
38
51
isBasePath : false ,
@@ -41,7 +54,7 @@ describe('POST /api/torrents/add-urls', () => {
41
54
42
55
const torrentAdded = new Promise ( ( resolve ) => {
43
56
rl . on ( 'line' , ( input ) => {
44
- if ( input . includes ( 'TORRENT_LIST_DIFF_CHANGE ' ) ) {
57
+ if ( input . includes ( 'TORRENT_LIST_ACTION_TORRENT_ADDED ' ) ) {
45
58
resolve ( ) ;
46
59
}
47
60
} ) ;
@@ -62,7 +75,7 @@ describe('POST /api/torrents/add-urls', () => {
62
75
} ) ;
63
76
} ) ;
64
77
65
- it ( 'GET /api/torrents' , ( done ) => {
78
+ it ( 'GET /api/torrents to verify torrents are added via URLs ' , ( done ) => {
66
79
torrentAdded . then ( ( ) => {
67
80
request
68
81
. get ( '/api/torrents' )
@@ -95,6 +108,63 @@ describe('POST /api/torrents/add-urls', () => {
95
108
} ) ;
96
109
} ) ;
97
110
111
+ describe ( 'POST /api/torrents/add-files' , ( ) => {
112
+ const addTorrentByFileOptions : AddTorrentByFileOptions = {
113
+ files : torrentFiles ,
114
+ destination : tempDirectory ,
115
+ tags : [ 'test' ] ,
116
+ isBasePath : false ,
117
+ start : false ,
118
+ } ;
119
+
120
+ const torrentAdded = new Promise ( ( resolve ) => {
121
+ rl . on ( 'line' , ( input ) => {
122
+ if ( input . includes ( 'TORRENT_LIST_ACTION_TORRENT_ADDED' ) ) {
123
+ resolve ( ) ;
124
+ }
125
+ } ) ;
126
+ } ) ;
127
+
128
+ it ( 'Adds a torrent from files' , ( done ) => {
129
+ request
130
+ . post ( '/api/torrents/add-files' )
131
+ . send ( addTorrentByFileOptions )
132
+ . set ( 'Cookie' , [ authToken ] )
133
+ . set ( 'Accept' , 'application/json' )
134
+ . expect ( 200 )
135
+ . expect ( 'Content-Type' , / j s o n / )
136
+ . end ( ( err , _res ) => {
137
+ if ( err ) done ( err ) ;
138
+
139
+ done ( ) ;
140
+ } ) ;
141
+ } ) ;
142
+
143
+ it ( 'GET /api/torrents to verify torrents are added via files' , ( done ) => {
144
+ torrentAdded . then ( ( ) => {
145
+ request
146
+ . get ( '/api/torrents' )
147
+ . send ( )
148
+ . set ( 'Cookie' , [ authToken ] )
149
+ . set ( 'Accept' , 'application/json' )
150
+ . expect ( 200 )
151
+ . expect ( 'Content-Type' , / j s o n / )
152
+ . end ( ( err , res ) => {
153
+ if ( err ) done ( err ) ;
154
+
155
+ expect ( res . body . torrents == null ) . toBe ( false ) ;
156
+ const torrentList : TorrentList = res . body . torrents ;
157
+
158
+ expect ( Object . keys ( torrentList ) . length ) . toBeGreaterThanOrEqual (
159
+ torrentFiles . length + ( torrentHash !== '' ? 1 : 0 ) ,
160
+ ) ;
161
+
162
+ done ( ) ;
163
+ } ) ;
164
+ } ) ;
165
+ } ) ;
166
+ } ) ;
167
+
98
168
describe ( 'PATCH /api/torrents/trackers' , ( ) => {
99
169
const testTrackers = [
100
170
`https://${ crypto . randomBytes ( 8 ) . toString ( 'hex' ) } .com/announce` ,
0 commit comments