def test_system_importer_file_csv_cron_wrong_type(self): """ test importer view """ # change config set_csv_import_path(os.path.join(BASE_DIR, 'dfirtrack_main/tests/system_importer/system_importer_file_csv_files')) # change config set_csv_import_filename('system_importer_file_csv_testfile_04_wrong_type.png') # execute cron job / scheduled task system_cron() # login testuser self.client.login(username='******', password='******') # get response response = self.client.get('/system/') # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertEqual(str(response.context['user']), 'testuser_system_importer_file_csv_check_content_file_type') self.assertEqual(messages[0].message, '[Scheduled task CSV system importer] Wrong file type for CSV import. Check config or file system!') self.assertEqual(messages[0].level_tag, 'error') # switch user context self.client.logout() self.client.login(username='******', password='******') # get response response = self.client.get('/system/') # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertEqual(str(response.context['user']), 'message_user') self.assertEqual(messages[0].message, '[Scheduled task CSV system importer] Wrong file type for CSV import. Check config or file system!') self.assertEqual(messages[0].level_tag, 'error')
def test_system_importer_file_csv_check_content_file_system_create_cron_path_not_existing( self): """ test importer view """ # change config set_csv_import_path('/path_not_existing') # login testuser self.client.login( username= '******', password='******') # create url destination = urllib.parse.quote('/system/', safe='/') # get response response = self.client.get('/system/importer/file/csv/cron/', follow=True) # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertRedirects(response, destination, status_code=302, target_status_code=200) self.assertEqual( messages[0].message, 'CSV import path does not exist. Check config or file system!') self.assertEqual(messages[0].level_tag, 'error')
def test_system_importer_file_csv_check_content_file_system_instant_file_empty( self): """ test importer view """ # change config set_csv_import_path( os.path.join( BASE_DIR, 'dfirtrack_main/tests/system_importer/system_importer_file_csv_files/' )) # change config set_csv_import_filename( 'system_importer_file_csv_testfile_06_empty.csv') # login testuser self.client.login( username= '******', password='******') # create url destination = urllib.parse.quote('/system/', safe='/') # get response response = self.client.get('/system/importer/file/csv/instant/', follow=True) # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertRedirects(response, destination, status_code=302, target_status_code=200) self.assertEqual( messages[0].message, 'CSV import file is empty. Check config or file system!') self.assertEqual(messages[0].level_tag, 'error')
def setUpTestData(cls): """ one-time setup """ # create user test_user = User.objects.create_user(username='******', password='******') User.objects.create_user(username='******', password='******') # create objects analysisstatus_1 = Analysisstatus.objects.create(analysisstatus_name='analysisstatus_1') analysisstatus_2 = Analysisstatus.objects.create(analysisstatus_name='analysisstatus_2') systemstatus_1 = Systemstatus.objects.create(systemstatus_name='systemstatus_1') systemstatus_2 = Systemstatus.objects.create(systemstatus_name='systemstatus_2') # build local path with test files set_csv_import_path(os.path.join(BASE_DIR, 'dfirtrack_main/tests/system_importer/system_importer_file_csv_files/')) # change config system_importer_file_csv_config_model = SystemImporterFileCsvConfigModel.objects.get(system_importer_file_csv_config_name='SystemImporterFileCsvConfig') system_importer_file_csv_config_model.csv_column_system = 1 system_importer_file_csv_config_model.csv_skip_existing_system = True system_importer_file_csv_config_model.csv_headline = False system_importer_file_csv_config_model.csv_import_username = test_user system_importer_file_csv_config_model.csv_default_systemstatus = systemstatus_1 system_importer_file_csv_config_model.csv_default_analysisstatus = analysisstatus_1 system_importer_file_csv_config_model.csv_default_tagfree_systemstatus = systemstatus_2 system_importer_file_csv_config_model.csv_default_tagfree_analysisstatus = analysisstatus_2 system_importer_file_csv_config_model.csv_tag_lock_systemstatus = 'LOCK_SYSTEMSTATUS' system_importer_file_csv_config_model.csv_tag_lock_analysisstatus = 'LOCK_ANALYSISSTATUS' system_importer_file_csv_config_model.csv_remove_tag = 'tag_remove_prefix' system_importer_file_csv_config_model.csv_field_delimiter = 'field_comma' system_importer_file_csv_config_model.csv_text_quote = 'text_double_quotation_marks' system_importer_file_csv_config_model.csv_ip_delimiter = 'ip_semicolon' system_importer_file_csv_config_model.csv_tag_delimiter = 'tag_space' system_importer_file_csv_config_model.save()
def setUpTestData(cls): """ one-time setup """ # create user test_user = User.objects.create_user( username='******', is_staff=True, is_superuser=True, password='******', ) # change config set_csv_import_path( os.path.join( BASE_DIR, 'dfirtrack_main/tests/system_importer/system_importer_file_csv_files/' )) # change config set_csv_import_filename( 'system_importer_file_csv_testfile_01_minimal_double_quotation.csv' ) system_importer_file_csv_config_model = SystemImporterFileCsvConfigModel.objects.get( system_importer_file_csv_config_name='SystemImporterFileCsvConfig') system_importer_file_csv_config_model.csv_import_username = test_user system_importer_file_csv_config_model.save()
def test_system_importer_file_csv_instant_wrong_type(self): """ test importer view """ # change config set_csv_import_path( os.path.join( BASE_DIR, 'dfirtrack_main/tests/system_importer/system_importer_file_csv_files' )) # change config set_csv_import_filename( 'system_importer_file_csv_testfile_04_wrong_type.png') # login testuser self.client.login( username= '******', password='******') # create url destination = urllib.parse.quote('/system/', safe='/') # get response response = self.client.get('/system/importer/file/csv/instant/', follow=True) # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertRedirects(response, destination, status_code=302, target_status_code=200) self.assertEqual( messages[0].message, 'Wrong file type for CSV import. Check config or file system!') self.assertEqual(messages[0].level_tag, 'error')
def test_system_importer_file_csv_check_content_file_system_cron_file_no_read_permission( self, ): """test importer view""" # get timestamp string t1 = timezone.now().strftime('%Y%m%d_%H%M%S') # set file system attributes csv_import_path = '/tmp' csv_import_filename = f'{t1}_cron_no_read_permission.csv' # create file create_file_no_read_permission(csv_import_path, csv_import_filename) # change config set_csv_import_path(csv_import_path) # change config set_csv_import_filename(csv_import_filename) # execute cron job / scheduled task system_cron() # login testuser self.client.login( username= '******', password='******', ) # get response response = self.client.get('/system/') # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertEqual( str(response.context['user']), 'testuser_system_importer_file_csv_check_content_file_system', ) self.assertEqual( messages[0].message, '[Scheduled task CSV system importer] No read permission for CSV import file. Check config or file system!', ) self.assertEqual(messages[0].level_tag, 'error') # switch user context self.client.logout() self.client.login(username='******', password='******') # get response response = self.client.get('/system/') # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertEqual(str(response.context['user']), 'message_user') self.assertEqual( messages[0].message, '[Scheduled task CSV system importer] No read permission for CSV import file. Check config or file system!', ) self.assertEqual(messages[0].level_tag, 'error')
def test_system_importer_file_csv_check_content_file_system_cron_file_empty( self): """test importer view""" # change config set_csv_import_path( os.path.join( BASE_DIR, 'dfirtrack_main/tests/system_importer/system_importer_file_csv_files/', )) # change config set_csv_import_filename( 'system_importer_file_csv_testfile_06_empty.csv') # execute cron job / scheduled task system_cron() # login testuser self.client.login( username= '******', password='******', ) # get response response = self.client.get('/system/') # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertEqual( str(response.context['user']), 'testuser_system_importer_file_csv_check_content_file_system', ) self.assertEqual( messages[0].message, '[Scheduled task CSV system importer] CSV import file is empty. Check config or file system!', ) self.assertEqual(messages[0].level_tag, 'error') # switch user context self.client.logout() self.client.login(username='******', password='******') # get response response = self.client.get('/system/') # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertEqual(str(response.context['user']), 'message_user') self.assertEqual( messages[0].message, '[Scheduled task CSV system importer] CSV import file is empty. Check config or file system!', ) self.assertEqual(messages[0].level_tag, 'error')
def test_system_importer_file_csv_check_content_file_system_cron_file_not_existing( self, ): """test importer view""" # change config set_csv_import_path('/tmp') # change config set_csv_import_filename('filename_not_existing.abc') # execute cron job / scheduled task system_cron() # login testuser self.client.login( username= '******', password='******', ) # get response response = self.client.get('/system/') # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertEqual( str(response.context['user']), 'testuser_system_importer_file_csv_check_content_file_system', ) self.assertEqual( messages[0].message, '[Scheduled task CSV system importer] CSV import file does not exist. Check config or provide file!', ) self.assertEqual(messages[0].level_tag, 'error') # switch user context self.client.logout() self.client.login(username='******', password='******') # get response response = self.client.get('/system/') # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertEqual(str(response.context['user']), 'message_user') self.assertEqual( messages[0].message, '[Scheduled task CSV system importer] CSV import file does not exist. Check config or provide file!', ) self.assertEqual(messages[0].level_tag, 'error')
def test_system_importer_file_csv_check_content_file_system_cron_path_no_read_permission( self): """ test importer view """ # change config set_csv_import_path('/root') # execute cron job / scheduled task system_cron() # login testuser self.client.login( username= '******', password='******') # get response response = self.client.get('/system/') # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertEqual( str(response.context['user']), 'testuser_system_importer_file_csv_check_content_file_system') self.assertEqual( messages[0].message, '[Scheduled task CSV system importer] No read permission for CSV import path. Check config or file system!' ) self.assertEqual(messages[0].level_tag, 'error') # switch user context self.client.logout() self.client.login(username='******', password='******') # get response response = self.client.get('/system/') # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertEqual(str(response.context['user']), 'message_user') self.assertEqual( messages[0].message, '[Scheduled task CSV system importer] No read permission for CSV import path. Check config or file system!' ) self.assertEqual(messages[0].level_tag, 'error')
def test_system_importer_file_csv_check_content_file_system_instant_file_no_read_permission( self, ): """test importer view""" # get timestamp string t1 = timezone.now().strftime('%Y%m%d_%H%M%S') # set file system attributes csv_import_path = '/tmp' csv_import_filename = f'{t1}_instant_no_read_permission.csv' # create file create_file_no_read_permission(csv_import_path, csv_import_filename) # change config set_csv_import_path(csv_import_path) # change config set_csv_import_filename(csv_import_filename) # login testuser self.client.login( username= '******', password='******', ) # create url destination = urllib.parse.quote('/system/', safe='/') # get response response = self.client.get('/system/importer/file/csv/instant/', follow=True) # get messages messages = list(get_messages(response.wsgi_request)) # compare self.assertRedirects(response, destination, status_code=302, target_status_code=200) self.assertEqual( messages[0].message, 'No read permission for CSV import file. Check config or file system!', ) self.assertEqual(messages[0].level_tag, 'error')
def setUpTestData(cls): """ one-time setup """ # create user test_user = User.objects.create_user(username='******', password='******') User.objects.create_user(username='******', password='******') """ create objects and add attributes """ # create status analysisstatus_1 = Analysisstatus.objects.create(analysisstatus_name='analysisstatus_1') analysisstatus_2 = Analysisstatus.objects.create(analysisstatus_name='analysisstatus_2') analysisstatus_3 = Analysisstatus.objects.create(analysisstatus_name='analysisstatus_3') systemstatus_1 = Systemstatus.objects.create(systemstatus_name='systemstatus_1') systemstatus_2 = Systemstatus.objects.create(systemstatus_name='systemstatus_2') systemstatus_3 = Systemstatus.objects.create(systemstatus_name='systemstatus_3') # create fk objects dnsname_1 = Dnsname.objects.create(dnsname_name='dnsname_1') domain_1 = Domain.objects.create(domain_name='domain_1') location_1 = Location.objects.create(location_name='location_1') os_1 = Os.objects.create(os_name='os_1') reason_1 = Reason.objects.create(reason_name='reason_1') recommendation_1 = Recommendation.objects.create(recommendation_name='recommendation_1') serviceprovider_1 = Serviceprovider.objects.create(serviceprovider_name='serviceprovider_1') systemtype_1 = Systemtype.objects.create(systemtype_name='systemtype_1') # create systems system_csv_41_001 = System.objects.create( system_name = 'system_csv_41_001', systemstatus = systemstatus_1, analysisstatus = analysisstatus_1, dnsname = dnsname_1, domain = domain_1, location = location_1, os = os_1, reason = reason_1, recommendation = recommendation_1, serviceprovider = serviceprovider_1, systemtype = systemtype_1, system_modify_time = timezone.now(), system_created_by_user_id = test_user, system_modified_by_user_id = test_user, ) system_csv_41_002 = System.objects.create( system_name = 'system_csv_41_002', systemstatus = systemstatus_1, analysisstatus = analysisstatus_1, dnsname = dnsname_1, domain = domain_1, location = location_1, os = os_1, reason = reason_1, recommendation = recommendation_1, serviceprovider = serviceprovider_1, systemtype = systemtype_1, system_modify_time = timezone.now(), system_created_by_user_id = test_user, system_modified_by_user_id = test_user, ) system_csv_41_003 = System.objects.create( system_name = 'system_csv_41_003', systemstatus = systemstatus_1, analysisstatus = analysisstatus_1, dnsname = dnsname_1, domain = domain_1, location = location_1, os = os_1, reason = reason_1, recommendation = recommendation_1, serviceprovider = serviceprovider_1, systemtype = systemtype_1, system_modify_time = timezone.now(), system_created_by_user_id = test_user, system_modified_by_user_id = test_user, ) # create / add m2m objects case_1 = Case.objects.create( case_name='case_1', case_is_incident=True, case_created_by_user_id=test_user, ) system_csv_41_001.case.add(case_1) system_csv_41_002.case.add(case_1) system_csv_41_003.case.add(case_1) company_1 = Company.objects.create(company_name='company_1') system_csv_41_001.company.add(company_1) system_csv_41_002.company.add(company_1) system_csv_41_003.company.add(company_1) ip_1 = Ip.objects.create(ip_ip='127.99.1.1') ip_2 = Ip.objects.create(ip_ip='127.99.2.2') ip_3 = Ip.objects.create(ip_ip='127.99.3.3') system_csv_41_001.ip.add(ip_1) system_csv_41_002.ip.add(ip_2) system_csv_41_003.ip.add(ip_3) tagcolor_1 = Tagcolor.objects.create(tagcolor_name='tagcolor_1') tag_99_1 = Tag.objects.create( tag_name='tag_99_1', tagcolor=tagcolor_1, ) system_csv_41_001.tag.add(tag_99_1) system_csv_41_002.tag.add(tag_99_1) system_csv_41_003.tag.add(tag_99_1) """ config """ # build local path with test files set_csv_import_path(os.path.join(BASE_DIR, 'dfirtrack_main/tests/system_importer/system_importer_file_csv_files/')) # set config system_importer_file_csv_config_model = SystemImporterFileCsvConfigModel.objects.get(system_importer_file_csv_config_name='SystemImporterFileCsvConfig') system_importer_file_csv_config_model.csv_column_system = 1 system_importer_file_csv_config_model.csv_skip_existing_system = False system_importer_file_csv_config_model.csv_headline = False system_importer_file_csv_config_model.csv_import_username = test_user system_importer_file_csv_config_model.csv_default_systemstatus = systemstatus_2 system_importer_file_csv_config_model.csv_default_analysisstatus = analysisstatus_2 system_importer_file_csv_config_model.csv_default_tagfree_systemstatus = systemstatus_3 system_importer_file_csv_config_model.csv_default_tagfree_analysisstatus = analysisstatus_3 system_importer_file_csv_config_model.csv_tag_lock_systemstatus = 'LOCK_SYSTEMSTATUS' system_importer_file_csv_config_model.csv_tag_lock_analysisstatus = 'LOCK_ANALYSISSTATUS' system_importer_file_csv_config_model.csv_remove_tag = 'tag_remove_prefix' system_importer_file_csv_config_model.csv_field_delimiter = 'field_comma' system_importer_file_csv_config_model.csv_text_quote = 'text_double_quotation_marks' system_importer_file_csv_config_model.csv_ip_delimiter = 'ip_semicolon' system_importer_file_csv_config_model.csv_tag_delimiter = 'tag_space' system_importer_file_csv_config_model.save()
def setUpTestData(cls): """ one-time setup """ # create user test_user = User.objects.create_user( username='******', password='******') User.objects.create_user(username='******', password='******') # create objects analysisstatus_1 = Analysisstatus.objects.create( analysisstatus_name='analysisstatus_1') analysisstatus_2 = Analysisstatus.objects.create( analysisstatus_name='analysisstatus_2') systemstatus_1 = Systemstatus.objects.create( systemstatus_name='systemstatus_1') systemstatus_2 = Systemstatus.objects.create( systemstatus_name='systemstatus_2') # create systems system_csv_56_001 = System.objects.create( system_name='system_csv_56_001', systemstatus=systemstatus_1, analysisstatus=analysisstatus_1, system_modify_time=timezone.now(), system_created_by_user_id=test_user, system_modified_by_user_id=test_user, ) system_csv_56_002 = System.objects.create( system_name='system_csv_56_002', systemstatus=systemstatus_1, analysisstatus=analysisstatus_1, system_modify_time=timezone.now(), system_created_by_user_id=test_user, system_modified_by_user_id=test_user, ) system_csv_56_003 = System.objects.create( system_name='system_csv_56_003', systemstatus=systemstatus_1, analysisstatus=analysisstatus_1, system_modify_time=timezone.now(), system_created_by_user_id=test_user, system_modified_by_user_id=test_user, ) tagcolor_1 = Tagcolor.objects.create(tagcolor_name='tagcolor_1') AUTO_tag_96_1 = Tag.objects.create( tag_name='AUTO_tag_96_1', tagcolor=tagcolor_1, ) tag_97_1 = Tag.objects.create( tag_name='tag_97_1', tagcolor=tagcolor_1, ) system_csv_56_001.tag.add(AUTO_tag_96_1) system_csv_56_002.tag.add(AUTO_tag_96_1) system_csv_56_003.tag.add(AUTO_tag_96_1) system_csv_56_001.tag.add(tag_97_1) system_csv_56_002.tag.add(tag_97_1) system_csv_56_003.tag.add(tag_97_1) # set config set_csv_import_path( os.path.join( BASE_DIR, 'dfirtrack_main/tests/system_importer/system_importer_file_csv_files/' )) # set config set_csv_import_filename( 'system_importer_file_csv_testfile_56_tag_delimiter_space.csv') # set config system_importer_file_csv_config_model = SystemImporterFileCsvConfigModel.objects.get( system_importer_file_csv_config_name='SystemImporterFileCsvConfig') system_importer_file_csv_config_model.csv_column_system = 1 system_importer_file_csv_config_model.csv_skip_existing_system = False system_importer_file_csv_config_model.csv_headline = False system_importer_file_csv_config_model.csv_import_username = test_user system_importer_file_csv_config_model.csv_default_systemstatus = systemstatus_1 system_importer_file_csv_config_model.csv_default_analysisstatus = analysisstatus_1 system_importer_file_csv_config_model.csv_default_tagfree_systemstatus = systemstatus_2 system_importer_file_csv_config_model.csv_default_tagfree_analysisstatus = analysisstatus_2 system_importer_file_csv_config_model.csv_tag_lock_systemstatus = 'LOCK_SYSTEMSTATUS' system_importer_file_csv_config_model.csv_tag_lock_analysisstatus = 'LOCK_ANALYSISSTATUS' system_importer_file_csv_config_model.csv_field_delimiter = 'field_comma' system_importer_file_csv_config_model.csv_text_quote = 'text_double_quotation_marks' system_importer_file_csv_config_model.csv_ip_delimiter = 'ip_semicolon' system_importer_file_csv_config_model.csv_tag_delimiter = 'tag_space' system_importer_file_csv_config_model.csv_tag_prefix = 'AUTO' system_importer_file_csv_config_model.csv_tag_prefix_delimiter = 'tag_prefix_underscore' system_importer_file_csv_config_model.csv_choice_tag = True system_importer_file_csv_config_model.csv_column_tag = 2 system_importer_file_csv_config_model.save()