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
/
maps_test.py
225 lines (191 loc) · 9.46 KB
/
maps_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
#!/usr/bin/python
# 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 maps.py."""
__author__ = 'shakusa@google.com (Steve Hakusa)'
import config
import domains
import maps
import model
import test_utils
class MapTest(test_utils.BaseTest):
"""Tests for single-map pages served by maps.py."""
def setUp(self):
test_utils.BaseTest.setUp(self)
config.Set('primary_domain', 'primary.com')
def testGetClientConfig(self):
"""Confirms that GetClientConfig sets up the correct JS parameters."""
analytics_id = 'US-foo'
client_config = maps.ClientConfig.Create(
'goog-test',
allowed_referer_domains=['google.org', 'cr.appspot.com'],
hide_footer=True,
hide_share_button=True,
hide_my_location_button=True,
allow_embed_map_callback=True,
show_login=True,
analytics_id=analytics_id,
enable_editing=True)
client_config.put()
self.assertEquals({'hide_share_button': True,
'hide_my_location_button': True,
'hide_footer': True,
'allow_embed_map_callback': True,
'show_login': True,
'hide_google_plus_button': False,
'hide_facebook_button': False,
'hide_twitter_button': False,
'analytics_id': analytics_id,
'custom_head_html': '',
'panel_side': 'right',
'panel_float': False,
'enable_editing': True,
'enable_osm_map_type': False,
'enable_osm_map_type_editing': False,
'minimal_map_controls': False,
'hide_panel_header': False,
'enable_layer_filter': False,
'google_api_key': '',
'use_tab_panel': False,
'use_details_tab': False,
'urlshortener_api_url': ''},
client_config.AsDict())
config_dict = client_config.AsDict()
# Try invalid referers.
self.assertEquals({}, maps.GetClientConfig(None, None))
self.assertEquals({}, maps.GetClientConfig('', ''))
self.assertEquals({}, maps.GetClientConfig('goog-test', None))
# Try referers that aren't allowed to use this config.
self.assertEquals({}, maps.GetClientConfig(
'goog-test', 'http://foo.appspot.com'))
self.assertEquals({}, maps.GetClientConfig(
'goog-test', 'http://fakegoogle.org'))
# Try a nonexistent config.
self.assertEquals({}, maps.GetClientConfig(
'goog-test2', 'http://cr.appspot.com'))
# Try referers that should be allowed to use this config.
self.assertEquals(config_dict, maps.GetClientConfig(
'goog-test', None, dev_mode=True))
self.assertEquals(config_dict, maps.GetClientConfig(
'goog-test', 'http://cr.appspot.com'))
self.assertEquals(config_dict, maps.GetClientConfig(
'goog-test', 'https://www.google.org'))
# test that setting default overrides even without a referer domain.
maps.ClientConfig.Create('default', enable_editing=True).put()
self.assertTrue(maps.GetClientConfig(None, None)['enable_editing'])
def testGetMapPickerItems(self):
"""Tests GetMapPickerItems()."""
with test_utils.RootLogin():
model.CatalogEntryModel(key_name='foo.com:m1', domain='foo.com',
label='m1', title='Map 1', is_listed=True).put()
model.CatalogEntryModel(key_name='primary.com:m2', domain='primary.com',
label='m2', title='Map 2', is_listed=True).put()
self.assertEquals([{'title': 'Map 1', 'url': '/root/foo.com/m1'}],
maps.GetMapPickerItems('foo.com', '/root'))
self.assertEquals([{'title': 'Map 2', 'url': '/root/m2'}],
maps.GetMapPickerItems('primary.com', '/root'))
def testClientConfigOverride(self):
"""Verifies that query parameters can override client config settings."""
cm_config = maps.GetConfig(test_utils.SetupRequest('/?dev=1&show_login=1'))
self.assertEquals(True, cm_config['show_login'])
def testGetMapsApiClientId(self):
"""Tests the GetMapsApiClientId method."""
self.assertEquals('google-crisis-response',
maps.GetMapsApiClientId('google.com'))
self.assertEquals('google-crisis-response',
maps.GetMapsApiClientId('google.org'))
self.assertEquals('google-crisis-response',
maps.GetMapsApiClientId('foo.google.com'))
self.assertEquals('google-crisis-response',
maps.GetMapsApiClientId('foo.google.com:8000'))
self.assertEquals('', maps.GetMapsApiClientId('localhost'))
self.assertEquals('', maps.GetMapsApiClientId('localhost:8000'))
self.assertEquals('', maps.GetMapsApiClientId('foo.appspot.com'))
self.assertEquals('', maps.GetMapsApiClientId('foo.googleplex.com'))
def testMapsApiUrlI18n(self):
"""Verifies that language and region are set correctly for the Maps API."""
cm_config = maps.GetConfig(test_utils.SetupRequest('/'))
self.assertTrue('language=en' in cm_config['maps_api_url'])
self.assertFalse('region=' in cm_config['maps_api_url'])
cm_config = maps.GetConfig(test_utils.SetupRequest('/?hl=ja', 'ja'))
self.assertTrue('language=ja' in cm_config['maps_api_url'])
self.assertFalse('region=' in cm_config['maps_api_url'])
cm_config = maps.GetConfig(test_utils.SetupRequest('/?hl=th&gl=IN', 'th'))
self.assertTrue('language=th' in cm_config['maps_api_url'])
self.assertTrue('region=IN' in cm_config['maps_api_url'])
def testMapRegion(self):
"""Verifies that the 'region' property affects the Maps API URL."""
m1 = test_utils.CreateMap({'title': 'no region'})
m2 = test_utils.CreateMap({'title': 'has region', 'region': 'in'})
with test_utils.RootLogin():
cm_config = maps.GetConfig(test_utils.SetupRequest('/.maps/' + m1.id), m1)
self.assertTrue('region=' not in cm_config['maps_api_url'])
cm_config = maps.GetConfig(test_utils.SetupRequest('/.maps/' + m2.id), m2)
self.assertTrue('region=in' in cm_config['maps_api_url'])
def testMapIdInConfig_DraftMap(self):
"""Verifies the map ID is added to the map_root always."""
with test_utils.RootLogin():
my_map = test_utils.CreateMap()
cfg = maps.GetConfig(test_utils.SetupRequest('/.maps/' + my_map.id),
my_map)
self.assertEqual(my_map.id, cfg['map_root']['id'])
def testMapIdInConfig_PublishedMap(self):
with test_utils.RootLogin():
my_map = test_utils.CreateMap()
my_entry = model.CatalogEntry.Create(
test_utils.PRIMARY_DOMAIN, 'label', my_map)
cfg = maps.GetConfig(
test_utils.SetupRequest('/a/%s/label' % test_utils.PRIMARY_DOMAIN),
catalog_entry=my_entry)
self.assertEqual(my_map.id, cfg['map_root']['id'])
def testToPlainText(self):
self.assertEquals('', maps.ToPlainText(None))
self.assertEquals('', maps.ToPlainText(''))
# Converts block tags to ' / ' and collapses ' / / ' to ' / '.
self.assertEquals(' / paragraph with a / break', maps.ToPlainText(
'<p>paragraph with a <br/> break</p>'))
self.assertEquals('multiple / paragraph breaks', maps.ToPlainText(
'multiple <p> <p> <p> paragraph breaks'))
# Strips out all other HTML tags.
self.assertEquals(' / p in a ul with a span', maps.ToPlainText(
'<ul><p>p in a ul with a <span>span</span></p><ul>'))
# Preserves entity and charrefs.
self.assertEquals('&{ø', maps.ToPlainText(
'&{ø'))
def testEmbeddable(self):
response = self.DoGet('/empty')
self.assertFalse(response.headers.get('X-Frame-Options'))
class MapListTest(test_utils.BaseTest):
"""Tests for the map listing pages served by maps.py."""
def testGet(self):
"""Tests the map listing page."""
with test_utils.RootLogin():
domains.Domain.Put('cows.net')
domains.Domain.Put('dogs.org')
m1 = model.Map.Create({'title': 'Moo'}, 'cows.net', viewers=['viewer'])
m2 = model.Map.Create({'title': 'Arf'}, 'dogs.org', viewers=['viewer'])
with test_utils.Login('viewer'):
result = self.DoGet('/.maps').body
self.assertTrue('Moo' in result, result)
self.assertTrue('.maps/' + m1.id in result, result)
self.assertTrue('Arf' in result, result)
self.assertTrue('.maps/' + m2.id in result, result)
result = self.DoGet('/dogs.org/.maps').body
self.assertTrue('Moo' not in result, result)
self.assertTrue('.maps/' + m1.id not in result, result)
self.assertTrue('Arf' in result, result)
self.assertTrue('.maps/' + m2.id in result, result)
def testClickjackingPrevention(self):
with test_utils.Login('viewer'):
response = self.DoGet('/.maps')
self.assertEquals('DENY', response.headers.get('X-Frame-Options'))
if __name__ == '__main__':
test_utils.main()