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
23
24
25
26
27 @register
28 -class TestUserKml(PrivateLayerList):
30 form = 'madrona.layers.tests.TestUserKmlForm'
31
35
37
38
40 kml_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fixtures/public_layers.kml')
41
42
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
49
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
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
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
74
75 self.assertTrue(self.layer1.is_viewable(self.user1)[0])
76
77
78 self.assertTrue(self.layer1.is_viewable(self.user2)[0])
79
80
81 self.assertFalse(self.layer1.is_viewable(self.user3)[0])
82
83
84 self.assertFalse(self.layer2.is_viewable(self.user1)[0])
85
86
87 self.assertTrue(self.layer2.is_viewable(self.user2)[0])
88
89
90 self.assertFalse(self.layer2.is_viewable(self.user3)[0])
91
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")
110 response = self.client.get(url)
111 self.assertEqual(response.status_code, 404)
112 self.client.logout()
113
116 fixtures = ['example_data']
117
131
148
171
182