Esempio n. 1
0
    def test_authenticated_user_read(self):
        """Test authenticated user can read any taskrun"""

        with self.flask_app.test_request_context('/'):
            self.configure_fixtures()
            anonymous_taskrun = TaskRun(app_id=self.app.id,
                                        task_id=self.task.id,
                                        user_ip='127.0.0.0',
                                        info="some taskrun info")
            other_users_taskrun = TaskRun(app_id=self.app.id,
                                          task_id=self.task.id,
                                          user_id=self.root.id,
                                          info="a different taskrun info")
            own_taskrun = TaskRun(app_id=self.app.id,
                                  task_id=self.task.id,
                                  user_id=self.mock_authenticated.id,
                                  info="another taskrun info")

            assert_not_raises(Exception,
                          getattr(require, 'taskrun').read,
                          anonymous_taskrun)
            assert_not_raises(Exception,
                          getattr(require, 'taskrun').read,
                          other_users_taskrun)
            assert_not_raises(Exception,
                          getattr(require, 'taskrun').read,
                          own_taskrun)
    def test_task_run_errors(self):
        """Test TASK_RUN model errors."""
        user = User(email_addr="*****@*****.**",
                    name="johndoe",
                    fullname="John Doe",
                    locale="en")
        db.session.add(user)
        db.session.commit()

        user = db.session.query(User).first()
        category = Category(name='cat', short_name='cat', description='cat')
        project = Project(name='Application',
                          short_name='app',
                          description='desc',
                          owner_id=user.id,
                          category=category)
        db.session.add(project)
        db.session.commit()

        task = Task(project_id=project.id)
        db.session.add(task)
        db.session.commit()

        task_run = TaskRun(project_id=None, task_id=task.id)
        db.session.add(task_run)
        assert_raises(IntegrityError, db.session.commit)
        db.session.rollback()

        task_run = TaskRun(project_id=project.id, task_id=None)
        db.session.add(task_run)
        assert_raises(IntegrityError, db.session.commit)
        db.session.rollback()
Esempio n. 3
0
    def test_anonymous_user_create_repeated_taskrun(self):
        """Test anonymous user cannot create a taskrun for a task to which
        he has previously posted a taskrun"""

        with self.flask_app.test_request_context('/'):
            self.configure_fixtures()
            taskrun1 = TaskRun(app_id=self.app.id,
                               task_id=self.task.id,
                               user_ip='127.0.0.0',
                               info="some taskrun info")
            db.session.add(taskrun1)
            db.session.commit()
            taskrun2 = TaskRun(app_id=self.app.id,
                               task_id=self.task.id,
                               user_ip='127.0.0.0',
                               info="a different taskrun info")
            assert_raises(Forbidden,
                        getattr(require, 'taskrun').create,
                        taskrun2)

            # But the user can still create taskruns for different tasks
            task2 = Task(app_id=self.app.id, state='0', n_answers=10)
            task2.app = self.app
            db.session.add(task2)
            db.session.commit()
            taskrun3 = TaskRun(app_id=self.app.id,
                               task_id=task2.id,
                               user_ip='127.0.0.0',
                               info="some taskrun info")
            assert_not_raises(Exception,
                          getattr(require, 'taskrun').create,
                          taskrun3)
    def test_user_03_respects_limit_tasks(self):
        """ Test SCHED newtask respects the limit of 30 TaskRuns per Task"""
        project = ProjectFactory.create(info=dict(sched='depth_first_all'),
                                        owner=UserFactory.create(id=500))
        orig_tasks = TaskFactory.create_batch(1, project=project, n_answers=10)
        user = UserFactory.create()

        tasks = get_depth_first_all_task(project.id, user.id)
        assert len(tasks) == 1, len(tasks)
        assert tasks[0].id == orig_tasks[0].id, tasks
        assert tasks[0].state == 'ongoing', tasks

        for i in range(10):
            tr = TaskRun(project_id=project.id,
                         task_id=orig_tasks[0].id,
                         user_ip='127.0.0.%s' % i)
            db.session.add(tr)
            db.session.commit()

        tasks = get_depth_first_all_task(project.id, user.id)
        assert len(tasks) == 1, len(tasks)
        assert tasks[0].id == orig_tasks[0].id, tasks
        assert tasks[0].state == 'completed', tasks
        assert len(tasks[0].task_runs) == 10, tasks

        tr = TaskRun(project_id=project.id,
                     task_id=orig_tasks[0].id,
                     user_id=user.id)
        db.session.add(tr)
        db.session.commit()

        tasks = get_depth_first_all_task(project.id, user.id)

        assert len(tasks) == 0, tasks
Esempio n. 5
0
    def test_user_03_respects_limit_tasks_limit(self):
        """ Test SCHED limit arg newtask respects the limit of 30 TaskRuns per list of Tasks"""
        # Del previous TaskRuns
        assigned_tasks = []
        project = ProjectFactory.create(info=dict(sched='depth_first_all'),
                                        owner=UserFactory.create(id=500))

        user = UserFactory.create()

        orig_tasks = TaskFactory.create_batch(2, project=project, n_answers=10)

        tasks = get_depth_first_all_task(project.id,
                                         user.id,
                                         limit=2,
                                         orderby='id',
                                         desc=False)
        assert len(tasks) == 2, len(tasks)
        assert tasks[0].id == orig_tasks[0].id, tasks
        assert tasks[0].state == 'ongoing', tasks
        assert tasks[1].id == orig_tasks[1].id, tasks
        assert tasks[1].state == 'ongoing', tasks

        for i in range(10):
            tr = TaskRun(project_id=project.id,
                         task_id=tasks[0].id,
                         user_ip='127.0.0.%s' % i)
            db.session.add(tr)
            db.session.commit()

        tasks = get_depth_first_all_task(project.id,
                                         user.id,
                                         limit=2,
                                         orderby='id',
                                         desc=False)
        assert len(tasks) == 2, len(tasks)
        assert tasks[0].id == orig_tasks[0].id, tasks
        assert tasks[0].state == 'completed', tasks
        assert len(tasks[0].task_runs) == 10, tasks
        assert tasks[1].id == orig_tasks[1].id, tasks
        assert tasks[1].state == 'ongoing', tasks
        assert len(tasks[1].task_runs) == 0, tasks

        tr = TaskRun(project_id=project.id,
                     task_id=tasks[0].id,
                     user_id=user.id)
        db.session.add(tr)
        db.session.commit()

        tasks = get_depth_first_all_task(project.id,
                                         user.id,
                                         limit=2,
                                         orderby='id',
                                         desc=False)

        assert len(tasks) == 1, tasks
        assert tasks[0].id == orig_tasks[1].id
        assert tasks[0].state == 'ongoing'
Esempio n. 6
0
    def test_all(self):
        """Test MODEL works"""
        username = u'test-user-1'
        user = User(name=username, fullname=username, email_addr=username)
        info = {
            'total': 150,
            'long_description': 'hello world'}
        app = App(
            name=u'My New Project',
            short_name=u'my-new-app',
            description=u'description',
            info=info)
        category = Category(name=u'cat', short_name=u'cat', description=u'cat')
        app.category = category
        app.owner = user
        task_info = {
            'question': 'My random question',
            'url': 'my url'}
        task = Task(info=task_info)
        task_run_info = {'answer': u'annakarenina'}
        task_run = TaskRun(info=task_run_info)
        task.app = app
        task_run.task = task
        task_run.app = app
        task_run.user = user
        db.session.add_all([user, app, task, task_run])
        db.session.commit()
        app_id = app.id

        db.session.remove()

        app = db.session.query(App).get(app_id)
        assert app.name == u'My New Project', app
        # year would start with 201...
        assert app.created.startswith('201'), app.created
        assert app.long_tasks == 0, app.long_tasks
        assert app.hidden == 0, app.hidden
        assert app.time_estimate == 0, app
        assert app.time_limit == 0, app
        assert app.calibration_frac == 0, app
        assert app.bolt_course_id == 0
        assert len(app.tasks) == 1, app
        assert app.owner.name == username, app
        out_task = app.tasks[0]
        assert out_task.info['question'] == task_info['question'], out_task
        assert out_task.quorum == 0, out_task
        assert out_task.state == "ongoing", out_task
        assert out_task.calibration == 0, out_task
        assert out_task.priority_0 == 0, out_task
        assert len(out_task.task_runs) == 1, out_task
        outrun = out_task.task_runs[0]
        assert outrun.info['answer'] == task_run_info['answer'], outrun
        assert outrun.user.name == username, outrun

        user = User.by_name(username)
        assert user.apps[0].id == app_id, user
    def test_external_uid_03_respects_limit_tasks(self):
        """ Test SCHED newtask external uid respects the limit of 30 TaskRuns per Task for
        external user id"""
        assigned_tasks = []
        project = ProjectFactory.create(info=dict(sched='depth_first_all'),
                                        owner=UserFactory.create(id=500))
        user = UserFactory.create()

        task = TaskFactory.create(project=project, n_answers=10)

        uid = '1xa'
        tasks = get_depth_first_all_task(project.id, external_uid=uid)
        assert len(tasks) == 1, len(tasks)
        assert tasks[0].id == task.id, tasks
        assert tasks[0].state == 'ongoing', tasks

        # Add taskruns
        for i in range(10):
            tr = TaskRun(project_id=project.id,
                         task_id=task.id,
                         user_ip='127.0.0.%s' % i)
            db.session.add(tr)
            db.session.commit()

        tasks = get_depth_first_all_task(project.id, external_uid=uid)
        assert len(tasks) == 1, len(tasks)
        assert tasks[0].id == task.id, tasks
        assert tasks[0].state == 'completed', tasks
        assert len(tasks[0].task_runs) == 10, tasks

        url = 'api/project/%s/newtask?external_uid=%s' % (project.id,
                                                          uid)
        headers = self.get_headers_jwt(project)

        res = self.app.get(url, headers=headers)
        data = json.loads(res.data)

        assert data['id'] == task.id
        assert data['state'] == 'completed'

        tr = TaskRun(project_id=project.id,
                     task_id=task.id,
                     external_uid=uid)

        db.session.add(tr)
        db.session.commit()

        tasks = get_depth_first_all_task(project.id, external_uid=uid)
        assert len(tasks) == 0, len(tasks)

        res = self.app.get(url, headers=headers)
        data = json.loads(res.data)
        assert len(data) == 0, data
Esempio n. 8
0
    def test_all(self):
        """Test MODEL works"""
        username = u'test-user-1'
        user = User(name=username, fullname=username, email_addr=username)
        info = {'total': 150, 'long_description': 'hello world'}
        app = App(name=u'My New Project',
                  short_name=u'my-new-app',
                  description=u'description',
                  info=info)
        category = Category(name=u'cat', short_name=u'cat', description=u'cat')
        app.category = category
        app.owner = user
        task_info = {'question': 'My random question', 'url': 'my url'}
        task = Task(info=task_info)
        task_run_info = {'answer': u'annakarenina'}
        task_run = TaskRun(info=task_run_info)
        task.app = app
        task_run.task = task
        task_run.app = app
        task_run.user = user
        db.session.add_all([user, app, task, task_run])
        db.session.commit()
        app_id = app.id

        db.session.remove()

        app = db.session.query(App).get(app_id)
        assert app.name == u'My New Project', app
        # year would start with 201...
        assert app.created.startswith('201'), app.created
        assert app.long_tasks == 0, app.long_tasks
        assert app.hidden == 0, app.hidden
        assert app.time_estimate == 0, app
        assert app.time_limit == 0, app
        assert app.calibration_frac == 0, app
        assert app.bolt_course_id == 0
        assert len(app.tasks) == 1, app
        assert app.owner.name == username, app
        out_task = app.tasks[0]
        assert out_task.info['question'] == task_info['question'], out_task
        assert out_task.quorum == 0, out_task
        assert out_task.state == "ongoing", out_task
        assert out_task.calibration == 0, out_task
        assert out_task.priority_0 == 0, out_task
        assert len(out_task.task_runs) == 1, out_task
        outrun = out_task.task_runs[0]
        assert outrun.info['answer'] == task_run_info['answer'], outrun
        assert outrun.user.name == username, outrun

        user = User.by_name(username)
        assert user.apps[0].id == app_id, user
Esempio n. 9
0
    def test_anonymous_03_respects_limit_tasks_limits(self):
        """ Test SCHED newtask limit respects the limit of 30 TaskRuns per Task using limits"""
        assigned_tasks = []
        user = UserFactory.create()
        project = ProjectFactory.create(info=dict(sched='depth_first_all'))

        orig_tasks = TaskFactory.create_batch(2, project=project, n_answers=5)

        tasks = get_depth_first_all_task(project.id, user.id, limit=2)
        assert len(tasks) == 2, len(tasks)
        assert tasks[0].id == orig_tasks[0].id, tasks
        assert tasks[1].id == orig_tasks[1].id, tasks

        for i in range(5):
            tr = TaskRun(project_id=project.id,
                         task_id=tasks[0].id,
                         user_ip='127.0.0.%s' % i)
            db.session.add(tr)
            db.session.commit()

        # Task should be marked as completed, but as user has no
        # participated it should get the completed one as well.
        tasks = get_depth_first_all_task(project.id,
                                         user.id,
                                         limit=2,
                                         orderby='id',
                                         desc=False)
        assert len(tasks) == 2, len(tasks)
        assert tasks[0].id == orig_tasks[0].id, tasks[0]
        assert tasks[0].state == 'completed', tasks[0].state
        assert len(tasks[0].task_runs) == 5
        assert tasks[1].id == orig_tasks[1].id
        assert tasks[1].state == 'ongoing', tasks[1].state
        assert len(tasks[1].task_runs) == 0

        # User contributes, so only one task should be returned
        tr = TaskRun(project_id=project.id,
                     task_id=tasks[0].id,
                     user_id=user.id)
        db.session.add(tr)
        db.session.commit()

        tasks = get_depth_first_all_task(project.id,
                                         user.id,
                                         limit=2,
                                         orderby='id',
                                         desc=False)
        assert len(tasks) == 1, len(tasks)
        assert tasks[0].id == orig_tasks[1].id, tasks[0]
        assert tasks[0].state == 'ongoing', tasks[0].state
        assert len(tasks[0].task_runs) == 0
Esempio n. 10
0
    def test_anonymous_03_respects_limit_tasks(self):
        """ Test SCHED newtask respects the limit of 30 TaskRuns per Task"""
        assigned_tasks = []
        # Get Task until scheduler returns None
        for i in range(10):
            res = self.app.get('api/project/1/newtask')
            data = json.loads(res.data)

            while data.get('info') is not None:
                # Check that we received a Task
                assert data.get('info'), data

                # Save the assigned task
                assigned_tasks.append(data)

                # Submit an Answer for the assigned task
                tr = TaskRun(project_id=data['project_id'],
                             task_id=data['id'],
                             user_ip="127.0.0." + str(i),
                             info={'answer': 'Yes'})
                db.session.add(tr)
                db.session.commit()
                res = self.app.get('api/project/1/newtask')
                data = json.loads(res.data)

        # Check if there are 30 TaskRuns per Task
        tasks = db.session.query(Task).filter_by(project_id=1).all()
        for t in tasks:
            assert len(t.task_runs) == 10, len(t.task_runs)
        # Check that all the answers are from different IPs
        err_msg = "There are two or more Answers from same IP"
        for t in tasks:
            for tr in t.task_runs:
                assert self.is_unique(tr.user_ip, t.task_runs), err_msg
Esempio n. 11
0
    def test_external_uid_03_respects_limit_tasks(self):
        """ Test SCHED newtask external uid respects the limit of 30 TaskRuns per Task for
        external user id"""
        assigned_tasks = []
        project = ProjectFactory.create(info=dict(sched='depth_first_all'),
                                        owner=UserFactory.create(id=500))
        user = UserFactory.create()

        task = TaskFactory.create(project=project, n_answers=10)

        uid = '1xa'
        tasks = get_depth_first_all_task(project.id, external_uid=uid)
        assert len(tasks) == 1, len(tasks)
        assert tasks[0].id == task.id, tasks
        assert tasks[0].state == 'ongoing', tasks

        # Add taskruns
        for i in range(10):
            tr = TaskRun(project_id=project.id,
                         task_id=task.id,
                         user_ip='127.0.0.%s' % i)
            db.session.add(tr)
            db.session.commit()

        tasks = get_depth_first_all_task(project.id, external_uid=uid)
        assert len(tasks) == 1, len(tasks)
        assert tasks[0].id == task.id, tasks
        assert tasks[0].state == 'completed', tasks
        assert len(tasks[0].task_runs) == 10, tasks
Esempio n. 12
0
    def test_all(self):
        """Test MODEL works"""
        username = u'test-user-1'
        user = User(name=username, fullname=username, email_addr=username)
        info = {
            'total': 150,
            'long_description': 'hello world'}
        project = Project(
            name=u'My New Project',
            short_name=u'my-new-app',
            description=u'description',
            info=info)
        category = Category(name=u'cat', short_name=u'cat', description=u'cat')
        project.category = category
        project.owner = user
        task_info = {
            'question': 'My random question',
            'url': 'my url'}
        task = Task(info=task_info)
        task_run_info = {'answer': u'annakarenina'}
        task_run = TaskRun(info=task_run_info)
        task.project = project
        task_run.task = task
        task_run.project = project
        task_run.user = user
        db.session.add_all([user, project, task, task_run])
        db.session.commit()
        project_id = project.id

        db.session.remove()

        project = db.session.query(Project).get(project_id)
        assert project.name == u'My New Project', project
        # year would start with 201...
        assert project.created.startswith('201'), project.created
        assert len(project.tasks) == 1, project
        assert project.owner.name == username, project
        out_task = project.tasks[0]
        assert out_task.info['question'] == task_info['question'], out_task
        assert out_task.quorum == 0, out_task
        assert out_task.state == "ongoing", out_task
        assert out_task.calibration == 0, out_task
        assert out_task.priority_0 == 0, out_task
        assert len(out_task.task_runs) == 1, out_task
        outrun = out_task.task_runs[0]
        assert outrun.info['answer'] == task_run_info['answer'], outrun
        assert outrun.user.name == username, outrun
Esempio n. 13
0
    def test_all(self):
        """Test MODEL works"""
        username = u'test-user-1'
        user = User(name=username, fullname=username, email_addr=username)
        info = {
            'total': 150,
            'long_description': 'hello world'}
        project = Project(
            name=u'My New Project',
            short_name=u'my-new-app',
            description=u'description',
            info=info)
        category = Category(name=u'cat', short_name=u'cat', description=u'cat')
        project.category = category
        project.owner = user
        task_info = {
            'question': 'My random question',
            'url': 'my url'}
        task = Task(info=task_info)
        task_run_info = {'answer': u'annakarenina'}
        task_run = TaskRun(info=task_run_info)
        task.project = project
        task_run.task = task
        task_run.project = project
        task_run.user = user
        db.session.add_all([user, project, task, task_run])
        db.session.commit()
        project_id = project.id

        db.session.remove()

        project = db.session.query(Project).get(project_id)
        assert project.name == u'My New Project', project
        # year would start with 201...
        assert project.created.startswith('201'), project.created
        assert len(project.tasks) == 1, project
        assert project.owner.name == username, project
        out_task = project.tasks[0]
        assert out_task.info['question'] == task_info['question'], out_task
        assert out_task.quorum == 0, out_task
        assert out_task.state == "ongoing", out_task
        assert out_task.calibration == 0, out_task
        assert out_task.priority_0 == 0, out_task
        assert len(out_task.task_runs) == 1, out_task
        outrun = out_task.task_runs[0]
        assert outrun.info['answer'] == task_run_info['answer'], outrun
        assert outrun.user.name == username, outrun
Esempio n. 14
0
    def test_08_user_progress_authenticated_user(self):
        """Test API userprogress as an authenticated user works"""
        self.register()
        self.signin()
        user = db.session.query(User)\
                 .filter(User.name == 'johndoe')\
                 .first()
        app = db.session.query(App)\
                .get(1)
        tasks = db.session.query(Task)\
                  .filter(Task.app_id == app.id)\
                  .all()

        taskruns = db.session.query(TaskRun)\
                     .filter(TaskRun.app_id == app.id)\
                     .filter(TaskRun.user_id == user.id)\
                     .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

        url = '/api/app/%s/userprogress' % app.short_name
        res = self.app.get(url, 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

        url = '/api/app/5000/userprogress'
        res = self.app.get(url, follow_redirects=True)
        assert res.status_code == 404, res.status_code

        url = '/api/app/userprogress'
        res = self.app.get(url, follow_redirects=True)
        assert res.status_code == 404, res.status_code

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

        res = self.app.get('/api/app/1/newtask')
        data = json.loads(res.data)

        # Add a new TaskRun and check again
        tr = TaskRun(app_id=1,
                     task_id=data['id'],
                     user_id=user.id,
                     info={'answer': u'annakarenina'})
        db.session.add(tr)
        db.session.commit()

        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
        self.signout()
Esempio n. 15
0
 def create_task_and_run(cls,task_info, task_run_info, project, user, order):
     task = Task(project_id = 1, state = '0', info = task_info, n_answers=10)
     task.project = project
     # Taskruns will be assigned randomly to a signed user or an anonymous one
     if random.randint(0,1) == 1:
         task_run = TaskRun(
                 project_id = 1,
                 task_id = 1,
                 user_id = 1,
                 info = task_run_info)
         task_run.user = user
     else:
         task_run = TaskRun(
                 project_id = 1,
                 task_id = 1,
                 user_ip = '127.0.0.%s' % order,
                 info = task_run_info)
     task_run.task = task
     return task, task_run
Esempio n. 16
0
    def test_00_limits_query(self):
        """Test API GET limits works"""
        for i in range(30):
            app = App(name="name%s" % i,
                      short_name="short_name%s" % i,
                      description="desc",
                      owner_id=1)

            info = dict(a=0)
            task = Task(app_id=1, info=info)
            taskrun = TaskRun(app_id=1, task_id=1)
            db.session.add(app)
            db.session.add(task)
            db.session.add(taskrun)
        db.session.commit()

        res = self.app.get('/api/app')
        print res.data
        data = json.loads(res.data)
        assert len(data) == 20, len(data)

        res = self.app.get('/api/app?limit=10')
        data = json.loads(res.data)
        assert len(data) == 10, len(data)

        res = self.app.get('/api/app?limit=10&offset=10')
        data = json.loads(res.data)
        assert len(data) == 10, len(data)
        assert data[0].get('name') == 'name9'

        res = self.app.get('/api/task')
        data = json.loads(res.data)
        assert len(data) == 20, len(data)

        res = self.app.get('/api/taskrun')
        data = json.loads(res.data)
        assert len(data) == 20, len(data)

        # Register 30 new users to test limit on users too
        for i in range(30):
            self.register(fullname="User%s" % i, username="******" % i)

        res = self.app.get('/api/user')
        data = json.loads(res.data)
        assert len(data) == 20, len(data)

        res = self.app.get('/api/user?limit=10')
        data = json.loads(res.data)
        print data
        assert len(data) == 10, len(data)

        res = self.app.get('/api/user?limit=10&offset=10')
        data = json.loads(res.data)
        assert len(data) == 10, len(data)
        assert data[0].get('name') == 'user7', data
    def test_check_task_requested_by_user_anonymous_key_exists(self, user):
        """_check_task_requested_by_user should return True for an anonymous
        user that requested a task"""
        user.return_value = {'user_id': None, 'user_ip': '127.0.0.1'}
        taskrun = TaskRun(task_id=22)
        key = 'pybossa:task_requested:user:127.0.0.1:task:22'
        self.connection.setex(key, 10, True)

        check = _check_task_requested_by_user(taskrun, self.connection)

        assert check is True, check
    def test_check_task_requested_by_user_authenticated_key_exists(self, user):
        """_check_task_requested_by_user should return True for an authorized
        user that requested a task"""
        user.return_value = {'user_id': 33, 'user_ip': None}
        taskrun = TaskRun(task_id=22)
        key = 'pybossa:task_requested:user:33:task:22'
        self.connection.setex(key, 10, True)

        check = _check_task_requested_by_user(taskrun, self.connection)

        assert check is True, check
    def test_check_task_requested_by_user_wrong_key(self, user):
        """_check_task_requested_by_user should return False for a user that did
        not request a task"""
        user.return_value = {'user_id': 33, 'user_ip': None}
        taskrun = TaskRun(task_id=22)
        key = 'pybossa:task_requested:user:88:task:44'
        self.connection.setex(key, 10, True)

        check = _check_task_requested_by_user(taskrun, self.connection)

        assert check is False, check
Esempio n. 20
0
    def test_authenticated_user_update_other_users_taskrun(self):
        """Test authenticated user cannot update any taskrun"""

        with self.flask_app.test_request_context('/'):
            self.configure_fixtures()
            own_taskrun = TaskRun(app_id=self.app.id,
                                  task_id=self.task.id,
                                  user_id=self.mock_authenticated.id,
                                  info="some taskrun info")
            other_users_taskrun = TaskRun(app_id=self.app.id,
                                          task_id=self.task.id,
                                          user_id=self.root.id,
                                          info="a different taskrun info")

            assert_raises(Forbidden,
                          getattr(require, 'taskrun').update,
                          own_taskrun)
            assert_raises(Forbidden,
                          getattr(require, 'taskrun').update,
                          other_users_taskrun)
Esempio n. 21
0
    def test_authenticated_user_delete_other_users_taskrun(self):
        """Test authenticated user cannot delete a taskrun if it was created
        by another authenticated user, but can delete his own taskruns"""

        with self.flask_app.test_request_context('/'):
            self.configure_fixtures()
            own_taskrun = TaskRun(app_id=self.app.id,
                                  task_id=self.task.id,
                                  user_id=self.mock_authenticated.id,
                                  info="some taskrun info")
            other_users_taskrun = TaskRun(app_id=self.app.id,
                                          task_id=self.task.id,
                                          user_id=self.root.id,
                                          info="a different taskrun info")

            assert_not_raises(Exception,
                      getattr(require, 'taskrun').delete,
                      own_taskrun)
            assert_raises(Forbidden,
                      getattr(require, 'taskrun').delete,
                      other_users_taskrun)
Esempio n. 22
0
    def test_anonymous_user_update_user_taskrun(self):
        """Test anonymous user cannot update taskruns posted by authenticated users"""
        with self.flask_app.test_request_context('/'):
            self.configure_fixtures()
            user_taskrun = TaskRun(app_id=self.app.id,
                                   task_id=self.task.id,
                                   user_id=self.root.id,
                                   info="some taskrun info")

            assert_raises(Unauthorized,
                          getattr(require, 'taskrun').update,
                          user_taskrun)
    def test_anonymous_03_respects_limit_tasks(self):
        """ Test SCHED newtask respects the limit of 10 TaskRuns per Task"""
        assigned_tasks = []
        project = ProjectFactory.create(owner=UserFactory.create(id=500),
                                        info=dict(sched='depth_first_all'))

        user = UserFactory.create()

        task = TaskFactory.create(project=project, n_answers=10)

        tasks = get_depth_first_all_task(project.id, user.id)
        assert len(tasks) == 1, len(tasks)
        assert tasks[0].id == task.id, tasks
        assert tasks[0].state == 'ongoing', tasks

        for i in range(10):
            tr = TaskRun(project_id=project.id,
                         task_id=task.id,
                         user_ip='127.0.0.%s' % i)
            db.session.add(tr)
            db.session.commit()

        tasks = get_depth_first_all_task(project.id, user.id)
        assert len(tasks) == 1, len(tasks)
        assert tasks[0].id == task.id, tasks
        assert tasks[0].state == 'completed', tasks

        for i in range(10):
            tasks = get_depth_first_all_task(project.id,
                                             user_id=None,
                                             user_ip='127.0.0.%s' % i)
            assert len(tasks) == 0, tasks

        tr = TaskRun(project_id=project.id,
                     task_id=task.id,
                     user_id=user.id)
        db.session.add(tr)
        db.session.commit()
        tasks = get_depth_first_all_task(project.id, user.id)
        assert len(tasks) == 0, tasks
Esempio n. 24
0
 def create_app_with_contributors(self, anonymous, registered, two_tasks=False, name='my_app'):
     app = App(name=name,
               short_name='%s_shortname' % name,
               description=u'description')
     app.owner = self.user
     db.session.add(app)
     task = Task(app=app)
     db.session.add(task)
     if two_tasks:
         task2 = Task(app=app)
         db.session.add(task2)
     db.session.commit()
     for i in range(anonymous):
         task_run = TaskRun(app_id = app.id,
                            task_id = 1,
                            user_ip = '127.0.0.%s' % i)
         db.session.add(task_run)
         if two_tasks:
             task_run2 = TaskRun(app_id = app.id,
                            task_id = 2,
                            user_ip = '127.0.0.%s' % i)
             db.session.add(task_run2)
     for i in range(registered):
         user = User(email_addr = "*****@*****.**" % i,
                     name = "user%s" % i,
                     passwd_hash = "1234%s" % i,
                     fullname = "user_fullname%s" % i)
         db.session.add(user)
         task_run = TaskRun(app_id = app.id,
                            task_id = 1,
                            user = user)
         db.session.add(task_run)
         if two_tasks:
             task_run2 = TaskRun(app_id = app.id,
                            task_id = 2,
                            user = user)
             db.session.add(task_run2)
     db.session.commit()
     return app
Esempio n. 25
0
    def test_anonymous_user_create_first_taskrun(self):
        """Test anonymous user can create a taskrun for a given task if he
        hasn't already done it"""

        with self.flask_app.test_request_context('/'):
            self.configure_fixtures()
            taskrun = TaskRun(app_id=self.app.id,
                              task_id=self.task.id,
                              user_ip='127.0.0.0',
                              info="some taskrun info")
            assert_not_raises(Exception,
                          getattr(require, 'taskrun').create,
                          taskrun)
Esempio n. 26
0
    def test_admin_delete_user_taskrun(self):
        """Test admins can delete taskruns posted by authenticated users"""

        with self.flask_app.test_request_context('/'):
            self.configure_fixtures()
            user_taskrun = TaskRun(app_id=self.app.id,
                                   task_id=self.task.id,
                                   user_id=self.user1.id,
                                   info="some taskrun info")

            assert_not_raises(Exception,
                      getattr(require, 'taskrun').delete,
                      user_taskrun)
    def test_check_task_requested_by_user_anonymous_preserves_key(self, user):
        """_check_task_requested_by_user does not delete the key after checking
        that an anonymous user requested the task (in case many simultaneous
        anonymous users are sharing the same IP"""
        user.return_value = {'user_id': None, 'user_ip': '127.0.0.1'}
        taskrun = TaskRun(task_id=22)
        key = 'pybossa:task_requested:user:127.0.0.1:task:22'
        self.connection.setex(key, 10, True)

        _check_task_requested_by_user(taskrun, self.connection)
        key_deleted = self.connection.get(key) is None

        assert key_deleted is False, key_deleted
Esempio n. 28
0
    def test_admin_delete_anonymous_taskrun(self):
        """Test admins can delete anonymously posted taskruns"""

        with self.flask_app.test_request_context('/'):
            self.configure_fixtures()
            anonymous_taskrun = TaskRun(app_id=self.app.id,
                                        task_id=self.task.id,
                                        user_ip='127.0.0.0',
                                        info="some taskrun info")

            assert_not_raises(Exception,
                          getattr(require, 'taskrun').delete,
                          anonymous_taskrun)
Esempio n. 29
0
    def test_authenticated_user_delete_anonymous_taskrun(self):
        """Test authenticated users cannot delete an anonymously posted taskrun"""

        with self.flask_app.test_request_context('/'):
            self.configure_fixtures()
            anonymous_taskrun = TaskRun(app_id=self.app.id,
                                        task_id=self.task.id,
                                        user_ip='127.0.0.0',
                                        info="some taskrun info")

            assert_raises(Forbidden,
                          getattr(require, 'taskrun').delete,
                          anonymous_taskrun)
Esempio n. 30
0
    def test_anonymous_user_update_anoymous_taskrun(self):
        """Test anonymous users cannot update an anonymously posted taskrun"""

        with self.flask_app.test_request_context('/'):
            self.configure_fixtures()
            anonymous_taskrun = TaskRun(app_id=self.app.id,
                                        task_id=self.task.id,
                                        user_ip='127.0.0.0',
                                        info="some taskrun info")

            assert_raises(Unauthorized,
                          getattr(require, 'taskrun').update,
                          anonymous_taskrun)
    def test_check_task_requested_by_user_authenticated_deletes_key(
            self, user):
        """_check_task_requested_by_user deletes the key after checking that
        an authenticated user requested the task"""
        user.return_value = {'user_id': 33, 'user_ip': None}
        taskrun = TaskRun(task_id=22)
        key = 'pybossa:task_requested:user:33:task:22'
        self.connection.setex(key, 10, True)

        _check_task_requested_by_user(taskrun, self.connection)
        key_deleted = self.connection.get(key) is None

        assert key_deleted is True, key_deleted
Esempio n. 32
0
    def test_07_user_progress_anonymous(self):
        """Test API userprogress as anonymous works"""
        self.signout()
        app = db.session.query(App).get(1)
        tasks = db.session.query(Task)\
                  .filter(Task.app_id == app.id)\
                  .all()

        # User ID = 2 because, the 1 is the root user
        taskruns = db.session.query(TaskRun)\
                     .filter(TaskRun.app_id == app.id)\
                     .filter(TaskRun.user_id == 2)\
                     .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

        res = self.app.get('/api/app/1/newtask')
        data = json.loads(res.data)
        # Add a new TaskRun and check again
        tr = TaskRun(app_id=1,
                     task_id=data['id'],
                     user_id=1,
                     info={'answer': u'annakarenina'})
        db.session.add(tr)
        db.session.commit()

        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
Esempio n. 33
0
 def create_task_and_run(cls, task_info, task_run_info, app, user, order):
     task = Task(app_id=1, state='0', info=task_info, n_answers=10)
     task.app = app
     # Taskruns will be assigned randomly to a signed user or an anonymous one
     if random.randint(0, 1) == 1:
         task_run = TaskRun(app_id=1,
                            task_id=1,
                            user_id=1,
                            info=task_run_info)
         task_run.user = user
     else:
         task_run = TaskRun(app_id=1,
                            task_id=1,
                            user_ip='127.0.0.%s' % order,
                            info=task_run_info)
     task_run.task = task
     return task, task_run