|
10 | 10 |
|
11 | 11 | from openwisp_controller.config.api.serializers import BaseConfigSerializer |
12 | 12 | from openwisp_controller.tests.utils import TestAdminMixin |
13 | | -from openwisp_users.tests.test_api import AuthenticationMixin |
| 13 | +from openwisp_users.tests.test_api import AuthenticationMixin, TestMultitenantApiMixin |
14 | 14 | from openwisp_utils.tests import capture_any_output, catch_signal |
15 | 15 |
|
16 | 16 | from .. import settings as app_settings |
|
33 | 33 | OrganizationUser = load_model("openwisp_users", "OrganizationUser") |
34 | 34 |
|
35 | 35 |
|
36 | | -class ApiTestMixin: |
| 36 | +class ApiTestMixin(AuthenticationMixin, TestMultitenantApiMixin): |
37 | 37 | @property |
38 | 38 | def _template_data(self): |
39 | 39 | return { |
@@ -103,7 +103,6 @@ class TestConfigApi( |
103 | 103 | CreateConfigTemplateMixin, |
104 | 104 | TestVpnX509Mixin, |
105 | 105 | CreateDeviceGroupMixin, |
106 | | - AuthenticationMixin, |
107 | 106 | TestCase, |
108 | 107 | ): |
109 | 108 | def setUp(self): |
@@ -671,29 +670,59 @@ def test_template_create_of_vpn_type(self): |
671 | 670 | self.assertEqual(Template.objects.count(), 1) |
672 | 671 | self.assertEqual(r.status_code, 201) |
673 | 672 |
|
674 | | - def test_template_create_with_shared_vpn(self): |
675 | | - org1 = self._get_org() |
676 | | - test_user = self._create_operator(organizations=[org1]) |
677 | | - self.client.force_login(test_user) |
678 | | - vpn1 = self._create_vpn(name="vpn1", organization=None) |
679 | | - path = reverse("config_api:template_list") |
680 | | - data = self._template_data |
681 | | - data["type"] = "vpn" |
682 | | - data["vpn"] = vpn1.id |
683 | | - data["organization"] = org1.pk |
684 | | - r = self.client.post(path, data, content_type="application/json") |
685 | | - self.assertEqual(r.status_code, 201) |
686 | | - self.assertEqual(Template.objects.count(), 1) |
687 | | - self.assertEqual(r.data["vpn"], vpn1.id) |
688 | | - |
689 | | - def test_template_creation_with_no_org_by_operator(self): |
690 | | - path = reverse("config_api:template_list") |
691 | | - data = self._template_data |
| 673 | + def test_operator_access_shared_template(self): |
692 | 674 | test_user = self._create_operator(organizations=[self._get_org()]) |
693 | | - self.client.force_login(test_user) |
694 | | - r = self.client.post(path, data, content_type="application/json") |
695 | | - self.assertEqual(r.status_code, 400) |
696 | | - self.assertIn("This field may not be null.", str(r.content)) |
| 675 | + token = self._obtain_auth_token(test_user) |
| 676 | + self._create_template(organization=None) |
| 677 | + self._test_org_user_access_shared_object( |
| 678 | + listview_name='config_api:template_list', |
| 679 | + detailview_name='config_api:template_detail', |
| 680 | + create_payload={'name': 'test', 'organization': ''}, |
| 681 | + update_payload={'name': 'updated-test'}, |
| 682 | + expected_count=1, |
| 683 | + token=token, |
| 684 | + expected_status_codes={ |
| 685 | + 'create': 400, |
| 686 | + 'list': 200, |
| 687 | + 'retrieve': 403, |
| 688 | + 'update': 403, |
| 689 | + 'delete': 403, |
| 690 | + 'head': 403, |
| 691 | + 'option': 200, |
| 692 | + }, |
| 693 | + ) |
| 694 | + |
| 695 | + def test_org_admin_create_template_with_shared_vpn(self): |
| 696 | + org = self._get_org() |
| 697 | + vpn = self._create_vpn(organization=None) |
| 698 | + create_payload = self._template_data |
| 699 | + create_payload.update( |
| 700 | + { |
| 701 | + 'organization': org.pk, |
| 702 | + 'type': 'vpn', |
| 703 | + 'vpn': vpn.pk, |
| 704 | + } |
| 705 | + ) |
| 706 | + update_payload = create_payload.copy() |
| 707 | + update_payload['name'] = 'updated-test' |
| 708 | + test_user = self._create_operator(organizations=[org]) |
| 709 | + self._test_org_user_access_shared_object( |
| 710 | + listview_name='config_api:template_list', |
| 711 | + detailview_name='config_api:template_detail', |
| 712 | + create_payload=create_payload, |
| 713 | + update_payload=update_payload, |
| 714 | + expected_count=1, |
| 715 | + expected_status_codes={ |
| 716 | + 'create': 201, |
| 717 | + 'list': 200, |
| 718 | + 'retrieve': 200, |
| 719 | + 'update': 200, |
| 720 | + 'delete': 204, |
| 721 | + 'head': 403, |
| 722 | + 'option': 200, |
| 723 | + }, |
| 724 | + token=self._obtain_auth_token(test_user), |
| 725 | + ) |
697 | 726 |
|
698 | 727 | def test_template_create_with_empty_config(self): |
699 | 728 | path = reverse("config_api:template_list") |
@@ -855,19 +884,50 @@ def test_vpn_create_api(self): |
855 | 884 | self.assertEqual(r.status_code, 201) |
856 | 885 | self.assertEqual(Vpn.objects.count(), 1) |
857 | 886 |
|
858 | | - def test_vpn_create_with_shared_objects(self): |
859 | | - org1 = self._get_org() |
860 | | - shared_ca = self._create_ca(name="shared_ca", organization=None) |
861 | | - test_user = self._create_administrator(organizations=[org1]) |
862 | | - self.client.force_login(test_user) |
| 887 | + def test_org_admin_access_vpn_with_shared_objects(self): |
| 888 | + org = self._get_org() |
| 889 | + shared_ca = self._create_ca(name='shared_ca', organization=None) |
| 890 | + create_payload = self._vpn_data |
| 891 | + create_payload.update( |
| 892 | + { |
| 893 | + 'organization': org.pk, |
| 894 | + 'ca': shared_ca.pk, |
| 895 | + } |
| 896 | + ) |
| 897 | + update_payload = create_payload.copy() |
| 898 | + update_payload['name'] = 'updated-test-vpn' |
| 899 | + administrator = self._create_administrator(organizations=[org]) |
| 900 | + self._test_access_shared_object( |
| 901 | + listview_name='config_api:vpn_list', |
| 902 | + detailview_name='config_api:vpn_detail', |
| 903 | + create_payload=create_payload, |
| 904 | + update_payload=update_payload, |
| 905 | + expected_count=1, |
| 906 | + expected_status_codes={ |
| 907 | + 'create': 201, |
| 908 | + 'list': 200, |
| 909 | + 'retrieve': 200, |
| 910 | + 'update': 200, |
| 911 | + 'delete': 204, |
| 912 | + 'head': 200, |
| 913 | + 'option': 200, |
| 914 | + }, |
| 915 | + token=self._obtain_auth_token(administrator), |
| 916 | + ) |
| 917 | + |
| 918 | + def test_org_admin_create_shared_vpn(self): |
| 919 | + shared_ca = self._create_ca(name='shared_ca', organization=None) |
863 | 920 | data = self._vpn_data |
864 | | - data["organization"] = org1.pk |
865 | | - data["ca"] = shared_ca.pk |
866 | | - path = reverse("config_api:vpn_list") |
867 | | - r = self.client.post(path, data, content_type="application/json") |
868 | | - self.assertEqual(Vpn.objects.count(), 1) |
869 | | - self.assertEqual(r.status_code, 201) |
870 | | - self.assertEqual(r.data["ca"], shared_ca.pk) |
| 921 | + data['ca'] = shared_ca.pk |
| 922 | + # API does not allow creating shared VPN by org admin, |
| 923 | + # therefore we create an object to test the detail view. |
| 924 | + self._create_vpn(organization=None, ca=shared_ca) |
| 925 | + self._test_org_user_access_shared_object( |
| 926 | + listview_name='config_api:vpn_list', |
| 927 | + detailview_name='config_api:vpn_detail', |
| 928 | + create_payload=data, |
| 929 | + expected_count=1, |
| 930 | + ) |
871 | 931 |
|
872 | 932 | def test_vpn_list_api(self): |
873 | 933 | org = self._get_org() |
|
0 commit comments