feat(slack): add test_connection method (#5340)

This commit is contained in:
Sergio Garcia
2024-10-29 18:14:06 +01:00
committed by GitHub
parent f01910e4f2
commit e6053ce218
4 changed files with 215 additions and 1 deletions
+84
View File
@@ -1,7 +1,13 @@
from unittest import mock
from prowler.config.config import aws_logo, azure_logo, gcp_logo
from prowler.lib.outputs.slack.exceptions.exceptions import (
SlackChannelNotFound,
SlackClientError,
SlackNoCredentialsError,
)
from prowler.lib.outputs.slack.slack import Slack
from prowler.providers.common.models import Connection
from tests.providers.aws.utils import AWS_ACCOUNT_NUMBER, set_mocked_aws_provider
from tests.providers.azure.azure_fixtures import (
AZURE_SUBSCRIPTION_ID,
@@ -12,6 +18,7 @@ from tests.providers.gcp.gcp_fixtures import set_mocked_gcp_provider
SLACK_CHANNEL = "test-channel"
SLACK_TOKEN = "test-token"
NON_EXISTING_CHANNEL = "non-existing-channel"
class TestSlackIntegration:
@@ -486,3 +493,80 @@ class TestSlackIntegration:
args = "--slack"
response = slack.send(stats, args)
assert response == mocked_slack_response
def test_test_connection(self):
mocked_auth_response = {"ok": True}
mocked_conversations_info = {
"ok": True,
"channels": [
{"id": "C87654321", "name": SLACK_CHANNEL, "is_member": True},
],
}
mocked_web_client = mock.MagicMock()
mocked_web_client.auth_test = mock.Mock(return_value=mocked_auth_response)
mocked_web_client.conversations_info = mock.Mock(
return_value=mocked_conversations_info
)
with mock.patch(
"prowler.lib.outputs.slack.slack.WebClient", return_value=mocked_web_client
):
assert Slack.test_connection(
token=SLACK_TOKEN, channel=SLACK_CHANNEL
) == Connection(is_connected=True)
def test_slack_no_credentials_error(self):
mocked_auth_response = {"ok": False, "error": "invalid_auth"}
mocked_web_client = mock.MagicMock()
mocked_web_client.auth_test = mock.Mock(return_value=mocked_auth_response)
with mock.patch(
"prowler.lib.outputs.slack.slack.WebClient", return_value=mocked_web_client
):
connection = Slack.test_connection(
token=SLACK_TOKEN,
channel=NON_EXISTING_CHANNEL,
raise_on_exception=False,
)
assert not connection.is_connected
assert isinstance(connection.error, SlackNoCredentialsError)
assert "invalid_auth" in str(connection.error)
def test_slack_channel_not_found(self):
mocked_auth_response = {"ok": True}
mocked_conversations_info = {"ok": False, "error": "channel_not_found"}
mocked_web_client = mock.MagicMock()
mocked_web_client.auth_test = mock.Mock(return_value=mocked_auth_response)
mocked_web_client.conversations_info = mock.Mock(
return_value=mocked_conversations_info
)
with mock.patch(
"prowler.lib.outputs.slack.slack.WebClient", return_value=mocked_web_client
):
connection = Slack.test_connection(
token=SLACK_TOKEN,
channel=NON_EXISTING_CHANNEL,
raise_on_exception=False,
)
assert not connection.is_connected
assert isinstance(connection.error, SlackChannelNotFound)
assert "channel_not_found" in str(connection.error)
def test_slack_client_error(self):
mocked_web_client = mock.MagicMock()
mocked_web_client.auth_test = mock.Mock(side_effect=SlackClientError)
with mock.patch(
"prowler.lib.outputs.slack.slack.WebClient", return_value=mocked_web_client
):
connection = Slack.test_connection(
token=SLACK_TOKEN,
channel=NON_EXISTING_CHANNEL,
raise_on_exception=False,
)
assert not connection.is_connected
assert isinstance(connection.error, SlackClientError)
assert "Slack ClientError occurred" in str(connection.error)