Files
@ 57f6ebd8ea4b
Branch filter:
Location: rattail-project/rattail/tests/db/test_init.py - annotation
57f6ebd8ea4b
3.5 KiB
text/x-python
Add `User.active` and disallow authentication for inactive users.
a04712524db6 177478f7d054 177478f7d054 177478f7d054 177478f7d054 177478f7d054 177478f7d054 177478f7d054 177478f7d054 177478f7d054 177478f7d054 177478f7d054 177478f7d054 177478f7d054 177478f7d054 177478f7d054 177478f7d054 a04712524db6 a04712524db6 177478f7d054 177478f7d054 177478f7d054 177478f7d054 a566dc63d57d a566dc63d57d a566dc63d57d 177478f7d054 177478f7d054 a04712524db6 a566dc63d57d a566dc63d57d a566dc63d57d 177478f7d054 a566dc63d57d a566dc63d57d a04712524db6 a566dc63d57d 177478f7d054 177478f7d054 a04712524db6 a566dc63d57d a566dc63d57d a566dc63d57d 177478f7d054 a566dc63d57d a566dc63d57d a04712524db6 a566dc63d57d 177478f7d054 177478f7d054 177478f7d054 a04712524db6 177478f7d054 a04712524db6 177478f7d054 177478f7d054 177478f7d054 a04712524db6 177478f7d054 a04712524db6 a04712524db6 177478f7d054 177478f7d054 177478f7d054 177478f7d054 a04712524db6 177478f7d054 a04712524db6 a04712524db6 177478f7d054 177478f7d054 177478f7d054 177478f7d054 a04712524db6 177478f7d054 a04712524db6 a04712524db6 a04712524db6 177478f7d054 177478f7d054 177478f7d054 | # -*- coding: utf-8 -*-
from unittest import TestCase
from mock import patch
from sqlalchemy.orm import sessionmaker
from sqlalchemy.engine import Engine
from edbob.configuration import AppConfigParser
from rattail import db
class TestConfigureSessionFactory(TestCase):
def setUp(self):
self.config = AppConfigParser(u'rattail')
self.config.add_section(u'rattail.db')
self.Session = sessionmaker()
def test_session_is_not_bound_if_no_engine_is_defined_by_config(self):
db.configure_session_factory(self.config, self.Session)
session = self.Session()
self.assertTrue(session.bind is None)
session.close()
def test_session_is_correctly_bound_if_engine_is_defined_by_config(self):
self.config.set(u'rattail.db', u'sqlalchemy.url', u'sqlite:////a/very/custom/db')
session = self.Session()
self.assertTrue(session.bind is None)
session.close()
db.configure_session_factory(self.config, self.Session)
session = self.Session()
self.assertTrue(isinstance(session.bind, Engine))
self.assertEqual(unicode(session.bind.url), u'sqlite:////a/very/custom/db')
session.close()
def test_global_session_is_configured_by_default(self):
self.config.set(u'rattail.db', u'sqlalchemy.url', u'sqlite:////path/to/rattail.sqlite')
session = db.Session()
self.assertTrue(session.bind is None)
session.close()
db.configure_session_factory(self.config)
session = db.Session()
self.assertTrue(isinstance(session.bind, Engine))
self.assertEqual(unicode(session.bind.url), u'sqlite:////path/to/rattail.sqlite')
session.close()
# Must undo that configuration, this thing is global.
db.Session.configure(bind=None)
@patch(u'rattail.db.changes.record_changes')
def test_changes_will_not_be_recorded_by_default(self, record_changes):
self.config.set(u'rattail.db', u'sqlalchemy.url', u'sqlite://')
db.configure_session_factory(self.config, self.Session)
self.assertFalse(record_changes.called)
@patch(u'rattail.db.changes.record_changes')
def test_changes_will_be_recorded_by_so_configured(self, record_changes):
self.config.set(u'rattail.db', u'sqlalchemy.url', u'sqlite://')
self.config.set(u'rattail.db', u'changes.record', u'true')
db.configure_session_factory(self.config, self.Session)
# Role changes are ignored by default.
record_changes.assert_called_once_with(self.Session, True)
@patch(u'rattail.db.changes.record_changes')
def test_changes_will_still_be_recorded_with_deprecated_config(self, record_changes):
self.config.set(u'rattail.db', u'sqlalchemy.url', u'sqlite://')
self.config.set(u'rattail.db', u'changes.record', u'true')
db.configure_session_factory(self.config, self.Session)
# Role changes are ignored by default.
record_changes.assert_called_once_with(self.Session, True)
@patch(u'rattail.db.changes.record_changes')
def test_config_determines_if_role_changes_are_ignored(self, record_changes):
self.config.set(u'rattail.db', u'sqlalchemy.url', u'sqlite://')
self.config.set(u'rattail.db', u'changes.record', u'true')
self.config.set(u'rattail.db', u'changes.ignore_roles', u'false')
db.configure_session_factory(self.config, self.Session)
# Role changes are ignored by default; False means config works.
record_changes.assert_called_once_with(self.Session, False)
|