@@ -6,7 +6,13 @@ import {
6
6
createMockFile ,
7
7
} from "../test-helpers" ;
8
8
import { v4 as uuid } from "uuid" ;
9
- import { Asset , AssetPatchPayload , Task , User } from "../schema/types" ;
9
+ import {
10
+ ApiToken ,
11
+ Asset ,
12
+ AssetPatchPayload ,
13
+ Task ,
14
+ User ,
15
+ } from "../schema/types" ;
10
16
import { db } from "../store" ;
11
17
import { WithID } from "../store/types" ;
12
18
import Table from "../store/table" ;
@@ -76,6 +82,39 @@ describe("controllers/asset", () => {
76
82
let nonAdminUser : User ;
77
83
let nonAdminToken : string ;
78
84
85
+ const createProject = async ( ) => {
86
+ let res = await client . post ( `/project` ) ;
87
+ expect ( res . status ) . toBe ( 201 ) ;
88
+ const project = await res . json ( ) ;
89
+ expect ( project ) . toBeDefined ( ) ;
90
+ return project ;
91
+ } ;
92
+
93
+ const allowedOrigins = [
94
+ "http://localhost:3000" ,
95
+ "https://staging.wetube.com" ,
96
+ "http://blockflix.io:69" ,
97
+ ] ;
98
+
99
+ const createApiToken = async (
100
+ cors : ApiToken [ "access" ] [ "cors" ] ,
101
+ projectId : string
102
+ ) => {
103
+ client . jwtAuth = nonAdminToken ;
104
+ let res = await client . post ( `/api-token/?projectId=${ projectId } ` , {
105
+ name : "test" ,
106
+ access : { cors } ,
107
+ } ) ;
108
+ client . jwtAuth = null ;
109
+ expect ( res . status ) . toBe ( 201 ) ;
110
+ const apiKeyObj = await res . json ( ) ;
111
+ expect ( apiKeyObj ) . toMatchObject ( {
112
+ id : expect . any ( String ) ,
113
+ access : { cors } ,
114
+ } ) ;
115
+ return apiKeyObj . id ;
116
+ } ;
117
+
79
118
beforeEach ( async ( ) => {
80
119
await db . objectStore . create ( {
81
120
id : "mock_vod_store" ,
@@ -103,6 +142,7 @@ describe("controllers/asset", () => {
103
142
type : "url" ,
104
143
url : spec . url ,
105
144
} ,
145
+ projectId : "" , //should be blank when using jwt and projectId not specified as query-param
106
146
status : { phase : "waiting" } ,
107
147
} ) ;
108
148
@@ -123,6 +163,7 @@ describe("controllers/asset", () => {
123
163
124
164
client . jwtAuth = null ;
125
165
client . apiKey = adminApiKey ;
166
+
126
167
res = await client . post ( `/task/${ taskId } /status` , {
127
168
status : {
128
169
phase : "running" ,
@@ -148,6 +189,103 @@ describe("controllers/asset", () => {
148
189
} ) ;
149
190
} ) ;
150
191
192
+ it . only ( "should import asset (using jwt) for existing project (created with jwt)" , async ( ) => {
193
+ const spec = {
194
+ name : "test" ,
195
+ url : "https://example.com/test.mp4" ,
196
+ } ;
197
+ const projectId = await createProject ( ) ;
198
+
199
+ let res = await client . post (
200
+ `/asset/upload/url/?projectId=${ projectId } ` ,
201
+ spec
202
+ ) ;
203
+ expect ( res . status ) . toBe ( 201 ) ;
204
+ const { asset, task } = await res . json ( ) ;
205
+ expect ( asset ) . toMatchObject ( {
206
+ id : expect . any ( String ) ,
207
+ name : "test" ,
208
+ source : {
209
+ type : "url" ,
210
+ url : spec . url ,
211
+ } ,
212
+ projectId : `${ projectId } ` ,
213
+ status : { phase : "waiting" } ,
214
+ } ) ;
215
+
216
+ client . jwtAuth = null ;
217
+ client . apiKey = adminApiKey ;
218
+
219
+ res = await client . get ( `/project/${ projectId } ` ) ;
220
+ const project = await res . json ( ) ;
221
+ expect ( res . status ) . toBe ( 200 ) ;
222
+ expect ( project ) . toBeDefined ( ) ; //api-key be retrieve if adminApiKey is used..
223
+ } ) ;
224
+
225
+ it . only ( "should import asset (using api-token) for existing project (created with jwt)" , async ( ) => {
226
+ const spec = {
227
+ name : "test" ,
228
+ url : "https://example.com/test.mp4" ,
229
+ } ;
230
+ const projectId = await createProject ( ) ;
231
+
232
+ client . jwtAuth = null ;
233
+ client . apiKey = await createApiToken ( { allowedOrigins } , projectId ) ;
234
+
235
+ let res = await client . post ( `/asset/upload/url/` , spec ) ;
236
+ expect ( res . status ) . toBe ( 201 ) ;
237
+ const { asset, task } = await res . json ( ) ;
238
+ expect ( asset ) . toMatchObject ( {
239
+ id : expect . any ( String ) ,
240
+ name : "test" ,
241
+ source : {
242
+ type : "url" ,
243
+ url : spec . url ,
244
+ } ,
245
+ projectId : `${ projectId } ` ,
246
+ status : { phase : "waiting" } ,
247
+ } ) ;
248
+
249
+ client . apiKey = adminApiKey ;
250
+ res = await client . get ( `/project/${ projectId } ` ) ;
251
+ const project = await res . json ( ) ;
252
+ expect ( res . status ) . toBe ( 200 ) ;
253
+ expect ( project . id ) . toBeDefined ( ) ;
254
+ } ) ;
255
+
256
+ it ( "should NOT import asset (using api-key) when projectId passed as ouery-param" , async ( ) => {
257
+ const spec = {
258
+ name : "test" ,
259
+ url : "https://example.com/test.mp4" ,
260
+ } ;
261
+
262
+ client . jwtAuth = null ;
263
+ client . apiKey = adminApiKey ;
264
+
265
+ const projectId = await createProject ( ) ;
266
+
267
+ // BadRequest is expected if projectId is passed in as query-param
268
+ let res = await client . post (
269
+ `/asset/upload/url/?projectId=${ projectId } ` ,
270
+ spec
271
+ ) ;
272
+ expect ( res . status ) . toBe ( 400 ) ;
273
+
274
+ // Let's try again without query-param
275
+ res = await client . post ( `/asset/upload/url/` , spec ) ;
276
+ const { asset, task } = await res . json ( ) ;
277
+ expect ( asset ) . toMatchObject ( {
278
+ id : expect . any ( String ) ,
279
+ name : "test" ,
280
+ source : {
281
+ type : "url" ,
282
+ url : spec . url ,
283
+ } ,
284
+ projectId : "" , //should be blank when using an existing api-key and new project was created
285
+ status : { phase : "waiting" } ,
286
+ } ) ;
287
+ } ) ;
288
+
151
289
it ( "should detect duplicate assets" , async ( ) => {
152
290
const spec = {
153
291
name : "test" ,
0 commit comments