importosfromdjango.testimportTransactionTestCasefromdjango.utilsimportsimplejsonfromdjango.core.urlresolversimportreversefromdjango.contrib.auth.modelsimportUserfromleaflet_storage.modelsimportMap,Category,Marker,Polygon,Polylinefrom.baseimport(MapFactory,CategoryFactory,MarkerFactory,UserFactory,BaseTest)classMapViews(BaseTest):deftest_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'])deftest_quick_create_POST(self):url=reverse('map_add')# POST only mendatory fieldsname='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 automaticallyself.assertEqual(Category.objects.filter(map=created_map).count(),1)# Default tilelayer must have been linked to the mapself.assertEqual(created_map.tilelayers.count(),1)self.assertEqual(created_map.tilelayers.all()[0],self.tilelayer)deftest_quick_create_with_existing_name_should_not_succeed(self):url=reverse('map_add')# POST only mendatory fieldspost_data={'name':self.map.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)self.assertIn('html',json)self.assertEqual(Map.objects.filter(name=self.map.name,owner=self.map.owner).count(),1)deftest_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'])deftest_quick_update_POST(self):url=reverse('map_update',kwargs={'map_id':self.map.pk})# POST only mendatory fieldsnew_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)deftest_quick_update_with_existing_name_should_no_succeed(self):url=reverse('map_update',kwargs={'map_id':self.map.pk})other_map=MapFactory(name="existing name",slug="existing-name",owner=self.user)# POST only mendatory fieldspost_data={'name':other_map.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.assertIn("html",json)self.assertEqual(Map.objects.filter(name=other_map.name,owner=self.map.owner).count(),1)deftest_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'])deftest_delete_POST(self):# create some featuresmarker1=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 deleteself.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 impactedself.assertEqual(User.objects.filter(pk=self.user.pk).count(),1)# Test response is a jsonjson=simplejson.loads(response.content)self.assertIn("redirect",json)classViewsPermissionsTest(BaseTest):defsetUp(self):super(ViewsPermissionsTest,self).setUp()self.other_user=UserFactory(username="Bob",password="123123")defcheck_url_permissions(self,url):# GET anonymousresponse=self.client.get(url)self.assertLoginRequired(response)# POST anonymousresponse=self.client.post(url,{})self.assertLoginRequired(response)# GET with wrong permissionsself.client.login(username=self.other_user.username,password="123123")response=self.client.get(url)self.assertEqual(response.status_code,403)# POST with wrong permissionsself.client.login(username=self.other_user.username,password="123123")response=self.client.post(url,{})self.assertEqual(response.status_code,403)classMapViewsPermissions(ViewsPermissionsTest):deftest_map_add_permissions(self):url=reverse('map_add')# GET anonymousresponse=self.client.get(url)self.assertLoginRequired(response)# POST anonymousresponse=self.client.post(url,{})self.assertLoginRequired(response)deftest_map_update_permissions(self):url=reverse('map_update',kwargs={'map_id':self.map.pk})self.check_url_permissions(url)deftest_upload_data_permissions(self):url=reverse('upload_data',kwargs={'map_id':self.map.pk})self.check_url_permissions(url)deftest_map_update_extend_permissions(self):# only POST is available for this viewurl=reverse('map_update_extent',kwargs={'map_id':self.map.pk})# POST anonymousresponse=self.client.post(url,{})self.assertLoginRequired(response)# POST with wrong permissionsself.client.login(username=self.other_user.username,password="123123")response=self.client.post(url,{})self.assertEqual(response.status_code,403)deftest_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 nowresponse=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'])deftest_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)classMarkerViewsPermissions(ViewsPermissionsTest):deftest_marker_add(self):url=reverse('marker_add',kwargs={'map_id':self.map.pk})self.check_url_permissions(url)deftest_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)deftest_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)classMarkerViews(BaseTest):deftest_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'])deftest_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 jsonjson=simplejson.loads(response.content)self.assertIn("geometry",json)deftest_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'])deftest_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 impactedself.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 jsonjson=simplejson.loads(response.content)self.assertIn("info",json)classUploadData(TransactionTestCase):defsetUp(self):self.user=UserFactory(password="123123")self.map=MapFactory(owner=self.user)self.category=CategoryFactory(map=self.map)deftearDown(self):self.user.delete()defprocess_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)returnresponsedeftest_GeoJSON_generic(self):# Contains tow Point, two Polygons and one Polylineresponse=self.process_file("test_upload_data.json","json")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 populationself.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,"")self.assertFalse("color"inmarker.options)deftest_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","json")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)deftest_GeoJSON_non_linear_ring_should_not_be_imported(self):response=self.process_file("test_upload_non_linear_ring.json","json")self.assertEqual(response.status_code,200)self.assertEqual(Polygon.objects.filter(category=self.category).count(),0)deftest_GeoJSON_missing_name_should_not_stop_import(self):# One feature is missing a name# We have to make sure that the other feature are importedself.assertEqual(Marker.objects.filter(category=self.category).count(),0)response=self.process_file("test_upload_missing_name.json","json")self.assertEqual(response.status_code,200)self.assertEqual(Marker.objects.filter(category=self.category).count(),1)self.assertEqual(Polyline.objects.filter(category=self.category).count(),1)self.assertEqual(Polygon.objects.filter(category=self.category).count(),1)deftest_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_file':"file://%s"%fixture_path,'content_type':'json'}self.client.login(username=self.user.username,password="123123")response=self.client.post(url,post_data)response=self.process_file("test_upload_data.json","json")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)deftest_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)deftest_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)classCategoryViews(BaseTest):deftest_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'])deftest_delete_POST(self):# create some featuresmarker1=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 deleteself.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 impactedself.assertEqual(Map.objects.filter(pk=self.map.pk).count(),1)# Test response is a jsonjson=simplejson.loads(response.content)self.assertIn("info",json)