This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
map_review_test.py
265 lines (234 loc) · 10.9 KB
/
map_review_test.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at: http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distrib-
# uted under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. See the License for
# specific language governing permissions and limitations under the License.
"""Tests for map_review.py."""
__author__ = '[email protected] (Steve Hakusa)'
import urllib
import map_review
import model
import perms
import test_utils
class MapReviewTest(test_utils.BaseTest):
"""Tests for single-map pages served by maps.py."""
def setUp(self):
super(MapReviewTest, self).setUp()
map_root = {
'id': 'map1',
'title': 'Map 1',
'layers': [
{'id': '1', 'title': 'Emergency shelters', 'type': 'KML'}
],
'topics': [{
'id': 'shelter',
'title': 'Shelter',
'layer_ids': ['1'],
'crowd_enabled': True,
'cluster_radius': 456,
'questions': [{
'id': 'q1',
'text': 'Is there space available?<br>',
'type': 'CHOICE',
'choices': [
{'id': 'y', 'title': 'Yes', 'label': '[space]'},
{'id': 'n', 'title': 'No', 'label': '[no space]'}]
}, {
'id': 'q2',
'text': 'Are overnight stays allowed?',
'type': 'CHOICE',
'choices': [
{'id': 'y', 'title': 'Yes', 'label': '[overnight]'},
{'id': 'n', 'title': 'No', 'label': '[day only]'}]
}, {
'id': 'q3',
'text': 'How many beds are open?',
'title': 'Beds',
'type': 'NUMBER'
}, {
'id': 'q4',
'text': 'What is the phone number?',
'title': 'Phone',
'type': 'STRING'
}]
}, {
'id': 'water',
'title': 'Water',
'layer_ids': ['2'],
'crowd_enabled': True,
'cluster_radius': 123,
'questions': [{
'id': 'q5',
'text': 'Is there water?',
'type': 'CHOICE',
'choices': [
{'id': 'y', 'title': 'Yes', 'label': '[water]'},
{'id': 'n', 'title': 'No', 'label': '[no water]'}]
}]
}, {
'id': 'flooding',
'title': 'Flooding',
'layer_ids': ['3', '4'],
'crowd_enabled': True,
'cluster_radius': 987,
}]
}
self.map_object = test_utils.CreateMap(map_root, reviewers=['reviewer'])
self.map_id = self.map_object.id
self.topic1_id = '%s.shelter' % self.map_id
self.q1_id = '%s.shelter.q1' % self.map_id
self.q2_id = '%s.shelter.q2' % self.map_id
self.q3_id = '%s.shelter.q3' % self.map_id
self.q4_id = '%s.shelter.q4' % self.map_id
self.SetTime(1300000000)
self.cr1 = test_utils.NewCrowdReport(text='26 beds here',
topic_ids=[self.topic1_id],
answers={self.q1_id: 'y',
self.q3_id: 26,
self.q4_id: '555-1234'})
self.topic2_id = '%s.water' % self.map_id
self.q5_id = '%s.water.q5' % self.map_id
self.SetTime(1300000001)
self.cr2 = test_utils.NewCrowdReport(author='http://foo.com/abc',
text='bottled water here </script>',
topic_ids=[self.topic2_id],
answers={self.q5_id: 'n'})
def testGet(self):
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review' % self.map_id)
self.assertTrue('>shelter<' in response.body)
self.assertTrue('>water<' in response.body)
self.assertTrue('>flooding<' in response.body)
self.assertTrue(self.cr1.id in response.body)
self.assertTrue(self.cr2.id in response.body)
self.assertTrue('[space]' in response.body)
self.assertTrue('[no water]' in response.body)
self.assertTrue('Beds: 26' in response.body)
self.assertTrue('Phone: 555-1234' in response.body)
self.assertTrue('bottled water here </script>' in response.body)
self.assertTrue('name="accept"' in response.body)
self.assertTrue(map_review._ICON_URL_TEMPLATE % 'aaa' in response.body)
def testGetFromDomainReviewer(self):
perms.Grant('domainreviewer', perms.Role.DOMAIN_REVIEWER, 'xyz.com')
with test_utils.DomainLogin('domainreviewer', 'xyz.com'):
self.DoGet('/.maps/%s/review' % self.map_id)
def testGetPublishedMap(self):
model.CatalogEntryModel(key_name='xyz.com:zz', domain='xyz.com',
label='zz', title='Map 1', map_id=self.map_id).put()
with test_utils.Login('reviewer'):
self.DoGet('/xyz.com/zz/review')
def testGetWithSearch(self):
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review?query=beds' % self.map_id)
self.assertTrue(self.cr1.id in response.body)
self.assertFalse(self.cr2.id in response.body)
def testGetWithComplexSearch(self):
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review?query=%s' % (
self.map_id, urllib.quote('here author:"http://foo.com/abc"')))
self.assertFalse(self.cr1.id in response.body)
self.assertTrue(self.cr2.id in response.body)
def testGetWithTopic(self):
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review?topic=shelter' % self.map_id)
self.assertTrue(self.cr1.id in response.body)
self.assertFalse(self.cr2.id in response.body)
def testGetWithAuthor(self):
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review?author=http://foo.com/abc' %
self.map_id)
self.assertFalse(self.cr1.id in response.body)
self.assertTrue(self.cr2.id in response.body)
def testGetWithReportId(self):
cr_id = self.cr1.id
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review?id=%s' % (self.map_id, cr_id))
self.assertTrue(self.cr1.id in response.body)
self.assertFalse(self.cr2.id in response.body)
def testGetWithCount(self):
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review?count=1' % self.map_id)
self.assertFalse(self.cr1.id in response.body)
self.assertTrue(self.cr2.id in response.body)
def testGetWithSkip(self):
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review?skip=1' % self.map_id)
self.assertTrue(self.cr1.id in response.body)
self.assertFalse(self.cr2.id in response.body)
def testGetWithHidden(self):
model.CrowdVote.Put(self.cr1.id, 'voter1', 'ANONYMOUS_DOWN')
model.CrowdVote.Put(self.cr1.id, 'voter2', 'ANONYMOUS_DOWN')
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review' % self.map_id)
self.assertTrue(self.cr1.id in response.body)
self.assertTrue(self.cr2.id in response.body)
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review?hidden=true' % self.map_id)
self.assertTrue(self.cr1.id in response.body)
self.assertFalse(self.cr2.id in response.body)
def testGetWithReviewed(self):
model.CrowdReport.MarkAsReviewed(self.cr1.id)
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review' % self.map_id)
self.assertFalse(self.cr1.id in response.body)
self.assertTrue(self.cr2.id in response.body)
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review?reviewed=true' % self.map_id)
self.assertTrue(self.cr1.id in response.body)
self.assertTrue(self.cr2.id in response.body)
def testPostMarkAsReviewed(self):
with test_utils.Login('reviewer'):
self.DoPost('/.maps/%s/review' % self.map_id,
'xsrf_token=XSRF&accept=%s&accept=%s' % (self.cr1.id,
self.cr2.id))
self.assertTrue(model.CrowdReport.Get(self.cr1.id).reviewed)
self.assertTrue(model.CrowdReport.Get(self.cr2.id).reviewed)
# Both reports get marked as reviewed and should not show up by default
# on the review page
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review' % self.map_id)
self.assertFalse(self.cr1.id in response.body)
self.assertFalse(self.cr2.id in response.body)
def testPostUpvoteDownvoteFromPublishedMap(self):
model.CatalogEntryModel(key_name='xyz.com:zz', domain='xyz.com',
label='zz', title='Map 1', map_id=self.map_id).put()
model.CrowdVote.Put(self.cr1.id, 'voter1', 'ANONYMOUS_DOWN')
model.CrowdVote.Put(self.cr1.id, 'voter2', 'ANONYMOUS_DOWN')
self.assertTrue(model.CrowdReport.Get(self.cr1.id).hidden)
with test_utils.Login('reviewer'):
self.DoPost('/xyz.com/zz/review',
'xsrf_token=XSRF&upvote=%s&downvote=%s' % (self.cr1.id,
self.cr2.id))
self.assertFalse(model.CrowdReport.Get(self.cr1.id).hidden)
self.assertTrue(model.CrowdReport.Get(self.cr2.id).hidden)
# Both reports get marked as reviewed and should not show up by default
# on the review page
with test_utils.Login('reviewer'):
response = self.DoGet('/xyz.com/zz/review')
self.assertFalse(self.cr1.id in response.body)
self.assertFalse(self.cr2.id in response.body)
def testGetWithInvalidQuestionAnswer(self):
q_invalid_id = '%s.water.qINVALID' % self.map_id
cr_bad_question = test_utils.NewCrowdReport(author='http://foo.com/abc',
text='',
topic_ids=[self.topic2_id],
answers={q_invalid_id: 'n'})
cr_bad_answer = test_utils.NewCrowdReport(author='http://foo.com/abc',
text='',
topic_ids=[self.topic2_id],
answers={self.q5_id: 'INVALID'})
with test_utils.Login('reviewer'):
response = self.DoGet('/.maps/%s/review' % self.map_id)
self.assertTrue(self.cr1.id in response.body)
self.assertTrue(self.cr2.id in response.body)
self.assertTrue(cr_bad_question.id in response.body)
self.assertTrue('OBSOLETE QUESTION' in response.body)
self.assertTrue(cr_bad_answer.id in response.body)
self.assertTrue('OBSOLETE CHOICE' in response.body)
if __name__ == '__main__':
test_utils.main()