1
2
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
20 fixtures = ['example_data']
21
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
50 self.group1 = Group.objects.create(name="Test Group 1")
51 self.group1.save()
52 enable_sharing(self.group1)
53
54
55 self.user.groups.add(self.group1)
56 self.user2.groups.add(self.group1)
57
58
59 self.array1.share_with(self.group1)
60
61
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
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
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
83 response = self.client.get(url)
84
85 self.assertEquals(response.status_code, 401)
86
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
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
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
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
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
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
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
142 """
143 Tests that user can retrieve valid KML file with network links to arrays
144 """
145 self.client.login(username=self.user.username, password=self.password)
146 url = reverse('kmlapp-userlinks-kml', kwargs={'session_key': '0', 'input_username': self.user.username})
147 response = self.client.get(url)
148 self.assertEquals(response.status_code, 200)
149 errors = kml_errors(response.content)
150 self.assertFalse(errors,"invalid KML %s" % str(errors))
151
152 url = reverse('kmlapp-userlinks-kml', kwargs={'session_key': self.client.session.session_key, 'input_username': self.user.username})
153 response = self.other_client.get(url)
154 self.assertEquals(response.status_code, 200)
155
157 """
158 Tests that user can retrieve valid KML file for public shared mpas and arrays
159 """
160
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
169 """
170 Tests that ANY user can retrieve valid KML file for public shared mpas and arrays
171 """
172
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
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
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
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
214 self.client.logout()
215 response = self.other_client.get(url)
216 self.assertEquals(response.status_code, 401)
217
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
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