Skip to content

Commit

Permalink
Functional testing improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
FedericoCeratto committed Jul 15, 2013
1 parent 214eedd commit 6396be7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 52 deletions.
120 changes: 81 additions & 39 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,13 @@

REDIR = '302 Found'

#FIXME: fix skipped tests


class Test(object):
def __init__(self):
self._tmpdir = None
self._tmproot = None
self._app = None
self._starting_dir = os.getcwd()

#def setUp(self):
# assert False
def populate_conf_directory(self):
"""Populate a directory with valid configuration files, to be run just once
The files are not modified by each test
Expand Down Expand Up @@ -148,41 +143,102 @@ def create_app_instance(self):
self._app = TestApp(self._bottle_app)
print("Test App created")

def assert_redirect(self, page, redir_page):
def teardown(self):
print("Doing teardown")
try:
self._app.post('/logout')
except:
pass

# drop the cookie
self._app.reset()
assert 'beaker.session.id' not in self._app.cookies, "Unexpected cookie found"
# drop the cookie
self._app.reset()

#assert self._app.get('/admin').status != '200 OK'
os.chdir(self._starting_dir)
#if self._tmproot is not None:
# testutils.purge_temp_directory(self._tmproot)

self._app.app.options['timeout'] = self._default_timeout
self._app = None
self._tmproot = None
self._tmpdir = None
print("Teardown done")

def setup(self):
# create test dir and populate it using the example files

# save the directory where the unit testing has been run
if self._starting_dir is None:
self._starting_dir = os.getcwd()

# create json files to be used by Cork
self._tmproot = testutils.pick_temp_directory()
assert self._tmproot is not None

# purge the temporary test directory
self.remove_temp_dir()

self.populate_temp_dir()
self.create_app_instance()
self._app.reset()
print("Reset done")
self._default_timeout = self._app.app.options['timeout']
print("Setup completed")

def assert_200(self, path, match):
"""Assert that a page returns 200"""
p = self._app.get(path)
assert p.status_int == 200, "Status: %d, Location: %s" % \
(p.status_int, p.location)

if match is not None:
assert match in p.body, "'%s' not found in body: '%s'" % (match, p.body)

return p

def assert_redirect(self, page, redir_page, post=None):
"""Assert that a page redirects to another one"""
p = self._app.get(page, status=302)

# perform GET or POST
if post is None:
p = self._app.get(page, status=302)
else:
assert isinstance(post, dict)
p = self._app.post(page, post, status=302)

dest = p.location.split(':80/')[-1]
dest = "/%s" % dest
assert dest == redir_page, "%s redirects to %s instead of %s" % \
(page, dest, redir_page)

return p

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')

p = self._app.post('/login', {'username': 'admin', 'password': 'admin'})
assert p.status == REDIR, "Redirect expected"
assert p.location == 'http://localhost:80/', \
"Incorrect redirect to %s" % p.location
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"

a = self._app.app
for n in 'app', 'environ_key', 'options', 'session', 'wrap_app':
print
print(n)
print("REP %s" % repr(getattr(a, n)))
print("DIR %s" % dir(getattr(a, n)))

import bottle
session = bottle.request.environ.get('beaker.session')
print("Session from func. test", repr(session))

print('running GET myrole')
p = self._app.get('/my_role')
print('myrole', repr(p))
self.assert_200('/login', 'Please insert your credentials')


p = self._app.get('/admin')
assert 'Welcome' in p.body, repr(p)
Expand All @@ -193,24 +249,8 @@ def login_as_admin(self):

print("Login performed")

def teardown(self):
print("Doing logout")
try:
self._app.post('/logout')
except:
pass

assert self._app.get('/admin').status != '200 OK'
os.chdir(self._starting_dir)
#if self._tmproot is not None:
# testutils.purge_temp_directory(self._tmproot)

self._app = None
self._tmproot = None
self._tmpdir = None
print("Teardown done")

@SkipTest
def test_functional_login(self):
assert self._app
self._app.get('/admin', status=302)
Expand Down Expand Up @@ -252,7 +292,6 @@ def test_login_existing_user_wrong_password(self):
assert p.location == 'http://localhost:80/login', \
"Incorrect redirect to %s" % p.location

@SkipTest
def test_functional_login_logout(self):
# Incorrect login
p = self._app.post('/login', {'username': 'admin', 'password': 'BogusPassword'})
Expand All @@ -266,6 +305,8 @@ def test_functional_login_logout(self):
assert p.location == 'http://localhost:80/', \
"Incorrect redirect to %s" % p.location

self.assert_200('/my_role', 'admin')

# fetch a page successfully
assert self._app.get('/admin').status == '200 OK', "Admin page should be served"

Expand All @@ -279,7 +320,6 @@ def test_functional_login_logout(self):
# fetch the same page, unsuccessfully
assert self._app.get('/admin').status == REDIR

@SkipTest
def test_functional_user_creation_login_deletion(self):
assert self._app.cookies == {}, "The cookie should be not set"

Expand All @@ -289,6 +329,8 @@ def test_functional_user_creation_login_deletion(self):
assert p.location == 'http://localhost:80/', \
"Incorrect redirect to %s" % p.location

self.assert_200('/my_role', 'admin')

# Create new user
username = 'BrandNewUser'
password = '42IsTheAnswer'
Expand Down
14 changes: 1 addition & 13 deletions tests/test_functional_decorated.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,17 @@ def login_as_admin(self):
self.assert_redirect('/login', '/', post=post)
assert 'beaker.session.id' in self._app.cookies, "Cookie not found"

#for n in 'app', 'environ_key', 'options', 'session', 'wrap_app':
for n in ():
print
print("NAME: %s" % n)
print("REPR: %s" % repr(getattr(self._app.app, n)))
print("DIR: %s" % dir(getattr(self._app.app, n)))

#ret = self.assert_200('/user_is_anonymous', 'True')
#assert 'beaker.session.id' in ret.cookies_set, "Cookie not found"
self.assert_200('/my_role', 'admin')
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))

print '#' * 33
self.assert_200('/login', 'Please insert your credentials')
print '#' * 33

p = self._app.get('/admin')
print (p.location)

p = self._app.get('/admin')
assert 'Welcome' in p.body, repr(p)

p = self._app.get('/my_role', status=200)
Expand Down

0 comments on commit 6396be7

Please sign in to comment.