def factory_org_model(org_info: dict = TestOrgInfo.org1, org_type_info: dict = TestOrgTypeInfo.test_type, org_status_info: dict = TestOrgStatusInfo.test_status, payment_type_info: dict = TestPaymentTypeInfo.test_type, user_id=None): """Produce a templated org model.""" org_type = OrgTypeModel.get_default_type() if org_type_info['code'] != TestOrgTypeInfo.implicit['code']: org_type = OrgTypeModel(code=org_type_info['code'], desc=org_type_info['desc']) org_type.save() org_status = OrgStatusModel(code=org_status_info['code'], desc=org_status_info['desc']) org_status.save() preferred_payment = PaymentTypeModel(code=payment_type_info['code'], desc=payment_type_info['desc']) preferred_payment.save() org = OrgModel(name=org_info['name']) org.org_type = org_type org.org_status = org_status org.preferred_payment = preferred_payment org.created_by_id = user_id org.save() return org
def factory_org_model(org_info: dict = TestOrgInfo.org1, org_type_info: dict = None, org_status_info: dict = None, user_id=None, bcol_info: dict = TestBCOLInfo.bcol1): """Produce a templated org model.""" org_type = OrgTypeModel.get_default_type() if org_type_info and org_type_info['code'] != TestOrgTypeInfo.implicit[ 'code']: org_type = OrgTypeModel(code=org_type_info['code'], description=org_type_info['desc']) org_type.save() if org_status_info is not None: org_status = OrgStatusModel(code=org_status_info['code'], description=org_status_info['desc']) org_status.save() else: org_status = OrgStatusModel.get_default_status() org = OrgModel(name=org_info['name']) org.org_type = org_type org.access_type = org_info.get('accessType', '') org.org_status = org_status org.created_by_id = user_id org.bcol_account_id = bcol_info.get('bcol_account_id', '') org.bcol_user_id = bcol_info.get('bcol_user_id', '') org.save() return org
def factory_membersip_model(session): """Produce a templated org model.""" user = factory_user_model() org_type = OrgTypeModel(code='TEST', description='Test') session.add(org_type) session.commit() org_status = OrgStatusModel(code='TEST', description='Test') session.add(org_status) session.commit() preferred_payment = PaymentTypeModel(code='TEST', description='Test') session.add(preferred_payment) session.commit() org = OrgModel(name='Test Org') org.org_type = org_type org.org_status = OrgStatusModel.get_default_status() org.preferred_payment = preferred_payment org.save() membership = MembershipModel(org_id=org.id, user_id=user.id, membership_type_code=ADMIN, status=1) membership.save() return membership
def factory_org_model(name): """Produce a templated org model.""" org_type = OrgTypeModel(code='TEST', desc='Test') org_type.save() org_status = OrgStatusModel(code='TEST', desc='Test') org_status.save() preferred_payment = PaymentTypeModel(code='TEST', desc='Test') preferred_payment.save() org = OrgModel(name=name) org.org_type = org_type org.org_status = org_status org.preferred_payment = preferred_payment org.save() return org
def factory_org_model(org_name: str = 'Test ORg', user_id=None): """Produce a templated org model.""" org_type = OrgTypeModel.get_default_type() org_status = OrgStatusModel.get_default_status() org = OrgModel(name=org_name) org.org_type = org_type org.access_type = AccessType.REGULAR.value org.org_status = org_status org.created_by_id = user_id org.save() return org
def factory_org_model(org_info: dict = TestOrgInfo.org1, org_type_info: dict = TestOrgTypeInfo.test_type, org_status_info: dict = TestOrgStatusInfo.test_status, payment_type_info: dict = TestPaymentTypeInfo.test_type, user_id=None, bcol_info: dict = TestBCOLInfo.bcol1): """Produce a templated org model.""" org_type = OrgTypeModel.get_default_type() if org_type_info['code'] != TestOrgTypeInfo.implicit['code']: org_type = OrgTypeModel(code=org_type_info['code'], desc=org_type_info['desc']) org_type.save() if org_status_info: org_status = OrgStatusModel(code=org_status_info['code'], desc=org_status_info['desc']) org_status.save() else: org_status = OrgStatusModel.get_default_status() if payment_type_info: preferred_payment = PaymentTypeModel(code=payment_type_info['code'], desc=payment_type_info['desc']) preferred_payment.save() else: preferred_payment = PaymentTypeModel.get_default_payment_type() org = OrgModel(name=org_info['name']) org.org_type = org_type org.access_type = org_info.get('accessType', '') org.org_status = org_status org.created_by_id = user_id org.bcol_account_id = bcol_info.get('bcol_account_id', '') org.bcol_user_id = bcol_info.get('bcol_user_id', '') org.save() org.bcol_account_id = bcol_info['bcol_account_id'] return org
def factory_org_model(name, session): """Produce a templated org model.""" org_type = OrgTypeModel(code='TEST', description='Test') session.add(org_type) session.commit() org_status = OrgStatusModel(code='TEST', description='Test') session.add(org_status) session.commit() preferred_payment = PaymentTypeModel(code='TEST', description='Test') session.add(preferred_payment) session.commit() org = OrgModel(name=name) org.org_type = org_type org.org_status = OrgStatusModel.get_default_status() org.preferred_payment = preferred_payment org.save() return org
async def test_events_listener_queue(app, session, stan_server, event_loop, client_id, events_stan, future, monkeypatch): """Assert that events can be retrieved and decoded from the Queue.""" # Call back for the subscription from business_events_listener.worker import cb_nr_subscription_handler # 1. Create an Org # 2. Mock the rest service to return the invoices with the org created. # 3. Publish NR event and assert it's affiliated to the org. org = OrgModel(name='Test', org_type=OrgTypeModel.get_default_type(), org_status=OrgStatusModel.get_default_status()).save() org_id = org.id events_subject = 'test_subject' events_queue = 'test_queue' events_durable_name = 'test_durable' nr_number = 'NR 1234' nr_state = 'DRAFT' # register the handler to test it await subscribe_to_queue(events_stan, events_subject, events_queue, events_durable_name, cb_nr_subscription_handler) # Mock the rest service response to return the org just created. def get_invoices_mock(nr_number, token): response_content = json.dumps({ 'invoices': [{ 'businessIdentifier': nr_number, 'paymentAccount': { 'accountId': org_id } }] }) response = Response() response.status_code = 200 response._content = str.encode(response_content) return response monkeypatch.setattr('auth_api.services.rest_service.RestService.get', get_invoices_mock) monkeypatch.setattr( 'auth_api.services.rest_service.RestService.get_service_account_token', lambda *args, **kwargs: None) # add an event to queue await helper_add_event_to_queue(events_stan, events_subject, nr_number, nr_state, 'TEST') # Query the affiliations and assert the org has affiliation for the NR. entity: EntityModel = EntityModel.find_by_business_identifier(nr_number) assert entity assert entity.pass_code_claimed affiliations: [AffiliationModel ] = AffiliationModel.find_affiliations_by_org_id(org_id) assert len(affiliations) == 1 assert affiliations[0].entity_id == entity.id assert affiliations[0].org_id == org_id # Publish message again and assert it doesn't create duplicate affiliation. await helper_add_event_to_queue(events_stan, events_subject, nr_number, nr_state, 'TEST') affiliations: [AffiliationModel ] = AffiliationModel.find_affiliations_by_org_id(org_id) assert len(affiliations) == 1 # Publish message for an NR which was done using a service account or staff with no user account. def get_invoices_mock(nr_number, token): response_content = json.dumps({ 'invoices': [{ 'businessIdentifier': nr_number, 'paymentAccount': { 'accountId': 'SERVICE-ACCOUNT-NAME-REQUEST-SERVICE-ACCOUNT' } }] }) response = Response() response.status_code = 200 response._content = str.encode(response_content) return response monkeypatch.setattr('auth_api.services.rest_service.RestService.get', get_invoices_mock) # add an event to queue nr_number = 'NR 000001' await helper_add_event_to_queue(events_stan, events_subject, nr_number, nr_state, 'TEST') # Query the entity and assert the entity is not affiliated. entity: EntityModel = EntityModel.find_by_business_identifier(nr_number) assert entity assert not entity.pass_code_claimed