def test_add_criterion_should_behave_as_add_with_criterion_param(self): my_criteria_with_add = criteria.Criteria()\ .add(criterion=criteria.StringExpressionFactory.field("contentRef").equal_to("theContentRef")) my_criteria_with_add_criterion = criteria.Criteria()\ .add_criterion(criteria.StringExpressionFactory.field("contentRef").equal_to("theContentRef")) assert my_criteria_with_add.get_criteria_params( ) == my_criteria_with_add_criterion.get_criteria_params()
def test_add_inclusion_should_behave_as_add_with_inclusion_param(self): my_criteria_with_add = criteria.Criteria() \ .add(inclusion=criteria.Inclusion.resource("assets").fields("ref", "name")) my_criteria_with_add_inclusion = criteria.Criteria() \ .add_inclusion(criteria.Inclusion.resource("assets").fields("ref", "name")) assert my_criteria_with_add.get_criteria_params( ) == my_criteria_with_add_inclusion.get_criteria_params()
def test_refresh_sequoia_token(self): """ When you perform a query against Sequoia and your token has expired or it's been invalidated, a new token is taken and used to perform your query. """ assets_ids, content_name = self._create_ids_and_content(5) content = content_template % content_name assets = assets_template % assets_ids client = Client(self.registry, grant_client_id=self.config.sequoia.username, grant_client_secret=self.config.sequoia.password) content_endpoint = client.metadata.contents assets_endpoint = client.metadata.assets content_endpoint.store(self.owner, content) assets_endpoint.store(self.owner, assets) sleep(1) response = assets_endpoint.browse( self.owner, criteria.Criteria().add( criterion=criteria.StringExpressionFactory.field( "contentRef").equal_to('testmock:%s' % content_name))) assert_that(response.resources, has_length(5)) # Revoke the token (not using clientsdk) token_previous = client._auth.session.access_token import base64, requests my_auth_creds = base64.b64encode('{}:{}'.format( self.config.sequoia.username, self.config.sequoia.password).encode('ascii')).decode('ascii') url = "https://identity.sandbox.eu-west-1.palettedev.aws.pikselpalette.com/oauth/revoke" payload = 'token=' + token_previous headers = { 'authorization': 'Basic ' + my_auth_creds, 'Content-Type': 'application/x-www-form-urlencoded' } response = requests.request("POST", url, headers=headers, data=payload) assert_that(response.status_code, is_(200)) # Query again sleep(10) response = assets_endpoint.browse( self.owner, criteria.Criteria().add( criterion=criteria.StringExpressionFactory.field( "contentRef").equal_to('testmock:%s' % content_name))) assert_that(response.resources, has_length(5)) token_current = client._auth.session.access_token print('*********') print('TOKEN OLD (revoked): ' + token_previous) print('TOKEN NEW (updated): ' + token_current) print('*********') # assert_that(token_previous, is_not(equal_to(token_current))) # Clean up content_endpoint.delete(self.owner, 'testmock:' + content_name) for name in assets_ids: assets_endpoint.delete(self.owner, 'testmock:' + name)
def test_get_criteria_params_given_simple_criterion_then_param_should_be_created_properly( self): my_criteria = criteria.Criteria()\ .add(criterion=criteria.StringExpressionFactory.field("contentRef").equal_to("theContentRef")) params = my_criteria.get_criteria_params() assert len(params) == 1 assert params["withContentRef"] == "theContentRef"
def test_add_given_inclusion_with_duplicated_field_then_field_should_be_added_only_once( self): my_criteria = criteria.Criteria().add( inclusion=criteria.Inclusion.resource("assets").fields( "ref", "ref")) assert len(my_criteria.inclusion_entries) == 1 assert len(my_criteria._get_inclusion_entries()[0]._get_fields()) == 1 assert my_criteria._get_inclusion_entries( )[0].resource_name == "assets"
def test_get_criteria_params_given_one_inclusion_with_fields_then_inclusion_params_should_be_created( self): my_criteria = criteria.Criteria()\ .add(inclusion=criteria.Inclusion.resource("assets").fields("ref", "name")) params = my_criteria.get_criteria_params() assert len(params) == 2 assert params["include"] == "assets" assert params["assets.fields"] == "name,ref"
def test_get_criteria_params_given_more_than_one_inclusion_then_inclusion_param_should_be_created_properly( self): my_criteria = criteria.Criteria()\ .add(inclusion=criteria.Inclusion.resource("assets"))\ .add(inclusion=criteria.Inclusion.resource("contents")) params = my_criteria.get_criteria_params() assert len(params) == 1 assert params == {"include": "assets,contents"}
def test_retry_when_empty_then_browse_with_content_ref_should_not_retrieve_assets( self): self._configure_logging() unique_name = str(uuid.uuid4()) import backoff client = Client(self.registry, grant_client_id=self.config.sequoia.username, grant_client_secret=self.config.sequoia.password, user_agent='backoff_test') contents_endpoint = client.metadata.contents categories_endpoint = client.metadata.categories content = content_template_unique % (unique_name, unique_name, unique_name) categories = category_template_unique % (unique_name, unique_name) contents_result = contents_endpoint.store(self.owner, content) categories_result = categories_endpoint.store(self.owner, categories) sleep(1) response = contents_endpoint.browse( self.owner, criteria.Criteria().add_criterion( criteria.StringExpressionFactory.field('ref'). equal_to(f'testmock:{unique_name}')).add_inclusion( criteria.Inclusion.resource('categories')).add_inclusion( criteria.Inclusion.resource('assets')), backoff_strategy={ 'wait_gen': backoff.expo, 'max_tries': 3, # 'max_time': 10, 'retry_http_status_codes': '404', 'retry_when_empty_result': { 'contents': True, 'assets': True, 'categories': True } }) from pprint import pformat logging.info(pformat(response.resources)) assert len(response.resources) == 1 assert len(response.linked('categories').resources) == 2 assert len(response.linked('assets').resources) == 0 # This test returns the data it has found after retrying the query as there's no assets and they are required categories_endpoint.delete(self.owner, categories_result.resources[0]['ref']) categories_endpoint.delete(self.owner, categories_result.resources[1]['ref']) contents_endpoint.delete(self.owner, contents_result.resources[0]['ref'])
def test_browse_assets_with_criteria_returns_mocked_assets(self): mocking.add_get_mapping_for(self.mock, 'metadata', 'valid_metadata_assets_response') my_criteria = criteria.Criteria() my_criteria.add(criterion=criteria.StringExpressionFactory.field( 'contentRef').equal_to('theContentRef')) under_test = self.client.metadata.assets response = under_test.browse('testmock', my_criteria) assert_that(response.resources, has_length(4)) assert_that(response.resources[0]['name'], '016b9e5f-c184-48ea-a5e2-6e6bc2d62791')
def test_given_no_assets_then_browse_with_content_ref_should_not_retrieve_assets( self): client = Client(self.registry, grant_client_id=self.config.sequoia.username, grant_client_secret=self.config.sequoia.password) assets_endpoint = client.metadata.assets response = assets_endpoint.browse( self.owner, criteria.Criteria().add( criterion=criteria.StringExpressionFactory.field( 'contentRef').equal_to('testmock:nonExistingContent'))) assert len(response.resources) == 0
def test_retry_when_empty_then_browse_with_content_ref_should_not_retrieve_assets( self): self._configure_logging() unique_name = str(uuid.uuid4()) import backoff client = Client( self.registry, grant_client_id=self.config.sequoia.username, grant_client_secret=self.config.sequoia.password, user_agent='backoff_test', backoff_strategy={ 'wait_gen': backoff.expo, # 'max_tries': 3, 'max_time': 10, 'retry_http_status_codes': '404' }) contents_endpoint = client.metadata.contents categories_endpoint = client.metadata.categories content = content_template_unique % (unique_name, unique_name, unique_name) categories = category_template_unique % (unique_name, unique_name) contents_endpoint.store(self.owner, content) categories_endpoint.store(self.owner, categories) sleep(1) response = contents_endpoint.browse( self.owner, criteria.Criteria().add_criterion( criteria.StringExpressionFactory.field('ref'). equal_to(f'testmock:{unique_name}')).add_inclusion( criteria.Inclusion.resource('categories')).add_inclusion( criteria.Inclusion.resource('assets')), retry_when_empty_result={ 'contents': True, 'assets': True, 'categories': True }) from pprint import pprint pprint(response.resources) assert len(response.resources) == 1 assert len(response.resources[0]['categoryRefs']) == 2
def test_browse_assets_with_paging_and_inclusions_returns_mocked_assets( self): mocking.add_get_mapping_for_url( self.mock, 'data/assets\?withContentRef=theContentRef&perPage=2&include=content&owner=testmock', 'valid_metadata_assets_response_with_include_page_1') mocking.add_get_mapping_for_url( self.mock, 'data/assets\?owner=testmock&withContentRef=testmock&page=2&perPage=2&include=content', 'valid_metadata_assets_response_with_include_page_2') under_test = self.client.metadata.assets inclusion_contents = criteria.Criteria().add( inclusion=criteria.Inclusion.resource('content')) response_list = [ response for response in under_test.browse( 'testmock', query_string='withContentRef=theContentRef&perPage=2', criteria=inclusion_contents) ] assert_that(response_list, has_length(2)) assert_that(response_list[0].resources, has_length(2)) assert_that(response_list[1].resources, has_length(2)) assert_that(response_list[0].resources[0]['name'], is_('016b9e5f-c184-48ea-a5e2-6e6bc2d62791')) assert_that(response_list[0].resources[1]['name'], is_('192e78ad-25d1-47f8-b539-19053a2b4a6f')) assert_that(response_list[1].resources[0]['name'], is_('3bf33965-41fe-4f94-8aa9-63b6b8a379da')) assert_that(response_list[1].resources[1]['name'], is_('44c6170a-2c03-42ce-bfa3-101fec955188')) expected_requests = \ [{'path': '/services/testmock', 'query': ''}, {'path': '/oauth/token', 'query': ''}, {'path': '/data/assets', 'query': 'withcontentref=thecontentref&perpage=2&include=content&owner=testmock'}, {'path': '/data/assets', 'query': 'owner=testmock&withcontentref=testmock&page=2&perpage=2&include=content'}] performed_requests = [{ 'path': r.path, 'query': r.query } for r in self.mock.request_history] assert_that(performed_requests, is_(expected_requests))
def test_browse_assets_with_query_string_and_criteria_returns_mocked_assets( self): mocking.add_get_mapping_for_url( self.mock, 'data/assets\?withContentRef=theContentRef&owner=testmock&withType=type', 'valid_metadata_assets_response') under_test = self.client.metadata.assets a_criteria = criteria.Criteria().add( criterion=criteria.StringExpressionFactory.field('type').equal_to( 'type')) response = under_test.browse( 'testmock', criteria=a_criteria, query_string='withContentRef=theContentRef') assert_that(response.resources, has_length(4)) assert_that(response.resources[0]['name'], '016b9e5f-c184-48ea-a5e2-6e6bc2d62791')
def test_can_post_and_retrieve_linked_content(self): client = Client(self.registry, grant_client_id=self.config.sequoia.username, grant_client_secret=self.config.sequoia.password, model_resolution='direct') contents_endpoint = client.metadata.contents categories_endpoint = client.metadata.categories store_content = None store_category = None try: store_content = contents_endpoint.store( self.owner, content_with_category_template) store_category = categories_endpoint.store(self.owner, category_template) sleep(1) result = contents_endpoint.browse( self.owner, criteria.Criteria().add( inclusion=criteria.Inclusion.resource('categories')).add( criterion=criteria.StringExpressionFactory.field('ref') .equal_to(store_content.resources[0]['ref']))) finally: if store_content: delete_result = contents_endpoint.delete( self.owner, store_content.resources[0]['ref']) if store_category: delete_result = categories_endpoint.delete( self.owner, store_category.resources[0]['ref']) assert_that(result.model, has_length(1)) assert_that(result.status, 201) assert_that(result.model[0]['name'], 'a_test_content_linked_to_categories') assert_that(result.model[0]['categories'], has_length(1)) assert_that(result.model[0]['categories'][0]['ref'], 'testmock:genre_animation')
def test_given_assets_then_browse_with_content_ref_should_retrieve_assets( self): assets_ids, content_name = self._create_ids_and_content(5) content = content_template % content_name assets = assets_template % assets_ids client = Client(self.registry, grant_client_id=self.config.sequoia.username, grant_client_secret=self.config.sequoia.password) content_endpoint = client.metadata.contents assets_endpoint = client.metadata.assets content_endpoint.store(self.owner, content) assets_endpoint.store(self.owner, assets) sleep(1) response = assets_endpoint.browse( self.owner, criteria.Criteria().add( criterion=criteria.StringExpressionFactory.field( "contentRef").equal_to('testmock:%s' % content_name))) assert_that(response.resources, has_length(5)) # Clean up content_endpoint.delete(self.owner, 'testmock:' + content_name) for name in assets_ids: assets_endpoint.delete(self.owner, 'testmock:' + name)