11
11
import os
12
12
13
13
from django .contrib .auth .models import Group , User
14
+ from django .core import signing
14
15
from django .test import TestCase
15
16
from rest_framework import status
16
17
from rest_framework .test import APIClient
17
18
18
19
from minecode .models import ScannableURI
20
+ from minecode .utils import get_webhook_url
19
21
from minecode .utils_test import JsonBasedTesting
20
22
from packagedb .models import Package , Resource
21
23
@@ -34,6 +36,7 @@ def setUp(self):
34
36
self .scan_queue_worker_auth = f"Token { self .scan_queue_worker_user .auth_token .key } "
35
37
self .scan_queue_worker_client = APIClient (enforce_csrf_checks = True )
36
38
self .scan_queue_worker_client .credentials (HTTP_AUTHORIZATION = self .scan_queue_worker_auth )
39
+ self .scan_queue_worker_user_id_str = str (self .scan_queue_worker_user .id )
37
40
38
41
# create a staff user
39
42
self .staff_user = User .objects .create_user (
@@ -120,126 +123,79 @@ def test_api_scannable_uri_list_endpoint(self):
120
123
self .assertEqual (3 , response .data .get ('count' ))
121
124
122
125
def test_api_scannable_uri_get_next_download_url (self ):
126
+ def check_webhook_url (self , webhook_url ):
127
+ webhook_url = response .data .get ('webhook_url' )
128
+ key = webhook_url .rstrip ('/' ).split ('/' )[- 1 ]
129
+ self .assertIn ('/api/scan_queue/index_package_scan/' , webhook_url )
130
+ self .assertEqual (signing .loads (key ), str (self .scan_queue_worker_user .id ))
131
+
123
132
response = self .scan_queue_worker_client .get ('/api/scan_queue/get_next_download_url/' )
124
133
self .assertEqual (response .status_code , status .HTTP_200_OK )
125
134
self .assertEqual (response .data .get ('scannable_uri_uuid' ), self .scannable_uri1 .uuid )
126
135
self .assertEqual (response .data .get ('download_url' ), self .scannable_uri1 .uri )
136
+ check_webhook_url (self , response .data .get ('webhook_url' ))
127
137
128
138
response = self .scan_queue_worker_client .get ('/api/scan_queue/get_next_download_url/' )
129
139
self .assertEqual (response .status_code , status .HTTP_200_OK )
130
140
self .assertEqual (response .data .get ('scannable_uri_uuid' ), self .scannable_uri2 .uuid )
131
141
self .assertEqual (response .data .get ('download_url' ), self .scannable_uri2 .uri )
142
+ check_webhook_url (self , response .data .get ('webhook_url' ))
132
143
133
144
response = self .scan_queue_worker_client .get ('/api/scan_queue/get_next_download_url/' )
134
145
self .assertEqual (response .status_code , status .HTTP_200_OK )
135
146
self .assertEqual (response .data .get ('scannable_uri_uuid' ), self .scannable_uri3 .uuid )
136
147
self .assertEqual (response .data .get ('download_url' ), self .scannable_uri3 .uri )
148
+ check_webhook_url (self , response .data .get ('webhook_url' ))
137
149
138
150
response = self .scan_queue_worker_client .get ('/api/scan_queue/get_next_download_url/' )
139
151
self .assertEqual (response .status_code , status .HTTP_200_OK )
140
152
self .assertEqual (response .data .get ('scannable_uri_uuid' ), '' )
141
153
self .assertEqual (response .data .get ('download_url' ), '' )
154
+ self .assertEqual (response .data .get ('webhook_url' ), '' )
142
155
143
156
response = self .staff_client .get ('/api/scan_queue/get_next_download_url/' )
144
157
self .assertEqual (response .status_code , status .HTTP_200_OK )
145
158
self .assertEqual (response .data .get ('scannable_uri_uuid' ), '' )
146
159
self .assertEqual (response .data .get ('download_url' ), '' )
160
+ self .assertEqual (response .data .get ('webhook_url' ), '' )
147
161
148
162
def test_api_scannable_uri_update_status (self ):
163
+ scannable_uri1_uuid = self .scannable_uri1 .uuid
164
+ scannable_uri2_uuid = self .scannable_uri2 .uuid
165
+ scannable_uri1_update_status_url = f'/api/scan_queue/{ scannable_uri1_uuid } /update_status/'
166
+ scannable_uri2_update_status_url = f'/api/scan_queue/{ scannable_uri2_uuid } /update_status/'
167
+
149
168
self .assertEqual (ScannableURI .SCAN_NEW , self .scannable_uri1 .scan_status )
150
169
data = {
151
- "scannable_uri_uuid" : self . scannable_uri1 . uuid ,
170
+ "scannable_uri_uuid" : scannable_uri1_uuid ,
152
171
"scan_status" : 'failed' ,
153
172
'scan_log' : 'scan_log' ,
154
173
}
155
- response = self .scan_queue_worker_client .post ('/api/scan_queue/update_status/' , data = data )
174
+ response = self .scan_queue_worker_client .post (scannable_uri1_update_status_url , data = data )
156
175
self .assertEqual (response .status_code , status .HTTP_200_OK )
157
176
self .scannable_uri1 .refresh_from_db ()
158
177
self .assertEqual (ScannableURI .SCAN_FAILED , self .scannable_uri1 .scan_status )
159
178
self .assertEqual ('scan_log' , self .scannable_uri1 .scan_error )
160
179
161
- self .assertFalse (self .package2 .md5 )
162
- self .assertFalse (self .package2 .sha1 )
163
- self .assertFalse (self .package2 .sha256 )
164
- self .assertFalse (self .package2 .sha512 )
165
- self .assertFalse (self .package2 .size )
166
- self .assertFalse (self .package2 .declared_license_expression )
167
- self .assertFalse (self .package2 .copyright )
168
- self .assertEqual (0 , Resource .objects .all ().count ())
169
- scan_file_location = self .get_test_loc ('scancodeio/get_scan_data.json' )
170
- summary_file_location = self .get_test_loc ('scancodeio/scan_summary_response.json' )
171
- project_extra_data = {
172
- 'md5' : 'md5' ,
173
- 'sha1' : 'sha1' ,
174
- 'sha256' : 'sha256' ,
175
- 'sha512' : 'sha512' ,
176
- 'size' : 100 ,
177
- }
178
- with open (scan_file_location ) as scan_file :
179
- with open (summary_file_location ) as summary_file :
180
- data = {
181
- "scannable_uri_uuid" : self .scannable_uri2 .uuid ,
182
- "scan_status" : 'scanned' ,
183
- 'project_extra_data' : json .dumps (project_extra_data ),
184
- 'scan_results_file' : scan_file ,
185
- 'scan_summary_file' : summary_file ,
186
- }
187
- response = self .scan_queue_worker_client .post ('/api/scan_queue/update_status/' , data = data )
188
- self .assertEqual (response .status_code , status .HTTP_200_OK )
189
- self .scannable_uri2 .refresh_from_db ()
190
- self .assertEqual (ScannableURI .SCAN_INDEXED , self .scannable_uri2 .scan_status )
191
- self .package2 .refresh_from_db ()
192
- self .assertEqual ('md5' , self .package2 .md5 )
193
- self .assertEqual ('sha1' , self .package2 .sha1 )
194
- self .assertEqual ('sha256' , self .package2 .sha256 )
195
- self .assertEqual ('sha512' , self .package2 .sha512 )
196
- self .assertEqual (100 , self .package2 .size )
197
- self .assertEqual ('apache-2.0' , self .package2 .declared_license_expression )
198
- self .assertEqual ('Copyright (c) Apache Software Foundation' , self .package2 .copyright )
199
- self .assertFalse (self .scannable_uri2 .scan_error )
200
- self .assertEqual (64 , Resource .objects .all ().count ())
201
-
202
- data = {}
203
- response = self .scan_queue_worker_client .post ('/api/scan_queue/update_status/' , data = data )
204
- expected_response = {'error' : 'missing scannable_uri_uuid' }
205
- self .assertEqual (expected_response , response .data )
206
-
207
180
data = {
208
- 'scannable_uri_uuid' : self .scannable_uri2 .uuid ,
209
181
'scan_status' : ''
210
182
}
211
- response = self .scan_queue_worker_client .post ('/api/scan_queue/update_status/' , data = data )
183
+ response = self .scan_queue_worker_client .post (scannable_uri2_update_status_url , data = data )
212
184
expected_response = {'error' : 'missing scan_status' }
213
185
self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
214
186
self .assertEqual (expected_response , response .data )
215
187
216
188
data = {
217
- 'scannable_uri_uuid' : self .scannable_uri2 .uuid ,
218
189
'scan_status' : 'invalid'
219
190
}
220
- response = self .scan_queue_worker_client .post ('/api/scan_queue/update_status/' , data = data )
191
+ response = self .scan_queue_worker_client .post (scannable_uri2_update_status_url , data = data )
221
192
expected_response = {'error' : 'invalid scan_status: invalid' }
222
193
self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
223
194
self .assertEqual (expected_response , response .data )
224
195
225
- data = {
226
- 'scannable_uri_uuid' : 'asdf' ,
227
- 'scan_status' : 'scanned'
228
- }
229
- response = self .scan_queue_worker_client .post ('/api/scan_queue/update_status/' , data = data )
230
- expected_response = {'error' : 'invalid scannable_uri_uuid: asdf' }
231
- self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
232
- self .assertEqual (expected_response , response .data )
233
-
234
- # Test that staff user can use endpoint
235
- data = {
236
- 'scannable_uri_uuid' : 'asdf' ,
237
- 'scan_status' : 'scanned'
238
- }
239
- response = self .staff_client .post ('/api/scan_queue/update_status/' , data = data )
240
- expected_response = {'error' : 'invalid scannable_uri_uuid: asdf' }
241
- self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
242
- self .assertEqual (expected_response , response .data )
196
+ data = {}
197
+ response = self .scan_queue_worker_client .post ('/api/scan_queue/asdf/' , data = data )
198
+ self .assertEqual (response .status_code , status .HTTP_405_METHOD_NOT_ALLOWED )
243
199
244
200
def test_api_scannable_uri_update_status_update_finished_scannable_uri (self ):
245
201
scannable_uri_uuid = self .scannable_uri3 .uuid
@@ -255,11 +211,63 @@ def test_api_scannable_uri_update_status_update_finished_scannable_uri(self):
255
211
'scannable_uri_uuid' : scannable_uri_uuid ,
256
212
'scan_status' : 'scanned'
257
213
}
258
- response = self .scan_queue_worker_client .post ('/api/scan_queue/update_status/' , data = data )
214
+ response = self .scan_queue_worker_client .post (
215
+ f'/api/scan_queue/{ scannable_uri_uuid } /update_status/' , data = data
216
+ )
259
217
expected_response = {
260
218
'error' : 'cannot update status for scannable_uri '
261
219
f'{ self .scannable_uri3 .uuid } : scannable_uri has finished '
262
220
f'with status "{ ScannableURI .SCAN_STATUSES_BY_CODE [scan_status ]} "'
263
221
}
264
222
self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
265
223
self .assertEqual (expected_response , response .data )
224
+
225
+ def test_api_scannable_uri_index_package_scan (self ):
226
+ self .assertFalse (self .package2 .md5 )
227
+ self .assertFalse (self .package2 .sha1 )
228
+ self .assertFalse (self .package2 .sha256 )
229
+ self .assertFalse (self .package2 .sha512 )
230
+ self .assertFalse (self .package2 .size )
231
+ self .assertFalse (self .package2 .declared_license_expression )
232
+ self .assertFalse (self .package2 .copyright )
233
+ self .assertEqual (0 , Resource .objects .all ().count ())
234
+ scan_file_location = self .get_test_loc ('scancodeio/get_scan_data.json' )
235
+ summary_file_location = self .get_test_loc ('scancodeio/scan_summary_response.json' )
236
+ project_extra_data = {
237
+ 'scannable_uri_uuid' : self .scannable_uri2 .uuid ,
238
+ 'md5' : 'md5' ,
239
+ 'sha1' : 'sha1' ,
240
+ 'sha256' : 'sha256' ,
241
+ 'sha512' : 'sha512' ,
242
+ 'size' : 100 ,
243
+ }
244
+ with (
245
+ open (scan_file_location ) as scan_file ,
246
+ open (summary_file_location ) as summary_file
247
+ ):
248
+ results = json .load (scan_file )
249
+ summary = json .load (summary_file )
250
+ data = {
251
+ 'project' : {
252
+ 'extra_data' : project_extra_data ,
253
+ },
254
+ 'results' : results ,
255
+ 'summary' : summary ,
256
+ }
257
+
258
+ webhook_url = get_webhook_url ('index_package_scan' , self .scan_queue_worker_user .id )
259
+
260
+ response = self .scan_queue_worker_client .post (webhook_url , data = data , format = 'json' )
261
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
262
+ self .scannable_uri2 .refresh_from_db ()
263
+ self .assertEqual (ScannableURI .SCAN_INDEXED , self .scannable_uri2 .scan_status )
264
+ self .package2 .refresh_from_db ()
265
+ self .assertEqual ('md5' , self .package2 .md5 )
266
+ self .assertEqual ('sha1' , self .package2 .sha1 )
267
+ self .assertEqual ('sha256' , self .package2 .sha256 )
268
+ self .assertEqual ('sha512' , self .package2 .sha512 )
269
+ self .assertEqual (100 , self .package2 .size )
270
+ self .assertEqual ('apache-2.0' , self .package2 .declared_license_expression )
271
+ self .assertEqual ('Copyright (c) Apache Software Foundation' , self .package2 .copyright )
272
+ self .assertFalse (self .scannable_uri2 .scan_error )
273
+ self .assertEqual (64 , Resource .objects .all ().count ())
0 commit comments