Beispiel #1
0
    def test_batches_available_for_anon(self):
        anonymous_user = AnonymousUser()

        project_protected = Project(
            active=True,
            login_required=True,
        )
        project_protected.save()
        self.assertEqual(
            len(project_protected.batches_available_for(anonymous_user)), 0)

        Batch(project=project_protected).save()
        self.assertEqual(
            len(project_protected.batches_available_for(anonymous_user)), 0)

        project_unprotected = Project(
            active=True,
            login_required=False,
        )
        project_unprotected.save()
        self.assertEqual(
            len(project_unprotected.batches_available_for(anonymous_user)), 0)

        Batch(project=project_unprotected).save()
        self.assertEqual(
            len(project_unprotected.batches_available_for(anonymous_user)), 1)
Beispiel #2
0
    def test_available_tasks_for_anon_user(self):
        anonymous_user = AnonymousUser()
        user = User.objects.create_user('user', password='******')

        project_protected = Project(
            active=True,
            login_required=True,
        )
        project_protected.save()
        self.assertEqual(len(Project.all_available_for(anonymous_user)), 0)
        self.assertEqual(len(Project.all_available_for(user)), 2)  # Project created by setUp
        batch_protected = Batch(project=project_protected)
        batch_protected.save()
        Task(batch=batch_protected).save()
        self.assertEqual(len(batch_protected.available_tasks_for(anonymous_user)), 0)
        self.assertEqual(len(batch_protected.available_tasks_for(user)), 1)

        project_unprotected = Project(
            active=True,
            login_required=False,
        )
        project_unprotected.save()
        batch_unprotected = Batch(project=project_unprotected)
        batch_unprotected.save()
        Task(batch=batch_unprotected).save()
        self.assertEqual(len(Project.all_available_for(anonymous_user)), 1)
        self.assertEqual(len(Project.all_available_for(user)), 3)
        self.assertEqual(len(project_unprotected.batches_available_for(anonymous_user)), 1)
        self.assertEqual(len(project_unprotected.batches_available_for(user)), 1)
        self.assertEqual(len(batch_unprotected.available_tasks_for(anonymous_user)), 1)
        self.assertEqual(len(batch_unprotected.available_tasks_for(user)), 1)
Beispiel #3
0
    def test_available_for_active_flag(self):
        user = User.objects.create_user('testuser', password='******')

        self.assertEqual(len(Project.all_available_for(user)), 0)

        Project(active=False, ).save()
        self.assertEqual(len(Project.all_available_for(user)), 0)

        Project(active=True, ).save()
        self.assertEqual(len(Project.all_available_for(user)), 1)
Beispiel #4
0
 def test_login_required_validation_2(self):
     # No ValidationError thrown
     Project(
         assignments_per_task=2,
         login_required=True,
         html_template='<textarea>'
     ).clean()
Beispiel #5
0
 def test_login_required_validation_3(self):
     with self.assertRaisesMessage(ValidationError, 'Assignments per Task must be 1'):
         Project(
             assignments_per_task=2,
             login_required=False,
             html_template='<textarea>'
         ).clean()
    def test_batch_add_validation_extra_csv_fields(self):
        project = Project(name='foo', html_template='<p>${f2}</p>')
        project.save()

        self.assertFalse(Batch.objects.filter(name='batch_save').exists())

        client = django.test.Client()
        client.login(username='******', password='******')
        # CSV file has fields 'f2' and 'f3'
        with open(
                os.path.abspath(
                    'turkle/tests/resources/mismatched_fields.csv')) as fp:
            response = client.post(
                u'/admin/turkle/batch/add/', {
                    'assignments_per_task': 1,
                    'project': project.id,
                    'name': 'batch_save',
                    'csv_file': fp
                })
        self.assertEqual(response.status_code, 302)
        self.assertTrue(Batch.objects.filter(name='batch_save').exists())
        matching_batch = Batch.objects.filter(name='batch_save').first()
        self.assertEqual(
            response['Location'],
            reverse('turkle_admin:review_batch',
                    kwargs={'batch_id': matching_batch.id}))
        messages = list(get_messages(response.wsgi_request))
        self.assertEqual(len(messages), 1)
        self.assertTrue(
            u'The CSV file contained fields that are not in the HTML template.'
            in str(messages[0]))
Beispiel #7
0
    def test_task_marked_as_completed_two_way_redundancy(self):
        # When assignment_per_task==2, completing 2 Assignments marks Task as complete
        project = Project(
            name='test',
            html_template='<p>${number} - ${letter}</p><textarea>')
        project.save()
        batch = Batch(name='test', project=project)
        batch.assignments_per_task = 2
        batch.save()

        task = Task(batch=batch,
                    input_csv_fields={
                        'number': '1',
                        'letter': 'a'
                    })
        task.save()
        self.assertFalse(task.completed)
        self.assertFalse(batch.completed)

        TaskAssignment(assigned_to=None, completed=True, task=task).save()
        task.refresh_from_db()
        batch.refresh_from_db()
        self.assertFalse(task.completed)
        self.assertFalse(batch.completed)

        TaskAssignment(assigned_to=None, completed=True, task=task).save()
        task.refresh_from_db()
        batch.refresh_from_db()
        self.assertTrue(task.completed)
        self.assertTrue(batch.completed)
Beispiel #8
0
    def setUp(self):
        """
        Sets up Project, Task objects, and saves them to the DB.
        The Project form HTML only displays the one input variable.
        The Task has inputs and answers and refers to the Project form.
        """
        admin = User.objects.create_superuser('admin', '*****@*****.**', 'secret')
        project = Project(name='test', html_template="<p>${foo}</p><textarea>")
        project.created_by = admin
        project.updated_by = admin
        save_model(project)
        batch = Batch(name='test', project=project, filename='test.csv')
        batch.created_by = admin
        batch.updated_by = admin
        save_model(batch)

        task = Task(
            batch=batch,
            input_csv_fields={'foo': 'bar'},
            completed=True,
        )
        task.save()
        self.task = task

        self.task_assignment = TaskAssignment(
            answers={
                "comment": "\u221e", "userDisplayLanguage": "",
                "sentence_textbox_3_verb1": "", "city": "",
                "sentence_textbox_1_verb6": "",
                "sentence_textbox_1_verb7": "",
                "sentence_textbox_1_verb4": "",
                "sentence_textbox_1_verb5": "",
                "sentence_textbox_1_verb2": "",
                "sentence_textbox_1_verb3": "",
                "sentence_textbox_1_verb1": "",
                "sentence_textbox_2_verb4": "",
                "csrfmiddlewaretoken": "7zxQ9Yyug6Nsnm4nLky9p8ObJwNipdu8",
                "sentence_drop_2_verb3": "foo",
                "sentence_drop_2_verb2": "foo",
                "sentence_drop_2_verb1": "foo",
                "sentence_textbox_2_verb1": "",
                "sentence_textbox_2_verb3": "",
                "sentence_drop_2_verb4": "foo",
                "sentence_textbox_2_verb2": "",
                "submitit": "Submit", "browserInfo": "",
                "sentence_drop_1_verb1": "foo",
                "sentence_drop_1_verb2": "foo",
                "sentence_drop_1_verb3": "foo",
                "sentence_drop_1_verb4": "foo",
                "sentence_drop_1_verb5": "foo",
                "sentence_drop_1_verb6": "foo",
                "sentence_drop_1_verb7": "foo", "country": "",
                "sentence_drop_3_verb1": "foo",
                "ipAddress": "", "region": ""
            },
            assigned_to=None,
            completed=True,
            task=task
        )
        self.task_assignment.save()
Beispiel #9
0
    def test_batch_to_csv_variable_number_of_answers(self):
        project = Project(name='test',
                          html_template='<p>${letter}</p><textarea>')
        project.save()
        batch = Batch(project=project)
        batch.save()

        task1 = Task(
            batch=batch,
            completed=True,
            input_csv_fields={'letter': 'a'},
        )
        task1.save()
        TaskAssignment(
            answers={
                '1': 1,
                '2': 2
            },
            assigned_to=None,
            completed=True,
            task=task1,
        ).save()

        task2 = Task(
            batch=batch,
            completed=True,
            input_csv_fields={'letter': 'b'},
        )
        task2.save()
        TaskAssignment(answers={
            '3': 3,
            '4': 4
        },
                       assigned_to=None,
                       completed=True,
                       task=task2).save()

        task3 = Task(
            batch=batch,
            completed=True,
            input_csv_fields={'letter': 'c'},
        )
        task3.save()
        TaskAssignment(answers={
            '3': 3,
            '2': 2
        },
                       assigned_to=None,
                       completed=True,
                       task=task3).save()

        csv_output = StringIO()
        batch.to_csv(csv_output)
        rows = csv_output.getvalue().split()
        self.assertTrue(
            '"Input.letter","Answer.1","Answer.2","Answer.3","Answer.4"' in
            rows[0])
        self.assertTrue(any(['"a","1","2","",""' in row for row in rows[1:]]))
        self.assertTrue(any(['"b","","","3","4"' in row for row in rows[1:]]))
        self.assertTrue(any(['"c","","2","3",""' in row for row in rows[1:]]))
Beispiel #10
0
    def test_extract_fieldnames_from_form_html(self):
        self.assertEqual({'foo': True}, self.task.batch.project.fieldnames)

        project = Project(name='test',
                          html_template='<p>${foo} - ${bar}</p><textarea>')
        project.clean()
        self.assertEqual({'foo': True, 'bar': True}, project.fieldnames)
Beispiel #11
0
 def test_too_large_template(self):
     limit = get_turkle_template_limit(True)
     template = 'a' * limit + '<textarea>'
     with self.assertRaisesMessage(ValidationError, 'Template is too large'):
         Project(
             html_template=template,
         ).clean()
Beispiel #12
0
    def test_batch_to_input_csv(self):
        project = Project(name='test', html_template='<p>${letter}</p><textarea>')
        project.save()
        batch = Batch(project=project)
        batch.save()

        task1 = Task(
            batch=batch,
            completed=True,
            input_csv_fields={'letter': 'a', 'number': '1'},
        )
        task1.save()
        task2 = Task(
            batch=batch,
            completed=True,
            input_csv_fields={'letter': 'b', 'number': '2'},
        )
        task2.save()

        csv_output = StringIO()
        batch.to_input_csv(csv_output)
        rows = csv_output.getvalue().split()

        # Original column order not guaranteed
        if '"letter","number"' in rows[0]:
            self.assertTrue(any(['"a","1"' in row for row in rows[1:]]))
            self.assertTrue(any(['"b","2"' in row for row in rows[1:]]))
        elif '"number","letter"' in rows[0]:
            self.assertTrue(any(['"1","a"' in row for row in rows[1:]]))
            self.assertTrue(any(['"2","b"' in row for row in rows[1:]]))
        else:
            self.fail('Unexpected header value: %s' % rows[0])
Beispiel #13
0
    def test_expire_abandoned_assignments(self):
        t = timezone.now()
        dt = datetime.timedelta(hours=2)
        past = t - dt

        project = Project(login_required=False)
        project.save()
        batch = Batch(allotted_assignment_time=1, project=project)
        batch.save()
        task = Task(batch=batch)
        task.save()
        ha = TaskAssignment(
            completed=False,
            expires_at=past,
            task=task,
        )
        # Bypass TaskAssignment's save(), which updates expires_at
        super(TaskAssignment, ha).save()
        self.assertEqual(TaskAssignment.objects.count(), 1)

        User.objects.create_superuser('admin', '*****@*****.**', 'secret')
        client = django.test.Client()
        client.login(username='******', password='******')
        response = client.get(
            reverse('turkle_admin:expire_abandoned_assignments'))
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['Location'], '/admin/')
        self.assertEqual(TaskAssignment.objects.count(), 0)
        messages = list(get_messages(response.wsgi_request))
        self.assertEqual(len(messages), 1)
        self.assertEqual(str(messages[0]),
                         u'All 1 abandoned Tasks have been expired')
Beispiel #14
0
    def setUp(self):
        with open('turkle/tests/resources/form_0.html') as f:
            html_template = f.read()

        self.project = Project(name="filepath", html_template=html_template)
        self.project.save()
        self.batch = Batch(project=self.project)
        self.batch.save()
        field_names = "tweet0_id,tweet0_entity,tweet0_before_entity,tweet0_after_entity," + \
            "tweet0_word0,tweet0_word1,tweet0_word2,tweet1_id,tweet1_entity," + \
            "tweet1_before_entity,tweet1_after_entity,tweet1_word0,tweet1_word1,tweet1_word2," + \
            "tweet2_id,tweet2_entity,tweet2_before_entity,tweet2_after_entity,tweet2_word0," + \
            "tweet2_word1,tweet2_word2,tweet3_id,tweet3_entity,tweet3_before_entity," + \
            "tweet3_after_entity,tweet3_word0,tweet3_word1,tweet3_word2,tweet4_id," + \
            "tweet4_entity,tweet4_before_entity,tweet4_after_entity,tweet4_word0," + \
            "tweet4_word1,tweet4_word2,tweet5_id,tweet5_entity,tweet5_before_entity," + \
            "tweet5_after_entity,tweet5_word0,tweet5_word1,tweet5_word2",
        values = "268,SANTOS, Muy bien America ......... y lo siento mucho , un muy buen " + \
            "rival,mucho,&nbsp;,&nbsp;,2472,GREGORY, Ah bueno , tampoco andes pidiendo ese " +\
            "tipo de milagros . @jcabrerac @CarlosCabreraR,bueno,&nbsp;,&nbsp;,478,ALEJANDRO," + \
            " @aguillen19 &#44; un super abrazo mi querido , &#44; mis mejores deseos para " + \
            "este 2012 ... muakkk !,querido,&nbsp;,&nbsp;,906_control, PF, Acusan camioneros " + \
            "extorsiones de, : Transportistas acusaron que deben pagar entre 13 y 15 mil " + \
            "pesos a agentes que .. http://t.co/d8LUVvhP,acusaron,&nbsp;,&nbsp;,2793_control," + \
            " CHICARO, Me gusta cuando chicharo hace su oracion es lo que lo hace especial .," + \
            "&nbsp;,gusta,&nbsp;,&nbsp;,357,OSCAR WILDE&QUOT;, &quot; @ ifilosofia : Las " + \
            "pequeñas acciones de cada día son las que hacen o deshacen el carácter.&quot; , " + \
            "bueno !!!! Es así,bueno,&nbsp;,&nbsp;",
        self.task = Task(batch=self.batch,
                         input_csv_fields=dict(zip(field_names, values)))
        self.task.save()
Beispiel #15
0
    def test_batch_add_csv_with_emoji(self):
        project = Project(name='foo',
                          html_template='<p>${emoji}: ${more_emoji}</p>')
        project.save()

        self.assertFalse(Batch.objects.filter(name='batch_save').exists())

        client = django.test.Client()
        client.login(username='******', password='******')
        with open(os.path.abspath('turkle/tests/resources/emoji.csv')) as fp:
            response = client.post(
                u'/admin/turkle/batch/add/', {
                    'assignments_per_task': 1,
                    'project': project.id,
                    'name': 'batch_save',
                    'csv_file': fp
                })
        self.assertTrue(b'error' not in response.content)
        self.assertEqual(response.status_code, 302)
        self.assertTrue(Batch.objects.filter(name='batch_save').exists())
        matching_batch = Batch.objects.filter(name='batch_save').last()
        self.assertEqual(
            response['Location'],
            u'/admin/turkle/batch/{}/review/'.format(matching_batch.id))
        self.assertEqual(matching_batch.filename, u'emoji.csv')

        self.assertEqual(matching_batch.total_tasks(), 3)
        tasks = matching_batch.task_set.all()
        self.assertEqual(tasks[0].input_csv_fields['emoji'], u'😀')
        self.assertEqual(tasks[0].input_csv_fields['more_emoji'], u'😃')
        self.assertEqual(tasks[2].input_csv_fields['emoji'], u'🤔')
        self.assertEqual(tasks[2].input_csv_fields['more_emoji'], u'🤭')
Beispiel #16
0
    def test_batch_add(self):
        project = Project(name='foo', html_template='<p>${foo}: ${bar}</p>')
        project.save()

        self.assertFalse(Batch.objects.filter(name='batch_save').exists())

        client = django.test.Client()
        client.login(username='******', password='******')
        with open(os.path.abspath(
                'turkle/tests/resources/form_1_vals.csv')) as fp:
            response = client.post(
                u'/admin/turkle/batch/add/', {
                    'assignments_per_task': 1,
                    'project': project.id,
                    'name': 'batch_save',
                    'csv_file': fp
                })
        self.assertTrue(b'error' not in response.content)
        self.assertEqual(response.status_code, 302)
        self.assertTrue(Batch.objects.filter(name='batch_save').exists())
        matching_batch = Batch.objects.filter(name='batch_save').last()
        self.assertEqual(
            response['Location'],
            u'/admin/turkle/batch/{}/review/'.format(matching_batch.id))
        self.assertEqual(matching_batch.filename, u'form_1_vals.csv')
        self.assertEqual(matching_batch.total_tasks(), 1)
        self.assertEqual(
            matching_batch.allotted_assignment_time,
            Batch._meta.get_field('allotted_assignment_time').get_default())
        self.assertEqual(matching_batch.created_by, self.user)
Beispiel #17
0
    def test_index_unprotected_template(self):
        project_unprotected = Project(
            active=True,
            login_required=False,
            name='MY_TEMPLATE_NAME',
        )
        project_unprotected.save()
        batch = Batch(project=project_unprotected, name='MY_BATCH_NAME')
        batch.save()
        Task(batch=batch).save()

        anon_client = django.test.Client()
        response = anon_client.get(reverse('index'))
        self.assertEqual(response.status_code, 200)
        self.assertFalse(b'No Tasks available' in response.content)
        self.assertTrue(b'MY_TEMPLATE_NAME' in response.content)
        self.assertTrue(b'MY_BATCH_NAME' in response.content)

        known_client = django.test.Client()
        User.objects.create_superuser('admin', '*****@*****.**', 'secret')
        known_client.login(username='******', password='******')
        response = known_client.get(reverse('index'))
        self.assertEqual(response.status_code, 200)
        self.assertFalse(b'No Tasks available' in response.content)
        self.assertTrue(b'MY_TEMPLATE_NAME' in response.content)
        self.assertTrue(b'MY_BATCH_NAME' in response.content)
Beispiel #18
0
 def setUp(self):
     project = Project(login_required=False)
     project.save()
     self.batch = Batch(project=project)
     self.batch.save()
     self.task = Task(batch=self.batch)
     self.task.save()
Beispiel #19
0
    def test_batch_add_validation_missing_csv_fields(self):
        project = Project(name='foo',
                          html_template='<p>${f1} ${f2} ${f3}</p><textarea>')
        project.created_by = self.user
        project.updated_by = self.user
        save_model(project)

        self.assertFalse(Batch.objects.filter(name='batch_save').exists())

        client = django.test.Client()
        client.login(username='******', password='******')
        # CSV file has fields 'f2' and 'f3'
        with open(
                os.path.abspath(
                    'turkle/tests/resources/mismatched_fields.csv')) as fp:
            response = client.post('/admin/turkle/batch/add/', {
                'project': project.id,
                'name': 'batch_save',
                'csv_file': fp
            })
        self.assertEqual(response.status_code, 200)
        print(response.content)
        self.assertTrue(b'Please correct the error' in response.content)
        self.assertTrue(b'extra fields' not in response.content)
        self.assertTrue(b'missing fields' in response.content)
Beispiel #20
0
 def test_form_without_submit_button(self):
     project = Project(name='Test',
                       html_template='<p>Quick brown fox</p><textarea>')
     project.created_by = self.admin
     project.updated_by = self.admin
     save_model(project)
     self.assertFalse(project.html_template_has_submit_button)
Beispiel #21
0
 def test_form_with_submit_button(self):
     project = Project(
         html_template=
         '<p><input id="my_submit_button" type="submit" value="MySubmit" /></p>'
     )
     project.save()
     self.assertTrue(project.html_template_has_submit_button)
Beispiel #22
0
 def test_login_required_validation_2(self):
     # No ValidationError thrown
     project = Project(login_required=True, )
     project.save()
     Batch(
         assignments_per_task=2,
         project=project,
     ).clean()
Beispiel #23
0
 def test_input_field_required(self):
     msg_part = "Please include at least one field"
     with self.assertRaisesRegex(ValidationError, msg_part):
         Project(
             assignments_per_task=2,
             login_required=True,
             html_template='<script>do stuff here</script>'
         ).clean()
Beispiel #24
0
    def setUp(self):
        """
        Sets up Project, Task objects, and saves them to the DB.
        The Project form HTML only displays the one input variable.
        The Task has inputs and answers and refers to the Project form.
        """
        project = Project(name='test', html_template="<p>${foo}</p>")
        project.save()
        batch = Batch(project=project)
        batch.save()

        task = Task(
            batch=batch,
            input_csv_fields={u'foo': u'bar'},
            completed=True,
        )
        task.save()
        self.task = task

        self.task_assignment = TaskAssignment(answers={
            u"comment": u"\u221e",
            u"userDisplayLanguage": u"",
            u"sentence_textbox_3_verb1": u"",
            u"city": u"",
            u"sentence_textbox_1_verb6": u"",
            u"sentence_textbox_1_verb7": u"",
            u"sentence_textbox_1_verb4": u"",
            u"sentence_textbox_1_verb5": u"",
            u"sentence_textbox_1_verb2": u"",
            u"sentence_textbox_1_verb3": u"",
            u"sentence_textbox_1_verb1": u"",
            u"sentence_textbox_2_verb4": u"",
            u"csrfmiddlewaretoken": u"7zxQ9Yyug6Nsnm4nLky9p8ObJwNipdu8",
            u"sentence_drop_2_verb3": u"foo",
            u"sentence_drop_2_verb2": u"foo",
            u"sentence_drop_2_verb1": u"foo",
            u"sentence_textbox_2_verb1": u"",
            u"sentence_textbox_2_verb3": u"",
            u"sentence_drop_2_verb4": u"foo",
            u"sentence_textbox_2_verb2": u"",
            u"submitit": u"Submit",
            u"browserInfo": u"",
            u"sentence_drop_1_verb1": u"foo",
            u"sentence_drop_1_verb2": u"foo",
            u"sentence_drop_1_verb3": u"foo",
            u"sentence_drop_1_verb4": u"foo",
            u"sentence_drop_1_verb5": u"foo",
            u"sentence_drop_1_verb6": u"foo",
            u"sentence_drop_1_verb7": u"foo",
            u"country": u"",
            u"sentence_drop_3_verb1": u"foo",
            u"ipAddress": u"",
            u"region": u""
        },
                                              assigned_to=None,
                                              completed=True,
                                              task=task)
        self.task_assignment.save()
Beispiel #25
0
 def setUp(self):
     self.project = Project(login_required=False)
     self.project.save()
     batch = Batch(project=self.project)
     batch.save()
     self.task = Task(input_csv_fields={}, batch=batch)
     self.task.save()
     self.task_assignment = TaskAssignment(task=self.task)
     self.task_assignment.save()
Beispiel #26
0
 def test_form_with_submit_button(self):
     project = Project(
         name='Test',
         html_template='<p><input id="my_submit_button" type="submit" value="MySubmit" /></p>'
     )
     project.created_by = self.admin
     project.updated_by = self.admin
     save_model(project)
     self.assertTrue(project.html_template_has_submit_button)
Beispiel #27
0
    def setUp(self):
        project = Project(name='foo', html_template='<p>${foo}: ${bar}</p>')
        project.save()

        batch = Batch(project=project, name='foo', filename='foo.csv')
        batch.save()

        self.task = Task(batch=batch)
        self.task.save()
Beispiel #28
0
 def setUp(self):
     User.objects.create_superuser('admin', '*****@*****.**', 'secret')
     project = Project(name='foo',
                       html_template='<p>${foo}: ${bar}</p><textarea>')
     project.save()
     self.batch = Batch(project=project,
                        name='MY_BATCH_NAME',
                        published=False)
     self.batch.save()
Beispiel #29
0
    def setUp(self):
        self.user = User.objects.create_user('testuser', password='******')

        self.project = Project()
        self.project.save()
        self.batch = Batch(project=self.project)
        self.batch.save()
        self.task = Task(batch=self.batch)
        self.task.save()
Beispiel #30
0
 def test_login_required_validation_3(self):
     with self.assertRaisesMessage(ValidationError,
                                   'Assignments per Task must be 1'):
         project = Project(login_required=False, )
         project.save()
         Batch(
             assignments_per_task=2,
             project=project,
         ).clean()