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

Source Code for Module madrona.kmlapp.tests

  1  # -*- coding: utf-8 -*- 
  2  # ^^^ this is required on 1st or 2nd line; see http://www.python.org/dev/peps/pep-0263/ 
  3  """ 
  4  Unit tests for the KML App 
  5  """ 
  6  from django.conf import settings 
  7  from django.test import TestCase 
  8  from django.test.client import Client 
  9  from django.contrib.gis.geos import GEOSGeometry  
 10  from django.contrib.auth.models import * 
 11  from madrona.common import utils  
 12  from madrona.common.utils import kml_errors, enable_sharing 
 13  from django.core.urlresolvers import reverse 
 14  from django.contrib.contenttypes.models import ContentType 
 15  from madrona.features.tests import TestMpa as Mpa 
 16  from madrona.features.tests import TestArray as MpaArray 
 17  from django.contrib.auth.models import Group 
 18   
19 -class KMLAppTest(TestCase):
20 fixtures = ['example_data'] 21
22 - def setUp(self):
23 self.client = Client() 24 self.other_client = Client() 25 self.password = 'iluvge' 26 self.user = User.objects.create_user('kmltest', 'kmltest@madrona.org', password=self.password) 27 self.user2 = User.objects.create_user('kmltest2', 'kmltest2@madrona.org', password=self.password) 28 29 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))') 30 g2 = GEOSGeometry('SRID=4326;POLYGON ((-121.42 34.37, -120.64 34.32, -120.63 34.12, -121.44 34.15, -121.42 34.37))') 31 g3 = GEOSGeometry('SRID=4326;POLYGON ((-122.42 34.37, -121.64 34.32, -121.63 34.12, -122.44 34.15, -122.42 34.37))') 32 33 g1.transform(settings.GEOMETRY_DB_SRID) 34 g2.transform(settings.GEOMETRY_DB_SRID) 35 g3.transform(settings.GEOMETRY_DB_SRID) 36 37 self.mpa1 = Mpa.objects.create(name='Test_MPA_1', user=self.user, geometry_final=g1) 38 self.mpa2 = Mpa.objects.create(name=u'Test_MPA_2_with_some_uniçode', user=self.user, geometry_final=g2) 39 self.mpa3 = Mpa.objects.create(name='Test_MPA_3', user=self.user, geometry_final=g3) 40 self.mpa1.save() 41 self.mpa2.save() 42 self.mpa3.save() 43 44 self.array1 = MpaArray.objects.create(name='Test_Array_1', user=self.user) 45 self.array1.save() 46 self.mpa1.add_to_collection(self.array1) 47 self.mpa2.add_to_collection(self.array1) 48 49 # Then make the group with permissions 50 self.group1 = Group.objects.create(name="Test Group 1") 51 self.group1.save() 52 enable_sharing(self.group1) 53 54 # Add users to group 55 self.user.groups.add(self.group1) 56 self.user2.groups.add(self.group1) 57 58 # Share with common group 59 self.array1.share_with(self.group1) 60 61 # Share with public 62 public_group = Group.objects.filter(name__in=settings.SHARING_TO_PUBLIC_GROUPS)[0] 63 self.user.groups.add(public_group) 64 self.array1.share_with(public_group)
65
66 - def test_nonauth_user_kml(self):
67 """ 68 Tests that non-authenticated user can't retrieve any MPAs 69 """ 70 url = reverse('kmlapp-user-kml', kwargs={'session_key': 0, 'input_username': self.user.username}) 71 response = self.client.get(url) 72 errors = kml_errors(response.content) 73 self.assertEquals(response.status_code, 401)
74
75 - def test_other_user_kml(self):
76 """ 77 Tests that an authenticated user can't retrieve another user's MPAs 78 """ 79 other_user = User.objects.create_user('other', 'other@madrona.org', password='pword') 80 self.client.login(username=other_user.username, password='pword') 81 url = reverse('kmlapp-user-kml', kwargs={'session_key': 0, 'input_username': self.user.username}) 82 # response = self.client.get('/kml/%s/user_mpa.kml' % self.user.username, {}) 83 response = self.client.get(url) 84 # errors = kml_errors(response.content) 85 self.assertEquals(response.status_code, 401) 86 # Test for same result using session_key in url 87 self.client.logout() 88 self.client.login(username=other_user.username, password='pword') 89 url = reverse('kmlapp-user-kml', kwargs={'session_key': self.client.session.session_key, 'input_username': self.user.username}) 90 response = self.other_client.get(url) 91 self.assertEquals(response.status_code, 401)
92
93 - def test_user_kml(self):
94 """ 95 Tests that user can retrieve valid KML file of their MPAs 96 """ 97 self.client.login(username=self.user.username, password=self.password) 98 url = reverse('kmlapp-user-kml', kwargs={'session_key': 0, 'input_username': self.user.username}) 99 response = self.client.get(url) 100 self.assertEquals(response.status_code, 200) 101 errors = kml_errors(response.content) 102 self.assertFalse(errors,"invalid KML %s" % str(errors)) 103 104 # test for session key url 105 url = reverse('kmlapp-user-kml', kwargs={'session_key': self.client.session.session_key, 'input_username': self.user.username}) 106 response = self.other_client.get(url) 107 self.assertEquals(response.status_code, 200)
108
109 - def test_array_kml(self):
110 """ 111 Tests that Array can be represented as valid KML 112 """ 113 self.client.login(username=self.user.username, password=self.password) 114 url = reverse('kmlapp-feature-kml', kwargs={'session_key': '0', 'input_uid': self.array1.uid}) 115 response = self.client.get(url) 116 self.assertEquals(response.status_code, 200) 117 errors = kml_errors(response.content) 118 self.assertFalse(errors,"invalid KML %s" % str(errors)) 119 120 # test session_key in url method 121 url = reverse('kmlapp-feature-kml', kwargs={'session_key': self.client.session.session_key, 'input_uid': self.array1.uid}) 122 response = self.other_client.get(url) 123 self.assertEquals(response.status_code, 200)
124
125 - def test_single_kml(self):
126 """ 127 Tests that single MPA can be represented as valid KML 128 """ 129 self.client.login(username=self.user.username, password=self.password) 130 url = reverse('kmlapp-feature-kml', kwargs={'session_key': '0', 'input_uid': self.mpa1.uid}) 131 response = self.client.get(url) 132 self.assertEquals(response.status_code, 200) 133 errors = kml_errors(response.content) 134 self.assertFalse(errors,"invalid KML %s" % str(errors)) 135 136 # test session_key in url method 137 url = reverse('kmlapp-feature-kml', kwargs={'session_key': self.client.session.session_key, 'input_uid': self.mpa1.uid}) 138 response = self.other_client.get(url) 139 self.assertEquals(response.status_code, 200)
140 155
156 - def test_public_kml_auth(self):
157 """ 158 Tests that user can retrieve valid KML file for public shared mpas and arrays 159 """ 160 # As authenticated user 161 self.client.login(username=self.user.username, password=self.password) 162 url = reverse('kmlapp-publicshared-kml', kwargs={'session_key':self.client.session.session_key}) 163 response = self.client.get(url) 164 self.assertEquals(response.status_code, 200, response) 165 errors = kml_errors(response.content) 166 self.assertFalse(errors,"invalid KML %s" % str(errors))
167
168 - def test_public_kml_unauth(self):
169 """ 170 Tests that ANY user can retrieve valid KML file for public shared mpas and arrays 171 """ 172 # As anonymous user 173 url = reverse('kmlapp-publicshared-kml', kwargs={'session_key': '0'}) 174 response = self.client.get(url) 175 self.assertEquals(response.status_code, 200) 176 errors = kml_errors(response.content) 177 self.assertFalse(errors,"invalid KML %s" % str(errors))
178
179 - def test_shared_kml(self):
180 """ 181 Tests that another user can view the shared_kml (with network links the the sharedby-kmls) 182 """ 183 self.client.login(username=self.user2.username, password=self.password) 184 url = reverse('kmlapp-sharedlinks-kml', kwargs={'session_key':self.client.session.session_key, 'input_username':self.user2.username}) 185 response = self.client.get(url) 186 self.assertEquals(response.status_code, 200, response) 187 errors = kml_errors(response.content) 188 self.assertFalse(errors,"invalid KML %s" % str(errors))
189
190 - def test_sharedby_kml(self):
191 """ 192 Tests that user can view the sharedby_kml (mpas shared by a given group) 193 """ 194 self.client.login(username=self.user2.username, password=self.password) 195 url = reverse('kmlapp-sharedby-kml', kwargs={'session_key':self.client.session.session_key, 'input_shareuser': self.user.pk, 'input_sharegroup':self.group1.pk}) 196 response = self.client.get(url) 197 self.assertEquals(response.status_code, 200, response) 198 errors = kml_errors(response.content) 199 self.assertFalse(errors,"invalid KML %s" % str(errors))
200
201 - def test_kmz_view(self):
202 """ 203 Tests that we can retrieve a zipped KML file (ie KMZ) 204 """ 205 self.client.login(username=self.user.username, password=self.password) 206 url = reverse('kmlapp-user-kmz', kwargs={'session_key': '0', 'input_username': self.user.username}) 207 response = self.client.get(url) 208 self.assertEquals(response.status_code, 200) 209 210 url = reverse('kmlapp-user-kmz', kwargs={'session_key': self.client.session.session_key, 'input_username': self.user.username}) 211 response = self.other_client.get(url) 212 self.assertEquals(response.status_code, 200) 213 # once a user logs out, the session should be no good 214 self.client.logout() 215 response = self.other_client.get(url) 216 self.assertEquals(response.status_code, 401)
217
218 - def test_nonexistant_feature(self):
219 self.client.login(username=self.user.username, password=self.password) 220 url = reverse('kmlapp-feature-kmz', kwargs={'session_key': self.client.session.session_key, 'input_uid': 'blah_blah_12345678910'}) 221 response = self.client.get(url) 222 self.assertEquals(response.status_code, 404)
223
224 - def test_invalid_kml(self):
225 """ 226 Tests that invalid KML gets noticed 227 """ 228 self.client.login(username=self.user.username, password=self.password) 229 url = reverse('kmlapp-user-kml', kwargs={'session_key': 0, 'input_username': self.user.username}) 230 response = self.client.get(url) 231 self.assertEquals(response.status_code, 200) 232 233 bad = str(response.content).replace("<Document>","<foobar /><Document>") 234 errors = kml_errors(bad) 235 self.assertEquals(errors[0][1]['element'],u'foobar')
236