Source

django-leaflet-storage / leaflet_storage / tests / views.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import os

from django.test import TransactionTestCase
from django.utils import simplejson
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.test.utils import override_settings
from django.core.signing import get_cookie_signer

from leaflet_storage.models import Map, Category, Marker, Polygon, Polyline

from .base import (MapFactory, CategoryFactory, MarkerFactory,
                   UserFactory, BaseTest)


@override_settings(LEAFLET_STORAGE_ALLOW_ANONYMOUS=False)
class MapViews(BaseTest):

    def test_quick_create_GET(self):
        url = reverse('map_add')
        self.client.login(username=self.user.username, password="123123")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        self.assertIn("html", json)
        self.assertIn("form", json['html'])

    def test_quick_create_POST(self):
        url = reverse('map_add')
        # POST only mendatory fields
        name = 'test-map-with-new-name'
        post_data = {
            'name': name,
            'licence': self.licence.pk
        }
        self.client.login(username=self.user.username, password="123123")
        response = self.client.post(url, post_data)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        created_map = Map.objects.latest('pk')
        self.assertEqual(json['redirect'], created_map.get_absolute_url())
        self.assertEqual(created_map.name, name)
        # A category must have been created automatically
        self.assertEqual(Category.objects.filter(map=created_map).count(), 1)
        # Default tilelayer must have been linked to the map
        self.assertEqual(created_map.tilelayers.count(), 1)
        self.assertEqual(created_map.tilelayers.all()[0], self.tilelayer)

    def test_quick_update_GET(self):
        url = reverse('map_update', kwargs={'map_id': self.map.pk})
        self.client.login(username=self.user.username, password="123123")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        self.assertIn("html", json)
        self.assertIn(self.map.name, json['html'])

    def test_quick_update_POST(self):
        url = reverse('map_update', kwargs={'map_id': self.map.pk})
        # POST only mendatory fields
        new_name = 'new map name'
        post_data = {
            'name': new_name,
            'licence': self.map.licence.pk
        }
        self.client.login(username=self.user.username, password="123123")
        response = self.client.post(url, post_data)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        self.assertNotIn("html", json)
        updated_map = Map.objects.get(pk=self.map.pk)
        self.assertEqual(json['redirect'], updated_map.get_absolute_url())
        self.assertEqual(updated_map.name, new_name)

    def test_delete_GET(self):
        url = reverse('map_delete', args=(self.map.pk, ))
        self.client.login(username=self.user.username, password="123123")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        self.assertIn("html", json)
        self.assertIn("form", json['html'])

    def test_delete_POST(self):
        # create some features
        marker1 = MarkerFactory(category=self.category)
        marker2 = MarkerFactory(category=self.category)
        marker3 = MarkerFactory(category=self.category)
        url = reverse('map_delete', args=(self.map.pk, ))
        post_data = {
            'confirm': "yes",
        }
        self.client.login(username=self.user.username, password="123123")
        response = self.client.post(url, post_data, follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Map.objects.filter(pk=self.map.pk).count(), 0)
        self.assertEqual(Category.objects.filter(pk=self.category.pk).count(), 0)
        # Check that features have been delete
        self.assertEqual(Marker.objects.filter(pk=marker1.pk).count(), 0)
        self.assertEqual(Marker.objects.filter(pk=marker2.pk).count(), 0)
        self.assertEqual(Marker.objects.filter(pk=marker3.pk).count(), 0)
        # Check that user has not been impacted
        self.assertEqual(User.objects.filter(pk=self.user.pk).count(), 1)
        # Test response is a json
        json = simplejson.loads(response.content)
        self.assertIn("redirect", json)

    def test_short_url_should_redirect_to_canonical(self):
        url = reverse('map_short_url', kwargs={'pk': self.map.pk})
        canonical = reverse('map', kwargs={'pk': self.map.pk, 'slug': self.map.slug})
        response = self.client.get(url)
        self.assertRedirects(response, canonical, status_code=301)

    def test_old_url_should_redirect_to_canonical(self):
        url = reverse(
            'map_old_url',
            kwargs={'username': self.map.owner.username, 'slug': self.map.slug}
        )
        canonical = reverse('map', kwargs={'pk': self.map.pk, 'slug': self.map.slug})
        response = self.client.get(url)
        self.assertRedirects(response, canonical, status_code=301)


@override_settings(LEAFLET_STORAGE_ALLOW_ANONYMOUS=True)
class AnonymousMapViews(BaseTest):

    def setUp(self):
        super(AnonymousMapViews, self).setUp()
        self.anonymous_map = MapFactory(
            name="an-anonymous-map",
            owner=None,
            licence=self.licence
        )
        key, value = self.anonymous_map.signed_cookie_elements
        self.anonymous_cookie_key = key
        self.anonymous_cookie_value = get_cookie_signer(salt=key).sign(value)

    def test_quick_create_GET(self):
        url = reverse('map_add')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        self.assertIn("html", json)
        self.assertIn("form", json['html'])

    def test_quick_create_POST(self):
        url = reverse('map_add')
        # POST only mendatory fields
        name = 'test-map-with-new-name'
        post_data = {
            'name': name,
            'licence': self.licence.pk
        }
        response = self.client.post(url, post_data)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        created_map = Map.objects.latest('pk')
        self.assertEqual(json['redirect'], created_map.get_absolute_url())
        self.assertEqual(created_map.name, name)
        key, value = created_map.signed_cookie_elements
        self.assertIn(key, self.client.cookies)

    def test_quick_update_no_cookie_GET(self):
        url = reverse('map_update', kwargs={'map_id': self.anonymous_map.pk})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        self.assertNotIn("html", json)
        self.assertIn("login_required", json)

    def test_quick_update_no_cookie_POST(self):
        url = reverse('map_update', kwargs={'map_id': self.anonymous_map.pk})
        # POST only mendatory fields
        new_name = 'new map name'
        post_data = {
            'name': new_name,
            'licence': self.anonymous_map.licence.pk
        }
        response = self.client.post(url, post_data)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        self.assertNotIn("html", json)
        self.assertIn("login_required", json)

    def test_quick_update_with_cookie_GET(self):
        url = reverse('map_update', kwargs={'map_id': self.anonymous_map.pk})
        self.client.cookies[self.anonymous_cookie_key] = self.anonymous_cookie_value
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        self.assertIn("html", json)

    def test_quick_update_with_cookie_POST(self):
        url = reverse('map_update', kwargs={'map_id': self.anonymous_map.pk})
        self.client.cookies[self.anonymous_cookie_key] = self.anonymous_cookie_value
        # POST only mendatory fields
        new_name = 'new map name'
        post_data = {
            'name': new_name,
            'licence': self.anonymous_map.licence.pk
        }
        response = self.client.post(url, post_data)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        updated_map = Map.objects.get(pk=self.anonymous_map.pk)
        self.assertEqual(json['redirect'], updated_map.get_absolute_url())

    def test_anonymous_edit_url(self):
        url = self.anonymous_map.get_anonymous_edit_url()
        canonical = reverse(
            'map',
            kwargs={'pk': self.anonymous_map.pk, 'slug': self.anonymous_map.slug}
        )
        response = self.client.post(url)
        self.assertRedirects(response, canonical, status_code=302)
        key, value = self.anonymous_map.signed_cookie_elements
        self.assertIn(key, self.client.cookies)


@override_settings(LEAFLET_STORAGE_ALLOW_ANONYMOUS=False)
class ViewsPermissionsTest(BaseTest):

    def setUp(self):
        super(ViewsPermissionsTest, self).setUp()
        self.other_user = UserFactory(username="Bob", password="123123")

    def check_url_permissions(self, url):
        # GET anonymous
        response = self.client.get(url)
        self.assertLoginRequired(response)
        # POST anonymous
        response = self.client.post(url, {})
        self.assertLoginRequired(response)
        # GET with wrong permissions
        self.client.login(username=self.other_user.username, password="123123")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 403)
        # POST with wrong permissions
        self.client.login(username=self.other_user.username, password="123123")
        response = self.client.post(url, {})
        self.assertEqual(response.status_code, 403)


@override_settings(LEAFLET_STORAGE_ALLOW_ANONYMOUS=False)
class MapViewsPermissions(ViewsPermissionsTest):

    def test_map_add_permissions(self):
        url = reverse('map_add')
        # GET anonymous
        response = self.client.get(url)
        self.assertLoginRequired(response)
        # POST anonymous
        response = self.client.post(url, {})
        self.assertLoginRequired(response)

    def test_map_update_permissions(self):
        url = reverse('map_update', kwargs={'map_id': self.map.pk})
        self.check_url_permissions(url)

    def test_upload_data_permissions(self):
        url = reverse('upload_data', kwargs={'map_id': self.map.pk})
        self.check_url_permissions(url)

    def test_map_update_extend_permissions(self):
        # only POST is available for this view
        url = reverse('map_update_extent', kwargs={'map_id': self.map.pk})
        # POST anonymous
        response = self.client.post(url, {})
        self.assertLoginRequired(response)
        # POST with wrong permissions
        self.client.login(username=self.other_user.username, password="123123")
        response = self.client.post(url, {})
        self.assertEqual(response.status_code, 403)

    def test_embed_view_should_be_open_bar(self):
        url = reverse('map_embed', kwargs={'map_id': self.map.pk})
        # HTTP_HOST is needed by the view for now
        response = self.client.get(url, HTTP_HOST="local.local")
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        self.assertIn("html", json)
        self.assertIn("iframe", json['html'])

    def test_only_owner_can_delete(self):
        self.map.editors.add(self.other_user)
        url = reverse('map_delete', kwargs={'map_id': self.map.pk})
        post_data = {
            'confirm': "yes",
        }
        self.client.login(username=self.other_user.username, password="123123")
        response = self.client.post(url, post_data, follow=True)
        self.assertEqual(response.status_code, 403)


@override_settings(LEAFLET_STORAGE_ALLOW_ANONYMOUS=False)
class MarkerViewsPermissions(ViewsPermissionsTest):

    def test_marker_add(self):
        url = reverse('marker_add', kwargs={'map_id': self.map.pk})
        self.check_url_permissions(url)

    def test_marker_update(self):
        marker = MarkerFactory(category=self.category)
        url = reverse('marker_update', kwargs={'map_id': self.map.pk, 'pk': marker.pk})
        self.check_url_permissions(url)

    def test_marker_delete(self):
        marker = MarkerFactory(category=self.category)
        url = reverse('marker_delete', kwargs={'map_id': self.map.pk, 'pk': marker.pk})
        self.check_url_permissions(url)


class MarkerViews(BaseTest):

    def test_add_GET(self):
        url = reverse('marker_add', args=(self.map.pk, ))
        self.client.login(username=self.user.username, password="123123")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        self.assertIn("html", json)
        self.assertIn("form", json['html'])

    def test_add_POST(self):
        url = reverse('marker_add', args=(self.map.pk, ))
        name = 'test-marker'
        post_data = {
            'name': name,
            'category': self.category.pk,
            'latlng': '{"type": "Point","coordinates": [-0.1318359375,51.474540439419755]}'
        }
        self.client.login(username=self.user.username, password="123123")
        response = self.client.post(url, post_data, follow=True)
        self.assertEqual(response.status_code, 200)
        created_marker = Marker.objects.latest('pk')
        self.assertEqual(created_marker.name, name)
        # Test response is a json
        json = simplejson.loads(response.content)
        self.assertIn("geometry", json)

    def test_delete_GET(self):
        marker = MarkerFactory(category=self.category)
        url = reverse('marker_delete', args=(self.map.pk, marker.pk))
        self.client.login(username=self.user.username, password="123123")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        self.assertIn("html", json)
        self.assertIn("form", json['html'])

    def test_delete_POST(self):
        marker = MarkerFactory(category=self.category)
        url = reverse('marker_delete', args=(self.map.pk, marker.pk))
        post_data = {
            'confirm': "yes",
        }
        self.client.login(username=self.user.username, password="123123")
        response = self.client.post(url, post_data, follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Marker.objects.filter(pk=marker.pk).count(), 0)
        # Check that category and map have not been impacted
        self.assertEqual(Map.objects.filter(pk=self.map.pk).count(), 1)
        self.assertEqual(Category.objects.filter(pk=self.category.pk).count(), 1)
        # Test response is a json
        json = simplejson.loads(response.content)
        self.assertIn("info", json)


class UploadData(TransactionTestCase):

    def setUp(self):
        self.user = UserFactory(password="123123")
        self.map = MapFactory(owner=self.user)
        self.category = CategoryFactory(map=self.map)

    def tearDown(self):
        self.user.delete()

    def process_file(self, filename, content_type):
        """
        Process a file stored in tests/fixtures/
        """
        url = reverse('upload_data', kwargs={'map_id': self.map.pk})
        current_path = os.path.dirname(os.path.realpath(__file__))
        fixture_path = os.path.join(
            current_path,
            'fixtures',
            filename
        )
        f = open(fixture_path)
        post_data = {
            'category': self.category.pk,
            'data_file': f,
            'content_type': content_type
        }
        self.client.login(username=self.user.username, password="123123")
        response = self.client.post(url, post_data)
        return response

    def test_GeoJSON_generic(self):
        # Contains tow Point, two Polygons and one Polyline
        response = self.process_file("test_upload_data.json", "geojson")
        self.client.login(username=self.user.username, password="123123")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Marker.objects.filter(category=self.category).count(), 2)
        self.assertEqual(Polygon.objects.filter(category=self.category).count(), 2)
        self.assertEqual(Polyline.objects.filter(category=self.category).count(), 1)
        # Check properties population
        self.assertEqual(Marker.objects.filter(category=self.category, name="London").count(), 1)
        marker = Marker.objects.get(category=self.category, name="London")
        self.assertEqual(marker.description, "London description")
        self.assertEqual(marker.options["color"], "Pink")
        self.assertEqual(Marker.objects.filter(category=self.category, name="Antwerpen").count(), 1)
        marker = Marker.objects.get(category=self.category, name="Antwerpen")
        self.assertEqual(marker.description, None)
        self.assertFalse("color" in marker.options)

    def test_GeoJSON_empty_coordinates_should_not_be_imported(self):
        self.assertEqual(Marker.objects.filter(category=self.category).count(), 0)
        self.assertEqual(Polyline.objects.filter(category=self.category).count(), 0)
        self.assertEqual(Polygon.objects.filter(category=self.category).count(), 0)
        response = self.process_file("test_upload_empty_coordinates.json", "geojson")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Marker.objects.filter(category=self.category).count(), 0)
        self.assertEqual(Polyline.objects.filter(category=self.category).count(), 0)
        self.assertEqual(Polygon.objects.filter(category=self.category).count(), 0)

    def test_GeoJSON_non_linear_ring_should_not_be_imported(self):
        response = self.process_file("test_upload_non_linear_ring.json", "geojson")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Polygon.objects.filter(category=self.category).count(), 0)

    def test_GeoJSON_missing_name_should_be_set_with_category_name(self):
        # One feature is missing a name
        self.assertEqual(Marker.objects.filter(category=self.category).count(), 0)
        response = self.process_file("test_upload_missing_name.json", "geojson")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Marker.objects.filter(category=self.category).count(), 2)
        self.assertEqual(Marker.objects.filter(category=self.category, name=self.category.name).count(), 1)
        self.assertEqual(Polyline.objects.filter(category=self.category).count(), 1)
        self.assertEqual(Polygon.objects.filter(category=self.category).count(), 1)

    def test_import_data_from_url(self):
        url = reverse('upload_data', kwargs={'map_id': self.map.pk})
        current_path = os.path.dirname(os.path.realpath(__file__))
        fixture_path = os.path.join(
            current_path,
            'fixtures',
            "test_upload_data.json"
        )
        post_data = {
            'category': self.category.pk,
            'data_url': "file://%s" % fixture_path,
            'content_type': 'geojson'
        }
        self.client.login(username=self.user.username, password="123123")
        response = self.client.post(url, post_data)
        response = self.process_file("test_upload_data.json", "geojson")
        self.client.login(username=self.user.username, password="123123")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Marker.objects.filter(category=self.category).count(), 2)
        self.assertEqual(Polygon.objects.filter(category=self.category).count(), 2)
        self.assertEqual(Polyline.objects.filter(category=self.category).count(), 1)

    def test_KML_generic(self):
        response = self.process_file("test_upload_data.kml", "kml")
        self.client.login(username=self.user.username, password="123123")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Polyline.objects.filter(category=self.category).count(), 1)
        self.assertEqual(Marker.objects.filter(category=self.category).count(), 1)
        self.assertEqual(Polygon.objects.filter(category=self.category).count(), 1)

    def test_GPX_generic(self):
        response = self.process_file("test_upload_data.gpx", "gpx")
        self.client.login(username=self.user.username, password="123123")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Polyline.objects.filter(category=self.category).count(), 1)
        self.assertEqual(Marker.objects.filter(category=self.category).count(), 1)


class CategoryViews(BaseTest):

    def test_delete_GET(self):
        url = reverse('category_delete', args=(self.map.pk, self.category.pk))
        self.client.login(username=self.user.username, password="123123")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        json = simplejson.loads(response.content)
        self.assertIn("html", json)
        self.assertIn("form", json['html'])

    def test_delete_POST(self):
        # create some features
        marker1 = MarkerFactory(category=self.category)
        marker2 = MarkerFactory(category=self.category)
        marker3 = MarkerFactory(category=self.category)
        url = reverse('category_delete', args=(self.map.pk, self.category.pk))
        post_data = {
            'confirm': "yes",
        }
        self.client.login(username=self.user.username, password="123123")
        response = self.client.post(url, post_data, follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Category.objects.filter(pk=self.category.pk).count(), 0)
        # Check that features have been delete
        self.assertEqual(Marker.objects.filter(pk=marker1.pk).count(), 0)
        self.assertEqual(Marker.objects.filter(pk=marker2.pk).count(), 0)
        self.assertEqual(Marker.objects.filter(pk=marker3.pk).count(), 0)
        # Check that map has not been impacted
        self.assertEqual(Map.objects.filter(pk=self.map.pk).count(), 1)
        # Test response is a json
        json = simplejson.loads(response.content)
        self.assertIn("info", json)