1
1
import assert from "assert" ;
2
2
import { client } from "../utils/httpClient" ;
3
- import { getHash } from "../../src/utils/getHash" ;
4
3
import { db } from "../../src/databases/databases" ;
5
4
import { UserID } from "../../src/types/user.model" ;
6
5
import { Category , VideoID } from "../../src/types/segments.model" ;
6
+ import { insertVipUser } from "../utils/queryGen" ;
7
+ import { genUser } from "../utils/genUser" ;
8
+ import { genRandomValue } from "../utils/getRandom" ;
7
9
8
10
interface LockCategory {
9
11
category : Category ,
10
12
reason : string ,
11
13
videoID : VideoID ,
12
14
userID : UserID
13
15
}
14
- const lockVIPUser = "lockCategoriesHttpVIPUser" ;
15
- const lockVIPUserHash = getHash ( lockVIPUser ) ;
16
16
const endpoint = "/api/lockCategories" ;
17
+ const lockUser = genUser ( "lockCategoriesHttp" , "vip" ) ;
17
18
const checkLockCategories = ( videoID : string ) : Promise < LockCategory [ ] > => db . prepare ( "all" , 'SELECT * FROM "lockCategories" WHERE "videoID" = ?' , [ videoID ] ) ;
18
19
19
-
20
20
const goodResponse = ( ) : any => ( {
21
21
videoID : "test-videoid" ,
22
22
userID : "not-vip-test-userid" ,
23
23
categories : [ "sponsor" ] ,
24
24
actionTypes : [ "skip" ]
25
25
} ) ;
26
26
27
+ const errTest = ( method = "POST" , override : any ) : Promise < void > => {
28
+ const data = { ...goodResponse ( ) , ...override } ;
29
+ return client ( endpoint , { method, data } )
30
+ . then ( res => assert . strictEqual ( res . status , 400 ) ) ;
31
+ } ;
32
+
33
+ const statusTest = ( method = "POST" , data : any , status : number ) : Promise < void > =>
34
+ client ( endpoint , { method, data } )
35
+ . then ( res => assert . strictEqual ( res . status , status ) ) ;
36
+
27
37
describe ( "POST lockCategories HTTP submitting" , ( ) => {
28
38
before ( async ( ) => {
29
- const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)' ;
30
- await db . prepare ( "run" , insertVipUserQuery , [ lockVIPUserHash ] ) ;
31
- } ) ;
32
-
33
- it ( "Should update the database version when starting the application" , async ( ) => {
34
- const version = ( await db . prepare ( "get" , "SELECT key, value FROM config where key = ?" , [ "version" ] ) ) . value ;
35
- assert . ok ( version > 1 ) ;
39
+ await insertVipUser ( db , lockUser ) ;
36
40
} ) ;
37
41
38
- it ( "should be able to add poi type category by type skip" , ( done ) => {
39
- const videoID = "add-record-poi" ;
40
- client . post ( endpoint , {
42
+ it ( "should be able to add poi type category by type skip" , ( ) => {
43
+ const videoID = genRandomValue ( "video" , "lockCategoriesHttp" ) ;
44
+ const json = {
41
45
videoID,
42
- userID : lockVIPUser ,
46
+ userID : lockUser . privID ,
43
47
categories : [ "poi_highlight" ] ,
44
48
actionTypes : [ "skip" ]
45
- } )
46
- . then ( res => {
47
- assert . strictEqual ( res . status , 200 ) ;
48
- checkLockCategories ( videoID )
49
- . then ( result => {
50
- assert . strictEqual ( result . length , 1 ) ;
51
- assert . strictEqual ( result [ 0 ] , "poi_highlight" ) ;
52
- } ) ;
53
- done ( ) ;
54
- } )
55
- . catch ( err => done ( err ) ) ;
49
+ } ;
50
+ return statusTest ( "POST" , json , 200 )
51
+ . then ( ( ) => checkLockCategories ( videoID ) )
52
+ . then ( result => {
53
+ assert . strictEqual ( result . length , 1 ) ;
54
+ assert . strictEqual ( result [ 0 ] . category , "poi_highlight" ) ;
55
+ } ) ;
56
56
} ) ;
57
57
58
- it ( "Should not add lock of invalid type" , ( done ) => {
59
- const videoID = "add_invalid_type" ;
60
- client . post ( endpoint , {
58
+ it ( "Should not add lock of invalid type" , ( ) => {
59
+ const videoID = genRandomValue ( "video" , "lockCategoriesHttp" ) ;
60
+ const json = {
61
61
videoID,
62
- userID : lockVIPUser ,
62
+ userID : lockUser . privID ,
63
63
categories : [ "future_unused_invalid_type" ] ,
64
64
actionTypes : [ "skip" ]
65
- } )
66
- . then ( res => {
67
- assert . strictEqual ( res . status , 200 ) ;
68
- checkLockCategories ( videoID )
69
- . then ( result => {
70
- assert . strictEqual ( result . length , 0 ) ;
71
- } ) ;
72
- done ( ) ;
73
- } )
74
- . catch ( err => done ( err ) ) ;
65
+ } ;
66
+ return statusTest ( "POST" , json , 200 )
67
+ . then ( ( ) => checkLockCategories ( videoID ) )
68
+ . then ( result => assert . strictEqual ( result . length , 0 ) ) ;
75
69
} ) ;
76
70
} ) ;
77
71
78
72
describe ( "DELETE lockCategories 403/400 tests" , ( ) => {
79
- it ( " Should return 400 for no data" , ( done ) => {
80
- client . delete ( endpoint , { } )
81
- . then ( res => {
82
- assert . strictEqual ( res . status , 400 ) ;
83
- done ( ) ;
84
- } )
85
- . catch ( err => done ( err ) ) ;
86
- } ) ;
73
+ it ( "Should return 400 for no data" , ( ) => statusTest ( "DELETE" , { } , 400 ) ) ;
74
+ it ( "Should return 400 for no data" , ( ) => statusTest ( "POST" , { } , 400 ) ) ;
87
75
88
- it ( "Should return 400 for no categories" , ( done ) => {
76
+ it ( "Should return 400 for no categories" , ( ) => {
89
77
const json : any = {
90
78
videoID : "test" ,
91
79
userID : "test" ,
92
80
categories : [ ] ,
93
81
} ;
94
- client . delete ( endpoint , json )
95
- . then ( res => {
96
- assert . strictEqual ( res . status , 400 ) ;
97
- done ( ) ;
98
- } )
99
- . catch ( err => done ( err ) ) ;
82
+ return statusTest ( "DELETE" , json , 400 ) ;
100
83
} ) ;
101
84
102
- it ( "Should return 400 for no userID" , ( done ) => {
85
+ it ( "Should return 400 for no userID" , ( ) => {
103
86
const json : any = {
104
87
videoID : "test" ,
105
88
userID : null ,
106
89
categories : [ "sponsor" ] ,
107
90
} ;
108
-
109
- client . post ( endpoint , json )
110
- . then ( res => {
111
- assert . strictEqual ( res . status , 400 ) ;
112
- done ( ) ;
113
- } )
114
- . catch ( err => done ( err ) ) ;
91
+ return statusTest ( "POST" , json , 400 ) ;
115
92
} ) ;
116
93
117
- it ( "Should return 400 for no videoID" , ( done ) => {
94
+ it ( "Should return 400 for no videoID" , ( ) => {
118
95
const json : any = {
119
96
videoID : null ,
120
97
userID : "test" ,
121
98
categories : [ "sponsor" ] ,
122
99
} ;
123
-
124
- client . post ( endpoint , json )
125
- . then ( res => {
126
- assert . strictEqual ( res . status , 400 ) ;
127
- done ( ) ;
128
- } )
129
- . catch ( err => done ( err ) ) ;
100
+ return statusTest ( "POST" , json , 400 ) ;
130
101
} ) ;
131
102
132
- it ( "Should return 400 for invalid category array" , ( done ) => {
103
+ it ( "Should return 400 for invalid category array" , ( ) => {
133
104
const json = {
134
105
videoID : "test" ,
135
106
userID : "test" ,
136
107
categories : { } ,
137
108
} ;
138
-
139
- client . post ( endpoint , json )
140
- . then ( res => {
141
- assert . strictEqual ( res . status , 400 ) ;
142
- done ( ) ;
143
- } )
144
- . catch ( err => done ( err ) ) ;
145
- } ) ;
146
-
147
- it ( "Should return 400 for bad format categories" , ( done ) => {
148
- const json = {
149
- videoID : "test" ,
150
- userID : "test" ,
151
- categories : "sponsor" ,
152
- } ;
153
-
154
- client . post ( endpoint , json )
155
- . then ( res => {
156
- assert . strictEqual ( res . status , 400 ) ;
157
- done ( ) ;
158
- } )
159
- . catch ( err => done ( err ) ) ;
160
- } ) ;
161
-
162
- it ( "Should return 403 if user is not VIP" , ( done ) => {
163
- const json = {
164
- videoID : "test" ,
165
- userID : "test" ,
166
- categories : [
167
- "sponsor" ,
168
- ] ,
169
- } ;
170
-
171
- client . post ( endpoint , json )
172
- . then ( res => {
173
- assert . strictEqual ( res . status , 403 ) ;
174
- done ( ) ;
175
- } )
176
- . catch ( err => done ( err ) ) ;
109
+ return statusTest ( "POST" , json , 400 ) ;
177
110
} ) ;
178
111
} ) ;
179
112
180
113
describe ( "manual DELETE/POST lockCategories 400 tests" , ( ) => {
181
- it ( "DELETE Should return 400 for no data" , ( done ) => {
182
- client . delete ( endpoint , { data : { } } )
183
- . then ( res => {
184
- assert . strictEqual ( res . status , 400 ) ;
185
- done ( ) ;
186
- } )
187
- . catch ( err => done ( err ) ) ;
188
- } ) ;
189
- it ( "POST Should return 400 for no data" , ( done ) => {
190
- client . post ( endpoint , { } )
191
- . then ( res => {
192
- assert . strictEqual ( res . status , 400 ) ;
193
- done ( ) ;
194
- } )
195
- . catch ( err => done ( err ) ) ;
196
- } ) ;
197
- it ( "DELETE Should return 400 for bad format categories" , ( done ) => {
198
- const data = goodResponse ( ) ;
199
- data . categories = "sponsor" ;
200
- client . delete ( endpoint , { data } )
201
- . then ( res => {
202
- assert . strictEqual ( res . status , 400 ) ;
203
- done ( ) ;
204
- } )
205
- . catch ( err => done ( err ) ) ;
206
- } ) ;
207
- it ( "POST Should return 400 for bad format categories" , ( done ) => {
208
- const data = goodResponse ( ) ;
209
- data . categories = "sponsor" ;
210
- client . post ( endpoint , data )
211
- . then ( res => {
212
- assert . strictEqual ( res . status , 400 ) ;
213
- done ( ) ;
214
- } )
215
- . catch ( err => done ( err ) ) ;
216
- } ) ;
217
- it ( "DELETE Should return 403 if user is not VIP" , ( done ) => {
218
- const data = goodResponse ( ) ;
219
- client . delete ( endpoint , { data } )
220
- . then ( res => {
221
- assert . strictEqual ( res . status , 403 ) ;
222
- done ( ) ;
223
- } )
224
- . catch ( err => done ( err ) ) ;
225
- } ) ;
226
- it ( "POST Should return 403 if user is not VIP" , ( done ) => {
227
- const data = goodResponse ( ) ;
228
- client . post ( endpoint , data )
229
- . then ( res => {
230
- assert . strictEqual ( res . status , 403 ) ;
231
- done ( ) ;
232
- } )
233
- . catch ( err => done ( err ) ) ;
234
- } ) ;
114
+ it ( "DELETE Should return 400 for no data" , ( ) => statusTest ( "DELETE" , { } , 400 ) ) ;
115
+ it ( "POST Should return 400 for no data" , ( ) => statusTest ( "POST" , { } , 400 ) ) ;
116
+
117
+ it ( "DELETE Should return 400 for bad format categories" , ( ) => errTest ( "DELETE" , { categories : "sponsor" } ) ) ;
118
+ it ( "POST Should return 400 for bad format categories" , ( ) => errTest ( "POST" , { categories : "sponsor" } ) ) ;
119
+
120
+ it ( "DELETE Should return 403 if user is not VIP" , ( ) => statusTest ( "DELETE" , goodResponse ( ) , 403 ) ) ;
121
+ it ( "POST Should return 403 if user is not VIP" , ( ) => statusTest ( "POST" , goodResponse ( ) , 403 ) ) ;
235
122
} ) ;
236
123
237
124
describe ( "array of DELETE/POST lockCategories 400 tests" , ( ) => {
238
125
for ( const key of [ "videoID" , "userID" , "categories" ] ) {
239
126
for ( const method of [ "DELETE" , "POST" ] ) {
240
- it ( `${ method } - Should return 400 for invalid ${ key } ` , ( done ) => {
241
- const data = goodResponse ( ) ;
242
- data [ key ] = null ;
243
- client ( endpoint , { data, method } )
244
- . then ( res => {
245
- assert . strictEqual ( res . status , 400 ) ;
246
- done ( ) ;
247
- } )
248
- . catch ( err => done ( err ) ) ;
249
- } ) ;
127
+ it ( `${ method } - Should return 400 for invalid ${ key } ` , ( ) =>
128
+ errTest ( method , { [ key ] : null } )
129
+ ) ;
250
130
}
251
131
}
252
132
} ) ;
0 commit comments