Best Python code snippet using tempest_python
test_project_endpoint.py
Source:test_project_endpoint.py  
1# Copyright 2020 SUSE LLC2#3# Licensed under the Apache License, Version 2.0 (the "License"); you may4# not use this file except in compliance with the License. You may obtain5# a copy of the License at6#7#      http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the12# License for the specific language governing permissions and limitations13# under the License.14import abc15from tempest.api.identity import base16from tempest.lib.common.utils import data_utils17from tempest.lib import exceptions18from keystone_tempest_plugin.tests.rbac.v3 import base as rbac_base19class IdentityV3RbacProjectEndpointsTests(rbac_base.IdentityV3RbacBaseTests,20                                          metaclass=abc.ABCMeta):21    @classmethod22    def setup_clients(cls):23        super(IdentityV3RbacProjectEndpointsTests, cls).setup_clients()24        cls.persona = getattr(cls, 'os_%s' % cls.credentials[0])25        cls.client = cls.persona.endpoint_filter_client26        cls.admin_client = cls.os_system_admin27        cls.admin_ef_client = cls.admin_client.endpoint_filter_client28    @classmethod29    def resource_setup(cls):30        super(IdentityV3RbacProjectEndpointsTests, cls).resource_setup()31        cls.project_id = cls.admin_client.projects_client.create_project(32            name=data_utils.rand_name())['project']['id']33        cls.addClassResourceCleanup(34            cls.admin_client.projects_client.delete_project,35            project_id=cls.project_id)36        service = cls.admin_client.identity_services_v3_client.create_service(37            type=data_utils.rand_name())['service']38        cls.addClassResourceCleanup(39            cls.admin_client.identity_services_v3_client.delete_service,40            service['id'])41        cls.endpoint_id = cls.admin_client.endpoints_v3_client.create_endpoint(42            interface='public',43            url='http://localhost/foo',44            service_id=service['id'])['endpoint']['id']45        cls.addClassResourceCleanup(46            cls.admin_client.endpoints_v3_client.delete_endpoint,47            endpoint_id=cls.endpoint_id)48    @abc.abstractmethod49    def test_identity_add_endpoint_to_project(self):50        """Test identity:add_endpoint_to_project policy.51        This test must check:52          * whether the persona can allow a project to access an endpoint53        """54        pass55    @abc.abstractmethod56    def test_identity_check_endpoint_in_project(self):57        """Test identity:check_endpoint_in_project policy.58        This test must check:59          * whether the persona can check if a project has access to an60            endpoint61        """62        pass63    @abc.abstractmethod64    def test_identity_list_projects_for_endpoint(self):65        """Test identity:list_projects_for_endpoint policy.66        This test must check:67          * whether the persona can list all projects that have access to an68            endpoint69        """70        pass71    @abc.abstractmethod72    def test_identity_list_endpoints_for_project(self):73        """Test identity:list_endpoints_for_project policy.74        This test must check:75          * whether the persona can list all endpoints to which a project has76            access77        """78        pass79    @abc.abstractmethod80    def test_identity_remove_endpoint_from_project(self):81        """Test identity:remove_endpoint_from_project policy.82        This test must check83          * whether the persona can remove a project's access to an endpoint84        """85        pass86class SystemAdminTests(IdentityV3RbacProjectEndpointsTests):87    credentials = ['system_admin']88    def test_identity_add_endpoint_to_project(self):89        self.do_request('add_endpoint_to_project',90                        expected_status=204,91                        project_id=self.project_id,92                        endpoint_id=self.endpoint_id)93        self.addCleanup(self.admin_ef_client.delete_endpoint_from_project,94                        project_id=self.project_id,95                        endpoint_id=self.endpoint_id)96    def test_identity_check_endpoint_in_project(self):97        self.admin_ef_client.add_endpoint_to_project(98            project_id=self.project_id,99            endpoint_id=self.endpoint_id)100        self.addCleanup(self.admin_ef_client.delete_endpoint_from_project,101                        project_id=self.project_id,102                        endpoint_id=self.endpoint_id)103        self.do_request('check_endpoint_in_project',104                        expected_status=204,105                        project_id=self.project_id,106                        endpoint_id=self.endpoint_id)107    def test_identity_list_projects_for_endpoint(self):108        self.admin_ef_client.add_endpoint_to_project(109            project_id=self.project_id,110            endpoint_id=self.endpoint_id)111        self.addCleanup(self.admin_ef_client.delete_endpoint_from_project,112                        project_id=self.project_id,113                        endpoint_id=self.endpoint_id)114        resp = self.do_request('list_projects_for_endpoint',115                               endpoint_id=self.endpoint_id)116        self.assertIn(self.project_id, [p['id'] for p in resp['projects']])117    def test_identity_list_endpoints_for_project(self):118        self.admin_ef_client.add_endpoint_to_project(119            project_id=self.project_id,120            endpoint_id=self.endpoint_id)121        self.addCleanup(self.admin_ef_client.delete_endpoint_from_project,122                        project_id=self.project_id,123                        endpoint_id=self.endpoint_id)124        resp = self.do_request('list_endpoints_in_project',125                               project_id=self.project_id)126        self.assertIn(self.endpoint_id, [e['id'] for e in resp['endpoints']])127    def test_identity_remove_endpoint_from_project(self):128        self.admin_ef_client.add_endpoint_to_project(129            project_id=self.project_id,130            endpoint_id=self.endpoint_id)131        self.do_request('delete_endpoint_from_project',132                        expected_status=204,133                        project_id=self.project_id,134                        endpoint_id=self.endpoint_id)135class SystemMemberTests(SystemAdminTests):136    credentials = ['system_member', 'system_admin']137    def test_identity_add_endpoint_to_project(self):138        self.do_request('add_endpoint_to_project',139                        expected_status=exceptions.Forbidden,140                        project_id=self.project_id,141                        endpoint_id=self.endpoint_id)142    def test_identity_remove_endpoint_from_project(self):143        self.admin_ef_client.add_endpoint_to_project(144            project_id=self.project_id,145            endpoint_id=self.endpoint_id)146        self.addCleanup(self.admin_ef_client.delete_endpoint_from_project,147                        project_id=self.project_id,148                        endpoint_id=self.endpoint_id)149        self.do_request('delete_endpoint_from_project',150                        expected_status=exceptions.Forbidden,151                        project_id=self.project_id,152                        endpoint_id=self.endpoint_id)153class SystemReaderTests(SystemMemberTests):154    credentials = ['system_reader', 'system_admin']155class DomainAdminTests(SystemReaderTests, base.BaseIdentityTest):156    credentials = ['domain_admin', 'system_admin']157    def test_identity_check_endpoint_in_project(self):158        self.admin_ef_client.add_endpoint_to_project(159            project_id=self.project_id,160            endpoint_id=self.endpoint_id)161        self.addCleanup(self.admin_ef_client.delete_endpoint_from_project,162                        project_id=self.project_id,163                        endpoint_id=self.endpoint_id)164        self.do_request('check_endpoint_in_project',165                        expected_status=exceptions.Forbidden,166                        project_id=self.project_id,167                        endpoint_id=self.endpoint_id)168    def test_identity_list_projects_for_endpoint(self):169        self.admin_ef_client.add_endpoint_to_project(170            project_id=self.project_id,171            endpoint_id=self.endpoint_id)172        self.addCleanup(self.admin_ef_client.delete_endpoint_from_project,173                        project_id=self.project_id,174                        endpoint_id=self.endpoint_id)175        self.do_request('list_projects_for_endpoint',176                        expected_status=exceptions.Forbidden,177                        endpoint_id=self.endpoint_id)178    def test_identity_list_endpoints_for_project(self):179        self.admin_ef_client.add_endpoint_to_project(180            project_id=self.project_id,181            endpoint_id=self.endpoint_id)182        self.addCleanup(self.admin_ef_client.delete_endpoint_from_project,183                        project_id=self.project_id,184                        endpoint_id=self.endpoint_id)185        self.do_request('list_endpoints_in_project',186                        expected_status=exceptions.Forbidden,187                        project_id=self.project_id)188class DomainMemberTests(DomainAdminTests, base.BaseIdentityTest):189    credentials = ['domain_member', 'system_admin']190class DomainReaderTests(DomainMemberTests):191    credentials = ['domain_reader', 'system_admin']192class ProjectAdminTests(DomainReaderTests, base.BaseIdentityTest):193    credentials = ['project_admin', 'system_admin']194class ProjectMemberTests(ProjectAdminTests):195    credentials = ['project_member', 'system_admin']196class ProjectReaderTests(ProjectAdminTests):...endpoint_filter_client.py
Source:endpoint_filter_client.py  
...41            self.ep_filter + '/projects/%s/endpoints/%s' %42            (project_id, endpoint_id), None)43        self.expected_success(204, resp.status)44        return rest_client.ResponseBody(resp, body)45    def list_endpoints_in_project(self, project_id):46        """List Endpoints associated with Project."""47        resp, body = self.get(self.ep_filter + '/projects/%s/endpoints'48                              % project_id)49        self.expected_success(200, resp.status)50        body = json.loads(body)51        return rest_client.ResponseBody(resp, body)52    def delete_endpoint_from_project(self, project_id, endpoint_id):53        """Delete association between project and endpoint."""54        resp, body = self.delete(55            self.ep_filter + '/projects/%s/endpoints/%s'56            % (project_id, endpoint_id))57        self.expected_success(204, resp.status)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
