def test_main_config_form_empty(self): """ test minimum form requirements / INVALID """ # get object form = MainConfigForm(data={}) # compare self.assertFalse(form.is_valid())
def test_main_config_form_statushistory_entry_numbers_filled(self): """ test minimum form requirements / INVALID """ # get object form = MainConfigForm(data={ 'statushistory_entry_numbers': 9, }) # compare self.assertFalse(form.is_valid())
def test_main_config_form_cron_username_filled(self): """ test minimum form requirements / VALID """ # get object form = MainConfigForm( data={ 'statushistory_entry_numbers': 7, 'cron_export_path': '/tmp', 'cron_username': '******', }) # compare self.assertTrue(form.is_valid())
def test_main_config_form_same_artifactstatus(self): """test custom field validation""" # create obects artifactstatus_1 = Artifactstatus.objects.create( artifactstatus_name='artifactstatus_1').artifactstatus_id artifactstatus_2 = Artifactstatus.objects.create( artifactstatus_name='artifactstatus_2').artifactstatus_id artifactstatus_3 = Artifactstatus.objects.create( artifactstatus_name='artifactstatus_3').artifactstatus_id artifactstatus_4 = Artifactstatus.objects.create( artifactstatus_name='artifactstatus_4').artifactstatus_id artifactstatus_5 = Artifactstatus.objects.create( artifactstatus_name='artifactstatus_5').artifactstatus_id # get object form = MainConfigForm( data={ 'statushistory_entry_numbers': 4, 'cron_export_path': '/tmp', 'cron_username': '******', 'main_overview': 'main_overview_system', 'artifactstatus_requested': [ artifactstatus_1, artifactstatus_2, artifactstatus_3, ], 'artifactstatus_acquisition': [ artifactstatus_3, artifactstatus_4, artifactstatus_5, ], }) # compare self.assertFalse(form.is_valid()) self.assertEqual( form.errors['artifactstatus_requested'], [ 'Same artifactstatus were chosen for requested and acquisition time.' ], ) self.assertEqual( form.errors['artifactstatus_acquisition'], [ 'Same artifactstatus were chosen for requested and acquisition time.' ], )
def test_main_config_form_path_no_write_permission(self): """ test custom field validation """ # get object form = MainConfigForm( data={ 'statushistory_entry_numbers': 6, 'cron_export_path': '/root', 'cron_username': '******', 'main_overview': 'main_overview_system', }) # compare self.assertFalse(form.is_valid()) self.assertEqual(form.errors['cron_export_path'], ['No write permission for export path.'])
def test_main_config_form_path_not_existent(self): """ test custom field validation """ # get object form = MainConfigForm( data={ 'statushistory_entry_numbers': 6, 'cron_export_path': '/this_path_does_not_exist', 'cron_username': '******', 'main_overview': 'main_overview_system', }) # compare self.assertFalse(form.is_valid()) self.assertEqual(form.errors['cron_export_path'], ['Export path does not exist.'])
def test_main_config_main_overview_form_label(self): """ test form label """ # get object form = MainConfigForm() # compare self.assertEqual(form.fields['main_overview'].label, 'Main overview page')
def test_main_config_system_name_editable_form_label(self): """ test form label """ # get object form = MainConfigForm() # compare self.assertEqual(form.fields['system_name_editable'].label, 'Make system name editable')
def test_main_config_statushistory_entry_numbers_form_label(self): """ test form label """ # get object form = MainConfigForm() # compare self.assertEqual( form.fields['statushistory_entry_numbers'].label, 'Show only this number of last statushistory entries')
def main_config_view(request): # form was valid to post if request.method == "POST": # get config model model = MainConfigModel.objects.get(main_config_name='MainConfig') # get form form = MainConfigForm(request.POST, instance=model) if form.is_valid(): # save settings model = form.save(commit=False) model.save() form.save_m2m() # create message messages.success(request, 'Main config changed') # call logger info_logger(str(request.user), " MAIN_CONFIG_CHANGED") # close popup return HttpResponse( '<script type="text/javascript">window.close();</script>') else: # show form page again return render( request, 'dfirtrack_config/main_config_popup.html', { 'form': form, }, ) else: # get config model model = MainConfigModel.objects.get(main_config_name='MainConfig') # get form form = MainConfigForm(instance=model) # show form page return render( request, 'dfirtrack_config/main_config_popup.html', { 'form': form, }, )
def test_main_config_form_same_casestatus(self): """test custom field validation""" # create obects casestatus_1 = Casestatus.objects.create( casestatus_name='casestatus_1').casestatus_id casestatus_2 = Casestatus.objects.create( casestatus_name='casestatus_2').casestatus_id casestatus_3 = Casestatus.objects.create( casestatus_name='casestatus_3').casestatus_id casestatus_4 = Casestatus.objects.create( casestatus_name='casestatus_4').casestatus_id casestatus_5 = Casestatus.objects.create( casestatus_name='casestatus_5').casestatus_id # get object form = MainConfigForm( data={ 'statushistory_entry_numbers': 2, 'cron_export_path': '/tmp', 'cron_username': '******', 'main_overview': 'main_overview_system', 'casestatus_start': [ casestatus_1, casestatus_2, casestatus_3, ], 'casestatus_end': [ casestatus_3, casestatus_4, casestatus_5, ], }) # compare self.assertFalse(form.is_valid()) self.assertEqual( form.errors['casestatus_start'], ['Same casestatus were chosen for start and end time.'], ) self.assertEqual( form.errors['casestatus_end'], ['Same casestatus were chosen for start and end time.'], )
def test_main_config_casestatus_form_label(self): """ test form label """ # get object form = MainConfigForm() # compare self.assertEqual(form.fields['casestatus_open'].label, 'Casestatus to be considered open') self.assertEqual(form.fields['casestatus_start'].label, 'Casestatus setting the case start time') self.assertEqual(form.fields['casestatus_end'].label, 'Casestatus setting the case end time')
def test_main_config_form_different_artifactstatus(self): """ test custom field validation """ # create obects artifactstatus_1 = Artifactstatus.objects.create( artifactstatus_name='artifactstatus_1').artifactstatus_id artifactstatus_2 = Artifactstatus.objects.create( artifactstatus_name='artifactstatus_2').artifactstatus_id artifactstatus_3 = Artifactstatus.objects.create( artifactstatus_name='artifactstatus_3').artifactstatus_id artifactstatus_4 = Artifactstatus.objects.create( artifactstatus_name='artifactstatus_4').artifactstatus_id artifactstatus_5 = Artifactstatus.objects.create( artifactstatus_name='artifactstatus_5').artifactstatus_id artifactstatus_6 = Artifactstatus.objects.create( artifactstatus_name='artifactstatus_6').artifactstatus_id # get object form = MainConfigForm( data={ 'statushistory_entry_numbers': 5, 'cron_export_path': '/tmp', 'cron_username': '******', 'main_overview': 'main_overview_system', 'artifactstatus_requested': [ artifactstatus_1, artifactstatus_2, artifactstatus_3, ], 'artifactstatus_acquisition': [ artifactstatus_4, artifactstatus_5, artifactstatus_6, ], }) # compare self.assertTrue(form.is_valid())
def test_main_config_cron_form_label(self): """ test form label """ # get object form = MainConfigForm() # compare self.assertEqual( form.fields['cron_export_path'].label, 'Export files created by scheduled tasks to this path') self.assertEqual( form.fields['cron_username'].label, 'Use this username for scheduled tasks (just for logging, does not have to exist)' )
def test_main_config_artifactstatus_form_label(self): """ test form label """ # get object form = MainConfigForm() # compare self.assertEqual(form.fields['artifactstatus_open'].label, 'Artifactstatus to be considered open') self.assertEqual(form.fields['artifactstatus_requested'].label, 'Artifactstatus setting the artifact requested time') self.assertEqual( form.fields['artifactstatus_acquisition'].label, 'Artifactstatus setting the artifact acquisition time')
def test_main_config_form_different_casestatus(self): """ test custom field validation """ # create obects casestatus_1 = Casestatus.objects.create( casestatus_name='casestatus_1').casestatus_id casestatus_2 = Casestatus.objects.create( casestatus_name='casestatus_2').casestatus_id casestatus_3 = Casestatus.objects.create( casestatus_name='casestatus_3').casestatus_id casestatus_4 = Casestatus.objects.create( casestatus_name='casestatus_4').casestatus_id casestatus_5 = Casestatus.objects.create( casestatus_name='casestatus_5').casestatus_id casestatus_6 = Casestatus.objects.create( casestatus_name='casestatus_6').casestatus_id # get object form = MainConfigForm( data={ 'statushistory_entry_numbers': 3, 'cron_export_path': '/tmp', 'cron_username': '******', 'main_overview': 'main_overview_system', 'casestatus_start': [ casestatus_1, casestatus_2, casestatus_3, ], 'casestatus_end': [ casestatus_4, casestatus_5, casestatus_6, ], }) # compare self.assertTrue(form.is_valid())
def main_config_view(request): # form was valid to post if request.method == "POST": # get config model model = MainConfigModel.objects.get(main_config_name='MainConfig') # get form form = MainConfigForm(request.POST, instance=model) if form.is_valid(): # save settings model = form.save(commit=False) model.save() # call logger info_logger(str(request.user), " MAIN_CONFIG_CHANGED") # close popup return HttpResponse( '<script type="text/javascript">window.close();</script>') # TODO: with 'system_name_editable' as the only non-mandatory model attribute, it is not possible to get an invalid form # TODO: finish prepared tests in 'dfirtrack_config.tests.main.test_main_config_views' # TODO: remove the coverage limitation with further mandatory model attributes else: # coverage: ignore branch # show form page return render(request, 'dfirtrack_config/main_config_popup.html', { 'form': form, }) else: # get config model model = MainConfigModel.objects.get(main_config_name='MainConfig') # get form form = MainConfigForm(instance=model) # show form page return render(request, 'dfirtrack_config/main_config_popup.html', { 'form': form, })