Skip to content

Commit

Permalink
some messy wip
Browse files Browse the repository at this point in the history
  • Loading branch information
thruflo committed Nov 21, 2010
1 parent d6046ef commit fb4de00
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 36 deletions.
3 changes: 0 additions & 3 deletions etc/paste.ini → etc/base.ini
@@ -1,10 +1,7 @@
[app:main]
use = egg:tstable#main
debug = true
sqlite_path = sqlite:////%(here)s/../var/sqlite.db

[server:main]
use = egg:gunicorn#main
workers = 1
host = 127.0.0.1
port = 8080
9 changes: 9 additions & 0 deletions etc/dev.ini
@@ -0,0 +1,9 @@
[app:main]
use = config:base.ini
mode = dev
debug = true
sqlite_path = sqlite:////%(here)s/../var/dev.db

[server:main]
use = config:base.ini
workers = 1
8 changes: 8 additions & 0 deletions etc/prod.ini
@@ -0,0 +1,8 @@
[app:main]
use = config:base.ini
mode = prod
debug = false

[server:main]
use = config:base.ini
workers = 4
9 changes: 9 additions & 0 deletions etc/test.ini
@@ -0,0 +1,9 @@
[app:main]
use = config:base.ini
mode = test
debug = false
sqlite_path = sqlite:///:memory:

[server:main]
use = config:base.ini
workers = 1
File renamed without changes.
73 changes: 49 additions & 24 deletions src/tstable/main.py
Expand Up @@ -4,8 +4,7 @@
"""
"""

import code
import getpass
# import getpass

import logging
import sys
Expand All @@ -21,38 +20,64 @@
# from urls import mapping
mapping = []

def app_factory(global_config, **local_conf):
"""
class WSGIApplicationFactory(object):
""" n.b.: should adapt a wsgiapp and return it when called
but for now...
"""

# Config
def setup_registrations(self, site_manager):
site_manager.registerUtility(userFactory, IFactory, 'user')
provideAdapter(UserAuthenticator)
if self.settings['mode'] == 'dev':
session = SQLiteSQLAlchemySession(self.settings['sqlite_path'])
site_manager.registerUtility(session, ISQLAlchemySession)
elif self.settings['mode'] == 'test':
session = SQLiteSQLAlchemySession(self.settings['sqlite_path'])
site_manager.registerUtility(session, ISQLAlchemySession)
elif self.settings['mode'] == 'prod':
raise NotImplementedError
else:
raise ValueError


settings = global_config
settings.update(local_conf)
settings['debug'] = bool(settings['debug'] == 'true')
def configure_logging(self):
LOG_LEVEL_1 = self.settings['debug'] and logging.DEBUG or logging.INFO
LOG_LEVEL_2 = self.settings['debug'] and logging.INFO or logging.WARNING

logging.basicConfig(level=LOG_LEVEL_1)
logging.getLogger('beaker').setLevel(LOG_LEVEL_2)
logging.getLogger('gunicorn.arbiter').setLevel(LOG_LEVEL_2)


# Logging
def __init__(self, global_config, local_config):
s = global_config
s.update(local_config)
s['debug'] = bool(s['debug'] == 'true')
self.settings = s


LOG_LEVEL_1 = settings['debug'] and logging.DEBUG or logging.INFO
LOG_LEVEL_2 = settings['debug'] and logging.INFO or logging.WARNING

logging.basicConfig(level=LOG_LEVEL_1)
logging.getLogger('beaker').setLevel(LOG_LEVEL_2)
logging.getLogger('gunicorn.arbiter').setLevel(LOG_LEVEL_2)


def app_factory(global_config, **local_config):
"""
"""

# Registrations
site_manager = getGlobalSiteManager()

gsm = getGlobalSiteManager()
session = SQLiteSQLAlchemySession(settings['sqlite_path'])
gsm.registerUtility(session, ISQLAlchemySession)
gsm.registerUtility(userFactory, IFactory, 'user')
provideAdapter(UserAuthenticator)
factory = WSGIApplicationFactory(global_config, local_config)
factory.configure_logging()
factory.setup_registrations(site_manager)

# Go ...
if factory.settings['mode'] == 'test':
sys.argv = [sys.argv[0], '-c', '/env/sandbox/tstable/nose.cfg']
import nose
nose.run()

# if settings.has_key('shell'):
code.interact(local=locals())
if factory.settings.has_key('shell'):
import code
code.interact(local=locals())

# return web.WSGIApplication(mapping, settings=settings)
return factory()(mapping, settings=factory.settings)


46 changes: 37 additions & 9 deletions src/tstable/tests.py
Expand Up @@ -7,6 +7,8 @@
import unittest
from mock import Mock

from zope.component import getGlobalSiteManager

from interfaces import *
from model import *

Expand All @@ -15,13 +17,7 @@ class TestUser(unittest.TestCase):
"""

def setUp(self):
self.user = User(
first_name=u'James',
last_name=u'Arthur',
email=u'thruflo@geemail.com',
username=u'thruflo',
password=u'...'
)
self.user = User(password=u'...')



Expand All @@ -30,7 +26,6 @@ def test_public_key(self):
pwd = self.user.password
self.user.password = None
self.assertTrue(self.user.public_key is None)
self.user.password = pwd



Expand Down Expand Up @@ -84,6 +79,39 @@ def test_authenticate_invalid_credentials(self):



class TestIntegration(unittest.TestCase):

def setUp(self):
gsm = getGlobalSiteManager()
self.user = User(username=u'foo', password=u'...')
self.db = gsm.getUtility(ISQLAlchemySession)
self.db.add(self.user)
#try:
# self.db.commit()
#except IntegrityError, err:
# logging.err(err)
# self.db.rollback()
self.authenticator = UserAuthenticator(User, self.db)



def test_successful_username_password_authenticate(self):
result = self.authenticator.authenticate(
username=u'thruflo',
password=u'wrong'
)
self.assertTrue(result.username == self.user.username)

# raise NotImplementedError('this test current works regardless')



def tearDown(self):
self.db.delete(self.user)





if __name__ == '__main__':
unittest.main()

0 comments on commit fb4de00

Please sign in to comment.