diff --git a/openwisp_controller/config/templates/admin/config/device/delete_confirmation.html b/openwisp_controller/config/templates/admin/config/device/delete_confirmation.html index 6830ae019..54a6b7890 100644 --- a/openwisp_controller/config/templates/admin/config/device/delete_confirmation.html +++ b/openwisp_controller/config/templates/admin/config/device/delete_confirmation.html @@ -30,10 +30,10 @@

{% translate "Objects" %}

{% csrf_token %}
- {% if is_popup %}{% endif %} {% if to_field %}{% endif %} + {% if deactivating_warning %}{% endif %} {% translate "No, take me back" %}
diff --git a/openwisp_controller/config/tests/test_selenium.py b/openwisp_controller/config/tests/test_selenium.py index c096724d9..d03dfdf6b 100644 --- a/openwisp_controller/config/tests/test_selenium.py +++ b/openwisp_controller/config/tests/test_selenium.py @@ -10,11 +10,14 @@ from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import Select, WebDriverWait +from swapper import load_model from openwisp_utils.test_selenium_mixins import SeleniumTestMixin from .utils import CreateConfigTemplateMixin, TestWireguardVpnMixin +Device = load_model('config', 'Device') + class SeleniumBaseMixin(CreateConfigTemplateMixin, SeleniumTestMixin): def setUp(self): @@ -286,6 +289,77 @@ def test_template_context_variables(self): alert.accept() self.fail('Unsaved changes alert displayed without any change') + def test_force_delete_device_with_deactivating_config(self): + self._create_template(default=True) + config = self._create_config(organization=self._get_org()) + device = config.device + self.assertEqual(device.is_deactivated(), False) + self.assertEqual(config.status, 'modified') + + self.login() + self.open(reverse('admin:config_device_change', args=[device.id])) + self.web_driver.find_elements( + by=By.CSS_SELECTOR, value='input.deletelink[type="submit"]' + )[-1].click() + device.refresh_from_db() + config.refresh_from_db() + self.assertEqual(device.is_deactivated(), True) + self.assertEqual(config.is_deactivating(), True) + + self.open(reverse('admin:config_device_change', args=[device.id])) + self.web_driver.find_elements(by=By.CSS_SELECTOR, value='a.deletelink')[ + -1 + ].click() + WebDriverWait(self.web_driver, 2).until( + EC.visibility_of_element_located( + (By.CSS_SELECTOR, '#deactivating-warning .messagelist .warning p') + ) + ) + self.web_driver.find_element(by=By.CSS_SELECTOR, value='#warning-ack').click() + delete_confirm = WebDriverWait(self.web_driver, 2).until( + EC.visibility_of_element_located( + (By.CSS_SELECTOR, 'form[method="post"] input[type="submit"]') + ) + ) + delete_confirm.click() + self.assertEqual(Device.objects.count(), 0) + + def test_force_delete_multiple_devices_with_deactivating_config(self): + self._create_template(default=True) + org = self._get_org() + device1 = self._create_device(organization=org) + config1 = self._create_config(device=device1) + device2 = self._create_device( + organization=org, name='test2', mac_address='22:22:22:22:22:22' + ) + config2 = self._create_config(device=device2) + self.assertEqual(device1.is_deactivated(), False) + self.assertEqual(config1.status, 'modified') + self.assertEqual(device2.is_deactivated(), False) + self.assertEqual(config2.status, 'modified') + + self.login() + self.open(reverse('admin:config_device_changelist')) + self.web_driver.find_element(by=By.CSS_SELECTOR, value='#action-toggle').click() + select = Select(self.web_driver.find_element(by=By.NAME, value='action')) + select.select_by_value('delete_selected') + self.web_driver.find_element( + by=By.CSS_SELECTOR, value='button[type="submit"][name="index"][value="0"]' + ).click() + WebDriverWait(self.web_driver, 2).until( + EC.visibility_of_element_located( + (By.CSS_SELECTOR, '#deactivating-warning .messagelist .warning p') + ) + ) + self.web_driver.find_element(by=By.CSS_SELECTOR, value='#warning-ack').click() + delete_confirm = WebDriverWait(self.web_driver, 2).until( + EC.visibility_of_element_located( + (By.CSS_SELECTOR, 'form[method="post"] input[type="submit"]') + ) + ) + delete_confirm.click() + self.assertEqual(Device.objects.count(), 0) + class TestVpnAdmin(SeleniumBaseMixin, TestWireguardVpnMixin, StaticLiveServerTestCase): def test_vpn_edit(self): diff --git a/openwisp_controller/tests/test_selenium.py b/openwisp_controller/tests/test_selenium.py index fd451114f..e5369293a 100644 --- a/openwisp_controller/tests/test_selenium.py +++ b/openwisp_controller/tests/test_selenium.py @@ -57,7 +57,7 @@ def test_restoring_deleted_device(self, *args): reverse(f'admin:{self.config_app_label}_device_delete', args=[device.id]) ) self.web_driver.find_element( - by=By.XPATH, value='//*[@id="content"]/form/div/input[2]' + by=By.CSS_SELECTOR, value='#content form input[type="submit"]' ).click() self.assertEqual(Device.objects.count(), 0) self.assertEqual(DeviceConnection.objects.count(), 0) diff --git a/runtests.py b/runtests.py index 6683ceb9f..0bfb4bdba 100755 --- a/runtests.py +++ b/runtests.py @@ -35,4 +35,4 @@ else: app_dir = 'tests/openwisp2/' - sys.exit(pytest.main([app_dir])) + # sys.exit(pytest.main([app_dir]))