Skip to content

Commit ba5975f

Browse files
authored
Merge pull request #997 from plotly/error-msg-for-http
validate plotly_(api)_domain - HTTPS, not HTTP
2 parents 8600291 + 2a0872c commit ba5975f

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

Diff for: plotly/tests/test_core/test_stream/test_stream.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
tk = 'vaia8trjjb'
1919
config = {'plotly_domain': 'https://plot.ly',
2020
'plotly_streaming_domain': 'stream.plot.ly',
21-
'plotly_api_domain': 'https://api.plot.ly',
21+
'plotly_api_domain': 'https://api.plot.ly',
2222
'plotly_ssl_verification': False}
2323

2424

Diff for: plotly/tests/test_core/test_tools/test_file_tools.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from plotly import tools, session
22
from plotly.tests.utils import PlotlyTestCase
33

4+
import warnings
5+
46

57
class FileToolsTest(PlotlyTestCase):
68

@@ -43,14 +45,37 @@ def test_set_config_file_two_entries(self):
4345
self.assertEqual(config['plotly_streaming_domain'], streaming_domain)
4446
tools.reset_config_file()
4547

46-
4748
def test_set_config_file_world_readable(self):
4849

4950
# Return TypeError when world_readable type is not a bool
5051

5152
kwargs = {'world_readable': 'True'}
5253
self.assertRaises(TypeError, tools.set_config_file, **kwargs)
5354

55+
def test_set_config_expected_warning_msg(self):
56+
57+
# Check that UserWarning is being called with http plotly_domain
58+
59+
with warnings.catch_warnings(record=True) as w:
60+
warnings.simplefilter("always")
61+
kwargs = {'plotly_domain': 'http://www.foo-bar.com'}
62+
tools.set_config_file(**kwargs)
63+
assert len(w) == 1
64+
assert issubclass(w[-1].category, UserWarning)
65+
assert "plotly_domain" in str(w[-1].message)
66+
67+
68+
def test_set_config_no_warning_msg_if_plotly_domain_is_https(self):
69+
70+
# Check that no UserWarning is being called with https plotly_domain
71+
72+
with warnings.catch_warnings(record=True) as w:
73+
warnings.simplefilter("always")
74+
kwargs = {'plotly_domain': 'https://www.foo-bar.com'}
75+
tools.set_config_file(**kwargs)
76+
assert len(w) == 0
77+
78+
5479
def test_reset_config_file(self):
5580

5681
# Check reset_config and get_config return the same values

Diff for: plotly/tools.py

+6
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def set_config_file(plotly_domain=None,
192192
ensure_local_plotly_files() # make sure what's there is OK
193193
utils.validate_world_readable_and_sharing_settings({
194194
'sharing': sharing, 'world_readable': world_readable})
195+
195196
settings = get_config_file()
196197
if isinstance(plotly_domain, six.string_types):
197198
settings['plotly_domain'] = plotly_domain
@@ -218,6 +219,11 @@ def set_config_file(plotly_domain=None,
218219
elif auto_open is not None:
219220
raise TypeError('auto_open should be a boolean')
220221

222+
# validate plotly_domain and plotly_api_domain
223+
utils.validate_plotly_domains(
224+
{'plotly_domain': plotly_domain, 'plotly_api_domain': plotly_api_domain}
225+
)
226+
221227
if isinstance(world_readable, bool):
222228
settings['world_readable'] = world_readable
223229
settings.pop('sharing')

Diff for: plotly/utils.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
"""
88
from __future__ import absolute_import
99

10+
import decimal
1011
import os.path
1112
import re
1213
import sys
1314
import threading
14-
import decimal
15+
import warnings
1516
from collections import deque
1617

1718
import pytz
@@ -32,6 +33,30 @@
3233
lock = threading.Lock()
3334

3435

36+
http_msg = (
37+
"The plotly_domain and plotly_api_domain of your config file must start "
38+
"with 'https', not 'http'. If you are not using On-Premise then run the "
39+
"following code to ensure your plotly_domain and plotly_api_domain start "
40+
"with 'https':\n\n\n"
41+
"import plotly\n"
42+
"plotly.tools.set_config_file(\n"
43+
" plotly_domain='https://plot.ly',\n"
44+
" plotly_api_domain='https://api.plot.ly'\n"
45+
")\n\n\n"
46+
"If you are using On-Premise then you will need to use your company's "
47+
"domain and api_domain urls:\n\n\n"
48+
"import plotly\n"
49+
"plotly.tools.set_config_file(\n"
50+
" plotly_domain='https://plotly.your-company.com',\n"
51+
" plotly_api_domain='https://plotly.your-company.com'\n"
52+
")\n\n\n"
53+
"Make sure to replace `your-company.com` with the URL of your Plotly "
54+
"On-Premise server.\nSee "
55+
"https://plot.ly/python/getting-started/#special-instructions-for-plotly-onpremise-users "
56+
"for more help with getting started with On-Premise."
57+
)
58+
59+
3560
### general file setup tools ###
3661

3762
def load_json_dict(filename, *args):
@@ -297,6 +322,7 @@ def encode_as_decimal(obj):
297322
else:
298323
raise NotEncodable
299324

325+
300326
### unicode stuff ###
301327
def decode_unicode(coll):
302328
if isinstance(coll, list):
@@ -436,6 +462,16 @@ def validate_world_readable_and_sharing_settings(option_set):
436462
)
437463

438464

465+
def validate_plotly_domains(option_set):
466+
domains_not_none = []
467+
for d in ['plotly_domain', 'plotly_api_domain']:
468+
if d in option_set and option_set[d]:
469+
domains_not_none.append(option_set[d])
470+
471+
if not all(d.lower().startswith('https') for d in domains_not_none):
472+
warnings.warn(http_msg, category=UserWarning)
473+
474+
439475
def set_sharing_and_world_readable(option_set):
440476
if 'world_readable' in option_set and 'sharing' not in option_set:
441477
option_set['sharing'] = (

0 commit comments

Comments
 (0)