feat(azure): check related with App Insights service (#3395)

This commit is contained in:
Rubén De la Torre Vico
2024-02-13 12:27:12 +00:00
committed by GitHub
parent cc71249e21
commit 4740a7b930
10 changed files with 271 additions and 24 deletions

View File

@@ -0,0 +1,77 @@
from unittest import mock
from prowler.providers.azure.services.appinsights.appinsights_service import Component
from tests.providers.azure.azure_fixtures import AZURE_SUBSCRIPTION
class Test_appinsights_ensure_is_configured:
def test_appinsights_no_subscriptions(self):
appinsights_client = mock.MagicMock
appinsights_client.components = {}
with mock.patch(
"prowler.providers.azure.services.appinsights.appinsights_ensure_is_configured.appinsights_ensure_is_configured.appinsights_client",
new=appinsights_client,
):
from prowler.providers.azure.services.appinsights.appinsights_ensure_is_configured.appinsights_ensure_is_configured import (
appinsights_ensure_is_configured,
)
check = appinsights_ensure_is_configured()
result = check.execute()
assert len(result) == 0
def test_no_appinsights(self):
appinsights_client = mock.MagicMock
appinsights_client.components = {AZURE_SUBSCRIPTION: {}}
with mock.patch(
"prowler.providers.azure.services.appinsights.appinsights_ensure_is_configured.appinsights_ensure_is_configured.appinsights_client",
new=appinsights_client,
):
from prowler.providers.azure.services.appinsights.appinsights_ensure_is_configured.appinsights_ensure_is_configured import (
appinsights_ensure_is_configured,
)
check = appinsights_ensure_is_configured()
result = check.execute()
assert len(result) == 1
assert result[0].subscription == AZURE_SUBSCRIPTION
assert result[0].status == "FAIL"
assert result[0].resource_id == "AppInsights"
assert result[0].resource_name == "AppInsights"
assert (
result[0].status_extended
== f"There are no AppInsight configured in susbscription {AZURE_SUBSCRIPTION}."
)
def test_appinsights_configured(self):
appinsights_client = mock.MagicMock
appinsights_client.components = {
AZURE_SUBSCRIPTION: {
"app_id-1": Component(
resource_id="/subscriptions/resource_id",
resource_name="AppInsightsTest",
)
}
}
with mock.patch(
"prowler.providers.azure.services.appinsights.appinsights_ensure_is_configured.appinsights_ensure_is_configured.appinsights_client",
new=appinsights_client,
):
from prowler.providers.azure.services.appinsights.appinsights_ensure_is_configured.appinsights_ensure_is_configured import (
appinsights_ensure_is_configured,
)
check = appinsights_ensure_is_configured()
result = check.execute()
assert len(result) == 1
assert result[0].subscription == AZURE_SUBSCRIPTION
assert result[0].status == "PASS"
assert result[0].resource_id == "AppInsights"
assert result[0].resource_name == "AppInsights"
assert (
result[0].status_extended
== f"There is at least one AppInsight configured in susbscription {AZURE_SUBSCRIPTION}."
)

View File

@@ -0,0 +1,50 @@
from unittest.mock import patch
from prowler.providers.azure.services.appinsights.appinsights_service import (
AppInsights,
Component,
)
from tests.providers.azure.azure_fixtures import (
AZURE_SUBSCRIPTION,
set_mocked_azure_audit_info,
)
def mock_appinsights_get_components(_):
return {
AZURE_SUBSCRIPTION: {
"app_id-1": Component(
resource_id="/subscriptions/resource_id",
resource_name="AppInsightsTest",
)
}
}
@patch(
"prowler.providers.azure.services.appinsights.appinsights_service.AppInsights.__get_components__",
new=mock_appinsights_get_components,
)
class Test_AppInsights_Service:
def test__get_client__(self):
app_insights = AppInsights(set_mocked_azure_audit_info())
assert (
app_insights.clients[AZURE_SUBSCRIPTION].__class__.__name__
== "ApplicationInsightsManagementClient"
)
def test__get_subscriptions__(self):
app_insights = AppInsights(set_mocked_azure_audit_info())
assert app_insights.subscriptions.__class__.__name__ == "dict"
def test__get_componentes(self):
appinsights = AppInsights(set_mocked_azure_audit_info())
assert len(appinsights.components) == 1
assert (
appinsights.components[AZURE_SUBSCRIPTION]["app_id-1"].resource_id
== "/subscriptions/resource_id"
)
assert (
appinsights.components[AZURE_SUBSCRIPTION]["app_id-1"].resource_name
== "AppInsightsTest"
)