def test_manual_task_with_multiple_blacklist_entries(self, monkeypatch): self.mock_api_call(monkeypatch) # try to execute it, while no auto-sync is enabled app = AppSettings() app.set_periodic_sync_enabled(False) app.set_cisco_eox_api_queries("WS-C2960-*") app.set_product_blacklist_regex("WS-C2950G-48-EI-WS;WS-C2950G-24-EI") task = tasks.execute_task_to_synchronize_cisco_eox_states.delay( ignore_periodic_sync_flag=True) expected_result = [ '<p style="text-align: left;">The following queries were executed:<br>' '<ul style="text-align: left;"><li><code>WS-C2960-*</code> (<b>affects 3 products</b>, ' 'success)</li></ul>' ] assert task is not None assert task.status == "SUCCESS", task.traceback assert task.state == TaskState.SUCCESS for er in expected_result: assert er in task.info.get("status_message") assert NotificationMessage.objects.count( ) == 1, "Task should create a Notification Message" assert Product.objects.count( ) == 1, "Only a single product is imported" nm = NotificationMessage.objects.first() assert nm.type == NotificationMessage.MESSAGE_SUCCESS, "Incomplete configuration, should throw a warning "
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 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_product_blacklist_regex_from_config(self): settings = AppSettings() # get values value = settings.get_product_blacklist_regex() assert value == "" # set values blacklist_entries = "test\nanother\ntest" settings.set_product_blacklist_regex(blacklist_entries) value = settings.get_product_blacklist_regex() assert value == blacklist_entries
def test_manual_task_with_multiple_blacklist_entries(self, monkeypatch): self.mock_api_call(monkeypatch) # try to execute it, while no auto-sync is enabled app = AppSettings() app.set_periodic_sync_enabled(False) app.set_cisco_eox_api_queries("WS-C2960-*") app.set_product_blacklist_regex("WS-C2950G-48-EI-WS;WS-C2950G-24-EI") task = tasks.execute_task_to_synchronize_cisco_eox_states.delay(ignore_periodic_sync_flag=True) expected_result = '<div style="text-align:left;"><h3>Query: WS-C2960-*</h3>The following products are ' \ 'affected by this update:</p><ul><li>Product data for <code>WS-C2950G-48-EI-WS</code> ' \ 'ignored</li><li>create the Product <code>WS-C2950T-48-SI-WS</code> in the ' \ 'database</li><li>Product data for <code>WS-C2950G-24-EI</code> ignored</li>' \ '</ul></div>' assert task is not None assert task.status == "SUCCESS", task.traceback assert task.state == TaskState.SUCCESS assert task.info.get("status_message") == expected_result assert NotificationMessage.objects.count() == 1, "Task should create a Notification Message" assert Product.objects.count() == 1, "Only a single product is imported"
def test_offline_valid_update_cisco_eox_database_with_create_flag_and_invalid_blacklist(self, monkeypatch): # mock the underlying GET request def mock_response(): r = Response() r.status_code = 200 with open("app/ciscoeox/tests/data/cisco_eox_response_page_1_of_1.json") as f: r._content = f.read().encode("utf-8") return r monkeypatch.setattr(requests, "get", lambda x, headers: mock_response()) app = AppSettings() app.set_product_blacklist_regex("*-WS$") result = api_crawler.update_cisco_eox_database("WS-C2950G-48-EI-WS") assert len(result) == 3, "Three products should be imported" # every product should contain the following values for e in result: assert "blacklist" in e assert "updated" in e assert "created" in e assert "PID" in e assert "message" in e if e["PID"] == "WS-C2950G-48-EI-WS": # Nothing is blacklisted, input of an invalid regular expression should not be possible assert e["blacklist"] is False assert e["updated"] is True assert e["created"] is True assert type(e["PID"]) == str assert e["message"] is None else: assert e["blacklist"] is False assert e["updated"] is True assert e["created"] is True assert type(e["PID"]) == str assert e["message"] is None assert Product.objects.count() == 3, "No products are created, because the creation mode is disabled by default"
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)
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)