-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Validate signin #668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate signin #668
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from __future__ import absolute_import | ||
|
||
from plotly.api.v2 import files, folders, grids, images, plot_schema, plots | ||
from plotly.api.v2 import (files, folders, grids, images, plot_schema, plots, | ||
users) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""Interface to Plotly's /v2/files endpoints.""" | ||
from __future__ import absolute_import | ||
|
||
from plotly.api.v2.utils import build_url, request | ||
|
||
RESOURCE = 'users' | ||
|
||
|
||
def current(): | ||
""" | ||
Retrieve information on the logged-in user from Plotly. | ||
|
||
:returns: (requests.Response) Returns response directly from requests. | ||
|
||
""" | ||
url = build_url(RESOURCE, route='current') | ||
return request('get', url) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import absolute_import | ||
|
||
from plotly.api.v2 import users | ||
from plotly.tests.test_core.test_api import PlotlyApiTestCase | ||
|
||
|
||
class UsersTest(PlotlyApiTestCase): | ||
|
||
def setUp(self): | ||
super(UsersTest, self).setUp() | ||
|
||
# Mock the actual api call, we don't want to do network tests here. | ||
self.request_mock = self.mock('plotly.api.v2.utils.requests.request') | ||
self.request_mock.return_value = self.get_response() | ||
|
||
# Mock the validation function since we can test that elsewhere. | ||
self.mock('plotly.api.v2.utils.validate_response') | ||
|
||
def test_current(self): | ||
users.current() | ||
self.request_mock.assert_called_once() | ||
args, kwargs = self.request_mock.call_args | ||
method, url = args | ||
self.assertEqual(method, 'get') | ||
self.assertEqual( | ||
url, '{}/v2/users/current'.format(self.plotly_api_domain) | ||
) | ||
self.assertNotIn('params', kwargs) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,43 @@ | ||
from __future__ import absolute_import | ||
|
||
from unittest import TestCase | ||
from mock import patch | ||
|
||
import plotly.plotly.plotly as py | ||
import plotly.session as session | ||
import plotly.tools as tls | ||
from plotly import exceptions | ||
from plotly.tests.utils import PlotlyTestCase | ||
|
||
|
||
def test_get_credentials(): | ||
session_credentials = session.get_session_credentials() | ||
if 'username' in session_credentials: | ||
del session._session['credentials']['username'] | ||
if 'api_key' in session_credentials: | ||
del session._session['credentials']['api_key'] | ||
creds = py.get_credentials() | ||
file_creds = tls.get_credentials_file() | ||
print(creds) | ||
print(file_creds) | ||
assert creds == file_creds | ||
class TestSignIn(PlotlyTestCase): | ||
|
||
def setUp(self): | ||
super(TestSignIn, self).setUp() | ||
patcher = patch('plotly.api.v2.users.current') | ||
self.users_current_mock = patcher.start() | ||
self.addCleanup(patcher.stop) | ||
|
||
def test_sign_in(): | ||
un = 'anyone' | ||
ak = 'something' | ||
# TODO, add this! | ||
# si = ['this', 'and-this'] | ||
py.sign_in(un, ak) | ||
creds = py.get_credentials() | ||
assert creds['username'] == un | ||
assert creds['api_key'] == ak | ||
# TODO, and check it! | ||
# assert creds['stream_ids'] == si | ||
def test_get_credentials(self): | ||
session_credentials = session.get_session_credentials() | ||
if 'username' in session_credentials: | ||
del session._session['credentials']['username'] | ||
if 'api_key' in session_credentials: | ||
del session._session['credentials']['api_key'] | ||
creds = py.get_credentials() | ||
file_creds = tls.get_credentials_file() | ||
self.assertEqual(creds, file_creds) | ||
|
||
|
||
class TestSignIn(TestCase): | ||
def test_sign_in(self): | ||
un = 'anyone' | ||
ak = 'something' | ||
# TODO, add this! | ||
# si = ['this', 'and-this'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's just old code. I just needed to add it to the |
||
py.sign_in(un, ak) | ||
creds = py.get_credentials() | ||
self.assertEqual(creds['username'], un) | ||
self.assertEqual(creds['api_key'], ak) | ||
# TODO, and check it! | ||
# assert creds['stream_ids'] == si | ||
|
||
def test_get_config(self): | ||
plotly_domain = 'test domain' | ||
|
@@ -74,3 +78,10 @@ def test_sign_in_with_config(self): | |
self.assertEqual( | ||
config['plotly_ssl_verification'], plotly_ssl_verification | ||
) | ||
|
||
def test_sign_in_cannot_validate(self): | ||
self.users_current_mock.side_effect = exceptions.PlotlyRequestError( | ||
'msg', 400, 'foobar' | ||
) | ||
with self.assertRaisesRegexp(exceptions.PlotlyError, 'Sign in failed'): | ||
py.sign_in('foo', 'bar') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just curious, why did you switch up the order like this? Is it because
tools
is moving into a different location for2.0.0.
and therefore its priority is being hierarchically diminished? (I don't know the proper jargon for what I'm describing...)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like it's just alphabetical 😎