def relationship_with_10_records(org): lm = factories.LogicModule(name='Products Service', endpoint_name='products') lmm = factories.LogicModuleModel( logic_module_endpoint_name=lm.endpoint_name, model='Product', endpoint='/products/', lookup_field_name='uuid') lm_document = factories.LogicModule(name='Document Service', endpoint_name='documents') lmm_document = factories.LogicModuleModel( logic_module_endpoint_name=lm_document.endpoint_name, model='Document', endpoint='/documents/', lookup_field_name='uuid') relationship = factories.Relationship(origin_model=lmm, related_model=lmm_document, key='product_document_relationship') for _ in range(10): factories.JoinRecord.create(relationship=relationship, record_uuid=uuid.uuid4(), record_id=None, related_record_uuid=uuid.uuid4(), related_record_id=None, organization=org) return relationship
def relationship(): lm = factories.LogicModule(name='Products Service', endpoint_name='products') lmm = factories.LogicModuleModel(logic_module_endpoint_name=lm.endpoint_name, model='Product', endpoint='/products/') lm_document = factories.LogicModule(name='Document Service', endpoint_name='documents') lmm_document = factories.LogicModuleModel(logic_module_endpoint_name=lm_document.endpoint_name, model='Document', endpoint='/documents/') return factories.Relationship(origin_model=lmm, related_model=lmm_document, key='product_document_relationship')
def setUp(self): self.client = APIClient() self.core_user = factories.CoreUser() self.lm = factories.LogicModule(name='documents') # bypass authentication self.client.force_authenticate(user=self.core_user)
def test_load_swagger_resource_aggregate_raises_url_error( self, mock_perform_request, mock_utils, mock_app): # create extra service crm_lm = factories.LogicModule( name='Contacts and Appointments Service', endpoint='http://crm.example.com', endpoint_name='crm', relationships={}) # mock schema urls and app product_schema_url = {self.lm.endpoint_name: self.lm.endpoint} crm_schema_url = {'crm': crm_lm.endpoint} mock_utils.get_swagger_urls.side_effect = [ product_schema_url, crm_schema_url ] msg = f'Make sure that {crm_lm.endpoint} is accessible.' exception_obj = error.URLError(msg) mock_app.side_effect = exception_obj # mock service response service_response = Mock(PySwaggerResponse) service_response.status = 200 service_response.data = self.response_data mock_perform_request.return_value = service_response # make api request and validate error path = '/old/{}/{}/'.format(self.lm.endpoint_name, 'products') with self.assertRaises(error.URLError) as context: self.client.get(path, {'aggregate': 'true'}) self.assertTrue(msg in context.exception)
def relationship_with_local(): lm = factories.LogicModule(name='Products Service', endpoint_name='products') lmm = factories.LogicModuleModel(logic_module_endpoint_name=lm.endpoint_name, model='Product', endpoint='/products/') lmm_org = factories.LogicModuleModel(logic_module_endpoint_name='core', model='Organization', endpoint='/organization/', lookup_field_name='organization_uuid', is_local=True) return factories.Relationship(origin_model=lmm, related_model=lmm_org, key='product_document_relationship')
def relationship2(relationship): lmm = relationship.origin_model # Products model from 1st relationship lm_location = factories.LogicModule(name='Location Service', endpoint_name='location') lmm_location = factories.LogicModuleModel( logic_module_endpoint_name=lm_location.endpoint_name, model='Location', endpoint='/siteprofile/') return factories.Relationship(origin_model=lmm, related_model=lmm_location, key='location_relationship')
def setUp(self): self.client = APIClient() self.core_user = factories.CoreUser() self.lm = factories.LogicModule(name='Products Service', endpoint_name='products') # bypass authentication self.client.force_authenticate(user=self.core_user) self.response_data = { 'id': 1, 'workflowlevel2_uuid': str(uuid.uuid4()), 'name': 'test', 'contact_uuid': 1 }
def test_load_swagger_resource_of_external_service(self, mock_perform_request, mock_app): # create extra service factories.LogicModule(name='Contacts and Appointments Service', endpoint='http://crm.example.com', endpoint_name='crm', relationships={}) # update relationships self.lm.relationships['products'] = {'contact_uuid': 'crm.Contact'} self.lm.save() mock_app.return_value = Mock(App) # mock service response headers = {'Content-Type': ['application/json']} service_response = Mock(PySwaggerResponse) service_response.status = 200 service_response.data = self.response_data service_response.header = headers # mock expand response expand_data = { 'first_name': 'Jeferson', 'last_name': 'Moura', 'contact_type': 'company', 'company': 'Humanitec' } expand_response = Mock(PySwaggerResponse) expand_response.data = expand_data mock_perform_request.side_effect = [service_response, expand_response] # make api request path = '/old/{}/{}/'.format(self.lm.endpoint_name, 'products') response = self.client.get(path, {'aggregate': 'true'}) # validate result expected_data = { 'id': 1, 'workflowlevel2_uuid': self.response_data['workflowlevel2_uuid'], 'name': 'test', 'contact_uuid': expand_data } self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.content), expected_data)
def test_validate_buildly_logic_module_no_viewset(self): request = self.get_mock_request('/', APIGatewayView, self.core_user) lm = factories.LogicModule() with self.assertRaises(GatewayError): validate_object_access(request, lm)
def crm_logic_module(): return factories.LogicModule( name='crm', endpoint_name='crm' )
def document_logic_module(): return factories.LogicModule( name='document', endpoint_name='document' )