# Copyright (C) 2010 Linaro Limited
#
# Author: Zygmunt Krynicki <zygmunt.krynicki@linaro.org>
#
# This file is part of Launch Control.
#
# Launch Control is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License version 3
# as published by the Free Software Foundation
#
# Launch Control is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Launch Control.  If not, see <http://www.gnu.org/licenses/>.

"""
Tests for Cross-Site Request Forgery middleware configuration
"""
import xmlrpclib

import django
from django import forms
from django.conf.urls.defaults import patterns, url
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.template import Template, Context

from dashboard_app.tests.utils import CSRFTestCase
from dashboard_app.views import dashboard_xml_rpc_handler
from dashboard_app import urls


class CSRFConfigurationTestCase(CSRFTestCase):

    @property
    def urls(self):
        urlpatterns = urls.urlpatterns
        urlpatterns += patterns('', url(r'^test-form/', test_form))
        return type('urls', (), dict(urlpatterns=urlpatterns))

    def setUp(self):
        super(CSRFConfigurationTestCase, self).setUp()
        self.form_path = reverse(test_form)

    def test_csrf_token_present_in_form(self):
        if django.VERSION[:2] == (1, 1):
            # This feature is not supported on django 1.1
            return
        response = self.client.get(self.form_path)
        self.assertContains(response, "csrfmiddlewaretoken")

    def test_cross_site_form_submission_fails(self):
        if django.VERSION[:2] == (1, 1):
            # This feature is not supported on django 1.1
            return
        response = self.client.post(self.form_path, {'text': 'text'})
        self.assertEquals(response.status_code, 403)

    def test_csrf_not_protecting_xml_rpc_views(self):
        """call version and check that we didn't get 403"""
        endpoint_path = reverse(dashboard_xml_rpc_handler)
        request_body = xmlrpclib.dumps((), methodname="version")
        response = self.client.post(endpoint_path, request_body, "text/xml")
        self.assertContains(response, "<methodResponse>", status_code=200)


def test_form(request):
    t = Template(template)
    html = t.render(Context({'form': SingleTextFieldForm()}))
    return HttpResponse(html)


class SingleTextFieldForm(forms.Form):
    text = forms.CharField()


template = """
    <html>
     <body>
      <form action="." method="POST">
       <table>{{ form.as_table }}</table>
      </form>
     </body>
    </html>
    """

@@ -1020,16 +1020,23 @@
 mplate, 
+Request
 Context%0A
@@ -2592,16 +2592,23 @@
 der(
+Request
 Context(
 %7B'fo
@@ -2603,16 +2603,25 @@
 Context(
+request, 
 %7B'form':
@@ -2824,16 +2824,40 @@
 %22POST%22%3E%0A
+      %7B%25 csrf_token %25%7D %0A
        %3C

Fix csrf test to explicitly use RequestContext and csrf_token