Esempio n. 1
0
    def test_in_role(self):
        user = self.test_user
        sm = INTERVENTION.SELF_MANAGEMENT
        sm.public_access = False
        d = {
            'function': 'in_role_list',
            'kwargs': [{
                'name': 'role_list',
                'value': [ROLE.PATIENT]
            }]
        }

        with SessionScope(db):
            strat = AccessStrategy(name="SELF_MANAGEMENT if PATIENT",
                                   intervention_id=sm.id,
                                   function_details=json.dumps(d))
            db.session.add(strat)
            db.session.commit()
        user, sm = map(db.session.merge, (user, sm))

        # Prior to granting user PATIENT role, the strategy
        # should not give access to SM
        self.assertFalse(sm.display_for_user(user).access)

        # Add PATIENT to user's roles
        add_role(user, ROLE.PATIENT)
        with SessionScope(db):
            db.session.commit()
        user, sm = map(db.session.merge, (user, sm))
        self.assertTrue(sm.display_for_user(user).access)
Esempio n. 2
0
    def test_thankyou_on_expired(self):
        """If baseline expired and other QB's done, should see thank you"""
        ae = INTERVENTION.ASSESSMENT_ENGINE
        ae_id = ae.id
        # backdate so baseline is expired
        self.bless_with_basics(backdate=timedelta(days=60))

        # generate questionnaire banks and associate user with
        # metastatic organization
        mock_questionnairebanks()
        metastatic_org = Organization.query.filter_by(name='metastatic').one()
        self.test_user.organizations.append(metastatic_org)

        # Add a fake assessment only to non-expired one from indefinite
        mock_qr(user_id=TEST_USER_ID, instrument_id='irondemog')

        with SessionScope(db):
            d = {'function': 'update_card_html_on_completion', 'kwargs': []}
            strat = AccessStrategy(
                name="update assessment_engine card_html on completion",
                intervention_id=ae_id,
                function_details=json.dumps(d))
            db.session.add(strat)
            db.session.commit()
        user, ae = map(db.session.merge, (self.test_user, ae))

        self.assertTrue("Thank you" in ae.display_for_user(user).card_html)
Esempio n. 3
0
    def test_clinc_id(self):
        # Create several orgs with identifier
        org1 = Organization(name='org1')
        org2 = Organization(name='org2')
        org3 = Organization(name='org3')
        identifier = Identifier(value='pick me', system=DECISION_SUPPORT_GROUP)
        for org in (org1, org2, org3):
            org.identifiers.append(identifier)

        # Add access strategy to the care plan intervention
        cp = INTERVENTION.CARE_PLAN
        cp.public_access = False  # turn off public access to force strategy
        cp_id = cp.id

        with SessionScope(db):
            map(db.session.add, (org1, org2, org3))
            db.session.commit()

        org1, org2, org3 = map(db.session.merge, (org1, org2, org3))
        d = {
            'function': 'limit_by_clinic_w_id',
            'kwargs': [{
                'name': 'identifier_value',
                'value': 'pick me'
            }]
        }
        strat = AccessStrategy(name="member of org with identifier",
                               intervention_id=cp_id,
                               function_details=json.dumps(d))

        with SessionScope(db):
            db.session.add(strat)
            db.session.commit()

        cp = INTERVENTION.CARE_PLAN
        user = db.session.merge(self.test_user)

        # Prior to associating user with any orgs, shouldn't have access
        self.assertFalse(cp.display_for_user(user).access)

        # Add association and test again
        user.organizations.append(org3)
        with SessionScope(db):
            db.session.commit()
        user, cp = map(db.session.merge, (user, cp))
        self.assertTrue(cp.display_for_user(user).access)
Esempio n. 4
0
 def test_strat_from_json(self):
     """Create access strategy from json"""
     d = {
         'name': 'unit test example',
         'description': 'a lovely way to test',
         'function_details': {
             'function':
             'allow_if_not_in_intervention',
             'kwargs': [{
                 'name': 'intervention_name',
                 'value': INTERVENTION.SELF_MANAGEMENT.name
             }]
         }
     }
     acc_strat = AccessStrategy.from_json(d)
     self.assertEquals(d['name'], acc_strat.name)
     self.assertEquals(d['function_details'],
                       json.loads(acc_strat.function_details))
Esempio n. 5
0
    def test_no_tx(self):
        """Test strategy for not starting treatment"""
        # Add access strategies to the care plan intervention
        cp = INTERVENTION.CARE_PLAN
        cp.public_access = False  # turn off public access to force strategy
        cp_id = cp.id

        with SessionScope(db):
            d = {
                'function': 'tx_begun',
                'kwargs': [{
                    'name': 'boolean_value',
                    'value': 'false'
                }]
            }
            strat = AccessStrategy(name="has not stared treatment",
                                   intervention_id=cp_id,
                                   function_details=json.dumps(d))
            db.session.add(strat)
            db.session.commit()
        cp = INTERVENTION.CARE_PLAN
        user = db.session.merge(self.test_user)

        # Prior to declaring TX, user should have access
        self.assertTrue(cp.display_for_user(user).access)

        self.add_procedure(code='424313000',
                           display='Started active surveillance')
        with SessionScope(db):
            db.session.commit()
        user, cp = map(db.session.merge, (user, cp))

        # Declaring they started a non TX proc, should still have access
        self.assertTrue(cp.display_for_user(user).access)

        self.add_procedure(code='26294005',
                           display='Radical prostatectomy (nerve-sparing)',
                           system=SNOMED)
        with SessionScope(db):
            db.session.commit()
        user, cp = map(db.session.merge, (user, cp))

        # Declaring they started a TX proc, should lose access
        self.assertFalse(cp.display_for_user(user).access)
Esempio n. 6
0
    def test_exclusive_stategy(self):
        """Test exclusive intervention strategy"""
        user = self.test_user
        ds_p3p = INTERVENTION.DECISION_SUPPORT_P3P
        ds_wc = INTERVENTION.DECISION_SUPPORT_WISERCARE

        ds_p3p.public_access = False
        ds_wc.public_access = False

        with SessionScope(db):
            d = {
                'function': 'allow_if_not_in_intervention',
                'kwargs': [{
                    'name': 'intervention_name',
                    'value': ds_wc.name
                }]
            }
            strat = AccessStrategy(name="exclusive decision support strategy",
                                   intervention_id=ds_p3p.id,
                                   function_details=json.dumps(d))
            db.session.add(strat)
            db.session.commit()
        user, ds_p3p, ds_wc = map(db.session.merge, (user, ds_p3p, ds_wc))

        # Prior to associating user w/ decision support, the strategy
        # should give access to p3p
        self.assertTrue(ds_p3p.display_for_user(user).access)
        self.assertFalse(ds_wc.display_for_user(user).access)

        # Add user to wisercare, confirm it's the only w/ access

        ui = UserIntervention(user_id=user.id,
                              intervention_id=ds_wc.id,
                              access='granted')
        with SessionScope(db):
            db.session.add(ui)
            db.session.commit()
        user, ds_p3p, ds_wc = map(db.session.merge, (user, ds_p3p, ds_wc))

        self.assertFalse(ds_p3p.display_for_user(user).access)
        self.assertTrue(ds_wc.display_for_user(user).access)
Esempio n. 7
0
    def test_diag_stategy(self):
        """Test strategy for diagnosis"""
        # Add access strategies to the care plan intervention
        cp = INTERVENTION.CARE_PLAN
        cp.public_access = False  # turn off public access to force strategy
        cp_id = cp.id

        with SessionScope(db):
            d = {
                'function':
                'observation_check',
                'kwargs': [{
                    'name': 'display',
                    'value': CC.PCaDIAG.codings[0].display
                }, {
                    'name': 'boolean_value',
                    'value': 'true'
                }]
            }
            strat = AccessStrategy(name="has PCa diagnosis",
                                   intervention_id=cp_id,
                                   function_details=json.dumps(d))
            db.session.add(strat)
            db.session.commit()
        cp = INTERVENTION.CARE_PLAN
        user = db.session.merge(self.test_user)

        # Prior to PCa dx, user shouldn't have access
        self.assertFalse(cp.display_for_user(user).access)

        # Bless the test user with PCa diagnosis
        self.login()
        user.save_constrained_observation(codeable_concept=CC.PCaDIAG,
                                          value_quantity=CC.TRUE_VALUE,
                                          audit=Audit(user_id=TEST_USER_ID,
                                                      subject_id=TEST_USER_ID))
        with SessionScope(db):
            db.session.commit()
        user, cp = map(db.session.merge, (user, cp))

        self.assertTrue(cp.display_for_user(user).access)
Esempio n. 8
0
    def test_card_html_update(self):
        """Test strategy with side effects - card_html update"""
        ae = INTERVENTION.ASSESSMENT_ENGINE
        ae_id = ae.id
        self.bless_with_basics()

        # generate questionnaire banks and associate user with
        # metastatic organization
        mock_questionnairebanks()
        metastatic_org = Organization.query.filter_by(name='metastatic').one()
        self.test_user.organizations.append(metastatic_org)

        with SessionScope(db):
            d = {'function': 'update_card_html_on_completion', 'kwargs': []}
            strat = AccessStrategy(
                name="update assessment_engine card_html on completion",
                intervention_id=ae_id,
                function_details=json.dumps(d))
            db.session.add(strat)
            db.session.commit()
        user, ae = map(db.session.merge, (self.test_user, ae))

        # without completing an assessment, card_html should includ username
        self.assertTrue(
            user.display_name in ae.display_for_user(user).card_html)

        # Add a fake assessments and see a change
        mock_qr(user_id=TEST_USER_ID, instrument_id='eortc')
        mock_qr(user_id=TEST_USER_ID, instrument_id='ironmisc')
        mock_qr(user_id=TEST_USER_ID, instrument_id='factfpsi')
        mock_qr(user_id=TEST_USER_ID, instrument_id='epic26')
        mock_qr(user_id=TEST_USER_ID, instrument_id='prems')
        mock_qr(user_id=TEST_USER_ID, instrument_id='irondemog')

        user, ae = map(db.session.merge, (self.test_user, ae))
        self.assertTrue("Thank you" in ae.display_for_user(user).card_html)
Esempio n. 9
0
    def test_truenth_st_conditions(self):
        # Test the list of conditions expected for SymptomTracker in truenth
        sm = INTERVENTION.SELF_MANAGEMENT
        sm.public_access = False
        user = self.test_user
        add_role(user, ROLE.PATIENT)
        sm_identifier = Identifier(value='self_management',
                                   system=DECISION_SUPPORT_GROUP)
        uw = Organization(name='UW Medicine (University of Washington)')
        uw.identifiers.append(sm_identifier)
        with SessionScope(db):
            db.session.add(uw)
            db.session.commit()
        user.organizations.append(uw)
        INTERVENTION.SEXUAL_RECOVERY.public_access = False
        with SessionScope(db):
            db.session.add(user)
            db.session.commit()
        user, uw = map(db.session.merge, (user, uw))

        # Full logic from story #150532380
        description = ("[strategy_1: (user NOT IN sexual_recovery)] "
                       "AND [strategy_2: (user has role PATIENT)] "
                       "AND [strategy_3: (user has BIOPSY)]")

        d = {
            'function':
            'combine_strategies',
            'kwargs': [
                # Not in SR (strat 1)
                {
                    'name': 'strategy_1',
                    'value': 'allow_if_not_in_intervention'
                },
                {
                    'name':
                    'strategy_1_kwargs',
                    'value': [{
                        'name': 'intervention_name',
                        'value': INTERVENTION.SEXUAL_RECOVERY.name
                    }]
                },
                # Does has role PATIENT (strat 2)
                {
                    'name': 'strategy_2',
                    'value': 'in_role_list'
                },
                {
                    'name': 'strategy_2_kwargs',
                    'value': [{
                        'name': 'role_list',
                        'value': [ROLE.PATIENT]
                    }]
                },
                # Has Localized PCa (strat 3)
                {
                    'name': 'strategy_3',
                    'value': 'observation_check'
                },
                {
                    'name':
                    'strategy_3_kwargs',
                    'value': [{
                        'name': 'display',
                        'value': CC.BIOPSY.codings[0].display
                    }, {
                        'name': 'boolean_value',
                        'value': 'true'
                    }]
                }
            ]
        }
        with SessionScope(db):
            strat = AccessStrategy(
                name='Symptom Tracker Conditions',
                description=description,
                intervention_id=INTERVENTION.SELF_MANAGEMENT.id,
                function_details=json.dumps(d))
            # print json.dumps(strat.as_json(), indent=2)
            db.session.add(strat)
            db.session.commit()
        user, sm = map(db.session.merge, (user, sm))

        # only first two strats true so far, therfore, should be False
        self.assertFalse(sm.display_for_user(user).access)

        self.add_procedure(code='424313000',
                           display='Started active surveillance')
        user = db.session.merge(user)
        self.login()
        user.save_constrained_observation(codeable_concept=CC.BIOPSY,
                                          value_quantity=CC.TRUE_VALUE,
                                          audit=Audit(user_id=TEST_USER_ID,
                                                      subject_id=TEST_USER_ID))
        with SessionScope(db):
            db.session.commit()
        user, sm = map(db.session.merge, (user, sm))

        # All conditions now met, should have access
        self.assertTrue(sm.display_for_user(user).access)

        # Remove all clinics, should still have access
        user.organizations = []
        with SessionScope(db):
            db.session.commit()
        user, sm = map(db.session.merge, (user, sm))
        self.assertEquals(user.organizations.count(), 0)
        self.assertTrue(sm.display_for_user(user).access)

        # Finally, remove the PATIENT role and it should disappear
        user.roles.pop()
        with SessionScope(db):
            db.session.add(user)
            db.session.commit()
        user, sm = map(db.session.merge, (user, sm))
        self.assertFalse(sm.display_for_user(user).access)
Esempio n. 10
0
    def test_eproms_p3p_conditions(self):
        # Test the list of conditions expected for p3p on eproms
        # very similar to truenth p3p, plus ! role write_only
        ds_p3p = INTERVENTION.DECISION_SUPPORT_P3P
        ds_p3p.public_access = False
        user = self.test_user
        p3p_identifier = Identifier(value='decision_support_p3p',
                                    system=DECISION_SUPPORT_GROUP)
        wc_identifier = Identifier(value='decision_support_wisercare',
                                   system=DECISION_SUPPORT_GROUP)
        ucsf = Organization(name='UCSF Medical Center')
        uw = Organization(name='UW Medicine (University of Washington)')
        ucsf.identifiers.append(wc_identifier)
        uw.identifiers.append(p3p_identifier)
        with SessionScope(db):
            db.session.add(ucsf)
            db.session.add(uw)
            db.session.commit()
        user.organizations.append(ucsf)
        user.organizations.append(uw)
        INTERVENTION.SEXUAL_RECOVERY.public_access = False
        with SessionScope(db):
            db.session.commit()
        ucsf, user, uw = map(db.session.merge, (ucsf, user, uw))

        # Full logic from story #127433167
        description = (
            "[strategy_1: (user NOT IN sexual_recovery)] "
            "AND [strategy_2 <a nested combined strategy>: "
            "((user NOT IN list of clinics (including UCSF)) OR "
            "(user IN list of clinics including UCSF and UW))] "
            "AND [strategy_3: (user has NOT started TX)] "
            "AND [strategy_4: (user does NOT have PCaMETASTASIZE)] "
            "AND [startegy_5: (user does NOT have roll WRITE_ONLY)]")

        d = {
            'function':
            'combine_strategies',
            'kwargs': [
                # Not in SR (strat 1)
                {
                    'name': 'strategy_1',
                    'value': 'allow_if_not_in_intervention'
                },
                {
                    'name':
                    'strategy_1_kwargs',
                    'value': [{
                        'name': 'intervention_name',
                        'value': INTERVENTION.SEXUAL_RECOVERY.name
                    }]
                },
                # Not in clinic list (UCSF,) OR (In Clinic UW and UCSF) (#2)
                {
                    'name': 'strategy_2',
                    'value': 'combine_strategies'
                },
                {
                    'name':
                    'strategy_2_kwargs',
                    'value': [
                        {
                            'name': 'combinator',
                            'value': 'any'
                        },  # makes this combination an 'OR'
                        {
                            'name': 'strategy_1',
                            'value': 'not_in_clinic_w_id'
                        },
                        {
                            'name':
                            'strategy_1_kwargs',
                            'value': [{
                                'name': 'identifier_value',
                                'value': 'decision_support_wisercare'
                            }]
                        },
                        {
                            'name': 'strategy_2',
                            'value': 'combine_strategies'
                        },
                        {
                            'name':
                            'strategy_2_kwargs',
                            'value': [
                                {
                                    'name': 'strategy_1',
                                    'value': 'limit_by_clinic_w_id'
                                },
                                {
                                    'name':
                                    'strategy_1_kwargs',
                                    'value': [{
                                        'name':
                                        'identifier_value',
                                        'value':
                                        'decision_support_wisercare'
                                    }]
                                },
                                {
                                    'name': 'strategy_2',
                                    'value': 'limit_by_clinic_w_id'
                                },
                                {
                                    'name':
                                    'strategy_2_kwargs',
                                    'value': [{
                                        'name': 'identifier_value',
                                        'value': 'decision_support_p3p'
                                    }]
                                },
                            ]
                        },
                    ]
                },
                # Not Started TX (strat 3)
                {
                    'name': 'strategy_3',
                    'value': 'tx_begun'
                },
                {
                    'name': 'strategy_3_kwargs',
                    'value': [{
                        'name': 'boolean_value',
                        'value': 'false'
                    }]
                },
                # Has Localized PCa (strat 4)
                {
                    'name': 'strategy_4',
                    'value': 'observation_check'
                },
                {
                    'name':
                    'strategy_4_kwargs',
                    'value': [{
                        'name': 'display',
                        'value': CC.PCaLocalized.codings[0].display
                    }, {
                        'name': 'boolean_value',
                        'value': 'true'
                    }]
                },
                # Does NOT have roll WRITE_ONLY (strat 5)
                {
                    'name': 'strategy_5',
                    'value': 'not_in_role_list'
                },
                {
                    'name': 'strategy_5_kwargs',
                    'value': [{
                        'name': 'role_list',
                        'value': [ROLE.WRITE_ONLY]
                    }]
                }
            ]
        }
        with SessionScope(db):
            strat = AccessStrategy(
                name='P3P Access Conditions',
                description=description,
                intervention_id=INTERVENTION.DECISION_SUPPORT_P3P.id,
                function_details=json.dumps(d))
            # print json.dumps(strat.as_json(), indent=2)
            db.session.add(strat)
            db.session.commit()
        user, ds_p3p = map(db.session.merge, (user, ds_p3p))

        # only first two strats true so far, therfore, should be False
        self.assertFalse(ds_p3p.display_for_user(user).access)

        self.add_procedure(code='424313000',
                           display='Started active surveillance')
        user = db.session.merge(user)
        self.login()
        user.save_constrained_observation(codeable_concept=CC.PCaLocalized,
                                          value_quantity=CC.TRUE_VALUE,
                                          audit=Audit(user_id=TEST_USER_ID,
                                                      subject_id=TEST_USER_ID))
        with SessionScope(db):
            db.session.commit()
        user, ds_p3p = map(db.session.merge, (user, ds_p3p))

        # All conditions now met, should have access
        self.assertTrue(ds_p3p.display_for_user(user).access)

        # Remove all clinics, should still have access
        user.organizations = []
        with SessionScope(db):
            db.session.commit()
        user, ds_p3p = map(db.session.merge, (user, ds_p3p))
        self.assertEquals(user.organizations.count(), 0)
        self.assertTrue(ds_p3p.display_for_user(user).access)

        # Finally, add the WRITE_ONLY group and it should disappear
        add_role(user, ROLE.WRITE_ONLY)
        with SessionScope(db):
            db.session.commit()
        user, ds_p3p = map(db.session.merge, (user, ds_p3p))
        self.assertFalse(ds_p3p.display_for_user(user).access)
Esempio n. 11
0
    def test_and_strats(self):
        # Create a logical 'and' with multiple strategies

        ds_p3p = INTERVENTION.DECISION_SUPPORT_P3P
        ds_p3p.public_access = False
        user = self.test_user
        identifier = Identifier(value='decision_support_p3p',
                                system=DECISION_SUPPORT_GROUP)
        uw = Organization(name='UW Medicine (University of Washington)')
        uw.identifiers.append(identifier)
        INTERVENTION.SEXUAL_RECOVERY.public_access = False
        with SessionScope(db):
            db.session.add(uw)
            db.session.commit()
        user, uw = map(db.session.merge, (user, uw))
        uw_child = Organization(name='UW clinic', partOf_id=uw.id)
        with SessionScope(db):
            db.session.add(uw_child)
            db.session.commit()
        user, uw, uw_child = map(db.session.merge, (user, uw, uw_child))

        d = {
            'name':
            'not in SR _and_ in clinc UW',
            'function':
            'combine_strategies',
            'kwargs': [{
                'name': 'strategy_1',
                'value': 'allow_if_not_in_intervention'
            }, {
                'name':
                'strategy_1_kwargs',
                'value': [{
                    'name': 'intervention_name',
                    'value': INTERVENTION.SEXUAL_RECOVERY.name
                }]
            }, {
                'name': 'strategy_2',
                'value': 'limit_by_clinic_w_id'
            }, {
                'name':
                'strategy_2_kwargs',
                'value': [{
                    'name': 'identifier_value',
                    'value': 'decision_support_p3p'
                }]
            }]
        }
        with SessionScope(db):
            strat = AccessStrategy(
                name=d['name'],
                intervention_id=INTERVENTION.DECISION_SUPPORT_P3P.id,
                function_details=json.dumps(d))
            db.session.add(strat)
            db.session.commit()
        user, ds_p3p = map(db.session.merge, (user, ds_p3p))

        # first strat true, second false.  therfore, should be False
        self.assertFalse(ds_p3p.display_for_user(user).access)

        # Add the child organization to the user, which should be included
        # due to default behavior of limit_by_clinic_w_id
        user.organizations.append(uw_child)
        with SessionScope(db):
            db.session.commit()
        user, ds_p3p = map(db.session.merge, (user, ds_p3p))
        # first strat true, second true.  therfore, should be True
        self.assertTrue(ds_p3p.display_for_user(user).access)

        ui = UserIntervention(user_id=user.id,
                              intervention_id=INTERVENTION.SEXUAL_RECOVERY.id,
                              access='granted')
        with SessionScope(db):
            db.session.add(ui)
            db.session.commit()
        user, ds_p3p = map(db.session.merge, (user, ds_p3p))

        # first strat true, second false.  AND should be false
        self.assertFalse(ds_p3p.display_for_user(user).access)
Esempio n. 12
0
    def test_not_in_role_or_sr(self):
        user = self.test_user
        sm = INTERVENTION.SELF_MANAGEMENT
        sr = INTERVENTION.SEXUAL_RECOVERY

        sm.public_access = False
        sr.public_access = False
        d = {
            'function':
            'combine_strategies',
            'kwargs': [{
                'name': 'strategy_1',
                'value': 'allow_if_not_in_intervention'
            }, {
                'name':
                'strategy_1_kwargs',
                'value': [{
                    'name': 'intervention_name',
                    'value': INTERVENTION.SEXUAL_RECOVERY.name
                }]
            }, {
                'name': 'strategy_2',
                'value': 'not_in_role_list'
            }, {
                'name':
                'strategy_2_kwargs',
                'value': [{
                    'name': 'role_list',
                    'value': [ROLE.WRITE_ONLY]
                }]
            }]
        }

        with SessionScope(db):
            strat = AccessStrategy(
                name="SELF_MANAGEMENT if not SR and not in WRITE_ONLY",
                intervention_id=sm.id,
                function_details=json.dumps(d))
            # print json.dumps(strat.as_json(), indent=2)
            db.session.add(strat)
            db.session.commit()
        user, sm, sr = map(db.session.merge, (user, sm, sr))

        # Prior to granting user WRITE_ONLY role, the strategy
        # should give access to p3p
        self.assertTrue(sm.display_for_user(user).access)

        # Add WRITE_ONLY to user's roles
        add_role(user, ROLE.WRITE_ONLY)
        with SessionScope(db):
            db.session.commit()
        user, sm, sr = map(db.session.merge, (user, sm, sr))
        self.assertFalse(sm.display_for_user(user).access)

        # Revert role change for next condition
        user.roles = []
        with SessionScope(db):
            db.session.commit()
        user, sm, sr = map(db.session.merge, (user, sm, sr))
        self.assertTrue(sm.display_for_user(user).access)

        # Grant user sr access, they should lose sm visibility
        ui = UserIntervention(user_id=user.id,
                              intervention_id=INTERVENTION.SEXUAL_RECOVERY.id,
                              access='granted')
        with SessionScope(db):
            db.session.add(ui)
            db.session.commit()
        user, sm, sr = map(db.session.merge, (user, sm, sr))
        self.assertFalse(sm.display_for_user(user).access)