from mapentity.factories import SuperUserFactory
from django.utils.translation import ugettext_lazy as _
from django.test import LiveServerTestCase

from geotrek.common.tests import CommonTest
from geotrek.feedback import models as feedback_models
from geotrek.feedback import factories as feedback_factories


class ReportViewsTest(CommonTest):
    model = feedback_models.Report
    modelfactory = feedback_factories.ReportFactory
    userfactory = SuperUserFactory

    def get_bad_data(self):
        return {'geom': 'FOO'}, _('Invalid geometry value.')

    def get_good_data(self):
        return {
            'geom': '{"type": "Point", "coordinates": [0, 0]}',
            'name': 'You Yeah',
            'email': 'yeah@you.com',
        }


class CreateReportsAPITest(LiveServerTestCase):
    pass
@@ -1,52 +1,20 @@
-from mapentity.factories import SuperUserFactory
+import requests%0A
 %0Afro
@@ -108,16 +108,172 @@
 TestCase
+%0Afrom django.contrib.auth.models import Permission%0Afrom mapentity.factories import SuperUserFactory, UserFactory%0Afrom mapentity.helpers import smart_urljoin
 %0A%0Afrom g
@@ -923,8 +923,1750 @@
     
-pass
+def setUp(self):%0A        self.user = UserFactory(password='booh')%0A        perm = Permission.objects.get_by_natural_key('add_report', 'feedback', 'report')%0A        self.user.user_permissions.add(perm)%0A%0A        self.login_url = smart_urljoin(self.live_server_url, '/login/')%0A        self.add_url = smart_urljoin(self.live_server_url, '/report/add/')%0A%0A        self.session = requests.Session()%0A        self.login()%0A%0A    def login(self):%0A        response = self.session.get(self.login_url)%0A        csrftoken = response.cookies.get('csrftoken', '')%0A        response = self.session.post(self.login_url,%0A                                     %7B'username': self.user.username,%0A                                      'password': 'booh',%0A                                      'csrfmiddlewaretoken': csrftoken%7D,%0A                                     allow_redirects=False)%0A        self.assertEqual(response.status_code, 302)%0A%0A    def post_report_data(self, data):%0A        response = self.session.get(self.add_url,%0A                                    allow_redirects=False)%0A        self.assertEqual(response.status_code, 200)%0A        csrf = response.cookies%5B'csrftoken'%5D%0A        data%5B'csrfmiddlewaretoken'%5D = csrf%0A        response = self.session.post(self.add_url, data=data,%0A                                     allow_redirects=False)%0A        return response%0A%0A    def test_reports_can_be_created_using_post(self):%0A        data = %7B%0A            'geom': '%7B%22type%22: %22Point%22, %22coordinates%22: %5B0, 0%5D%7D',%0A            'name': 'You Yeah',%0A            'email': 'yeah@you.com'%0A        %7D%0A        response = self.post_report_data(data)%0A        self.assertEqual(response.status_code, 302)%0A        self.assertTrue(feedback_models.Report.objects.filter(name='You Yeah').exists())%0A

Test creation of reports via simple POST