|
| 1 | +import json |
1 | 2 | import time
|
2 | 3 |
|
| 4 | +from django.contrib.auth.models import Permission |
3 | 5 | from django.contrib.staticfiles.testing import StaticLiveServerTestCase
|
4 | 6 | from django.test import tag
|
5 | 7 | from django.urls.base import reverse
|
|
13 | 15 |
|
14 | 16 | from openwisp_utils.tests import SeleniumTestMixin as BaseSeleniumTestMixin
|
15 | 17 |
|
16 |
| -from .utils import CreateConfigTemplateMixin, TestVpnX509Mixin, TestWireguardVpnMixin |
| 18 | +from .utils import ( |
| 19 | + CreateConfigTemplateMixin, |
| 20 | + CreateDeviceGroupMixin, |
| 21 | + TestVpnX509Mixin, |
| 22 | + TestWireguardVpnMixin, |
| 23 | +) |
17 | 24 |
|
18 | 25 | Device = load_model("config", "Device")
|
19 | 26 | DeviceGroup = load_model("config", "DeviceGroup")
|
| 27 | +OrganizationConfigSettings = load_model("config", "OrganizationConfigSettings") |
20 | 28 | Cert = load_model("django_x509", "Cert")
|
21 | 29 |
|
22 | 30 |
|
@@ -44,6 +52,80 @@ def _verify_templates_visibility(self, hidden=None, visible=None):
|
44 | 52 | for template in visible:
|
45 | 53 | self.wait_for_visibility(By.XPATH, f'//*[@value="{template.id}"]')
|
46 | 54 |
|
| 55 | + def _create_readonly_user( |
| 56 | + self, username="readonly_user", email="[email protected]", organization=None |
| 57 | + ): |
| 58 | + """ |
| 59 | + Creates a readonly user with staff privileges and view-only permissions. |
| 60 | + Returns the user object. |
| 61 | + """ |
| 62 | + readonly_user = self._create_user(username=username, email=email, is_staff=True) |
| 63 | + org = organization or self._get_org() |
| 64 | + self._create_org_user(user=readonly_user, organization=org, is_admin=True) |
| 65 | + readonly_user.user_permissions.add( |
| 66 | + *Permission.objects.filter( |
| 67 | + codename__in=[ |
| 68 | + "view_device", |
| 69 | + "view_template", |
| 70 | + "view_vpn", |
| 71 | + "view_config", |
| 72 | + "view_devicegroup", |
| 73 | + "view_organization", |
| 74 | + "view_organizationconfigsettings", |
| 75 | + ] |
| 76 | + ) |
| 77 | + ) |
| 78 | + return readonly_user |
| 79 | + |
| 80 | + def _test_readonly_json_fields( |
| 81 | + self, |
| 82 | + url, |
| 83 | + field_selectors, |
| 84 | + scroll_to_bottom=True, |
| 85 | + hide_loading_overlay=True, |
| 86 | + user=None, |
| 87 | + ): |
| 88 | + """ |
| 89 | + Reusable method to test readonly JSON fields rendering. |
| 90 | +
|
| 91 | + Args: |
| 92 | + url: The URL to open for testing |
| 93 | + field_selectors: Dictionary where key is CSS selector and value is |
| 94 | + expected text content |
| 95 | + scroll_to_bottom: Whether to scroll to bottom of page (default: True) |
| 96 | + user: User object to login as. If None, creates a readonly user |
| 97 | + (default: None) |
| 98 | + """ |
| 99 | + if user is None: |
| 100 | + org = self._get_org() |
| 101 | + user = self._create_readonly_user(organization=org) |
| 102 | + |
| 103 | + self.login(username=user.username, password="tester") |
| 104 | + self.open(url) |
| 105 | + if hide_loading_overlay: |
| 106 | + self.hide_loading_overlay() |
| 107 | + |
| 108 | + if scroll_to_bottom: |
| 109 | + self.web_driver.execute_script( |
| 110 | + "window.scrollTo(0, document.body.scrollHeight);" |
| 111 | + ) |
| 112 | + |
| 113 | + for css_selector, expected_content in field_selectors.items(): |
| 114 | + readonly_element = self.find_element( |
| 115 | + by=By.CSS_SELECTOR, |
| 116 | + value=css_selector, |
| 117 | + ) |
| 118 | + self.assertEqual(readonly_element.is_displayed(), True) |
| 119 | + if isinstance(expected_content, dict): |
| 120 | + # If expected_content is a dict, format it as JSON |
| 121 | + self.assertEqual( |
| 122 | + readonly_element.text, |
| 123 | + json.dumps(expected_content, indent=4), |
| 124 | + ) |
| 125 | + else: |
| 126 | + # Otherwise, check if the text contains the expected content |
| 127 | + self.assertIn(expected_content, readonly_element.text) |
| 128 | + |
47 | 129 |
|
48 | 130 | @tag("selenium_tests")
|
49 | 131 | class TestDeviceAdmin(
|
@@ -373,11 +455,73 @@ def test_add_remove_templates(self):
|
373 | 455 | self.assertEqual(config.templates.count(), 0)
|
374 | 456 | self.assertEqual(config.status, "modified")
|
375 | 457 |
|
| 458 | + def test_readonly_config_fields(self): |
| 459 | + """ |
| 460 | + Test that configuration variables and configuration render properly |
| 461 | + when the device only has read only permission. |
| 462 | + """ |
| 463 | + org = self._get_org() |
| 464 | + readonly_user = self._create_readonly_user(organization=org) |
| 465 | + |
| 466 | + template = self._create_template( |
| 467 | + organization=org, |
| 468 | + default_values={"mac_address": "00:00:00:00:00:00", "ssid": "OpenWisp"}, |
| 469 | + config={ |
| 470 | + "interfaces": [ |
| 471 | + { |
| 472 | + "name": "wlan0", |
| 473 | + "network": "br-lan", |
| 474 | + "type": "wireless", |
| 475 | + "wireless": { |
| 476 | + "mode": "access_point", |
| 477 | + "radio": "radio0", |
| 478 | + "ssid": "{{ ssid }}", |
| 479 | + }, |
| 480 | + } |
| 481 | + ] |
| 482 | + }, |
| 483 | + ) |
| 484 | + device = self._create_device(organization=org) |
| 485 | + config = self._create_config( |
| 486 | + device=device, |
| 487 | + context={"hostname": "readonly-device", "ssid": "ReadOnlyWiFi"}, |
| 488 | + ) |
| 489 | + config.templates.add(template) |
| 490 | + |
| 491 | + with self.subTest("Template default values and config rendered as readonly"): |
| 492 | + template_url = reverse("admin:config_template_change", args=[template.id]) |
| 493 | + template_selectors = { |
| 494 | + ".field-default_values .readonly pre.readonly-json-widget": ( |
| 495 | + template.default_values |
| 496 | + ), |
| 497 | + ".field-config .readonly pre.readonly-json-widget": template.config, |
| 498 | + } |
| 499 | + self._test_readonly_json_fields( |
| 500 | + url=template_url, field_selectors=template_selectors, user=readonly_user |
| 501 | + ) |
| 502 | + |
| 503 | + with self.subTest("Device configuration variables rendered as readonly"): |
| 504 | + device_url = ( |
| 505 | + reverse("admin:config_device_change", args=[device.id]) |
| 506 | + + "#config-group" |
| 507 | + ) |
| 508 | + device_selectors = { |
| 509 | + ".field-context .readonly pre.readonly-json-widget": { |
| 510 | + "hostname": "readonly-device", |
| 511 | + "ssid": "ReadOnlyWiFi", |
| 512 | + }, |
| 513 | + ".field-config .readonly pre.readonly-json-widget": config.config, |
| 514 | + } |
| 515 | + self._test_readonly_json_fields( |
| 516 | + url=device_url, field_selectors=device_selectors, user=readonly_user |
| 517 | + ) |
| 518 | + |
376 | 519 |
|
377 | 520 | @tag("selenium_tests")
|
378 | 521 | class TestDeviceGroupAdmin(
|
379 | 522 | SeleniumTestMixin,
|
380 | 523 | CreateConfigTemplateMixin,
|
| 524 | + CreateDeviceGroupMixin, |
381 | 525 | StaticLiveServerTestCase,
|
382 | 526 | ):
|
383 | 527 | def test_show_relevant_templates(self):
|
@@ -476,6 +620,35 @@ def test_show_relevant_templates(self):
|
476 | 620 | False,
|
477 | 621 | )
|
478 | 622 |
|
| 623 | + def test_readonly_devicegroup(self): |
| 624 | + """ |
| 625 | + Test that device group context renders properly |
| 626 | + when the user only has read only permission. |
| 627 | + """ |
| 628 | + org = self._get_org() |
| 629 | + readonly_user = self._create_readonly_user(organization=org) |
| 630 | + device_group = self._create_device_group( |
| 631 | + name="readonly-group", |
| 632 | + organization=org, |
| 633 | + context={"mesh_id": "readonly-mesh", "vni": "100"}, |
| 634 | + ) |
| 635 | + |
| 636 | + device_group_url = reverse( |
| 637 | + "admin:config_devicegroup_change", args=[device_group.id] |
| 638 | + ) |
| 639 | + device_group_selectors = { |
| 640 | + ".field-context .readonly pre.readonly-json-widget": device_group.context, |
| 641 | + ".field-meta_data .readonly pre.readonly-json-widget": ( |
| 642 | + device_group.meta_data |
| 643 | + ), |
| 644 | + } |
| 645 | + self._test_readonly_json_fields( |
| 646 | + url=device_group_url, |
| 647 | + field_selectors=device_group_selectors, |
| 648 | + user=readonly_user, |
| 649 | + hide_loading_overlay=False, |
| 650 | + ) |
| 651 | + |
479 | 652 |
|
480 | 653 | @tag("selenium_tests")
|
481 | 654 | class TestDeviceAdminUnsavedChanges(
|
@@ -618,3 +791,43 @@ def test_vpn_edit(self):
|
618 | 791 | backend.select_by_visible_text("OpenVPN")
|
619 | 792 | self.wait_for_invisibility(by=By.CLASS_NAME, value="field-webhook_endpoint")
|
620 | 793 | self.wait_for_invisibility(by=By.CLASS_NAME, value="field-auth_token")
|
| 794 | + |
| 795 | + def test_readonly_vpn_config(self): |
| 796 | + """ |
| 797 | + Test that VPN configuration renders properly |
| 798 | + when the user only has read only permission. |
| 799 | + """ |
| 800 | + org = self._get_org() |
| 801 | + readonly_user = self._create_readonly_user(organization=org) |
| 802 | + vpn = self._create_wireguard_vpn(organization=org) |
| 803 | + |
| 804 | + vpn_url = reverse("admin:config_vpn_change", args=[vpn.id]) |
| 805 | + vpn_selectors = { |
| 806 | + ".field-config .readonly pre.readonly-json-widget": vpn.config, |
| 807 | + } |
| 808 | + self._test_readonly_json_fields( |
| 809 | + url=vpn_url, field_selectors=vpn_selectors, user=readonly_user |
| 810 | + ) |
| 811 | + |
| 812 | + |
| 813 | +@tag("selenium_tests") |
| 814 | +class TestOrganizationConfigSettingsInlineAdmin( |
| 815 | + SeleniumTestMixin, CreateConfigTemplateMixin, StaticLiveServerTestCase |
| 816 | +): |
| 817 | + def test_organization_config_settings_readonly_fields(self): |
| 818 | + org = self._get_org() |
| 819 | + config_settings = OrganizationConfigSettings.objects.create( |
| 820 | + organization=org, |
| 821 | + context={"key1": "value1", "key2": "value2"}, |
| 822 | + ) |
| 823 | + readonly_user = self._create_readonly_user(organization=org) |
| 824 | + self._test_readonly_json_fields( |
| 825 | + url=reverse("admin:openwisp_users_organization_change", args=[org.id]), |
| 826 | + field_selectors={ |
| 827 | + ".field-context .readonly pre.readonly-json-widget": ( |
| 828 | + config_settings.context |
| 829 | + ), |
| 830 | + }, |
| 831 | + user=readonly_user, |
| 832 | + hide_loading_overlay=False, |
| 833 | + ) |
0 commit comments