Skip to content

Commit

Permalink
Merge pull request #212 from michelesr/dev
Browse files Browse the repository at this point in the history
Test of legacy urls
  • Loading branch information
feroda committed Nov 24, 2015
2 parents fbb1102 + 28a686c commit 3c829bd
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ before_install:
- sudo mv docker-compose /usr/local/bin

install:
- pip install pytest
# replace the docker-compose.yml with the specific for the CI
- mv compose/ci.yml docker-compose.yml
# set containers as privileged to bypass Travis environment limitations
Expand All @@ -40,3 +41,4 @@ script:

# start tests
- docker-compose run e2e
- py.test test/integration/http
Empty file.
112 changes: 112 additions & 0 deletions test/integration/http/test_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/env python2

import Cookie
import cookielib
import collections
import sys
import urllib
import urllib2
from os.path import realpath

LEGACY_REST_PREFIX = "gasistafelice/rest/"
verbose = False


def get_cookie(base_url):
""" Init the session with the backend and return the session cookie """

auth_url = base_url + \
"gasistafelice/accounts/login/?next=/gasistafelice/rest"
req = urllib2.Request(auth_url)
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.open(req)

for cookie in cj:
if cookie.name == "csrftoken":
csrftoken = cookie.value
elif cookie.name == "sessionid":
sessionid = cookie.value

data = urllib.urlencode({"username": "01gas1",
"password": "des",
"csrfmiddlewaretoken": csrftoken})
c = Cookie.SimpleCookie()
c['sessionid'] = sessionid
c['csrftoken'] = csrftoken
cookie_string = c.output(header="", sep=";")

urllib2.Request(auth_url, data=data, headers={"Cookie": cookie_string})
return c


def check_url(cookie, url):
"""
Check that a url returns HTTP status code 200
"""

# Selenium does not offer a way to check the HTTP status code,
# and it would even slow to use it for this kind of tests,
# so we get the valid session id from the webdriver and we pass
# it in the cookie header
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'

cookie_string = cookie.output(header="", sep=";")

headers = {'User-Agent': user_agent, 'Cookie': cookie_string, }

req = urllib2.Request(url, headers=headers)
resp = urllib2.urlopen(req)
return resp


def main(base_url):
"""
Do the job and return a dictionary with elements:
* 'OK' => a list of dicts with 'code' and 'url' of success;
* 'ERROR' => a list of dicts with 'code' and 'url' of failures;
The urls are readed from the "urls.txt" failures
"""

cookie = get_cookie(base_url)
rv = collections.OrderedDict([('OK', []), ('ERROR', [])])

# get the script directory, that is the same of "urls.txt"
file_dir = '/'.join(realpath(__file__).split('/')[:-1])
with open(file_dir + "/urls.txt", "r") as urls_file:
for line in urls_file.readlines():
# strip the '/n'
url = base_url + line[:-1]
try:
response = check_url(cookie, url)
except urllib2.HTTPError, e:
rv['ERROR'].append({'code': e.code, 'url': url})
else:
rv['OK'].append({'code': response.getcode(), 'url': url})

return rv


def test_entrypoint():
"""Entrypoint for pytest runner"""
base_url = "http://localhost:8080/"

if __name__ == "__main__":
try:
# two slashes are better then zero
base_url = sys.argv[1] + "/"
except IndexError:
pass

rv = main(base_url)
for kind, resps in rv.items():
for r in resps:
formatted_out = "{}[{}] {}".format(kind, r['code'], r['url'])
if verbose:
print(formatted_out)
assert r['code'] == 200, formatted_out


if __name__ == "__main__":
verbose = True
test_entrypoint()
68 changes: 68 additions & 0 deletions test/integration/http/urls.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
gasistafelice/rest/gasmember/1/
gasistafelice/rest/gasmember/1/order
gasistafelice/rest/gasmember/1/basket
gasistafelice/rest/gasmember/1/basket_sent
gasistafelice/rest/gasmember/1/gasmember_details
gasistafelice/rest/gasmember/1/person_details
gasistafelice/rest/gasmember/1/balance_gm
gasistafelice/rest/gasmember/1/gasmember_transactions
gasistafelice/rest/person/1/
gasistafelice/rest/person/1/person_details
gasistafelice/rest/person/1/person_gasmembers
gasistafelice/rest/pact/1/
gasistafelice/rest/pact/1/open_orders
gasistafelice/rest/pact/1/gasstocks
gasistafelice/rest/pact/1/pact_details
gasistafelice/rest/pact/1/closed_orders
gasistafelice/rest/pact/1/balance_pact
gasistafelice/rest/pact/1/insolutes_orders
gasistafelice/rest/pact/1/transactions
gasistafelice/rest/pact/1/stored_orders
gasistafelice/rest/place/1/
gasistafelice/rest/supplier/1/
gasistafelice/rest/supplier/1/stocks
gasistafelice/rest/supplier/1/open_orders
gasistafelice/rest/supplier/1/prepared_orders
gasistafelice/rest/supplier/1/supplier_pacts
gasistafelice/rest/supplier/1/supplier_details
gasistafelice/rest/supplier/1/categories
gasistafelice/rest/supplier/1/closed_orders
gasistafelice/rest/supplier/1/supplier_users
gasistafelice/rest/supplier/1/balance
gasistafelice/rest/supplier/1/transactions
gasistafelice/rest/supplier/1/stored_orders
gasistafelice/rest/stock/1/
gasistafelice/rest/stock/1/stock_details
gasistafelice/rest/stock/1/open_orders
gasistafelice/rest/gas/1/
gasistafelice/rest/gas/1/open_orders
gasistafelice/rest/gas/1/closed_orders
gasistafelice/rest/gas/1/prepared_orders
gasistafelice/rest/gas/1/gas_pacts
gasistafelice/rest/gas/1/categories
gasistafelice/rest/gas/1/gas_details
gasistafelice/rest/gas/1/gasmembers
gasistafelice/rest/gas/1/gas_users
gasistafelice/rest/gas/1/balance_gas
gasistafelice/rest/gas/1/insolutes_orders
gasistafelice/rest/gas/1/transactions
gasistafelice/rest/gas/1/recharge
gasistafelice/rest/gas/1/fee
gasistafelice/rest/gas/1/stored_orders
gasistafelice/rest/site/1/
gasistafelice/rest/site/1/gas_list
gasistafelice/rest/site/1/suppliers_report
gasistafelice/rest/site/1/persons
gasistafelice/rest/site/1/open_orders
gasistafelice/rest/site/1/des_pacts
gasistafelice/rest/site/1/details
gasistafelice/rest/site/1/categories
gasistafelice/rest/site/1/balance
gasistafelice/rest/site/1/site_transactions
gasistafelice/rest/site/1/stored_orders
gasistafelice/rest/order/1/
gasistafelice/rest/order/1/order_details
gasistafelice/rest/order/1/order_report
gasistafelice/rest/order/1/order_invoice
gasistafelice/rest/order/1/curtail
gasistafelice/rest/place/1/details

0 comments on commit 3c829bd

Please sign in to comment.