def teardown_test_data():
    with Transaction() as t:
        acct_repo = AccountRepo(t)
        admin_repo = AdminRepo(t)
        acct_repo.delete_account(ACCT_ID_1)
        admin_repo.delete_project_by_name(DUMMY_PROJ_NAME)
        t.commit()
def delete_account(account_id, token_info):
    validate_admin_access(token_info)

    with Transaction() as t:
        acct_repo = AccountRepo(t)
        src_repo = SourceRepo(t)
        samp_repo = SampleRepo(t)
        sar_repo = SurveyAnswersRepo(t)

        acct = acct_repo.get_account(account_id)
        if acct is None:
            return jsonify(message="Account not found", code=404), 404
        else:
            # the account is already scrubbed so let's stop early
            if acct.account_type == 'deleted':
                return None, 204

        sample_count = 0
        sources = src_repo.get_sources_in_account(account_id)

        for source in sources:
            samples = samp_repo.get_samples_by_source(account_id, source.id)

            has_samples = len(samples) > 0
            sample_count += len(samples)

            for sample in samples:
                # we scrub rather than disassociate in the event that the
                # sample is in our freezers but not with an up-to-date scan
                samp_repo.scrub(account_id, source.id, sample.id)

            surveys = sar_repo.list_answered_surveys(account_id, source.id)
            if has_samples:
                # if we have samples, we need to scrub survey / source
                # free text
                for survey_id in surveys:
                    sar_repo.scrub(account_id, source.id, survey_id)
                src_repo.scrub(account_id, source.id)
            else:
                # if we do not have associated samples, then the source
                # is safe to delete
                for survey_id in surveys:
                    sar_repo.delete_answered_survey(account_id, survey_id)
                src_repo.delete_source(account_id, source.id)

        # an account is safe to delete if there are no associated samples
        if sample_count > 0:
            acct_repo.scrub(account_id)
        else:
            acct_repo.delete_account(account_id)

        t.commit()

    return None, 204
def teardown_test_data():
    with Transaction() as t:
        acct_repo = AccountRepo(t)
        admin_repo = AdminRepo(t)
        acct_repo.delete_account(ACCT_ID_1)
        admin_repo.delete_project_by_name(DUMMY_PROJ_NAME)
        with t.cursor() as cur:
            cur.execute("UPDATE barcodes.project"
                        " SET is_active = TRUE"
                        " WHERE project_id = 2")

        t.commit()
def delete_dummy_accts():
    all_sample_ids = []
    acct_ids = [ACCT_ID_1, ACCT_ID_2]

    with Transaction() as t:
        acct_repo = AccountRepo(t)
        source_repo = SourceRepo(t)
        survey_answers_repo = SurveyAnswersRepo(t)
        sample_repo = SampleRepo(t)

        for curr_acct_id in acct_ids:
            sources = source_repo.get_sources_in_account(curr_acct_id)
            for curr_source in sources:
                source_samples = sample_repo.get_samples_by_source(
                    curr_acct_id, curr_source.id)
                sample_ids = [x.id for x in source_samples]
                all_sample_ids.extend(sample_ids)

                # Dissociate all samples linked to this source from all
                # answered surveys linked to this source, then delete all
                # answered surveys
                delete_dummy_answered_surveys_from_source_with_t(
                    t, curr_acct_id, curr_source.id, sample_ids,
                    survey_answers_repo)

                # Now dissociate all the samples from this source
                for curr_sample_id in sample_ids:
                    sample_repo.dissociate_sample(curr_acct_id, curr_source.id,
                                                  curr_sample_id)

                # Finally, delete the source
                source_repo.delete_source(curr_acct_id, curr_source.id)

            # Delete the account
            acct_repo.delete_account(curr_acct_id)

        # Belt and suspenders: these test emails are used by some tests outside
        # of this module as well, so can't be sure they are paired with the
        # above dummy account ids
        acct_repo.delete_account_by_email(TEST_EMAIL)
        acct_repo.delete_account_by_email(TEST_EMAIL_2)

        # Delete the kit and all samples that were attached to any sources
        # NB: This won't clean up any samples that were created but NOT
        # attached to any sources ...
        if len(all_sample_ids) == 0:
            all_sample_ids = None
        _remove_mock_kit(t, mock_sample_ids=all_sample_ids)

        t.commit()
Example #5
0
def delete_dummy_accts():
    with Transaction() as t:
        source_repo = SourceRepo(t)
        survey_answers_repo = SurveyAnswersRepo(t)
        sources = source_repo.get_sources_in_account(ACCT_ID_1)
        for curr_source in sources:
            answers = survey_answers_repo.list_answered_surveys(
                ACCT_ID_1, curr_source.id)
            for survey_id in answers:
                survey_answers_repo.delete_answered_survey(
                    ACCT_ID_1, survey_id)

            source_repo.delete_source(ACCT_ID_1, curr_source.id)

        acct_repo = AccountRepo(t)
        acct_repo.delete_account(ACCT_ID_1)
        acct_repo.delete_account(ACCT_ID_2)
        # Belt and suspenders: these test emails are used by some tests outside
        # of this module as well, so can't be sure they are paired with the
        # above dummy account ids
        acct_repo.delete_account_by_email(TEST_EMAIL)
        acct_repo.delete_account_by_email(TEST_EMAIL_2)
        t.commit()
Example #6
0
 def teardown_test_data():
     with Transaction() as t:
         acct_repo = AccountRepo(t)
         acct_repo.delete_account(STANDARD_ACCT_ID)
         acct_repo.delete_account(ADMIN_ACCT_ID)
         t.commit()