feat: add basic configure view for appinfo
This commit is contained in:
parent
dd207a4a05
commit
ed67cdb2d8
15 changed files with 847 additions and 42 deletions
|
@ -8,6 +8,17 @@ from pyramid.config import Configurator
|
|||
from pyramid.router import Router
|
||||
|
||||
from wuttaweb import app as mod
|
||||
from wuttjamaican.conf import WuttaConfig
|
||||
|
||||
|
||||
class TestWebAppProvider(TestCase):
|
||||
|
||||
def test_basic(self):
|
||||
# nb. just normal usage here, confirm it does the one thing we
|
||||
# need it to..
|
||||
config = WuttaConfig()
|
||||
app = config.get_app()
|
||||
handler = app.get_web_handler()
|
||||
|
||||
|
||||
class TestMakeWuttaConfig(FileConfigTestCase):
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
from unittest import TestCase
|
||||
from unittest.mock import patch
|
||||
|
||||
from pyramid import testing
|
||||
|
||||
|
@ -290,3 +291,45 @@ class TestGetFormData(TestCase):
|
|||
request = self.make_request(POST=None, content_type='application/json')
|
||||
data = util.get_form_data(request)
|
||||
self.assertEqual(data, {'foo2': 'baz'})
|
||||
|
||||
|
||||
class TestGetCsrfToken(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.config = WuttaConfig()
|
||||
self.request = testing.DummyRequest(wutta_config=self.config)
|
||||
|
||||
def test_same_token(self):
|
||||
|
||||
# same token returned for same request
|
||||
# TODO: dummy request is always returning same token!
|
||||
# so this isn't really testing anything.. :(
|
||||
first = util.get_csrf_token(self.request)
|
||||
self.assertIsNotNone(first)
|
||||
second = util.get_csrf_token(self.request)
|
||||
self.assertEqual(first, second)
|
||||
|
||||
# TODO: ideally would make a new request here and confirm it
|
||||
# gets a different token, but see note above..
|
||||
|
||||
def test_new_token(self):
|
||||
|
||||
# nb. dummy request always returns same token, so must
|
||||
# trick it into thinking it doesn't have one yet
|
||||
with patch.object(self.request.session, 'get_csrf_token', return_value=None):
|
||||
token = util.get_csrf_token(self.request)
|
||||
self.assertIsNotNone(token)
|
||||
|
||||
|
||||
class TestRenderCsrfToken(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.config = WuttaConfig()
|
||||
self.request = testing.DummyRequest(wutta_config=self.config)
|
||||
|
||||
def test_basics(self):
|
||||
html = util.render_csrf_token(self.request)
|
||||
self.assertIn('type="hidden"', html)
|
||||
self.assertIn('name="_csrf"', html)
|
||||
token = util.get_csrf_token(self.request)
|
||||
self.assertIn(f'value="{token}"', html)
|
||||
|
|
|
@ -1,39 +1,21 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
import functools
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from pyramid import testing
|
||||
from pyramid.response import Response
|
||||
from pyramid.httpexceptions import HTTPFound
|
||||
|
||||
from wuttjamaican.conf import WuttaConfig
|
||||
from wuttaweb.views import master
|
||||
from wuttaweb.subscribers import new_request_set_user
|
||||
|
||||
from tests.views.utils import WebTestCase
|
||||
|
||||
class TestMasterView(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.config = WuttaConfig(defaults={
|
||||
'wutta.web.menus.handler_spec': 'tests.utils:NullMenuHandler',
|
||||
})
|
||||
self.app = self.config.get_app()
|
||||
self.request = testing.DummyRequest(wutta_config=self.config, use_oruga=False)
|
||||
self.pyramid_config = testing.setUp(request=self.request, settings={
|
||||
'wutta_config': self.config,
|
||||
'mako.directories': ['wuttaweb:templates'],
|
||||
})
|
||||
self.pyramid_config.include('pyramid_mako')
|
||||
self.pyramid_config.include('wuttaweb.static')
|
||||
self.pyramid_config.include('wuttaweb.views.essential')
|
||||
self.pyramid_config.add_subscriber('wuttaweb.subscribers.before_render',
|
||||
'pyramid.events.BeforeRender')
|
||||
|
||||
event = MagicMock(request=self.request)
|
||||
new_request_set_user(event)
|
||||
|
||||
def tearDown(self):
|
||||
testing.tearDown()
|
||||
class TestMasterView(WebTestCase):
|
||||
|
||||
def test_defaults(self):
|
||||
master.MasterView.model_name = 'Widget'
|
||||
|
@ -233,6 +215,37 @@ class TestMasterView(TestCase):
|
|||
self.assertEqual(master.MasterView.get_template_prefix(), '/machines')
|
||||
del master.MasterView.model_class
|
||||
|
||||
def test_get_config_title(self):
|
||||
|
||||
# error by default (since no model class)
|
||||
self.assertRaises(AttributeError, master.MasterView.get_config_title)
|
||||
|
||||
# subclass may specify config title
|
||||
master.MasterView.config_title = 'Widgets'
|
||||
self.assertEqual(master.MasterView.get_config_title(), "Widgets")
|
||||
del master.MasterView.config_title
|
||||
|
||||
# subclass may specify *plural* model title
|
||||
master.MasterView.model_title_plural = 'People'
|
||||
self.assertEqual(master.MasterView.get_config_title(), "People")
|
||||
del master.MasterView.model_title_plural
|
||||
|
||||
# or it may specify *singular* model title
|
||||
master.MasterView.model_title = 'Wutta Widget'
|
||||
self.assertEqual(master.MasterView.get_config_title(), "Wutta Widgets")
|
||||
del master.MasterView.model_title
|
||||
|
||||
# or it may specify model name
|
||||
master.MasterView.model_name = 'Blaster'
|
||||
self.assertEqual(master.MasterView.get_config_title(), "Blasters")
|
||||
del master.MasterView.model_name
|
||||
|
||||
# or it may specify model class
|
||||
MyModel = MagicMock(__name__='Dinosaur')
|
||||
master.MasterView.model_class = MyModel
|
||||
self.assertEqual(master.MasterView.get_config_title(), "Dinosaurs")
|
||||
del master.MasterView.model_class
|
||||
|
||||
##############################
|
||||
# support methods
|
||||
##############################
|
||||
|
@ -245,6 +258,10 @@ class TestMasterView(TestCase):
|
|||
|
||||
def test_render_to_response(self):
|
||||
|
||||
def widgets(request): return {}
|
||||
self.pyramid_config.add_route('widgets', '/widgets/')
|
||||
self.pyramid_config.add_view(widgets, route_name='widgets')
|
||||
|
||||
# basic sanity check using /master/index.mako
|
||||
# (nb. it skips /widgets/index.mako since that doesn't exist)
|
||||
master.MasterView.model_name = 'Widget'
|
||||
|
@ -255,12 +272,14 @@ class TestMasterView(TestCase):
|
|||
|
||||
# basic sanity check using /appinfo/index.mako
|
||||
master.MasterView.model_name = 'AppInfo'
|
||||
master.MasterView.template_prefix = '/appinfo'
|
||||
master.MasterView.route_prefix = 'appinfo'
|
||||
master.MasterView.url_prefix = '/appinfo'
|
||||
view = master.MasterView(self.request)
|
||||
response = view.render_to_response('index', {})
|
||||
self.assertIsInstance(response, Response)
|
||||
del master.MasterView.model_name
|
||||
del master.MasterView.template_prefix
|
||||
del master.MasterView.route_prefix
|
||||
del master.MasterView.url_prefix
|
||||
|
||||
# bad template name causes error
|
||||
master.MasterView.model_name = 'Widget'
|
||||
|
@ -275,8 +294,77 @@ class TestMasterView(TestCase):
|
|||
|
||||
# basic sanity check using /appinfo
|
||||
master.MasterView.model_name = 'AppInfo'
|
||||
master.MasterView.route_prefix = 'appinfo'
|
||||
master.MasterView.template_prefix = '/appinfo'
|
||||
view = master.MasterView(self.request)
|
||||
response = view.index()
|
||||
del master.MasterView.model_name
|
||||
del master.MasterView.route_prefix
|
||||
del master.MasterView.template_prefix
|
||||
|
||||
def test_configure(self):
|
||||
model = self.app.model
|
||||
|
||||
# setup
|
||||
master.MasterView.model_name = 'AppInfo'
|
||||
master.MasterView.route_prefix = 'appinfo'
|
||||
master.MasterView.template_prefix = '/appinfo'
|
||||
|
||||
# mock settings
|
||||
settings = [
|
||||
{'name': 'wutta.app_title'},
|
||||
{'name': 'wutta.foo', 'value': 'bar'},
|
||||
{'name': 'wutta.flag', 'type': bool},
|
||||
{'name': 'wutta.number', 'type': int, 'default': 42},
|
||||
{'name': 'wutta.value1', 'save_if_empty': True},
|
||||
{'name': 'wutta.value2', 'save_if_empty': False},
|
||||
]
|
||||
|
||||
view = master.MasterView(self.request)
|
||||
with patch.object(self.request, 'current_route_url',
|
||||
return_value='/appinfo/configure'):
|
||||
with patch.object(master.MasterView, 'configure_get_simple_settings',
|
||||
return_value=settings):
|
||||
with patch.object(master, 'Session', return_value=self.session):
|
||||
|
||||
# get the form page
|
||||
response = view.configure()
|
||||
self.assertIsInstance(response, Response)
|
||||
|
||||
# post request to save settings
|
||||
self.request.method = 'POST'
|
||||
self.request.POST = {
|
||||
'wutta.app_title': 'Wutta',
|
||||
'wutta.foo': 'bar',
|
||||
'wutta.flag': 'true',
|
||||
}
|
||||
response = view.configure()
|
||||
# nb. should get redirect back to configure page
|
||||
self.assertIsInstance(response, HTTPFound)
|
||||
|
||||
# should now have 5 settings
|
||||
count = self.session.query(model.Setting).count()
|
||||
self.assertEqual(count, 5)
|
||||
get_setting = functools.partial(self.app.get_setting, self.session)
|
||||
self.assertEqual(get_setting('wutta.app_title'), 'Wutta')
|
||||
self.assertEqual(get_setting('wutta.foo'), 'bar')
|
||||
self.assertEqual(get_setting('wutta.flag'), 'true')
|
||||
self.assertEqual(get_setting('wutta.number'), '42')
|
||||
self.assertEqual(get_setting('wutta.value1'), '')
|
||||
self.assertEqual(get_setting('wutta.value2'), None)
|
||||
|
||||
# post request to remove settings
|
||||
self.request.method = 'POST'
|
||||
self.request.POST = {'remove_settings': '1'}
|
||||
response = view.configure()
|
||||
# nb. should get redirect back to configure page
|
||||
self.assertIsInstance(response, HTTPFound)
|
||||
|
||||
# should now have 0 settings
|
||||
count = self.session.query(model.Setting).count()
|
||||
self.assertEqual(count, 0)
|
||||
|
||||
# teardown
|
||||
del master.MasterView.model_name
|
||||
del master.MasterView.route_prefix
|
||||
del master.MasterView.template_prefix
|
||||
|
|
|
@ -11,3 +11,8 @@ class TestAppInfoView(WebTestCase):
|
|||
# just a sanity check
|
||||
view = settings.AppInfoView(self.request)
|
||||
response = view.index()
|
||||
|
||||
def test_configure_get_simple_settings(self):
|
||||
# just a sanity check
|
||||
view = settings.AppInfoView(self.request)
|
||||
simple = view.configure_get_simple_settings()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue