Esempio n. 1
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()
Esempio n. 2
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()
Esempio n. 3
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()
Esempio n. 4
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()
Esempio n. 5
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()