diff --git a/src/NAC_Service_Portal/settings.py b/src/NAC_Service_Portal/settings.py index 283375a..ac026b6 100644 --- a/src/NAC_Service_Portal/settings.py +++ b/src/NAC_Service_Portal/settings.py @@ -68,6 +68,7 @@ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', + 'nac.context_processors.armis_context', ], }, }, diff --git a/src/nac/context_processors.py b/src/nac/context_processors.py new file mode 100644 index 0000000..4687540 --- /dev/null +++ b/src/nac/context_processors.py @@ -0,0 +1,11 @@ +from helper.armis import get_tenant_url +from django.core.cache import cache + + +# is armis configured? We need this to render to nav-bar correctly +def armis_context(request): + armis_is_configured = cache.get("armis_is_configured") + if armis_is_configured is None: + armis_is_configured = get_tenant_url() != "https://" + cache.set("armis_is_configured", armis_is_configured) + return {"armis_is_configured": armis_is_configured} diff --git a/src/templates/base.html b/src/templates/base.html index f7df82c..8e98d94 100644 --- a/src/templates/base.html +++ b/src/templates/base.html @@ -66,6 +66,7 @@ Add Device + {% if armis_is_configured %} + + {% endif %} diff --git a/src/tests/test_helper/test_armis.py b/src/tests/test_helper/test_armis.py index 1dc0f63..8792e25 100644 --- a/src/tests/test_helper/test_armis.py +++ b/src/tests/test_helper/test_armis.py @@ -1,4 +1,5 @@ import pytest +from django.test import RequestFactory from unittest.mock import patch, MagicMock from helper.armis import ( armiscloud, @@ -7,6 +8,8 @@ _remove_existing_devices, get_devices, get_tenant_url, ) +from nac.context_processors import armis_context +from django.core.cache import cache @pytest.fixture @@ -99,6 +102,27 @@ def test_get_devices(mock_remove_existing_devices, mock_config): mock_remove_existing_devices.assert_called_once_with(mock_devices) -def test_get_tenent_url(mock_config): +def test_get_tenant_url(mock_config): with patch('helper.armis.armis_config', mock_config): assert get_tenant_url() == "https://test_host" + + +@pytest.mark.parametrize("tenant_hostname, validity", [ + ("", False), + ("test_host", True), +]) +def test_armis_context(tenant_hostname, validity): + + mock_config = { + 'armis-server': { + 'api_secret_key': 'test_key', + 'tenant_hostname': tenant_hostname, + 'sites_pattern': 'Site\\d+', # 'Site' + one or more numeric id's + 'vlan_blacklist': '100,200' + } + } + with patch('helper.armis.armis_config', mock_config): + cache.clear() + factory = RequestFactory() + request = factory.get("/") + assert armis_context(request)["armis_is_configured"] == validity