예제 #1
0
    def test_get_project_jobs_for_non_pro_users(self):
        """Test JOB get project jobs works for non pro users."""
        AppFactory.create()
        jobs = get_project_jobs()

        err_msg = "There should be only 0 jobs"
        assert len(jobs) == 0, err_msg
예제 #2
0
    def test_get_project_jobs_for_non_pro_users(self):
        """Test JOB get project jobs works for non pro users."""
        AppFactory.create()
        jobs = get_project_jobs()

        err_msg = "There should be only 0 jobs"
        assert len(jobs) == 0, err_msg
예제 #3
0
 def test_autoimport_jobs(self):
     """Test JOB autoimport jobs works."""
     user = UserFactory.create(pro=True)
     AppFactory.create(owner=user)
     jobs = get_autoimport_jobs()
     msg = "There should be 0 jobs."
     assert len(jobs) == 0, msg
예제 #4
0
    def test_n_featured_returns_featured(self):
        """Test CACHE PROJECTS _n_featured returns number of featured projects"""
        AppFactory.create(featured=True)

        number_of_featured = cached_apps._n_featured()

        assert number_of_featured == 1, number_of_featured
예제 #5
0
    def test_blogpost_update_errors(self):
        """Test blogposts update for non existing projects raises errors"""
        self.register()
        user = user_repo.get(1)
        app1 = AppFactory.create(owner=user)
        app2 = AppFactory.create(owner=user)
        blogpost = BlogpostFactory.create(app=app1, body='body')

        # To a non-existing app
        url = "/app/non-existing-app/%s/update" % blogpost.id
        res = self.app.post(url, data={'title':'new title', 'body':'body'},
                            follow_redirects=True)
        assert res.status_code == 404, res.status_code

        # To a non-existing post
        url = "/app/%s/999999/update" % app1.short_name
        res = self.app.post(url, data={'title':'new title', 'body':'body'},
                            follow_redirects=True)
        assert res.status_code == 404, res.status_code

        # To an existing post but with a project in the URL it does not belong to
        url = "/app/%s/%s/update" % (app2.short_name, blogpost.id)
        res = self.app.post(url, data={'title':'new title', 'body':'body'},
                            follow_redirects=True)
        assert res.status_code == 404, res.status_code
예제 #6
0
    def test_get_by_returns_none_if_no_project(self):
        """Test get_by returns None if no project matches the query"""

        AppFactory.create(name='My Project', short_name='myproject')

        project = self.project_repo.get_by(name='no_name')

        assert project is None, project
예제 #7
0
    def test_get_draft_not_returns_hidden_apps(self):
        """Test CACHE PROJECTS get_draft does not return hidden projects"""

        AppFactory.create(info={}, hidden=1)

        drafts = cached_apps.get_draft()

        assert len(drafts) is 0, drafts
예제 #8
0
    def test_get_by_returns_none_if_no_project(self):
        """Test get_by returns None if no project matches the query"""

        AppFactory.create(name='My Project', short_name='myproject')

        project = self.project_repo.get_by(name='no_name')

        assert project is None, project
예제 #9
0
    def test_get_featured(self):
        """Test CACHE PROJECTS get_featured returns featured projects"""

        AppFactory.create(featured=True)

        featured = cached_apps.get_featured()

        assert len(featured) is 1, featured
예제 #10
0
    def test_n_draft_with_drafts(self):
        """Test CACHE PROJECTS n_draft returns 2 if there are 2 draft projects"""
        # Here, we are suposing that a project is draft iff has no presenter AND has no tasks

        AppFactory.create_batch(2, info={})

        number_of_drafts = cached_apps.n_draft()

        assert number_of_drafts == 2, number_of_drafts
예제 #11
0
    def test_filter_by_no_matches(self):
        """Test filter_by returns an empty list if no projects match the query"""

        AppFactory.create(name='My Project', short_name='myproject')

        retrieved_projects = self.project_repo.filter_by(name='no_name')

        assert isinstance(retrieved_projects, list)
        assert len(retrieved_projects) == 0, retrieved_projects
예제 #12
0
    def test_filter_by_no_matches(self):
        """Test filter_by returns an empty list if no projects match the query"""

        AppFactory.create(name='My Project', short_name='myproject')

        retrieved_projects = self.project_repo.filter_by(name='no_name')

        assert isinstance(retrieved_projects, list)
        assert len(retrieved_projects) == 0, retrieved_projects
예제 #13
0
    def test_get_from_pro_user_projects_no_projects(self):
        """Test CACHE PROJECTS get_from_pro_user returns empty list if no projects
        with 'pro' owners"""
        pro_user = UserFactory.create(pro=True)
        AppFactory.create()

        pro_owned_projects = cached_apps.get_from_pro_user()

        assert pro_owned_projects == [], pro_owned_projects
예제 #14
0
    def test_n_draft_with_drafts(self):
        """Test CACHE PROJECTS _n_draft returns 2 if there are 2 draft projects"""
        # Here, we are suposing that a project is draft iff has no presenter AND has no tasks

        AppFactory.create_batch(2, info={})

        number_of_drafts = cached_apps._n_draft()

        assert number_of_drafts == 2, number_of_drafts
예제 #15
0
    def test_get_draft(self):
        """Test CACHE PROJECTS get_draft returns draft_projects"""
        # Here, we are suposing that a project is draft iff has no presenter AND has no tasks

        AppFactory.create(info={})

        drafts = cached_apps.get_draft()

        assert len(drafts) is 1, drafts
예제 #16
0
    def test_get_featured_only_returns_featured(self):
        """Test CACHE PROJECTS get_featured returns only featured projects"""

        featured_app = AppFactory.create(featured=True)
        non_featured_app = AppFactory.create()

        featured = cached_apps.get_featured()

        assert len(featured) is 1, featured
예제 #17
0
    def test_get_only_returns_category_projects(self):
        """Test CACHE PROJECTS get returns only projects from required category"""

        project = self.create_app_with_tasks(1, 0)
        #create a non published project too
        AppFactory.create()

        projects = cached_apps.get(project.category.short_name)

        assert len(projects) is 1, projects
예제 #18
0
    def test_get_featured_front_page_only_returns_featured(self):
        """Test CACHE PROJECTS get_featured_front_page returns only featured projects"""

        featured_app = AppFactory.create()
        non_featured_app = AppFactory.create()
        FeaturedFactory.create(app=featured_app)

        featured = cached_apps.get_featured_front_page()

        assert len(featured) is 1, featured
예제 #19
0
    def test_get_from_pro_users_returns_required_fields(self):
        """Test CACHE PROJECTS get_from_pro_user returns required fields"""
        pro_user = UserFactory.create(pro=True)
        AppFactory.create(owner=pro_user)
        fields = ('id', 'short_name')

        pro_owned_projects = cached_apps.get_from_pro_user()

        for field in fields:
            assert field in pro_owned_projects[0].keys(), field
예제 #20
0
    def test_get_draft_not_returns_published_apps(self):
        """Test CACHE PROJECTS get_draft does not return projects with either tasks or a presenter (REVIEW DEFINITION OF A DRAFT PROJECT REQUIRED)"""

        app_no_presenter = AppFactory.create(info={})
        TaskFactory.create(app=app_no_presenter)
        app_no_task = AppFactory.create()

        drafts = cached_apps.get_draft()

        assert len(drafts) is 0, drafts
예제 #21
0
    def test_get_not_returns_draft_apps(self):
        """Test CACHE PROJECTS get does not return draft (non-published) projects"""

        project = self.create_app_with_contributors(1, 0)
        # Create a project wothout presenter
        AppFactory.create(info={}, category=project.category)

        projects = cached_apps.get(project.category.short_name)

        assert len(projects) is 1, projects
예제 #22
0
    def test_n_count_with_published_projects(self):
        """Test CACHE PROJECTS n_count returns the number of published projects
        of a given category"""
        project = self.create_app_with_tasks(1, 0)
        #create a non published project too
        AppFactory.create()

        n_projects = cached_apps.n_count(project.category.short_name)

        assert n_projects == 1, n_projects
예제 #23
0
    def test_get_from_pro_user_projects(self):
        """Test CACHE PROJECTS get_from_pro_user returns list of projects with
        'pro' owners only"""
        pro_user = UserFactory.create(pro=True)
        AppFactory.create()
        pro_project = AppFactory.create(owner=pro_user)

        pro_owned_projects = cached_apps.get_from_pro_user()

        assert len(pro_owned_projects) is 1, len(pro_owned_projects)
        assert pro_owned_projects[0]['short_name'] == pro_project.short_name
예제 #24
0
    def test_hidden_apps_only_returns_hidden(self):
        """Test CACHE USERS hidden_apps does not return draft (even hidden)
        or another user's hidden projects"""
        user = UserFactory.create()
        another_user_hidden_project = AppFactory.create(hidden=1)
        TaskFactory.create(app=another_user_hidden_project)
        hidden_draft_project = AppFactory.create(owner=user, hidden=1, info={})

        hidden_projects = cached_users.hidden_apps(user.id)

        assert len(hidden_projects) == 0, hidden_projects
예제 #25
0
    def test_draft_apps_only_returns_drafts(self):
        """Test CACHE USERS draft_apps does not return any apps that are not draft
        (published) or drafts that belong to another user"""
        user = UserFactory.create()
        published_project = AppFactory.create(owner=user)
        TaskFactory.create(app=published_project)
        other_users_draft_project = AppFactory.create(info={})

        draft_projects = cached_users.draft_apps(user.id)

        assert len(draft_projects) == 0, draft_projects
예제 #26
0
    def test_draft_apps_only_returns_drafts(self):
        """Test CACHE USERS draft_apps does not return any apps that are not draft
        (published) or drafts that belong to another user"""
        user = UserFactory.create()
        published_project = AppFactory.create(owner=user)
        TaskFactory.create(app=published_project)
        other_users_draft_project = AppFactory.create(info={})

        draft_projects = cached_users.draft_apps(user.id)

        assert len(draft_projects) == 0, draft_projects
예제 #27
0
    def test_filter_by_one_condition(self):
        """Test filter_by returns a list of projects that meet the filtering
        condition"""

        AppFactory.create_batch(3, allow_anonymous_contributors=False)
        should_be_missing = AppFactory.create(allow_anonymous_contributors=True)

        retrieved_projects = self.project_repo.filter_by(allow_anonymous_contributors=False)

        assert len(retrieved_projects) == 3, retrieved_projects
        assert should_be_missing not in retrieved_projects, retrieved_projects
예제 #28
0
    def test_hidden_apps_only_returns_hidden(self):
        """Test CACHE USERS hidden_apps does not return draft (even hidden)
        or another user's hidden projects"""
        user = UserFactory.create()
        another_user_hidden_project = AppFactory.create(hidden=1)
        TaskFactory.create(app=another_user_hidden_project)
        hidden_draft_project = AppFactory.create(owner=user, hidden=1, info={})

        hidden_projects = cached_users.hidden_apps(user.id)

        assert len(hidden_projects) == 0, hidden_projects
예제 #29
0
    def test_filter_by_multiple_conditions(self):
        """Test filter_by supports multiple-condition queries"""

        AppFactory.create_batch(2, allow_anonymous_contributors=False, hidden=0)
        project = AppFactory.create(allow_anonymous_contributors=False, hidden=1)

        retrieved_projects = self.project_repo.filter_by(
                                            allow_anonymous_contributors=False,
                                            hidden=1)

        assert len(retrieved_projects) == 1, retrieved_projects
        assert project in retrieved_projects, retrieved_projects
예제 #30
0
    def test_published_apps_only_returns_published(self):
        """Test CACHE USERS published_apps does not return hidden, draft
        or another user's projects"""
        user = UserFactory.create()
        another_user_published_project = AppFactory.create()
        TaskFactory.create(app=another_user_published_project)
        draft_project = AppFactory.create(info={})
        hidden_project = AppFactory.create(owner=user, hidden=1)
        TaskFactory.create(app=hidden_project)

        apps_published = cached_users.published_apps(user.id)

        assert len(apps_published) == 0, apps_published
예제 #31
0
    def test_published_apps_only_returns_published(self):
        """Test CACHE USERS published_apps does not return hidden, draft
        or another user's projects"""
        user = UserFactory.create()
        another_user_published_project = AppFactory.create()
        TaskFactory.create(app=another_user_published_project)
        draft_project = AppFactory.create(info={})
        hidden_project = AppFactory.create(owner=user, hidden=1)
        TaskFactory.create(app=hidden_project)

        apps_published = cached_users.published_apps(user.id)

        assert len(apps_published) == 0, apps_published
예제 #32
0
    def test_filter_by_one_condition(self):
        """Test filter_by returns a list of projects that meet the filtering
        condition"""

        AppFactory.create_batch(3, allow_anonymous_contributors=False)
        should_be_missing = AppFactory.create(
            allow_anonymous_contributors=True)

        retrieved_projects = self.project_repo.filter_by(
            allow_anonymous_contributors=False)

        assert len(retrieved_projects) == 3, retrieved_projects
        assert should_be_missing not in retrieved_projects, retrieved_projects
예제 #33
0
    def test_filter_by_limit_offset(self):
        """Test that filter_by supports limit and offset options"""

        AppFactory.create_batch(4)
        all_projects = self.project_repo.filter_by()

        first_two = self.project_repo.filter_by(limit=2)
        last_two = self.project_repo.filter_by(limit=2, offset=2)

        assert len(first_two) == 2, first_two
        assert len(last_two) == 2, last_two
        assert first_two == all_projects[:2]
        assert last_two == all_projects[2:]
예제 #34
0
    def test_apps_contributed_contributions(self):
        """Test CACHE USERS apps_contributed returns a list of projects that has
        contributed to"""
        user = UserFactory.create()
        app_contributed = AppFactory.create()
        task = TaskFactory.create(app=app_contributed)
        TaskRunFactory.create(task=task, user=user)
        another_app = AppFactory.create()

        apps_contributed = cached_users.apps_contributed(user.id)

        assert len(apps_contributed) == 1
        assert apps_contributed[0]['short_name'] == app_contributed.short_name, apps_contributed
예제 #35
0
    def test_filter_by_limit_offset(self):
        """Test that filter_by supports limit and offset options"""

        AppFactory.create_batch(4)
        all_projects = self.project_repo.filter_by()

        first_two = self.project_repo.filter_by(limit=2)
        last_two = self.project_repo.filter_by(limit=2, offset=2)

        assert len(first_two) == 2, first_two
        assert len(last_two) == 2, last_two
        assert first_two == all_projects[:2]
        assert last_two == all_projects[2:]
예제 #36
0
    def test_filter_by_multiple_conditions(self):
        """Test filter_by supports multiple-condition queries"""

        AppFactory.create_batch(2,
                                allow_anonymous_contributors=False,
                                hidden=0)
        project = AppFactory.create(allow_anonymous_contributors=False,
                                    hidden=1)

        retrieved_projects = self.project_repo.filter_by(
            allow_anonymous_contributors=False, hidden=1)

        assert len(retrieved_projects) == 1, retrieved_projects
        assert project in retrieved_projects, retrieved_projects
예제 #37
0
    def test_apps_contributed_contributions(self):
        """Test CACHE USERS apps_contributed returns a list of projects that has
        contributed to"""
        user = UserFactory.create()
        app_contributed = AppFactory.create()
        task = TaskFactory.create(app=app_contributed)
        TaskRunFactory.create(task=task, user=user)
        another_app = AppFactory.create()

        apps_contributed = cached_users.apps_contributed(user.id)

        assert len(apps_contributed) == 1
        assert apps_contributed[0][
            'short_name'] == app_contributed.short_name, apps_contributed
예제 #38
0
    def test_get_featured_returns_required_fields(self):
        """Test CACHE PROJECTS get_featured returns the required info
        about each featured project"""

        fields = ('id', 'name', 'short_name', 'info', 'created', 'description',
                  'last_activity', 'last_activity_raw', 'overall_progress',
                   'n_tasks', 'n_volunteers', 'owner', 'info')

        AppFactory.create(featured=True)

        featured = cached_apps.get_featured()[0]

        for field in fields:
            assert featured.has_key(field), "%s not in app info" % field
예제 #39
0
    def test_anonymous_user_read_given_blogpost(self):
        """Test anonymous users can read a given blogpost"""

        app = AppFactory.create()
        blogpost = BlogpostFactory.create(app=app)

        assert_not_raises(Exception, getattr(require, 'blogpost').read, blogpost)
예제 #40
0
    def test_admin_can_delete(self):
        """Test an admin can delete a project"""
        owner = UserFactory.build_batch(2)[1]
        project = AppFactory.create(hidden=1, owner=owner)

        assert project.owner.id != self.mock_admin.id, project.owner
        assert_not_raises(Exception, getattr(require, 'app').delete, project)
예제 #41
0
    def test_anonymous_user_create_given_blogpost(self):
        """Test anonymous users cannot create a given blogpost"""

        app = AppFactory.create()
        blogpost = BlogpostFactory.build(app=app, owner=None)

        assert_raises(Unauthorized, getattr(require, 'blogpost').create, blogpost)
예제 #42
0
    def test_anonymous_user_read_given_blogpost_hidden_app(self):
        """Test anonymous users cannot read a given blogpost of a hidden project"""

        app = AppFactory.create(hidden=1)
        blogpost = BlogpostFactory.create(app=app)

        assert_raises(Unauthorized, getattr(require, 'blogpost').read, blogpost)
예제 #43
0
    def test_app_update_attributes(self):
        """Test Auditlog API project update attributes works."""
        app = AppFactory.create()

        data = {
            'name': 'New Name',
            'short_name': 'new_short_name',
            'description': 'new_description',
            'long_description': 'new_long_description',
            'allow_anonymous_contributors': 'False',
        }
        attributes = data.keys()
        url = '/api/app/%s?api_key=%s' % (app.id, app.owner.api_key)
        self.app.put(url, data=json.dumps(data))
        logs = auditlog_repo.filter_by(app_id=app.id)

        assert len(logs) == 5, logs
        for log in logs:
            assert log.user_id == app.owner_id, log.user_id
            assert log.user_name == app.owner.name, log.user_name
            assert log.app_short_name == app.short_name, log.app_short_name
            assert log.action == 'update', log.action
            assert log.caller == 'api', log.caller
            assert log.attribute in attributes, log.attribute
            msg = "%s != %s" % (data[log.attribute], log.new_value)
            assert data[log.attribute] == log.new_value, msg
예제 #44
0
    def test_get_query_with_api_key(self):
        """ Test API GET query with an API-KEY"""
        users = UserFactory.create_batch(3)
        app = AppFactory.create(owner=users[0], info={'total': 150})
        task = TaskFactory.create(app=app, info={'url': 'my url'})
        taskrun = TaskRunFactory.create(task=task, user=users[0],
                                        info={'answer': 'annakarenina'})
        for endpoint in self.endpoints:
            url = '/api/' + endpoint + '?api_key=' + users[1].api_key
            res = self.app.get(url)
            data = json.loads(res.data)

            if endpoint == 'app':
                assert len(data) == 1, data
                app = data[0]
                assert app['info']['total'] == 150, data
                assert res.mimetype == 'application/json', res

            if endpoint == 'task':
                assert len(data) == 1, data
                task = data[0]
                assert task['info']['url'] == 'my url', data
                assert res.mimetype == 'application/json', res

            if endpoint == 'taskrun':
                assert len(data) == 1, data
                taskrun = data[0]
                assert taskrun['info']['answer'] == 'annakarenina', data
                assert res.mimetype == 'application/json', res

            if endpoint == 'user':
                assert len(data) == 3, data
                user = data[0]
                assert user['name'] == 'user1', data
                assert res.mimetype == 'application/json', res
예제 #45
0
    def test_taskrun_updates_task_state(self, mock_request):
        """Test API TaskRun POST updates task state"""
        app = AppFactory.create()
        task = TaskFactory.create(app=app, n_answers=2)
        url = '/api/taskrun?api_key=%s' % app.owner.api_key

        # Post first taskrun
        data = dict(
            app_id=task.app_id,
            task_id=task.id,
            user_id=app.owner.id,
            info='my task result')
        datajson = json.dumps(data)
        tmp = self.app.post(url, data=datajson)
        r_taskrun = json.loads(tmp.data)

        assert tmp.status_code == 200, r_taskrun

        err_msg = "Task state should be different from completed"
        assert task.state == 'ongoing', err_msg

        # Post second taskrun
        mock_request.remote_addr = '127.0.0.0'
        url = '/api/taskrun'
        data = dict(
            app_id=task.app_id,
            task_id=task.id,
            info='my task result anon')
        datajson = json.dumps(data)
        tmp = self.app.post(url, data=datajson)
        r_taskrun = json.loads(tmp.data)

        assert tmp.status_code == 200, r_taskrun
        err_msg = "Task state should be equal to completed"
        assert task.state == 'completed', err_msg
예제 #46
0
 def test_warn_project_owner_limits(self):
     """Test JOB email gets at most 25 projects."""
     from pybossa.core import mail
     # Create 50 projects with old updated dates
     date = '2010-10-22T11:02:00.000000'
     apps = []
     for i in range(0, 50):
         apps.append(AppFactory.create(updated=date))
     # The first day that we run the job only 25 emails should be sent
     with mail.record_messages() as outbox:
         warn_old_project_owners()
         err_msg = "There should be only 25 emails."
         assert len(outbox) == 25, err_msg
     # The second day that we run the job only 25 emails should be sent
     with mail.record_messages() as outbox:
         warn_old_project_owners()
         err_msg = ("There should be only 25 emails, but there are %s."
                    % len(outbox))
         assert len(outbox) == 25, err_msg
     # The third day that we run the job only 0 emails should be sent
     # as the previous projects have been already contacted.
     with mail.record_messages() as outbox:
         warn_old_project_owners()
         err_msg = "There should be only 0 emails."
         assert len(outbox) == 0, err_msg
예제 #47
0
    def test_browse_tasks_returns_pct_status(self):
        """Test CACHE PROJECTS browse_tasks returns also the completion
        percentage of each task"""

        project = AppFactory.create()
        task = TaskFactory.create( app=project, info={}, n_answers=4)

        cached_task = cached_apps.browse_tasks(project.id)[0]
        # 0 if no task runs
        assert cached_task.get('pct_status') == 0, cached_task.get('pct_status')

        TaskRunFactory.create(task=task)
        cached_task = cached_apps.browse_tasks(project.id)[0]
        # Gets updated with new task runs
        assert cached_task.get('pct_status') == 0.25, cached_task.get('pct_status')

        TaskRunFactory.create_batch(3, task=task)
        cached_task = cached_apps.browse_tasks(project.id)[0]
        # To a maximum of 1
        assert cached_task.get('pct_status') == 1.0, cached_task.get('pct_status')

        TaskRunFactory.create(task=task)
        cached_task = cached_apps.browse_tasks(project.id)[0]
        # And it does not go over 1 (that is 100%!!)
        assert cached_task.get('pct_status') == 1.0, cached_task.get('pct_status')
예제 #48
0
    def test_owner_can_delete(self):
        """Test owners can delete a project"""
        owner = UserFactory.build_batch(2)[1]
        project = AppFactory.create(owner=owner)

        assert project.owner.id == self.mock_authenticated.id, project.owner
        assert_not_raises(Exception, getattr(require, 'app').delete, project)
예제 #49
0
    def test_user_progress_anonymous(self):
        """Test API userprogress as anonymous works"""
        user = UserFactory.create()
        app = AppFactory.create(owner=user)
        tasks = TaskFactory.create_batch(2, app=app)
        taskruns = []
        for task in tasks:
            taskruns.extend(AnonymousTaskRunFactory.create_batch(2, task=task))

        res = self.app.get('/api/app/1/userprogress', follow_redirects=True)
        data = json.loads(res.data)

        error_msg = "The reported total number of tasks is wrong"
        assert len(tasks) == data['total'], error_msg

        error_msg = "The reported number of done tasks is wrong"
        assert len(taskruns) == data['done'], data

        # Add a new TaskRun and check again
        taskrun = AnonymousTaskRunFactory.create(task=tasks[0],
                                                 info={'answer': u'hello'})

        res = self.app.get('/api/app/1/userprogress', follow_redirects=True)
        data = json.loads(res.data)
        error_msg = "The reported total number of tasks is wrong"
        assert len(tasks) == data['total'], error_msg

        error_msg = "Number of done tasks is wrong: %s" % len(taskruns)
        assert len(taskruns) + 1 == data['done'], error_msg
예제 #50
0
    def test_no_more_tasks(self):
        """Test that a users gets always tasks"""
        owner = UserFactory.create()
        app = AppFactory.create(owner=owner, short_name='egil', name='egil',
                  description='egil')

        app_id = app.id

        for i in range(20):
            task = TaskFactory.create(app=app, info={'i': i}, n_answers=10)

        tasks = db.session.query(Task).filter_by(app_id=app.id).limit(11).all()
        for t in tasks[0:10]:
            for x in range(10):
                self._add_task_run(app, t)

        assert tasks[0].n_answers == 10

        url = 'api/app/%s/newtask' % app_id
        res = self.app.get(url)
        data = json.loads(res.data)

        err_msg = "User should get a task"
        assert 'app_id' in data.keys(), err_msg
        assert data['app_id'] == app_id, err_msg
        assert data['id'] == tasks[10].id, err_msg
예제 #51
0
    def test_anonymous_02_gets_different_tasks(self):
        """ Test SCHED newtask returns N different Tasks for the Anonymous User"""
        assigned_tasks = []
        # Get a Task until scheduler returns None
        project = AppFactory.create()
        tasks = TaskFactory.create_batch(3, app=project)
        res = self.app.get('api/app/%s/newtask' %project.id)
        data = json.loads(res.data)
        while data.get('info') is not None:
            # Save the assigned task
            assigned_tasks.append(data)

            task = db.session.query(Task).get(data['id'])
            # Submit an Answer for the assigned task
            tr = AnonymousTaskRunFactory.create(app=project, task=task)
            res = self.app.get('api/app/%s/newtask' %project.id)
            data = json.loads(res.data)

        # Check if we received the same number of tasks that the available ones
        assert len(assigned_tasks) == len(tasks), len(assigned_tasks)
        # Check if all the assigned Task.id are equal to the available ones
        err_msg = "Assigned Task not found in DB Tasks"
        for at in assigned_tasks:
            assert self.is_task(at['id'], tasks), err_msg
        # Check that there are no duplicated tasks
        err_msg = "One Assigned Task is duplicated"
        for at in assigned_tasks:
            assert self.is_unique(at['id'], assigned_tasks), err_msg
예제 #52
0
    def test_app_auditlog_autoimporter_delete(self):
        self.register()
        owner = user_repo.get(1)
        autoimporter = {'type': 'csv', 'csv_url': 'http://fakeurl.com'}
        app = AppFactory.create(owner=owner,
                                info={'autoimporter': autoimporter})
        short_name = app.short_name

        attribute = 'autoimporter'

        old_value = json.dumps(autoimporter)

        new_value = 'Nothing'

        url = "/app/%s/tasks/autoimporter/delete" % short_name
        self.app.post(url, data={}, follow_redirects=True)

        logs = auditlog_repo.filter_by(app_short_name=short_name)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == attribute, log.attribute
            assert log.old_value == old_value, log.old_value
            assert log.new_value == new_value, log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'delete', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id
예제 #53
0
    def test_user_progress_anonymous(self):
        """Test API userprogress as anonymous works"""
        user = UserFactory.create()
        app = AppFactory.create(owner=user)
        tasks = TaskFactory.create_batch(2, app=app)
        for task in tasks:
            taskruns = AnonymousTaskRunFactory.create_batch(2, task=task)
        taskruns = db.session.query(TaskRun)\
                     .filter(TaskRun.app_id == app.id)\
                     .filter(TaskRun.user_ip == '127.0.0.1')\
                     .all()

        res = self.app.get('/api/app/1/userprogress', follow_redirects=True)
        data = json.loads(res.data)

        error_msg = "The reported total number of tasks is wrong"
        assert len(tasks) == data['total'], error_msg

        error_msg = "The reported number of done tasks is wrong"
        assert len(taskruns) == data['done'], data

        # Add a new TaskRun and check again
        taskrun = AnonymousTaskRunFactory.create(task=tasks[0], info={'answer': u'hello'})

        res = self.app.get('/api/app/1/userprogress', follow_redirects=True)
        data = json.loads(res.data)
        error_msg = "The reported total number of tasks is wrong"
        assert len(tasks) == data['total'], error_msg

        error_msg = "Number of done tasks is wrong: %s" % len(taskruns)
        assert len(taskruns) + 1 == data['done'], error_msg
예제 #54
0
 def test_get_non_updated_apps_returns_one_project(self):
     """Test JOB get non updated returns one project."""
     app = AppFactory.create(updated='2010-10-22T11:02:00.000000')
     apps = get_non_updated_apps()
     err_msg = "There should be one outdated project."
     assert len(apps) == 1, err_msg
     assert apps[0].name == app.name, err_msg
예제 #55
0
    def test_blogposts_get_all_with_hidden_app(self):
        """Test blogpost GET does not show hidden projects"""
        self.register()
        admin = user_repo.get(1)
        self.signout()
        self.register(name='user', email='*****@*****.**')
        user = user_repo.get(2)
        app = AppFactory.create(owner=user, hidden=1)
        blogpost = BlogpostFactory.create(app=app, title='title')

        url = "/app/%s/blog" % app.short_name

        # As app owner
        res = self.app.get(url, follow_redirects=True)
        assert res.status_code == 200, res.status_code
        assert 'title' in res.data

        # As authenticated
        self.signout()
        self.register(name='notowner', email='*****@*****.**')
        res = self.app.get(url, follow_redirects=True)
        assert res.status_code == 403, res.status_code

        # As anonymous
        self.signout()
        res = self.app.get(url, follow_redirects=True)
        assert res.status_code == 401, res.status_code

        # As admin
        self.signin()
        res = self.app.get(url, follow_redirects=True)
        assert res.status_code == 200, res.status_code
        assert 'title' in res.data
예제 #56
0
    def test_taskrun_post_with_bad_data(self):
        """Test API TaskRun error messages."""
        app = AppFactory.create()
        task = TaskFactory.create(app=app)
        app_id = app.id
        task_run = dict(app_id=app.id, task_id=task.id, info='my task result')
        url = '/api/taskrun?api_key=%s' % app.owner.api_key

        # POST with not JSON data
        res = self.app.post(url, data=task_run)
        err = json.loads(res.data)
        assert res.status_code == 415, err
        assert err['status'] == 'failed', err
        assert err['target'] == 'taskrun', err
        assert err['action'] == 'POST', err
        assert err['exception_cls'] == 'ValueError', err

        # POST with not allowed args
        res = self.app.post(url + '&foo=bar', data=task_run)
        err = json.loads(res.data)
        assert res.status_code == 415, err
        assert err['status'] == 'failed', err
        assert err['target'] == 'taskrun', err
        assert err['action'] == 'POST', err
        assert err['exception_cls'] == 'AttributeError', err

        # POST with fake data
        task_run['wrongfield'] = 13
        res = self.app.post(url, data=json.dumps(task_run))
        err = json.loads(res.data)
        assert res.status_code == 415, err
        assert err['status'] == 'failed', err
        assert err['target'] == 'taskrun', err
        assert err['action'] == 'POST', err
        assert err['exception_cls'] == 'TypeError', err
예제 #57
0
    def test_owners_can_read_given_hidden(self):
        """Test the owner of a project can read it despite being hidden"""
        owner = UserFactory.build_batch(2)[1]
        project = AppFactory.create(hidden=1, owner=owner)

        assert project.owner.id == self.mock_authenticated.id, project.owner
        assert_not_raises(Exception, getattr(require, 'app').read, project)
예제 #58
0
    def test_query_taskrun(self):
        """Test API query for taskrun with params works"""
        app = AppFactory.create()
        TaskRunFactory.create_batch(10, app=app)
        # Test for real field
        res = self.app.get("/api/taskrun?app_id=1")
        data = json.loads(res.data)
        # Should return one result
        assert len(data) == 10, data
        # Correct result
        assert data[0]['app_id'] == 1, data

        # Valid field but wrong value
        res = self.app.get("/api/taskrun?app_id=99999999")
        data = json.loads(res.data)
        assert len(data) == 0, data

        # Multiple fields
        res = self.app.get('/api/taskrun?app_id=1&task_id=1')
        data = json.loads(res.data)
        # One result
        assert len(data) == 1, data
        # Correct result
        assert data[0]['app_id'] == 1, data
        assert data[0]['task_id'] == 1, data

        # Limits
        res = self.app.get("/api/taskrun?app_id=1&limit=5")
        data = json.loads(res.data)
        for item in data:
            assert item['app_id'] == 1, item
        assert len(data) == 5, data
예제 #59
0
    def test_admin_can_read_given_hidden(self):
        """Test an admin can read a project despite being hidden"""
        owner = UserFactory.build_batch(2)[1]
        project = AppFactory.create(hidden=1, owner=owner)

        assert project.owner.id != self.mock_admin.id, project.owner
        assert_not_raises(Exception, getattr(require, 'app').read, project)