def generate_sas_token(self): fake_key = 'a' * 30 + 'b' * 30 return '?' + generate_account_sas( account_name='test', # name of the storage account account_key=fake_key, # key for the storage account resource_types=ResourceTypes(object=True), permission=AccountSasPermissions(read=True, list=True), start=datetime.now() - timedelta(hours=24), expiry=datetime.now() + timedelta(days=8))
async def test_account_sas(self, resource_group, location, storage_account, storage_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange account_url = self.account_url(storage_account, "table") if 'cosmos' in account_url: pytest.skip("Cosmos Tables does not yet support sas") tsc = self.create_client_from_credential(TableServiceClient, storage_account_key, account_url=account_url) table = await self._create_table(tsc) try: entity = { 'PartitionKey': 'test', 'RowKey': 'test1', 'text': 'hello', } await table.upsert_entity(entity=entity) entity['RowKey'] = 'test2' await table.upsert_entity(entity=entity) token = generate_account_sas( storage_account.name, storage_account_key, resource_types=ResourceTypes(object=True), permission=AccountSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) account_url = self.account_url(storage_account, "table") service = self.create_client_from_credential( TableServiceClient, token, account_url=account_url) # Act # service = TableServiceClient( # self.account_url(storage_account, "table"), # credential=token, # ) sas_table = service.get_table_client(table.table_name) entities = [] async for e in sas_table.list_entities(): entities.append(e) # Assert assert len(entities) == 2 assert entities[0].text.value == u'hello' assert entities[1].text.value == u'hello' finally: await self._delete_table(table=table, ts=tsc)
def test_account_sas(self, tables_storage_account_name, tables_primary_storage_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange account_url = self.account_url(tables_storage_account_name, "table") tsc = self.create_client_from_credential( TableServiceClient, tables_primary_storage_account_key, account_url=account_url) table = self._create_table(tsc) try: entity = { 'PartitionKey': u'test', 'RowKey': u'test1', 'text': u'hello', } table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) entity['RowKey'] = u'test2' table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) token = generate_account_sas( tables_storage_account_name, tables_primary_storage_account_key, resource_types=ResourceTypes(object=True), permission=AccountSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) account_url = self.account_url(tables_storage_account_name, "table") service = self.create_client_from_credential( TableServiceClient, token, account_url=account_url) # Act # service = TableServiceClient( # self.account_url(tables_storage_account_name, "table"), # credential=token, # ) sas_table = service.get_table_client(table.table_name) entities = list(sas_table.list_entities()) # Assert assert len(entities) == 2 assert entities[0].text == u'hello' assert entities[1].text == u'hello' finally: self._delete_table(table=table, ts=tsc)
async def test_account_sas(self, resource_group, location, cosmos_account, cosmos_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange url = self.account_url(cosmos_account, "cosmos") if 'cosmos' in url: pytest.skip("Cosmos Tables does not yet support sas") tsc = TableServiceClient(url, cosmos_account_key) table = await self._create_table(tsc) try: entity = { 'PartitionKey': 'test', 'RowKey': 'test1', 'text': 'hello', } await table.upsert_insert_merge_entity( table_entity_properties=entity) entity['RowKey'] = 'test2' await table.upsert_insert_merge_entity( table_entity_properties=entity) token = generate_account_sas( cosmos_account.name, cosmos_account_key, resource_types=ResourceTypes(container=True), permission=AccountSasPermissions(list=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) # Act service = TableServiceClient( self.account_url(cosmos_account, "cosmos"), credential=token, ) entities = [] async for e in service.list_tables(): entities.append(e) # Assert self.assertEqual(len(entities), 1) # self.assertEqual(entities[0].text, 'hello') # self.assertEqual(entities[1].text, 'hello') finally: await self._delete_table(table=table, ts=tsc) if self.is_live: sleep(SLEEP_DELAY)
async def test_account_sas(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange url = self.account_url(tables_cosmos_account_name, "cosmos") tsc = TableServiceClient(url, tables_primary_cosmos_account_key) table = await self._create_table(tsc) try: entity = { 'PartitionKey': 'test', 'RowKey': 'test1', 'text': 'hello', } await table.upsert_insert_merge_entity( table_entity_properties=entity) entity['RowKey'] = 'test2' await table.upsert_insert_merge_entity( table_entity_properties=entity) token = generate_account_sas( tables_cosmos_account_name, tables_primary_cosmos_account_key, resource_types=ResourceTypes(container=True), permission=AccountSasPermissions(list=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) # Act service = TableServiceClient( self.account_url(tables_cosmos_account_name, "cosmos"), credential=token, ) entities = [] async for e in service.list_tables(): entities.append(e) # Assert assert len(entities) == 1 finally: await self._delete_table(table=table, ts=tsc) if self.is_live: sleep(SLEEP_DELAY)
def test_account_sas(self, resource_group, location, storage_account, storage_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange url = self.account_url(storage_account, "table") if 'cosmos' in url: pytest.skip("Cosmos Tables does not yet support sas") tsc = TableServiceClient(url, storage_account_key) table = self._create_table(tsc) try: entity = { 'PartitionKey': 'test', 'RowKey': 'test1', 'text': 'hello', } table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) entity['RowKey'] = 'test2' table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) token = generate_account_sas( storage_account.name, storage_account_key, resource_types=ResourceTypes(object=True), permission=AccountSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) # Act service = TableServiceClient( self.account_url(storage_account, "table"), credential=token, ) sas_table = service.get_table_client(table.table_name) entities = list(sas_table.list_entities()) # Assert self.assertEqual(len(entities), 2) self.assertEqual(entities[0].text, 'hello') self.assertEqual(entities[1].text, 'hello') finally: self._delete_table(table=table, ts=tsc)