-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtests.py
229 lines (191 loc) · 9.14 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from geonode.maps.models import Layer
from geonode.maps.models import Map
from geonode.maps.models import MapLayer
from geonode.maps.models import Thumbnail
from geonode.simplesearch import models as simplesearch
import mapstory.social_signals # this just needs activating but is not used
from mapstory import forms
from mapstory.models import UserActivity
from mapstory.models import ProfileIncomplete
from mapstory.models import audit_layer_metadata
from mapstory.models import Topic
from mapstory.templatetags import mapstory_tags
from agon_ratings.models import Rating
from actstream.models import Action
from dialogos.models import Comment
from mailer import engine as email_engine
# these can just get whacked
simplesearch.map_updated = lambda **kw: None
simplesearch.object_created = lambda **kw: None
Layer.delete_from_geoserver = lambda self: None
Layer._populate_from_gs = lambda s: None
Layer.verify = lambda s: None
Layer.save_to_geoserver = lambda s: None
class SocialTest(TestCase):
fixtures = ['test_data.json','map_data.json']
def setUp(self):
self.bobby = User.objects.get(username='bobby')
self.admin = User.objects.get(username='admin')
def test_social_map_layer_actions(self):
Layer.objects.create(owner=self.bobby, name='layer1',typename='layer1')
bobby_layer = Layer.objects.create(owner=self.bobby, name='layer2', typename='layer2')
# no activity yet, still Private
self.assertFalse(self.bobby.actor_actions.all())
# lets publish it
bobby_layer.publish.status = 'Public'
bobby_layer.publish.save()
actions = self.bobby.actor_actions.all()
# there should be a single action
self.assertEqual(1, len(actions))
self.assertEqual('bobby published layer2 Layer 0 minutes ago', str(actions[0]))
# now create a map
admin_map = Map.objects.create(owner=self.admin, zoom=1, center_x=0, center_y=0, title='map1')
# have to use a 'dummy' map to create the appropriate JSON
dummy = Map.objects.get(id=admin_map.id)
dummy.id += 1
dummy.save()
MapLayer.objects.create(name = 'layer1', ows_url='layer1', map=dummy, stack_order=1)
# and 'add' the layer
admin_map.update_from_viewer(dummy.viewer_json())
# no activity yet, still Private
self.assertFalse(self.admin.actor_actions.all())
# lets publish it and ensure things work
self.bobby.useractivity.other_actor_actions.clear()
admin_map.publish.status = 'Public'
admin_map.publish.save()
# there should be a single 'public' action (the other exists so it can hang on bobby)
actions = self.admin.actor_actions.public()
self.assertEqual(1, len(actions))
self.assertEqual('admin published map1 by admin 0 minutes ago', str(actions[0]))
# and a single action for bobby
actions = self.bobby.useractivity.other_actor_actions.all()
self.assertEqual(1, len(actions))
self.assertEqual('admin published layer1 Layer on map1 by admin 0 minutes ago', str(actions[0]))
# already published, add another layer and make sure it shows up in bobby
self.bobby.useractivity.other_actor_actions.clear()
MapLayer.objects.create(name = 'layer2', ows_url='layer2', map=dummy, stack_order=2)
admin_map.update_from_viewer(dummy.viewer_json())
actions = self.bobby.useractivity.other_actor_actions.all()
self.assertEqual(1, len(actions))
self.assertEqual('admin added layer2 Layer on map1 by admin 0 minutes ago', str(actions[0]))
def test_activity_item_tag(self):
lyr = Layer.objects.create(owner=self.bobby, name='layer1',typename='layer1', title='example')
lyr.publish.status = 'Public'
lyr.publish.save()
comment_on(lyr, self.bobby, 'a comment')
expected = ("http://localhost:8000/mapstory/storyteller/bobby (bobby)"
" commented on http://localhost:8000/data/layer1 (the StoryLayer 'example')"
" [ 0 minutes ago ]")
actual = mapstory_tags.activity_item(self.bobby.actor_actions.all()[0], plain_text=True)
self.assertEqual(expected, actual)
rate(lyr, self.bobby, 4)
expected = ("http://localhost:8000/mapstory/storyteller/bobby (bobby)"
" gave http://localhost:8000/data/layer1 (the StoryLayer 'example')"
" a rating of 4 [ 0 minutes ago ]")
actual = mapstory_tags.activity_item(self.bobby.actor_actions.all()[0], plain_text=True)
self.assertEqual(expected, actual)
lyr.delete()
# it seems like comment objects are not deleted when the commented-on object
# is deleted - test that the tag doesn't blow up
# @todo is this somehow related to mptt in threaded comments?
self.assertEqual(1, len(self.bobby.actor_actions.all()))
for a in self.bobby.actor_actions.all():
self.assertEqual('', mapstory_tags.activity_item(a))
def drain_mail_queue(self):
# mailer doesn't play well with default mail testing
mails = []
for m in email_engine.prioritize():
mails.append(m)
m.delete()
return mails
def test_no_notifications(self):
prefs = UserActivity.objects.get(user=self.bobby)
prefs.notification_preference = 'S'
layer = Layer.objects.create(owner=self.bobby, name='layer1',typename='layer1')
comment_on(layer, self.admin, "This is great")
prefs.notification_preference = 'E'
prefs.save()
comment_on(layer, self.admin, "This is great")
mail = self.drain_mail_queue()
self.assertEqual(1, len(mail))
def comment_on(obj, user, comment, reply_to=None):
ct = ContentType.objects.get_for_model(obj)
return Comment.objects.create(author=user, content_type=ct, object_id=obj.id,
comment=comment, parent=reply_to)
def rate(obj, user, rating):
ct = ContentType.objects.get_for_model(obj)
return Rating.objects.create(user=user, content_type=ct, object_id=obj.id,
rating=rating)
class LayerAuditTest(TestCase):
fixtures = ['test_data.json','map_data.json']
def test_audit(self):
self.bobby = User.objects.get(username='bobby')
t = Topic.objects.create(name='xyz')
layer = Layer.objects.create(owner=self.bobby, name='layer1',typename='layer1')
self.assertFalse(audit_layer_metadata(layer))
atts = ('abstract','title','purpose','language','supplemental_information',
'data_quality_statement')
for a in atts:
setattr(layer, a, 'a')
self.assertFalse(audit_layer_metadata(layer))
layer.topic_set.add(t)
self.assertFalse(audit_layer_metadata(layer))
layer.keywords.add('FOOBARq')
self.assertFalse(audit_layer_metadata(layer))
layer.has_thumbnail = lambda : True
self.assertTrue(audit_layer_metadata(layer))
class ContactDetailTests(TestCase):
c = Client()
def test_incomplete_profile(self):
u = User.objects.create(username='billy')
# this will fail if not incomplete, no need for assertions
ProfileIncomplete.objects.get(user = u)
# now fill stuff out
p = u.get_profile()
u.first_name = 'Billy'
u.last_name = 'Bob'
u.save()
p.update_audit()
# still incomplete
ProfileIncomplete.objects.get(user = u)
p.blurb = 'I Billy Bob'
p.email = 'billy@b.ob'
p.save()
p.update_audit()
# still incomplete
ProfileIncomplete.objects.get(user = u)
# add avatar
a = u.avatar_set.model(user=u)
a.save()
u.avatar_set.add(a)
p.update_audit()
# finally
self.assertEqual(0, ProfileIncomplete.objects.filter(user = u).count())
def test_profile_form(self):
email = 'billy@bil.ly'
u = User.objects.create(username='billy', email=email)
form = forms.ProfileForm(instance=u.get_profile())
# email carried over from user
self.assertEqual(email, form['email'].value())
# invalid
form = forms.ProfileForm(data={}, instance=u.get_profile())
self.assertTrue(not form.is_valid())
self.assertEqual(['first_name', 'last_name', 'email', 'blurb'], form.errors.keys())
# first, last, blurb, and email handling all work
new_email = 'bill@billy.name'
form = forms.ProfileForm(data={'first_name':'Billy',
'last_name':'Bob',
'blurb':'I Billy Bob',
'email':new_email},
instance=u.get_profile())
self.assertTrue(form.is_valid())
form.save()
# computed name field
self.assertEqual('Billy Bob', u.get_profile().name)
# and email applied to both user and profile
self.assertEqual(new_email, u.email)
self.assertEqual(new_email, u.get_profile().email)