|
7 | 7 | """
|
8 | 8 | from __future__ import absolute_import
|
9 | 9 |
|
10 |
| -from unittest import TestCase |
| 10 | +import requests |
| 11 | +import six |
11 | 12 |
|
| 13 | +from unittest import TestCase |
12 | 14 | from nose.tools import raises
|
13 | 15 |
|
14 |
| -from plotly.exceptions import PlotlyError, PlotlyEmptyDataError |
| 16 | +import plotly.tools as tls |
15 | 17 | from plotly.plotly import plotly as py
|
| 18 | +from plotly.exceptions import PlotlyError, PlotlyEmptyDataError |
16 | 19 |
|
17 | 20 |
|
18 | 21 | # username for tests: 'PlotlyImageTest'
|
@@ -168,3 +171,83 @@ def test_plot_option_logic_only_sharing_given(self):
|
168 | 171 | 'world_readable': False,
|
169 | 172 | 'sharing': 'private'}
|
170 | 173 | self.assertEqual(plot_option_logic, expected_plot_option_logic)
|
| 174 | + |
| 175 | + def test_plot_url_given_sharing_key(self): |
| 176 | + |
| 177 | + # Give share_key is requested, the retun url should contain |
| 178 | + # the share_key |
| 179 | + |
| 180 | + data = { |
| 181 | + 'data': [ |
| 182 | + { |
| 183 | + 'x': [1, 2, 3], |
| 184 | + 'y': [2, 1, 2] |
| 185 | + } |
| 186 | + ] |
| 187 | + } |
| 188 | + validate = True |
| 189 | + fig = tls.return_figure_from_figure_or_data(data, validate) |
| 190 | + kwargs = {'filename': 'is_share_key_included', |
| 191 | + 'fileopt': 'overwrite', |
| 192 | + 'world_readable': False, |
| 193 | + 'sharing': 'secret'} |
| 194 | + |
| 195 | + self.assertTrue('share_key=' in |
| 196 | + py._send_to_plotly(fig, **kwargs)['url']) |
| 197 | + |
| 198 | + def test_plot_url_response_given_sharing_key(self): |
| 199 | + |
| 200 | + # Give share_key is requested, the get request of url should |
| 201 | + # be 200 |
| 202 | + |
| 203 | + data = { |
| 204 | + 'data': [ |
| 205 | + { |
| 206 | + 'x': [1, 2, 3], |
| 207 | + 'y': [2, 1, 2] |
| 208 | + } |
| 209 | + ] |
| 210 | + } |
| 211 | + validate = True |
| 212 | + fig = tls.return_figure_from_figure_or_data(data, validate) |
| 213 | + kwargs = {'filename': 'is_share_key_included', |
| 214 | + 'fileopt': 'overwrite', |
| 215 | + 'world_readable': False, |
| 216 | + 'sharing': 'secret'} |
| 217 | + |
| 218 | + self.assertTrue(200 == |
| 219 | + requests.get( |
| 220 | + py._send_to_plotly(fig, **kwargs)['url']) |
| 221 | + .status_code) |
| 222 | + |
| 223 | + def test_private_plot_response_with_and_without_share_key(self): |
| 224 | + |
| 225 | + # The get response of private plot should be 404 and once share_key |
| 226 | + # is added it should be 200 |
| 227 | + |
| 228 | + data = { |
| 229 | + 'data': [ |
| 230 | + { |
| 231 | + 'x': [1, 2, 3], |
| 232 | + 'y': [2, 1, 2] |
| 233 | + } |
| 234 | + ] |
| 235 | + } |
| 236 | + validate = True |
| 237 | + fig = tls.return_figure_from_figure_or_data(data, validate) |
| 238 | + kwargs = {'filename': 'is_share_key_included', |
| 239 | + 'fileopt': 'overwrite', |
| 240 | + 'world_readable': False, |
| 241 | + 'sharing': 'private'} |
| 242 | + |
| 243 | + private_plot_url = py._send_to_plotly(fig, **kwargs)['url'] |
| 244 | + |
| 245 | + self.assertTrue(404 == |
| 246 | + requests.get(private_plot_url + ".json").status_code) |
| 247 | + |
| 248 | + secret_plot_url = py.add_share_key_to_url(private_plot_url) |
| 249 | + urlsplit = six.moves.urllib.parse.urlparse(secret_plot_url) |
| 250 | + secret_plot_url_in_json = six.moves.urllib.parse.urljoin( |
| 251 | + urlsplit.geturl(), "?.json" + urlsplit.query) |
| 252 | + self.assertTrue(200 == |
| 253 | + requests.get(secret_plot_url_in_json).status_code) |
0 commit comments