How to use _get_cors_result_header_snapshot_transformer method in localstack

Best Python code snippet using localstack_python

test_s3.py

Source:test_s3.py Github

copy

Full Screen

...1992 ]1993 )1994 def test_cors_with_allowed_origins(self, s3_client, s3_create_bucket, snapshot, monkeypatch):1995 monkeypatch.setattr(config, "DISABLE_CUSTOM_CORS_S3", False)1996 snapshot.add_transformer(self._get_cors_result_header_snapshot_transformer(snapshot))1997 bucket_cors_config = {1998 "CORSRules": [1999 {2000 "AllowedOrigins": ["https://localhost:4200"],2001 "AllowedMethods": ["GET", "PUT"],2002 "MaxAgeSeconds": 3000,2003 "AllowedHeaders": ["*"],2004 }2005 ]2006 }2007 bucket_name = f"bucket-{short_uid()}"2008 object_key = "424f6bae-c48f-42d8-9e25-52046aecc64d/document.pdf"2009 s3_create_bucket(Bucket=bucket_name)2010 s3_client.put_bucket_cors(Bucket=bucket_name, CORSConfiguration=bucket_cors_config)2011 # create signed url2012 url = s3_client.generate_presigned_url(2013 ClientMethod="put_object",2014 Params={2015 "Bucket": bucket_name,2016 "Key": object_key,2017 "ContentType": "application/pdf",2018 "ACL": "bucket-owner-full-control",2019 },2020 ExpiresIn=3600,2021 )2022 result = requests.put(2023 url,2024 data="something",2025 verify=False,2026 headers={2027 "Origin": "https://localhost:4200",2028 "Content-Type": "application/pdf",2029 },2030 )2031 assert result.status_code == 2002032 # result.headers is type CaseInsensitiveDict and needs to be converted first2033 snapshot.match("raw-response-headers", dict(result.headers))2034 bucket_cors_config = {2035 "CORSRules": [2036 {2037 "AllowedOrigins": [2038 "https://localhost:4200",2039 "https://localhost:4201",2040 ],2041 "AllowedMethods": ["GET", "PUT"],2042 "MaxAgeSeconds": 3000,2043 "AllowedHeaders": ["*"],2044 }2045 ]2046 }2047 s3_client.put_bucket_cors(Bucket=bucket_name, CORSConfiguration=bucket_cors_config)2048 # create signed url2049 url = s3_client.generate_presigned_url(2050 ClientMethod="put_object",2051 Params={2052 "Bucket": bucket_name,2053 "Key": object_key,2054 "ContentType": "application/pdf",2055 "ACL": "bucket-owner-full-control",2056 },2057 ExpiresIn=3600,2058 )2059 # mimic chrome behavior, sending OPTIONS request first for strict-origin-when-cross-origin2060 result = requests.options(2061 url,2062 headers={2063 "Origin": "https://localhost:4200",2064 "Access-Control-Request-Method": "PUT",2065 },2066 )2067 snapshot.match("raw-response-headers-2", dict(result.headers))2068 result = requests.put(2069 url,2070 data="something",2071 verify=False,2072 headers={2073 "Origin": "https://localhost:4200",2074 "Content-Type": "application/pdf",2075 },2076 )2077 assert result.status_code == 2002078 snapshot.match("raw-response-headers-3", dict(result.headers))2079 result = requests.put(2080 url,2081 data="something",2082 verify=False,2083 headers={2084 "Origin": "https://localhost:4201",2085 "Content-Type": "application/pdf",2086 },2087 )2088 assert result.status_code == 2002089 snapshot.match("raw-response-headers-4", dict(result.headers))2090 @pytest.mark.aws_validated2091 @pytest.mark.skip_snapshot_verify(2092 paths=[2093 "$..Access-Control-Allow-Headers",2094 "$..Connection",2095 "$..Location",2096 "$..Vary",2097 "$..Content-Type",2098 "$..x-amzn-requestid",2099 "$..last-modified",2100 "$..accept-ranges",2101 "$..content-language",2102 "$..content-md5",2103 "$..content-type",2104 "$..x-amz-version-id",2105 "$..Last-Modified",2106 "$..Accept-Ranges",2107 "$..raw-response-headers-2.Access-Control-Allow-Credentials",2108 ]2109 )2110 def test_cors_configurations(self, s3_client, s3_create_bucket, monkeypatch, snapshot):2111 monkeypatch.setattr(config, "DISABLE_CUSTOM_CORS_S3", False)2112 snapshot.add_transformer(self._get_cors_result_header_snapshot_transformer(snapshot))2113 bucket = f"test-cors-{short_uid()}"2114 object_key = "index.html"2115 url = "{}/{}".format(_bucket_url(bucket), object_key)2116 BUCKET_CORS_CONFIG = {2117 "CORSRules": [2118 {2119 "AllowedOrigins": [config.get_edge_url()],2120 "AllowedMethods": ["GET", "PUT"],2121 "MaxAgeSeconds": 3000,2122 "AllowedHeaders": ["x-amz-tagging"],2123 }2124 ]2125 }2126 s3_create_bucket(Bucket=bucket, ACL="public-read")2127 s3_client.put_bucket_cors(Bucket=bucket, CORSConfiguration=BUCKET_CORS_CONFIG)2128 s3_client.put_object(2129 Bucket=bucket, Key=object_key, Body="<h1>Index</html>", ACL="public-read"2130 )2131 response = requests.get(2132 url, headers={"Origin": config.get_edge_url(), "Content-Type": "text/html"}2133 )2134 assert 200 == response.status_code2135 snapshot.match("raw-response-headers", dict(response.headers))2136 BUCKET_CORS_CONFIG = {2137 "CORSRules": [2138 {2139 "AllowedOrigins": ["https://anydomain.com"],2140 "AllowedMethods": ["GET", "PUT"],2141 "MaxAgeSeconds": 3000,2142 "AllowedHeaders": ["x-amz-tagging"],2143 }2144 ]2145 }2146 s3_client.put_bucket_cors(Bucket=bucket, CORSConfiguration=BUCKET_CORS_CONFIG)2147 response = requests.get(2148 url, headers={"Origin": config.get_edge_url(), "Content-Type": "text/html"}2149 )2150 assert 200 == response.status_code2151 snapshot.match("raw-response-headers-2", dict(response.headers))2152 def _get_cors_result_header_snapshot_transformer(self, snapshot):2153 return [2154 snapshot.transform.key_value("x-amz-id-2", "<id>", reference_replacement=False),2155 snapshot.transform.key_value(2156 "x-amz-request-id", "<request-id>", reference_replacement=False2157 ),2158 snapshot.transform.key_value("Date", "<date>", reference_replacement=False),2159 snapshot.transform.key_value("Server", "<server>", reference_replacement=False),2160 snapshot.transform.key_value("Last-Modified", "<date>", reference_replacement=False),2161 ]2162 @pytest.mark.parametrize(2163 "signature_version, use_virtual_address",2164 [2165 ("s3", False),2166 ("s3", True),...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful