Esempio n. 1
0
    def test_post_request_with_valid_data_rolls_back_if_exception_raised(self):
        def receiver(*args, **kwargs):
            self._create_message()

        with self._overrides(MANDRILL_WEBHOOK_URL=u'https://testhost/', MANDRILL_WEBHOOK_KEYS=[u'testkey']):
            webhook_event.connect(receiver)

            # No exceptions, data commited
            with created_instances(Message.objects) as msg_set:
                self.client.post(self._webhook_url(), secure=True,
                        data={u'mandrill_events': json.dumps([
                            {u'event': u'click', u'_id': u'remote-1'},
                            ])},
                        HTTP_X_MANDRILL_SIGNATURE=u'phOye9ZN3XunJ8SG7R9AT6KhpUo=')
            self.assertTrue(msg_set.exists())

            # With exception, data rolled back
            with created_instances(Message.objects) as msg_set:
                with patch_with_exception(u'poleno.mail.transports.mandrill.views.HttpResponse'):
                    self.client.post(self._webhook_url(), secure=True,
                            data={u'mandrill_events': json.dumps([
                                {u'event': u'click', u'_id': u'remote-1'},
                                ])},
                            HTTP_X_MANDRILL_SIGNATURE=u'phOye9ZN3XunJ8SG7R9AT6KhpUo=')
            self.assertFalse(msg_set.exists())
    def test_post_with_email_button_and_valid_data_sends_action_by_email(self):
        scenario = self._create_scenario()
        data = self._create_post_data(button=u'email', branch=scenario.branch)
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            with created_instances(Message.objects) as message_set:
                response = self.client.post(url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        action = action_set.get()
        email = message_set.get()

        self.assertEqual(email.type, Message.TYPES.OUTBOUND)
        self.assertEqual(action.email, email)
Esempio n. 3
0
    def test_add_expiration_if_expired_method(self):
        tests = (                                   # Expected action type,      branch, scenario
                (Action.TYPES.REQUEST,                Action.TYPES.EXPIRATION,        0, []),
                (Action.TYPES.CLARIFICATION_RESPONSE, Action.TYPES.EXPIRATION,        0, [u'clarification_request', u'clarification_response']),
                (Action.TYPES.APPEAL,                 Action.TYPES.APPEAL_EXPIRATION, 0, [u'expiration', u'appeal']),
                (Action.TYPES.CONFIRMATION,           Action.TYPES.EXPIRATION,        0, [u'confirmation']),
                (Action.TYPES.EXTENSION,              Action.TYPES.EXPIRATION,        0, [u'extension']),
                (Action.TYPES.ADVANCEMENT,            None,                           0, [u'advancement']),
                (Action.TYPES.CLARIFICATION_REQUEST,  None,                           0, [u'clarification_request']),
                (Action.TYPES.DISCLOSURE,             None,                           0, [u'disclosure']),
                (Action.TYPES.REFUSAL,                None,                           0, [u'refusal']),
                (Action.TYPES.AFFIRMATION,            None,                           0, [u'refusal', u'appeal', u'affirmation']),
                (Action.TYPES.REVERSION,              None,                           0, [u'refusal', u'appeal', u'reversion']),
                (Action.TYPES.REMANDMENT,             Action.TYPES.EXPIRATION,        0, [u'refusal', u'appeal', u'remandment']),
                (Action.TYPES.ADVANCED_REQUEST,       Action.TYPES.EXPIRATION,        1, [u'advancement']),
                (Action.TYPES.EXPIRATION,             None,                           0, [u'expiration']),
                (Action.TYPES.APPEAL_EXPIRATION,      None,                           0, [u'refusal', u'appeal', u'appeal_expiration']),
                )
        # Make sure we are testing all defined action types
        tested_action_types = set(a for a, _, _, _ in tests)
        defined_action_types = Action.TYPES._inverse.keys()
        self.assertItemsEqual(tested_action_types, defined_action_types)

        for action_type, expected_action_type, branch, scenario in tests:
            timewarp.jump(local_datetime_from_local(u'2010-07-05 10:33:00'))
            objs = self._create_inforequest_scenario(*scenario)
            branch = [o for o in flatten(objs) if isinstance(o, Branch)][branch]
            self.assertEqual(branch.last_action.type, action_type)
            original_last_action = branch.last_action

            # Deadline not expired yet
            with created_instances(Action.objects) as action_set:
                branch.add_expiration_if_expired()
            self.assertEqual(action_set.count(), 0)

            # Any deadline is expired now
            timewarp.jump(local_datetime_from_local(u'2010-10-05 10:33:00'))
            branch = Branch.objects.get(pk=branch.pk)
            with created_instances(Action.objects) as action_set:
                branch.add_expiration_if_expired()

            branch = Branch.objects.get(pk=branch.pk)
            if expected_action_type is None:
                self.assertEqual(action_set.count(), 0)
                self.assertEqual(branch.last_action, original_last_action)
            else:
                added_action = action_set.get()
                self.assertEqual(branch.last_action, added_action)
                self.assertEqual(added_action.type, expected_action_type)
                self.assertEqual(added_action.effective_date, naive_date(u'2010-10-05'))
Esempio n. 4
0
    def test_post_with_email_button_and_valid_data_sends_action_by_email(self):
        scenario = self._create_scenario()
        data = self._create_post_data(button=u'email', branch=scenario.branch)
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            with created_instances(Message.objects) as message_set:
                response = self.client.post(
                    url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        action = action_set.get()
        email = message_set.get()

        self.assertEqual(email.type, Message.TYPES.OUTBOUND)
        self.assertEqual(action.email, email)
Esempio n. 5
0
    def test_notification_email_is_not_sent_if_inforequest_is_closed(self):
        inforequest = self._create_inforequest(closed=True)
        msg = self._create_message(received_for=inforequest.unique_email)

        with created_instances(Message.objects) as message_set:
            assign_email_on_message_received(sender=None, message=msg)
        self.assertFalse(message_set.exists())
Esempio n. 6
0
    def test_post_with_submit_button_and_valid_data_creates_inforequest(self):
        self._login_user(self.user1)
        obligee = self._create_obligee(name=u'Obligee')
        attachment1 = self._create_attachment(
            generic_object=self._get_session())
        attachment2 = self._create_attachment(
            generic_object=self._get_session())
        data = self._create_post_data(button=u'submit',
                                      obligee=u'Obligee',
                                      subject=[u'Subject'],
                                      content=[u'Content'],
                                      attachments=u'%s,%s' %
                                      (attachment1.pk, attachment2.pk))

        with created_instances(Inforequest.objects) as inforequest_set:
            response = self.client.post(reverse(u'inforequests:create'), data)

        inforequest = inforequest_set.get()
        branch = inforequest.main_branch
        action = branch.last_action
        self.assertEqual(inforequest.applicant, self.user1)
        self.assertItemsEqual(inforequest.branches, [branch])
        self.assertEqual(branch.obligee, obligee)
        self.assertItemsEqual(branch.action_set.all(), [action])
        self.assertEqual(action.type, Action.TYPES.REQUEST)
        self.assertIn(u'Subject', action.subject)
        self.assertIn(u'Content', action.content)
        self.assertItemsEqual(action.attachment_set.all(),
                              [attachment1, attachment2])
Esempio n. 7
0
 def test_post_with_submit_button_and_invalid_data_does_not_send_inforequest_email(
         self):
     data = self._create_post_data(button=u'submit', obligee=u'invalid')
     self._login_user()
     with created_instances(Message.objects) as message_set:
         response = self.client.post(reverse(u'inforequests:create'), data)
     self.assertFalse(message_set.exists())
Esempio n. 8
0
 def test_bulk_create_without_save_method(self):
     with created_instances(TestModelsModel.objects) as obj_set:
         TestModelsModel.objects.bulk_create([
             TestModelsModel(name=u'aaa'),
             TestModelsModel(name=u'bbb'),
             ])
     self.assertEqual(obj_set.count(), 2)
Esempio n. 9
0
    def test_post_with_draft_button_and_valid_data_updates_existing_draft_instance(
            self):
        self._login_user(self.user1)
        obligee = self._create_obligee(name=u'New Obligee')
        attachment1 = self._create_attachment(
            generic_object=self._get_session())
        attachment2 = self._create_attachment(
            generic_object=self._get_session())
        draft = self._create_inforequest_draft(applicant=self.user1,
                                               obligee=self.obligee1,
                                               subject=[u'Old Subject'],
                                               content=[u'Old Content'])
        attachment3 = self._create_attachment(generic_object=draft)
        data = self._create_post_data(button=u'draft',
                                      obligee=u'New Obligee',
                                      subject=[u'New Subject'],
                                      content=[u'New Content'],
                                      attachments=u'%s,%s' %
                                      (attachment1.pk, attachment3.pk))

        with created_instances(
                InforequestDraft.objects) as inforequestdraft_set:
            response = self.client.post(
                reverse(u'inforequests:create_from_draft', args=(draft.pk, )),
                data)
        self.assertFalse(inforequestdraft_set.exists())

        draft = InforequestDraft.objects.get(pk=draft.pk)
        self.assertEqual(draft.applicant, self.user1)
        self.assertEqual(draft.obligee, obligee)
        self.assertEqual(draft.subject, [u'New Subject'])
        self.assertEqual(draft.content, [u'New Content'])
        self.assertItemsEqual(draft.attachment_set.all(),
                              [attachment1, attachment3])
    def test_post_with_valid_data_creates_action_instance(self):
        scenario = self._create_scenario(email_args=dict(subject=u'Subject', text=u'Content', processed=utc_datetime_from_local(u'2010-10-05 00:33:00')))
        attachment1 = self._create_attachment(generic_object=scenario.email, name=u'filename.txt', content=u'content', content_type=u'text/plain')
        attachment2 = self._create_attachment(generic_object=scenario.email, name=u'filename.html', content=u'<p>content</p>', content_type=u'text/html')
        data = self._create_post_data(branch=scenario.branch)
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            response = self.client.post(url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        action = action_set.get()

        self.assertEqual(action.email, scenario.email)
        self.assertEqual(action.type, self.action_type)
        self.assertEqual(action.subject, u'Subject')
        self.assertEqual(action.content, u'Content')
        self.assertEqual(action.effective_date, naive_date(u'2010-10-05'))

        attachments = [(a.name, a.content, a.content_type) for a in action.attachment_set.all()]
        self.assertItemsEqual(attachments, [
            (u'filename.txt', u'content', u'text/plain'),
            (u'filename.html', u'<p>content</p>', u'text/html'),
            ])

        scenario.rel = InforequestEmail.objects.get(pk=scenario.rel.pk)
        self.assertEqual(scenario.rel.type, InforequestEmail.TYPES.OBLIGEE_ACTION)
Esempio n. 11
0
    def test_post_with_draft_button_and_valid_data_does_not_update_existing_draft_instance_if_exception_raised(
            self):
        self._login_user(self.user1)
        obligee = self._create_obligee(name=u'New Obligee')
        attachment1 = self._create_attachment(
            generic_object=self._get_session())
        attachment2 = self._create_attachment(
            generic_object=self._get_session())
        draft = self._create_inforequest_draft(applicant=self.user1,
                                               obligee=self.obligee1,
                                               subject=[u'Old Subject'],
                                               content=[u'Old Content'])
        attachment3 = self._create_attachment(generic_object=draft)
        data = self._create_post_data(button=u'draft',
                                      obligee=u'New Obligee',
                                      subject=[u'New Subject'],
                                      content=[u'New Content'],
                                      attachments=u'%s,%s' %
                                      (attachment1.pk, attachment3.pk))

        with created_instances(
                InforequestDraft.objects) as inforequestdraft_set:
            with patch_with_exception(
                    u'chcemvediet.apps.inforequests.views.HttpResponseRedirect'
            ):
                response = self.client.post(
                    reverse(u'inforequests:create_from_draft',
                            args=(draft.pk, )), data)

        self.assertFalse(inforequestdraft_set.exists())
        draft = InforequestDraft.objects.get(pk=draft.pk)
        self.assertEqual(draft.subject, [u'Old Subject'])
Esempio n. 12
0
    def test_post_with_valid_data_does_not_create_action_instance_if_exception_raised(
            self):
        scenario = self._create_scenario(email_args=dict(
            subject=u'Subject',
            text=u'Content',
            processed=utc_datetime_from_local(u'2010-10-05 00:33:00')))
        attachment1 = self._create_attachment(generic_object=scenario.email,
                                              name=u'filename.txt',
                                              content=u'content',
                                              content_type=u'text/plain')
        attachment2 = self._create_attachment(generic_object=scenario.email,
                                              name=u'filename.html',
                                              content=u'<p>content</p>',
                                              content_type=u'text/html')
        data = self._create_post_data(branch=scenario.branch)
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            with patch_with_exception(
                    u'chcemvediet.apps.inforequests.views.JsonResponse'):
                response = self.client.post(
                    url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        self.assertFalse(action_set.exists())

        scenario.rel = InforequestEmail.objects.get(pk=scenario.rel.pk)
        self.assertEqual(scenario.rel.type, InforequestEmail.TYPES.UNDECIDED)
Esempio n. 13
0
    def test_post_with_draft_button_and_valid_data_creates_new_draft_instance(
            self):
        self._login_user(self.user1)
        obligee = self._create_obligee(name=u'Obligee')
        attachment1 = self._create_attachment(
            generic_object=self._get_session())
        attachment2 = self._create_attachment(
            generic_object=self._get_session())
        data = self._create_post_data(button=u'draft',
                                      obligee=u'Obligee',
                                      subject=[u'Subject'],
                                      content=[u'Content'],
                                      attachments=u'%s,%s' %
                                      (attachment1.pk, attachment2.pk))

        with created_instances(
                InforequestDraft.objects) as inforequestdraft_set:
            response = self.client.post(reverse(u'inforequests:create'), data)
        draft = inforequestdraft_set.get()

        self.assertEqual(draft.applicant, self.user1)
        self.assertEqual(draft.obligee, obligee)
        self.assertEqual(draft.subject, [u'Subject'])
        self.assertEqual(draft.content, [u'Content'])
        self.assertItemsEqual(draft.attachment_set.all(),
                              [attachment1, attachment2])
Esempio n. 14
0
 def test_attachments_field_is_not_required_for_submit_button(self):
     data = self._create_post_data(button=u'submit', omit=[u'attachments'])
     self._login_user()
     with created_instances(Inforequest.objects) as inforequest_set:
         response = self.client.post(reverse(u'inforequests:create'), data)
     inforequest = inforequest_set.get()
     self.assertRedirects(response, reverse(u'inforequests:detail', args=(inforequest.pk,)))
Esempio n. 15
0
    def test_upload_file_returns_json_with_uploaded_file(self):
        self._login_user()
        data = {u'files': ContentFile(u'Content', name=u'filename.txt')}
        url = reverse(u'inforequests:upload_attachment')

        with created_instances(Attachment.objects) as attachment_set:
            response = self.client.post(
                url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        attachment = attachment_set.get()

        self.assertIs(type(response), JsonResponse)
        self.assertEqual(response.status_code, 200)

        data = json.loads(response.content)
        self.assertEqual(
            data, {
                u'files': [{
                    u'url':
                    reverse(u'inforequests:download_attachment',
                            args=(attachment.pk, )),
                    u'pk':
                    attachment.pk,
                    u'name':
                    u'filename.txt',
                    u'size':
                    7,
                }]
            })
Esempio n. 16
0
    def test_inforequest_is_skipped_if_exception_raised_while_checking_it(
            self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        scenarios = [self._create_inforequest_scenario() for i in range(3)]

        timewarp.jump(local_datetime_from_local(u'2010-10-05 10:33:00'))
        with mock.patch(u'chcemvediet.apps.inforequests.cron.nop',
                        side_effect=[None, Exception, None, None, None]):
            with mock.patch(u'chcemvediet.apps.inforequests.cron.cron_logger'
                            ) as logger:
                with created_instances(Action.objects) as action_set:
                    self._call_cron_job()
        self.assertEqual(action_set.count(), 2)
        self.assertEqual(Inforequest.objects.closed().count(), 2)
        self.assertEqual(len(logger.mock_calls), 3)
        self.assertRegexpMatches(
            logger.mock_calls[0][1][0],
            u'Checking if inforequest should be closed failed: <Inforequest: %s>'
            % scenarios[1][0].pk)
        self.assertRegexpMatches(
            logger.mock_calls[1][1][0],
            u'Closed inforequest: <Inforequest: %s>' % scenarios[0][0].pk)
        self.assertRegexpMatches(
            logger.mock_calls[2][1][0],
            u'Closed inforequest: <Inforequest: %s>' % scenarios[2][0].pk)
Esempio n. 17
0
 def test_bulk_create_with_prevent_bulk_create_true(self):
     with created_instances(TestModelsBulkModel.objects) as obj_set:
         TestModelsBulkModel.objects.bulk_create([
             TestModelsBulkModel(name=u'aaa'),
             TestModelsBulkModel(name=u'bbb'),
             ])
     self.assertEqual(obj_set.count(), 2)
Esempio n. 18
0
 def test_post_with_submit_button_and_valid_data_redirects_to_inforequests_detail(self):
     data = self._create_post_data(button=u'submit')
     self._login_user(self.user1)
     with created_instances(Inforequest.objects) as inforequest_set:
         response = self.client.post(reverse(u'inforequests:create'), data)
     inforequest = inforequest_set.get()
     self.assertRedirects(response, reverse(u'inforequests:detail', args=(inforequest.pk,)))
Esempio n. 19
0
    def test_notification_email_is_not_sent_if_inforequest_is_closed(self):
        inforequest = self._create_inforequest(closed=True)
        msg = self._create_message(received_for=inforequest.unique_email)

        with created_instances(Message.objects) as message_set:
            assign_email_on_message_received(sender=None, message=msg)
        self.assertFalse(message_set.exists())
Esempio n. 20
0
 def test_post_with_draft_button_and_invalid_data_does_not_create_new_draft_instance(
         self):
     data = self._create_post_data(button=u'draft', obligee=u'Invalid')
     self._login_user(self.user1)
     with created_instances(
             InforequestDraft.objects) as inforequestdraft_set:
         response = self.client.post(reverse(u'inforequests:create'), data)
     self.assertFalse(inforequestdraft_set.exists())
Esempio n. 21
0
 def test_bulk_create_with_prevent_bulk_create_false(self):
     with created_instances(TestModelsNoBulkModel.objects) as obj_set:
         with self.assertRaisesMessage(ValueError, u"Can't bulk create TestModelsNoBulkModel"):
             TestModelsNoBulkModel.objects.bulk_create([
                 TestModelsNoBulkModel(name=u'aaa'),
                 TestModelsNoBulkModel(name=u'bbb'),
                 ])
     self.assertFalse(obj_set.exists())
Esempio n. 22
0
    def test_expiration_not_added_if_last_action_does_not_have_obligee_deadline(self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        _, branch, _ = self._create_inforequest_scenario(u'disclosure')

        timewarp.jump(local_datetime_from_local(u'2010-10-05 10:33:00'))
        with created_instances(branch.action_set) as action_set:
            self._call_cron_job()
        self.assertFalse(action_set.exists())
Esempio n. 23
0
    def test_expiration_not_added_if_inforequest_is_already_closed(self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        _, branch, _ = self._create_inforequest_scenario(dict(closed=True))

        timewarp.jump(local_datetime_from_local(u'2010-10-05 10:33:00'))
        with created_instances(branch.action_set) as action_set:
            self._call_cron_job()
        self.assertFalse(action_set.exists())
Esempio n. 24
0
    def test_expiration_not_added_if_inforequest_is_already_closed(self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        _, branch, _ = self._create_inforequest_scenario(dict(closed=True))

        timewarp.jump(local_datetime_from_local(u'2010-10-05 10:33:00'))
        with created_instances(branch.action_set) as action_set:
            self._call_cron_job()
        self.assertFalse(action_set.exists())
Esempio n. 25
0
 def test_attachments_field_is_not_required_for_submit_button(self):
     data = self._create_post_data(button=u'submit', omit=[u'attachments'])
     self._login_user()
     with created_instances(Inforequest.objects) as inforequest_set:
         response = self.client.post(reverse(u'inforequests:create'), data)
     inforequest = inforequest_set.get()
     self.assertRedirects(
         response, reverse(u'inforequests:detail', args=(inforequest.pk, )))
Esempio n. 26
0
    def test_post_with_submit_button_and_valid_data_sends_inforequest_email(self):
        user = self._create_user(first_name=u'John', last_name=u'Smith')
        data = self._create_post_data(button=u'submit')

        self._login_user(user)
        with self.settings(INFOREQUEST_UNIQUE_EMAIL=u'{token}@example.com'):
            with mock.patch(u'chcemvediet.apps.inforequests.models.inforequest.random_readable_string', return_value=u'aaaa'):
                with created_instances(Message.objects) as message_set:
                    with created_instances(Inforequest.objects) as inforequest_set:
                        response = self.client.post(reverse(u'inforequests:create'), data)

        email = message_set.get()
        self.assertEqual(email.type, Message.TYPES.OUTBOUND)
        self.assertEqual(email.from_formatted, u'John Smith <*****@*****.**>')

        inforequest = inforequest_set.get()
        self.assertItemsEqual(inforequest.email_set.all(), [email])
        self.assertEqual(inforequest.main_branch.last_action.email, email)
Esempio n. 27
0
 def test_post_with_draft_button_and_invalid_data_does_not_update_existing_draft_instance(self):
     draft = self._create_inforequest_draft(applicant=self.user1, subject=[u'Old Subject'])
     data = self._create_post_data(button=u'draft', obligee=u'Invalid')
     self._login_user(self.user1)
     with created_instances(InforequestDraft.objects) as inforequestdraft_set:
         response = self.client.post(reverse(u'inforequests:create_from_draft', args=(draft.pk,)), data)
     self.assertFalse(inforequestdraft_set.exists())
     draft = InforequestDraft.objects.get(pk=draft.pk)
     self.assertEqual(draft.subject, [u'Old Subject'])
Esempio n. 28
0
    def test_received_message_with_no_recipients_is_not_assigned(self):
        inforequest = self._create_inforequest()
        msg = self._create_message(omit=[u'received_for'])

        with created_instances(InforequestEmail.objects) as rel_set:
            assign_email_on_message_received(sender=None, message=msg)
        self.assertFalse(rel_set.exists())

        self.assertItemsEqual(msg.inforequest_set.all(), [])
Esempio n. 29
0
    def test_expiration_not_added_if_last_action_does_not_have_obligee_deadline(
            self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        _, branch, _ = self._create_inforequest_scenario(u'disclosure')

        timewarp.jump(local_datetime_from_local(u'2010-10-05 10:33:00'))
        with created_instances(branch.action_set) as action_set:
            self._call_cron_job()
        self.assertFalse(action_set.exists())
Esempio n. 30
0
    def test_post_with_email_button_and_invalid_data_does_not_send_email(self):
        scenario = self._create_scenario()
        data = self._create_post_data(button=u'email', branch=u'invalid')
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(Message.objects) as message_set:
            response = self.client.post(url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        self.assertFalse(message_set.exists())
Esempio n. 31
0
    def test_received_message_with_no_recipients_is_not_assigned(self):
        inforequest = self._create_inforequest()
        msg = self._create_message(omit=[u'received_for'])

        with created_instances(InforequestEmail.objects) as rel_set:
            assign_email_on_message_received(sender=None, message=msg)
        self.assertFalse(rel_set.exists())

        self.assertItemsEqual(msg.inforequest_set.all(), [])
Esempio n. 32
0
 def test_post_with_submit_button_and_valid_data_redirects_to_inforequests_detail(
         self):
     data = self._create_post_data(button=u'submit')
     self._login_user(self.user1)
     with created_instances(Inforequest.objects) as inforequest_set:
         response = self.client.post(reverse(u'inforequests:create'), data)
     inforequest = inforequest_set.get()
     self.assertRedirects(
         response, reverse(u'inforequests:detail', args=(inforequest.pk, )))
Esempio n. 33
0
    def test_post_with_default_button_and_invalid_data_does_not_create_action(self):
        scenario = self._create_scenario()
        data = self._create_post_data(branch=u'invalid')
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            response = self.client.post(url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        self.assertFalse(action_set.exists())
Esempio n. 34
0
    def test_post_with_submit_button_and_valid_data_does_not_send_inforequest_email_if_exception_raised(self):
        user = self._create_user(first_name=u'John', last_name=u'Smith')
        data = self._create_post_data(button=u'submit')

        self._login_user(user)
        with created_instances(Message.objects) as message_set:
            with patch_with_exception(u'chcemvediet.apps.inforequests.views.HttpResponseRedirect'):
                response = self.client.post(reverse(u'inforequests:create'), data)
        self.assertFalse(message_set.exists())
Esempio n. 35
0
    def test_post_with_draft_button_and_invalid_data_does_not_create_new_draft_instance(self):
        scenario = self._create_scenario()
        data = self._create_post_data(button=u'draft', branch=u'invalid')
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(ActionDraft.objects) as actiondraft_set:
            response = self.client.post(url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        self.assertFalse(actiondraft_set.exists())
Esempio n. 36
0
    def test_send_by_email_works_only_for_applicant_actions(self):
        tests = (
            (Action.TYPES.REQUEST, True),
            (Action.TYPES.CLARIFICATION_RESPONSE, True),
            (Action.TYPES.APPEAL, True),
            (Action.TYPES.CONFIRMATION, False),
            (Action.TYPES.EXTENSION, False),
            (Action.TYPES.ADVANCEMENT, False),
            (Action.TYPES.CLARIFICATION_REQUEST, False),
            (Action.TYPES.DISCLOSURE, False),
            (Action.TYPES.REFUSAL, False),
            (Action.TYPES.AFFIRMATION, False),
            (Action.TYPES.REVERSION, False),
            (Action.TYPES.REMANDMENT, False),
            (Action.TYPES.ADVANCED_REQUEST, False),
            (Action.TYPES.EXPIRATION, False),
            (Action.TYPES.APPEAL_EXPIRATION, False),
        )
        # Make sure we are testing all defined action types
        tested_action_types = [a for a, _ in tests]
        defined_action_types = Action.TYPES._inverse.keys()
        self.assertItemsEqual(tested_action_types, defined_action_types)

        inforequest = self._create_inforequest()
        branch = self._create_branch(inforequest=inforequest)
        for action_type, can_send in tests:
            action = self._create_action(branch=branch, type=action_type)
            if can_send:
                with created_instances(Message.objects) as message_set:
                    action.send_by_email()
                email = message_set.get()
                self.assertEqual(email.type, Message.TYPES.OUTBOUND)
                self.assertEqual(action.email, email)
                self.assertIn(email, inforequest.email_set.all())
                rel = InforequestEmail.objects.get(email=email)
                self.assertEqual(rel.inforequest, inforequest)
                self.assertEqual(rel.type,
                                 InforequestEmail.TYPES.APPLICANT_ACTION)
            else:
                with created_instances(Message.objects) as message_set:
                    with self.assertRaisesMessage(TypeError,
                                                  u'is not applicant action'):
                        action.send_by_email()
                self.assertEqual(message_set.count(), 0)
Esempio n. 37
0
    def test_post_with_default_button_and_valid_data_does_not_create_action_if_exception_raised(self):
        scenario = self._create_scenario()
        data = self._create_post_data(branch=scenario.branch)
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            with patch_with_exception(u'chcemvediet.apps.inforequests.views.JsonResponse'):
                response = self.client.post(url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        self.assertFalse(action_set.exists())
Esempio n. 38
0
    def test_post_request_with_valid_data_rolls_back_if_exception_raised(self):
        def receiver(*args, **kwargs):
            self._create_message()

        with self._overrides(MANDRILL_WEBHOOK_URL=u'https://testhost/',
                             MANDRILL_WEBHOOK_KEYS=[u'testkey']):
            webhook_event.connect(receiver)

            # No exceptions, data commited
            with created_instances(Message.objects) as msg_set:
                self.client.post(
                    self._webhook_url(),
                    secure=True,
                    data={
                        u'mandrill_events':
                        json.dumps([
                            {
                                u'event': u'click',
                                u'_id': u'remote-1'
                            },
                        ])
                    },
                    HTTP_X_MANDRILL_SIGNATURE=u'phOye9ZN3XunJ8SG7R9AT6KhpUo=')
            self.assertTrue(msg_set.exists())

            # With exception, data rolled back
            with created_instances(Message.objects) as msg_set:
                with patch_with_exception(
                        u'poleno.mail.transports.mandrill.views.HttpResponse'):
                    self.client.post(self._webhook_url(),
                                     secure=True,
                                     data={
                                         u'mandrill_events':
                                         json.dumps([
                                             {
                                                 u'event': u'click',
                                                 u'_id': u'remote-1'
                                             },
                                         ])
                                     },
                                     HTTP_X_MANDRILL_SIGNATURE=
                                     u'phOye9ZN3XunJ8SG7R9AT6KhpUo=')
            self.assertFalse(msg_set.exists())
Esempio n. 39
0
    def test_post_with_email_button_and_invalid_data_does_not_send_email(self):
        scenario = self._create_scenario()
        data = self._create_post_data(button=u'email', branch=u'invalid')
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(Message.objects) as message_set:
            response = self.client.post(
                url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        self.assertFalse(message_set.exists())
Esempio n. 40
0
    def test_expiration_added_if_last_action_has_obligee_deadline(self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        _, branch, _ = self._create_inforequest_scenario()

        timewarp.jump(local_datetime_from_local(u'2010-10-05 10:33:00'))
        with created_instances(branch.action_set) as action_set:
            self._call_cron_job()
        action = action_set.get()

        self.assertEqual(action.type, Action.TYPES.EXPIRATION)
Esempio n. 41
0
    def test_expiration_added_if_last_action_has_obligee_deadline(self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        _, branch, _ = self._create_inforequest_scenario()

        timewarp.jump(local_datetime_from_local(u'2010-10-05 10:33:00'))
        with created_instances(branch.action_set) as action_set:
            self._call_cron_job()
        action = action_set.get()

        self.assertEqual(action.type, Action.TYPES.EXPIRATION)
Esempio n. 42
0
    def test_expirations_added_for_inforequest_with_multiple_branches(self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        _, branch, actions = self._create_inforequest_scenario((u'advancement',
                [u'confirmation'],
                [u'clarification_request'],
                [u'clarification_request', u'clarification_response'],
                ))
        _, (_, [(branch1, _), (branch2, _), (branch3, _)]) = actions

        timewarp.jump(local_datetime_from_local(u'2010-10-05 10:33:00'))
        with created_instances(branch.action_set) as action_set:
            with created_instances(branch1.action_set) as action_set1:
                with created_instances(branch2.action_set) as action_set2:
                    with created_instances(branch3.action_set) as action_set3:
                        self._call_cron_job()
        self.assertFalse(action_set.exists())
        self.assertTrue(action_set1.exists())
        self.assertFalse(action_set2.exists())
        self.assertTrue(action_set3.exists())
Esempio n. 43
0
    def test_send_by_email_works_only_for_applicant_actions(self):
        tests = (
                (Action.TYPES.REQUEST,                True),
                (Action.TYPES.CLARIFICATION_RESPONSE, True),
                (Action.TYPES.APPEAL,                 True),
                (Action.TYPES.CONFIRMATION,           False),
                (Action.TYPES.EXTENSION,              False),
                (Action.TYPES.ADVANCEMENT,            False),
                (Action.TYPES.CLARIFICATION_REQUEST,  False),
                (Action.TYPES.DISCLOSURE,             False),
                (Action.TYPES.REFUSAL,                False),
                (Action.TYPES.AFFIRMATION,            False),
                (Action.TYPES.REVERSION,              False),
                (Action.TYPES.REMANDMENT,             False),
                (Action.TYPES.ADVANCED_REQUEST,       False),
                (Action.TYPES.EXPIRATION,             False),
                (Action.TYPES.APPEAL_EXPIRATION,      False),
                )
        # Make sure we are testing all defined action types
        tested_action_types = [a for a, _ in tests]
        defined_action_types = Action.TYPES._inverse.keys()
        self.assertItemsEqual(tested_action_types, defined_action_types)

        inforequest = self._create_inforequest()
        branch = self._create_branch(inforequest=inforequest)
        for action_type, can_send in tests:
            action = self._create_action(branch=branch, type=action_type)
            if can_send:
                with created_instances(Message.objects) as message_set:
                    action.send_by_email()
                email = message_set.get()
                self.assertEqual(email.type, Message.TYPES.OUTBOUND)
                self.assertEqual(action.email, email)
                self.assertIn(email, inforequest.email_set.all())
                rel = InforequestEmail.objects.get(email=email)
                self.assertEqual(rel.inforequest, inforequest)
                self.assertEqual(rel.type, InforequestEmail.TYPES.APPLICANT_ACTION)
            else:
                with created_instances(Message.objects) as message_set:
                    with self.assertRaisesMessage(TypeError, u'is not applicant action'):
                        action.send_by_email()
                self.assertEqual(message_set.count(), 0)
Esempio n. 44
0
    def test_deadline_field_is_saved(self):
        scenario = self._create_scenario()
        data = self._create_post_data(branch=scenario.branch, deadline=13)
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            response = self.client.post(url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        action = action_set.get()

        self.assertEqual(action.deadline, 13)
Esempio n. 45
0
    def test_post_with_default_button_and_invalid_data_does_not_create_action(
            self):
        scenario = self._create_scenario()
        data = self._create_post_data(branch=u'invalid')
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            response = self.client.post(
                url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        self.assertFalse(action_set.exists())
Esempio n. 46
0
    def test_disclosure_level_field_is_saved(self):
        scenario = self._create_scenario()
        data = self._create_post_data(branch=scenario.branch, disclosure_level=Action.DISCLOSURE_LEVELS.FULL)
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            response = self.client.post(url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        action = action_set.get()

        self.assertEqual(action.disclosure_level, Action.DISCLOSURE_LEVELS.FULL)
Esempio n. 47
0
    def test_post_with_draft_button_and_invalid_data_does_not_create_new_draft_instance(
            self):
        scenario = self._create_scenario()
        data = self._create_post_data(button=u'draft', branch=u'invalid')
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(ActionDraft.objects) as actiondraft_set:
            response = self.client.post(
                url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        self.assertFalse(actiondraft_set.exists())
Esempio n. 48
0
    def test_refusal_reason_field_is_saved(self):
        scenario = self._create_scenario()
        data = self._create_post_data(branch=scenario.branch, refusal_reason=Action.REFUSAL_REASONS.PERSONAL)
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            response = self.client.post(url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        action = action_set.get()

        self.assertEqual(action.refusal_reason, Action.REFUSAL_REASONS.PERSONAL)
Esempio n. 49
0
    def test_post_with_default_button_and_invalid_data_does_not_add_expiration_if_expired(self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        scenario = self._create_scenario(inforequest_scenario=[u'request'])
        data = self._create_post_data(branch=u'invalid')
        url = self._create_url(scenario)

        timewarp.jump(local_datetime_from_local(u'2010-08-06 10:33:00'))
        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            response = self.client.post(url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        self.assertFalse(action_set.exists())
Esempio n. 50
0
    def test_expirations_added_for_inforequest_with_multiple_branches(self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        _, branch, actions = self._create_inforequest_scenario((
            u'advancement',
            [u'confirmation'],
            [u'clarification_request'],
            [u'clarification_request', u'clarification_response'],
        ))
        _, (_, [(branch1, _), (branch2, _), (branch3, _)]) = actions

        timewarp.jump(local_datetime_from_local(u'2010-10-05 10:33:00'))
        with created_instances(branch.action_set) as action_set:
            with created_instances(branch1.action_set) as action_set1:
                with created_instances(branch2.action_set) as action_set2:
                    with created_instances(branch3.action_set) as action_set3:
                        self._call_cron_job()
        self.assertFalse(action_set.exists())
        self.assertTrue(action_set1.exists())
        self.assertFalse(action_set2.exists())
        self.assertTrue(action_set3.exists())
Esempio n. 51
0
    def test_post_with_default_button_and_valid_data_adds_expiration_if_expired(self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        scenario = self._create_scenario(inforequest_scenario=[u'request'])
        data = self._create_post_data(branch=scenario.branch)
        url = self._create_url(scenario)

        timewarp.jump(local_datetime_from_local(u'2010-08-06 10:33:00'))
        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            response = self.client.post(url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        action_types = [a.type for a in action_set.all()]
        self.assertEqual(action_types, [Action.TYPES.EXPIRATION, Action.TYPES.APPEAL])
Esempio n. 52
0
    def test_post_with_default_button_and_valid_data_creates_action_with_effective_date_today(self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        scenario = self._create_scenario()
        data = self._create_post_data(branch=scenario.branch)
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            response = self.client.post(url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        action = action_set.get()

        self.assertEqual(action.effective_date, naive_date(u'2010-03-05'))
Esempio n. 53
0
    def test_post_with_draft_button_and_valid_data_creates_new_draft_instance(self):
        scenario = self._create_scenario()
        data = self._create_post_data(button=u'draft', branch=scenario.branch)
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(ActionDraft.objects) as actiondraft_set:
            response = self.client.post(url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        draft = actiondraft_set.get()

        self.assertEqual(draft.inforequest, scenario.inforequest)
        self.assertEqual(draft.branch, scenario.branch)
Esempio n. 54
0
    def test_post_with_default_button_and_invalid_data_does_not_add_expiration_if_expired(
            self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        scenario = self._create_scenario(inforequest_scenario=[u'request'])
        data = self._create_post_data(branch=u'invalid')
        url = self._create_url(scenario)

        timewarp.jump(local_datetime_from_local(u'2010-08-06 10:33:00'))
        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            response = self.client.post(
                url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        self.assertFalse(action_set.exists())
Esempio n. 55
0
    def test_post_with_submit_button_and_valid_data_does_not_send_inforequest_email_if_exception_raised(
            self):
        user = self._create_user(first_name=u'John', last_name=u'Smith')
        data = self._create_post_data(button=u'submit')

        self._login_user(user)
        with created_instances(Message.objects) as message_set:
            with patch_with_exception(
                    u'chcemvediet.apps.inforequests.views.HttpResponseRedirect'
            ):
                response = self.client.post(reverse(u'inforequests:create'),
                                            data)
        self.assertFalse(message_set.exists())
Esempio n. 56
0
    def test_received_message_is_assigned_and_marked_undecided(self):
        inforequest = self._create_inforequest()
        msg = self._create_message(omit=[u'received_for'])
        self._create_recipient(message=msg, mail=inforequest.unique_email)

        with created_instances(InforequestEmail.objects) as rel_set:
            assign_email_on_message_received(sender=None, message=msg)
        rel = rel_set.get()

        self.assertEqual(rel.inforequest, inforequest)
        self.assertEqual(rel.email, msg)
        self.assertEqual(rel.type, InforequestEmail.TYPES.UNDECIDED)
        self.assertItemsEqual(msg.inforequest_set.all(), [inforequest])
Esempio n. 57
0
    def test_post_with_invalid_data_does_not_create_action_instance(self):
        scenario = self._create_scenario()
        data = self._create_post_data(branch=u'invalid')
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(Action.objects) as action_set:
            response = self.client.post(
                url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        self.assertFalse(action_set.exists())

        scenario.rel = InforequestEmail.objects.get(pk=scenario.rel.pk)
        self.assertEqual(scenario.rel.type, InforequestEmail.TYPES.UNDECIDED)
Esempio n. 58
0
    def test_post_with_default_button_and_valid_data_does_not_create_action_if_exception_raised(
            self):
        scenario = self._create_scenario()
        data = self._create_post_data(branch=scenario.branch)
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            with patch_with_exception(
                    u'chcemvediet.apps.inforequests.views.JsonResponse'):
                response = self.client.post(
                    url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        self.assertFalse(action_set.exists())
Esempio n. 59
0
    def test_post_with_default_button_and_valid_data_creates_action_with_effective_date_today(
            self):
        timewarp.jump(local_datetime_from_local(u'2010-03-05 10:33:00'))
        scenario = self._create_scenario()
        data = self._create_post_data(branch=scenario.branch)
        url = self._create_url(scenario)

        self._login_user()
        with created_instances(scenario.branch.action_set) as action_set:
            response = self.client.post(
                url, data, HTTP_X_REQUESTED_WITH=u'XMLHttpRequest')
        action = action_set.get()

        self.assertEqual(action.effective_date, naive_date(u'2010-03-05'))
Esempio n. 60
0
    def test_send_by_email_subject_and_content(self):
        inforequest = self._create_inforequest()
        branch = self._create_branch(inforequest=inforequest)
        action = self._create_action(branch=branch,
                                     subject=u'Subject',
                                     content=u'Content')

        with created_instances(Message.objects) as message_set:
            action.send_by_email()
        email = message_set.get()

        self.assertEqual(email.subject, u'Subject')
        self.assertEqual(email.text, u'Content')
        self.assertEqual(email.html, u'')