def test_run_with_csv_data_format(self, mock_write_csv, mock_json_stringify, mock_email_factory): """Test run() with json data format.""" notifier_config = fake_violations.NOTIFIER_CONFIGS_EMAIL_DEFAULT notification_config = notifier_config['email_connector'] resource = 'policy_violations' inventory_index_id = 1514764800123456 mock_json_stringify.return_value = 'test123' evp = email_violations.EmailViolations(resource, inventory_index_id, fake_violations.VIOLATIONS, fake_violations.GLOBAL_CONFIGS, notifier_config, notification_config) evp._get_output_filename = mock.MagicMock() evp._make_attachment_json = mock.MagicMock() evp.run() self.assertTrue(evp._get_output_filename.called) self.assertEqual(string_formats.VIOLATION_CSV_FMT, evp._get_output_filename.call_args[0][0]) self.assertFalse(evp._make_attachment_json.called) self.assertTrue(mock_write_csv.called) self.assertFalse(mock_json_stringify.called)
def test_run_with_invalid_data_format(self, mock_write_csv, mock_json_stringify, mock_mail_util): """Test run() with invalid data format.""" notifier_config = ( fake_violations.NOTIFIER_CONFIGS_EMAIL_INVALID_DATA_FORMAT) notification_config = notifier_config['email_connector'] resource = 'policy_violations' cycle_timestamp = '2018-03-24T00:49:02.891287' mock_json_stringify.return_value = 'test123' evp = email_violations.EmailViolations(resource, cycle_timestamp, fake_violations.VIOLATIONS, fake_violations.GLOBAL_CONFIGS, notifier_config, notification_config) evp._get_output_filename = mock.MagicMock() evp._make_attachment_csv = mock.MagicMock() evp._make_attachment_json = mock.MagicMock() with self.assertRaises(base_notification.InvalidDataFormatError): evp.run() self.assertFalse(evp._get_output_filename.called) self.assertFalse(evp._make_attachment_csv.called) self.assertFalse(evp._make_attachment_json.called) self.assertFalse(mock_write_csv.called) self.assertFalse(mock_json_stringify.called)
def test_make_attachment_json_no_temp_files_left(self, mock_get_connector): """Test _make_attachment_json() leaves no temp files behind.""" connector = mock.MagicMock(spec=BaseEmailConnector) mock_get_connector.return_value = connector evp = email_violations.EmailViolations(*self.evp_init_args) evp._make_attachment_json() self.assertTrue(connector.create_attachment.called) self.assertFalse( os.path.exists( connector.create_attachment.call_args[1]['file_location']))
def test_make_attachment_csv_correctness(self, mock_os, mock_get_connector): """Test the CSV file correctness.""" connector = mock.MagicMock(spec=SendgridConnector) mock_get_connector.return_value = connector evp = email_violations.EmailViolations(*self.evp_init_args) evp._make_attachment_csv() self.assertTrue(connector.create_attachment.called) test = connector.create_attachment.call_args[1]['file_location'] self.assertTrue( filecmp.cmp( connector.create_attachment.call_args[1]['file_location'], self.expected_csv_attachment_path, shallow=False))
def test_make_attachment_csv_name(self, mock_date_time): """Test the CSV attachment name().""" mock_date_time.get_utc_now_datetime = mock.MagicMock() mock_date_time.get_utc_now_datetime.return_value = self.fake_utcnow expected_timestamp = self.fake_utcnow.strftime( string_formats.TIMESTAMP_TIMEZONE_FILES) evp = email_violations.EmailViolations(*self.evp_init_args) attachment = evp._make_attachment_csv() self.assertEquals( string_formats.VIOLATION_CSV_FMT.format( evp.resource, evp.inventory_index_id, expected_timestamp), attachment.filename)