Best Python code snippet using localstack_python
test_permissioning.py
Source:test_permissioning.py  
...468        PERMISSION_TESTING_KEY_ID = response["response"]["id"]469    def default_permissions_denies_create_update_delete_api_keys(self):470        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)471        self.assertEqual(c.create_api_key(), default_action_forbidden_response("create_api_key"))472        self.assertEqual(c.update_api_key("whatever"), default_action_forbidden_response("update_api_key"))473        self.assertEqual(c.delete_api_key("whatever"), default_action_forbidden_response("delete_api_key"))474    def default_allow_false_forbids_all_functions(self):475        setup = self.client.update_api_key(476            PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": False, "permissions": {}}477        )478        self.assertTrue(setup.get("ok"), setup)479        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)480        for method in all_client_functions:481            method = dict(method)482            response = getattr(c, method["fn"])(*method["params"])483            self.assertEqual(response.get("status"), 403, method)484    def default_allow_true_allows_all_functions(self):485        setup = self.client.update_api_key(PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": True, "permissions": {}})486        self.assertTrue(setup.get("ok"), setup)487        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)488        for method in all_client_functions:489            method = dict(method)490            response = getattr(c, method["fn"])(*method["params"])491            self.assertNotEqual(response.get("status"), 403, method)492    # GLOBAL GROUP #493    def global_allow_create_works_with_all_create_functions(self):494        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)495        # Check default true, allow false496        setup = self.client.update_api_key(497            PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": True, "permissions": {"allow_create": False}}498        )499        self.assertTrue(setup.get("ok"), setup)500        methods = get_permission_type_functions("create")501        for method in methods:502            method = dict(method)503            response = getattr(c, method["fn"])(*method["params"])504            self.assertEqual(response.get("status"), 403, method)505        # Check default false, allow true506        setup = self.client.update_api_key(507            PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": False, "permissions": {"allow_create": True}}508        )509        self.assertTrue(setup.get("ok"), setup)510        for method in methods:511            method = dict(method)512            response = getattr(c, method["fn"])(*method["params"])513            self.assertNotEqual(response.get("status"), 403, method)514    def global_allow_read_works_with_all_read_functions(self):515        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)516        # Check default true, allow false517        setup = self.client.update_api_key(518            PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": True, "permissions": {"allow_read": False}}519        )520        self.assertTrue(setup.get("ok"), setup)521        methods = get_permission_type_functions("read")522        for method in methods:523            method = dict(method)524            response = getattr(c, method["fn"])(*method["params"])525            self.assertEqual(response.get("status"), 403, method)526        # Check default false, allow true527        setup = self.client.update_api_key(528            PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": False, "permissions": {"allow_read": True}}529        )530        self.assertTrue(setup.get("ok"), setup)531        for method in methods:532            method = dict(method)533            response = getattr(c, method["fn"])(*method["params"])534            self.assertNotEqual(response.get("status"), 403, method)535    def global_allow_update_works_with_all_update_functions(self):536        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)537        # Check default true, allow false538        setup = self.client.update_api_key(539            PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": True, "permissions": {"allow_update": False}}540        )541        self.assertTrue(setup.get("ok"), setup)542        methods = get_permission_type_functions("update")543        for method in methods:544            method = dict(method)545            response = getattr(c, method["fn"])(*method["params"])546            self.assertEqual(response.get("status"), 403, method)547        # Check default false, allow true548        setup = self.client.update_api_key(549            PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": False, "permissions": {"allow_update": True}}550        )551        self.assertTrue(setup.get("ok"), setup)552        for method in methods:553            method = dict(method)554            response = getattr(c, method["fn"])(*method["params"])555            self.assertNotEqual(response.get("status"), 403, method)556    def global_allow_delete_works_with_all_delete_functions(self):557        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)558        # Check default true, allow false559        setup = self.client.update_api_key(560            PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": True, "permissions": {"allow_delete": False}}561        )562        self.assertTrue(setup.get("ok"), setup)563        methods = get_permission_type_functions("delete")564        for method in methods:565            method = dict(method)566            response = getattr(c, method["fn"])(*method["params"])567            self.assertEqual(response.get("status"), 403, method)568        # Check default false, allow true569        setup = self.client.update_api_key(570            PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": False, "permissions": {"allow_delete": True}}571        )572        self.assertTrue(setup.get("ok"), setup)573        for method in methods:574            method = dict(method)575            response = getattr(c, method["fn"])(*method["params"])576            self.assertNotEqual(response.get("status"), 403, method)577    # Specific GROUP #578    def group_allow_create_works_with_group_create_functions(self):579        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)580        for group in permission_groups:581            # Check default true, allow false582            setup = self.client.update_api_key(583                PERMISSION_TESTING_KEY_ID,584                permissions_document={"version": "1", "default_allow": True, "permissions": {group: {"allow_create": False}}},585            )586            self.assertTrue(setup.get("ok"), setup)587            methods = get_permission_group_functions(group).intersection(get_permission_type_functions("create"))588            for method in methods:589                method = dict(method)590                response = getattr(c, method["fn"])(*method["params"])591                self.assertEqual(response.get("status"), 403, method)592            # Check default false, allow true593            setup = self.client.update_api_key(594                PERMISSION_TESTING_KEY_ID,595                permissions_document={"version": "1", "default_allow": False, "permissions": {group: {"allow_create": True}}},596            )597            self.assertTrue(setup.get("ok"), setup)598            for method in methods:599                method = dict(method)600                response = getattr(c, method["fn"])(*method["params"])601                self.assertNotEqual(response.get("status"), 403, method)602    def group_allow_read_works_with_group_read_functions(self):603        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)604        for group in permission_groups:605            # Check default true, allow false606            setup = self.client.update_api_key(607                PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": True, "permissions": {group: {"allow_read": False}}}608            )609            self.assertTrue(setup.get("ok"), setup)610            methods = get_permission_group_functions(group).intersection(get_permission_type_functions("read"))611            for method in methods:612                method = dict(method)613                response = getattr(c, method["fn"])(*method["params"])614                self.assertEqual(response.get("status"), 403, method)615            # Check default false, allow true616            setup = self.client.update_api_key(617                PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": False, "permissions": {group: {"allow_read": True}}}618            )619            self.assertTrue(setup.get("ok"), setup)620            for method in methods:621                method = dict(method)622                response = getattr(c, method["fn"])(*method["params"])623                self.assertNotEqual(response.get("status"), 403, method)624    def group_allow_update_works_with_group_update_functions(self):625        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)626        for group in permission_groups:627            # Check default true, allow false628            setup = self.client.update_api_key(629                PERMISSION_TESTING_KEY_ID,630                permissions_document={"version": "1", "default_allow": True, "permissions": {group: {"allow_update": False}}},631            )632            self.assertTrue(setup.get("ok"), setup)633            methods = get_permission_group_functions(group).intersection(get_permission_type_functions("update"))634            for method in methods:635                method = dict(method)636                response = getattr(c, method["fn"])(*method["params"])637                self.assertEqual(response.get("status"), 403, method)638            # Check default false, allow true639            setup = self.client.update_api_key(640                PERMISSION_TESTING_KEY_ID,641                permissions_document={"version": "1", "default_allow": False, "permissions": {group: {"allow_update": True}}},642            )643            self.assertTrue(setup.get("ok"), setup)644            for method in methods:645                method = dict(method)646                response = getattr(c, method["fn"])(*method["params"])647                self.assertNotEqual(response.get("status"), 403, method)648    def group_allow_delete_works_with_group_delete_functions(self):649        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)650        for group in permission_groups:651            # Check default true, allow false652            setup = self.client.update_api_key(653                PERMISSION_TESTING_KEY_ID,654                permissions_document={"version": "1", "default_allow": True, "permissions": {group: {"allow_delete": False}}},655            )656            self.assertTrue(setup.get("ok"), setup)657            methods = get_permission_group_functions(group).intersection(get_permission_type_functions("delete"))658            for method in methods:659                method = dict(method)660                response = getattr(c, method["fn"])(*method["params"])661                self.assertEqual(response.get("status"), 403, method)662            # Check default false, allow true663            setup = self.client.update_api_key(664                PERMISSION_TESTING_KEY_ID,665                permissions_document={"version": "1", "default_allow": False, "permissions": {group: {"allow_delete": True}}},666            )667            self.assertTrue(setup.get("ok"), setup)668            for method in methods:669                method = dict(method)670                response = getattr(c, method["fn"])(*method["params"])671                self.assertNotEqual(response.get("status"), 403, method)672    # Specific permissions673    def test_individual_endpoints_allow_true_works_when_default_false(self):674        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)675        for method in all_client_functions:676            method = dict(method)677            setup = self.client.update_api_key(678                PERMISSION_TESTING_KEY_ID,679                permissions_document={680                    "version": "1",681                    "default_allow": False,682                    "permissions": {method["permission_group"]: {method["permission_name"]: {"allowed": True}}},683                },684            )685            self.assertTrue(setup.get("ok"), setup)686            response = getattr(c, method["fn"])(*method["params"])687            self.assertNotEqual(response.get("status"), 403, method)688    def test_individual_endpoints_allow_false_works_when_default_true(self):689        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)690        for method in all_client_functions:691            method = dict(method)692            setup = self.client.update_api_key(693                PERMISSION_TESTING_KEY_ID,694                permissions_document={695                    "version": "1",696                    "default_allow": True,697                    "permissions": {method["permission_group"]: {method["permission_name"]: {"allowed": False}}},698                },699            )700            self.assertTrue(setup.get("ok"), setup)701            response = getattr(c, method["fn"])(*method["params"])702            self.assertEqual(response.get("status"), 403, method)703    def test_most_specific_permission_takes_priority_when_false(self):704        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)705        # Arbitrarily picked get_transaction for testing this generic behavior706        setup = self.client.update_api_key(707            PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": True, "permissions": {"allow_read": False}}708        )709        self.assertTrue(setup.get("ok"), setup)710        response = c.get_transaction("blah")711        self.assertEqual(response.get("status"), 403, response)712        setup = self.client.update_api_key(713            PERMISSION_TESTING_KEY_ID,714            permissions_document={"version": "1", "default_allow": True, "permissions": {"allow_read": True, "transactions": {"allow_read": False}}},715        )716        self.assertTrue(setup.get("ok"), setup)717        response = c.get_transaction("blah")718        self.assertEqual(response.get("status"), 403, response)719        setup = self.client.update_api_key(720            PERMISSION_TESTING_KEY_ID,721            permissions_document={722                "version": "1",723                "default_allow": True,724                "permissions": {"allow_read": True, "transactions": {"allow_read": True, "get_transaction": {"allowed": False}}},725            },726        )727        self.assertTrue(setup.get("ok"), setup)728        response = c.get_transaction("blah")729        self.assertEqual(response.get("status"), 403, response)730    def test_most_specific_permission_takes_priority_when_true(self):731        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)732        # Arbitrarily picked get_transaction for testing this generic behavior733        setup = self.client.update_api_key(734            PERMISSION_TESTING_KEY_ID, permissions_document={"version": "1", "default_allow": False, "permissions": {"allow_read": True}}735        )736        self.assertTrue(setup.get("ok"), setup)737        response = c.get_transaction("blah")738        self.assertNotEqual(response.get("status"), 403, response)739        setup = self.client.update_api_key(740            PERMISSION_TESTING_KEY_ID,741            permissions_document={"version": "1", "default_allow": False, "permissions": {"allow_read": False, "transactions": {"allow_read": True}}},742        )743        self.assertTrue(setup.get("ok"), setup)744        response = c.get_transaction("blah")745        self.assertNotEqual(response.get("status"), 403, response)746        setup = self.client.update_api_key(747            PERMISSION_TESTING_KEY_ID,748            permissions_document={749                "version": "1",750                "default_allow": False,751                "permissions": {"allow_read": False, "transactions": {"allow_read": False, "get_transaction": {"allowed": True}}},752            },753        )754        self.assertTrue(setup.get("ok"), setup)755        response = c.get_transaction("blah")756        self.assertNotEqual(response.get("status"), 403, response)757    def test_custom_create_transaction_permissions(self):758        c = dragonchain_sdk.create_client(auth_key=PERMISSION_TESTING_KEY, auth_key_id=PERMISSION_TESTING_KEY_ID)759        # Test blacklist permissioning760        setup = self.client.update_api_key(761            PERMISSION_TESTING_KEY_ID,762            permissions_document={763                "version": "1",764                "default_allow": False,765                "permissions": {"transactions": {"create_transaction": {"allowed": True, "transaction_types": {"banana": False}}}},766            },767        )768        self.assertTrue(setup.get("ok"), setup)769        # Check with single not allowed transaction770        response = c.create_transaction("banana", "payload")771        self.assertEqual(response.get("status"), 403, response)772        # Check with single allowed transaction773        response = c.create_transaction("notbanana", "payload")774        self.assertNotEqual(response.get("status"), 403, response)775        # Check bulk with allowed/not allowed mix776        response = c.create_bulk_transaction(777            [{"transaction_type": "banana", "payload": "payload"}, {"transaction_type": "notbanana", "payload": "payload"}]778        )779        self.assertEqual(response.get("status"), 403, response)780        # Check bulk with only not allowed transactions781        response = c.create_bulk_transaction(782            [{"transaction_type": "banana", "payload": "payload"}, {"transaction_type": "banana", "payload": "payload"}]783        )784        self.assertEqual(response.get("status"), 403, response)785        # Check bulk with only allowed transactions786        response = c.create_bulk_transaction(787            [{"transaction_type": "notbanana", "payload": "payload"}, {"transaction_type": "notbanana", "payload": "payload"}]788        )789        self.assertNotEqual(response.get("status"), 403, response)790        # Test whitelist permissioning791        setup = self.client.update_api_key(792            PERMISSION_TESTING_KEY_ID,793            permissions_document={794                "version": "1",795                "default_allow": False,796                "permissions": {"transactions": {"create_transaction": {"allowed": False, "transaction_types": {"banana": True}}}},797            },798        )799        self.assertTrue(setup.get("ok"), setup)800        # Check with single allowed transaction801        response = c.create_transaction("banana", "payload")802        self.assertNotEqual(response.get("status"), 403, response)803        # Check with single not allowed transaction804        response = c.create_transaction("notbanana", "payload")805        self.assertEqual(response.get("status"), 403, response)...test_credential.py
Source:test_credential.py  
...26        assert result27        # rotate both keys28        credential.update_subscription_key("xxx")29        assert credential.subscription_key == "xxx"30        credential.update_api_key("xxx")31        assert credential.api_key == "xxx"32        # call fails33        with pytest.raises(ClientAuthenticationError):34            result = client.get_feedback(feedback_id=self.feedback_id)35        # rotate back to valid credentials36        credential.update_subscription_key(self.subscription_key)37        assert credential.subscription_key == self.subscription_key38        credential.update_api_key(self.api_key)39        assert credential.api_key == self.api_key40        # make successful call41        result = client.get_feedback(feedback_id=self.feedback_id)42        assert result43    def test_credential_rotate_sub_key_only(self):44        credential = MetricsAdvisorKeyCredential(self.subscription_key, self.api_key)45        client = MetricsAdvisorClient(self.service_endpoint, credential)46        # make successful call47        result = client.get_feedback(feedback_id=self.feedback_id)48        assert result49        # rotate one key50        credential.update_subscription_key("xxx")51        assert credential.subscription_key == "xxx"52        assert credential.api_key == self.api_key53        # call fails54        with pytest.raises(ClientAuthenticationError):55            result = client.get_feedback(feedback_id=self.feedback_id)56        # rotate back to valid credentials57        credential.update_subscription_key(self.subscription_key)58        assert credential.subscription_key == self.subscription_key59        assert credential.api_key == self.api_key60        # make successful call61        result = client.get_feedback(feedback_id=self.feedback_id)62        assert result63    def test_credential_rotate_api_key_only(self):64        credential = MetricsAdvisorKeyCredential(self.subscription_key, self.api_key)65        client = MetricsAdvisorClient(self.service_endpoint, credential)66        # make successful call67        result = client.get_feedback(feedback_id=self.feedback_id)68        assert result69        # rotate one key70        credential.update_api_key("xxx")71        assert credential.subscription_key == self.subscription_key72        assert credential.api_key == "xxx"73        # call fails74        with pytest.raises(HttpResponseError):75            result = client.get_feedback(feedback_id=self.feedback_id)76        # rotate back to valid credentials77        credential.update_api_key(self.api_key)78        assert credential.subscription_key == self.subscription_key79        assert credential.api_key == self.api_key80        # make successful call81        result = client.get_feedback(feedback_id=self.feedback_id)82        assert result83    def test_credential_bad_input(self):84        credential = MetricsAdvisorKeyCredential(self.subscription_key, self.api_key)85        with pytest.raises(ValueError):86            credential.update_subscription_key(None)87        with pytest.raises(ValueError):88            credential.update_api_key(None)89        with pytest.raises(TypeError):90            credential.update_subscription_key(subscription_key=34)91        with pytest.raises(TypeError):...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!!
