|
@@ -1,11 +1,17 @@
|
|
|
+import datetime
|
|
|
import json
|
|
|
+import warnings
|
|
|
|
|
|
from django.core import mail
|
|
|
+from django.core.signing import BadSignature
|
|
|
from django.contrib.auth.models import User
|
|
|
from django.test import TestCase, Client, override_settings
|
|
|
+from freezegun import freeze_time
|
|
|
+import pytz
|
|
|
|
|
|
from contribmap.models import Contrib
|
|
|
from contribmap.forms import PublicContribForm
|
|
|
+from contribmap.tokens import ContribTokenManager, URLTokenManager
|
|
|
|
|
|
|
|
|
class APITestClient(Client):
|
|
@@ -84,6 +90,26 @@ class TestContribPrivacy(TestCase):
|
|
|
|
|
|
|
|
|
class TestViews(APITestCase):
|
|
|
+ def mk_contrib_post_data(self, *args, **kwargs):
|
|
|
+ post_data = {
|
|
|
+ 'roof': True,
|
|
|
+ 'privacy_place_details': True,
|
|
|
+ 'privacy_coordinates': True,
|
|
|
+ 'phone': '0202020202',
|
|
|
+ 'orientations': ('N', 'NO', 'O', 'SO', 'S', 'SE', 'E', 'NE'),
|
|
|
+ 'orientation': 'all',
|
|
|
+ 'name': 'JohnCleese',
|
|
|
+ 'longitude': -1.553621,
|
|
|
+ 'latitude': 47.218371,
|
|
|
+ 'floor_total': '2',
|
|
|
+ 'floor': 1,
|
|
|
+ 'email': 'coucou@example.com',
|
|
|
+ 'contrib_type': 'connect',
|
|
|
+ 'connect_local': 'on',
|
|
|
+ }
|
|
|
+ post_data.update(kwargs)
|
|
|
+ return post_data
|
|
|
+
|
|
|
def test_public_json(self):
|
|
|
response = self.client.json_get('/map/public.json')
|
|
|
self.assertEqual(response.status_code, 200)
|
|
@@ -115,37 +141,107 @@ class TestViews(APITestCase):
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
@override_settings(NOTIFICATION_EMAILS=['foo@example.com'])
|
|
|
- def test_add_contrib_sends_email(self):
|
|
|
- response = self.client.post('/map/contribute', {
|
|
|
- 'roof': True,
|
|
|
- 'privacy_place_details': True,
|
|
|
- 'privacy_coordinates': True,
|
|
|
- 'phone': '0202020202',
|
|
|
- 'orientations': 'N',
|
|
|
- 'orientations': 'NO',
|
|
|
- 'orientations': 'O',
|
|
|
- 'orientations': 'SO',
|
|
|
- 'orientations': 'S',
|
|
|
- 'orientations': 'SE',
|
|
|
- 'orientations': 'E',
|
|
|
- 'orientations': 'NE',
|
|
|
- 'orientation': 'all',
|
|
|
- 'name': 'JohnCleese',
|
|
|
- 'longitude': -1.553621,
|
|
|
- 'latitude': 47.218371,
|
|
|
- 'floor_total': '2',
|
|
|
- 'floor': 1,
|
|
|
- 'email': 'coucou@example.com',
|
|
|
- 'contrib_type': 'connect',
|
|
|
- 'connect_local': 'on',
|
|
|
- })
|
|
|
+ def test_add_contrib_sends_moderator_email(self):
|
|
|
+ post_data = self.mk_contrib_post_data({'name': 'JohnCleese'})
|
|
|
+ del post_data['email']
|
|
|
+
|
|
|
+ response = self.client.post('/map/contribute', post_data)
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
self.assertIn('JohnCleese', mail.outbox[0].subject)
|
|
|
self.assertIn('JohnCleese', mail.outbox[0].body)
|
|
|
+ self.assertEqual(mail.outbox[0].recipients(), ['foo@example.com'])
|
|
|
+
|
|
|
+ def test_add_contrib_sends_no_author_email(self):
|
|
|
+ # Send no email if author did not mentioned an email
|
|
|
+ post_data = self.mk_contrib_post_data()
|
|
|
+ del post_data['email']
|
|
|
+
|
|
|
+ response = self.client.post('/map/contribute', post_data)
|
|
|
+ self.assertEqual(response.status_code, 302)
|
|
|
+ self.assertEqual(len(mail.outbox), 0)
|
|
|
+
|
|
|
+ def test_add_contrib_sends_author_email(self):
|
|
|
+ # Send no email if author did not mentioned an email
|
|
|
+ response = self.client.post(
|
|
|
+ '/map/contribute',
|
|
|
+ self.mk_contrib_post_data(email='author@example.com'))
|
|
|
+ self.assertEqual(response.status_code, 302)
|
|
|
+ self.assertEqual(len(mail.outbox), 1)
|
|
|
+
|
|
|
+
|
|
|
+class TestManageView(APITestCase):
|
|
|
+ def setUp(self):
|
|
|
+ self.contrib = Contrib.objects.create(
|
|
|
+ name='John',
|
|
|
+ phone='010101010101',
|
|
|
+ contrib_type=Contrib.CONTRIB_CONNECT,
|
|
|
+ privacy_coordinates=True,
|
|
|
+ latitude=0.5,
|
|
|
+ longitude=0.5,
|
|
|
+ )
|
|
|
+ self.token = ContribTokenManager().mk_token(self.contrib)
|
|
|
+
|
|
|
+ def test_manage_with_token(self):
|
|
|
+ # No token
|
|
|
+ response = self.client.get('/map/manage/{}'.format(self.contrib.pk))
|
|
|
+ self.assertEqual(response.status_code, 403)
|
|
|
+
|
|
|
+ # Garbage token
|
|
|
+ response = self.client.get(
|
|
|
+ '/map/manage/{}?token=burp'.format(self.contrib.pk))
|
|
|
+ self.assertEqual(response.status_code, 403)
|
|
|
+
|
|
|
+ # Valid token, but for another contrib
|
|
|
+ contrib2 = Contrib.objects.create(
|
|
|
+ name='John2',
|
|
|
+ phone='010101010101',
|
|
|
+ contrib_type=Contrib.CONTRIB_CONNECT,
|
|
|
+ privacy_coordinates=True,
|
|
|
+ latitude=0.5,
|
|
|
+ longitude=0.5,
|
|
|
+ )
|
|
|
+ token2 = ContribTokenManager().mk_token(contrib2)
|
|
|
+
|
|
|
+ response = self.client.get('/map/manage/{}?token={}'.format(
|
|
|
+ self.contrib.pk, token2))
|
|
|
+ self.assertEqual(response.status_code, 403)
|
|
|
+
|
|
|
+ # Normal legitimate access case
|
|
|
+ response = self.client.get(
|
|
|
+ '/map/manage/{}?token={}'.format(self.contrib.pk, self.token))
|
|
|
+ self.assertEqual(response.status_code, 200)
|
|
|
+
|
|
|
+ # Deleted contrib
|
|
|
+ Contrib.objects.all().delete()
|
|
|
+ response = self.client.get(
|
|
|
+ '/map/manage/{}?token={}'.format(self.contrib.pk, self.token))
|
|
|
+ self.assertEqual(response.status_code, 404)
|
|
|
+
|
|
|
+ def test_delete(self):
|
|
|
+ response = self.client.post(
|
|
|
+ '/map/manage/{}?token={}'.format(self.contrib.pk, self.token),
|
|
|
+ {'action': 'delete'})
|
|
|
+ self.assertEqual(response.status_code, 302)
|
|
|
+ self.assertFalse(Contrib.objects.filter(pk=self.contrib.pk).exists())
|
|
|
+
|
|
|
+ def test_renew(self):
|
|
|
+ self.contrib.date = datetime.datetime(2009, 10, 10, tzinfo=pytz.utc)
|
|
|
+ self.contrib.expiration_date = datetime.datetime(2010, 10, 10, tzinfo=pytz.utc)
|
|
|
+ self.contrib.save()
|
|
|
+
|
|
|
+ with freeze_time('12-12-2100', tz_offset=0):
|
|
|
+ response = self.client.post(
|
|
|
+ '/map/manage/{}?token={}'.format(self.contrib.pk, self.token),
|
|
|
+ {'action': 'renew'})
|
|
|
+ self.assertEqual(response.status_code, 200)
|
|
|
+ self.contrib = Contrib.objects.get(pk=self.contrib.pk) # refresh
|
|
|
+ self.assertEqual(
|
|
|
+ self.contrib.expiration_date.date(),
|
|
|
+ datetime.date(2101, 12, 12))
|
|
|
+
|
|
|
|
|
|
-<<<<<<< HEAD
|
|
|
class TestForms(TestCase):
|
|
|
valid_data = {
|
|
|
'roof': True,
|
|
@@ -203,30 +299,9 @@ class TestForms(TestCase):
|
|
|
|
|
|
@override_settings(NOTIFICATION_EMAILS=['foo@example.com'])
|
|
|
def test_add_contrib_like_a_robot(self):
|
|
|
- response = self.client.post('/map/contribute', {
|
|
|
- 'roof': True,
|
|
|
- 'human_field': 'should not have no value',
|
|
|
- 'privacy_place_details': True,
|
|
|
- 'privacy_coordinates': True,
|
|
|
- 'phone': '0202020202',
|
|
|
- 'orientations': 'N',
|
|
|
- 'orientations': 'NO',
|
|
|
- 'orientations': 'O',
|
|
|
- 'orientations': 'SO',
|
|
|
- 'orientations': 'S',
|
|
|
- 'orientations': 'SE',
|
|
|
- 'orientations': 'E',
|
|
|
- 'orientations': 'NE',
|
|
|
- 'orientation': 'all',
|
|
|
- 'name': 'JohnCleese',
|
|
|
- 'longitude': -1.553621,
|
|
|
- 'latitude': 47.218371,
|
|
|
- 'floor_total': '2',
|
|
|
- 'floor': 1,
|
|
|
- 'email': 'coucou@example.com',
|
|
|
- 'contrib_type': 'connect',
|
|
|
- 'connect_local': 'on',
|
|
|
- })
|
|
|
+ robot_data = self.valid_data.copy()
|
|
|
+ robot_data['human_field'] = 'should contain no value'
|
|
|
+ response = self.client.post('/map/contribute', robot_data)
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
|
@@ -234,7 +309,48 @@ class TestForms(TestCase):
|
|
|
class TestDataImport(TestCase):
|
|
|
fixtures = ['bottle_data.yaml']
|
|
|
|
|
|
+ @classmethod
|
|
|
+ def setUpClass(cls, *args, **kwargs):
|
|
|
+ # Silence the warnings about naive datetimes contained in the yaml.
|
|
|
+ with warnings.catch_warnings(): # Scope warn catch to this block
|
|
|
+ warnings.simplefilter('ignore', RuntimeWarning)
|
|
|
+ return super().setUpClass(*args, **kwargs)
|
|
|
+
|
|
|
def test_re_save(self):
|
|
|
for contrib in Contrib.objects.all():
|
|
|
contrib.full_clean()
|
|
|
contrib.save()
|
|
|
+
|
|
|
+
|
|
|
+class URLTokenManagerTests(TestCase):
|
|
|
+ def test_sign_unsign_ok(self):
|
|
|
+ input_data = {'foo': 12}
|
|
|
+ at = URLTokenManager().sign(input_data)
|
|
|
+ output_data = URLTokenManager().unsign(at)
|
|
|
+ self.assertEqual(input_data, output_data)
|
|
|
+
|
|
|
+ def test_sign_unsign_wrong_sig(self):
|
|
|
+ with self.assertRaises(BadSignature):
|
|
|
+ URLTokenManager().unsign(
|
|
|
+ b'eyJmb28iOiAxfTpvUFZ1Q3FsSldtQ2htMXJBMmx5VFV0ZWxDLWM')
|
|
|
+
|
|
|
+
|
|
|
+class ContribTokenManagerTests(TestCase):
|
|
|
+ def test_sign_unsign_ok(self):
|
|
|
+ Contrib.objects.create(
|
|
|
+ name='John2',
|
|
|
+ phone='010101020101',
|
|
|
+ contrib_type=Contrib.CONTRIB_CONNECT,
|
|
|
+ privacy_coordinates=True,
|
|
|
+ latitude=0.1,
|
|
|
+ longitude=0.12,
|
|
|
+ )
|
|
|
+
|
|
|
+ contrib = Contrib.objects.all().first()
|
|
|
+
|
|
|
+ manager = ContribTokenManager()
|
|
|
+ token = manager.mk_token(contrib)
|
|
|
+
|
|
|
+ self.assertEqual(
|
|
|
+ manager.get_instance_if_allowed(token, contrib.pk),
|
|
|
+ contrib)
|