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

Source Code for Module madrona.layers.tests

  1  """ 
  2  This file demonstrates two different styles of tests (one doctest and one 
  3  unittest). These will both pass when you run "manage.py test". 
  4   
  5  Replace these with more appropriate tests for your application. 
  6  """ 
  7   
  8  from django.test import TestCase 
  9  from django.core.files import File 
 10  from madrona.layers.models import PublicLayerList, PrivateLayerList 
 11  from madrona.features.forms import FeatureForm 
 12  from madrona.features import register 
 13  from madrona.common.utils import enable_sharing 
 14  from django.contrib.contenttypes.models import ContentType 
 15  from django.contrib.auth.models import User, Group 
 16  from django.conf import settings 
 17  from django.test.client import Client 
 18  from django.conf.urls.defaults import * 
 19  from django.core.urlresolvers import reverse 
 20  import os 
21 22 #urlpatterns = patterns('', 23 # # Example: 24 # (r'^layers/', include('madrona.layers.urls')), 25 #) 26 27 @register 28 -class TestUserKml(PrivateLayerList):
29 - class Options:
30 form = 'madrona.layers.tests.TestUserKmlForm'
31
32 -class TestUserKmlForm(FeatureForm):
33 - class Meta(FeatureForm.Meta):
35
36 -class PrivateLayerListTest(TestCase):
37 #urls = 'madrona.layers.tests' 38
39 - def setUp(self):
40 kml_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fixtures/public_layers.kml') 41 42 # Create 3 users 43 self.password = 'iluvsharing' 44 self.user1 = User.objects.create_user('user1', 'test@madrona.org', password=self.password) 45 self.user2 = User.objects.create_user('user2', 'test@madrona.org', password=self.password) 46 self.user3 = User.objects.create_user('user3', 'test@madrona.org', password=self.password) 47 48 # Create some groups 49 # Group 1 has user1 and user2 and can share features 50 self.group1 = Group.objects.create(name="Test Group 1") 51 self.group1.save() 52 self.user1.groups.add(self.group1) 53 self.user2.groups.add(self.group1) 54 enable_sharing(self.group1) 55 56 path = os.path.dirname(os.path.abspath(__file__)) 57 f = File(open(kml_path)) 58 settings.MEDIA_URL = '' 59 60 # User1 creates Layer1 and shares with Group1 61 self.layer1 = TestUserKml.objects.create(user=self.user1) 62 self.layer1.save() 63 self.layer1.kml_file.save('layer1.kml', f) 64 self.layer1.save() 65 self.layer1.share_with(self.group1) 66 67 # User2 creates Layer2 and doesnt share (the selfish bastard) 68 self.layer2 = TestUserKml.objects.create(user=self.user2) 69 self.layer2.save() 70 self.layer2.kml_file.save('layer2.kml', f) 71 self.layer2.save()
72
73 - def test_permissions(self):
74 # User 1 can view Layer 1 75 self.assertTrue(self.layer1.is_viewable(self.user1)[0]) 76 77 # User 2 can view Layer 1 (both members of shared group) 78 self.assertTrue(self.layer1.is_viewable(self.user2)[0]) 79 80 # User 3 can't view Layer 1 81 self.assertFalse(self.layer1.is_viewable(self.user3)[0]) 82 83 # User 1 can't view Layer 2 (User 2 hasnt shared it) 84 self.assertFalse(self.layer2.is_viewable(self.user1)[0]) 85 86 # User 2 can view Layer 2 (owns it) 87 self.assertTrue(self.layer2.is_viewable(self.user2)[0]) 88 89 # User 3 can't view Layer 2 90 self.assertFalse(self.layer2.is_viewable(self.user3)[0])
91
92 - def test_webservice(self):
93 url = TestUserKml.get_options().get_resource(self.layer1.pk) 94 95 self.client.login(username=self.user1, password=self.password) 96 response = self.client.get(url) 97 self.assertEqual(response.status_code, 200) 98 self.client.logout() 99 100 self.client.login(username=self.user2, password=self.password) 101 response = self.client.get(url) 102 self.assertEqual(response.status_code, 200) 103 self.client.logout() 104 105 self.client.login(username=self.user3, password=self.password) 106 response = self.client.get(url) 107 self.assertEqual(response.status_code, 403) 108 109 url = url.replace(str(self.layer1.pk),"123456789") # doesnt exist 110 response = self.client.get(url) 111 self.assertEqual(response.status_code, 404) 112 self.client.logout()
113
114 115 -class PublicLayerListTest(TestCase):
116 fixtures = ['example_data'] 117
118 - def testCreate(self):
119 """ 120 Test saving an instance of PublicLayerList to the repository 121 """ 122 layer = PublicLayerList.objects.create(active=True) 123 path = os.path.dirname(os.path.abspath(__file__)) 124 125 f = File(open(path + '/fixtures/public_layers.kml')) 126 settings.MEDIA_URL = '' 127 layer.kml_file.save('kml-file.kml', f) 128 # 2 because the initial_data fixture loads one 129 self.assertEquals(PublicLayerList.objects.count(), 2) 130 self.assertTrue(layer.kml_file.size > 0)
131
132 - def testOnlyOneActiveLayer(self):
133 layer = PublicLayerList.objects.create(active=True) 134 self.assertTrue(layer.active) 135 # Create a new layer that is not active. Should not affect the current 136 # active layer 137 new_layer = PublicLayerList.objects.create(active=False) 138 self.assertFalse(new_layer.active) 139 active = PublicLayerList.objects.filter(active=True) 140 self.assertEqual(active.count(), 1) 141 self.assertEqual(active[0].pk, layer.pk) 142 # Now create a new active layer. The first active layer should now 143 # be deactivated. 144 new_active_layer = PublicLayerList.objects.create(active=True) 145 active = PublicLayerList.objects.filter(active=True) 146 self.assertEqual(active.count(), 1) 147 self.assertEqual(active[0].pk, new_active_layer.pk)
148
149 - def testLayerService(self):
150 #urls = 'madrona.layers.tests' 151 # Should fail with a 404 when no layers are specified 152 client = Client() 153 l = PublicLayerList.objects.get(pk=1) 154 # deactivate the fixture so it doesn't interfere with the test 155 l.active = False 156 l.save() 157 response = client.get('/layers/public/') 158 self.failUnlessEqual(response.status_code, 404) 159 l.active = True 160 l.save() 161 # Create a layer to grab from the public layers service 162 layer = PublicLayerList.objects.create(active=True) 163 path = os.path.dirname(os.path.abspath(__file__)) 164 f = File(open(path + '/fixtures/public_layers.kml')) 165 settings.MEDIA_URL = '' 166 layer.kml_file.save('kml-file.kml', f) 167 self.assertEquals(PublicLayerList.objects.count(), 2) 168 client = Client() 169 response = client.get('/layers/public/') 170 self.failUnlessEqual(response.status_code, 200)
171
172 - def tearDown(self):
173 layer = PublicLayerList.objects.create(active=True) 174 path = os.path.dirname(os.path.abspath(__file__)) 175 176 f = File(open(path + '/fixtures/public_layers.kml')) 177 settings.MEDIA_URL = '' 178 layer.kml_file.save('kml-file.kml', f) 179 dr = os.path.dirname(layer.kml_file.file.name) 180 cmd = 'rm -f %s/*.kml' % (dr, ) 181 os.system(cmd)
182