def test_org_tree_nodes(self):
        self.shallow_org_tree()
        with self.assertRaises(ValueError) as context:
            OrgTree().all_leaves_below_id(0)  # none of the above
        self.assertTrue('not found' in context.exception.message)

        nodes = OrgTree().all_leaves_below_id(101)
        self.assertEquals(1, len(nodes))
    def test_org_tree_nodes(self):
        self.shallow_org_tree()
        with pytest.raises(ValueError) as context:
            OrgTree().all_leaves_below_id(0)  # none of the above
        assert 'not found' in str(context)

        nodes = OrgTree().all_leaves_below_id(101)
        assert 1 == len(nodes)
示例#3
0
def update_org_id(old_id, new_id, new_parent_id=None, new_name=None):
    bind = op.get_bind()
    session = Session(bind=bind)

    org = session.query(Organization).get(old_id)
    if not org:
        return
    OT = OrgTree()
    if len(OT.here_and_below_id(old_id)) > 1:
        raise ValueError('organization `{}` cannot be moved '
                         'before children'.format(old_id))

    make_transient(org)

    org.id = new_id
    if new_parent_id:
        org.partOf_id = new_parent_id
    if new_name:
        org.name = new_name
    session.add(org)
    session.commit()

    # update the related fields to point at new org

    for qb in session.query(QuestionnaireBank).filter_by(
            organization_id=old_id):
        qb.organization_id = new_id
    session.commit()

    for uc in session.query(UserConsent).filter_by(organization_id=old_id):
        uc.organization_id = new_id
    session.commit()

    for uo in session.query(UserOrganization).filter_by(
            organization_id=old_id):
        uo.organization_id = new_id
    session.commit()

    for tou in session.query(ToU).filter_by(organization_id=old_id):
        tou.organization_id = new_id
    session.commit()

    for oi in session.query(OrganizationIdentifier).filter_by(
            organization_id=old_id):
        oi.organization_id = new_id
    session.commit()

    for ol in session.query(OrganizationLocale).filter_by(
            organization_id=old_id):
        ol.organization_id = new_id
    session.commit()

    for oa in session.query(OrganizationAddress).filter_by(
            organization_id=old_id):
        oa.organization_id = new_id
    session.commit()

    session.execute('DELETE FROM organizations WHERE id = {}'.format(old_id))
示例#4
0
 def shallow_org_tree(self):
     """Create shallow org tree for common test needs"""
     org_101 = Organization(id=101, name='101')
     org_102 = Organization(id=102, name='102')
     org_1001 = Organization(id=1001, name='1001', partOf_id=101)
     with SessionScope(db):
         [db.session.add(org) for org in (org_101, org_102, org_1001)]
         db.session.commit()
     OrgTree.invalidate_cache()
示例#5
0
 def deepen_org_tree(self):
     """Create deeper tree when test needs it"""
     self.shallow_org_tree()
     org_l2 = Organization(id=1002, name='l2', partOf_id=102)
     org_l3_1 = Organization(id=10031, name='l3_1', partOf_id=1002)
     org_l3_2 = Organization(id=10032, name='l3_2', partOf_id=1002)
     with SessionScope(db):
         [db.session.add(org) for org in (org_l2, org_l3_1, org_l3_2)]
         db.session.commit()
     OrgTree.invalidate_cache()
def update_org_id(old_id, new_id, new_parent_id=None, new_name=None):
    bind = op.get_bind()
    session = Session(bind=bind)

    org = session.query(Organization).get(old_id)
    if not org:
        return
    OT = OrgTree()
    if len(OT.here_and_below_id(old_id)) > 1:
        raise ValueError('organization `{}` cannot be moved '
                         'before children'.format(old_id))

    make_transient(org)

    org.id = new_id
    if new_parent_id:
        org.partOf_id = new_parent_id
    if new_name:
        org.name = new_name
    session.add(org)
    session.commit()

    # update the related fields to point at new org

    for qb in session.query(QuestionnaireBank).filter_by(organization_id=old_id):
        qb.organization_id = new_id
    session.commit()

    for uc in session.query(UserConsent).filter_by(organization_id=old_id):
        uc.organization_id = new_id
    session.commit()

    for uo in session.query(UserOrganization).filter_by(organization_id=old_id):
        uo.organization_id = new_id
    session.commit()

    for tou in session.query(ToU).filter_by(organization_id=old_id):
        tou.organization_id = new_id
    session.commit()

    for oi in session.query(OrganizationIdentifier).filter_by(organization_id=old_id):
        oi.organization_id = new_id
    session.commit()

    for ol in session.query(OrganizationLocale).filter_by(organization_id=old_id):
        ol.organization_id = new_id
    session.commit()

    for oa in session.query(OrganizationAddress).filter_by(organization_id=old_id):
        oa.organization_id = new_id
    session.commit()

    session.execute('DELETE FROM organizations WHERE id = {}'.format(old_id))
 def test_all_leaves(self):
     # can we get a list of just the leaf orgs
     self.deepen_org_tree()
     leaves = OrgTree().all_leaf_ids()
     self.assertEquals(len(leaves), 3)
     for i in (1001, 10031, 10032):
         self.assertTrue(i in leaves)
示例#8
0
    def bless_with_basics(self, backdate=None, setdate=None):
        """Bless test user with basic requirements for coredata

        :param backdate: timedelta value.  Define to mock consents
          happening said period in the past.  See
          ``associative_backdate`` for issues with 'months'.

        :param setdate: datetime value.  Define to mock consents
          happening at exact time in the past

        """
        self.test_user = db.session.merge(self.test_user)
        self.test_user.birthdate = datetime.utcnow()

        # Register with a clinic
        self.shallow_org_tree()
        org = Organization.query.filter(
            Organization.partOf_id != None).first()
        assert org
        self.test_user.organizations.append(org)

        # Agree to Terms of Use and sign consent
        audit = Audit(user_id=TEST_USER_ID, subject_id=TEST_USER_ID)
        tou = ToU(audit=audit, agreement_url='http://not.really.org',
                  type='website terms of use')
        privacy = ToU(audit=audit, agreement_url='http://not.really.org',
                  type='privacy policy')
        parent_org = OrgTree().find(org.id).top_level()
        options = (STAFF_EDITABLE_MASK | INCLUDE_IN_REPORTS_MASK |
                   SEND_REMINDERS_MASK)
        consent = UserConsent(
            user_id=TEST_USER_ID, organization_id=parent_org,
            options=options, audit=audit, agreement_url='http://fake.org',
            acceptance_date=calc_date_params(
                backdate=backdate, setdate=setdate))
        with SessionScope(db):
            db.session.add(tou)
            db.session.add(privacy)
            db.session.add(consent)
            db.session.commit()

        # Invalidate org tree cache, in case orgs are added by other
        # tests.  W/o doing so, the new orgs aren't in the orgtree
        OrgTree.invalidate_cache()
示例#9
0
    def tearDown(self):
        """Clean db session.

        Database drop_all is done at setup due to app context challenges with
        LiveServerTestCase (it cleans up its context AFTER tearDown()
        is called)

        """
        db.session.remove()
        db.engine.dispose()

        # lazyprops can't survive a db purge - purge cached attributes
        for attr in dir(CC):
            if attr.startswith('_lazy'):
                delattr(CC, attr)
        for attr in dir(INTERVENTION):
            if attr.startswith('_lazy'):
                delattr(INTERVENTION, attr)
        OrgTree.invalidate_cache()
def qbs_for_user(user, session):
    users_top_orgs = set()
    for org in (o for o in user.organizations if o.id):
        users_top_orgs.add(OrgTree().find(org.id).top_level())

    results = [] if not users_top_orgs else (
        session.query(QuestionnaireBank).filter(
            QuestionnaireBank.organization_id.in_(users_top_orgs)).all())

    intervention_associated_qbs = session.query(QuestionnaireBank).filter(
        QuestionnaireBank.intervention_id.isnot(None))
    for qb in intervention_associated_qbs:
        intervention = session.query(Intervention).get(qb.intervention_id)
        display_details = intervention.display_for_user(user)
        if display_details.access:
            chec_func = observation_check("biopsy", 'true')
            if chec_func(intervention=intervention, user=user):
                results.append(qb)
    return results
示例#11
0
    def test_non_admin_org_change(self):
        """non-admin staff can't change their top-level orgs"""
        self.bless_with_basics()
        self.promote_user(role_name=ROLE.STAFF.value)
        self.test_user = db.session.merge(self.test_user)

        top = OrgTree().find(self.test_user.organizations[0].id).top_level()

        # Attempt to add the top-level org should raise
        self.login()
        data = {
            "careProvider": [{
                "reference": "Organization/{}".format(top)
            }],
            "resourceType": "Patient",
        }

        response = self.client.put('/api/demographics/%s' % TEST_USER_ID,
                                   content_type='application/json',
                                   data=json.dumps(data))
        assert response.status_code == 400
示例#12
0
    def bless_with_basics(
            self, user=None, backdate=None, setdate=None,
            local_metastatic=None, make_patient=True):
        """Bless user with basic requirements for coredata

        :param user: user to bless, self.test_user by default
        :param backdate: timedelta value.  Define to mock consents
          happening said period in the past.  See
          ``associative_backdate`` for issues with 'months'.
        :param setdate: datetime value.  Define to mock consents
          happening at exact time in the past
        :param local_metastatic: set to 'localized' or 'metastatic' for
          tests needing those respective orgs assigned to the user
        :param make_patient: add patient role unless set False

        """
        if not user:
            user = db.session.merge(self.test_user)
        else:
            user = db.session.merge(user)
        user_id = user.id
        user.birthdate = datetime.utcnow()

        if make_patient:
            self.promote_user(user=user, role_name=ROLE.PATIENT.value)

        # Register with a clinic
        self.shallow_org_tree()

        if local_metastatic:
            org = Organization.query.filter(
                Organization.name == local_metastatic).one()
        else:
            org = Organization.query.filter(
                Organization.partOf_id.isnot(None)).first()
        assert org
        user = db.session.merge(user)
        user.organizations.append(org)

        # Agree to Terms of Use and sign consent
        audit = Audit(user_id=user_id, subject_id=user_id)
        tou = ToU(
            audit=audit, agreement_url='http://not.really.org',
            type='website terms of use')
        privacy = ToU(
            audit=audit, agreement_url='http://not.really.org',
            type='privacy policy')
        parent_org = OrgTree().find(org.id).top_level()
        options = (STAFF_EDITABLE_MASK | INCLUDE_IN_REPORTS_MASK |
                   SEND_REMINDERS_MASK)
        consent = UserConsent(
            user_id=user_id, organization_id=parent_org,
            options=options, audit=audit, agreement_url='http://fake.org',
            acceptance_date=calc_date_params(
                backdate=backdate, setdate=setdate))
        with SessionScope(db):
            db.session.add(tou)
            db.session.add(privacy)
            db.session.add(consent)
            db.session.commit()

        # Invalidate org tree cache, in case orgs are added by other
        # tests.  W/o doing so, the new orgs aren't in the orgtree
        OrgTree.invalidate_cache()
 def test_here_and_below_id(self):
     self.deepen_org_tree()
     nodes = OrgTree().here_and_below_id(102)
     self.assertEquals(len(nodes), 4)
     for i in (102, 1002, 10031, 10032):
         self.assertTrue(i in nodes)