@@ -3280,44 +3280,152 @@ def test_investigate_fragment(self):
32803280 "draft-this-should-not-be-possible-00.txt" ,
32813281 )
32823282
3283- def test_investigate (self ):
3283+ def test_investigate_get (self ):
3284+ """GET with no querystring should retrieve the investigate UI"""
32843285 url = urlreverse ("ietf.doc.views_doc.investigate" )
32853286 login_testing_unauthorized (self , "secretary" , url )
32863287 r = self .client .get (url )
32873288 self .assertEqual (r .status_code , 200 )
32883289 q = PyQuery (r .content )
32893290 self .assertEqual (len (q ("form#investigate" )), 1 )
32903291 self .assertEqual (len (q ("div#results" )), 0 )
3291- r = self .client .post (url , dict (name_fragment = "this-is-not-found" ))
3292+
3293+ @mock .patch ("ietf.doc.views_doc.AsyncResult" )
3294+ def test_investgate_get_task_id (self , mock_asyncresult ):
3295+ """GET with querystring should lookup task status"""
3296+ url = urlreverse ("ietf.doc.views_doc.investigate" )
3297+ login_testing_unauthorized (self , "secretary" , url )
3298+ mock_asyncresult .return_value .ready .return_value = True
3299+ r = self .client .get (url + "?id=a-task-id" )
3300+ self .assertEqual (r .status_code , 200 )
3301+ self .assertEqual (r .json (), {"status" : "ready" })
3302+ self .assertTrue (mock_asyncresult .called )
3303+ self .assertEqual (mock_asyncresult .call_args , mock .call ("a-task-id" ))
3304+ mock_asyncresult .reset_mock ()
3305+
3306+ mock_asyncresult .return_value .ready .return_value = False
3307+ r = self .client .get (url + "?id=a-task-id" )
3308+ self .assertEqual (r .status_code , 200 )
3309+ self .assertEqual (r .json (), {"status" : "notready" })
3310+ self .assertTrue (mock_asyncresult .called )
3311+ self .assertEqual (mock_asyncresult .call_args , mock .call ("a-task-id" ))
3312+
3313+ @mock .patch ("ietf.doc.views_doc.investigate_fragment_task" )
3314+ def test_investigate_post (self , mock_investigate_fragment_task ):
3315+ """POST with a name_fragment and no task_id should start a celery task"""
3316+ url = urlreverse ("ietf.doc.views_doc.investigate" )
3317+ login_testing_unauthorized (self , "secretary" , url )
3318+
3319+ # test some invalid cases
3320+ r = self .client .post (url , {"name_fragment" : "short" }) # limit is >= 8 characters
32923321 self .assertEqual (r .status_code , 200 )
32933322 q = PyQuery (r .content )
3323+ self .assertEqual (len (q ("#id_name_fragment.is-invalid" )), 1 )
3324+ self .assertFalse (mock_investigate_fragment_task .delay .called )
3325+ for char in ["*" , "%" , "/" , "\\ " ]:
3326+ r = self .client .post (url , {"name_fragment" : f"bad{ char } character" })
3327+ self .assertEqual (r .status_code , 200 )
3328+ q = PyQuery (r .content )
3329+ self .assertEqual (len (q ("#id_name_fragment.is-invalid" )), 1 )
3330+ self .assertFalse (mock_investigate_fragment_task .delay .called )
3331+
3332+ # now a valid one
3333+ mock_investigate_fragment_task .delay .return_value .id = "a-task-id"
3334+ r = self .client .post (url , {"name_fragment" : "this-is-a-valid-fragment" })
3335+ self .assertEqual (r .status_code , 200 )
3336+ self .assertTrue (mock_investigate_fragment_task .delay .called )
3337+ self .assertEqual (mock_investigate_fragment_task .delay .call_args , mock .call ("this-is-a-valid-fragment" ))
3338+ self .assertEqual (r .json (), {"id" : "a-task-id" })
3339+
3340+ @mock .patch ("ietf.doc.views_doc.AsyncResult" )
3341+ def test_investigate_post_task_id (self , mock_asyncresult ):
3342+ """POST with name_fragment and task_id should retrieve results"""
3343+ url = urlreverse ("ietf.doc.views_doc.investigate" )
3344+ login_testing_unauthorized (self , "secretary" , url )
3345+
3346+ # First, test a non-successful result - this could be a failure or non-existent task id
3347+ mock_result = mock_asyncresult .return_value
3348+ mock_result .successful .return_value = False
3349+ r = self .client .post (url , {"name_fragment" : "some-fragment" , "task_id" : "a-task-id" })
3350+ self .assertContains (r , "The investigation task failed." , status_code = 200 )
3351+ self .assertTrue (mock_asyncresult .called )
3352+ self .assertEqual (mock_asyncresult .call_args , mock .call ("a-task-id" ))
3353+ self .assertFalse (mock_result .get .called )
3354+ mock_asyncresult .reset_mock ()
3355+ q = PyQuery (r .content )
3356+ self .assertEqual (q ("#id_name_fragment" ).val (), "some-fragment" )
3357+ self .assertEqual (q ("#id_task_id" ).val (), "a-task-id" )
3358+
3359+ # now the various successful result mixes
3360+ mock_result = mock_asyncresult .return_value
3361+ mock_result .successful .return_value = True
3362+ mock_result .get .return_value = {
3363+ "name_fragment" : "different-fragment" ,
3364+ "results" : {
3365+ "can_verify" : set (),
3366+ "unverifiable_collections" : set (),
3367+ "unexpected" : set (),
3368+ }
3369+ }
3370+ r = self .client .post (url , {"name_fragment" : "some-fragment" , "task_id" : "a-task-id" })
3371+ self .assertEqual (r .status_code , 200 )
3372+ self .assertTrue (mock_asyncresult .called )
3373+ self .assertEqual (mock_asyncresult .call_args , mock .call ("a-task-id" ))
3374+ mock_asyncresult .reset_mock ()
3375+ q = PyQuery (r .content )
3376+ self .assertEqual (q ("#id_name_fragment" ).val (), "different-fragment" , "name_fragment should be reset" )
3377+ self .assertEqual (q ("#id_task_id" ).val (), "" , "task_id should be cleared" )
32943378 self .assertEqual (len (q ("div#results" )), 1 )
32953379 self .assertEqual (len (q ("table#authenticated" )), 0 )
32963380 self .assertEqual (len (q ("table#unverifiable" )), 0 )
32973381 self .assertEqual (len (q ("table#unexpected" )), 0 )
3298- r = self .client .post (url , dict (name_fragment = "mixed-provenance" ))
3382+
3383+ # This file was created in setUp. It allows the view to render properly
3384+ # but its location / content don't matter for this test otherwise.
3385+ a_file_that_exists = Path (settings .INTERNET_DRAFT_PATH ) / "draft-this-is-active-00.txt"
3386+
3387+ mock_result .get .return_value = {
3388+ "name_fragment" : "different-fragment" ,
3389+ "results" : {
3390+ "can_verify" : {a_file_that_exists },
3391+ "unverifiable_collections" : {a_file_that_exists },
3392+ "unexpected" : set (),
3393+ }
3394+ }
3395+ r = self .client .post (url , {"name_fragment" : "some-fragment" , "task_id" : "a-task-id" })
32993396 self .assertEqual (r .status_code , 200 )
3397+ self .assertTrue (mock_asyncresult .called )
3398+ self .assertEqual (mock_asyncresult .call_args , mock .call ("a-task-id" ))
3399+ mock_asyncresult .reset_mock ()
33003400 q = PyQuery (r .content )
3401+ self .assertEqual (q ("#id_name_fragment" ).val (), "different-fragment" , "name_fragment should be reset" )
3402+ self .assertEqual (q ("#id_task_id" ).val (), "" , "task_id should be cleared" )
33013403 self .assertEqual (len (q ("div#results" )), 1 )
33023404 self .assertEqual (len (q ("table#authenticated" )), 1 )
33033405 self .assertEqual (len (q ("table#unverifiable" )), 1 )
33043406 self .assertEqual (len (q ("table#unexpected" )), 0 )
3305- r = self .client .post (url , dict (name_fragment = "not-be-possible" ))
3407+
3408+ mock_result .get .return_value = {
3409+ "name_fragment" : "different-fragment" ,
3410+ "results" : {
3411+ "can_verify" : set (),
3412+ "unverifiable_collections" : set (),
3413+ "unexpected" : {a_file_that_exists },
3414+ }
3415+ }
3416+ r = self .client .post (url , {"name_fragment" : "some-fragment" , "task_id" : "a-task-id" })
33063417 self .assertEqual (r .status_code , 200 )
3418+ self .assertTrue (mock_asyncresult .called )
3419+ self .assertEqual (mock_asyncresult .call_args , mock .call ("a-task-id" ))
3420+ mock_asyncresult .reset_mock ()
33073421 q = PyQuery (r .content )
3422+ self .assertEqual (q ("#id_name_fragment" ).val (), "different-fragment" , "name_fragment should be reset" )
3423+ self .assertEqual (q ("#id_task_id" ).val (), "" , "task_id should be cleared" )
33083424 self .assertEqual (len (q ("div#results" )), 1 )
33093425 self .assertEqual (len (q ("table#authenticated" )), 0 )
33103426 self .assertEqual (len (q ("table#unverifiable" )), 0 )
33113427 self .assertEqual (len (q ("table#unexpected" )), 1 )
3312- r = self .client .post (url , dict (name_fragment = "short" ))
3313- self .assertEqual (r .status_code , 200 )
3314- q = PyQuery (r .content )
3315- self .assertEqual (len (q ("#id_name_fragment.is-invalid" )), 1 )
3316- for char in ["*" , "%" , "/" , "\\ " ]:
3317- r = self .client .post (url , dict (name_fragment = f"bad{ char } character" ))
3318- self .assertEqual (r .status_code , 200 )
3319- q = PyQuery (r .content )
3320- self .assertEqual (len (q ("#id_name_fragment.is-invalid" )), 1 )
3428+
33213429
33223430class LogIOErrorTests (TestCase ):
33233431
0 commit comments