Ticket #1466: webtest_prelim.patch
| File webtest_prelim.patch, 70.3 KB (added by kskuhlman, 4 years ago) |
|---|
-
turbogears/controllers.py
412 412 assert isinstance(output, basestring) or isinstance(output, dict) \ 413 413 or isinstance(output, types.GeneratorType), \ 414 414 "Method %s.%s() returned unexpected output. Output should " \ 415 "be of type basestring, dict or generator ." % (416 args[0].__class__.__name__, func.__name__ )415 "be of type basestring, dict or generator, not %s." % ( 416 args[0].__class__.__name__, func.__name__, type(output)) 417 417 if isinstance(output, dict): 418 418 template = output.pop("tg_template", template) 419 419 format = output.pop("tg_format", format) -
turbogears/tests/test_form_controllers.py
34 34 self.name = name 35 35 self.age = age 36 36 self.date = date 37 return dict(name=name, age=age, date=date) 37 38 38 39 @expose() 39 40 @validate(form=myform) … … 43 44 self.name = name 44 45 self.age = age 45 46 self.date = date 47 return dict(name=name, age=age, date=date) 46 48 47 49 def test_form_translation(): 48 50 """Form input is translated into properly converted parameters""" 49 51 root = MyRoot() 50 52 cherrypy.root = root 51 testutil.create_request("/testform?name=ed&date=11/05/2005&age=5")53 response = testutil.create_request("/testform?name=ed&date=11/05/2005&age=5") 52 54 assert root.name == "ed" 53 55 assert root.age == 5 54 56 … … 56 58 """Form input is translated into properly converted parameters""" 57 59 root = MyRoot() 58 60 cherrypy.root = root 59 testutil.create_request("/testform_new_style?name=ed&date=11/05/2005&age=5&")61 response = testutil.create_request("/testform_new_style?name=ed&date=11/05/2005&age=5&") 60 62 assert root.name == "ed" 61 63 assert root.age == 5 62 64 63 65 def test_invalid_form_with_error_handling(): 64 66 """Invalid forms can be handled by the method""" 65 root = cherrypy.root 66 testutil.create_request("/testform?name=ed&age=edalso&date=11/05/2005") 67 assert root.has_errors 67 response = testutil.create_request("/testform?name=ed&age=edalso&date=11/05/2005", status=200) 68 68 69 69 def test_css_should_appear(): 70 70 """CSS should appear when asked for""" 71 testutil.create_request("/") 72 assert "calendar-system.css" in cherrypy.response.body[0] 71 root = cherrypy.root 72 response = testutil.create_request("/") 73 print response.body 74 assert "calendar-system.css" in response.body 73 75 74 76 def test_javascript_should_appear(): 75 77 """JavaScript should appear when asked for""" 76 testutil.create_request("/")77 assert "calendar.js" in cherrypy.response.body[0]78 response = testutil.create_request("/") 79 assert "calendar.js" in response.body 78 80 79 81 def test_include_mochikit(): 80 82 """JSLinks (and MochiKit especially) can be included easily""" 81 testutil.create_request("/usemochi") 82 assert "MochiKit.js" in cherrypy.response.body[0] 83 response = testutil.create_request("/usemochi") 84 print response.body 85 assert "MochiKit.js" in response.body 83 86 84 87 def test_suppress_mochikit(): 85 88 """MochiKit inclusion can be suppressed""" 86 config.update({"global": {"tg.mochikit_suppress": True}}) 87 testutil.create_request("/usemochi") 88 suppressed_body = cherrypy.response.body[0] 89 config.update({"global":{"tg.mochikit_suppress" : True}}) 90 suppressed = testutil.create_request("/usemochi") 89 91 # repair the fixture 90 config.update({"global": {"tg.mochikit_suppress": False}}) 91 testutil.create_request("/usemochi") 92 included_body = cherrypy.response.body[0] 93 assert "MochiKit.js" not in suppressed_body 94 assert "MochiKit.js" in included_body 92 config.update({"global":{"tg.mochikit_suppress" : False}}) 95 93 94 included = testutil.create_request("/usemochi") 95 assert "MochiKit.js" not in suppressed.body 96 assert "MochiKit.js" in included.body 97 96 98 def test_mochikit_everywhere(): 97 99 """MochiKit can be included everywhere by setting tg.mochikit_all""" 98 config.update({"global": {"tg.mochikit_all": True}})99 testutil.create_request("/")100 config.update({"global": {"tg.mochikit_all": False}})101 assert "MochiKit.js" in cherrypy.response.body[0]100 config.update({"global":{"tg.mochikit_all" : True}}) 101 response = testutil.create_request("/") 102 config.update({"global":{"tg.mochikit_all" : False}}) 103 assert "MochiKit.js" in response.body 102 104 103 105 def test_mochikit_nowhere(): 104 106 """Setting tg.mochikit_suppress will prevent including it everywhere""" 105 107 config.update({"global": {"tg.mochikit_all": True}}) 106 108 config.update({"global": {"tg.mochikit_suppress": True}}) 107 testutil.create_request("/")109 response = testutil.create_request("/") 108 110 config.update({"global": {"tg.mochikit_all": False}}) 109 111 config.update({"global": {"tg.mochikit_suppress": False}}) 110 assert "MochiKit.js" not in cherrypy.response.body[0]112 assert "MochiKit.js" not in response.body 111 113 112 114 def test_include_widgets(): 113 115 """Any widget can be included everywhere by setting tg.include_widgets""" 114 116 config.update({"global": {"tg.include_widgets": ["mochikit"]}}) 115 testutil.create_request("/")117 response = testutil.create_request("/") 116 118 config.update({"global": {"tg.include_widgets": []}}) 117 assert "MochiKit.js" in cherrypy.response.body[0]119 assert "MochiKit.js" in response.body 118 120 119 121 120 122 class State(object): … … 149 151 cherrypy.root = self.Controller() 150 152 # parameter values are irrelevant 151 153 url = '/validation?a=1&b=2&c.a=3&c.b=4' 152 testutil.create_request(url) 153 body = cherrypy.response.body[0] 154 response = testutil.create_request(url) 154 155 msg = "Validation state is not handled properly" 155 156 # 4 == 1 (a) + 1(b) + 1(c.a) + 1(c.b) 156 self.failUnless('counter: 4' in body, msg)157 self.failUnless('counter: 4' in response.body, msg) -
turbogears/tests/test_errorhandling.py
223 223 224 224 def test_defaultErrorHandler(self): 225 225 """ Default error handler. """ 226 testutil.create_request("/defaulterror?bar=abc")227 self.failUnless("Default error handler" in cherrypy.response.body[0])228 testutil.create_request("/defaulterror?bar=true")229 self.failUnless("Default error provider" in cherrypy.response.body[0])226 response = testutil.create_request("/defaulterror?bar=abc") 227 self.failUnless("Default error handler" in response.body) 228 response = testutil.create_request("/defaulterror?bar=true") 229 self.failUnless("Default error provider" in response.body) 230 230 231 231 def test_specialisedErrorHandler(self): 232 232 """ Error handler specialisation. """ 233 testutil.create_request("/specialisederror?bar=abc&baz=a@b.com") 234 self.failUnless("Default error handler" in cherrypy.response.body[0]) 235 testutil.create_request("/specialisederror?baz=abc&bar=1") 236 self.failUnless("Specialised error handler" in 237 cherrypy.response.body[0]) 238 testutil.create_request("/specialisederror?bar=1&baz=a@b.com") 239 self.failUnless("Specialised error provider" in 240 cherrypy.response.body[0]) 233 response = testutil.create_request("/specialisederror?bar=abc&baz=a@b.com") 234 self.failUnless("Default error handler" in response.body) 235 response = testutil.create_request("/specialisederror?baz=abc&bar=1") 236 self.failUnless("Specialised error handler" in response.body) 237 response = testutil.create_request("/specialisederror?bar=1&baz=a@b.com") 238 self.failUnless("Specialised error provider" in response.body) 241 239 242 240 def test_exceptionErrorHandler(self): 243 241 """ Error handler for exceptions. """ 244 testutil.create_request("/exceptionerror")245 self.failUnless("Default error handler" in cherrypy.response.body[0])242 response = testutil.create_request("/exceptionerror") 243 self.failUnless("Default error handler" in response.body) 246 244 247 245 def test_recursiveErrorHandler(self): 248 246 """ Recursive error handler. """ 249 testutil.create_request("/recursiveerror?bar=abc") 250 self.failUnless("Recursive error handler" in cherrypy.response.body[0]) 251 testutil.create_request("/recursiveerror?bar=1") 252 self.failUnless("Recursive error provider" in 253 cherrypy.response.body[0]) 247 response = testutil.create_request("/recursiveerror?bar=abc") 248 self.failUnless("Recursive error handler" in response.body) 249 response = testutil.create_request("/recursiveerror?bar=1") 250 self.failUnless("Recursive error provider" in response.body) 254 251 255 252 def test_implicitErrorHandler(self): 256 253 """ Implicit error handling. """ 257 testutil.create_request("/impliciterror?bar=abc") 258 self.failUnless("Implicit error handler" in 259 cherrypy.response.body[0]) 260 testutil.create_request("/impliciterror?bar=1") 261 self.failUnless("Implicit error provider" in 262 cherrypy.response.body[0]) 254 response = testutil.create_request("/impliciterror?bar=abc") 255 self.failUnless("Implicit error handler" in response.body) 256 response = testutil.create_request("/impliciterror?bar=1") 257 self.failUnless("Implicit error provider" in response.body) 263 258 264 259 def test_normalMethodErrorHandler(self): 265 260 """ Normal method as an error handler. """ 266 testutil.create_request("/normalmethodcaller?bar=abc")267 self.failUnless("Normal method" in cherrypy.response.body[0])268 testutil.create_request("/normalmethodcaller?bar=true")269 self.failUnless("Normal method caller" in cherrypy.response.body[0])261 response = testutil.create_request("/normalmethodcaller?bar=abc") 262 self.failUnless("Normal method" in response.body) 263 response = testutil.create_request("/normalmethodcaller?bar=true") 264 self.failUnless("Normal method caller" in response.body) 270 265 271 266 def test_infiniteRecursionPrevention(self): 272 267 """ Infinite recursion prevention. """ 273 testutil.create_request("/infiniteloop")274 self.failUnless("Exception 2" in cherrypy.response.body[0])268 response = testutil.create_request("/infiniteloop") 269 self.failUnless("Exception 2" in response.body) 275 270 276 271 def test_positionalArgs(self): 277 272 """ Positional argument validation. """ 278 testutil.create_request("/positionalargs/first/23/third?bar=abc")279 self.failUnless("Default error handler" in cherrypy.response.body[0])280 testutil.create_request("/positionalargs/first/abc/third?bar=false")281 self.failUnless("Default error handler" in cherrypy.response.body[0])282 testutil.create_request("/positionalargs/first/abc/third?bar=abc")283 self.failUnless("Default error handler" in cherrypy.response.body[0])284 testutil.create_request("/positionalargs/first/23/third?bar=true")285 self.failUnless("Positional arguments" in cherrypy.response.body[0])273 response = testutil.create_request("/positionalargs/first/23/third?bar=abc") 274 self.failUnless("Default error handler" in response.body) 275 response = testutil.create_request("/positionalargs/first/abc/third?bar=false") 276 self.failUnless("Default error handler" in response.body) 277 response = testutil.create_request("/positionalargs/first/abc/third?bar=abc") 278 self.failUnless("Default error handler" in response.body) 279 response = testutil.create_request("/positionalargs/first/23/third?bar=true") 280 self.failUnless("Positional arguments" in response.body) 286 281 self.failUnless(cherrypy.root.first == "first") 287 282 self.failUnless(cherrypy.root.second == 23) 288 283 self.failUnless(cherrypy.root.third == "third") 289 284 290 285 def test_missingArgs(self): 291 286 """ Arguments required in validation missing. """ 292 testutil.create_request("/missingargs")293 self.failUnless("Default error handler" in cherrypy.response.body[0])294 testutil.create_request("/missingargs?bar=12")295 self.failUnless("Missing args provider" in cherrypy.response.body[0])287 response = testutil.create_request("/missingargs") 288 self.failUnless("Default error handler" in response.body) 289 response = testutil.create_request("/missingargs?bar=12") 290 self.failUnless("Missing args provider" in response.body) 296 291 297 292 def test_nohandler(self): 298 """ No error han lder declared. """299 testutil.create_request("/nohandler")300 self.failUnless("Exception raised" in cherrypy.response.body[0])293 """ No error handler declared. """ 294 response = testutil.create_request("/nohandler") 295 self.failUnless("Exception raised" in response.body) 301 296 302 297 def test_bindArgs(self): 303 298 """ Arguments can be bond to an error handler. """ 304 testutil.create_request("/bindargs")305 self.failUnless("123" in cherrypy.response.body[0])299 response = testutil.create_request("/bindargs") 300 self.failUnless("123" in response.body) 306 301 307 302 def test_notExposed(self): 308 303 """ Validation error handling is decoupled from expose. """ 309 testutil.create_request("/notexposedcaller?foo=a&bar=rab&baz=c")310 self.failUnless("Not exposed error" in cherrypy.response.body[0])311 self.failUnless("rab" in cherrypy.response.body[0])304 response = testutil.create_request("/notexposedcaller?foo=a&bar=rab&baz=c") 305 self.failUnless("Not exposed error" in response.body) 306 self.failUnless("rab" in response.body) 312 307 313 308 def test_continuations(self): 314 309 """ Continuations via error handling mechanism. """ 315 testutil.create_request("/continuationcaller?bar=a")316 self.failUnless("Continuation caller" in cherrypy.response.body[0])310 response = testutil.create_request("/continuationcaller?bar=a") 311 self.failUnless("Continuation caller" in response.body) 317 312 self.failUnless(cherrypy.root.continuation == True) 318 313 319 314 def test_nested(self): 320 315 """ Potentially ambiguous cases. """ 321 testutil.create_request("/nest?bar=a")322 self.failUnless("Default error handler" in cherrypy.response.body[0])323 testutil.create_request("/nestedcontroller/nest?bar=a")324 self.failUnless("Nested" in cherrypy.response.body[0])316 response = testutil.create_request("/nest?bar=a") 317 self.failUnless("Default error handler" in response.body) 318 response = testutil.create_request("/nestedcontroller/nest?bar=a") 319 self.failUnless("Nested" in response.body) 325 320 326 321 def test_failsafe(self): 327 322 """ Failsafe values for erroneous input. """ 328 testutil.create_request("/failsafenone?bar=a&baz=b")329 self.failUnless('"bar": "a"' in cherrypy.response.body[0])330 self.failUnless('"baz": "b"' in cherrypy.response.body[0])331 testutil.create_request("/failsafevaluesdict?bar=a&baz=b")332 self.failUnless('"bar": 1' in cherrypy.response.body[0])333 self.failUnless('"baz": 2' in cherrypy.response.body[0])334 testutil.create_request("/failsafevaluesatom?bar=a&baz=b")335 self.failUnless('"bar": 13' in cherrypy.response.body[0])336 self.failUnless('"baz": 13' in cherrypy.response.body[0])337 testutil.create_request("/failsafemaperrors?bar=a&baz=b")323 response = testutil.create_request("/failsafenone?bar=a&baz=b") 324 self.failUnless('"bar": "a"' in response.body) 325 self.failUnless('"baz": "b"' in response.body) 326 response = testutil.create_request("/failsafevaluesdict?bar=a&baz=b") 327 self.failUnless('"bar": 1' in response.body) 328 self.failUnless('"baz": 2' in response.body) 329 response = testutil.create_request("/failsafevaluesatom?bar=a&baz=b") 330 self.failUnless('"bar": 13' in response.body) 331 self.failUnless('"baz": 13' in response.body) 332 response = testutil.create_request("/failsafemaperrors?bar=a&baz=b") 338 333 self.failUnless('"bar": "Please enter an integer value"' in 339 cherrypy.response.body[0])340 self.failUnless('"baz": "Please enter an integer value"' in 341 cherrypy.response.body[0])342 testutil.create_request("/failsafeformencode?bar=a&baz=b")343 self.failUnless('"bar": 1' in cherrypy.response.body[0])344 self.failUnless('"baz": 2' in cherrypy.response.body[0])345 testutil.create_request("/failsafedefaults?bar=a&baz=b")346 self.failUnless('"bar": 1' in cherrypy.response.body[0],cherrypy.response.body[0])347 self.failUnless('"baz": 2' in cherrypy.response.body[0])334 response.body) 335 self.failUnless('"baz": "Please enter an integer value"' in 336 response.body) 337 response = testutil.create_request("/failsafeformencode?bar=a&baz=b") 338 self.failUnless('"bar": 1' in response.body) 339 self.failUnless('"baz": 2' in response.body) 340 response = testutil.create_request("/failsafedefaults?bar=a&baz=b") 341 self.failUnless('"bar": 1' in response.body,response.body) 342 self.failUnless('"baz": 2' in response.body) -
turbogears/tests/test_sqlalchemy.py
135 135 will be marked with this name :) 136 136 137 137 """ 138 cherrypy.response.code = 501139 138 msg = "KARL25 responding\n" 140 139 msg += "user with id: '%s' should not be saved.\n" % id 141 140 msg += "An exception occurred: %r (%s)" % ((tg_exceptions,)*2) … … 181 180 """If a controller causes an SA exception, it is raised properly.""" 182 181 capture_log("turbogears.database") 183 182 cherrypy.root = MyRoot() 184 create_request("/create_person?id=20") 185 output = cherrypy.response.body[0] 183 response = create_request("/create_person?id=20") 186 184 print_log() 185 assert 'No exceptions occurred' in response.body 186 response = create_request("/create_person?id=20", status=500) 187 output = response.body 187 188 print output 188 assert 'No exceptions occurred' in output189 create_request("/create_person?id=20")190 output = cherrypy.response.body[0]191 print output192 189 assert 'KARL25' not in output, \ 193 190 'Exception should NOT have been handled by our handler' 194 191 assert 'DBAPIError' in output, \ … … 218 215 def test_cntrl_commit(): 219 216 """It's safe to commit a transaction in the controller.""" 220 217 cherrypy.root = MyRoot() 221 create_request("/create_person?id=23&docom=1")222 assert 'InvalidRequestError' not in cherrypy.response.body[0]218 response = create_request("/create_person?id=23&docom=1") 219 assert 'InvalidRequestError' not in response.body 223 220 assert session.query(Person).get(23) is not None, \ 224 221 'The Person 23 should have been saved during commit inside controller' 225 222 226 223 def test_cntrl_commit2(): 227 224 """A commit inside the controller is not rolled back by the exception.""" 228 225 cherrypy.root = MyRoot() 229 create_request("/create_person?id=24&docom=1&doerr=1")230 assert 'InvalidRequestError' not in cherrypy.response.body[0]226 response = create_request("/create_person?id=24&docom=1&doerr=1") 227 assert 'InvalidRequestError' not in response.body 231 228 assert session.query(Person).get(24) is not None, \ 232 229 'The Person 24 should have been saved during commit' \ 233 230 ' inside controller and not rolled back' … … 235 232 def test_cntrl_flush(): 236 233 """It's safe to flush in the controller.""" 237 234 cherrypy.root = MyRoot() 238 create_request("/create_person?id=25&doflush=1")239 print cherrypy.response.body[0]240 assert 'No exceptions occurred' in cherrypy.response.body[0]241 create_request("/create_person?id=25&doflush=0")242 assert 'IntegrityError' in cherrypy.response.body[0]243 create_request("/create_person?id=25&doflush=1")244 assert 'IntegrityError' in cherrypy.response.body[0]245 create_request("/create_person?id=25&doflush=2")246 assert 'No exceptions occurred' in cherrypy.response.body[0]235 response = create_request("/create_person?id=25&doflush=1") 236 print response.body 237 assert 'No exceptions occurred' in response.body 238 response = create_request("/create_person?id=25&doflush=0", status=500) 239 assert 'IntegrityError' in response.body 240 response = create_request("/create_person?id=25&doflush=1") 241 assert 'IntegrityError' in response.body 242 response = create_request("/create_person?id=25&doflush=2") 243 assert 'No exceptions occurred' in response.body 247 244 248 245 249 246 # Exception handling with rollback … … 297 294 298 295 """ 299 296 cherrypy.root = RbRoot() 300 create_request('/doerr?id=26')301 output = cherrypy.response.body[0]297 response = create_request('/doerr?id=26') 298 output = response.body 302 299 print output 303 300 assert 'KARL27' in output, 'Exception handler should have answered' 304 301 assert session.query(User).get(26) is None … … 312 309 313 310 """ 314 311 cherrypy.root = RbRoot() 315 create_request('/doerr?id=XX')316 output = cherrypy.response.body[0]312 response = create_request('/doerr?id=XX') 313 output = response.body 317 314 assert 'KARL27' in output, 'Exception handler should have answered' 318 315 assert session.query(User).get('XX') is None 319 316 320 317 def test_exc_done_rollback(): 321 318 """No problems with error handler if controller manually rollbacks.""" 322 319 cherrypy.root = RbRoot() 323 create_request('/doerr?id=28&dorb=1')324 output = cherrypy.response.body[0]320 response = create_request('/doerr?id=28&dorb=1') 321 output = response.body 325 322 assert 'KARL27' in output, 'Exception handler should have answered' 326 323 assert session.query(User).get(28) is None 327 324 assert session.query(User).get(29) is not None … … 356 353 """ 357 354 test_table.insert().execute(dict(id=1, val='a')) 358 355 cherrypy.root = FreshRoot() 359 create_request("/test1")360 assert 'AssertionError' not in cherrypy.response.body[0]356 response = create_request("/test1", status=500) 357 assert 'AssertionError' not in response.body 361 358 # Call test2 in a different thread 362 359 class ThreadB(threading.Thread): 363 360 def run(self): 364 create_request("/test2")365 assert 'AssertionError' not in cherrypy.response.body[0]361 response = create_request("/test2", status=500) 362 assert 'AssertionError' not in response.body 366 363 thrdb = ThreadB() 367 364 thrdb.start() 368 365 thrdb.join() 369 create_request("/test3")370 assert 'AssertionError' not in cherrypy.response.body[0]366 response = create_request("/test3", status=500) 367 assert 'AssertionError' not in response.body -
turbogears/tests/test_expose.py
21 21 22 22 def test_gettinghtml(): 23 23 cherrypy.root = ExposeRoot() 24 create_request("/with_json") 25 body = cherrypy.response.body[0] 26 assert "Paging all foo" in body 24 response = create_request("/with_json") 25 assert "Paging all foo" in response.body 27 26 28 27 def test_gettingjson(): 29 28 cherrypy.root = ExposeRoot() 30 create_request("/with_json?tg_format=json") 31 body = cherrypy.response.body[0] 32 assert '"title": "Foobar"' in body 29 response = create_request("/with_json?tg_format=json") 30 assert '"title": "Foobar"' in response.body 33 31 34 32 def test_gettingjsonviaaccept(): 35 33 cherrypy.root = ExposeRoot() 36 create_request("/with_json_via_accept",34 response = create_request("/with_json_via_accept", 37 35 headers=dict(Accept="text/javascript")) 38 body = cherrypy.response.body[0] 39 assert '"title": "Foobar"' in body 36 assert '"title": "Foobar"' in response.body 40 37 41 38 def test_getting_json_with_accept_but_using_tg_format(): 42 39 cherrypy.root = ExposeRoot() 43 create_request("/with_json_via_accept?tg_format=json") 44 body = cherrypy.response.body[0] 45 assert '"title": "Foobar"' in body 40 response = create_request("/with_json_via_accept?tg_format=json") 41 assert '"title": "Foobar"' in response.body 46 42 47 43 def test_getting_plaintext(): 48 44 cherrypy.root = ExposeRoot() 49 create_request("/with_json_via_accept",45 response = create_request("/with_json_via_accept", 50 46 headers=dict(Accept="text/plain")) 51 print cherrypy.response.body[0] 52 assert cherrypy.response.body[0] == "This is a plain text for foo." 47 assert response.body == "This is a plain text for foo." 53 48 54 49 def test_allow_json(): 55 50 … … 59 54 return dict(title="Foobar", mybool=False, someval="niggles") 60 55 61 56 cherrypy.root = NewRoot() 62 create_request("/test", headers= dict(accept="text/javascript")) 63 body = cherrypy.response.body[0] 64 values = simplejson.loads(body) 57 response = create_request("/test", headers= dict(accept="text/javascript")) 58 values = simplejson.loads(response.body) 65 59 assert values == dict(title="Foobar", mybool=False, someval="niggles", 66 60 tg_flash=None) 67 assert cherrypy.response.headers["Content-Type"] == "text/javascript" 68 create_request("/test?tg_format=json") 69 body = cherrypy.response.body[0] 70 values = simplejson.loads(body) 61 assert response.headers["Content-Type"] == "text/javascript" 62 response = create_request("/test?tg_format=json") 63 values = simplejson.loads(response.body) 71 64 assert values == dict(title="Foobar", mybool=False, someval="niggles", 72 65 tg_flash=None) 73 assert cherrypy.response.headers["Content-Type"] == "text/javascript"66 assert response.headers["Content-Type"] == "text/javascript" -
turbogears/tests/test_controllers.py
1 1 import unittest 2 import turbogears 3 from turbogears import config, controllers, database, \ 4 error_handler, exception_handler, expose, flash, redirect, \ 5 startup, testutil, url, validate, validators 6 import simplejson 2 7 import formencode 3 8 import cherrypy 4 9 import pkg_resources 5 from turbogears import config, controllers, database, \ 6 error_handler, exception_handler, expose, flash, redirect, \ 7 startup, testutil, url, validate, validators 10 from nose.tools import eq_ 8 11 9 12 10 13 class SubApp(controllers.RootController): 11 14 15 @expose() 12 16 def index(self): 13 17 return url("/Foo/") 14 index = expose()(index)15 18 19 @expose() 20 def foo(self): 21 return url("foo") 16 22 23 @expose() 24 def foo2(self): 25 return url("/foo") 26 27 @expose() 28 def redir(self): 29 turbogears.redirect("foo") 30 31 @expose() 32 def redir2(self): 33 raise turbogears.redirect("foo") 34 35 17 36 class MyRoot(controllers.RootController): 18 37 19 38 @expose() 20 39 def index(self): 21 pass40 return {} 22 41 23 42 def validation_error_handler(self, tg_source, tg_errors, *args, **kw): 24 43 self.functionname = tg_source.__name__ … … 215 234 216 235 def test_js_files(self): 217 236 """Can access the JavaScript files""" 218 testutil.create_request("/tg_js/MochiKit.js") 219 assert cherrypy.response.headers[ 220 "Content-Type"] == "application/x-javascript" 221 assert cherrypy.response.status == "200 OK" 237 response = testutil.create_request("/tg_js/MochiKit.js", status=200) 238 assert response.headers["Content-Type"] == "application/x-javascript" 222 239 223 240 def test_json_output(self): 224 testutil.create_request("/test?tg_format=json")225 import simplejson226 values = simplejson.loads(cherrypy.response.body[0])227 assert values == dict(title="Foobar", mybool=False,228 someval="niggles", tg_flash=None)229 assert cherrypy.response.headers["Content-Type"] == "text/javascript"241 response = testutil.create_request("/test?tg_format=json") 242 values = simplejson.loads(response.body) 243 expected = dict(title="Foobar", mybool=False, someval="niggles", 244 tg_flash=None) 245 eq_(values, expected) 246 assert response.headers["Content-Type"] == "text/javascript" 230 247 231 248 def test_implied_json(self): 232 testutil.create_request("/impliedjson?tg_format=json")233 assert '"title": "Blah"' in cherrypy.response.body[0]249 response = testutil.create_request("/impliedjson?tg_format=json") 250 assert '"title": "Blah"' in response.body 234 251 235 252 def test_allow_json(self): 236 testutil.create_request("/allowjson?tg_format=json")237 assert cherrypy.response.headers["Content-Type"] == "text/html"253 response = testutil.create_request("/allowjson?tg_format=json", status=500) 254 assert response.headers["Content-Type"] == "text/html", response.headers 238 255 239 256 def test_allow_json_config(self): 240 257 """JSON output can be enabled via config.""" … … 245 262 return dict(title="Foobar", mybool=False, someval="foo", 246 263 tg_html="turbogears.tests.simple") 247 264 cherrypy.root = JSONRoot() 248 testutil.create_request('/allowjsonconfig?tg_format=json')249 assert cherrypy.response.headers["Content-Type"] =="text/javascript"250 config.update({'tg.allow_json': False})265 response = testutil.create_request('/allowjsonconfig?tg_format=json') 266 assert response.headers["Content-Type"]=="text/javascript" 267 config.update({'tg.allow_json':False}) 251 268 252 269 def test_allow_json_config_false(self): 253 270 """Make sure JSON can still be restricted with a global config on.""" … … 258 275 return dict(title="Foobar", mybool=False, someval="foo", 259 276 tg_html="turbogears.tests.simple") 260 277 cherrypy.root = JSONRoot() 261 testutil.create_request('/allowjson?tg_format=json')262 assert cherrypy.response.headers["Content-Type"] =="text/html"278 response = testutil.create_request('/allowjson?tg_format=json', status=404) 279 assert response.headers["Content-Type"]=="text/html" 263 280 config.update({'tg.allow_json': False}) 264 281 265 282 def test_invalid_return(self): 266 testutil.create_request("/invalid") 267 assert cherrypy.response.status.startswith("500") 283 response = testutil.create_request("/invalid", status=500) 268 284 269 285 def test_strict_parameters(self): 270 config.update({"tg.strict_parameters": True}) 271 testutil.create_request( 272 "/save?submit=save&firstname=Foo&lastname=Bar&badparam=1") 273 assert cherrypy.response.status.startswith("500") 274 assert not hasattr(cherrypy.root, "errors") 286 config.update({"tg.strict_parameters" : True}) 287 response = testutil.create_request( 288 "/save?submit=save&firstname=Foo&lastname=Bar&badparam=1", 289 status = 500) 275 290 276 291 def test_throw_out_random(self): 277 292 """Can append random value to the URL to avoid caching problems.""" 278 testutil.create_request("/test?tg_random=1")279 assert "Paging all niggles" in cherrypy.response.body[0]293 response = testutil.create_request("/test?tg_random=1") 294 assert "Paging all niggles" in response.body 280 295 config.update({"tg.strict_parameters": True}) 281 testutil.create_request("/test?tg_random=1") 282 assert cherrypy.response.status.startswith("200") 283 assert "Paging all niggles" in cherrypy.response.body[0] 284 testutil.create_request("/test?tg_not_random=1") 285 assert cherrypy.response.status.startswith("500") 286 assert not hasattr(cherrypy.root, "errors") 296 response = testutil.create_request("/test?tg_random=1", status=200) 297 assert "Paging all niggles" in response.body 298 response = testutil.create_request("/test?tg_not_random=1", status=500) 287 299 288 300 def test_ignore_parameters(self): 289 301 config.update({"tg.strict_parameters": True}) 290 testutil.create_request("/test?ignore_me=1") 291 assert cherrypy.response.status.startswith("500") 302 response = testutil.create_request("/test?ignore_me=1", status=500) 292 303 assert not hasattr(cherrypy.root, "errors") 293 304 config.update({"tg.ignore_parameters": ['ignore_me', 'me_too']}) 294 testutil.create_request("/test?ignore_me=1") 295 assert "Paging all niggles" in cherrypy.response.body[0] 296 testutil.create_request("/test?me_too=1") 297 assert cherrypy.response.status.startswith("200") 298 assert "Paging all niggles" in cherrypy.response.body[0] 299 testutil.create_request("/test?me_not=1") 300 assert cherrypy.response.status.startswith("500") 305 response = testutil.create_request("/test?ignore_me=1") 306 assert "Paging all niggles" in response.body 307 response = testutil.create_request("/test?me_too=1", status=200) 308 assert "Paging all niggles" in response.body 309 testutil.create_request("/test?me_not=1", status=500) 301 310 assert not hasattr(cherrypy.root, "errors") 302 311 303 312 def test_retrieve_dict_directly(self): … … 305 314 assert d["title"] == "Foobar" 306 315 307 316 def test_templateOutput(self): 308 testutil.create_request("/test") 309 assert "Paging all niggles" in cherrypy.response.body[0] 317 response = testutil.create_request("/test") 318 print response.body 319 assert "Paging all niggles" in response.body 310 320 321 def test_throw_out_random(self): 322 """A random value can be appended to the URL to avoid caching 323 problems.""" 324 response = testutil.create_request("/test?tg_random=1") 325 assert "Paging all niggles" in response.body 326 311 327 def test_safari_unicode_fix(self): 312 testutil.create_request("/unicode", headers={'User-Agent':313 "Apple WebKit Safari/412.2"} )314 firstline = cherrypy.response.body[0].split('\n')[0]328 response = testutil.create_request("/unicode", headers={'User-Agent' : 329 "Apple WebKit Safari/412.2"}, status=200) 330 firstline = response.body.split('\n')[0] 315 331 assert firstline == "¿Habla español?" 316 332 assert isinstance(firstline, str) 317 333 318 334 def test_default_format(self): 319 335 """The default format can be set via expose""" 320 testutil.create_request("/returnjson") 321 firstline = cherrypy.response.body[0] 322 assert '"title": "Foobar"' in firstline 323 testutil.create_request("/returnjson?tg_format=html") 324 firstline = cherrypy.response.body[0] 325 assert '"title": "Foobar"' not in firstline 336 response = testutil.create_request("/returnjson") 337 assert '"title": "Foobar"' in response.body 338 response = testutil.create_request("/returnjson?tg_format=html", status=500) 339 assert '"title": "Foobar"' not in response.body 326 340 327 341 def test_content_type(self): 328 342 """The content-type can be set via expose""" 329 testutil.create_request("/contenttype")330 assert cherrypy.response.headers["Content-Type"] == "xml/atom"343 response = testutil.create_request("/contenttype") 344 assert response.headers["Content-Type"] == "xml/atom" 331 345 332 346 def test_returned_template_name(self): 333 testutil.create_request("/returnedtemplate")334 data = cherrypy.response.body[0].lower()347 response = testutil.create_request("/returnedtemplate") 348 data = response.body.lower() 335 349 assert "<body>" in data 336 350 assert 'groovy test template' in data 337 351 338 352 def test_returned_template_short(self): 339 testutil.create_request("/returnedtemplate_short")340 assert "Paging all foo" in cherrypy.response.body[0]353 response = testutil.create_request("/returnedtemplate_short") 354 assert "Paging all foo" in response.body 341 355 342 356 def test_expose_template_short(self): 343 testutil.create_request("/exposetemplate_short")344 assert "Paging all foo" in cherrypy.response.body[0]357 response = testutil.create_request("/exposetemplate_short") 358 assert "Paging all foo" in response.body 345 359 346 360 def test_validation(self): 347 361 """Data can be converted and validated""" 348 testutil.create_request("/istrue?value=true") 362 #TODO: Convert these to testutil.call, to make them less dependant on cp internals 363 response = testutil.create_request("/istrue?value=true") 349 364 assert cherrypy.root.value is True 350 365 testutil.create_request("/istrue?value=false") 351 366 assert cherrypy.root.value is False 352 367 cherrypy.root = MyRoot() 353 testutil.create_request("/istrue?value=foo")368 response = testutil.create_request("/istrue?value=foo") 354 369 assert not hasattr(cherrypy.root, "value") 355 370 assert cherrypy.root.functionname == "istrue" 356 testutil.create_request("/save?submit=send&firstname=John&lastname=Doe") 371 372 response = testutil.create_request("/save?submit=send&firstname=John&lastname=Doe") 357 373 assert cherrypy.root.fullname == "John Doe" 358 374 assert cherrypy.root.submit == "send" 359 375 testutil.create_request("/save?submit=send&firstname=Arthur") … … 369 385 assert cherrypy.root.errors.has_key("firstname") 370 386 371 387 def test_validation_with_schema(self): 372 """Data can be converted /validated with formencode.Schema instance"""388 """Data can be converted and validated with formencode.Schema instance""" 373 389 testutil.create_request("/save2?submit=send&firstname=Joe&lastname=Doe") 374 390 assert cherrypy.root.fullname == "Joe Doe" 375 391 assert cherrypy.root.submit == "send" … … 384 400 385 401 def test_other_template(self): 386 402 """'tg_html' in a returned dict will use the template specified there""" 387 testutil.create_request("/useother")388 assert "This is the other template" in cherrypy.response.body[0]403 response = testutil.create_request("/useother") 404 assert "This is the other template" in response.body, response.body 389 405 390 406 def test_cheetah_template(self): 391 407 """Cheetah templates can be used as well""" 392 testutil.create_request("/usecheetah") 393 body = cherrypy.response.body[0] 394 assert "This is the Cheetah test template." in body 395 assert "Paging all chimps." in body 408 response = testutil.create_request("/usecheetah") 409 assert "This is the Cheetah test template." in response.body 410 assert "Paging all chimps." in response.body 396 411 397 412 def test_run_with_trans(self): 398 413 """run_with_transaction is called only on topmost exposed method""" … … 405 420 406 421 def test_positional(self): 407 422 """Positional parameters should work""" 408 testutil.create_request("/pos/foo")423 response = testutil.create_request("/pos/foo") 409 424 assert cherrypy.root.posvalue == "foo" 410 425 411 426 def test_flash_plain(self): 412 427 """flash with strings should work""" 413 testutil.create_request("/flash_plain?tg_format=json") 414 import simplejson 415 values = simplejson.loads(cherrypy.response.body[0]) 428 response = testutil.create_request("/flash_plain?tg_format=json") 429 values = simplejson.loads(response.body) 416 430 assert values["tg_flash"] == "plain" 417 assert not cherrypy.response.simple_cookie.has_key("tg_flash")431 assert not response.cookies_set.has_key("tg_flash"), response.cookies_set 418 432 419 433 def test_flash_unicode(self): 420 434 """flash with unicode objects should work""" 421 testutil.create_request("/flash_unicode?tg_format=json") 422 import simplejson 423 values = simplejson.loads(cherrypy.response.body[0]) 435 response = testutil.create_request("/flash_unicode?tg_format=json") 436 values = simplejson.loads(response.body) 424 437 assert values["tg_flash"] == u"\xfcnicode" 425 assert not cherrypy.response.simple_cookie.has_key("tg_flash")438 assert not response.cookies_set.has_key("tg_flash"), response.cookies_set 426 439 427 440 def test_flash_on_redirect(self): 428 441 """flash must survive a redirect""" 429 testutil.create_request("/flash_redirect?tg_format=json") 430 assert cherrypy.response.status.startswith("302") 431 testutil.create_request(cherrypy.response.headers["Location"], 432 headers=dict(Cookie 433 =cherrypy.response.simple_cookie.output(header="").strip())) 434 import simplejson 435 values = simplejson.loads(cherrypy.response.body[0]) 442 response = testutil.create_request("/flash_redirect?tg_format=json", 443 status = 302) 444 response = testutil.create_request(response.headers["Location"], 445 headers=dict(Cookie=response.cookies_set.output())) 446 print response.body 447 values = simplejson.loads(response.body) 436 448 assert values["tg_flash"] == u"redirect \xfcnicode" 437 449 438 450 def test_double_flash(self): … … 440 452 # Here we are calling method that sets a flash message. However flash 441 453 # cookie is still there. Turbogears should discard old flash message 442 454 # from cookie and use new one, set by flash_plain(). 443 testutil.create_request("/flash_plain?tg_format=json", 444 headers=dict(Cookie='tg_flash="old flash"; Path=/;')) 445 import simplejson 446 values = simplejson.loads(cherrypy.response.body[0]) 455 response = testutil.create_request("/flash_plain?tg_format=json", 456 headers= {'Cookie':'tg_flash="old flash"; Path=/;'}) 457 values = simplejson.loads(response.body) 447 458 assert values["tg_flash"] == "plain" 448 assert cherrypy.response.simple_cookie.has_key("tg_flash"), \459 assert response.cookies_set.has_key("tg_flash"), \ 449 460 "Cookie clearing request should be present" 450 flashcookie = cherrypy.response.simple_cookie['tg_flash']461 flashcookie = response.cookies_set['tg_flash'] 451 462 assert flashcookie['expires'] == 0 452 463 453 464 def test_set_kid_outputformat_in_config(self): 454 465 """the outputformat for kid can be set in the config""" 455 466 config.update({'kid.outputformat': 'xhtml'}) 456 testutil.create_request('/test') 457 response = cherrypy.response.body[0] 458 assert '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ' in response 467 response = testutil.create_request('/test') 468 assert '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ' in response.body 459 469 config.update({'kid.outputformat': 'html'}) 460 testutil.create_request('/test') 461 response = cherrypy.response.body[0] 462 assert '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML ' in response 463 assert ' This is the groovy test ' in response 470 response = testutil.create_request('/test') 471 assert '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML ' in response.body 472 assert ' This is the groovy test ' in response.body 464 473 config.update({'kid.outputformat': 'html compact'}) 465 testutil.create_request('/test') 466 response = cherrypy.response.body[0] 467 assert '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML ' in response 468 assert 'This is the groovy test ' in response 469 assert ' ' not in response 474 response = testutil.create_request('/test') 475 assert '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML ' in response.body 476 assert 'This is the groovy test ' in response.body 477 assert ' ' not in response.body 470 478 471 479 def test_fileserving(self): 472 #outputcap = StringIO()473 #sys.stdout = outputcap474 480 testutil.create_request("/servefile") 475 481 assert cherrypy.root.servedit 476 482 assert not cherrypy.root.serve_exceptions … … 478 484 479 485 def test_internal_redirect(self): 480 486 """regression test for #1022, #1407 and #1598""" 481 testutil.create_request("/internal_redirect") 482 firstline = cherrypy.response.body[0] 483 assert "redirected OK" in firstline 487 response = testutil.create_request("/internal_redirect") 488 assert "redirected OK" in response.body 484 489 485 490 def test_internal_redirect_nested_variables(self): 486 491 """regression test for #1022, #1407 and #1598""" 487 testutil.create_request(492 response = testutil.create_request( 488 493 "/internal_redirect?a=1&a-1.b=2&a-2.c=3&a-2.c-1=4") 489 firstline = cherrypy.response.body[0] 490 assert "redirected OK" in firstline 494 assert "redirected OK" in response.body 491 495 492 496 def test_exc_value(self): 493 497 """Exception is handled gracefully by the right exception handler.""" 494 testutil.create_request("/raise_value_exc") 495 assert 'handling_value' in cherrypy.response.body[0] 498 response = testutil.create_request("/raise_value_exc") 499 print response.body 500 assert 'handling_value' in response.body 496 501 497 502 def test_exc_index(self): 498 503 """Exception is handled gracefully by the right exception handler.""" 499 testutil.create_request("/raise_index_exc")500 assert 'handling_index' in cherrypy.response.body[0]504 response = testutil.create_request("/raise_index_exc") 505 assert 'handling_index' in response.body, response.body 501 506 502 507 def test_exc_all(self): 503 508 """Test a controller that is protected by multiple exception handlers. … … 506 511 by their respective handlers without problem... 507 512 508 513 """ 509 testutil.create_request("/raise_all_exc?num=1")510 assert 'handling_value' in cherrypy.response.body[0]511 testutil.create_request("/raise_all_exc?num=2")512 assert 'handling_index' in cherrypy.response.body[0]513 testutil.create_request("/raise_all_exc?num=3")514 assert 'handling_key' in cherrypy.response.body[0]514 response = testutil.create_request("/raise_all_exc?num=1") 515 assert 'handling_value' in response.body, response.body 516 response = testutil.create_request("/raise_all_exc?num=2") 517 assert 'handling_index' in response.body, response.body 518 response = testutil.create_request("/raise_all_exc?num=3") 519 assert 'handling_key' in response.body, response.body 515 520 516 521 517 522 class TestURLs(unittest.TestCase): … … 521 526 cherrypy.root = MyRoot() 522 527 cherrypy.root.subthing = SubApp() 523 528 cherrypy.root.subthing.subsubthing = SubApp() 529 530 if testutil.harness == 'webob': 531 self.location_base = 'http://localhost:80' 532 else: 533 self.location_base = 'http://localhost' 524 534 525 535 def test_basic_urls(self): 526 536 testutil.create_request("/") … … 537 547 assert url("/foo") == "/foo" 538 548 539 549 def test_approots(self): 540 testutil.create_request("/subthing/") 541 assert url("foo") == "foo" 542 assert url("/foo") == "/subthing/foo" 550 response = testutil.create_request("/subthing/foo") 551 self.failUnlessEqual("foo", response.body) 552 553 response = testutil.create_request("/subthing/foo2") 554 self.failUnlessEqual("/subthing/foo", response.body) 543 555 544 556 def test_lower_approots(self): 545 testutil.create_request("/subthing/subsubthing/") 546 assert url("/foo") == "/subthing/subsubthing/foo" 557 config.update({"server.webpath" : "/XXX"}) 558 response = testutil.create_request("/subthing/subsubthing/") 559 eq_("/XXX/subthing/subsubthing/Foo/", response.body) 547 560 548 def test_approots_ With_path(self):549 config.update({"server.webpath" : "/coolsite/root"})561 def test_approots_with_path(self): 562 config.update({"server.webpath" : "/coolsite/root"}) 550 563 startup.startTurboGears() 551 testutil.create_request("/coolsite/root/subthing/")552 assert url("/foo") == "/coolsite/root/subthing/foo"564 response = testutil.create_request("/coolsite/root/subthing/") 565 self.failUnlessEqual("/coolsite/root/subthing/Foo/", response.body) 553 566 554 567 def test_redirect(self): 555 config.update({"server.webpath" : "/coolsite/root"})568 config.update({"server.webpath" : "/coolsite/root"}) 556 569 startup.startTurboGears() 557 testutil.create_request("/coolsite/root/subthing/") 558 try: 559 redirect("/foo") 560 assert False, "redirect exception should have been raised" 561 except cherrypy.HTTPRedirect, e: 562 assert "http://localhost/coolsite/root/subthing/foo" in e.urls 563 try: 564 raise redirect("/foo") 565 assert False, "redirect exception should have been raised" 566 except cherrypy.HTTPRedirect, e: 567 assert "http://localhost/coolsite/root/subthing/foo" in e.urls 570 response = testutil.create_request("/coolsite/root/subthing/redir") 571 self.failUnlessEqual(response.location, self.location_base + '/coolsite/root/subthing/foo') 568 572 573 response = testutil.create_request("/coolsite/root/subthing/redir2") 574 self.failUnlessEqual(response.location, self.location_base + '/coolsite/root/subthing/foo') 575 569 576 def test_multi_values(self): 570 577 testutil.create_request("/") 571 578 assert url("/foo", bar=[1, 2]) in \ … … 593 600 config.update({"server.webpath": ""}) 594 601 startup.startTurboGears() 595 602 596 597 603 def test_index_trailing_slash(): 598 604 """If there is no trailing slash on an index method call, redirect""" 599 605 cherrypy.root = SubApp() 600 606 cherrypy.root.foo = SubApp() 601 testutil.create_request("/foo") 602 assert cherrypy.response.status.startswith("302") 607 response = testutil.create_request("/foo") 608 print response.status 609 assert response.status.startswith("302") 603 610 611 604 612 def test_can_use_internally_defined_arguments(): 605 613 """Can use argument names that are internally used by TG in controllers""" 606 614 … … 611 619 return "\n".join(["%s:%s" % i for i in kw.iteritems()]) 612 620 613 621 cherrypy.root = App() 614 testutil.create_request("/?format=foo&template=bar&fragment=boo")615 output = cherrypy.response.body[0]622 response = testutil.create_request("/?format=foo&template=bar&fragment=boo") 623 output = response.body 616 624 assert "format:foo" in output 617 625 assert "template:bar" in output 618 626 assert "fragment:boo" in output -
turbogears/tests/test_view.py
1 1 # -*- coding: utf-8 -*- 2 3 from turbogears import view, config 2 import cherrypy 3 from turbogears import view, config, startup, controllers, expose, testutil 4 4 import unittest 5 5 6 class MyRoot(controllers.RootController): 7 def index(self): 8 return {} 9 index = expose()(index) 10 6 11 class TestView(unittest.TestCase): 7 12 13 def setUp(self): 14 cherrypy.root = MyRoot() 15 startup.startTurboGears() 16 8 17 def test_UnicodeValueAppearingInATemplateIsFine(self): 18 testutil.create_request('/') 9 19 ustr = u"micro-eXtreme Programming ( µ XP): Embedding XP Within Standard Projects" 10 20 info = dict(someval=ustr) 11 21 val = view.render(info, template="turbogears.tests.simple") 12 22 self.failUnless(u"Paging all " + ustr in val.decode("utf-8")) 13 23 14 24 def test_templateRetrievalByPath(self): 25 testutil.create_request('/') 15 26 config.update({'server.environment' : 'development'}) 16 27 from turbokid import kidsupport 17 28 ks = kidsupport.KidSupport() -
turbogears/tests/test_testutil.py
19 19 return "cookie not found" 20 20 get_name = turbogears.expose()(get_name) 21 21 22 #testutil.start_server() 22 23 23 24 def test_browser_session(): 24 25 cherrypy.root = MyRoot() … … 36 37 bs1.goto('/set_name?name=bs1') 37 38 bs2.goto('/set_name?name=bs2') 38 39 bs1.goto('/get_name') 39 assert bs1.response == 'bs1' 40 assert bs1.response == 'bs1', bs1.response 40 41 bs2.goto('/get_name') 41 42 assert bs2.response == 'bs2' -
turbogears/tests/test_catwalk.py
47 47 48 48 def test_wrong_filter_format(self): 49 49 cherrypy.root.catwalk = CatWalk(browse) 50 testutil.create_request("/catwalk/browse/?object_name=Song&filters=Guantanemera&tg_format=json") 51 response = cherrypy.response.body[0] 52 assert 'filter_format_error' in response 50 response = testutil.create_request("/catwalk/browse/?object_name=Song&filters=Guantanemera&tg_format=json") 51 assert 'filter_format_error' in response.body 53 52 54 53 def test_wrong_filter_column(self): 55 54 cherrypy.root.catwalk = CatWalk(browse) 56 testutil.create_request("/catwalk/browse/?object_name=Song&filters=guacamole:2&tg_format=json") 57 response = cherrypy.response.body[0] 58 assert 'filter_column_error' in response 55 response = testutil.create_request("/catwalk/browse/?object_name=Song&filters=guacamole:2&tg_format=json") 56 assert 'filter_column_error' in response.body 59 57 60 58 def test_filters(self): 61 59 cherrypy.root.catwalk = CatWalk(browse) 62 testutil.create_request("/catwalk/browse/?object_name=Song&tg_format=json") 63 response = cherrypy.response.body[0] 64 values = simplejson.loads(response) 60 reponse = testutil.create_request("/catwalk/browse/?object_name=Song&tg_format=json") 61 values = simplejson.loads(response.body) 65 62 assert values['total'] == 15 * 15 * 15 #without the filters we get all songs (3375) 66 testutil.create_request("/catwalk/browse/?object_name=Song&filters=album:1&tg_format=json") 67 response = cherrypy.response.body[0] 68 values = simplejson.loads(response) 63 response = testutil.create_request("/catwalk/browse/?object_name=Song&filters=album:1&tg_format=json") 64 values = simplejson.loads(response.body) 69 65 assert values['total'] == 15 #filter by album id (only 15 songs) 70 66 71 67 def test_response_fields(self): 72 68 #Check that the response contains the expected keys 73 69 cherrypy.root.catwalk = CatWalk(browse) 74 testutil.create_request("/catwalk/browse/?object_name=Artist&start=3&page_size=20&tg_format=json") 75 response = cherrypy.response.body[0] 76 values = simplejson.loads(response) 70 response = testutil.create_request("/catwalk/browse/?object_name=Artist&start=3&page_size=20&tg_format=json") 71 values = simplejson.loads(response.body) 77 72 assert values.has_key('headers') 78 73 assert values.has_key('rows') 79 74 assert values.has_key('start') … … 87 82 #Control that the count for related and multiple joins match 88 83 #the number of related instances when accessed as a field 89 84 cherrypy.root.catwalk = CatWalk(browse) 90 testutil.create_request("/catwalk/browse/?object_name=Artist&tg_format=json") 91 response = cherrypy.response.body[0] 92 values = simplejson.loads(response) 85 response = testutil.create_request("/catwalk/browse/?object_name=Artist&tg_format=json") 86 values = simplejson.loads(response.body) 93 87 artist = browse.Artist.get(1) 94 88 assert int(values['rows'][0]['genres']) == len(list(artist.genres)) 95 89 assert int(values['rows'][0]['albums']) == len(list(artist.albums)) … … 97 91 def test_rows_column_number(self): 98 92 #Control that the number of columns match the number of fields in the model 99 93 cherrypy.root.catwalk = CatWalk(browse) 100 testutil.create_request("/catwalk/browse/?object_name=Artist&tg_format=json") 101 response = cherrypy.response.body[0] 102 values = simplejson.loads(response) 94 response = testutil.create_request("/catwalk/browse/?object_name=Artist&tg_format=json") 95 values = simplejson.loads(response.body) 103 96 assert len(values['rows'][0]) == 4 104 97 105 98 def test_rows_limit(self): 106 99 #Update the limit of rows for the query and control the number of rows returned 107 100 cherrypy.root.catwalk = CatWalk(browse) 108 testutil.create_request("/catwalk/browse/?object_name=Artist&tg_format=json") 109 response = cherrypy.response.body[0] 110 values = simplejson.loads(response) 101 response = testutil.create_request("/catwalk/browse/?object_name=Artist&tg_format=json") 102 values = simplejson.loads(response.body) 111 103 assert values.has_key('rows') 112 104 assert len(values['rows']) == 10 113 testutil.create_request("/catwalk/browse/?object_name=Artist&page_size=15&tg_format=json") 114 response = cherrypy.response.body[0]115 values = simplejson.loads(response )105 106 response = testutil.create_request("/catwalk/browse/?object_name=Artist&page_size=15&tg_format=json") 107 values = simplejson.loads(response.body) 116 108 assert values.has_key('rows') 117 109 assert len(values['rows']) == 15 118 110 119 111 def test_header_labels(self): 120 112 #Check that the returned header labels match the the model 121 113 cherrypy.root.catwalk = CatWalk(browse) 122 testutil.create_request("/catwalk/browse/?object_name=Artist&tg_format=json") 123 response = cherrypy.response.body[0] 124 values = simplejson.loads(response) 114 response = testutil.create_request("/catwalk/browse/?object_name=Artist&tg_format=json") 115 values = simplejson.loads(response.body) 125 116 assert len(values['headers']) == 5 126 117 for header in values['headers']: 127 118 assert header['name'] in ['id','name','albums','genres', 'plays_instruments'] -
turbogears/tests/test_paginate.py
138 138 cherrypy.root = self.MyRoot() 139 139 140 140 def test_spy(self): 141 create_request('/spy') 142 body = cherrypy.response.body[0] 143 Spy.assert_ok(body, 'current_page', 1) 141 response = create_request('/spy') 142 Spy.assert_ok(response.body, 'current_page', 1) 144 143 try: 145 Spy.assert_ok( body, 'current_page', 2)144 Spy.assert_ok(response.body, 'current_page', 2) 146 145 raise Exception("above test should have failed") 147 146 except AssertionError: 148 147 pass 149 148 150 149 def test_correct_expectation(self): 151 create_request('/spy_correct_expectation') 152 body = cherrypy.response.body[0] 153 assert "ok: [paginate" in body 150 response = create_request('/spy_correct_expectation') 151 assert "ok: [paginate" in response.body 154 152 155 153 def test_wrong_expectation(self): 156 create_request('/spy_wrong_expectation') 157 body = cherrypy.response.body[0] 158 assert "fail: expected page_count=9, got page_count=10" in body 154 response = create_request('/spy_wrong_expectation') 155 assert "fail: expected page_count=9, got page_count=10" in response.body 159 156 160 157 def test_invalid_expectation(self): 161 create_request('/spy_invalid_expectation') 162 body = cherrypy.response.body[0] 163 assert "fail: paginate does not have 'foobar' attribute" in body 158 response = create_request('/spy_invalid_expectation') 159 assert "fail: paginate does not have 'foobar' attribute" in response.body 164 160 165 161 def test_raw_expectation(self): 166 create_request('/spy_correct_expectation')167 Spy.assert_ok( cherrypy.response.body[0], 'var_name', 'data')168 Spy.assert_ok( cherrypy.response.body[0], 'var_name', "'data'", raw=True)162 response = create_request('/spy_correct_expectation') 163 Spy.assert_ok(response.body, 'var_name', 'data') 164 Spy.assert_ok(response.body, 'var_name', "'data'", raw=True) 169 165 170 166 171 167 class TestPagination(unittest.TestCase): 172 168 """Base class for all Paginate TestCases""" 173 169 174 def request(self, url): 175 create_request(url) 176 self.body = cherrypy.response.body[0] 170 def request(self, url, status=None): 171 self.body = create_request(url, status=status).body 177 172 if "fail: " in self.body: 178 173 print self.body 179 assert False, "Spy alert! Check bodyoutput for details..."174 assert False, "Spy alert! Check response output for details..." 180 175 181 176 182 177 class TestBasicPagination(TestPagination): … … 358 353 Spy.assert_ok(self.body, 'pages', [4, 5, 6, 7]) 359 354 360 355 def test_invalid_dynamic_limit(self): 361 self.request("/invalid_dynamic" )356 self.request("/invalid_dynamic", status=500) 362 357 assert 'StandardError: dynamic_limit: foobar not found in output dict' in self.body 363 358 364 359 def test_dynamic_limit(self): … … 733 728 734 729 def test_invalid_default_reversed(self): 735 730 for method in "Q", "SR", "SO": 736 self.request("/wrong_reversed/?method=%s" % method )731 self.request("/wrong_reversed/?method=%s" % method, status=500) 737 732 assert 'StandardError: default_reversed (deprecated) is only allowed' in self.body 738 733 739 734 def test_reverse_ordering(self): -
turbogears/testutil.py
4 4 import unittest 5 5 import Cookie 6 6 import cStringIO as StringIO 7 from turbogears.util import deprecated 7 8 8 9 import cherrypy 9 from cherrypy import _cphttptools 10 cherrypy_major_ver = int(cherrypy.__version__.split('.')[0]) 11 if cherrypy_major_ver < 3: 12 from cherrypy._cphttptools import Request, Response 13 else: 14 from cherrypy import Request, Response 10 15 11 16 try: 12 17 import sqlobject … … 50 55 config.update({'global': 51 56 {'autoreload.on': False, 'tg.new_style_logging': True}}) 52 57 58 harness = config.get('test_framework', 'cp2_webob') 59 #harness = config.get('test_framework', 'webtest') 60 if harness == 'webtest': 61 from webtest import TestApp 53 62 54 def start_cp(): 63 def mount_app(app): 64 cherrypy.tree.mount(app) 65 66 def start_server(root=None): 67 if root: 68 mount_app(root) 55 69 if not config.get("cherrypy_started", False): 56 cherrypy.server.start(serverClass=None, initOnly=True) 57 config.update({"cherrypy_started" : True}) 70 if cherrypy_major_ver < 3: 71 cherrypy.server.start(serverClass=None, initOnly=True) 72 else: 73 mount_app(cherrypy.root) 74 cherrypy.server.quickstart() 75 cherrypy.engine.start() #blocking=False) 76 config.update({"cherrypy_started" : True}) 58 77 78 start_cp = deprecated("start_cp has been superceded by start_server.")(start_server) 59 79 60 80 test_user = None 61 81 … … 72 92 or current_provider.anonymous_identity()) 73 93 74 94 75 def create_request(request, method="GET", protocol="HTTP/1.1", 76 headers={}, rfile=None, clientAddress="127.0.0.1", 77 remoteHost="localhost", scheme="http"): 78 start_cp() 79 if not rfile: 80 rfile = StringIO.StringIO("") 81 if type(headers) != dict: 82 headerList = headers 95 def get_app(): 96 if cherrypy_major_ver < 3: 97 app = cherrypy._cpwsgi.wsgiApp 83 98 else: 84 headerList = [(key, value) for key, value in headers.items()] 85 headerList.append(("Host", "localhost")) 86 if not hasattr(cherrypy.root, "started"): 87 startup.startTurboGears() 88 cherrypy.root.started = True 89 req = _cphttptools.Request(clientAddress, 80, remoteHost, scheme) 90 cherrypy.serving.request = req 91 attach_identity(req) 92 cherrypy.serving.response = _cphttptools.Response() 93 req.run(" ".join((method, request, protocol)), headerList, rfile) 99 app = cherrypy.tree.mount(cherrypy.root, '/') #app.HelloWorld(), '/') 100 # app = cherrypy.wsgi.CPWSGIServer 101 return app 94 102 95 103 104 class PsuedoWebObResponse(object): 105 """ A wrapper around CherryPy 2's response to make it look more like a WebOb response.""" 106 def __init__(self, cp_response, status=None): 107 self._cp_response = cp_response 108 #raise Exception(dir(cp_response), str(cp_response.body)) 109 try: 110 self.body = cp_response.body[0] 111 except TypeError: 112 self.body = None 113 114 self.status = cp_response.status 115 status_ok = ((status == None and self.status[0] in ['2', '3']) or 116 (status==int(self.status[:3])) or (status == '*')) 117 if self.status[0] != '5': 118 msg = (status, self.status) 119 else: 120 msg = status_ok, (status, self.status, self.body) 121 assert status_ok, msg 122 123 self.cookies = getattr(cp_response, 'simple_cookie', Cookie.SimpleCookie()) 124 self.cookies_set = self.cookies 125 self.headers = cp_response.headers 126 self.location = cp_response.headers.get('Location', None) 127 128 def __str__(self): 129 return self.body 130 __repr__ = __str__ 131 132 133 def create_request(request, headers={}, cookies={}, status=None): 134 # Previously accepted the following parms, but they appeared to be unsed: 135 # protocol, remoteHost, clientAddress, rfile, scheme, method. 136 # Also, headers used to be able to be a list or a dict, but only dict was ever used. 137 rfile=StringIO.StringIO("") 138 protocol="HTTP/1.1" 139 clientAddress="127.0.0.1" 140 remoteHost="localhost" 141 scheme="http" 142 method="GET" 143 144 #start_server() 145 header_list = [(key, value) for key, value in headers.items()] 146 if not headers.get("Host", None): 147 header_list.append(("Host", "localhost")) 148 149 if 1==1 or harness.startswith('cp2'): 150 if not hasattr(cherrypy.root, "started"): 151 startup.startTurboGears() 152 cherrypy.root.started = True 153 #startup.startTurboGears() 154 start_server() 155 if harness.startswith('cp2'): 156 req = Request(clientAddress, 80, remoteHost, scheme) 157 cherrypy.serving.request = req 158 attach_identity(req) 159 cherrypy.serving.response = Response() 160 req.run(" ".join((method, request, protocol)), header_list, rfile) 161 response = cherrypy.serving.response 162 #response = cherrypy.response 163 if harness == 'cp2_webob': 164 # Lightly massage cherrypy's response to make it look slightly more like webob's 165 response = PsuedoWebObResponse(cherrypy.response, status) 166 167 # I don't know why this is necessary, but several tests fail 168 # unless I pull the top element out :-( 169 #if isinstance(response.body, list): 170 # response.body = response.body[0] 171 # raise 'hah! %s' % response.body 172 #else: 173 # raise 'wahh! %s' % type(response.body) 174 #elif harness != 'cp2_webob': 175 # raise 'Huhh?! %s' % harness 176 else: 177 start_server() 178 app = TestApp(get_app()) 179 app.cookies = cookies 180 response = app.get(request, headers, status=status) 181 response.app = app 182 #raise Exception(dir(response)) 183 return response 184 185 186 createRequest = deprecated("Use create_request instead of createRequest")(create_request) 187 188 96 189 class BrowsingSession(object): 97 190 98 191 def __init__(self): … … 104 197 if self.cookie: 105 198 headers = kwargs.setdefault('headers', {}) 106 199 headers['Cookie'] = self.cookie.output() 107 create_request(*args, **kwargs) 108 self.response = cherrypy.response.body[0] 109 self.status = cherrypy.response.status 110 if cherrypy.response.simple_cookie: 111 self.cookie.update(cherrypy.response.simple_cookie) 200 response = create_request(*args, **kwargs) 201 self.response = response.body 202 self.status = response.status 203 self.cookie = response.cookies_set 112 204 113 205 114 206 def _return_directly(output, *args): … … 137 229 138 230 139 231 def call(method, *args, **kw): 140 start_ cp()232 start_server() 141 233 output, response = call_with_request(method, DummyRequest(), *args, **kw) 142 234 return output 143 235 … … 150 242 """ 151 243 orig_proc_output = controllers._process_output 152 244 controllers._process_output = _return_directly 153 cherrypy.serving.response = _cphttptools.Response()245 cherrypy.serving.response = Response() 154 246 cherrypy.serving.request = request 155 247 if not hasattr(request, "identity"): 156 248 attach_identity(request) … … 301 393 302 394 __all__ = ["call", "create_request", "DBTest", 303 395 "attach_identity", "set_identity_user", 304 "capture_log", "print_log", "get_log", "sqlalchemy_cleanup"] 396 "capture_log", "print_log", "get_log", "sqlalchemy_cleanup", 397 "mount_app"] 398 -
turbogears/util.py
444 444 return re.sub("&(\w+);?", repl, htmltext) 445 445 446 446 447 # This decorator inspired by python cookbook recipe #391367. 448 import warnings 449 450 def deprecated(message=None): 451 """This is a decorator which can be used to mark functions 452 as deprecated. It will result in a warning being emmitted 453 when the function is used. 454 455 Examples: 456 457 >>> import sys; _stderr = sys.stderr; sys.stderr = sys.stdout #allow doctest to see warnings 458 459 >>> @deprecated('some_old_function has been deprecated. Use "new_func" instead') 460 ... def some_old_function(x,y): 461 ... return x + y 462 >>> print some_old_function(1, 2) 463 deprecated.py... DeprecationWarning: some_old_function has been deprecated. Use "new_func" instead 464 ... 465 3 466 >>> class SomeClass: 467 ... @deprecated() 468 ... def some_old_method(self, x,y): 469 ... return x + y 470 >>> sc = SomeClass() 471 >>> print sc.some_old_method(2, 3) 472 deprecated.py... DeprecationWarning:... 473 5 474 >>> def my_func(x, y): 475 ... return x + y 476 >>> myFunc = deprecated("Use the PEP8 version")(my_func) 477 >>> print myFunc(5, 2) 478 deprecated.py... DeprecationWarning: Use the PEP8 version 479 ... 480 7 481 >>> sys.stderr = _stderr 482 """ 483 def decorate(func): 484 if not decorate.message: 485 decorate.message = "Call to deprecated function %s." % func.__name__ 486 def newFunc(*args, **kwargs): 487 warnings.warn(decorate.message, category=DeprecationWarning, stacklevel =2) 488 return func(*args, **kwargs) 489 newFunc.__name__ = func.__name__ 490 newFunc.__doc__ = func.__doc__ 491 newFunc.__dict__.update(func.__dict__) 492 return newFunc 493 decorate.message = message 494 return decorate 495 496 447 497 __all__ = ["Enum", "setlike", 448 498 "get_package_name", "get_model", "load_project_config", 449 499 "url", "ensure_sequence", "has_arg", -
turbogears/view/base.py
90 90 engine = engines.get(enginename, None) 91 91 if not engine: 92 92 raise KeyError, \ 93 "Template engine %s is not installed " % enginename93 "Template engine %s is not installed. Installed are: %s" % (enginename, engines) 94 94 return engine, template, enginename 95 95 96 96