def execute(self, **kwargs): # noqa: D403 """ SlackAPIOperator calls will not fail even if the call is not unsuccessful. It should not prevent a DAG from completing in success """ if not self.api_params: self.construct_api_call_params() slack = SlackHook(token=self.token, slack_conn_id=self.slack_conn_id) slack.call(self.method, json=self.api_params)
def test_call_with_failure(self, slack_client_class_mock): slack_client_mock = mock.Mock() slack_client_class_mock.return_value = slack_client_mock expected_exception = SlackApiError(message='foo', response='bar') slack_client_mock.api_call = mock.Mock(side_effect=expected_exception) test_token = 'test_token' test_slack_conn_id = 'test_slack_conn_id' slack_hook = SlackHook(token=test_token, slack_conn_id=test_slack_conn_id) test_method = 'test_method' test_api_params = {'key1': 'value1', 'key2': 'value2'} with self.assertRaises(SlackApiError): slack_hook.call(test_method, test_api_params)
def test_call_with_success(self, slack_client_class_mock): slack_client_mock = mock.Mock() slack_client_class_mock.return_value = slack_client_mock slack_client_mock.api_call.return_value = {'ok': True} test_token = 'test_token' test_slack_conn_id = 'test_slack_conn_id' slack_hook = SlackHook(token=test_token, slack_conn_id=test_slack_conn_id) test_method = 'test_method' test_api_params = {'key1': 'value1', 'key2': 'value2'} slack_hook.call(test_method, test_api_params) slack_client_class_mock.assert_called_once_with(test_token) slack_client_mock.api_call.assert_called_once_with(test_method, **test_api_params)
def test_init_with_token_and_slack_conn_id(self): test_token = 'test_token' test_slack_conn_id = 'test_slack_conn_id' slack_hook = SlackHook(token=test_token, slack_conn_id=test_slack_conn_id) self.assertEqual(slack_hook.token, test_token)
def test_get_token_with_empty_password_slack_conn_id_only(self, get_connection_mock): """tests `__get_token` method when only connection is provided """ # Mock get_connection_mock.return_value = mock.Mock(password=None) # Assert with pytest.raises(AirflowException): SlackHook(token=None, slack_conn_id='x')
def test_init_with_valid_slack_conn_id_only(self, get_connection_mock): test_password = '******' get_connection_mock.return_value = mock.Mock(password=test_password) test_slack_conn_id = 'test_slack_conn_id' slack_hook = SlackHook(token=None, slack_conn_id=test_slack_conn_id) get_connection_mock.assert_called_once_with(test_slack_conn_id) self.assertEqual(slack_hook.token, test_password)
def test_call_with_success(self, slack_client_class_mock): slack_client_mock = mock.Mock() slack_client_class_mock.return_value = slack_client_mock slack_response = mock.Mock() slack_client_mock.api_call.return_value = slack_response slack_response.validate.return_value = True test_token = 'test_token' test_slack_conn_id = 'test_slack_conn_id' slack_hook = SlackHook(token=test_token, slack_conn_id=test_slack_conn_id) test_method = 'test_method' test_api_params = {'key1': 'value1', 'key2': 'value2'} slack_hook.call(test_method, json=test_api_params) slack_client_class_mock.assert_called_once_with(test_token) slack_client_mock.api_call.assert_called_once_with( test_method, json=test_api_params) self.assertEqual(slack_response.validate.call_count, 1)
def test_call_with_failure(self, slack_client_class_mock): slack_client_mock = mock.Mock() slack_client_class_mock.return_value = slack_client_mock slack_client_mock.api_call.return_value = {'ok': False, 'error': 'test_error'} test_token = 'test_token' test_slack_conn_id = 'test_slack_conn_id' slack_hook = SlackHook(token=test_token, slack_conn_id=test_slack_conn_id) test_method = 'test_method' test_api_params = {'key1': 'value1', 'key2': 'value2'} self.assertRaises(AirflowException, slack_hook.call, test_method, test_api_params)
def test_call_with_failure(self, slack_client_class_mock): slack_client_mock = mock.Mock() slack_client_class_mock.return_value = slack_client_mock slack_response = mock.Mock() slack_client_mock.api_call.return_value = slack_response expected_exception = SlackApiError(message='foo', response='bar') slack_response.validate = mock.Mock(side_effect=expected_exception) test_token = 'test_token' test_slack_conn_id = 'test_slack_conn_id' slack_hook = SlackHook(token=test_token, slack_conn_id=test_slack_conn_id) test_method = 'test_method' test_api_params = {'key1': 'value1', 'key2': 'value2'} try: slack_hook.call(test_method, test_api_params) self.fail() except AirflowException as exc: self.assertIn("foo", str(exc)) self.assertIn("bar", str(exc))
def execute(self, **kwargs): """ The SlackAPIOperator calls will not fail even if the call is not unsuccessful. It should not prevent a DAG from completing in success """ slack = SlackHook(token=self.token, slack_conn_id=self.slack_conn_id) # If file content is passed. if self.content is not None: self.api_params = { 'channels': self.channel, 'content': self.content, 'initial_comment': self.initial_comment, } slack.call(self.method, data=self.api_params) # If file name is passed. elif self.filename is not None: self.api_params = { 'channels': self.channel, 'filename': self.filename, 'filetype': self.filetype, 'initial_comment': self.initial_comment, } with open(self.filename, "rb") as file_handle: slack.call(self.method, data=self.api_params, files={'file': file_handle}) file_handle.close()
def test_get_token_with_token_and_slack_conn_id(self): """tests `__get_token` method when both arguments are provided """ # Given test_token = 'test_token' test_conn_id = 'x' # Run hook = SlackHook(test_token, test_conn_id) # Assert output = hook.token expected = test_token self.assertEqual(output, expected)
def test_get_token_with_token_only(self): """tests `__get_token` method when only token is provided """ # Given test_token = 'test_token' test_conn_id = None # Run hook = SlackHook(test_token, test_conn_id) # Assert output = hook.token expected = test_token self.assertEqual(output, expected)
def test_get_token_with_valid_slack_conn_id_only(self, get_connection_mock, mock_slack_client): """tests `__get_token` method when only connection is provided """ # Given test_token = None test_conn_id = 'x' test_password = '******' # Mock get_connection_mock.return_value = mock.Mock(password=test_password) # Run hook = SlackHook(test_token, test_conn_id) # Assert output = hook.token expected = test_password self.assertEqual(output, expected) mock_slack_client.assert_called_once_with(test_password)
def test_get_token_with_out_token_nor_slack_conn_id(self): """tests `__get_token` method when no arguments are provided """ with pytest.raises(AirflowException): SlackHook(token=None, slack_conn_id=None)
def test_api_call(self, mock_slack_client, mock_slack_api_call): slack_hook = SlackHook(token='test_token') test_api_json = {'channel': 'test_channel'} slack_hook.call("chat.postMessage", json=test_api_json) mock_slack_api_call.assert_called_once_with(mock_slack_client, "chat.postMessage", json=test_api_json)
def test_init_with_token_only(self): test_token = 'test_token' slack_hook = SlackHook(token=test_token, slack_conn_id=None) self.assertEqual(slack_hook.token, test_token)