Skip to content

Commit

Permalink
🔧 Cleanup test_get_requests.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
theengineear committed Feb 2, 2017
1 parent 463dfae commit 1441b71
Showing 1 changed file with 114 additions and 128 deletions.
242 changes: 114 additions & 128 deletions plotly/tests/test_core/test_get_requests/test_get_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from nose.plugins.attrib import attr
from requests.compat import json as _json

from plotly.tests.utils import PlotlyTestCase

default_headers = {'plotly-username': '',
'plotly-apikey': '',
Expand All @@ -20,137 +21,122 @@

server = "https://plot.ly"

# username = "get_test_user"
# password = "password"
# api_key = "vgs6e0cnoi" (currently...)


@attr('slow')
def test_user_does_not_exist():
username = 'user_does_not_exist'
api_key = 'invalid-apikey'
file_owner = 'get_test_user'
file_id = 0
hd = copy.copy(default_headers)
hd['plotly-username'] = username
hd['plotly-apikey'] = api_key
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = _json.loads(response.content)
print(response.status_code)
print(content)
assert response.status_code == 404
assert (content['error'] == "Aw, snap! We don't have an account for {0}. "
"Want to try again? Sign in is not case "
"sensitive.".format(username))


@attr('slow')
def test_file_does_not_exist():
username = 'PlotlyImageTest'
api_key = '786r5mecv0'
file_owner = 'get_test_user'
file_id = 1000
hd = copy.copy(default_headers)
hd['plotly-username'] = username
hd['plotly-apikey'] = api_key
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = _json.loads(response.content)
print(response.status_code)
print(content)
assert response.status_code == 404
assert (content['error'] == "Aw, snap! It looks like this file does not "
"exist. Want to try again?")


@attr('slow')
def test_wrong_api_key(): # TODO: does this test the right thing?
username = 'PlotlyImageTest'
api_key = 'invalid-apikey'
file_owner = 'get_test_user'
file_id = 0
hd = copy.copy(default_headers)
hd['plotly-username'] = username
hd['plotly-apikey'] = api_key
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
assert response.status_code == 401
# TODO: check error message?


# Locked File
# TODO

@attr('slow')
def test_private_permission_defined():
username = 'PlotlyImageTest'
api_key = '786r5mecv0'
file_owner = 'get_test_user'
file_id = 1 # 1 is a private file
hd = copy.copy(default_headers)
hd['plotly-username'] = username
hd['plotly-apikey'] = api_key
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = _json.loads(response.content)
print(response.status_code)
print(content)
assert response.status_code == 403

# Private File that is shared
# TODO


@attr('slow')
def test_missing_headers():
file_owner = 'get_test_user'
file_id = 0
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
headers = list(default_headers.keys())
for header in headers:

class GetRequestsTest(PlotlyTestCase):

@attr('slow')
def test_user_does_not_exist(self):
username = 'user_does_not_exist'
api_key = 'invalid-apikey'
file_owner = 'get_test_user'
file_id = 0
hd = copy.copy(default_headers)
hd['plotly-username'] = username
hd['plotly-apikey'] = api_key
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = _json.loads(response.content)
error_message = ("Aw, snap! We don't have an account for {0}. Want to "
"try again? Sign in is not case sensitive."
.format(username))
self.assertEqual(response.status_code, 404)
self.assertEqual(content['error'], error_message)

@attr('slow')
def test_file_does_not_exist(self):
username = 'PlotlyImageTest'
api_key = '786r5mecv0'
file_owner = 'get_test_user'
file_id = 1000
hd = copy.copy(default_headers)
hd['plotly-username'] = username
hd['plotly-apikey'] = api_key
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = _json.loads(response.content)
error_message = ("Aw, snap! It looks like this file does "
"not exist. Want to try again?")
self.assertEqual(response.status_code, 404)
self.assertEqual(content['error'], error_message)

@attr('slow')
def test_wrong_api_key(self): # TODO: does this test the right thing?
username = 'PlotlyImageTest'
api_key = 'invalid-apikey'
file_owner = 'get_test_user'
file_id = 0
hd = copy.copy(default_headers)
hd['plotly-username'] = username
hd['plotly-apikey'] = api_key
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
self.assertEqual(response.status_code, 401)
# TODO: check error message?

# Locked File
# TODO

@attr('slow')
def test_private_permission_defined(self):
username = 'PlotlyImageTest'
api_key = '786r5mecv0'
file_owner = 'get_test_user'
file_id = 1 # 1 is a private file
hd = copy.copy(default_headers)
hd['plotly-username'] = username
hd['plotly-apikey'] = api_key
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = _json.loads(response.content)
self.assertEqual(response.status_code, 403)

# Private File that is shared
# TODO

@attr('slow')
def test_missing_headers(self):
file_owner = 'get_test_user'
file_id = 0
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
headers = list(default_headers.keys())
for header in headers:
hd = copy.copy(default_headers)
del hd[header]
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = _json.loads(response.content)
self.assertEqual(response.status_code, 422)

@attr('slow')
def test_valid_request(self):
username = 'PlotlyImageTest'
api_key = '786r5mecv0'
file_owner = 'get_test_user'
file_id = 0
hd = copy.copy(default_headers)
del hd[header]
hd['plotly-username'] = username
hd['plotly-apikey'] = api_key
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = _json.loads(response.content)
print(response.status_code)
print(content)
assert response.status_code == 422


@attr('slow')
def test_valid_request():
username = 'PlotlyImageTest'
api_key = '786r5mecv0'
file_owner = 'get_test_user'
file_id = 0
hd = copy.copy(default_headers)
hd['plotly-username'] = username
hd['plotly-apikey'] = api_key
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = _json.loads(response.content)
print(response.status_code)
print(content)
assert response.status_code == 200
# content = _json.loads(res.content)
# response_payload = content['payload']
# figure = response_payload['figure']
# if figure['data'][0]['x'] != [u'1', u'2', u'3']:
# print('ERROR')
# return res
self.assertEqual(response.status_code, 200)
# content = _json.loads(res.content)
# response_payload = content['payload']
# figure = response_payload['figure']
# if figure['data'][0]['x'] != [u'1', u'2', u'3']:
# print('ERROR')
# return res

0 comments on commit 1441b71

Please sign in to comment.