def alt_tests(x): print "------------------- Alternative Storage --------------------" session=Session(); r=parse_info(session.transmit("/alt?x=a")); test(not ('sc' in session.state)) test(r["is_set_x"]=='0') test(r['x']=="''") test('sid' in r) sid = r['sid'] r=parse_info(session.transmit("/alt?sid=" + sid)); test(not ('sc' in session.state)) test(r["is_set_x"]=='1') test(r["x"]=="'a'"); test('sid' in r) sid = r['sid'] r=parse_info(session.transmit("/alt?x=2&sid=" + sid)); test(not ('sc' in session.state)) test(r["is_set_x"]=='1') test(r["x"]=="'a'"); test('sid' in r) sid = r['sid'] r=parse_info(session.transmit("/alt?sid=" + sid)); test(not ('sc' in session.state)) test(r["is_set_x"]=='1') test(r["x"]=="'2'"); test('sid' in r)
def no_replay(x): session = Session() session.transmit('/new') old_state = session.state session.transmit('/clear') session.state = old_state test(parse_info(session.transmit('/info'))['is_set_x'] == '0')
def delete_article( session=Session(), lang='en', slug='test-delete', ): SHOW_ARTICLE_URL = '/articles/show/' + slug CREATE_ARTICLE_URL = '/articles/create/' + slug REMOVE_ARTICLE_URL = '/articles/remove/' + slug session.transmit(REMOVE_ARTICLE_URL) if (not session.test_http_status([302])): print("ERROR while trying to remove an article") return False # now trying to access to the article should redirect us ... registerHTML = session.transmit(SHOW_ARTICLE_URL) if (not session.test_http_status([302])): print("ERROR while trying to access to a non-existing article") return False # ... to the page to create it if (session.get_redirection() != CREATE_ARTICLE_URL): print("ERROR we should be redirected to the page to create an article") return False return True
def no_replay(x): session=Session() session.transmit('/new') old_state = session.state session.transmit('/clear') session.state = old_state test(parse_info(session.transmit('/info'))['is_set_x']=='0')
def no_replay(x): session = Session() session.transmit("/new") old_state = session.state session.transmit("/clear") session.state = old_state test(parse_info(session.transmit("/info"))["is_set_x"] == "0")
def alt_tests(x): print "------------------- Alternative Storage --------------------" session = Session() r = parse_info(session.transmit("/alt?x=a")) test(not ('sc' in session.state)) test(r["is_set_x"] == '0') test(r['x'] == "''") test('sid' in r) sid = r['sid'] r = parse_info(session.transmit("/alt?sid=" + sid)) test(not ('sc' in session.state)) test(r["is_set_x"] == '1') test(r["x"] == "'a'") test('sid' in r) sid = r['sid'] r = parse_info(session.transmit("/alt?x=2&sid=" + sid)) test(not ('sc' in session.state)) test(r["is_set_x"] == '1') test(r["x"] == "'a'") test('sid' in r) sid = r['sid'] r = parse_info(session.transmit("/alt?sid=" + sid)) test(not ('sc' in session.state)) test(r["is_set_x"] == '1') test(r["x"] == "'2'") test('sid' in r)
def reset_session(x): print "------------------- Reset Session --------------------" session = Session() session.transmit('/new') prev_cookie = session.state['sc'].value session.transmit('/reset') new_cookie = session.state['sc'].value test(prev_cookie != new_cookie) test(prev_cookie[0] == 'I') test(new_cookie[0] == 'I') test(parse_info(session.transmit('/info'))['is_set_x'] == '1')
def reset_session(x): print "------------------- Reset Session --------------------" session=Session() session.transmit('/new') prev_cookie = session.state['sc'].value session.transmit('/reset') new_cookie = session.state['sc'].value test(prev_cookie != new_cookie) test(prev_cookie[0]=='I') test(new_cookie[0]=='I') test(parse_info(session.transmit('/info'))['is_set_x']=='1')
def reset_session(x): print "------------------- Reset Session --------------------" session = Session() session.transmit("/new") prev_cookie = session.state["sc"].value session.transmit("/reset") new_cookie = session.state["sc"].value test(prev_cookie != new_cookie) test(prev_cookie[0] == "I") test(new_cookie[0] == "I") test(parse_info(session.transmit("/info"))["is_set_x"] == "1")
def translate_article( session = Session(), lang = 'en', slug = 'test', translateLang = 'zh', translateSlug = 'test-zh', translateTitle= 'Test Zh', content = u'nihao', summary = u'created by an automated test in chinese' ): SHOW_ARTICLE_URL ='/articles/show/' + slug TRANSLATE_ARTICLE_URL ='/articles/translate/' + slug TRANSLATE_ARTICLE_TREAT_URL ='/articles/translate_treat' registerHTML = session.transmit(SHOW_ARTICLE_URL) if (not session.test_http_status([200])): return False session.transmit(TRANSLATE_ARTICLE_URL) if (not session.test_http_status([200])): print("ERROR while trying to access to the form to translate an article") return False # now we post a form to create an article postData = { "_1" : slug, "_2" : translateTitle, "_3" : translateLang, "translationSlug" : translateSlug, "_5" : content, "_6" : summary, "save_and_view" : "blah" } session.transmit_post_form( post_url = TRANSLATE_ARTICLE_TREAT_URL, post_data_array = postData ) if (not session.test_http_status([302])): print("ERROR we should be redirected"); print("status: " + str(session.status)) return False if (session.get_redirection().find(translateSlug) == -1): return False return True
def login( session=Session(print_cookies=False), username='******', password='******'): CHECKED = 'y' NEW_ARTICLE_URL = '/articles/create/foo' LOGIN_URL = '/users/login' LOGIN_TREAT_URL = '/users/login_treat' # we should be redirected to the login page when we try to access # to a page reserved to registered users session.transmit(NEW_ARTICLE_URL) if (not session.test_http_status([302])): print('ERROR we should be redirected when not logged') return False loginURL = session.get_redirection() #replace the encoded url by url encode if (loginURL != '/users/login?from=%2farticles%2fcreate%2ffoo'): print('ERROR we\'re not redirected to the login page') return False # we post the form postData = { 'username': username, 'password': password, 'rememberMe': CHECKED, 'login': '******' } loginTreatHTML = session.transmit_post_form(post_url=LOGIN_TREAT_URL, post_data_array=postData) if (not session.test_http_status([302])): print("ERROR while trying to login") return False # now try to access to a login-only page newArticleHTML = session.transmit(NEW_ARTICLE_URL, ) if (not session.test_http_status([200])): print("ERROR we're supposed to be logged in now") return False return True
def create_article(session=Session(), lang='en', title='test', slug='test', content='#Hello', summary='created by an automated test'): SHOW_ARTICLE_URL = '/articles/show/' + slug CREATE_ARTICLE_URL = '/articles/create/' + slug CREATE_ARTICLE_TREAT_URL = '/articles/create_treat' # we try to access to an article that does not exist # => we should be redirected to the page to create an article registerHTML = session.transmit(SHOW_ARTICLE_URL) if (not session.test_http_status([302])): print("ERROR while trying to access to a non-existing article") return False if (session.get_redirection() != CREATE_ARTICLE_URL): print("ERROR we should be redirected to the page to create an article") return False # now we post a form to create an article postData = { "_1": slug, "_2": title, "_3": content, "_4": summary, "save_and_view": "blah" } session.transmit_post_form(post_url=CREATE_ARTICLE_TREAT_URL, post_data_array=postData) if (not session.test_http_status([302])): print("ERROR we should be redirected") print("status: " + str(session.status)) return False if (session.get_redirection() != SHOW_ARTICLE_URL): return False return True
#! /usr/bin/env python from httpclient import Cookie, Session from secret import USERNAME, PASSWORD import re def get_csrf(text): for line in text.split("\n"): if (re.search("csrf",line)): return line.split("\"")[5] session = Session(print_cookies = True) LOGIN_URL ='/mdswo/jsp/sys/userLogin.jsp' LOGIN_TREAT_URL ='/mdswo/jsp/sys/customerLogin.do' ORDER_NEW_TREAT_URL = '/mdswo/jsp/order/startOrder.do' SELECT_ITEM_TREAT_URL = '/mdswo/jsp/menu/select.do' ADD_TO_CART_TREAT_URL = '/mdswo/jsp/menu/addToCart.do' START_CHECKOUT_TREAT_URL = '/mdswo/jsp/order/startCheckout.do' CONFIRM_DELIVERY_TREAT_URL = '/mdswo/jsp/order/confirmOrderDelivery.do' NOTIFICATION_BY_EMAIL = 2 DELIVERY_IN_30_MIN = 0 # magic number get by looking at the HTML # for other menu you "just" need to check the HTML on their website MENU_DOUBLE_CHEESE = '1838' # replace by the number you want ;-) NBR_DOUBLE_CHEESE_MENU_I_WANT = 1 #if you want to add some remarks with your order (precise the address #this kind of stuff) REMARKS_ABOUT_THE_ORDER = ''
def general_tests(method): print "------------------- General Tests --------------------" session = Session() session.transmit('/new') cookie = session.received['sc'] if method == 'fixed' or method == 'renew': test(cookie.max_age == '5') else: test(not cookie.max_age) test(cookie.domain == 'foo.bar') test(cookie.path == '/foo') time.sleep(2) session.transmit('/update') cookie = session.received['sc'] if method == 'fixed': test(int(cookie.max_age) < 5) elif method == 'renew': test(int(cookie.max_age) == 5) test(session.transmit('/clear') == 'clear') test(session.received['sc'].max_age == '0') test(parse_info(session.transmit('/info'))['is_set_x'] == '0') session.transmit('/new_short') if method != 'browser': test(session.received['sc'].max_age == '1') session.transmit('/expose') test(session.received['sc_x'].value == '1') time.sleep(2) test(session.transmit('/is_expired') == 'expired') test(session.received['sc_x'].max_age == '0') test(session.received['sc'].max_age == '0') print "new" session.transmit('/new') print "expose" session.transmit('/expose') test(session.received['sc_x'].value == '1') print "update" session.transmit('/update') test(session.received['sc_x'].value == '2') print "clear" session.transmit('/clear') test(session.received['sc_x'].max_age == '0') session.transmit('/new') test(parse_info(session.transmit('/info'))['is_exposed_x'] == '0') session.transmit('/expose') test(session.received['sc_x'].value == '1') test(parse_info(session.transmit('/info'))['is_exposed_x'] == '1') session.transmit('/unexpose') test(session.received['sc_x'].max_age == '0') test(parse_info(session.transmit('/info'))['is_exposed_x'] == '0') test('sc' in session.state) session.transmit('/clear') test(session.received['sc'].max_age == '0') test(not 'sc_x' in session.received) session.transmit('/new') dic = parse_info(session.transmit('/info')) test(dic['is_set_x'] == '1') test(dic['age'] == '5') test_method_is(dic, method) session.transmit('/fixed') test(int(session.received['sc'].max_age) > 3) test_method_is(parse_info(session.transmit('/info')), 'fixed') time.sleep(2) test_method_is(parse_info(session.transmit('/info')), 'fixed') test(not 'sc' in session.received) session.transmit('/renew') test(session.received['sc'].max_age == '5') test_method_is(parse_info(session.transmit('/info')), 'renew') time.sleep(2) test_method_is(parse_info(session.transmit('/info')), 'renew') test(session.received['sc'].max_age == '5') session.transmit('/browser') test(not session.received['sc'].max_age) test_method_is(parse_info(session.transmit('/info')), 'browser') session.transmit('/clear') session.transmit('/new') test(parse_info(session.transmit('/info'))['age'] == '5') session.transmit('/new_short') test(parse_info(session.transmit('/info'))['age'] == '1') session.transmit('/clear') test(session.transmit('/api') == 'ok')
def general_tests(method): print "------------------- General Tests --------------------" session = Session() session.transmit("/new") cookie = session.received["sc"] if method == "fixed" or method == "renew": test(cookie.max_age == "5") else: test(not cookie.max_age) test(cookie.domain == "foo.bar") test(cookie.path == "/foo") time.sleep(2) session.transmit("/update") cookie = session.received["sc"] if method == "fixed": test(int(cookie.max_age) < 5) elif method == "renew": test(int(cookie.max_age) == 5) test(session.transmit("/clear") == "clear") test(session.received["sc"].max_age == "0") test(parse_info(session.transmit("/info"))["is_set_x"] == "0") session.transmit("/new_short") if method != "browser": test(session.received["sc"].max_age == "1") time.sleep(2) test(session.transmit("/is_expired") == "expired") print "new" session.transmit("/new") print "expose" session.transmit("/expose") test(session.received["sc_x"].value == "1") print "update" session.transmit("/update") test(session.received["sc_x"].value == "2") print "clear" session.transmit("/clear") test(session.received["sc_x"].max_age == "0") session.transmit("/new") test(parse_info(session.transmit("/info"))["is_exposed_x"] == "0") session.transmit("/expose") test(session.received["sc_x"].value == "1") test(parse_info(session.transmit("/info"))["is_exposed_x"] == "1") session.transmit("/unexpose") test(session.received["sc_x"].max_age == "0") test(parse_info(session.transmit("/info"))["is_exposed_x"] == "0") test("sc" in session.state) session.transmit("/clear") test(session.received["sc"].max_age == "0") test(not "sc_x" in session.received) session.transmit("/new") dic = parse_info(session.transmit("/info")) test(dic["is_set_x"] == "1") test(dic["age"] == "5") test_method_is(dic, method) session.transmit("/fixed") test(int(session.received["sc"].max_age) > 3) test_method_is(parse_info(session.transmit("/info")), "fixed") time.sleep(2) test_method_is(parse_info(session.transmit("/info")), "fixed") test(not "sc" in session.received) session.transmit("/renew") test(session.received["sc"].max_age == "5") test_method_is(parse_info(session.transmit("/info")), "renew") time.sleep(2) test_method_is(parse_info(session.transmit("/info")), "renew") test(session.received["sc"].max_age == "5") session.transmit("/browser") test(not session.received["sc"].max_age) test_method_is(parse_info(session.transmit("/info")), "browser") session.transmit("/clear") session.transmit("/new") test(parse_info(session.transmit("/info"))["age"] == "5") session.transmit("/new_short") test(parse_info(session.transmit("/info"))["age"] == "1") session.transmit("/clear") test(session.transmit("/api") == "ok")
"_6" : summary, "save_and_view" : "blah" } session.transmit_post_form( post_url = TRANSLATE_ARTICLE_TREAT_URL, post_data_array = postData ) if (not session.test_http_status([302])): print("ERROR we should be redirected"); print("status: " + str(session.status)) return False if (session.get_redirection().find(translateSlug) == -1): return False return True if __name__ == '__main__': error = False session = Session(print_cookies=False) login(session) if (not translate_article(session)): error = True print("ERROR while trying to translate a normal article") if (translate_article(session,slug='doesnotexist')): error = True print("ERROR we're not supposed to be able to translate a non-existing article") if (not error): print("OK")
import urllib REGISTER_URL = '/users/register-new' NEW_ARTICLE_URL ='/articles/create/foo' REGISTER_TREAT_URL ="/users/register-new_treat" USERNAME = '******' PASSWORD = '******' EMAIL = '*****@*****.**' QUIZ = '12345' CHECKED = 'y' #we need something to keep trace of redirection etc. to emulate a 'real' browser #also something to send form , see the test in cppcms session = Session(print_cookies = False) #we first try to access to a page that require login #we're supposed to be redirected to the login page registerHTML = session.transmit(REGISTER_URL) if (session.status == 404): print("ERROR this page is supposed to exist") exit() if (session.status != 200): print("ERROR we're supposed to access to it directly") #print(session.status) #print(session.received) #print(registerHTML) #loginURL = session.get_redirection()
def force_server(x): print "------------------- Force Server --------------------" session=Session() session.transmit('/new') session.transmit('/update') test(session.state['sc'].value[0]=='C') test(parse_info(session.transmit('/info'))['on_server']=='0') session.transmit('/on_server') test(session.state['sc'].value[0]=='I') test(parse_info(session.transmit('/info'))['on_server']=='1') session.transmit('/update') test(session.state['sc'].value[0]=='I') test(parse_info(session.transmit('/info'))['on_server']=='1') session.transmit('/not_on_server') test(session.state['sc'].value[0]=='C') test(parse_info(session.transmit('/info'))['on_server']=='0') session.transmit('/update') test(session.state['sc'].value[0]=='C') test(parse_info(session.transmit('/info'))['on_server']=='0')
def force_server(x): print "------------------- Force Server --------------------" session = Session() session.transmit('/new') session.transmit('/update') test(session.state['sc'].value[0] == 'C') test(parse_info(session.transmit('/info'))['on_server'] == '0') session.transmit('/on_server') test(session.state['sc'].value[0] == 'I') test(parse_info(session.transmit('/info'))['on_server'] == '1') session.transmit('/update') test(session.state['sc'].value[0] == 'I') test(parse_info(session.transmit('/info'))['on_server'] == '1') session.transmit('/not_on_server') test(session.state['sc'].value[0] == 'C') test(parse_info(session.transmit('/info'))['on_server'] == '0') session.transmit('/update') test(session.state['sc'].value[0] == 'C') test(parse_info(session.transmit('/info'))['on_server'] == '0')
def size_tests(x): print "------------------- Force Server --------------------" session=Session() session.transmit("/huge") test(session.state['sc'].value[0]=='I') session.transmit("/small") test(session.state['sc'].value[0]=='C') session=Session() session.transmit("/new") test(session.state['sc'].value[0]=='C') session.transmit("/huge") test(session.state['sc'].value[0]=='I') session.transmit("/on_server") test(session.state['sc'].value[0]=='I') session.transmit("/not_on_server") test(session.state['sc'].value[0]=='I') session.transmit("/small") test(session.state['sc'].value[0]=='C')
def size_tests(x): print "------------------- Force Server --------------------" session = Session() session.transmit("/huge") test(session.state['sc'].value[0] == 'I') session.transmit("/small") test(session.state['sc'].value[0] == 'C') session = Session() session.transmit("/new") test(session.state['sc'].value[0] == 'C') session.transmit("/huge") test(session.state['sc'].value[0] == 'I') session.transmit("/on_server") test(session.state['sc'].value[0] == 'I') session.transmit("/not_on_server") test(session.state['sc'].value[0] == 'I') session.transmit("/small") test(session.state['sc'].value[0] == 'C')
def general_tests(method): print "------------------- General Tests --------------------" session = Session() session.transmit('/new') cookie = session.received['sc'] if method=='fixed' or method=='renew': test(cookie.max_age == '5') else: test(not cookie.max_age) test(cookie.domain == 'foo.bar') test(cookie.path == '/foo') time.sleep(2) session.transmit('/update') cookie = session.received['sc'] if method=='fixed': test(int(cookie.max_age) < 5) elif method=='renew': test(int(cookie.max_age) == 5) test(session.transmit('/clear') == 'clear') test(session.received['sc'].max_age == '0') test(parse_info(session.transmit('/info'))['is_set_x']=='0') session.transmit('/new_short') if method!='browser': test(session.received['sc'].max_age == '1') session.transmit('/expose') test(session.received['sc_x'].value=='1') time.sleep(2) test(session.transmit('/is_expired') == 'expired') test(session.received['sc_x'].max_age=='0') test(session.received['sc'].max_age=='0') print "new" session.transmit('/new') print "expose" session.transmit('/expose') test(session.received['sc_x'].value=='1') print "update" session.transmit('/update') test(session.received['sc_x'].value=='2') print "clear" session.transmit('/clear') test(session.received['sc_x'].max_age=='0') session.transmit('/new') test(parse_info(session.transmit('/info'))['is_exposed_x']=='0') session.transmit('/expose') test(session.received['sc_x'].value=='1') test(parse_info(session.transmit('/info'))['is_exposed_x']=='1') session.transmit('/unexpose') test(session.received['sc_x'].max_age=='0') test(parse_info(session.transmit('/info'))['is_exposed_x']=='0') test('sc' in session.state) session.transmit('/clear') test(session.received['sc'].max_age=='0') test(not 'sc_x' in session.received) session.transmit('/new') dic = parse_info(session.transmit('/info')) test(dic['is_set_x']=='1') test(dic['age']=='5') test_method_is(dic,method) session.transmit('/fixed') test(int(session.received['sc'].max_age) > 3) test_method_is(parse_info(session.transmit('/info')),'fixed') time.sleep(2); test_method_is(parse_info(session.transmit('/info')),'fixed') test(not 'sc' in session.received) session.transmit('/renew') test(session.received['sc'].max_age=='5') test_method_is(parse_info(session.transmit('/info')),'renew') time.sleep(2); test_method_is(parse_info(session.transmit('/info')),'renew') test(session.received['sc'].max_age=='5') session.transmit('/browser') test(not session.received['sc'].max_age) test_method_is(parse_info(session.transmit('/info')),'browser') session.transmit('/clear') session.transmit('/new') test(parse_info(session.transmit('/info'))['age']=='5') session.transmit('/new_short') test(parse_info(session.transmit('/info'))['age']=='1') session.transmit('/clear') test(session.transmit('/api')=='ok')
def force_server(x): print "------------------- Force Server --------------------" session = Session() session.transmit("/new") session.transmit("/update") test(session.state["sc"].value[0] == "C") test(parse_info(session.transmit("/info"))["on_server"] == "0") session.transmit("/on_server") test(session.state["sc"].value[0] == "I") test(parse_info(session.transmit("/info"))["on_server"] == "1") session.transmit("/update") test(session.state["sc"].value[0] == "I") test(parse_info(session.transmit("/info"))["on_server"] == "1") session.transmit("/not_on_server") test(session.state["sc"].value[0] == "C") test(parse_info(session.transmit("/info"))["on_server"] == "0") session.transmit("/update") test(session.state["sc"].value[0] == "C") test(parse_info(session.transmit("/info"))["on_server"] == "0")