How to use set_cors method in localstack

Best Python code snippet using localstack_python

buckets.py

Source:buckets.py Github

copy

Full Screen

...16 data = request.get_json()17 json_schema_validator(data, schema=schemas['create_bucket'])18 ceph = CephManager.from_session(session=session)19 bucket = ceph.create_buckets(bucket_name=data['name'])20 set_cors(data['name'])21 return make_response(jsonify(bucket), 200)22# @buckets.route('/buckets/<bucket_name>/cors', methods=['GET'], strict_slashes=False)23# def get_bucket_cors(bucket_name):24# ceph = CephManager.from_session(session=session)25# bucket = ceph.get_bucket(bucket_name=bucket_name)26# return make_response(jsonify(bucket), 200)27#28#29# @buckets.route('/buckets/<bucket_name>/cors', methods=['PUT'], strict_slashes=False)30# def set_bucket_cors(bucket_name):31# ceph = CephManager.from_session(session=session)32# bucket = ceph.get_bucket(bucket_name=bucket_name)33# return make_response(jsonify(bucket), 200)34@buckets.route('/buckets/<bucket_name>', methods=['DELETE'], strict_slashes=False)35def delete_bucket(bucket_name):36 ceph = CephManager.from_session(session=session)37 ceph.delete_bucket(bucket_name)38 resp = make_response(jsonify(dict(msg=f"Bucket {bucket_name} deleted.")), 200)39 return resp40@buckets.route('/buckets/<bucket_name>', methods=['GET'], strict_slashes=False)41def get_bucket(bucket_name):42 ceph = CephManager.from_session(session=session)43 objects = ceph.list_object(bucket_name)44 if 'Contents' in objects:45 size = sum(obj['Size'] for obj in objects['Contents'])46 length = len(objects['Contents'])47 else:48 size = 049 length = 050 response_data = dict(Name=bucket_name, Size=size, Length=length)51 set_cors(bucket_name)52 return make_response(jsonify(response_data), 200)53def set_cors(bucket_name):54 ceph = CephManager.from_session(session=session)55 if 'HTTP_HOST' in request.environ: # Only if the client is a browser this is needed56 ceph.set_bucket_cors(bucket_name=bucket_name, origins=['http://' + request.environ['HTTP_HOST'],57 'https://' + request.environ['HTTP_HOST']])58@buckets.route('/buckets/<bucket_name>/objects', methods=['GET'], strict_slashes=False)59def get_bucket_objects(bucket_name):60 ceph = CephManager.from_session(session=session)61 objects = ceph.list_object(bucket_name)...

Full Screen

Full Screen

test_cors.py

Source:test_cors.py Github

copy

Full Screen

...3from corsheaders.defaults import default_headers4from django_forest.utils.cors import set_cors5class UtilsCorsTests(TestCase):6 @override_settings(MIDDLEWARE=[], INSTALLED_APPS=[])7 def test_set_cors(self):8 set_cors()9 self.assertTrue('corsheaders' in settings.INSTALLED_APPS)10 self.assertTrue('corsheaders.middleware.CorsMiddleware' in settings.MIDDLEWARE)11 self.assertEqual(settings.MIDDLEWARE.index('django_forest.utils.cors.PnaMiddleware'), 0)12 self.assertEqual(settings.MIDDLEWARE.index('corsheaders.middleware.CorsMiddleware'), 1)13 self.assertEqual(settings.CORS_ALLOWED_ORIGIN_REGEXES, ['.*\\.forestadmin\\.com.*'])14 self.assertEqual(settings.CORS_ALLOW_HEADERS, list(default_headers) + ['forest-context-url'])15 self.assertEqual(settings.CORS_ALLOW_CREDENTIALS, True)16 @override_settings(INSTALLED_APPS=['corsheaders'])17 def test_set_cors_apps_already_present(self):18 set_cors()19 self.assertEqual(settings.INSTALLED_APPS, ['corsheaders'])20 @override_settings(MIDDLEWARE=['fake', 'django.middleware.X', 'corsheaders.middleware.CorsMiddleware'])21 def test_set_cors_middleware_already_present(self):22 set_cors()23 self.assertEqual(settings.MIDDLEWARE, ['fake', 'django.middleware.X', 'django_forest.utils.cors.PnaMiddleware', 'corsheaders.middleware.CorsMiddleware'])24 25 @override_settings(MIDDLEWARE=['fake', 'django.middleware.common.CommonMiddleware', 'corsheaders.middleware.CorsMiddleware'])26 def test_set_cors_with_common_middleware(self):27 self.assertEqual(settings.MIDDLEWARE, ['fake', 'django.middleware.common.CommonMiddleware', 'corsheaders.middleware.CorsMiddleware'])28 @override_settings(MIDDLEWARE=['fakemiddleware', 'django.middleware.common.CommonMiddleware', 'fakemiidleware2'])29 def test_set_cors_common_middleware(self):30 set_cors()31 self.assertTrue('corsheaders.middleware.CorsMiddleware' in settings.MIDDLEWARE)32 self.assertEqual(settings.MIDDLEWARE, ['fakemiddleware', 'django_forest.utils.cors.PnaMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', 'fakemiidleware2'])33 @override_settings(CORS_ALLOWED_ORIGIN_REGEXES=[r'/\.foo\.com$/',r'/\.bar\.com$/'])34 def test_extends_allowed_origin_regexes(self):35 set_cors()36 self.assertEqual(settings.CORS_ALLOWED_ORIGIN_REGEXES, ['.*\\.forestadmin\\.com.*', '/\\.foo\\.com$/', '/\\.bar\\.com$/'])37 38 @override_settings(CORS_ALLOW_HEADERS=['fake', 'fake2'])39 def test_extends_allowed_headers(self):40 set_cors()41 self.assertEqual(settings.CORS_ALLOW_HEADERS, ['fake', 'fake2', 'forest-context-url'])...

Full Screen

Full Screen

s3_config.py

Source:s3_config.py Github

copy

Full Screen

...4AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')5AWS_S3_BUCKET_NAME = os.environ.get('AWS_S3_BUCKET_NAME')6s3 = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)7bucket = s3.get_bucket(AWS_S3_BUCKET_NAME)8def set_cors():9 cors = boto.s3.cors.CORSConfiguration()10 cors.add_rule(allowed_method=['GET', 'PUT', 'POST', 'HEAD', 'DELETE'],11 allowed_origin='*', allowed_header='*',12 max_age_seconds=3000, expose_header='x-amz-server-side-encryption')13 bucket.set_cors(cors)14def get_cors():15 return bucket.get_cors_xml()16def main():17 return set_cors()18if __name__ == "__main__":...

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