2
A
回答
2
這個例子可能會有幫助:
from webtest import TestApp
from nose.tools import eq_
class TestAuthentication(object):
"""
Tests for the default authentication setup.
If your application changes how the authentication layer is configured
those tests should be updated accordingly
"""
application_under_test = 'main'
def setUp(self):
"""Method called by nose before running each test"""
# Loading the application:
conf_dir = config.here
wsgiapp = loadapp('config:test.ini#%s' % self.application_under_test,
relative_to=conf_dir)
self.app = TestApp(wsgiapp)
# Setting it up:
test_file = path.join(conf_dir, 'test.ini')
cmd = SetupCommand('setup-app')
cmd.run([test_file])
def tearDown(self):
"""Method called by nose after running each test"""
# Cleaning up the database:
model.DBSession.remove()
teardown_db()
def test_forced_login(self):
"""Anonymous users are forced to login
Test that anonymous users are automatically redirected to the login
form when authorization is denied. Next, upon successful login they
should be redirected to the initially requested page.
"""
# Requesting a protected area
resp = self.app.get('/secc/', status=302)
assert resp.location.startswith('http://localhost/login')
# Getting the login form:
resp = resp.follow(status=200)
print resp.forms
form = resp.forms
# Submitting the login form:
form['login'] = u'manager'
form['password'] = 'managepass'
post_login = form.submit(status=302)
# Being redirected to the initially requested page:
assert post_login.location.startswith('http://localhost/post_login')
initial_page = post_login.follow(status=302)
assert 'authtkt' in initial_page.request.cookies, \
"Session cookie wasn't defined: %s" % initial_page.request.cookies
assert initial_page.location.startswith('http://localhost/secc/'), \
initial_page.location
相關問題
- 1. 用鼻子/夾具/ webtest(amidoinitrite)測試cherrypy
- 2. WebTest單元測試瓶應用程序
- 3. WSGI應用程序中的cherrypy和相對路徑
- 4. 主要和測試版WSGI應用程序
- 5. webtest,web應用程序項目和tfsbuild
- 6. GAE:使用測試平臺和webtest測試blob的下載
- 7. 如何在Google App Engine中測試Web應用程序WSGI應用程序的響應?
- 8. 使用CherryPy部署django應用程序
- 9. 運行WSGI應用程序功能測試的最佳方法?
- 10. 如何從腳本測試WSGI應用程序?
- 11. 如何爲我自己的應用程序使用cherrypy測試工具?
- 12. 如何使用pyscripter調試cherrypy應用程序?
- 13. 一起使用django-nose和django-芹菜 - 單元測試
- 14. SSL和WSGI應用程序 - Python
- 15. python:APScheduler在WSGI應用程序
- 16. 在cherrypy應用程序中測試會話變量和db存儲
- 17. WebTest的谷歌應用程序引擎
- 18. Robolectric未使用測試應用程序
- 19. 使用Zombie測試Node.js應用程序
- 20. 使用Appium測試NativeScript應用程序
- 21. 使用SpecFlow測試WPF應用程序
- 22. 使用iPhone應用程序測試--basics
- 23. 使用NeoLoad測試ZK應用程序
- 24. 使用Valgrind測試iPhone應用程序
- 25. 使用PUSH APNS測試應用程序
- 26. 使用QProcess測試PySide應用程序
- 27. 使用會話測試應用程序
- 28. 使用Squish測試JavaScript應用程序
- 29. 使用JUnit測試Swing應用程序
- 30. 使用DRT測試Web應用程序