Esempio n. 1
0
    def test_set_cors(self):
        # Arrange
        cors_rule1 = CorsRule(['www.xyz.com'], ['GET'])

        allowed_origins = ['www.xyz.com', "www.ab.com", "www.bc.com"]
        allowed_methods = ['GET', 'PUT']
        max_age_in_seconds = 500
        exposed_headers = [
            "x-ms-meta-data*", "x-ms-meta-source*", "x-ms-meta-abc",
            "x-ms-meta-bcd"
        ]
        allowed_headers = [
            "x-ms-meta-data*", "x-ms-meta-target*", "x-ms-meta-xyz",
            "x-ms-meta-foo"
        ]
        cors_rule2 = CorsRule(allowed_origins,
                              allowed_methods,
                              max_age_in_seconds=max_age_in_seconds,
                              exposed_headers=exposed_headers,
                              allowed_headers=allowed_headers)

        cors = [cors_rule1, cors_rule2]

        # Act
        self.fsc.set_service_properties(cors=cors)

        # Assert
        received_props = self.fsc.get_service_properties()
        self._assert_cors_equal(received_props['cors'], cors)
Esempio n. 2
0
    async def test_set_cors_async(self, resource_group, location,
                                  storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        cors_rule1 = CorsRule(['www.xyz.com'], ['GET'])

        allowed_origins = ['www.xyz.com', "www.ab.com", "www.bc.com"]
        allowed_methods = ['GET', 'PUT']
        max_age_in_seconds = 500
        exposed_headers = [
            "x-ms-meta-data*", "x-ms-meta-source*", "x-ms-meta-abc",
            "x-ms-meta-bcd"
        ]
        allowed_headers = [
            "x-ms-meta-data*", "x-ms-meta-target*", "x-ms-meta-xyz",
            "x-ms-meta-foo"
        ]
        cors_rule2 = CorsRule(allowed_origins,
                              allowed_methods,
                              max_age_in_seconds=max_age_in_seconds,
                              exposed_headers=exposed_headers,
                              allowed_headers=allowed_headers)

        cors = [cors_rule1, cors_rule2]

        # Act
        await self.fsc.set_service_properties(cors=cors)

        # Assert
        received_props = await self.fsc.get_service_properties()
        self._assert_cors_equal(received_props['cors'], cors)
Esempio n. 3
0
    async def file_service_properties_async(self):
        # Instantiate the ShareServiceClient from a connection string
        from azure.storage.fileshare.aio import ShareServiceClient
        file_service = ShareServiceClient.from_connection_string(
            self.connection_string)

        # [START set_service_properties]
        # Create service properties
        from azure.storage.fileshare import Metrics, CorsRule, RetentionPolicy

        # Create metrics for requests statistics
        hour_metrics = Metrics(enabled=True,
                               include_apis=True,
                               retention_policy=RetentionPolicy(enabled=True,
                                                                days=5))
        minute_metrics = Metrics(enabled=True,
                                 include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True,
                                                                  days=5))

        # Create CORS rules
        cors_rule1 = CorsRule(['www.xyz.com'], ['GET'])
        allowed_origins = ['www.xyz.com', "www.ab.com", "www.bc.com"]
        allowed_methods = ['GET', 'PUT']
        max_age_in_seconds = 500
        exposed_headers = [
            "x-ms-meta-data*", "x-ms-meta-source*", "x-ms-meta-abc",
            "x-ms-meta-bcd"
        ]
        allowed_headers = [
            "x-ms-meta-data*", "x-ms-meta-target*", "x-ms-meta-xyz",
            "x-ms-meta-foo"
        ]
        cors_rule2 = CorsRule(allowed_origins,
                              allowed_methods,
                              max_age_in_seconds=max_age_in_seconds,
                              exposed_headers=exposed_headers,
                              allowed_headers=allowed_headers)

        cors = [cors_rule1, cors_rule2]

        async with file_service:
            # Set the service properties
            await file_service.set_service_properties(hour_metrics,
                                                      minute_metrics, cors)
            # [END set_service_properties]

            # [START get_service_properties]
            properties = await file_service.get_service_properties()
Esempio n. 4
0
    def test_too_many_cors_rules(self):
        # Arrange
        cors = []
        for i in range(0, 6):
            cors.append(CorsRule(['www.xyz.com'], ['GET']))

        # Assert
        self.assertRaises(HttpResponseError, self.fsc.set_service_properties,
                          None, None, cors)
    async def test_too_many_cors_rules_async(self, storage_account_name, storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        cors = []
        for i in range(0, 6):
            cors.append(CorsRule(['www.xyz.com'], ['GET']))

        # Assert
        with self.assertRaises(HttpResponseError):
            await self.fsc.set_service_properties(None, None, cors)
Esempio n. 6
0
    def test_too_many_cors_rules(self, resource_group, location,
                                 storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        cors = []
        for i in range(0, 6):
            cors.append(CorsRule(['www.xyz.com'], ['GET']))

        # Assert
        self.assertRaises(HttpResponseError, self.fsc.set_service_properties,
                          None, None, cors)
    def set_cors_rules(self, service):
        print('1. Get Cors Rules')
        original_cors_rules = service.get_service_properties()['cors']

        print('2. Overwrite Cors Rules')
        cors_rule = CorsRule(allowed_origins=['*'],
                             allowed_methods=['POST', 'GET'],
                             allowed_headers=['*'],
                             exposed_headers=['*'],
                             max_age_in_seconds=3600)

        try:
            service.set_service_properties(cors=[cors_rule])
        except Exception as e:
            print(e)
        finally:
            #reverting cors rules back to the original ones
            print('3. Revert Cors Rules back the original ones')
            service.set_service_properties(cors=original_cors_rules)

        print("CORS sample completed")