Skip to content

Commit

Permalink
refactoring for test fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
bmsauer committed Jun 12, 2017
1 parent 516ab13 commit c92a772
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
env/*
env.sh
*env.sh
__pycache__
*.pyc
logs/*.log*
Expand Down
3 changes: 3 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ MANUAL INSTALL INSTRUCTIONS
- cd to serversetup, run sudo -E ./install.tcl
- supervisord -c supervisord.conf

RUN TESTS
- nosetests -vsx tests

NOTES
Similar services: iron.io ironworker, aws lambda, stdlib.com, webtask.io, google functions,
azure functions, ibm openwhisk
1 change: 1 addition & 0 deletions env.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export BIGCGI_ENV="TEST"
export BIGCGI_SECRET_KEY="secret key"
export BIGCGI_ADMIN_PASSWORD="admin password"
export BIGCGI_SMTP_USERNAME="smtp username"
Expand Down
2 changes: 2 additions & 0 deletions exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ToolsException(Exception):
pass
21 changes: 20 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from log4mongo.handlers import BufferedMongoHandler

class AppSettings(object):
BIGCGI_ENV = os.environ["BIGCGI_ENV"]
SECRET_KEY = os.environ["BIGCGI_SECRET_KEY"]
ADMIN_PASSWORD = os.environ["BIGCGI_ADMIN_PASSWORD"]
SMTP_USERNAME = os.environ["BIGCGI_SMTP_USERNAME"]
Expand All @@ -30,6 +31,8 @@ class AppSettings(object):
DATABASE_USERNAME = os.environ["BIGCGI_DATABASE_USERNAME"]
DATABASE_PASSWORD = os.environ["BIGCGI_DATABASE_PASSWORD"]
DATABASE_URI = "mongodb://localhost:27017"
DATABASE_MAIN = "bigcgi-main"
DATABASE_CORK = "bigcgi-cork"

CGI_BASE_PATH_TEMPLATE = "/home/{}/public_html"

Expand Down Expand Up @@ -59,4 +62,20 @@ def get_logger(self):
self.logger.setLevel("INFO")
return self.logger

app_settings = AppSettings()
class TestSettings(AppSettings):
DATABASE_MAIN = "bigcgi-main-test"
DATABASE_CORK = "bigcgi-cork-test"

def get_logger(self):
if self.logger == None:
format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logging.basicConfig(format=format)
self.logger = logging.getLogger()
self.logger.setLevel("DEBUG")
return self.logger


if os.environ.get("BIGCGI_ENV", None) == "TEST":
app_settings = TestSettings()
else:
app_settings = AppSettings()
23 changes: 23 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import os
import sys

import toolrunner
from settings import app_settings

def setup():
print("setting up package")
if os.environ.get("BIGCGI_ENV", None) != "TEST":
print("Aborting tests: BIGCGI_ENV is not set to TEST.")
sys.exit(1)

print("setting up test database")
try:
toolrunner.go("setup_auth_db", "create_test_databases", [])
except Exception as e:
print(str(e))
sys.exit(1)


def teardown():
print("tearing down package")
print("tearing down test database")
try:
toolrunner.go("setup_auth_db", "delete_test_databases", [])
except Exception as e:
print(str(e))
sys.exit(1)
23 changes: 21 additions & 2 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
"""
This file is part of bigCGI.
bigCGI is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
bigCGI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with bigCGI. If not, see <http://www.gnu.org/licenses/>.
"""

from webtest import TestApp
from nose import with_setup
Expand All @@ -12,7 +28,10 @@ def teardown_func():

@with_setup(setup_func, teardown_func)
def test_index():
dweedle = TestApp(app.app)
response = dweedle.get("/")
testapp = TestApp(app.app)
response = testapp.get("/?error=errormsg&flash=flashmsg")
assert "bigCGI" in response
assert "errormsg" in response
assert "flashmsg" in response


37 changes: 37 additions & 0 deletions tests/test_cork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
This file is part of bigCGI.
bigCGI is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
bigCGI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with bigCGI. If not, see <http://www.gnu.org/licenses/>.
"""

from webtest import TestApp
from nose import with_setup

import app

def setup_func():
pass
def teardown_func():
pass

"""
@with_setup(setup_func, teardown_func)
def test_index():
testapp = TestApp(app.app)
response = testapp.get("/?error=errormsg&flash=flashmsg")
assert "bigCGI" in response
assert "errormsg" in response
assert "flashmsg" in response
"""

31 changes: 19 additions & 12 deletions toolrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,26 @@
#then attempts to run a function with the name given in subcommand
#(defaults to run)

def go(command, subcommand, arguments):
imp = "tools.{}".format(command,subcommand)
module = importlib.import_module(imp)

func = getattr(module, subcommand)
func(*arguments)

parser = argparse.ArgumentParser(description='Tool Runner for bigCGI')
parser.add_argument('command', type=str, help='The command to run.')
parser.add_argument("subcommand", type=str, help="The subcommand to run.")
parser.add_argument("arguments", type=str, help="Additional arguments for subcommand", nargs="*")
def main():
parser = argparse.ArgumentParser(description='Tool Runner for bigCGI')
parser.add_argument('command', type=str, help='The command to run.')
parser.add_argument("subcommand", type=str, help="The subcommand to run.")
parser.add_argument("arguments", type=str, help="Additional arguments for subcommand", nargs="*")

args = parser.parse_args()
command = args.command
subcommand = args.subcommand
arguments = args.arguments
args = parser.parse_args()
command = args.command
subcommand = args.subcommand
arguments = args.arguments

imp = "tools.{}".format(command,subcommand)
module = importlib.import_module(imp)
go(command, subcommand, arguments)

if __name__ == "__main__":
main()

func = getattr(module, subcommand)
func(*arguments)
17 changes: 14 additions & 3 deletions tools/setup_auth_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
import pymongo

from settings import app_settings
from exceptions import *

#sets up an admin user in the database.

def run(*args):
#cork (app auth)
mb = MongoDBBackend(db_name='bigcgi-cork', initialize=True)
mb = MongoDBBackend(db_name=app_settings.DATABASE_CORK, initialize=True)
cork = Cork(backend=mb)
admin_hash = cork._hash("admin", app_settings.ADMIN_PASSWORD)
mb.users._coll.insert({
Expand Down Expand Up @@ -75,9 +76,19 @@ def create_databases_with_auth(*args):
def create_role(*args):
new_role_name = args[0]
new_role_level = args[1]
mb = MongoDBBackend(db_name='bigcgi-cork', initialize=True, username=app_settings.DATABASE_USERNAME, password=app_settings.DATABASE_PASSWORD)
mb = MongoDBBackend(db_name=app_settings.DATABASE_CORK, initialize=True, username=app_settings.DATABASE_USERNAME, password=app_settings.DATABASE_PASSWORD)
mb.roles._coll.insert({'role': new_role_name, 'val': new_role_level})


def create_test_databases(*args):
create_databases_with_auth(app_settings.DATABASE_CORK)
create_databases_with_auth(app_settings.DATABASE_MAIN)

def delete_test_databases(*args):
if app_settings.BIGCGI_ENV != "TEST":
raise ToolsException("Cannot delete test databases if not in test environment.")
client = pymongo.MongoClient(app_settings.DATABASE_URI)
client.drop_database(app_settings.DATABASE_CORK)
client.drop_database(app_settings.DATABASE_MAIN)


if __name__=="__main__":
Expand Down

0 comments on commit c92a772

Please sign in to comment.