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