From 63fd546f44806286f782155b7554b8c5dcda456b Mon Sep 17 00:00:00 2001 From: Jake McDermott Date: Tue, 3 Dec 2019 13:36:18 -0500 Subject: [PATCH] Let cred admins and users test credential plugins --- awx/api/views/__init__.py | 1 + .../tests/functional/api/test_credential.py | 12 ++++++++++ .../functional/api/test_credential_type.py | 9 ++++++++ awx/main/tests/functional/conftest.py | 23 ++++++++++++------- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/awx/api/views/__init__.py b/awx/api/views/__init__.py index f938dd5945..4ef4ecf2e2 100644 --- a/awx/api/views/__init__.py +++ b/awx/api/views/__init__.py @@ -1385,6 +1385,7 @@ class CredentialExternalTest(SubDetailAPIView): model = models.Credential serializer_class = serializers.EmptySerializer + obj_permission_type = 'use' def post(self, request, *args, **kwargs): obj = self.get_object() diff --git a/awx/main/tests/functional/api/test_credential.py b/awx/main/tests/functional/api/test_credential.py index 2aa81097ee..adf61d0e45 100644 --- a/awx/main/tests/functional/api/test_credential.py +++ b/awx/main/tests/functional/api/test_credential.py @@ -1439,3 +1439,15 @@ def test_create_credential_with_invalid_url_xfail(post, organization, admin, url assert response.status_code == status if status != 201: assert response.data['inputs']['server_url'] == [msg] + + +@pytest.mark.django_db +def test_external_credential_rbac_test_endpoint(post, alice, external_credential): + url = reverse('api:credential_external_test', kwargs={'pk': external_credential.pk}) + data = {'metadata': {'key': 'some_key'}} + + external_credential.read_role.members.add(alice) + assert post(url, data, alice).status_code == 403 + + external_credential.use_role.members.add(alice) + assert post(url, data, alice).status_code == 202 diff --git a/awx/main/tests/functional/api/test_credential_type.py b/awx/main/tests/functional/api/test_credential_type.py index ecb11e7e0d..45b5e79994 100644 --- a/awx/main/tests/functional/api/test_credential_type.py +++ b/awx/main/tests/functional/api/test_credential_type.py @@ -481,3 +481,12 @@ def test_create_with_undefined_template_variable_xfail(post, admin): }, admin) assert response.status_code == 400 assert "'api_tolkien' is undefined" in json.dumps(response.data) + + +@pytest.mark.django_db +def test_credential_type_rbac_external_test(post, alice, admin, credentialtype_external): + # only admins may use the credential type test endpoint + url = reverse('api:credential_type_external_test', kwargs={'pk': credentialtype_external.pk}) + data = {'inputs': {}, 'metadata': {}} + assert post(url, data, admin).status_code == 202 + assert post(url, data, alice).status_code == 403 diff --git a/awx/main/tests/functional/conftest.py b/awx/main/tests/functional/conftest.py index ee44aad84a..404f92fdcf 100644 --- a/awx/main/tests/functional/conftest.py +++ b/awx/main/tests/functional/conftest.py @@ -280,14 +280,21 @@ def credentialtype_external(): }], 'required': ['url', 'token', 'key'], } - external_type = CredentialType( - kind='external', - managed_by_tower=True, - name='External Service', - inputs=external_type_inputs - ) - external_type.save() - return external_type + + class MockPlugin(object): + def backend(self, **kwargs): + return 'secret' + + with mock.patch('awx.main.models.credential.CredentialType.plugin', new_callable=PropertyMock) as mock_plugin: + mock_plugin.return_value = MockPlugin() + external_type = CredentialType( + kind='external', + managed_by_tower=True, + name='External Service', + inputs=external_type_inputs + ) + external_type.save() + yield external_type @pytest.fixture