def use_test_api_configuration(): app = AppSettings() with open(CREDENTIALS_FILE) as f: content = json.loads(f.read()) app.set_cisco_api_enabled(True) app.set_product_blacklist_regex("") app.set_cisco_eox_api_queries("") app.set_auto_create_new_products(True) app.set_periodic_sync_enabled(False) app.set_cisco_api_client_id(content.get("client_id", "dummy_id")) app.set_cisco_api_client_id(content.get("client_secret", "dummy_secret"))
def test_cisco_api_enabled(self): # create new AppSettings object and create defaults settings = AppSettings() assert settings.is_cisco_api_enabled() is False settings.set_cisco_api_enabled(True) assert settings.is_cisco_api_enabled() is True settings.set_cisco_api_enabled(False) assert settings.is_cisco_api_enabled() is False
def use_test_api_configuration(): app = AppSettings() app.set_cisco_api_enabled(True) app.set_product_blacklist_regex("") app.set_cisco_eox_api_queries("") app.set_auto_create_new_products(True) app.set_periodic_sync_enabled(False) app.set_cisco_api_client_id( os.getenv("TEST_CISCO_API_CLIENT_ID", "dummy_id")) app.set_cisco_api_client_secret( os.getenv("TEST_CISCO_API_CLIENT_SECRET", "dummy_secret"))
def mock_cisco_eox_api_access_broken(monkeypatch): app = AppSettings() app.set_cisco_api_enabled(True) app.set_cisco_api_client_id("client_id") app.set_cisco_api_client_id("client_secret") app.set_periodic_sync_enabled(True) app.set_cisco_eox_api_queries("") app.set_product_blacklist_regex("") app.set_auto_create_new_products(True) monkeypatch.setattr(utils, "check_cisco_eox_api_access", lambda client_id, client_secret, drop_credentials=False: False)
def test_trigger_manual_cisco_eox_synchronization(): app_config = AppSettings() app_config.set_cisco_api_enabled(True) app_config.set_periodic_sync_enabled(True) # schedule Cisco EoX API update url = reverse('cisco_api:start_cisco_eox_api_sync_now') client = Client() client.login(username="******", password="******") resp = client.get(url) assert resp.status_code == 302 # verify that task ID is saved in the cache (set by the schedule call) task_id = cache.get("CISCO_EOX_API_SYN_IN_PROGRESS", "") assert task_id != ""
def disable_cisco_api(): app = AppSettings() app.set_cisco_api_enabled(False)
def mock_cisco_eox_api_access_disabled(): app = AppSettings() app.set_cisco_api_enabled(False)
def enable_cisco_api(): app = AppSettings() app.set_cisco_api_enabled(True)
def change_configuration(request): """ change configuration of the Product Database """ # read settings from configuration file app_config = AppSettings() # read settings from database hp_content_after, _ = TextBlock.objects.get_or_create( name=TextBlock.TB_HOMEPAGE_TEXT_AFTER_FAVORITE_ACTIONS ) hp_content_before, _ = TextBlock.objects.get_or_create( name=TextBlock.TB_HOMEPAGE_TEXT_BEFORE_FAVORITE_ACTIONS ) if request.method == "POST": # create a form instance and populate it with data from the request: form = SettingsForm(request.POST) if form.is_valid(): # set common settings app_config.set_login_only_mode(form.cleaned_data["login_only_mode"]) hp_content_before.html_content = form.cleaned_data["homepage_text_before"] hp_content_before.save() hp_content_after.html_content = form.cleaned_data["homepage_text_after"] hp_content_after.save() # set the Cisco API configuration options api_enabled = form.cleaned_data["cisco_api_enabled"] if not api_enabled: # api is disabled, reset values to default app_config.set_cisco_api_enabled(api_enabled) app_config.set_cisco_api_client_id("PlsChgMe") app_config.set_cisco_api_client_secret("PlsChgMe") app_config.set_periodic_sync_enabled(False) app_config.set_auto_create_new_products(False) app_config.set_cisco_eox_api_queries("") app_config.set_product_blacklist_regex("") app_config.set_cisco_eox_api_sync_wait_time("5") else: app_config.set_cisco_api_enabled(api_enabled) client_id = form.cleaned_data["cisco_api_client_id"] \ if form.cleaned_data["cisco_api_client_id"] != "" else "PlsChgMe" app_config.set_cisco_api_client_id(client_id) client_secret = form.cleaned_data["cisco_api_client_secret"] \ if form.cleaned_data["cisco_api_client_secret"] != "" else "PlsChgMe" app_config.set_cisco_api_client_secret(client_secret) if client_id != "PlsChgMe": result = utils.check_cisco_eox_api_access( form.cleaned_data["cisco_api_client_id"], form.cleaned_data["cisco_api_client_secret"] ) if result: messages.success(request, "Successfully connected to the Cisco EoX API") else: messages.error(request, "Cannot contact the Cisco EoX API. Please contact your " "Administrator") else: messages.info( request, "Please configure your Cisco API credentials within the Cisco API settings tab." ) app_config.set_periodic_sync_enabled(form.cleaned_data["eox_api_auto_sync_enabled"]) app_config.set_internal_product_id_label(form.cleaned_data["internal_product_id_label"]) app_config.set_auto_create_new_products(form.cleaned_data["eox_auto_sync_auto_create_elements"]) app_config.set_cisco_eox_api_queries(form.cleaned_data["eox_api_queries"]) app_config.set_product_blacklist_regex(form.cleaned_data["eox_api_blacklist"]) if form.cleaned_data["eox_api_wait_time"]: app_config.set_cisco_eox_api_sync_wait_time(form.cleaned_data["eox_api_wait_time"]) # expire cached settings cache.delete("LOGIN_ONLY_MODE_SETTING") messages.success(request, "Settings saved successfully") return redirect(resolve_url("productdb_config:change_settings")) else: messages.error(request, "Invalid configuration option detected, please check it below.") else: form = SettingsForm() form.fields['cisco_api_enabled'].initial = app_config.is_cisco_api_enabled() form.fields['login_only_mode'].initial = app_config.is_login_only_mode() form.fields['internal_product_id_label'].initial = app_config.get_internal_product_id_label() form.fields['cisco_api_client_id'].initial = app_config.get_cisco_api_client_id() form.fields['cisco_api_client_secret'].initial = app_config.get_cisco_api_client_secret() form.fields['eox_api_auto_sync_enabled'].initial = app_config.is_periodic_sync_enabled() form.fields['eox_auto_sync_auto_create_elements'].initial = app_config.is_auto_create_new_products() form.fields['eox_api_queries'].initial = app_config.get_cisco_eox_api_queries() form.fields['eox_api_blacklist'].initial = app_config.get_product_blacklist_regex() form.fields['eox_api_wait_time'].initial = app_config.get_cisco_eox_api_sync_wait_time() form.fields['homepage_text_before'].initial = hp_content_before.html_content form.fields['homepage_text_after'].initial = hp_content_after.html_content context = { "form": form, "is_cisco_api_enabled": app_config.is_cisco_api_enabled(), "is_cisco_eox_api_auto_sync_enabled": app_config.is_periodic_sync_enabled() } return render(request, "config/change_configuration.html", context=context)
def change_configuration(request): """ change configuration of the Product Database """ # read settings from configuration file app_config = AppSettings() # read settings from database hp_content_after, _ = TextBlock.objects.get_or_create( name=TextBlock.TB_HOMEPAGE_TEXT_AFTER_FAVORITE_ACTIONS ) hp_content_before, _ = TextBlock.objects.get_or_create( name=TextBlock.TB_HOMEPAGE_TEXT_BEFORE_FAVORITE_ACTIONS ) if request.method == "POST": # create a form instance and populate it with data from the request: form = SettingsForm(request.POST) if form.is_valid(): # set common settings app_config.set_login_only_mode(form.cleaned_data["login_only_mode"]) hp_content_before.html_content = form.cleaned_data["homepage_text_before"] hp_content_before.save() hp_content_after.html_content = form.cleaned_data["homepage_text_after"] hp_content_after.save() # set the Cisco API configuration options api_enabled = form.cleaned_data["cisco_api_enabled"] if not api_enabled: # api is disabled, reset values to default app_config.set_cisco_api_enabled(api_enabled) app_config.set_cisco_api_client_id("PlsChgMe") app_config.set_cisco_api_client_secret("PlsChgMe") app_config.set_periodic_sync_enabled(False) app_config.set_auto_create_new_products(False) app_config.set_cisco_eox_api_queries("") app_config.set_product_blacklist_regex("") app_config.set_cisco_eox_api_sync_wait_time("5") else: app_config.set_cisco_api_enabled(api_enabled) client_id = form.cleaned_data["cisco_api_client_id"] \ if form.cleaned_data["cisco_api_client_id"] != "" else "PlsChgMe" app_config.set_cisco_api_client_id(client_id) client_secret = form.cleaned_data["cisco_api_client_secret"] \ if form.cleaned_data["cisco_api_client_secret"] != "" else "PlsChgMe" app_config.set_cisco_api_client_secret(client_secret) app_config.set_internal_product_id_label(form.cleaned_data["internal_product_id_label"]) app_config.set_periodic_sync_enabled(form.cleaned_data["eox_api_auto_sync_enabled"]) app_config.set_auto_create_new_products(form.cleaned_data["eox_auto_sync_auto_create_elements"]) app_config.set_cisco_eox_api_queries(form.cleaned_data["eox_api_queries"]) app_config.set_product_blacklist_regex(form.cleaned_data["eox_api_blacklist"]) if form.cleaned_data["eox_api_wait_time"]: app_config.set_cisco_eox_api_sync_wait_time(form.cleaned_data["eox_api_wait_time"]) if client_id != "PlsChgMe": result = utils.check_cisco_eox_api_access( form.cleaned_data["cisco_api_client_id"], form.cleaned_data["cisco_api_client_secret"] ) if result: messages.success(request, "Successfully connected to the Cisco EoX API") else: messages.error(request, "Cannot contact the Cisco EoX API. Please contact your " "Administrator") else: messages.info( request, "Please configure your Cisco API credentials within the Cisco API settings tab." ) # expire cached settings cache.delete("LOGIN_ONLY_MODE_SETTING") messages.success(request, "Settings saved successfully") return redirect(resolve_url("productdb_config:change_settings")) else: messages.error(request, "Invalid configuration option detected, please check it below.") else: form = SettingsForm() form.fields['cisco_api_enabled'].initial = app_config.is_cisco_api_enabled() form.fields['login_only_mode'].initial = app_config.is_login_only_mode() form.fields['internal_product_id_label'].initial = app_config.get_internal_product_id_label() form.fields['cisco_api_client_id'].initial = app_config.get_cisco_api_client_id() form.fields['cisco_api_client_secret'].initial = app_config.get_cisco_api_client_secret() form.fields['eox_api_auto_sync_enabled'].initial = app_config.is_periodic_sync_enabled() form.fields['eox_auto_sync_auto_create_elements'].initial = app_config.is_auto_create_new_products() form.fields['eox_api_queries'].initial = app_config.get_cisco_eox_api_queries() form.fields['eox_api_blacklist'].initial = app_config.get_product_blacklist_regex() form.fields['eox_api_wait_time'].initial = app_config.get_cisco_eox_api_sync_wait_time() form.fields['homepage_text_before'].initial = hp_content_before.html_content form.fields['homepage_text_after'].initial = hp_content_after.html_content context = { "form": form, "is_cisco_api_enabled": app_config.is_cisco_api_enabled(), "is_cisco_eox_api_auto_sync_enabled": app_config.is_periodic_sync_enabled() } return render(request, "config/change_configuration.html", context=context)