|
1 | 1 | from __future__ import absolute_import
|
2 | 2 |
|
3 |
| -from unittest import TestCase |
| 3 | +from mock import patch |
4 | 4 |
|
5 | 5 | import plotly.plotly.plotly as py
|
6 | 6 | import plotly.session as session
|
7 | 7 | import plotly.tools as tls
|
| 8 | +from plotly import exceptions |
| 9 | +from plotly.tests.utils import PlotlyTestCase |
8 | 10 |
|
9 | 11 |
|
10 |
| -def test_get_credentials(): |
11 |
| - session_credentials = session.get_session_credentials() |
12 |
| - if 'username' in session_credentials: |
13 |
| - del session._session['credentials']['username'] |
14 |
| - if 'api_key' in session_credentials: |
15 |
| - del session._session['credentials']['api_key'] |
16 |
| - creds = py.get_credentials() |
17 |
| - file_creds = tls.get_credentials_file() |
18 |
| - print(creds) |
19 |
| - print(file_creds) |
20 |
| - assert creds == file_creds |
| 12 | +class TestSignIn(PlotlyTestCase): |
21 | 13 |
|
| 14 | + def setUp(self): |
| 15 | + super(TestSignIn, self).setUp() |
| 16 | + patcher = patch('plotly.api.v2.users.current') |
| 17 | + self.users_current_mock = patcher.start() |
| 18 | + self.addCleanup(patcher.stop) |
22 | 19 |
|
23 |
| -def test_sign_in(): |
24 |
| - un = 'anyone' |
25 |
| - ak = 'something' |
26 |
| - # TODO, add this! |
27 |
| - # si = ['this', 'and-this'] |
28 |
| - py.sign_in(un, ak) |
29 |
| - creds = py.get_credentials() |
30 |
| - assert creds['username'] == un |
31 |
| - assert creds['api_key'] == ak |
32 |
| - # TODO, and check it! |
33 |
| - # assert creds['stream_ids'] == si |
| 20 | + def test_get_credentials(self): |
| 21 | + session_credentials = session.get_session_credentials() |
| 22 | + if 'username' in session_credentials: |
| 23 | + del session._session['credentials']['username'] |
| 24 | + if 'api_key' in session_credentials: |
| 25 | + del session._session['credentials']['api_key'] |
| 26 | + creds = py.get_credentials() |
| 27 | + file_creds = tls.get_credentials_file() |
| 28 | + self.assertEqual(creds, file_creds) |
34 | 29 |
|
35 |
| - |
36 |
| -class TestSignIn(TestCase): |
| 30 | + def test_sign_in(self): |
| 31 | + un = 'anyone' |
| 32 | + ak = 'something' |
| 33 | + # TODO, add this! |
| 34 | + # si = ['this', 'and-this'] |
| 35 | + py.sign_in(un, ak) |
| 36 | + creds = py.get_credentials() |
| 37 | + self.assertEqual(creds['username'], un) |
| 38 | + self.assertEqual(creds['api_key'], ak) |
| 39 | + # TODO, and check it! |
| 40 | + # assert creds['stream_ids'] == si |
37 | 41 |
|
38 | 42 | def test_get_config(self):
|
39 | 43 | plotly_domain = 'test domain'
|
@@ -74,3 +78,10 @@ def test_sign_in_with_config(self):
|
74 | 78 | self.assertEqual(
|
75 | 79 | config['plotly_ssl_verification'], plotly_ssl_verification
|
76 | 80 | )
|
| 81 | + |
| 82 | + def test_sign_in_cannot_validate(self): |
| 83 | + self.users_current_mock.side_effect = exceptions.PlotlyRequestError( |
| 84 | + 'msg', 400, 'foobar' |
| 85 | + ) |
| 86 | + with self.assertRaisesRegexp(exceptions.PlotlyError, 'Sign in failed'): |
| 87 | + py.sign_in('foo', 'bar') |
0 commit comments