Skip to content

Commit

Permalink
[tests] Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Jan 7, 2025
1 parent 9960afc commit f8b3b9f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ <h2>{% translate "Objects" %}</h2>
<form method="post">{% csrf_token %}
<div>
<input type="hidden" name="post" value="yes">
<input type="hidden" name="force_delete" value="false">
{% if is_popup %}<input type="hidden" name="{{ is_popup_var }}" value="1">{% endif %}
{% if to_field %}<input type="hidden" name="{{ to_field_var }}" value="{{ to_field }}">{% endif %}
<input type="submit" value="{% translate 'Yes, I’m sure' %}">
{% if deactivating_warning %}<input type="hidden" name="force_delete" value="false">{% endif %}
<a href="#" class="button cancel-link">{% translate "No, take me back" %}</a>
</div>
</form>
Expand Down
74 changes: 74 additions & 0 deletions openwisp_controller/config/tests/test_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion openwisp_controller/tests/test_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
else:
app_dir = 'tests/openwisp2/'

sys.exit(pytest.main([app_dir]))
# sys.exit(pytest.main([app_dir]))

0 comments on commit f8b3b9f

Please sign in to comment.