Exemplo n.º 1
0
def set_message_sender(request, slug, message_id):
    foirequest = get_object_or_404(FoiRequest, slug=slug)
    try:
        message = FoiMessage.objects.get(request=foirequest,
                pk=int(message_id))
    except (ValueError, FoiMessage.DoesNotExist):
        raise Http404
    if not request.user.is_authenticated():
        return render_403(request)
    if request.user != foirequest.user:
        return render_403(request)
    if not message.is_response:
        return render_400(request)
    form = MessagePublicBodySenderForm(message, request.POST)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(message.get_absolute_url())
    messages.add_message(request, messages.ERROR,
            form._errors['sender'][0])
    return render_400(request)
Exemplo n.º 2
0
    def test_set_message_sender(self):
        from foirequest.forms import MessagePublicBodySenderForm
        mail.outbox = []
        self.client.login(username="******", password="******")
        pb = PublicBody.objects.all()[0]
        post = {"subject": "A simple test request",
                "body": "This is another test body",
                "law": str(FoiLaw.get_default_law().id),
                "public_body": str(pb.id),
                "public": "on"}
        response = self.client.post(
                reverse('foirequest-submit_request'), post)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(len(mail.outbox), 2)
        req = FoiRequest.objects.get(title=post['subject'])
        req.add_message_from_email({
            'msgobj': None,
            'date': (datetime.now() + timedelta(days=1), 0),
            'subject': u"Re: %s" % req.title,
            'body': u"""Message""",
            'html': None,
            'from': ("FoI Officer", "*****@*****.**"),
            'to': [(req.user.get_full_name(), req.secret_address)],
            'cc': [],
            'resent_to': [],
            'resent_cc': [],
            'attachments': []
        }, "FAKE_ORIGINAL")
        self.assertEqual(len(req.messages), 2)
        self.assertEqual(len(mail.outbox), 3)
        notification = mail.outbox[-1]
        match = re.search('https?://[^/]+(/.*?/%d/[^\s]+)' % req.user.pk,
                notification.body)
        self.assertIsNotNone(match)
        url = match.group(1)
        self.client.logout()
        response = self.client.get(reverse('account-show'))
        self.assertEqual(response.status_code, 302)
        response = self.client.get(url)
        self.assertEqual(response.status_code, 302)
        message = req.messages[1]
        self.assertIn(req.get_absolute_url(), response['Location'])
        response = self.client.get(reverse('account-show'))
        self.assertEqual(response.status_code, 200)
        form = MessagePublicBodySenderForm(message)
        post_var = form.add_prefix("sender")
        self.assertTrue(message.is_response)
        alternate_pb = PublicBody.objects.all()[1]
        response = self.client.post(
                reverse('foirequest-set_message_sender',
                kwargs={"slug": req.slug, "message_id": "9"*8}),
                {post_var: alternate_pb.id})
        self.assertEqual(response.status_code, 404)
        self.assertNotEqual(message.sender_public_body, alternate_pb)

        self.client.logout()
        response = self.client.post(
                reverse('foirequest-set_message_sender',
                kwargs={"slug": req.slug, "message_id": str(message.pk)}),
                {post_var: alternate_pb.id})
        self.assertEqual(response.status_code, 403)
        self.assertNotEqual(message.sender_public_body, alternate_pb)

        self.client.login(username="******", password="******")
        response = self.client.post(
                reverse('foirequest-set_message_sender',
                kwargs={"slug": req.slug, "message_id": str(message.pk)}),
                {post_var: alternate_pb.id})
        self.assertEqual(response.status_code, 403)
        self.assertNotEqual(message.sender_public_body, alternate_pb)

        self.client.logout()
        self.client.login(username="******", password="******")
        mes = req.messages[0]
        response = self.client.post(
                reverse('foirequest-set_message_sender',
                kwargs={"slug": req.slug, "message_id": str(mes.pk)}),
                {post_var: str(alternate_pb.id)})
        self.assertEqual(response.status_code, 400)
        self.assertNotEqual(message.sender_public_body, alternate_pb)

        response = self.client.post(
                reverse('foirequest-set_message_sender',
                kwargs={"slug": req.slug,
                    "message_id": message.pk}),
                {post_var: "9" * 5})
        self.assertEqual(response.status_code, 400)
        self.assertNotEqual(message.sender_public_body, alternate_pb)

        response = self.client.post(
                reverse('foirequest-set_message_sender',
                kwargs={"slug": req.slug,
                    "message_id": message.pk}),
                {post_var: str(alternate_pb.id)})
        self.assertEqual(response.status_code, 302)
        message = FoiMessage.objects.get(pk=message.pk)
        self.assertEqual(message.sender_public_body, alternate_pb)