def check_addContent(self): myRequest = weblib.Request(environ={"PATH_INFO": "add.py"}, querystring="auth_check_flag=1", form={ "auth_name": "username", "auth_pass": "******", "title": "testSubject", "content": "content", "submit": "1" }) addPage = open(filename['addPage'], "r") eng = weblib.Engine(script=addPage, request=myRequest) eng.run() cur = dbc.cursor() cur.execute("select ID, title, content from base_content") row = cur.fetchone() assert row is not None, "addPage didn't add the content!" assert row[0] == 1, "ID should be 1, was: " + ` row[0] ` assert row[ 1] == "testSubject", "subject should be `testSubject`, was: " + ` row[ 1] ` assert row[ 2] == "content", "content should be 'content', got: " + ` row[2] `
def setUp(self): # auth requires a PATH_INFO variable.. otherwise, # it doesn't know where to redirect the form. # # @TODO: is PATH_INFO correct? I think standard might be SCRIPT_NAME # self.myReq = weblib.Request(environ={"PATH_INFO":"dummy.py"}) self.myRes = weblib.Response() self.sess = weblib.Sess(weblib.SessPool.InMemorySessPool(), self.myReq, self.myRes)
def setUp(self): # a hack to make the storage thing work. import weblib self.sess = {} weblib.sess = self.sess self.REQ = weblib.Request() self.app = ShopApp(self.REQ, zikeshop.Cart({}), clerk) self.cwd = os.getcwd() os.chdir("public") self.app.enter()
def check_login_invalid(self): """ Invalid login should show error, display form, and raise SystemExit. """ req = weblib.Request(environ = {"PATH_INFO":"sadfaf"}, querystring="auth_check_flag=1", form={"auth_username":"******", "auth_password":"******"}) sess = weblib.Sess(weblib.SessPool.InMemorySessPool(), req, self.myRes) try: auth = weblib.Auth(sess, {'username':'******'}) auth.check() gotExit = 0 except SystemExit: gotExit = 1 assert gotExit, \ "invalid login didn't get SystemExit" assert string.find(self.myRes.buffer, auth.LOGINFAILED) > -1, \ "invalid login doesn't give LOGINFAILED!"
def check_login_valid(self): """ Valid login should have no side effects. """ req = weblib.Request(environ = {"PATH_INFO":"sadfaf"}, querystring="auth_check_flag=1", form={"auth_username":"******", "auth_password":"******"}) sess = weblib.Sess(weblib.SessPool.InMemorySessPool(), req, self.myRes) try: auth = weblib.Auth(sess, {"username":"******"}) auth.check() gotExit = 0 except SystemExit: gotExit = 1 assert self.myRes.buffer == "", \ "valid login shouldn't output anything! [vs '%s']" \ % self.myRes.buffer assert not gotExit, \ "valid login still got SystemExit"
def setUp(self): self.ds = zikebase.test.dbc self.cur = zikebase.test.dbc.cursor() self.myReq = weblib.Request(environ={"PATH_INFO": "dummy.py"})
def check_checkout(self): ## THIS IS ONE *LONG* TEST............... import zikeshop from zikeshop.public.checkout import CheckoutApp import weblib weblib.request = weblib.Request() myCart = zikeshop.Cart({}) myCart.add("super-evil-destructo-ray") app = CheckoutApp({}, myCart, clerk, self.sess) # check for xxxData app.enter() assert app.billData["fname"] == "", "didn't create blank bill address" assert app.shipData["fname"] == "", "didn't create blank ship address" assert app.cardData["number"] is None, "didn't create blank card" # just call get_billing to make sure no error. # @TODO: compare this to an expected version.. RES = weblib.Response() app.do("get_billing") # try adding a bad address app.input = { "context": "bill", "fname": "michal", "lname": "wallace", "email": "INVALID_EMAIL", "address1": "123 easy street", "city": "Atlanta", "stateCD": "GA", "countryCD": "US", "postal": "76126", } app.do("add_address") assert app.model["errors"], \ "didn't get errors with invalid email: %s" \ % str(app.model["errors"]) assert app.next == "get_billing", "didn't redirect to billing page" # fix the error, try again: app.model["errors"] = [] app.input["email"] = "*****@*****.**" self.assertRaises(weblib.Redirect, app.do, "add_address") assert not app.model["errors"], \ "STILL got error: %s" % str(app.model["errors"]) assert app.next=="get_shipping", \ "didn't redirect to shipping page when no shipToBilling: %s" \ % app.next # try showing the get_shipping form: # @TODO: compare this to an expected version.. app.do("get_shipping") # ship to billing box.. app.input["shipToBilling"] = "1" self.assertRaises(weblib.Redirect, app.do, "add_address") assert app.next=="get_card", \ "didn't redirect to card page when shipToBilling: %s" \ % app.next assert app.billData == app.shipData, "didn't copy billing data" assert app.shipData["fname"]=="michal", \ "REALLY didn't copy billing data" # send that super-evil-destructo-ray to my mom instead: app.input["context"] = "ship" app.input["fname"] = "cathy" app.input["city"] = "benbrook" app.input["stateCD"] = "TX" self.assertRaises(weblib.Redirect, app.do, "add_address") assert app.shipData["fname"] == "cathy", "didn't set ship data" assert app.billData["fname"] == "michal", "overwrote bill data" assert app.next=="get_card",\ "didn't redirect to card page after shipping: %s" \ % app.next # credit card form # @TODO: compare this to an expected version.. app.do("get_card") assert app.cardData["name"]=="michal wallace", \ "didn't guess name from billing" # submit a card: fakeCard = "4111111111111119" goodCard = "4111111111111111" from zikeshop.Card import validate assert validate(fakeCard) == 0, "didn't catch fake card" assert validate(goodCard) == 1, "didn't pass good card" import time nowYear, nowMonth = time.localtime(time.time())[0:2] app.input = { "name": "Michal J Wallace", "number": fakeCard, "expMonth": str(nowMonth + 1), "expYear": str(nowYear), } #import pdb; pdb.set_trace() app.do("add_card") #@TODO: these ought not be called "add"_XXX anymore #@TODO: app.errors ought to jump automatically into model["errors"] assert app.model["errors"]==[{"error":"Invalid credit card number."}],\ "didn't catch invalid card: %s" % str(app.model["errors"]) assert app.next == "get_card", "didn't cue get_card after bad card" # expired card: app.model["errors"] = [] app.input["number"] = goodCard app.input["expYear"] = str(nowYear - 1) app.do("add_card") assert app.model["errors"]==[{"error":"Expired card."}],\ "didn't catch expired card: %s" % str(app.model["errors"]) # finally, get it all right... app.model["errors"] = [] app.input["expYear"] = nowYear + 1 self.assertRaises(weblib.Redirect, app.do, "add_card") assert app.model["errors"]==[], \ "STILL got error after everything is right" assert app.next=="confirm", \ "didn't redirect to confirmation page: %s" \ % app.next # show the confirmation page #@TODO: compare to expected app.do("confirm")
def handler(req): e = apache.build_cgi_env(req) #req.send_http_header() env = {} for bleh in e.keys(): env[bleh] = e[bleh] #print >> req, bleh, ":", e[bleh], "<br>" #return apache.OK wreq = weblib.Request(content=req.read(), environ=env) eng = ModPythonEngine(request=wreq, script="") eng.start() dir = os.sep.join(req.filename.split(os.sep)[:-1]) + os.sep os.chdir(dir) if os.path.exists(dir + ".weblib.py"): whichfile = dir + ".weblib.py" eng.execute(open(whichfile)) if (eng.result == eng.SUCCESS) or (eng.result is None): whichfile = req.filename eng.execute(open(whichfile)) eng.stop() if eng.result in (eng.SUCCESS, eng.REDIRECT, eng.EXIT): import string headers = eng.response.getHeaders() for i in string.split(headers, "\n"): if i != "": header = string.split(i, ":") req.headers_out[header[0]] = header[1] if string.lower(header[0]) == 'content-type': req.content_type = header[1] if string.lower(header[0]) == 'status': req.status = int(header[1]) req.send_http_header() req.write(eng.response.buffer) return apache.OK else: ## print debug output print >> req, weblib.trim(""" <html> <head> <title>weblib.cgi exception</title> <style type="text/css"> body, p { background: #cccccc; font-family: verdana, arial; font-size: 75%; } pre { font-size: 120%; } pre.traceback { color: red; } pre.output{ color : green } </style> </head> <body> """) if eng.result == eng.FAILURE: print >> req, "<b>assertion failure:</b>", eng.error print >> req, "</body>\n</html>" elif eng.result == eng.EXCEPTION: ## html error message ################################# print >> req, "<b>uncaught exception while running %s</b><br>" \ % whichfile print >> req, '<pre class="traceback">' \ + weblib.htmlEncode(eng.error) + "</pre>" print >> req, "<b>script input:</b>" print >> req, '<ul>' print >> req, '<li>form: %s</li>' % eng.request.form print >> req, '<li>querystring: %s</li>' % eng.request.querystring print >> req, '<li>cookie: %s</li>' % eng.request.cookie print >> req, '</ul>' print >> req, '<b>session data:</b><br>' print >> req, '<ul>' for item in eng.sess.keys(): print >> req, '<li>', item, ': ' try: print >> req, eng.sess[item] except: print >> req, '(can\'t unpickle)' print >> req, '</li>' print >> req, '</ul>' print >> req, "<b>script output:</b>" print >> req, '<pre class="output">' + \ weblib.htmlEncode(eng.response.getHeaders()) + \ weblib.htmlEncode(eng.response.buffer) + \ "</pre>" print >> req, "<hr>" print >> req, '<a href="http://weblib.sourceforge.net/">weblib</a> ' + \ '(c) copyright 2000-2001 ' + \ '<a href="http://www.zike.net/">Zike Interactive</a>. ' + \ 'All rights reserved.' print >> req, "</body>" print >> req, "</html>" return apache.OK