2024-08-05 19:21:58 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
|
2024-08-24 11:29:52 -05:00
|
|
|
import datetime
|
2024-08-23 19:23:40 -05:00
|
|
|
import decimal
|
2024-08-06 18:52:54 -05:00
|
|
|
import functools
|
2024-08-05 19:21:58 -05:00
|
|
|
from unittest import TestCase
|
2024-08-06 18:52:54 -05:00
|
|
|
from unittest.mock import MagicMock, patch
|
2024-08-05 19:21:58 -05:00
|
|
|
|
2024-08-18 14:05:10 -05:00
|
|
|
from sqlalchemy import orm
|
2024-08-05 19:21:58 -05:00
|
|
|
from pyramid import testing
|
|
|
|
from pyramid.response import Response
|
2024-08-15 02:10:08 -05:00
|
|
|
from pyramid.httpexceptions import HTTPNotFound
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
from wuttjamaican.conf import WuttaConfig
|
2024-08-21 11:46:38 -05:00
|
|
|
from wuttaweb.views import master as mod
|
2024-08-15 20:51:36 -05:00
|
|
|
from wuttaweb.views import View
|
2024-08-24 19:28:13 -05:00
|
|
|
from wuttaweb.progress import SessionProgress
|
2024-08-05 19:21:58 -05:00
|
|
|
from wuttaweb.subscribers import new_request_set_user
|
2024-08-12 21:17:08 -05:00
|
|
|
from tests.util import WebTestCase
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
|
2024-08-06 18:52:54 -05:00
|
|
|
class TestMasterView(WebTestCase):
|
2024-08-05 19:21:58 -05:00
|
|
|
|
2024-08-13 16:29:34 -05:00
|
|
|
def make_view(self):
|
2024-08-21 11:46:38 -05:00
|
|
|
return mod.MasterView(self.request)
|
2024-08-13 16:29:34 -05:00
|
|
|
|
2024-08-05 19:21:58 -05:00
|
|
|
def test_defaults(self):
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-10 21:07:38 -05:00
|
|
|
model_name='Widget',
|
2024-08-14 16:58:08 -05:00
|
|
|
model_key='uuid',
|
2024-08-24 14:26:13 -05:00
|
|
|
deletable_bulk=True,
|
2024-08-21 11:46:38 -05:00
|
|
|
has_autocomplete=True,
|
2024-08-25 12:20:28 -05:00
|
|
|
downloadable=True,
|
|
|
|
executable=True,
|
2024-08-14 16:58:08 -05:00
|
|
|
configurable=True):
|
2024-08-21 11:46:38 -05:00
|
|
|
mod.MasterView.defaults(self.pyramid_config)
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
##############################
|
|
|
|
# class methods
|
|
|
|
##############################
|
|
|
|
|
|
|
|
def test_get_model_class(self):
|
|
|
|
|
|
|
|
# no model class by default
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertIsNone(mod.MasterView.get_model_class())
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# subclass may specify
|
|
|
|
MyModel = MagicMock()
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 18:21:02 -05:00
|
|
|
model_class=MyModel):
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertIs(mod.MasterView.get_model_class(), MyModel)
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
def test_get_model_name(self):
|
|
|
|
|
|
|
|
# error by default (since no model class)
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertRaises(AttributeError, mod.MasterView.get_model_name)
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# subclass may specify model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name='Widget'):
|
|
|
|
self.assertEqual(mod.MasterView.get_model_name(), 'Widget')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify model class
|
|
|
|
MyModel = MagicMock(__name__='Blaster')
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 18:21:02 -05:00
|
|
|
model_class=MyModel):
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertEqual(mod.MasterView.get_model_name(), 'Blaster')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
def test_get_model_name_normalized(self):
|
|
|
|
|
|
|
|
# error by default (since no model class)
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertRaises(AttributeError, mod.MasterView.get_model_name_normalized)
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# subclass may specify *normalized* model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name_normalized='widget'):
|
|
|
|
self.assertEqual(mod.MasterView.get_model_name_normalized(), 'widget')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify *standard* model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name='Blaster'):
|
|
|
|
self.assertEqual(mod.MasterView.get_model_name_normalized(), 'blaster')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify model class
|
|
|
|
MyModel = MagicMock(__name__='Dinosaur')
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 18:21:02 -05:00
|
|
|
model_class=MyModel):
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertEqual(mod.MasterView.get_model_name_normalized(), 'dinosaur')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
def test_get_model_title(self):
|
|
|
|
|
|
|
|
# error by default (since no model class)
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertRaises(AttributeError, mod.MasterView.get_model_title)
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# subclass may specify model title
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_title='Wutta Widget'):
|
|
|
|
self.assertEqual(mod.MasterView.get_model_title(), "Wutta Widget")
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name='Blaster'):
|
|
|
|
self.assertEqual(mod.MasterView.get_model_title(), "Blaster")
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify model class
|
|
|
|
MyModel = MagicMock(__name__='Dinosaur')
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 18:21:02 -05:00
|
|
|
model_class=MyModel):
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertEqual(mod.MasterView.get_model_title(), "Dinosaur")
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
def test_get_model_title_plural(self):
|
|
|
|
|
|
|
|
# error by default (since no model class)
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertRaises(AttributeError, mod.MasterView.get_model_title_plural)
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# subclass may specify *plural* model title
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_title_plural='People'):
|
|
|
|
self.assertEqual(mod.MasterView.get_model_title_plural(), "People")
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify *singular* model title
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_title='Wutta Widget'):
|
|
|
|
self.assertEqual(mod.MasterView.get_model_title_plural(), "Wutta Widgets")
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name='Blaster'):
|
|
|
|
self.assertEqual(mod.MasterView.get_model_title_plural(), "Blasters")
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify model class
|
|
|
|
MyModel = MagicMock(__name__='Dinosaur')
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 18:21:02 -05:00
|
|
|
model_class=MyModel):
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertEqual(mod.MasterView.get_model_title_plural(), "Dinosaurs")
|
2024-08-05 19:21:58 -05:00
|
|
|
|
2024-08-07 19:47:24 -05:00
|
|
|
def test_get_model_key(self):
|
|
|
|
|
|
|
|
# error by default (since no model class)
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertRaises(AttributeError, mod.MasterView.get_model_key)
|
2024-08-07 19:47:24 -05:00
|
|
|
|
|
|
|
# subclass may specify model key
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_key='uuid'):
|
|
|
|
self.assertEqual(mod.MasterView.get_model_key(), ('uuid',))
|
2024-08-07 19:47:24 -05:00
|
|
|
|
2024-08-05 19:21:58 -05:00
|
|
|
def test_get_route_prefix(self):
|
|
|
|
|
|
|
|
# error by default (since no model class)
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertRaises(AttributeError, mod.MasterView.get_route_prefix)
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# subclass may specify route prefix
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
route_prefix='widgets'):
|
|
|
|
self.assertEqual(mod.MasterView.get_route_prefix(), 'widgets')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# subclass may specify *normalized* model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name_normalized='blaster'):
|
|
|
|
self.assertEqual(mod.MasterView.get_route_prefix(), 'blasters')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify *standard* model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name = 'Dinosaur'):
|
|
|
|
self.assertEqual(mod.MasterView.get_route_prefix(), 'dinosaurs')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify model class
|
|
|
|
MyModel = MagicMock(__name__='Truck')
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 18:21:02 -05:00
|
|
|
model_class=MyModel):
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertEqual(mod.MasterView.get_route_prefix(), 'trucks')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
2024-08-14 15:10:54 -05:00
|
|
|
def test_get_permission_prefix(self):
|
|
|
|
|
|
|
|
# error by default (since no model class)
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertRaises(AttributeError, mod.MasterView.get_permission_prefix)
|
2024-08-14 15:10:54 -05:00
|
|
|
|
|
|
|
# subclass may specify permission prefix
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.object(mod.MasterView, 'permission_prefix', new='widgets', create=True):
|
|
|
|
self.assertEqual(mod.MasterView.get_permission_prefix(), 'widgets')
|
2024-08-14 15:10:54 -05:00
|
|
|
|
|
|
|
# subclass may specify route prefix
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.object(mod.MasterView, 'route_prefix', new='widgets', create=True):
|
|
|
|
self.assertEqual(mod.MasterView.get_permission_prefix(), 'widgets')
|
2024-08-14 15:10:54 -05:00
|
|
|
|
|
|
|
# or it may specify model class
|
|
|
|
Truck = MagicMock(__name__='Truck')
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.object(mod.MasterView, 'model_class', new=Truck, create=True):
|
|
|
|
self.assertEqual(mod.MasterView.get_permission_prefix(), 'trucks')
|
2024-08-14 15:10:54 -05:00
|
|
|
|
2024-08-05 19:21:58 -05:00
|
|
|
def test_get_url_prefix(self):
|
|
|
|
|
|
|
|
# error by default (since no model class)
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertRaises(AttributeError, mod.MasterView.get_url_prefix)
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# subclass may specify url prefix
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
url_prefix='/widgets'):
|
|
|
|
self.assertEqual(mod.MasterView.get_url_prefix(), '/widgets')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify route prefix
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
route_prefix='trucks'):
|
|
|
|
self.assertEqual(mod.MasterView.get_url_prefix(), '/trucks')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify *normalized* model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name_normalized='blaster'):
|
|
|
|
self.assertEqual(mod.MasterView.get_url_prefix(), '/blasters')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify *standard* model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name='Dinosaur'):
|
|
|
|
self.assertEqual(mod.MasterView.get_url_prefix(), '/dinosaurs')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify model class
|
|
|
|
MyModel = MagicMock(__name__='Machine')
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 18:21:02 -05:00
|
|
|
model_class=MyModel):
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertEqual(mod.MasterView.get_url_prefix(), '/machines')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
2024-08-07 19:47:24 -05:00
|
|
|
def test_get_instance_url_prefix(self):
|
|
|
|
|
|
|
|
# error by default (since no model class)
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertRaises(AttributeError, mod.MasterView.get_instance_url_prefix)
|
2024-08-07 19:47:24 -05:00
|
|
|
|
|
|
|
# typical example with url_prefix and simple key
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
url_prefix='/widgets',
|
|
|
|
model_key='uuid'):
|
|
|
|
self.assertEqual(mod.MasterView.get_instance_url_prefix(), '/widgets/{uuid}')
|
2024-08-07 19:47:24 -05:00
|
|
|
|
|
|
|
# typical example with composite key
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
url_prefix='/widgets',
|
|
|
|
model_key=('foo', 'bar')):
|
|
|
|
self.assertEqual(mod.MasterView.get_instance_url_prefix(), '/widgets/{foo}|{bar}')
|
2024-08-07 19:47:24 -05:00
|
|
|
|
2024-08-05 19:21:58 -05:00
|
|
|
def test_get_template_prefix(self):
|
|
|
|
|
|
|
|
# error by default (since no model class)
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertRaises(AttributeError, mod.MasterView.get_template_prefix)
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# subclass may specify template prefix
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
template_prefix='/widgets'):
|
|
|
|
self.assertEqual(mod.MasterView.get_template_prefix(), '/widgets')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify url prefix
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
url_prefix='/trees'):
|
|
|
|
self.assertEqual(mod.MasterView.get_template_prefix(), '/trees')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify route prefix
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
route_prefix='trucks'):
|
|
|
|
self.assertEqual(mod.MasterView.get_template_prefix(), '/trucks')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify *normalized* model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name_normalized='blaster'):
|
|
|
|
self.assertEqual(mod.MasterView.get_template_prefix(), '/blasters')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify *standard* model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name='Dinosaur'):
|
|
|
|
self.assertEqual(mod.MasterView.get_template_prefix(), '/dinosaurs')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# or it may specify model class
|
|
|
|
MyModel = MagicMock(__name__='Machine')
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 18:21:02 -05:00
|
|
|
model_class=MyModel):
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertEqual(mod.MasterView.get_template_prefix(), '/machines')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
2024-08-07 14:00:53 -05:00
|
|
|
def test_get_grid_key(self):
|
|
|
|
|
|
|
|
# error by default (since no model class)
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertRaises(AttributeError, mod.MasterView.get_grid_key)
|
2024-08-07 14:00:53 -05:00
|
|
|
|
|
|
|
# subclass may specify grid key
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
grid_key='widgets'):
|
|
|
|
self.assertEqual(mod.MasterView.get_grid_key(), 'widgets')
|
2024-08-07 14:00:53 -05:00
|
|
|
|
|
|
|
# or it may specify route prefix
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
route_prefix='trucks'):
|
|
|
|
self.assertEqual(mod.MasterView.get_grid_key(), 'trucks')
|
2024-08-07 14:00:53 -05:00
|
|
|
|
|
|
|
# or it may specify *normalized* model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name_normalized='blaster'):
|
|
|
|
self.assertEqual(mod.MasterView.get_grid_key(), 'blasters')
|
2024-08-07 14:00:53 -05:00
|
|
|
|
|
|
|
# or it may specify *standard* model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name='Dinosaur'):
|
|
|
|
self.assertEqual(mod.MasterView.get_grid_key(), 'dinosaurs')
|
2024-08-07 14:00:53 -05:00
|
|
|
|
|
|
|
# or it may specify model class
|
|
|
|
MyModel = MagicMock(__name__='Machine')
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 18:21:02 -05:00
|
|
|
model_class=MyModel):
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertEqual(mod.MasterView.get_grid_key(), 'machines')
|
2024-08-07 14:00:53 -05:00
|
|
|
|
2024-08-06 18:52:54 -05:00
|
|
|
def test_get_config_title(self):
|
|
|
|
|
|
|
|
# error by default (since no model class)
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertRaises(AttributeError, mod.MasterView.get_config_title)
|
2024-08-06 18:52:54 -05:00
|
|
|
|
|
|
|
# subclass may specify config title
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
config_title='Widgets'):
|
|
|
|
self.assertEqual(mod.MasterView.get_config_title(), "Widgets")
|
2024-08-06 18:52:54 -05:00
|
|
|
|
|
|
|
# subclass may specify *plural* model title
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_title_plural='People'):
|
|
|
|
self.assertEqual(mod.MasterView.get_config_title(), "People")
|
2024-08-06 18:52:54 -05:00
|
|
|
|
|
|
|
# or it may specify *singular* model title
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_title='Wutta Widget'):
|
|
|
|
self.assertEqual(mod.MasterView.get_config_title(), "Wutta Widgets")
|
2024-08-06 18:52:54 -05:00
|
|
|
|
|
|
|
# or it may specify model name
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name='Blaster'):
|
|
|
|
self.assertEqual(mod.MasterView.get_config_title(), "Blasters")
|
2024-08-06 18:52:54 -05:00
|
|
|
|
|
|
|
# or it may specify model class
|
|
|
|
MyModel = MagicMock(__name__='Dinosaur')
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 18:21:02 -05:00
|
|
|
model_class=MyModel):
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertEqual(mod.MasterView.get_config_title(), "Dinosaurs")
|
2024-08-06 18:52:54 -05:00
|
|
|
|
2024-08-05 19:21:58 -05:00
|
|
|
##############################
|
|
|
|
# support methods
|
|
|
|
##############################
|
|
|
|
|
2024-08-15 20:51:36 -05:00
|
|
|
def test_get_class_hierarchy(self):
|
2024-08-21 11:46:38 -05:00
|
|
|
class MyView(mod.MasterView):
|
2024-08-15 20:51:36 -05:00
|
|
|
pass
|
|
|
|
|
|
|
|
view = MyView(self.request)
|
|
|
|
classes = view.get_class_hierarchy()
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertEqual(classes, [View, mod.MasterView, MyView])
|
2024-08-15 20:51:36 -05:00
|
|
|
|
2024-08-14 21:20:00 -05:00
|
|
|
def test_has_perm(self):
|
|
|
|
model = self.app.model
|
|
|
|
auth = self.app.get_auth_handler()
|
|
|
|
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-14 21:20:00 -05:00
|
|
|
model_name='Setting'):
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# anonymous user
|
|
|
|
self.assertFalse(view.has_perm('list'))
|
|
|
|
self.assertFalse(self.request.has_perm('list'))
|
|
|
|
|
|
|
|
# reset
|
|
|
|
del self.request.user_permissions
|
|
|
|
|
|
|
|
# make user with perms
|
|
|
|
barney = model.User(username='barney')
|
|
|
|
self.session.add(barney)
|
|
|
|
blokes = model.Role(name="Blokes")
|
|
|
|
self.session.add(blokes)
|
|
|
|
barney.roles.append(blokes)
|
|
|
|
auth.grant_permission(blokes, 'settings.list')
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
# this user has perms
|
|
|
|
self.request.user = barney
|
|
|
|
self.assertTrue(view.has_perm('list'))
|
|
|
|
self.assertTrue(self.request.has_perm('settings.list'))
|
|
|
|
|
|
|
|
def test_has_any_perm(self):
|
|
|
|
model = self.app.model
|
|
|
|
auth = self.app.get_auth_handler()
|
|
|
|
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-14 21:20:00 -05:00
|
|
|
model_name='Setting'):
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# anonymous user
|
|
|
|
self.assertFalse(view.has_any_perm('list', 'view'))
|
|
|
|
self.assertFalse(self.request.has_any_perm('settings.list', 'settings.view'))
|
|
|
|
|
|
|
|
# reset
|
|
|
|
del self.request.user_permissions
|
|
|
|
|
|
|
|
# make user with perms
|
|
|
|
barney = model.User(username='barney')
|
|
|
|
self.session.add(barney)
|
|
|
|
blokes = model.Role(name="Blokes")
|
|
|
|
self.session.add(blokes)
|
|
|
|
barney.roles.append(blokes)
|
|
|
|
auth.grant_permission(blokes, 'settings.view')
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
# this user has perms
|
|
|
|
self.request.user = barney
|
|
|
|
self.assertTrue(view.has_any_perm('list', 'view'))
|
|
|
|
self.assertTrue(self.request.has_any_perm('settings.list', 'settings.view'))
|
|
|
|
|
2024-08-24 14:26:13 -05:00
|
|
|
def test_make_button(self):
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# normal
|
|
|
|
html = view.make_button('click me')
|
|
|
|
self.assertIn('<b-button ', html)
|
|
|
|
self.assertIn('click me', html)
|
|
|
|
self.assertNotIn('is-primary', html)
|
|
|
|
|
|
|
|
# primary as primary
|
|
|
|
html = view.make_button('click me', primary=True)
|
|
|
|
self.assertIn('<b-button ', html)
|
|
|
|
self.assertIn('click me', html)
|
|
|
|
self.assertIn('is-primary', html)
|
|
|
|
|
|
|
|
# primary as variant
|
|
|
|
html = view.make_button('click me', variant='is-primary')
|
|
|
|
self.assertIn('<b-button ', html)
|
|
|
|
self.assertIn('click me', html)
|
|
|
|
self.assertIn('is-primary', html)
|
|
|
|
|
|
|
|
# primary as type
|
|
|
|
html = view.make_button('click me', type='is-primary')
|
|
|
|
self.assertIn('<b-button ', html)
|
|
|
|
self.assertIn('click me', html)
|
|
|
|
self.assertIn('is-primary', html)
|
|
|
|
|
2024-08-24 19:28:13 -05:00
|
|
|
def test_make_progress(self):
|
|
|
|
|
|
|
|
# basic
|
|
|
|
view = self.make_view()
|
|
|
|
self.request.session.id = 'mockid'
|
|
|
|
progress = view.make_progress('foo')
|
|
|
|
self.assertIsInstance(progress, SessionProgress)
|
|
|
|
|
|
|
|
def test_render_progress(self):
|
|
|
|
self.pyramid_config.add_route('progress', '/progress/{key}')
|
|
|
|
|
|
|
|
# sanity / coverage check
|
|
|
|
view = self.make_view()
|
|
|
|
progress = MagicMock()
|
|
|
|
response = view.render_progress(progress)
|
|
|
|
|
2024-08-05 19:21:58 -05:00
|
|
|
def test_render_to_response(self):
|
2024-08-14 15:10:54 -05:00
|
|
|
self.pyramid_config.include('wuttaweb.views.common')
|
|
|
|
self.pyramid_config.include('wuttaweb.views.auth')
|
|
|
|
self.pyramid_config.add_route('appinfo', '/appinfo/')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
2024-08-06 18:52:54 -05:00
|
|
|
def widgets(request): return {}
|
|
|
|
self.pyramid_config.add_route('widgets', '/widgets/')
|
|
|
|
self.pyramid_config.add_view(widgets, route_name='widgets')
|
|
|
|
|
2024-08-05 19:21:58 -05:00
|
|
|
# basic sanity check using /master/index.mako
|
|
|
|
# (nb. it skips /widgets/index.mako since that doesn't exist)
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 12:41:22 -05:00
|
|
|
model_name='Widget',
|
|
|
|
creatable=False):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-11 12:41:22 -05:00
|
|
|
response = view.render_to_response('index', {})
|
|
|
|
self.assertIsInstance(response, Response)
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# basic sanity check using /appinfo/index.mako
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 12:41:22 -05:00
|
|
|
model_name='AppInfo',
|
|
|
|
route_prefix='appinfo',
|
|
|
|
url_prefix='/appinfo',
|
|
|
|
creatable=False):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-20 21:26:38 -05:00
|
|
|
response = view.render_to_response('index', {
|
|
|
|
# nb. grid is required for this template
|
|
|
|
'grid': MagicMock(),
|
|
|
|
})
|
2024-08-11 12:41:22 -05:00
|
|
|
self.assertIsInstance(response, Response)
|
2024-08-05 19:21:58 -05:00
|
|
|
|
|
|
|
# bad template name causes error
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_name='Widget'):
|
|
|
|
self.assertRaises(IOError, view.render_to_response, 'nonexistent', {})
|
2024-08-05 19:21:58 -05:00
|
|
|
|
2024-08-07 19:47:24 -05:00
|
|
|
def test_get_index_title(self):
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_title_plural = "Wutta Widgets"):
|
|
|
|
view = mod.MasterView(self.request)
|
|
|
|
self.assertEqual(view.get_index_title(), "Wutta Widgets")
|
2024-08-07 19:47:24 -05:00
|
|
|
|
2024-08-15 20:51:36 -05:00
|
|
|
def test_collect_labels(self):
|
|
|
|
|
|
|
|
# no labels by default
|
|
|
|
view = self.make_view()
|
|
|
|
labels = view.collect_labels()
|
|
|
|
self.assertEqual(labels, {})
|
|
|
|
|
|
|
|
# labels come from all classes; subclass wins
|
|
|
|
with patch.object(View, 'labels', new={'foo': "Foo", 'bar': "Bar"}, create=True):
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.object(mod.MasterView, 'labels', new={'foo': "FOO FIGHTERS"}, create=True):
|
2024-08-15 20:51:36 -05:00
|
|
|
view = self.make_view()
|
|
|
|
labels = view.collect_labels()
|
|
|
|
self.assertEqual(labels, {'foo': "FOO FIGHTERS", 'bar': "Bar"})
|
|
|
|
|
|
|
|
def test_set_labels(self):
|
|
|
|
model = self.app.model
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.object(mod.MasterView, 'model_class', new=model.Setting, create=True):
|
2024-08-15 20:51:36 -05:00
|
|
|
|
|
|
|
# no labels by default
|
|
|
|
view = self.make_view()
|
|
|
|
grid = view.make_model_grid(session=self.session)
|
|
|
|
view.set_labels(grid)
|
|
|
|
self.assertEqual(grid.labels, {})
|
|
|
|
|
|
|
|
# labels come from all classes; subclass wins
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.object(mod.MasterView, 'labels', new={'name': "SETTING NAME"}, create=True):
|
2024-08-15 20:51:36 -05:00
|
|
|
view = self.make_view()
|
|
|
|
view.set_labels(grid)
|
|
|
|
self.assertEqual(grid.labels, {'name': "SETTING NAME"})
|
|
|
|
|
2024-08-11 16:52:13 -05:00
|
|
|
def test_make_model_grid(self):
|
2024-08-24 14:26:13 -05:00
|
|
|
self.pyramid_config.add_route('settings.delete_bulk', '/settings/delete-bulk')
|
2024-08-11 16:52:13 -05:00
|
|
|
model = self.app.model
|
|
|
|
|
|
|
|
# no model class
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 16:52:13 -05:00
|
|
|
model_name='Widget',
|
|
|
|
model_key='uuid'):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-11 16:52:13 -05:00
|
|
|
grid = view.make_model_grid()
|
|
|
|
self.assertIsNone(grid.model_class)
|
|
|
|
|
|
|
|
# explicit model class
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 16:52:13 -05:00
|
|
|
model_class=model.Setting):
|
|
|
|
grid = view.make_model_grid(session=self.session)
|
|
|
|
self.assertIs(grid.model_class, model.Setting)
|
|
|
|
|
2024-08-23 14:14:41 -05:00
|
|
|
# no row class by default
|
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_class=model.Setting):
|
|
|
|
grid = view.make_model_grid(session=self.session)
|
|
|
|
self.assertIsNone(grid.row_class)
|
|
|
|
|
|
|
|
# can specify row class
|
|
|
|
get_row_class = MagicMock()
|
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_class=model.Setting,
|
|
|
|
grid_row_class=get_row_class):
|
|
|
|
grid = view.make_model_grid(session=self.session)
|
|
|
|
self.assertIs(grid.row_class, get_row_class)
|
|
|
|
|
2024-08-14 21:20:00 -05:00
|
|
|
# no actions by default
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-14 21:20:00 -05:00
|
|
|
model_class=model.Setting):
|
|
|
|
grid = view.make_model_grid(session=self.session)
|
|
|
|
self.assertEqual(grid.actions, [])
|
|
|
|
|
|
|
|
# now let's test some more actions logic
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-14 21:20:00 -05:00
|
|
|
model_class=model.Setting,
|
|
|
|
viewable=True,
|
|
|
|
editable=True,
|
|
|
|
deletable=True):
|
|
|
|
|
|
|
|
# should have 3 actions now, but for lack of perms
|
|
|
|
grid = view.make_model_grid(session=self.session)
|
|
|
|
self.assertEqual(len(grid.actions), 0)
|
|
|
|
|
|
|
|
# but root user has perms, so gets 3 actions
|
|
|
|
with patch.object(self.request, 'is_root', new=True):
|
|
|
|
grid = view.make_model_grid(session=self.session)
|
|
|
|
self.assertEqual(len(grid.actions), 3)
|
|
|
|
|
2024-08-24 14:26:13 -05:00
|
|
|
# no tools by default
|
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_class=model.Setting):
|
|
|
|
grid = view.make_model_grid(session=self.session)
|
|
|
|
self.assertEqual(grid.tools, {})
|
|
|
|
|
|
|
|
# delete-results tool added if master/perms allow
|
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_class=model.Setting,
|
|
|
|
deletable_bulk=True):
|
|
|
|
with patch.object(self.request, 'is_root', new=True):
|
|
|
|
grid = view.make_model_grid(session=self.session)
|
|
|
|
self.assertIn('delete-results', grid.tools)
|
|
|
|
|
2024-08-11 18:21:02 -05:00
|
|
|
def test_get_grid_data(self):
|
|
|
|
model = self.app.model
|
|
|
|
self.app.save_setting(self.session, 'foo', 'bar')
|
|
|
|
self.session.commit()
|
2024-08-14 16:58:08 -05:00
|
|
|
setting = self.session.query(model.Setting).one()
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# empty by default
|
2024-08-21 11:46:38 -05:00
|
|
|
self.assertFalse(hasattr(mod.MasterView, 'model_class'))
|
2024-08-14 16:58:08 -05:00
|
|
|
data = view.get_grid_data(session=self.session)
|
|
|
|
self.assertEqual(data, [])
|
2024-08-11 18:21:02 -05:00
|
|
|
|
2024-08-18 14:05:10 -05:00
|
|
|
# grid with model class will produce data query
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 18:21:02 -05:00
|
|
|
model_class=model.Setting):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-18 14:05:10 -05:00
|
|
|
query = view.get_grid_data(session=self.session)
|
|
|
|
self.assertIsInstance(query, orm.Query)
|
|
|
|
data = query.all()
|
2024-08-11 18:21:02 -05:00
|
|
|
self.assertEqual(len(data), 1)
|
2024-08-14 16:58:08 -05:00
|
|
|
self.assertIs(data[0], setting)
|
2024-08-11 18:21:02 -05:00
|
|
|
|
2024-08-12 21:17:08 -05:00
|
|
|
def test_configure_grid(self):
|
|
|
|
model = self.app.model
|
|
|
|
|
|
|
|
# uuid field is pruned
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-12 21:17:08 -05:00
|
|
|
model_class=model.Setting):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-12 21:17:08 -05:00
|
|
|
grid = view.make_grid(model_class=model.Setting,
|
|
|
|
columns=['uuid', 'name', 'value'])
|
|
|
|
self.assertIn('uuid', grid.columns)
|
|
|
|
view.configure_grid(grid)
|
|
|
|
self.assertNotIn('uuid', grid.columns)
|
|
|
|
|
2024-08-23 19:23:40 -05:00
|
|
|
def test_grid_render_bool(self):
|
|
|
|
model = self.app.model
|
|
|
|
view = self.make_view()
|
|
|
|
user = model.User(username='barney', active=None)
|
|
|
|
|
|
|
|
# null
|
|
|
|
value = view.grid_render_bool(user, 'active', None)
|
|
|
|
self.assertIsNone(value)
|
|
|
|
|
|
|
|
# true
|
|
|
|
user.active = True
|
|
|
|
value = view.grid_render_bool(user, 'active', True)
|
|
|
|
self.assertEqual(value, "Yes")
|
|
|
|
|
|
|
|
# false
|
|
|
|
user.active = False
|
|
|
|
value = view.grid_render_bool(user, 'active', False)
|
|
|
|
self.assertEqual(value, "No")
|
|
|
|
|
|
|
|
def test_grid_render_currency(self):
|
|
|
|
view = self.make_view()
|
|
|
|
obj = {'amount': None}
|
|
|
|
|
|
|
|
# null
|
|
|
|
value = view.grid_render_currency(obj, 'amount', None)
|
|
|
|
self.assertIsNone(value)
|
|
|
|
|
|
|
|
# normal amount
|
|
|
|
obj['amount'] = decimal.Decimal('100.42')
|
|
|
|
value = view.grid_render_currency(obj, 'amount', '100.42')
|
|
|
|
self.assertEqual(value, "$100.42")
|
|
|
|
|
|
|
|
# negative amount
|
|
|
|
obj['amount'] = decimal.Decimal('-100.42')
|
|
|
|
value = view.grid_render_currency(obj, 'amount', '-100.42')
|
|
|
|
self.assertEqual(value, "($100.42)")
|
|
|
|
|
2024-08-24 11:29:52 -05:00
|
|
|
def test_grid_render_datetime(self):
|
|
|
|
view = self.make_view()
|
|
|
|
obj = {'dt': None}
|
|
|
|
|
|
|
|
# null
|
|
|
|
value = view.grid_render_datetime(obj, 'dt', None)
|
|
|
|
self.assertIsNone(value)
|
|
|
|
|
|
|
|
# normal
|
|
|
|
obj['dt'] = datetime.datetime(2024, 8, 24, 11)
|
|
|
|
value = view.grid_render_datetime(obj, 'dt', '2024-08-24T11:00:00')
|
|
|
|
self.assertEqual(value, '2024-08-24 11:00:00 AM')
|
|
|
|
|
|
|
|
def test_grid_render_enum(self):
|
|
|
|
enum = self.app.enum
|
|
|
|
view = self.make_view()
|
|
|
|
obj = {'status': None}
|
|
|
|
|
|
|
|
# null
|
|
|
|
value = view.grid_render_enum(obj, 'status', None, enum=enum.UpgradeStatus)
|
|
|
|
self.assertIsNone(value)
|
|
|
|
|
|
|
|
# normal
|
|
|
|
obj['status'] = enum.UpgradeStatus.SUCCESS
|
|
|
|
value = view.grid_render_enum(obj, 'status', 'SUCCESS', enum=enum.UpgradeStatus)
|
|
|
|
self.assertEqual(value, 'SUCCESS')
|
|
|
|
|
2024-08-13 16:29:34 -05:00
|
|
|
def test_grid_render_notes(self):
|
|
|
|
model = self.app.model
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# null
|
|
|
|
text = None
|
|
|
|
role = model.Role(name="Foo", notes=text)
|
|
|
|
value = view.grid_render_notes(role, 'notes', text)
|
|
|
|
self.assertIsNone(value)
|
|
|
|
|
|
|
|
# short string
|
|
|
|
text = "hello world"
|
|
|
|
role = model.Role(name="Foo", notes=text)
|
|
|
|
value = view.grid_render_notes(role, 'notes', text)
|
|
|
|
self.assertEqual(value, text)
|
|
|
|
|
|
|
|
# long string
|
|
|
|
text = "hello world " * 20
|
|
|
|
role = model.Role(name="Foo", notes=text)
|
|
|
|
value = view.grid_render_notes(role, 'notes', text)
|
|
|
|
self.assertIn('<span ', value)
|
|
|
|
|
2024-08-07 19:47:24 -05:00
|
|
|
def test_get_instance(self):
|
2024-08-11 16:52:13 -05:00
|
|
|
model = self.app.model
|
|
|
|
self.app.save_setting(self.session, 'foo', 'bar')
|
|
|
|
self.session.commit()
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 1)
|
|
|
|
|
|
|
|
# default not implemented
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-07 19:47:24 -05:00
|
|
|
self.assertRaises(NotImplementedError, view.get_instance)
|
|
|
|
|
2024-08-11 16:52:13 -05:00
|
|
|
# fetch from DB if model class is known
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 16:52:13 -05:00
|
|
|
model_class=model.Setting):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-11 16:52:13 -05:00
|
|
|
|
|
|
|
# existing setting is returned
|
|
|
|
self.request.matchdict = {'name': 'foo'}
|
|
|
|
setting = view.get_instance(session=self.session)
|
|
|
|
self.assertIsInstance(setting, model.Setting)
|
|
|
|
self.assertEqual(setting.name, 'foo')
|
|
|
|
self.assertEqual(setting.value, 'bar')
|
|
|
|
|
|
|
|
# missing setting not found
|
|
|
|
self.request.matchdict = {'name': 'blarg'}
|
|
|
|
self.assertRaises(HTTPNotFound, view.get_instance, session=self.session)
|
|
|
|
|
2024-08-14 21:20:00 -05:00
|
|
|
def test_get_action_url_view(self):
|
|
|
|
model = self.app.model
|
|
|
|
setting = model.Setting(name='foo', value='bar')
|
|
|
|
self.session.add(setting)
|
|
|
|
self.session.commit()
|
|
|
|
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-14 21:20:00 -05:00
|
|
|
model_class=model.Setting):
|
2024-08-21 11:46:38 -05:00
|
|
|
mod.MasterView.defaults(self.pyramid_config)
|
2024-08-14 21:20:00 -05:00
|
|
|
view = self.make_view()
|
|
|
|
url = view.get_action_url_view(setting, 0)
|
|
|
|
self.assertEqual(url, self.request.route_url('settings.view', name='foo'))
|
|
|
|
|
|
|
|
def test_get_action_url_edit(self):
|
|
|
|
model = self.app.model
|
|
|
|
setting = model.Setting(name='foo', value='bar')
|
|
|
|
self.session.add(setting)
|
|
|
|
self.session.commit()
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-14 21:20:00 -05:00
|
|
|
model_class=model.Setting):
|
2024-08-21 11:46:38 -05:00
|
|
|
mod.MasterView.defaults(self.pyramid_config)
|
2024-08-14 21:20:00 -05:00
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# typical
|
|
|
|
url = view.get_action_url_edit(setting, 0)
|
|
|
|
self.assertEqual(url, self.request.route_url('settings.edit', name='foo'))
|
|
|
|
|
|
|
|
# but null if instance not editable
|
|
|
|
with patch.object(view, 'is_editable', return_value=False):
|
|
|
|
url = view.get_action_url_edit(setting, 0)
|
|
|
|
self.assertIsNone(url)
|
|
|
|
|
|
|
|
def test_get_action_url_delete(self):
|
|
|
|
model = self.app.model
|
|
|
|
setting = model.Setting(name='foo', value='bar')
|
|
|
|
self.session.add(setting)
|
|
|
|
self.session.commit()
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-14 21:20:00 -05:00
|
|
|
model_class=model.Setting):
|
2024-08-21 11:46:38 -05:00
|
|
|
mod.MasterView.defaults(self.pyramid_config)
|
2024-08-14 21:20:00 -05:00
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# typical
|
|
|
|
url = view.get_action_url_delete(setting, 0)
|
|
|
|
self.assertEqual(url, self.request.route_url('settings.delete', name='foo'))
|
|
|
|
|
|
|
|
# but null if instance not deletable
|
|
|
|
with patch.object(view, 'is_deletable', return_value=False):
|
|
|
|
url = view.get_action_url_delete(setting, 0)
|
|
|
|
self.assertIsNone(url)
|
|
|
|
|
2024-08-11 16:52:13 -05:00
|
|
|
def test_make_model_form(self):
|
|
|
|
model = self.app.model
|
|
|
|
|
|
|
|
# no model class
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 16:52:13 -05:00
|
|
|
model_name='Widget',
|
|
|
|
model_key='uuid'):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-11 16:52:13 -05:00
|
|
|
form = view.make_model_form()
|
|
|
|
self.assertIsNone(form.model_class)
|
|
|
|
|
|
|
|
# explicit model class
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 16:52:13 -05:00
|
|
|
model_class=model.Setting):
|
|
|
|
form = view.make_model_form()
|
|
|
|
self.assertIs(form.model_class, model.Setting)
|
|
|
|
|
2024-08-11 18:21:02 -05:00
|
|
|
def test_configure_form(self):
|
|
|
|
model = self.app.model
|
|
|
|
|
|
|
|
# uuid field is pruned
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 18:21:02 -05:00
|
|
|
model_class=model.Setting):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-11 18:21:02 -05:00
|
|
|
form = view.make_form(model_class=model.Setting,
|
|
|
|
fields=['uuid', 'name', 'value'])
|
|
|
|
self.assertIn('uuid', form.fields)
|
|
|
|
view.configure_form(form)
|
|
|
|
self.assertNotIn('uuid', form.fields)
|
|
|
|
|
2024-08-11 16:52:13 -05:00
|
|
|
def test_objectify(self):
|
|
|
|
model = self.app.model
|
|
|
|
self.app.save_setting(self.session, 'foo', 'bar')
|
|
|
|
self.session.commit()
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 1)
|
|
|
|
|
|
|
|
# no model class
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 16:52:13 -05:00
|
|
|
model_name='Widget',
|
|
|
|
model_key='uuid'):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-12 21:17:08 -05:00
|
|
|
form = view.make_model_form(fields=['name', 'description'])
|
2024-08-11 16:52:13 -05:00
|
|
|
form.validated = {'name': 'first'}
|
|
|
|
obj = view.objectify(form)
|
|
|
|
self.assertIs(obj, form.validated)
|
|
|
|
|
|
|
|
# explicit model class (editing)
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 16:52:13 -05:00
|
|
|
model_class=model.Setting,
|
|
|
|
editing=True):
|
|
|
|
form = view.make_model_form()
|
|
|
|
form.validated = {'name': 'foo', 'value': 'blarg'}
|
|
|
|
form.model_instance = self.session.query(model.Setting).one()
|
|
|
|
obj = view.objectify(form)
|
|
|
|
self.assertIsInstance(obj, model.Setting)
|
|
|
|
self.assertEqual(obj.name, 'foo')
|
|
|
|
self.assertEqual(obj.value, 'blarg')
|
|
|
|
|
|
|
|
# explicit model class (creating)
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 16:52:13 -05:00
|
|
|
model_class=model.Setting,
|
|
|
|
creating=True):
|
|
|
|
form = view.make_model_form()
|
|
|
|
form.validated = {'name': 'another', 'value': 'whatever'}
|
|
|
|
obj = view.objectify(form)
|
|
|
|
self.assertIsInstance(obj, model.Setting)
|
|
|
|
self.assertEqual(obj.name, 'another')
|
|
|
|
self.assertEqual(obj.value, 'whatever')
|
|
|
|
|
|
|
|
def test_persist(self):
|
|
|
|
model = self.app.model
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 16:52:13 -05:00
|
|
|
model_class=model.Setting):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-11 16:52:13 -05:00
|
|
|
|
|
|
|
# new instance is persisted
|
|
|
|
setting = model.Setting(name='foo', value='bar')
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 0)
|
|
|
|
view.persist(setting, session=self.session)
|
|
|
|
self.session.commit()
|
|
|
|
setting = self.session.query(model.Setting).one()
|
|
|
|
self.assertEqual(setting.name, 'foo')
|
|
|
|
self.assertEqual(setting.value, 'bar')
|
|
|
|
|
2024-08-05 19:21:58 -05:00
|
|
|
##############################
|
|
|
|
# view methods
|
|
|
|
##############################
|
|
|
|
|
|
|
|
def test_index(self):
|
2024-08-14 15:10:54 -05:00
|
|
|
self.pyramid_config.include('wuttaweb.views.common')
|
|
|
|
self.pyramid_config.include('wuttaweb.views.auth')
|
|
|
|
self.pyramid_config.add_route('settings.create', '/settings/new')
|
|
|
|
self.pyramid_config.add_route('settings.view', '/settings/{name}')
|
|
|
|
self.pyramid_config.add_route('settings.edit', '/settings/{name}/edit')
|
|
|
|
self.pyramid_config.add_route('settings.delete', '/settings/{name}/delete')
|
2024-08-05 19:21:58 -05:00
|
|
|
|
2024-08-07 19:47:24 -05:00
|
|
|
# sanity/coverage check using /settings/
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-14 15:10:54 -05:00
|
|
|
model_name='Setting',
|
|
|
|
model_key='name',
|
|
|
|
get_index_url=MagicMock(return_value='/settings/'),
|
|
|
|
grid_columns=['name', 'value']):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-07 19:47:24 -05:00
|
|
|
response = view.index()
|
2024-08-14 15:10:54 -05:00
|
|
|
|
|
|
|
# then again with data, to include view action url
|
|
|
|
data = [{'name': 'foo', 'value': 'bar'}]
|
|
|
|
with patch.object(view, 'get_grid_data', return_value=data):
|
|
|
|
response = view.index()
|
2024-08-16 22:52:24 -05:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content_type, 'text/html')
|
|
|
|
|
|
|
|
# then once more as 'partial' - aka. data only
|
|
|
|
self.request.GET = {'partial': '1'}
|
|
|
|
response = view.index()
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content_type, 'application/json')
|
2024-08-07 19:47:24 -05:00
|
|
|
|
2024-08-22 13:50:29 -05:00
|
|
|
# redirects when view is reset
|
|
|
|
self.request.GET = {'reset-view': '1', 'hash': 'foo'}
|
|
|
|
with patch.object(self.request, 'current_route_url'):
|
|
|
|
response = view.index()
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
|
2024-08-11 12:41:22 -05:00
|
|
|
def test_create(self):
|
2024-08-14 15:10:54 -05:00
|
|
|
self.pyramid_config.include('wuttaweb.views.common')
|
|
|
|
self.pyramid_config.include('wuttaweb.views.auth')
|
|
|
|
self.pyramid_config.add_route('settings.view', '/settings/{name}')
|
2024-08-11 12:41:22 -05:00
|
|
|
model = self.app.model
|
|
|
|
|
|
|
|
# sanity/coverage check using /settings/new
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 12:41:22 -05:00
|
|
|
model_name='Setting',
|
|
|
|
model_key='name',
|
2024-08-14 15:10:54 -05:00
|
|
|
get_index_url=MagicMock(return_value='/settings/'),
|
2024-08-11 12:41:22 -05:00
|
|
|
form_fields=['name', 'value']):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-11 12:41:22 -05:00
|
|
|
|
|
|
|
# no setting yet
|
|
|
|
self.assertIsNone(self.app.get_setting(self.session, 'foo.bar'))
|
|
|
|
|
|
|
|
# get the form page
|
|
|
|
response = view.create()
|
|
|
|
self.assertIsInstance(response, Response)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
# self.assertIn('frazzle', response.text)
|
|
|
|
# nb. no error
|
|
|
|
self.assertNotIn('Required', response.text)
|
|
|
|
|
|
|
|
def persist(setting):
|
|
|
|
self.app.save_setting(self.session, setting['name'], setting['value'])
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
# post request to save setting
|
|
|
|
self.request.method = 'POST'
|
|
|
|
self.request.POST = {
|
|
|
|
'name': 'foo.bar',
|
|
|
|
'value': 'fraggle',
|
|
|
|
}
|
|
|
|
with patch.object(view, 'persist', new=persist):
|
|
|
|
response = view.create()
|
|
|
|
# nb. should get redirect back to view page
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
# setting should now be in DB
|
|
|
|
self.assertEqual(self.app.get_setting(self.session, 'foo.bar'), 'fraggle')
|
|
|
|
|
|
|
|
# try another post with invalid data (value is required)
|
|
|
|
self.request.method = 'POST'
|
|
|
|
self.request.POST = {}
|
|
|
|
with patch.object(view, 'persist', new=persist):
|
|
|
|
response = view.create()
|
|
|
|
# nb. should get a form with errors
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertIn('Required', response.text)
|
|
|
|
# setting did not change in DB
|
|
|
|
self.assertEqual(self.app.get_setting(self.session, 'foo.bar'), 'fraggle')
|
|
|
|
|
2024-08-07 19:47:24 -05:00
|
|
|
def test_view(self):
|
2024-08-14 15:10:54 -05:00
|
|
|
self.pyramid_config.include('wuttaweb.views.common')
|
|
|
|
self.pyramid_config.include('wuttaweb.views.auth')
|
|
|
|
self.pyramid_config.add_route('settings.create', '/settings/new')
|
|
|
|
self.pyramid_config.add_route('settings.edit', '/settings/{name}/edit')
|
|
|
|
self.pyramid_config.add_route('settings.delete', '/settings/{name}/delete')
|
2024-08-07 19:47:24 -05:00
|
|
|
|
|
|
|
# sanity/coverage check using /settings/XXX
|
|
|
|
setting = {'name': 'foo.bar', 'value': 'baz'}
|
|
|
|
self.request.matchdict = {'name': 'foo.bar'}
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-10 21:07:38 -05:00
|
|
|
model_name='Setting',
|
|
|
|
model_key='name',
|
2024-08-14 15:10:54 -05:00
|
|
|
get_index_url=MagicMock(return_value='/settings/'),
|
2024-08-10 21:07:38 -05:00
|
|
|
grid_columns=['name', 'value'],
|
|
|
|
form_fields=['name', 'value']):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-10 21:07:38 -05:00
|
|
|
with patch.object(view, 'get_instance', return_value=setting):
|
|
|
|
response = view.view()
|
|
|
|
|
|
|
|
def test_edit(self):
|
2024-08-14 15:10:54 -05:00
|
|
|
self.pyramid_config.include('wuttaweb.views.common')
|
|
|
|
self.pyramid_config.include('wuttaweb.views.auth')
|
|
|
|
self.pyramid_config.add_route('settings.create', '/settings/new')
|
|
|
|
self.pyramid_config.add_route('settings.view', '/settings/{name}')
|
|
|
|
self.pyramid_config.add_route('settings.delete', '/settings/{name}/delete')
|
2024-08-10 21:07:38 -05:00
|
|
|
model = self.app.model
|
|
|
|
self.app.save_setting(self.session, 'foo.bar', 'frazzle')
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
def get_instance():
|
|
|
|
setting = self.session.query(model.Setting).get('foo.bar')
|
|
|
|
return {
|
|
|
|
'name': setting.name,
|
|
|
|
'value': setting.value,
|
|
|
|
}
|
|
|
|
|
|
|
|
# sanity/coverage check using /settings/XXX/edit
|
|
|
|
self.request.matchdict = {'name': 'foo.bar'}
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-10 21:07:38 -05:00
|
|
|
model_name='Setting',
|
|
|
|
model_key='name',
|
2024-08-14 15:10:54 -05:00
|
|
|
get_index_url=MagicMock(return_value='/settings/'),
|
2024-08-10 21:07:38 -05:00
|
|
|
form_fields=['name', 'value']):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-10 21:07:38 -05:00
|
|
|
with patch.object(view, 'get_instance', new=get_instance):
|
|
|
|
|
|
|
|
# get the form page
|
|
|
|
response = view.edit()
|
|
|
|
self.assertIsInstance(response, Response)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertIn('frazzle', response.text)
|
|
|
|
# nb. no error
|
|
|
|
self.assertNotIn('Required', response.text)
|
|
|
|
|
|
|
|
def persist(setting):
|
2024-08-11 09:56:47 -05:00
|
|
|
self.app.save_setting(self.session, 'foo.bar', setting['value'])
|
2024-08-10 21:07:38 -05:00
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
# post request to save settings
|
|
|
|
self.request.method = 'POST'
|
|
|
|
self.request.POST = {
|
|
|
|
'name': 'foo.bar',
|
|
|
|
'value': 'froogle',
|
|
|
|
}
|
|
|
|
with patch.object(view, 'persist', new=persist):
|
|
|
|
response = view.edit()
|
|
|
|
# nb. should get redirect back to view page
|
2024-08-11 09:56:47 -05:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2024-08-10 21:07:38 -05:00
|
|
|
# setting should be updated in DB
|
|
|
|
self.assertEqual(self.app.get_setting(self.session, 'foo.bar'), 'froogle')
|
|
|
|
|
2024-08-11 09:56:47 -05:00
|
|
|
# try another post with invalid data (value is required)
|
2024-08-10 21:07:38 -05:00
|
|
|
self.request.method = 'POST'
|
2024-08-11 09:56:47 -05:00
|
|
|
self.request.POST = {}
|
2024-08-10 21:07:38 -05:00
|
|
|
with patch.object(view, 'persist', new=persist):
|
|
|
|
response = view.edit()
|
|
|
|
# nb. should get a form with errors
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertIn('Required', response.text)
|
|
|
|
# setting did not change in DB
|
|
|
|
self.assertEqual(self.app.get_setting(self.session, 'foo.bar'), 'froogle')
|
2024-08-06 18:52:54 -05:00
|
|
|
|
2024-08-11 09:56:47 -05:00
|
|
|
def test_delete(self):
|
2024-08-14 15:10:54 -05:00
|
|
|
self.pyramid_config.include('wuttaweb.views.common')
|
|
|
|
self.pyramid_config.include('wuttaweb.views.auth')
|
|
|
|
self.pyramid_config.add_route('settings.create', '/settings/new')
|
|
|
|
self.pyramid_config.add_route('settings.view', '/settings/{name}')
|
|
|
|
self.pyramid_config.add_route('settings.edit', '/settings/{name}/edit')
|
2024-08-11 09:56:47 -05:00
|
|
|
model = self.app.model
|
|
|
|
self.app.save_setting(self.session, 'foo.bar', 'frazzle')
|
|
|
|
self.session.commit()
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 1)
|
|
|
|
|
|
|
|
def get_instance():
|
|
|
|
setting = self.session.query(model.Setting).get('foo.bar')
|
|
|
|
return {
|
|
|
|
'name': setting.name,
|
|
|
|
'value': setting.value,
|
|
|
|
}
|
|
|
|
|
|
|
|
# sanity/coverage check using /settings/XXX/delete
|
|
|
|
self.request.matchdict = {'name': 'foo.bar'}
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 09:56:47 -05:00
|
|
|
model_name='Setting',
|
|
|
|
model_key='name',
|
2024-08-14 15:10:54 -05:00
|
|
|
get_index_url=MagicMock(return_value='/settings/'),
|
2024-08-11 09:56:47 -05:00
|
|
|
form_fields=['name', 'value']):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-11 09:56:47 -05:00
|
|
|
with patch.object(view, 'get_instance', new=get_instance):
|
|
|
|
|
|
|
|
# get the form page
|
|
|
|
response = view.delete()
|
|
|
|
self.assertIsInstance(response, Response)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertIn('frazzle', response.text)
|
|
|
|
|
|
|
|
def delete_instance(setting):
|
|
|
|
self.app.delete_setting(self.session, setting['name'])
|
|
|
|
|
|
|
|
self.request.method = 'POST'
|
|
|
|
self.request.POST = {}
|
|
|
|
with patch.object(view, 'delete_instance', new=delete_instance):
|
2024-08-14 16:58:08 -05:00
|
|
|
|
|
|
|
# enforces "instance not deletable" rules
|
|
|
|
with patch.object(view, 'is_deletable', return_value=False):
|
|
|
|
response = view.delete()
|
|
|
|
# nb. should get redirect back to view page
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
# setting remains in DB
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 1)
|
|
|
|
|
|
|
|
# post request to delete setting
|
2024-08-11 09:56:47 -05:00
|
|
|
response = view.delete()
|
2024-08-14 16:58:08 -05:00
|
|
|
# nb. should get redirect back to view page
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
# setting should be gone from DB
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 0)
|
2024-08-11 09:56:47 -05:00
|
|
|
|
|
|
|
def test_delete_instance(self):
|
|
|
|
model = self.app.model
|
|
|
|
self.app.save_setting(self.session, 'foo.bar', 'frazzle')
|
|
|
|
self.session.commit()
|
|
|
|
setting = self.session.query(model.Setting).one()
|
|
|
|
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 09:56:47 -05:00
|
|
|
model_class=model.Setting,
|
|
|
|
form_fields=['name', 'value']):
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-11 16:52:13 -05:00
|
|
|
view.delete_instance(setting)
|
|
|
|
self.session.commit()
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 0)
|
2024-08-11 09:56:47 -05:00
|
|
|
|
2024-08-24 14:26:13 -05:00
|
|
|
def test_delete_bulk(self):
|
|
|
|
self.pyramid_config.add_route('settings', '/settings/')
|
2024-08-24 19:28:13 -05:00
|
|
|
self.pyramid_config.add_route('progress', '/progress/{key}')
|
2024-08-24 14:26:13 -05:00
|
|
|
model = self.app.model
|
|
|
|
sample_data = [
|
|
|
|
{'name': 'foo1', 'value': 'ONE'},
|
|
|
|
{'name': 'foo2', 'value': 'two'},
|
|
|
|
{'name': 'foo3', 'value': 'three'},
|
|
|
|
{'name': 'foo4', 'value': 'four'},
|
|
|
|
{'name': 'foo5', 'value': 'five'},
|
|
|
|
{'name': 'foo6', 'value': 'six'},
|
|
|
|
{'name': 'foo7', 'value': 'seven'},
|
|
|
|
{'name': 'foo8', 'value': 'eight'},
|
|
|
|
{'name': 'foo9', 'value': 'nine'},
|
|
|
|
]
|
|
|
|
for setting in sample_data:
|
|
|
|
self.app.save_setting(self.session, setting['name'], setting['value'])
|
|
|
|
self.session.commit()
|
|
|
|
sample_query = self.session.query(model.Setting)
|
|
|
|
|
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_class=model.Setting):
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# sanity check on sample data
|
|
|
|
grid = view.make_model_grid(session=self.session)
|
|
|
|
data = grid.get_visible_data()
|
|
|
|
self.assertEqual(len(data), 9)
|
|
|
|
|
|
|
|
# and then let's filter it a little
|
|
|
|
self.request.GET = {'value': 's', 'value.verb': 'contains'}
|
|
|
|
grid = view.make_model_grid(session=self.session)
|
|
|
|
self.assertEqual(len(grid.filters), 2)
|
|
|
|
self.assertEqual(len(grid.active_filters), 1)
|
|
|
|
data = grid.get_visible_data()
|
|
|
|
self.assertEqual(len(data), 2)
|
|
|
|
|
2024-08-24 19:28:13 -05:00
|
|
|
# okay now let's delete those via quick method
|
|
|
|
# (user should be redirected back to index)
|
|
|
|
with patch.multiple(view,
|
|
|
|
deletable_bulk_quick=True,
|
|
|
|
make_model_grid=MagicMock(return_value=grid)):
|
|
|
|
response = view.delete_bulk()
|
2024-08-24 14:26:13 -05:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 7)
|
|
|
|
|
2024-08-24 19:28:13 -05:00
|
|
|
# now use another filter since those records are gone
|
|
|
|
self.request.GET = {'name': 'foo2', 'name.verb': 'equal'}
|
|
|
|
grid = view.make_model_grid(session=self.session)
|
|
|
|
self.assertEqual(len(grid.filters), 2)
|
|
|
|
self.assertEqual(len(grid.active_filters), 1)
|
|
|
|
data = grid.get_visible_data()
|
|
|
|
self.assertEqual(len(data), 1)
|
|
|
|
|
|
|
|
# this time we delete "slowly" with progress
|
|
|
|
self.request.session.id = 'ignorethis'
|
|
|
|
with patch.multiple(view,
|
|
|
|
deletable_bulk_quick=False,
|
|
|
|
make_model_grid=MagicMock(return_value=grid)):
|
|
|
|
with patch.object(mod, 'threading') as threading:
|
|
|
|
response = view.delete_bulk()
|
|
|
|
threading.Thread.return_value.start.assert_called_once_with()
|
|
|
|
# nb. user is shown progress page
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_delete_bulk_action(self):
|
2024-08-24 14:26:13 -05:00
|
|
|
self.pyramid_config.add_route('settings', '/settings/')
|
|
|
|
model = self.app.model
|
|
|
|
sample_data = [
|
|
|
|
{'name': 'foo1', 'value': 'ONE'},
|
|
|
|
{'name': 'foo2', 'value': 'two'},
|
|
|
|
{'name': 'foo3', 'value': 'three'},
|
|
|
|
{'name': 'foo4', 'value': 'four'},
|
|
|
|
{'name': 'foo5', 'value': 'five'},
|
|
|
|
{'name': 'foo6', 'value': 'six'},
|
|
|
|
{'name': 'foo7', 'value': 'seven'},
|
|
|
|
{'name': 'foo8', 'value': 'eight'},
|
|
|
|
{'name': 'foo9', 'value': 'nine'},
|
|
|
|
]
|
|
|
|
for setting in sample_data:
|
|
|
|
self.app.save_setting(self.session, setting['name'], setting['value'])
|
|
|
|
self.session.commit()
|
|
|
|
sample_query = self.session.query(model.Setting)
|
|
|
|
|
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_class=model.Setting):
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# basic bulk delete
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 9)
|
|
|
|
settings = self.session.query(model.Setting)\
|
|
|
|
.filter(model.Setting.value.ilike('%s%'))\
|
|
|
|
.all()
|
|
|
|
self.assertEqual(len(settings), 2)
|
2024-08-24 19:28:13 -05:00
|
|
|
view.delete_bulk_action(settings)
|
2024-08-24 14:26:13 -05:00
|
|
|
self.session.commit()
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 7)
|
|
|
|
|
2024-08-24 19:28:13 -05:00
|
|
|
def test_delete_bulk_thread(self):
|
|
|
|
self.pyramid_config.add_route('settings', '/settings/')
|
|
|
|
model = self.app.model
|
|
|
|
sample_data = [
|
|
|
|
{'name': 'foo1', 'value': 'ONE'},
|
|
|
|
{'name': 'foo2', 'value': 'two'},
|
|
|
|
{'name': 'foo3', 'value': 'three'},
|
|
|
|
{'name': 'foo4', 'value': 'four'},
|
|
|
|
{'name': 'foo5', 'value': 'five'},
|
|
|
|
{'name': 'foo6', 'value': 'six'},
|
|
|
|
{'name': 'foo7', 'value': 'seven'},
|
|
|
|
{'name': 'foo8', 'value': 'eight'},
|
|
|
|
{'name': 'foo9', 'value': 'nine'},
|
|
|
|
]
|
|
|
|
for setting in sample_data:
|
|
|
|
self.app.save_setting(self.session, setting['name'], setting['value'])
|
|
|
|
self.session.commit()
|
|
|
|
sample_query = self.session.query(model.Setting)
|
|
|
|
|
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_class=model.Setting):
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# basic delete, no progress
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 9)
|
|
|
|
settings = self.session.query(model.Setting)\
|
|
|
|
.filter(model.Setting.value.ilike('%s%'))
|
|
|
|
self.assertEqual(settings.count(), 2)
|
|
|
|
with patch.object(self.app, 'make_session', return_value=self.session):
|
|
|
|
view.delete_bulk_thread(settings)
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 7)
|
|
|
|
|
|
|
|
# basic delete, with progress
|
|
|
|
settings = self.session.query(model.Setting)\
|
|
|
|
.filter(model.Setting.name == 'foo1')
|
|
|
|
self.assertEqual(settings.count(), 1)
|
|
|
|
with patch.object(self.app, 'make_session', return_value=self.session):
|
|
|
|
view.delete_bulk_thread(settings, progress=MagicMock())
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 6)
|
|
|
|
|
|
|
|
# error, no progress
|
|
|
|
settings = self.session.query(model.Setting)\
|
|
|
|
.filter(model.Setting.name == 'foo2')
|
|
|
|
self.assertEqual(settings.count(), 1)
|
|
|
|
with patch.object(self.app, 'make_session', return_value=self.session):
|
|
|
|
with patch.object(view, 'delete_bulk_action', side_effect=RuntimeError):
|
|
|
|
view.delete_bulk_thread(settings)
|
|
|
|
# nb. nothing was deleted
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 6)
|
|
|
|
|
|
|
|
# error, with progress
|
|
|
|
self.assertEqual(settings.count(), 1)
|
|
|
|
with patch.object(self.app, 'make_session', return_value=self.session):
|
|
|
|
with patch.object(view, 'delete_bulk_action', side_effect=RuntimeError):
|
|
|
|
view.delete_bulk_thread(settings, progress=MagicMock())
|
|
|
|
# nb. nothing was deleted
|
|
|
|
self.assertEqual(self.session.query(model.Setting).count(), 6)
|
|
|
|
|
2024-08-21 11:46:38 -05:00
|
|
|
def test_autocomplete(self):
|
|
|
|
model = self.app.model
|
|
|
|
|
|
|
|
person1 = model.Person(full_name="George Jones")
|
|
|
|
self.session.add(person1)
|
|
|
|
person2 = model.Person(full_name="George Strait")
|
|
|
|
self.session.add(person2)
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
# no results for empty term
|
|
|
|
self.request.GET = {}
|
|
|
|
view = self.make_view()
|
|
|
|
results = view.autocomplete()
|
|
|
|
self.assertEqual(len(results), 0)
|
|
|
|
|
|
|
|
# search yields no results
|
|
|
|
self.request.GET = {'term': 'sally'}
|
|
|
|
view = self.make_view()
|
|
|
|
with patch.object(view, 'autocomplete_data', return_value=[]):
|
|
|
|
view = self.make_view()
|
|
|
|
results = view.autocomplete()
|
|
|
|
self.assertEqual(len(results), 0)
|
|
|
|
|
|
|
|
# search yields 2 results
|
|
|
|
self.request.GET = {'term': 'george'}
|
|
|
|
view = self.make_view()
|
|
|
|
with patch.object(view, 'autocomplete_data', return_value=[person1, person2]):
|
|
|
|
results = view.autocomplete()
|
|
|
|
self.assertEqual(len(results), 2)
|
|
|
|
self.assertEqual([res['value'] for res in results],
|
|
|
|
[p.uuid for p in [person1, person2]])
|
|
|
|
|
|
|
|
def test_autocomplete_normalize(self):
|
|
|
|
model = self.app.model
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
person = model.Person(full_name="Betty Boop", uuid='bogus')
|
|
|
|
normal = view.autocomplete_normalize(person)
|
|
|
|
self.assertEqual(normal, {'value': 'bogus',
|
|
|
|
'label': "Betty Boop"})
|
|
|
|
|
2024-08-25 12:20:28 -05:00
|
|
|
def test_download(self):
|
|
|
|
model = self.app.model
|
|
|
|
self.app.save_setting(self.session, 'foo', 'bar')
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_class=model.Setting,
|
|
|
|
model_key='name',
|
|
|
|
Session=MagicMock(return_value=self.session)):
|
|
|
|
view = self.make_view()
|
|
|
|
self.request.matchdict = {'name': 'foo'}
|
|
|
|
|
|
|
|
# 404 if no filename
|
|
|
|
response = view.download()
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
# 404 if bad filename
|
|
|
|
self.request.GET = {'filename': 'doesnotexist'}
|
|
|
|
response = view.download()
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
# 200 if good filename
|
|
|
|
foofile = self.write_file('foo.txt', 'foo')
|
|
|
|
with patch.object(view, 'download_path', return_value=foofile):
|
|
|
|
response = view.download()
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content_disposition, 'attachment; filename="foo.txt"')
|
|
|
|
|
|
|
|
def test_execute(self):
|
|
|
|
self.pyramid_config.add_route('settings.view', '/settings/{name}')
|
2024-08-25 15:52:29 -05:00
|
|
|
self.pyramid_config.add_route('progress', '/progress/{key}')
|
2024-08-25 12:20:28 -05:00
|
|
|
model = self.app.model
|
|
|
|
self.app.save_setting(self.session, 'foo', 'bar')
|
2024-08-25 15:52:29 -05:00
|
|
|
user = model.User(username='barney')
|
|
|
|
self.session.add(user)
|
2024-08-25 12:20:28 -05:00
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_class=model.Setting,
|
|
|
|
model_key='name',
|
|
|
|
Session=MagicMock(return_value=self.session)):
|
|
|
|
view = self.make_view()
|
|
|
|
self.request.matchdict = {'name': 'foo'}
|
2024-08-25 15:52:29 -05:00
|
|
|
self.request.session.id = 'mockid'
|
|
|
|
self.request.user = user
|
2024-08-25 12:20:28 -05:00
|
|
|
|
2024-08-25 15:52:29 -05:00
|
|
|
# basic usage; user is shown progress page
|
|
|
|
with patch.object(mod, 'threading') as threading:
|
2024-08-25 12:20:28 -05:00
|
|
|
response = view.execute()
|
2024-08-25 15:52:29 -05:00
|
|
|
threading.Thread.return_value.start.assert_called_once_with()
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_execute_thread(self):
|
|
|
|
model = self.app.model
|
|
|
|
enum = self.app.enum
|
|
|
|
user = model.User(username='barney')
|
|
|
|
self.session.add(user)
|
|
|
|
upgrade = model.Upgrade(description='test', created_by=user,
|
|
|
|
status=enum.UpgradeStatus.PENDING)
|
|
|
|
self.session.add(upgrade)
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
|
|
|
model_class=model.Upgrade):
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# basic execute, no progress
|
|
|
|
with patch.object(view, 'execute_instance') as execute_instance:
|
|
|
|
view.execute_thread({'uuid': upgrade.uuid}, user.uuid)
|
|
|
|
execute_instance.assert_called_once()
|
|
|
|
|
|
|
|
# basic execute, with progress
|
|
|
|
with patch.object(view, 'execute_instance') as execute_instance:
|
|
|
|
progress = MagicMock()
|
|
|
|
view.execute_thread({'uuid': upgrade.uuid}, user.uuid, progress=progress)
|
|
|
|
execute_instance.assert_called_once()
|
|
|
|
progress.handle_success.assert_called_once_with()
|
|
|
|
|
|
|
|
# error, no progress
|
|
|
|
with patch.object(view, 'execute_instance') as execute_instance:
|
|
|
|
execute_instance.side_effect = RuntimeError
|
|
|
|
view.execute_thread({'uuid': upgrade.uuid}, user.uuid)
|
|
|
|
execute_instance.assert_called_once()
|
|
|
|
|
|
|
|
# error, with progress
|
|
|
|
with patch.object(view, 'execute_instance') as execute_instance:
|
|
|
|
progress = MagicMock()
|
|
|
|
execute_instance.side_effect = RuntimeError
|
|
|
|
view.execute_thread({'uuid': upgrade.uuid}, user.uuid, progress=progress)
|
|
|
|
execute_instance.assert_called_once()
|
|
|
|
progress.handle_error.assert_called_once()
|
2024-08-25 12:20:28 -05:00
|
|
|
|
2024-08-06 18:52:54 -05:00
|
|
|
def test_configure(self):
|
2024-08-14 15:10:54 -05:00
|
|
|
self.pyramid_config.include('wuttaweb.views.common')
|
|
|
|
self.pyramid_config.include('wuttaweb.views.auth')
|
2024-08-06 18:52:54 -05:00
|
|
|
model = self.app.model
|
|
|
|
|
|
|
|
# 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},
|
|
|
|
]
|
|
|
|
|
2024-08-21 11:46:38 -05:00
|
|
|
view = mod.MasterView(self.request)
|
2024-08-11 12:41:22 -05:00
|
|
|
with patch.object(self.request, 'current_route_url', return_value='/appinfo/configure'):
|
2024-08-21 11:46:38 -05:00
|
|
|
with patch.object(mod, 'Session', return_value=self.session):
|
|
|
|
with patch.multiple(mod.MasterView, create=True,
|
2024-08-11 12:41:22 -05:00
|
|
|
model_name='AppInfo',
|
|
|
|
route_prefix='appinfo',
|
|
|
|
template_prefix='/appinfo',
|
|
|
|
creatable=False,
|
2024-08-14 15:10:54 -05:00
|
|
|
get_index_url=MagicMock(return_value='/appinfo/'),
|
2024-08-11 12:41:22 -05:00
|
|
|
configure_get_simple_settings=MagicMock(return_value=settings)):
|
2024-08-06 18:52:54 -05:00
|
|
|
|
|
|
|
# get the form page
|
2024-08-15 02:10:08 -05:00
|
|
|
response = view.configure(session=self.session)
|
2024-08-06 18:52:54 -05:00
|
|
|
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',
|
|
|
|
}
|
2024-08-15 02:10:08 -05:00
|
|
|
response = view.configure(session=self.session)
|
2024-08-06 18:52:54 -05:00
|
|
|
# nb. should get redirect back to configure page
|
2024-08-15 02:10:08 -05:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2024-08-06 18:52:54 -05:00
|
|
|
|
|
|
|
# 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'}
|
2024-08-15 02:10:08 -05:00
|
|
|
response = view.configure(session=self.session)
|
2024-08-06 18:52:54 -05:00
|
|
|
# nb. should get redirect back to configure page
|
2024-08-15 02:10:08 -05:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2024-08-06 18:52:54 -05:00
|
|
|
|
|
|
|
# should now have 0 settings
|
|
|
|
count = self.session.query(model.Setting).count()
|
|
|
|
self.assertEqual(count, 0)
|