Package madrona :: Package staticmap :: Module tests
[hide private]

Source Code for Module madrona.staticmap.tests

  1  """ 
  2  Unit tests for staticmap rendering via mapnik 
  3  """ 
  4  from django.conf import settings 
  5  from django.conf.urls.defaults import * 
  6  from django.contrib.auth.models import User, Group 
  7  from django.contrib.gis.geos import GEOSGeometry  
  8  from django.contrib.contenttypes.models import ContentType 
  9  from django.test import Client 
 10  from madrona.common.test_settings_manager import SettingsTestCase as TestCase 
 11  from madrona.common.utils import enable_sharing 
 12  from madrona.staticmap.models import MapConfig 
 13  from madrona.features.tests import TestMpa, TestArray 
 14   
 15   
16 -class StaticMapTest(TestCase):
17 fixtures = ['example_data'] 18
19 - def setUp(self):
20 self.client = Client() 21 22 # Create 3 users 23 self.password = 'iluvmapnik' 24 self.user = User.objects.create_user('user1', 'test@madrona.org', password=self.password) 25 self.user2 = User.objects.create_user('user2', 'test@madrona.org', password=self.password) 26 27 self.group1 = Group.objects.create(name="Test Group 1") 28 self.group1.save() 29 self.user.groups.add(self.group1) 30 self.user2.groups.add(self.group1) 31 enable_sharing(self.group1) 32 self.public = Group.objects.get(name=settings.SHARING_TO_PUBLIC_GROUPS[0]) 33 self.user.groups.add(self.public) 34 35 # Create some necessary objects 36 g1 = GEOSGeometry('SRID=4326;POLYGON((-120.42 34.37, -119.64 34.32, -119.63 34.12, -120.44 34.15, -120.42 34.37))') 37 g1.transform(settings.GEOMETRY_DB_SRID) 38 39 # Create 3 Mpas by different users 40 mpa1 = TestMpa.objects.create(name='Test_MPA_1', designation='R', user=self.user, geometry_orig=g1) 41 mpa1.save() 42 mpa2 = TestMpa.objects.create(name='Test_MPA_2', designation='P', user=self.user, geometry_orig=g1) 43 mpa2.save() 44 mpa3 = TestMpa.objects.create(name='Test_MPA_3', designation='C', user=self.user, geometry_orig=g1) 45 mpa3.save() 46 self.mpa_ids = [mpa1.pk, mpa2.pk, mpa3.pk] 47 self.mpa_uids = ['%s_%s' % (mpa1.model_uid(), x) for x in self.mpa_ids] 48 49 # User1 adds mpa to an array 50 array1 = TestArray.objects.create(name='Test_Array_1', user=self.user) 51 array1.save() 52 array2 = TestArray.objects.create(name='Test_Array_2', user=self.user) 53 array2.save() 54 mpa1.add_to_collection(array1) 55 mpa2.add_to_collection(array2) 56 57 array1.share_with(self.group1) 58 array2.share_with(self.public) 59 60 self.array_ids = [array1.pk, array2.pk] 61 self.array_uids = ['%s_%s' % (array1.model_uid(), x) for x in self.array_ids]
62
63 - def testMapConfigPresent(self):
64 """ 65 Check presence of initial MapConfig 66 """ 67 self.assertTrue(MapConfig.objects.count() > 0)
68
69 - def testMap(self):
70 response = self.client.get('/staticmap/default/', {}) 71 self.assertEquals(response.status_code, 200) 72 blank_map = response.content 73 74 # User 1 should see these 75 self.client.login(username=self.user.username, password=self.password) 76 uidcsv = ','.join(self.mpa_uids) 77 url = '/staticmap/default/?uids=%s' % uidcsv 78 response = self.client.get(url) 79 self.assertEquals(response.status_code, 200)
80
81 - def test_params(self):
82 # User 1 should see these 83 self.client.login(username=self.user.username, password=self.password) 84 uidcsv = ','.join(self.mpa_uids) 85 86 url = '/staticmap/default/?autozoom=true&uids=%s' % uidcsv 87 response = self.client.get(url) 88 self.assertEquals(response.status_code, 200) 89 90 url = '/staticmap/default/?show_extent=true&uids=%s' % uidcsv 91 response = self.client.get(url) 92 self.assertEquals(response.status_code, 200)
93
94 - def test_collection(self):
95 # User 1 should see these 96 self.client.login(username=self.user.username, password=self.password) 97 uidcsv = ','.join(self.array_uids) 98 url = '/staticmap/default/?uids=%s' % uidcsv 99 response = self.client.get(url) 100 self.assertEquals(response.status_code, 200)
101
102 - def test_inaccessible(self):
103 response = self.client.get('/staticmap/default/', {}) 104 self.assertEquals(response.status_code, 200) 105 blank_map = response.content 106 107 # User 2 shouldnt have access to mpa3 108 self.client.login(username=self.user2.username, password=self.password) 109 response = self.client.get('/staticmap/default/?uids=%s' % self.mpa_uids[2], {}) 110 self.assertEquals(response.status_code, 200)
111 #self.assertEquals(response.content, blank_map) 112
113 - def test_sharing(self):
114 response = self.client.get('/staticmap/default/', {}) 115 self.assertEquals(response.status_code, 200) 116 blank_map = response.content 117 118 # User 2 should see these 119 response = self.client.get('/staticmap/default/?uids=%s' % ','.join(self.mpa_uids), {}) 120 self.assertEquals(response.status_code, 200)
121 #self.assertNotEqual(response.content, blank_map) 122
123 - def test_anon(self):
124 response = self.client.get('/staticmap/default/', {}) 125 self.assertEquals(response.status_code, 200) 126 blank_map = response.content 127 128 # Anon user shouldnt have access to mpa3, but yes to mpa2 129 response = self.client.get('/staticmap/default/?uids=%s' % self.mpa_uids[2], {}) 130 self.assertEquals(response.status_code, 200) 131 #self.assertEquals(response.content, blank_map) 132 response = self.client.get('/staticmap/default/?uids=%s' % self.mpa_uids[1], {}) 133 self.assertEquals(response.status_code, 200)
134 #self.assertNotEquals(response.content, blank_map) 135
136 - def test_default_style(self):
137 from madrona.staticmap.views import default_style 138 style = default_style() 139 self.assertEquals(type(style).__name__, 'Style')
140
148