def test_validate_cohorts_with_project_variations(self, mock):
        self.helper_reset_validation()
        self.cohort.validate_as_user_ids = False

        wikiusers = self.session.query(WikiUserStore).all()
        username = '******'
        wikiusers[0].raw_id_or_name = username
        wikiusers[1].raw_id_or_name = username
        # set different project versions of the same project
        # they will be normalized to 'wiki' by the normalize_project mock
        wikiusers[0].project = 'en'
        wikiusers[1].project = 'enwiki'
        # add also the correspondent mediawiki user
        self.mwSession.add(MediawikiUser(user_name=username))
        self.mwSession.commit()

        v = ValidateCohort(self.cohort)
        v.validate_records(self.session, self.cohort)

        assert_equal(self.cohort.validated, True)
        assert_equal(len(
            self.session.query(WikiUserStore)
                .filter(WikiUserStore.validating_cohort == self.cohort.id)
                .filter(WikiUserStore.valid)
                .all()
        ), 3)
        assert_equal(len(
            self.session.query(WikiUserStore)
                .filter(WikiUserStore.validating_cohort == self.cohort.id)
                .filter(WikiUserStore.valid.in_([False]))
                .all()
        ), 0)
Пример #2
0
    def test_validate_cohorts_with_project_variations(self, mock):
        self.helper_reset_validation()
        self.cohort.validate_as_user_ids = False

        wikiusers = self.session.query(WikiUserStore).all()
        username = '******'
        wikiusers[0].raw_id_or_name = username
        wikiusers[1].raw_id_or_name = username
        # set different project versions of the same project
        # they will be normalized to 'wiki' by the normalize_project mock
        wikiusers[0].project = 'en'
        wikiusers[1].project = 'enwiki'
        # add also the correspondent mediawiki user
        self.mwSession.add(MediawikiUser(user_name=username))
        self.mwSession.commit()

        v = ValidateCohort(self.cohort)
        v.validate_records(self.session, self.cohort)

        assert_equal(self.cohort.validated, True)
        assert_equal(
            len(
                self.session.query(WikiUserStore).filter(
                    WikiUserStore.validating_cohort == self.cohort.id).filter(
                        WikiUserStore.valid).all()), 3)
        assert_equal(
            len(
                self.session.query(WikiUserStore).filter(
                    WikiUserStore.validating_cohort == self.cohort.id).filter(
                        WikiUserStore.valid.in_([False])).all()), 0)
Пример #3
0
 def test_validate_cohorts_when_invalid_project_causes_exception(
         self, mock):
     '''
     Mocks normalize_project to force an exception when accessing
     an unexisting project database. This exception should not be
     forwarded.
     '''
     self.helper_reset_validation()
     self.cohort.validate_as_user_ids = False
     wikiusers = self.session.query(WikiUserStore).all()
     wikiusers[0].project = 'blah'
     v = ValidateCohort(self.cohort)
     try:
         v.validate_records(self.session, self.cohort)
     except:
         assert_true(False,
                     'validate_records should not raise an exception')
     else:
         assert_equal(self.cohort.validated, True)
         assert_equal(
             len(
                 self.session.query(WikiUserStore).filter(
                     WikiUserStore.validating_cohort ==
                     self.cohort.id).filter(WikiUserStore.valid).all()), 3)
         records = (self.session.query(WikiUserStore).filter(
             WikiUserStore.validating_cohort == self.cohort.id).filter(
                 WikiUserStore.valid.in_([False])).all())
         assert_equal(len(records), 1)
         assert_equal(records[0].reason_invalid, 'invalid project')
 def test_validate_cohorts_when_invalid_project_causes_exception(self, mock):
     '''
     Mocks normalize_project to force an exception when accessing
     an unexisting project database. This exception should not be
     forwarded.
     '''
     self.helper_reset_validation()
     self.cohort.validate_as_user_ids = False
     wikiusers = self.session.query(WikiUserStore).all()
     wikiusers[0].project = 'blah'
     v = ValidateCohort(self.cohort)
     try:
         v.validate_records(self.session, self.cohort)
     except Exception:
         assert_true(False, 'validate_records should not raise an exception')
     else:
         assert_equal(self.cohort.validated, True)
         assert_equal(len(
             self.session.query(WikiUserStore)
                 .filter(WikiUserStore.validating_cohort == self.cohort.id)
                 .filter(WikiUserStore.valid)
                 .all()
         ), 3)
         records = (self.session.query(WikiUserStore)
                    .filter(WikiUserStore.validating_cohort == self.cohort.id)
                    .filter(WikiUserStore.valid.in_([False]))
                    .all())
         assert_equal(len(records), 1)
         assert_equal(records[0].reason_invalid, 'invalid project')
Пример #5
0
    def test_validate_cohorts(self):
        self.helper_reset_validation()
        self.cohort.validate_as_user_ids = False
        self.session.commit()
        v = ValidateCohort(self.cohort)
        v.validate_records(self.session, self.cohort)

        assert_equal(self.cohort.validated, True)
        assert_equal(
            len(
                self.session.query(WikiUserStore).filter(
                    WikiUserStore.validating_cohort == self.cohort.id).filter(
                        WikiUserStore.valid).all()), 4)
    def test_validate_cohorts(self):
        self.helper_reset_validation()
        self.cohort.validate_as_user_ids = False
        self.session.commit()
        v = ValidateCohort(self.cohort)
        v.validate_records(self.session, self.cohort)

        assert_equal(self.cohort.validated, True)
        assert_equal(len(
            self.session.query(WikiUserStore)
                .filter(WikiUserStore.validating_cohort == self.cohort.id)
                .filter(WikiUserStore.valid)
                .all()
        ), 4)
    def test_small_cohort(self):
        cohort_upload = CohortUpload()
        cohort_upload.name.data = 'small_cohort'
        cohort_upload.project.data = mediawiki_project
        cohort_upload.records = [
            # two existing users
            {'raw_id_or_name': 'Editor test-specific-0', 'project': mediawiki_project},
            {'raw_id_or_name': 'Editor test-specific-1', 'project': mediawiki_project},
            # one invalid username
            {'raw_id_or_name': 'Nonexisting', 'project': mediawiki_project},
            # one user with invalid project
            {'raw_id_or_name': 'Nonexisting2', 'project': 'Nonexisting'},
        ]

        v = ValidateCohort.from_upload(cohort_upload, self.owner_user_id)
        v.task.delay(v).get()
        self.session.commit()

        assert_equal(self.session.query(WikiUserStore).filter(
            WikiUserStore.raw_id_or_name == 'Editor test-specific-0').one().valid,
            True
        )
        assert_equal(self.session.query(WikiUserStore).filter(
            WikiUserStore.raw_id_or_name == 'Editor test-specific-1').one().valid,
            True
        )
        assert_equal(self.session.query(WikiUserStore).filter(
            WikiUserStore.raw_id_or_name == 'Nonexisting').one().valid, False)
        assert_equal(self.session.query(WikiUserStore).filter(
            WikiUserStore.raw_id_or_name == 'Nonexisting2').one().valid, False)
Пример #8
0
def program_metrics_reports_request():
    """
    Renders a page that facilitates kicking off a new ProgramMetrics report
    """
    form = ProgramMetricsForm()

    if request.method == 'POST':
        form = ProgramMetricsForm.from_request(request)
        try:
            if not form.validate():
                flash('Please fix validation problems.', 'warning')

            else:
                form.parse_records()
                vc = ValidateCohort.from_upload(form, current_user.id)
                gm = RunProgramMetricsReport(vc.cohort_id,
                                             form.start_date.data,
                                             form.end_date.data,
                                             current_user.id)
                # Validate the cohort, and on success, call the
                # RunProgramMetricsReport task. No parameters are passed
                # from ValidateCohort to the report, so we are using
                # an immutable task signature in the link param.
                vc.task.apply_async([vc], link=gm.task.si(gm))
                return redirect(url_for('reports_index'))
        except Exception, e:
            app.logger.exception(str(e))
            flash('Server error while processing your request', 'error')
Пример #9
0
    def test_from_upload_exception(self):
        cohort_upload = CohortUpload()
        cohort_upload.name.data = 'small_cohort'
        cohort_upload.project.data = 'wiki'
        cohort_upload.records = [{'fake': 'dict'}]

        v = ValidateCohort.from_upload(cohort_upload, self.owner_user_id)
        assert_equal(v, None)
    def test_from_upload_exception(self):
        cohort_upload = CohortUpload()
        cohort_upload.name.data = 'small_cohort'
        cohort_upload.project.data = 'wiki'
        cohort_upload.records = [{'fake': 'dict'}]

        v = ValidateCohort.from_upload(cohort_upload, self.owner_user_id)
        assert_equal(v, None)
    def test_detail_by_name_after_validate(self):
        self.helper_reset_validation()

        # Set a fake validation_queue_key as we are running on sync mode
        self.cohort.validation_queue_key = 33
        self.session.commit()
        #executed validation synchronously
        vc = ValidateCohort(self.cohort)
        vc.validate_records(self.session, self.cohort)
        with cohort_service_set(app, self.cohort_service):
            response = self.app.get('/cohorts/detail/{0}'.format(self.cohort.name))
            parsed = json.loads(response.data)

        # note than it does not make sense to assert validation status
        # as that is retrieved directly from celery and celery was not used in this test
        assert_equal(parsed['validated_count'], 4)
        assert_equal(parsed['total_count'], 4)
Пример #12
0
    def test_detail_by_name_after_validate(self):
        self.helper_reset_validation()

        # Set a fake validation_queue_key as we are running in sync mode
        self.cohort.validation_queue_key = '33'
        self.session.add(self.cohort)
        self.session.commit()

        # executed validation synchronously
        vc = ValidateCohort(self.cohort)
        vc.validate_records(self.session, self.cohort)
        with cohort_service_set(app, self.cohort_service):
            response = self.app.get('/cohorts/detail/{0}'.format(
                self.cohort.name))
            parsed = json.loads(response.data)

        # note than it does not make sense to assert validation status
        # as that is retrieved directly from celery and celery was not used in this test
        assert_equal(parsed['validation']['validated_count'], 4)
        assert_equal(parsed['validation']['total_count'], 4)
Пример #13
0
    def test_validate_cohorts_with_invalid_wikiusers(self):
        self.helper_reset_validation()
        self.cohort.validate_as_user_ids = False
        wikiusers = self.session.query(WikiUserStore).all()
        wikiusers[0].project = 'blah'
        wikiusers[1].raw_id_or_name = 'blah'
        self.session.commit()
        v = ValidateCohort(self.cohort)
        v.validate_records(self.session, self.cohort)

        assert_equal(self.cohort.validated, True)
        assert_equal(
            len(
                self.session.query(WikiUserStore).filter(
                    WikiUserStore.validating_cohort == self.cohort.id).filter(
                        WikiUserStore.valid).all()), 2)
        assert_equal(
            len(
                self.session.query(WikiUserStore).filter(
                    WikiUserStore.validating_cohort == self.cohort.id).filter(
                        WikiUserStore.valid.in_([False])).all()), 2)
    def test_validate_cohorts_with_invalid_wikiusers(self):
        self.helper_reset_validation()
        self.cohort.validate_as_user_ids = False
        wikiusers = self.session.query(WikiUserStore).all()
        wikiusers[0].project = 'blah'
        wikiusers[1].raw_id_or_name = 'blah'
        self.session.commit()
        v = ValidateCohort(self.cohort)
        v.validate_records(self.session, self.cohort)

        assert_equal(self.cohort.validated, True)
        assert_equal(len(
            self.session.query(WikiUserStore)
                .filter(WikiUserStore.validating_cohort == self.cohort.id)
                .filter(WikiUserStore.valid)
                .all()
        ), 2)
        assert_equal(len(
            self.session.query(WikiUserStore)
                .filter(WikiUserStore.validating_cohort == self.cohort.id)
                .filter(WikiUserStore.valid.in_([False]))
                .all()
        ), 2)
def validate_cohort(cohort_id):
    name = None
    session = db.get_session()
    try:
        c = g.cohort_service.get_for_display(session, current_user.id, by_id=cohort_id)
        name = c.name
        # TODO we need some kind of global config that is not db specific
        vc = ValidateCohort(c)
        vc.task.delay(vc)
        return json_response(message='Validating cohort "{0}"'.format(name))
    except Unauthorized:
        return json_error('You are not allowed to access this cohort')
    except NoResultFound:
        return json_error('This cohort does not exist')
Пример #16
0
    def test_small_cohort(self):
        cohort_upload = CohortUpload()
        cohort_upload.name.data = 'small_cohort'
        cohort_upload.project.data = mediawiki_project
        cohort_upload.records = [
            # two existing users
            {
                'raw_id_or_name': 'Editor test-specific-0',
                'project': mediawiki_project
            },
            {
                'raw_id_or_name': 'Editor test-specific-1',
                'project': mediawiki_project
            },
            # one invalid username
            {
                'raw_id_or_name': 'Nonexisting',
                'project': mediawiki_project
            },
            # one user with invalid project
            {
                'raw_id_or_name': 'Nonexisting2',
                'project': 'Nonexisting'
            },
        ]

        v = ValidateCohort.from_upload(cohort_upload, self.owner_user_id)
        v.task.delay(v).get()
        self.session.commit()

        assert_equal(
            self.session.query(WikiUserStore).filter(
                WikiUserStore.raw_id_or_name ==
                'Editor test-specific-0').one().valid, True)
        assert_equal(
            self.session.query(WikiUserStore).filter(
                WikiUserStore.raw_id_or_name ==
                'Editor test-specific-1').one().valid, True)
        assert_equal(
            self.session.query(WikiUserStore).filter(
                WikiUserStore.raw_id_or_name == 'Nonexisting').one().valid,
            False)
        assert_equal(
            self.session.query(WikiUserStore).filter(
                WikiUserStore.raw_id_or_name == 'Nonexisting2').one().valid,
            False)
def cohort_upload():
    """ View for uploading and validating a new cohort via CSV """
    form = CohortUpload()

    if request.method == 'POST':
        form = CohortUpload.from_request(request)
        try:
            if not form.validate():
                flash('Please fix validation problems.', 'warning')

            else:
                form.parse_records()
                vc = ValidateCohort.from_upload(form, current_user.id)
                vc.task.delay(vc)
                return redirect('{0}#{1}'.format(
                    url_for('cohorts_index'),
                    vc.cohort_id
                ))
        except Exception, e:
            app.logger.exception(str(e))
            flash('Server error while processing your upload', 'error')
Пример #18
0
    def validate_cohort(self, filename):
        '''
        Given a cohort file with usernames all users but one should validate.
        It will mingle the name of the 1st user.

        Parameters:
            filename : Name of a file that contains a cohort with user names
                       test will search for file in tests/static/public folder
        '''

        names = self.create_users_from_file(filename)

        # establish ownership for this cohort otherwise things do not work
        owner_user = UserStore(username='******',
                               email='*****@*****.**')
        self.session.add(owner_user)
        self.session.commit()

        # creating here kind of like a cohortupload mock
        # flask forms do not lend themselves to easy mocking
        cohort_upload = MockCohort()
        cohort_upload.name = MockCohort()
        cohort_upload.name.data = 'testing-cohort'
        cohort_upload.description = MockCohort()
        cohort_upload.description.data = 'testing-cohort'
        cohort_upload.project = MockCohort()
        cohort_upload.project.data = mediawiki_project
        cohort_upload.validate_as_user_ids = MockCohort()
        cohort_upload.validate_as_user_ids.data = False
        cohort_upload.records = []

        # mingle the name of the first user user
        not_valid_editor_name = 'Mr Not Valid'
        names[0] = not_valid_editor_name

        for name in names:
            cohort_upload.records.append({
                'raw_id_or_name': name,
                'project': mediawiki_project,
            })

        # TODO clear session situation?
        # all operations need to happen on the scope of the same session
        # but this session passed in is going to be closed
        vc = ValidateCohort.from_upload(cohort_upload, owner_user.id,
                                        self.session)

        cohort = self.session.query(CohortStore).first()
        self.session.commit()
        vc.validate_records(self.session, cohort)

        # now we need to assert that all users but the first one validate
        assert_equal(
            len(
                self.session.query(WikiUserStore).filter(
                    WikiUserStore.validating_cohort == cohort.id).filter(
                        WikiUserStore.valid).all()),
            len(names) - 1)

        # retrieve the user that should not be valid, make sure it is not indeed
        wiki_user = self.session.query(WikiUserStore)\
            .filter(WikiUserStore.validating_cohort == cohort.id)\
            .filter(WikiUserStore.raw_id_or_name == not_valid_editor_name).one()

        assert_false(wiki_user.valid)
    def validate_cohort(self, filename):
        '''
        Given a cohort file with usernames all users but one should validate.
        It will mingle the name of the 1st user.

        Parameters:
            filename : Name of a file that contains a cohort with user names
                       test will search for file in tests/static/public folder
        '''

        names = self.create_users_from_file(filename)

        # establish ownership for this cohort otherwise things do not work
        owner_user = UserStore(username='******', email='*****@*****.**')
        self.session.add(owner_user)
        self.session.commit()

        # creating here kind of like a cohortupload mock
        # flask forms do not lend themselves to easy mocking
        cohort_upload = MockCohort()
        cohort_upload.name = MockCohort()
        cohort_upload.name.data = 'testing-cohort'
        cohort_upload.description = MockCohort()
        cohort_upload.description.data = 'testing-cohort'
        cohort_upload.project = MockCohort()
        cohort_upload.project.data = mediawiki_project
        cohort_upload.validate_as_user_ids = MockCohort()
        cohort_upload.validate_as_user_ids.data = False
        cohort_upload.records = []

        # mingle the name of the first user user
        not_valid_editor_name = 'Mr Not Valid'
        names[0] = not_valid_editor_name

        for name in names:
            cohort_upload.records.append({
                'raw_id_or_name'  : name,
                'project'   : mediawiki_project,
            })

        # TODO clear session situation?
        # all operations need to happen on the scope of the same session
        # but this session passed in is going to be closed
        vc = ValidateCohort.from_upload(cohort_upload, owner_user.id, self.session)

        cohort = self.session.query(CohortStore).first()
        self.session.commit()
        vc.validate_records(self.session, cohort)

        # now we need to assert that all users but the first one validate
        assert_equal(len(
            self.session.query(WikiUserStore)
                .filter(WikiUserStore.validating_cohort == cohort.id)
                .filter(WikiUserStore.valid)
                .all()
        ), len(names) - 1)

        # retrieve the user that should not be valid, make sure it is not indeed
        wiki_user = self.session.query(WikiUserStore)\
            .filter(WikiUserStore.validating_cohort == cohort.id)\
            .filter(WikiUserStore.raw_id_or_name == not_valid_editor_name).one()

        assert_false(wiki_user.valid)
Пример #20
0
 def test_repr(self):
     cohort = CohortStore(id=1)
     v = ValidateCohort(cohort)
     assert_equal(str(v), '<ValidateCohort("1")>')