1# Cork - Authentication module for the Bottle web framework 2# Copyright (C) 2013 Federico Ceratto and others, see AUTHORS file. 3# Released under LGPLv3+ license, see LICENSE.txt 4# 5# Functional test using Json backend 6# 7# Requires WebTest http://webtest.pythonpaste.org/ 8# 9# Run as: nosetests functional_test.py 10# 11 12from nose import SkipTest 13from webtest import TestApp 14import shutil 15import os 16 17import testutils 18from cork import Cork 19 20REDIR = 302 21 22class Test(testutils.WebFunctional): 23 24 def create_app_instance(self): 25 """create TestApp instance""" 26 assert self._app is None 27 import simple_webapp 28 self._bottle_app = simple_webapp.app 29 self._app = TestApp(self._bottle_app) 30 print("Test App created") 31 32 33 34 35 def login_as_admin(self): 36 """perform log in""" 37 assert self._app is not None 38 assert 'beaker.session.id' not in self._app.cookies, "Unexpected cookie found" 39 40 self.assert_200('/login', 'Please insert your credentials') 41 assert 'beaker.session.id' not in self._app.cookies, "Unexpected cookie found" 42 43 self.assert_redirect('/admin', '/sorry_page') 44 45 self.assert_200('/user_is_anonymous', 'True') 46 assert 'beaker.session.id' not in self._app.cookies, "Unexpected cookie found" 47 48 post = {'username': 'admin', 'password': 'admin'} 49 self.assert_redirect('/login', '/', post=post) 50 assert 'beaker.session.id' in self._app.cookies, "Cookie not found" 51 52 import bottle 53 session = bottle.request.environ.get('beaker.session') 54 print("Session from func. test", repr(session)) 55 56 self.assert_200('/login', 'Please insert your credentials') 57 58 59 p = self._app.get('/admin') 60 assert 'Welcome' in p.body, repr(p) 61 62 p = self._app.get('/my_role', status=200) 63 assert p.status_int == 200 64 assert p.body == 'admin', "Sta" 65 66 print("Login performed") 67 68 69 70 def test_functional_expiration(self): 71 self.login_as_admin() 72 r = self._app.get('/admin') 73 assert r.status == '200 OK', repr(r) 74 # change the cookie expiration in order to expire it 75 self._app.app.options['timeout'] = 0 76 assert self._app.get('/admin').status_int == REDIR, "The cookie should have expired" 77 78