Exemplo n.º 1
0
 def test_no_times_by_default(self):
     report = Report(owner=self.user)
     report.encrypt_report("test report", "key")
     report.save()
     self.assertIsNone(Report.objects.first().last_edited)
     self.assertIsNone(Report.objects.first().submitted_to_school)
     self.assertIsNone(Report.objects.first().entered_into_matching)
Exemplo n.º 2
0
    def setUp(self):
        super(ExistingRecordTest, self).setUp()

        User.objects.create_user(username='******', password='******')
        self.client.login(username='******', password='******')
        self.request = HttpRequest()
        self.request.GET = {}
        self.request.method = 'GET'
        self.request.user = User.objects.get(username='******')

        self.report_text = """[
    { "answer": "test answer",
      "id": %i,
      "section": 1,
      "question_text": "first question",
      "type": "SingleLineText"
    },
    { "answer": "another answer to a different question",
      "id": %i,
      "section": 1,
      "question_text": "2nd question",
      "type": "SingleLineText"
    }
  ]""" % (self.question1.pk, self.question2.pk)
        self.report = Report(owner=self.request.user)
        self.report_key = 'bananabread! is not my key'
        self.report.encrypt_report(self.report_text, self.report_key)
        self.report.save()
        row = EvalRow()
        row.anonymise_record(action=EvalRow.CREATE,
                             report=self.report,
                             decrypted_text=self.report_text)
        row.save()
Exemplo n.º 3
0
 def test_edit_sets_edited_time(self):
     report = Report(owner=self.user)
     report.encrypt_report("test report", "key")
     report.save()
     report.encrypt_report("a different report", "key")
     report.save()
     self.assertIsNotNone(Report.objects.first().last_edited)
Exemplo n.º 4
0
 def create_match(self, user, identifier):
     report = Report(owner=user)
     report.encrypt_report("test report 1", "key")
     report.save()
     return MatchReport.objects.create(report=report,
                                       contact_phone='phone',
                                       contact_email='*****@*****.**',
                                       identifier=identifier)
Exemplo n.º 5
0
 def test_entered_into_matching_property_is_set(self):
     report = Report(owner=self.user)
     report.encrypt_report("test report", "key")
     report.save()
     MatchReport.objects.create(report=report,
                                contact_phone='phone',
                                contact_email='*****@*****.**',
                                identifier='dummy')
     self.assertIsNotNone(Report.objects.first().entered_into_matching)
Exemplo n.º 6
0
   def test_match_sends_custom_report(self):
       self.client.post(
           ('/test_reports/match_custom/%s/' % self.report.pk),
           data={
               'name': 'test submitter 1',
               'email': '*****@*****.**',
               'phone_number': '555-555-1212',
               'email_confirmation': "False",
               'key': self.report_key,
               'form-0-perp': 'facebook.com/triggered_match',
               'form-TOTAL_FORMS': '1',
               'form-INITIAL_FORMS': '1',
               'form-MAX_NUM_FORMS': '',
           })
       user2 = User.objects.create_user(username='******', password='******')
       self.client.login(username='******', password='******')
       report2_text = """[
   { "answer": "test answer",
     "id": %i,
     "section": 1,
     "question_text": "first question",
     "type": "SingleLineText"
   },
   { "answer": "another answer to a different question",
     "id": %i,
     "section": 1,
     "question_text": "2nd question",
     "type": "SingleLineText"
   }
 ]""" % (self.question1.pk, self.question2.pk)
       report2 = Report(owner=user2)
       report2_key = 'a key a key a key a key key'
       report2.encrypt_report(report2_text, report2_key)
       report2.save()
       response = self.client.post(
           ('/test_reports/match_custom/%s/' % report2.pk),
           data={
               'name': 'test submitter 2',
               'email': '*****@*****.**',
               'phone_number': '555-555-1213',
               'email_confirmation': "False",
               'key': report2_key,
               'form-0-perp': 'facebook.com/triggered_match',
               'form-TOTAL_FORMS': '1',
               'form-INITIAL_FORMS': '1',
               'form-MAX_NUM_FORMS': '',
           })
       self.assertNotIn('submit_error', response.context)
       self.assertEqual(len(mail.outbox), 3)
       message = mail.outbox[2]
       self.assertEqual(message.subject, 'test match delivery')
       self.assertEqual(message.to, ['*****@*****.**'])
       self.assertIn('"Custom" <custom@', message.from_email)
       self.assertIn('test match delivery body', message.body)
       self.assertRegexpMatches(message.attachments[0][0],
                                'custom_.*\.pdf\.gpg')
Exemplo n.º 7
0
 def test_can_decrypt_report(self):
     report = Report(owner=self.user)
     report.encrypt_report(
         "this text should be encrypted, yes it should by golly!",
         key='this is my key')
     report.save()
     saved_report = Report.objects.first()
     self.assertEqual(
         saved_report.decrypted_report('this is my key'),
         "this text should be encrypted, yes it should by golly!")
Exemplo n.º 8
0
 def test_can_encrypt_report(self):
     report = Report(owner=self.user)
     report.encrypt_report("this text should be encrypted",
                           key='this is my key')
     report.save()
     saved_report = Report.objects.first()
     self.assertIsNotNone(saved_report.salt)
     self.assertNotEqual(saved_report.salt, '')
     self.assertIsNotNone(saved_report.encrypted)
     self.assertTrue(len(saved_report.encrypted) > 0)
Exemplo n.º 9
0
 def test_user_identifier(self):
     user_with_email = User.objects.create_user(username="******",
                                                password="******",
                                                email="*****@*****.**")
     report = Report(owner=user_with_email)
     report.encrypt_report(self.decrypted_report, "a key a key a key")
     report.save()
     pdf_report = PDFFullReport(report, self.decrypted_report)
     output = pdf_report.generate_pdf_report(recipient=None, report_id=None)
     exported_report = BytesIO(output)
     pdfReader = PyPDF2.PdfFileReader(exported_report)
     self.assertIn("Submitted by: [email protected]",
                   pdfReader.getPage(0).extractText())
Exemplo n.º 10
0
 def test_can_withdraw_from_matching(self):
     report = Report(owner=self.user)
     report.encrypt_report("test report", "key")
     report.save()
     MatchReport.objects.create(report=report,
                                contact_phone='phone',
                                contact_email='*****@*****.**',
                                identifier='dummy')
     self.assertIsNotNone(Report.objects.first().entered_into_matching)
     report.match_found = True
     report.save()
     report.withdraw_from_matching()
     report.save()
     self.assertIsNone(Report.objects.first().entered_into_matching)
     self.assertFalse(Report.objects.first().match_found)
Exemplo n.º 11
0
   def setUp(self):
       super(ReportDeliveryTest, self).setUp()
       self.user = self.user1
       self.decrypted_report = """[
   { "answer": "test answer",
     "id": 1,
     "section": 1,
     "question_text": "first question",
     "type": "SingleLineText"
   },
   { "answer": "answer to 2nd question",
     "id": 2,
     "section": 1,
     "question_text": "2nd question",
     "type": "SingleLineText"
   }
 ]"""
       self.report = Report(owner=self.user)
       self.report.encrypt_report(self.decrypted_report, "a key a key a key")
       self.report.save()
Exemplo n.º 12
0
    def test_edit_saves_original_record_if_no_data_exists(self):
        old_report = Report(owner=self.request.user)
        old_report.encrypt_report(self.report_text, self.report_key)
        old_report.save()

        self.assertEqual(EvalRow.objects.count(), 1)
        self.assertEqual(
            EvalRow.objects.filter(action=EvalRow.FIRST).count(), 0)

        self.edit_record(record_to_edit=old_report)
        self.assertEqual(EvalRow.objects.count(), 3)
        self.assertEqual(
            EvalRow.objects.filter(action=EvalRow.FIRST).count(), 1)
        self.assertEqual(EvalRow.objects.last().action, EvalRow.EDIT)
        self.assertEqual(
            EvalRow.objects.filter(
                action=EvalRow.FIRST).first().record_identifier,
            EvalRow.objects.last().record_identifier)
        self.assertNotEqual(
            EvalRow.objects.filter(action=EvalRow.FIRST).first().row,
            EvalRow.objects.last().row)
Exemplo n.º 13
0
    def test_done_saves_anonymised_qs(self, mockEvalRow, mockReport):
        self.maxDiff = None

        radio_button_q = RadioButton.objects.create(
            text="this is a radio button question", page=self.page2)
        for i in range(5):
            Choice.objects.create(text="This is choice %i" % i,
                                  question=radio_button_q)
        wizard = EncryptedFormWizard.wizard_factory()()

        PageOneForm = wizard.form_list[0]
        PageTwoForm = wizard.form_list[1]
        KeyForm = wizard.form_list[2]

        page_one = PageOneForm(
            {'question_%i' % self.question1.pk: 'test answer'})
        page_one.is_valid()
        page_two = PageTwoForm({
            'question_%i' % self.question2.pk:
            'another answer to a different question',
            'question_%i' % radio_button_q.pk:
            radio_button_q.choice_set.all()[2].pk
        })
        page_two.is_valid()
        key_form = KeyForm({'key': self.report_key, 'key2': self.report_key})
        key_form.is_valid()

        mock_report = Report()
        mock_report.save = Mock()
        mock_report.owner = self.request.user
        mockReport.return_value = mock_report

        mock_eval_row = EvalRow()
        mock_eval_row.save = Mock()
        mockEvalRow.return_value = mock_eval_row

        self._get_wizard_response(wizard,
                                  form_list=[page_one, page_two, key_form],
                                  request=self.request)
        mock_eval_row.save.assert_any_call()
Exemplo n.º 14
0
 def test_wizard_done_is_called(self, mockReport):
     mock_report = Report()
     mock_report.id = 1
     mock_report.owner = self.request.user
     mockReport.return_value = mock_report
     wizard = EncryptedFormWizard.wizard_factory()()
     PageOneForm = wizard.form_list[0]
     PageTwoForm = wizard.form_list[1]
     KeyForm = wizard.form_list[2]
     page_one = PageOneForm({'question_%i' % self.question1.pk: ""})
     page_one.is_valid()
     page_two = PageTwoForm({'question_%i' % self.question2.pk: ""})
     page_two.is_valid()
     key_form = KeyForm({'key': self.report_key, 'key2': self.report_key})
     key_form.is_valid()
     form_list = [page_one, page_two, key_form]
     wizard.processed_answers = wizard.process_answers(
         form_list=form_list, form_dict=dict(enumerate(form_list)))
     response = wizard.done(form_list=form_list,
                            form_dict=dict(enumerate(form_list)),
                            request=self.request)
     self.assertContains(response, 1)
Exemplo n.º 15
0
    def test_done_serializes_questions(self, mockReport):
        self.maxDiff = None

        radio_button_q = RadioButton.objects.create(
            text="this is a radio button question", page=self.page2)
        for i in range(5):
            Choice.objects.create(text="This is choice %i" % i,
                                  question=radio_button_q)
        wizard = EncryptedFormWizard.wizard_factory()()

        PageOneForm = wizard.form_list[0]
        PageTwoForm = wizard.form_list[1]
        KeyForm = wizard.form_list[2]

        page_one = PageOneForm(
            {'question_%i' % self.question1.pk: 'test answer'})
        page_one.is_valid()
        page_two = PageTwoForm({
            'question_%i' % self.question2.pk:
            'another answer to a different question',
            'question_%i' % radio_button_q.pk:
            radio_button_q.choice_set.all()[2].pk
        })
        page_two.is_valid()
        key_form = KeyForm({'key': self.report_key, 'key2': self.report_key})
        key_form.is_valid()

        object_ids = [choice.pk for choice in radio_button_q.choice_set.all()]
        selected_id = object_ids[2]
        object_ids.insert(0, radio_button_q.pk)
        object_ids.insert(0, selected_id)
        object_ids.insert(0, self.question2.pk)
        object_ids.insert(0, self.question1.pk)

        json_report = """[
    { "answer": "test answer",
      "id": %i,
      "section": 1,
      "question_text": "first question",
      "type": "SingleLineText"
    },
    { "answer": "another answer to a different question",
      "id": %i,
      "section": 1,
      "question_text": "2nd question",
      "type": "SingleLineText"
    },
    { "answer": "%i",
      "id": %i,
      "section": 1,
      "question_text": "this is a radio button question",
            "choices": [{"id": %i, "choice_text": "This is choice 0"},
                  {"id": %i, "choice_text": "This is choice 1"},
                  {"id": %i, "choice_text": "This is choice 2"},
                  {"id": %i, "choice_text": "This is choice 3"},
                  {"id": %i, "choice_text": "This is choice 4"}],
      "type": "RadioButton"
    }
  ]""" % tuple(object_ids)

        mock_report = Report()
        mock_report.save = Mock()
        mock_report.owner = self.request.user
        mockReport.return_value = mock_report

        def check_json():
            self.assertEqual(
                sort_json(mock_report.decrypted_report(self.report_key)),
                sort_json(json_report))

        mock_report.save.side_effect = check_json

        self._get_wizard_response(wizard,
                                  form_list=[page_one, page_two, key_form],
                                  request=self.request)
        mock_report.save.assert_any_call()
Exemplo n.º 16
0
 def test_report_owner_is_not_optional(self):
     report = Report(encrypted=b'a report')
     with self.assertRaises(IntegrityError):
         report.save()
         report.full_clean()
Exemplo n.º 17
0
 def test_cannot_save_empty_reports(self):
     report = Report(owner=self.user, encrypted=b'')
     with self.assertRaises(ValidationError):
         report.save()
         report.full_clean()
Exemplo n.º 18
0
 def test_reports_have_owners(self):
     report = Report()
     report.owner = self.user
     report.save()
     self.assertIn(report, self.user.report_set.all())
Exemplo n.º 19
0
 def test_entered_into_matching_is_blank_before_entering_into_matching(
         self):
     report = Report(owner=self.user)
     report.encrypt_report("test report", "key")
     report.save()
     self.assertIsNone(Report.objects.first().entered_into_matching)
Exemplo n.º 20
0
 def setUp(self):
     user = User.objects.create(username="******", password="******")
     self.report = Report(owner=user)
     self.key = '~*~*~*~my key~*~*~*~'
     self.report.encrypt_report('this is a report', self.key)
     self.report.save()