Best Python code snippet using localstack_python
test_cli.py
Source:test_cli.py  
...37        assert result.exit_code == 038        assert container_client.is_container_running(39            config.MAIN_CONTAINER_NAME40        ), "container name was not running after wait"41        health = requests.get(get_edge_url() + "/health")42        assert health.ok, "health request did not return OK: %s" % health.text43        result = runner.invoke(cli, ["stop"])44        assert result.exit_code == 045        with pytest.raises(requests.ConnectionError):46            requests.get(get_edge_url() + "/health")47    def test_wait_timeout_raises_exception(self, runner, container_client):48        result = runner.invoke(cli, ["start", "-d"])49        assert result.exit_code == 050        assert "starting LocalStack" in result.output51        result = runner.invoke(cli, ["wait", "-t", "0.5"])52        # one day this test will surely fail ;-)53        assert result.exit_code != 054    def test_logs(self, runner, container_client):55        result = runner.invoke(cli, ["logs"])56        assert result.exit_code != 057        runner.invoke(cli, ["start", "-d"])58        runner.invoke(cli, ["wait", "-t", "60"])59        result = runner.invoke(cli, ["logs"])60        assert constants.READY_MARKER_OUTPUT in result.output...test_security.py
Source:test_security.py  
...5class TestCSRF(unittest.TestCase):6    def test_CSRF(self):7        headers = {"Origin": "http://attacker.com"}8        # Test if lambdas are enumerable9        response = requests.get(f"{config.get_edge_url()}/2015-03-31/functions/", headers=headers)10        self.assertEqual(403, response.status_code)11        # Test if config endpoint is reachable12        config_body = {"variable": "harmful", "value": "config"}13        response = requests.post(14            f"{config.get_edge_url()}/?_config_", headers=headers, json=config_body15        )16        self.assertEqual(403, response.status_code)17        # Test if endpoints are reachable without origin header18        response = requests.get(f"{config.get_edge_url()}/2015-03-31/functions/")19        self.assertEqual(200, response.status_code)20        self.assertEqual("*", response.headers["access-control-allow-origin"])21    def test_default_cors_headers(self):22        headers = {"Origin": "https://app.localstack.cloud"}23        response = requests.get(f"{config.get_edge_url()}/2015-03-31/functions/", headers=headers)24        self.assertEqual(200, response.status_code)25        self.assertEqual(26            "https://app.localstack.cloud",27            response.headers["access-control-allow-origin"],28        )29        self.assertIn("GET", response.headers["access-control-allow-methods"].split(","))30    def test_cors_s3_override(self):31        client = aws_stack.connect_to_service("s3")32        BUCKET_CORS_CONFIG = {33            "CORSRules": [34                {35                    "AllowedOrigins": ["https://localhost:4200"],36                    "AllowedMethods": ["GET", "PUT"],37                    "MaxAgeSeconds": 3000,38                    "AllowedHeaders": ["*"],39                }40            ]41        }42        bucket_name = "my-s3-bucket"43        try:44            client.create_bucket(Bucket=bucket_name)45            client.put_bucket_cors(Bucket=bucket_name, CORSConfiguration=BUCKET_CORS_CONFIG)46            # create signed url47            url = client.generate_presigned_url(48                ClientMethod="put_object",49                Params={50                    "Bucket": bucket_name,51                    "Key": "424f6bae-c48f-42d8-9e25-52046aecc64d/document.pdf",52                    "ContentType": "application/pdf",53                    "ACL": "bucket-owner-full-control",54                },55                ExpiresIn=3600,56            )57            old_config = config.DISABLE_CUSTOM_CORS_S358            config.DISABLE_CUSTOM_CORS_S3 = True59            result = requests.put(60                url,61                data="something",62                verify=False,63                headers={64                    "Origin": "https://localhost:4200",65                    "Content-Type": "application/pdf",66                },67            )68            self.assertEqual(403, result.status_code)69        finally:70            # cleanup71            config.DISABLE_CUSTOM_CORS_S3 = old_config72            client.delete_object(73                Bucket=bucket_name,74                Key="424f6bae-c48f-42d8-9e25-52046aecc64d/document.pdf",75            )76            client.delete_bucket(Bucket=bucket_name)77    def test_cors_disable(self):78        old_config = config.DISABLE_CORS_CHECKS79        try:80            headers = {"Origin": "https://invalid.localstack.cloud"}81            response = requests.get(82                f"{config.get_edge_url()}/2015-03-31/functions/", headers=headers83            )84            self.assertEqual(403, response.status_code)85            config.DISABLE_CORS_CHECKS = True86            response = requests.get(87                f"{config.get_edge_url()}/2015-03-31/functions/", headers=headers88            )89            self.assertEqual(200, response.status_code)90            self.assertEqual(headers["Origin"], response.headers["access-control-allow-origin"])91            self.assertIn("GET", response.headers["access-control-allow-methods"].split(","))92        finally:93            # cleanup...url.py
Source:url.py  
...13@ClientV1.get_url.register14def get_node_url(client: ClientV1, obj: NodeV1) -> str:15    return client.node_endpoint16@ClientV1.get_url.register17def get_edge_url(client: ClientV1, obj: EdgeV1) -> str:18    return client.edge_endpoint19@ClientV1.get_url.register20def get_edge_url(client: ClientV1, obj: NodeLabels) -> str:21    return client.node_endpoint22@ClientV1.get_url.register23def get_edge_url(client: ClientV1, obj: EdgeLabels) -> str:...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!!
