def test_preflight_invalid_service_user(self):
        # setup pre-flight conditions expected to fail
        ds_p3p = INTERVENTION.decision_support_p3p
        ds_client = Client(
            client_id='12345', client_secret='54321', user_id=TEST_USER_ID,
            intervention=ds_p3p, _redirect_uris='http://testsite.org',
            callback_url='http://callback.one')
        service = self.add_service_user(sponsor=self.test_user)

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

        ds_client = db.session.merge(ds_client)
        service = db.session.merge(service)
        service_id = service.id
        create_service_token(client=ds_client, user=service)

        # Export
        sp = SitePersistence(target_dir=self.tmpdir)
        sp.export(staging_exclusion=True)

        assert Token.query.count() == 1

        # Delete service account, and put fake patient in its place
        with SessionScope(db):
            db.session.delete(service)
            db.session.commit()

        assert User.query.count() == 1
        assert Token.query.count() == 0

        patient_role_id = Role.query.filter_by(
            name=ROLE.PATIENT.value).one().id
        patient_in_way = User(
            id=service_id, first_name='in the', last_name='way',
            email='*****@*****.**')
        with SessionScope(db):
            db.session.add(patient_in_way)
            db.session.commit()
        with SessionScope(db):
            db.session.add(UserRoles(
                user_id=service_id, role_id=patient_role_id))
            db.session.commit()

        # Import should now fail
        with self.assertRaises(ValueError) as context:
            sp.import_(keep_unmentioned=True, staging_exclusion=True)

        assert '*****@*****.**' in str(context.exception)
 def setUp(self):
     super(TestSitePersistence, self).setUp()
     if os.environ.get('PERSISTENCE_DIR'):
         self.fail("unset environment var PERSISTENCE_DIR for test")
     # Tests currently expect 'gil' version of persistence
     self.app.config['GIL'] = True
     SitePersistence(target_dir=None).import_(keep_unmentioned=False)
Example #3
0
    def testMidLevelOrgDeletion(self):
        """Test for problem scenario where mid level org should be removed"""
        Organization.query.delete()
        self.deepen_org_tree()

        # with deep (test) org tree in place, perform a delete by
        # repeating import w/o keep_unmentioned set
        SitePersistence(target_dir=None).import_(keep_unmentioned=False)
Example #4
0
def import_site_exclusions(directory):
    """Import serialized exclusions (saved on stage prior to prod db overwrite)

    :param directory: used to name a non-default target directory for import
      files

    """
    SitePersistence(target_dir=directory).import_(staging_exclusion=True,
                                                  keep_unmentioned=True)
Example #5
0
def import_site_exclusions(directory):
    """Import serialized exclusions (saved on stage prior to prod db overwrite)

    :param directory: used to name a non-default target directory for import
      files

    """
    if not directory:
        directory = app.config.get("PERSISTENCE_EXCLUSIONS_DIR")

    SitePersistence(target_dir=directory).import_(staging_exclusion=True,
                                                  keep_unmentioned=True)
    def test_preflight_valid(self):
        # setup pre-flight conditions expected to pass
        ds_p3p = INTERVENTION.decision_support_p3p
        ds_client = Client(
            client_id='12345', client_secret='54321', user_id=TEST_USER_ID,
            intervention=ds_p3p, _redirect_uris='http://testsite.org',
            callback_url='http://callback.one')
        service = self.add_service_user(sponsor=self.test_user)

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

        ds_client = db.session.merge(ds_client)
        service = db.session.merge(service)
        create_service_token(client=ds_client, user=service)

        # Export
        sp = SitePersistence(target_dir=self.tmpdir)
        sp.export(staging_exclusion=True)

        assert Token.query.count() == 1

        # Delete service account, expect it to return
        with SessionScope(db):
            db.session.delete(service)
            db.session.commit()

        assert User.query.count() == 1
        assert Token.query.count() == 0

        # Import
        sp.import_(keep_unmentioned=True, staging_exclusion=True)

        assert Token.query.count() == 1
        assert User.query.count() == 2
    def test_preflight_invalid_service_user(self):
        # setup pre-flight conditions expected to fail
        ds_p3p = INTERVENTION.decision_support_p3p
        ds_client = Client(client_id='12345',
                           client_secret='54321',
                           user_id=TEST_USER_ID,
                           intervention=ds_p3p,
                           _redirect_uris='http://testsite.org',
                           callback_url='http://callback.one')
        service = self.add_service_user(sponsor=self.test_user)

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

        ds_client = db.session.merge(ds_client)
        service = db.session.merge(service)
        service_id = service.id
        create_service_token(client=ds_client, user=service)

        # Export
        sp = SitePersistence(target_dir=self.tmpdir)
        sp.export(staging_exclusion=True)

        assert Token.query.count() == 1

        # Delete service account, and put fake patient in its place
        with SessionScope(db):
            db.session.delete(service)
            db.session.commit()

        assert User.query.count() == 1
        assert Token.query.count() == 0

        patient_role_id = Role.query.filter_by(
            name=ROLE.PATIENT.value).one().id
        patient_in_way = User(id=service_id,
                              first_name='in the',
                              last_name='way',
                              email='*****@*****.**')
        with SessionScope(db):
            db.session.add(patient_in_way)
            db.session.commit()
        with SessionScope(db):
            db.session.add(
                UserRoles(user_id=service_id, role_id=patient_role_id))
            db.session.commit()

        # Import should now fail
        with self.assertRaises(ValueError) as context:
            sp.import_(keep_unmentioned=True, staging_exclusion=True)

        assert '*****@*****.**' in str(context.exception)
Example #8
0
def export_site(directory, staging_exclusion):
    """Generate JSON file containing dynamic site config

    :param directory: used to name a non-default target directory for export
      files
    :param staging_exclusion: set only if persisting exclusions to retain
      on staging when pulling over production data.

    Portions of site configuration live in the database, such as
    Organizations and Access Strategies.  Generate a single export
    file for migration of this data to other instances of the service.

    NB the seed command imports the data file if found, along with
    other static data.

    """
    SitePersistence(target_dir=directory).export(
        staging_exclusion=staging_exclusion)
Example #9
0
def seed(keep_unmentioned=False):
    """Actual seed function

    NB this is defined separately so it can also be called internally,
    i.e. from sync

    """
    # Request context necessary for generating data from own HTTP APIs
    with app.test_request_context():
        add_static_concepts()

    add_static_interventions()
    add_static_organization()
    add_static_relationships()
    add_static_roles()
    db.session.commit()

    # import site export file if found
    SitePersistence(target_dir=None).import_(keep_unmentioned=keep_unmentioned)
    def test_preflight_valid(self):
        # setup pre-flight conditions expected to pass
        ds_p3p = INTERVENTION.decision_support_p3p
        ds_client = Client(client_id='12345',
                           client_secret='54321',
                           user_id=TEST_USER_ID,
                           intervention=ds_p3p,
                           _redirect_uris='http://testsite.org',
                           callback_url='http://callback.one')
        service = self.add_service_user(sponsor=self.test_user)

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

        ds_client = db.session.merge(ds_client)
        service = db.session.merge(service)
        create_service_token(client=ds_client, user=service)

        # Export
        sp = SitePersistence(target_dir=self.tmpdir)
        sp.export(staging_exclusion=True)

        assert Token.query.count() == 1

        # Delete service account, expect it to return
        with SessionScope(db):
            db.session.delete(service)
            db.session.commit()

        assert User.query.count() == 1
        assert Token.query.count() == 0

        # Import
        sp.import_(keep_unmentioned=True, staging_exclusion=True)

        assert Token.query.count() == 1
        assert User.query.count() == 2
Example #11
0
 def setUp(self):
     super(TestSitePersistence, self).setUp()
     # Tests currently expect 'gil' version of persistence
     self.app.config['PERSISTENCE_DIR'] = 'gil'
     SitePersistence(target_dir=None).import_(keep_unmentioned=False)