def get_app(app_id, request, content=None):
    portal = api.portal.get()
    context = Context()
    context.request = request
    context.parent_request = request.get("PARENT_REQUEST", None)
    if context.parent_request:
        path = '/'.join(
            context.parent_request.physicalPathFromURL(
                context.parent_request.URL))
    else:
        path = '/'.join(request.physicalPathFromURL(request.URL))
    path = path.split("@@")[0]
    context.content = content
    while not hasattr(context.content, 'portal_type'):
        try:
            context.content = portal.unrestrictedTraverse(path)
        except ConflictError:
            raise
        except Exception:
            pass
        path = '/'.join(path.split('/')[0:-1])
    context.portal = portal
    context.api = api
    context.rapido = lambda id, content=context.content: get_app(
        id, request, content=content)
    app = RapidoApplication(app_id, context)
    return IRapidoApplication(app)
Beispiel #2
0
 def test_default_settings(self):
     self.app_obj.settings = 'no_settings: {}'
     self.app = IRapidoApplication(self.app_obj)
     self.app.initialize()
     self.assertEquals(
         self.app.settings,
         {
             'no_settings': {},
             'acl': {
                 'roles': {},
                 'rights': {
                     'author': [],
                     'editor': [],
                     'reader': []
                 }
             }
         }
     )
Beispiel #3
0
    def setUp(self):
        XMLConfig("meta.zcml", zope.component)()
        XMLConfig("meta.zcml", zope.browserpage)()
        XMLConfig("configure.zcml", zope.annotation)()
        XMLConfig("configure.zcml", rapido.core)()
        XMLConfig("configure.zcml", rapido.souper)()
        XMLConfig("configure.zcml", rapido.core.tests)()
        root = SiteNode()
        root['myapp'] = SimpleRapidoApplication("testapp", root)
        self.app_obj = root['myapp']
        self.app_obj.settings = """debug: true
acl:
  rights:
    author: [isaac.newton]
    editor: [marie.curie]
    reader: []
  roles: {"boss": ["marie.curie"], "biology": ["FamousDiscoverers"]}"""
        self.app = IRapidoApplication(self.app_obj)
        self.app.initialize()
Beispiel #4
0
class TestCase(unittest.TestCase):

    def setUp(self):
        XMLConfig("meta.zcml", zope.component)()
        XMLConfig("meta.zcml", zope.browserpage)()
        XMLConfig("configure.zcml", zope.annotation)()
        XMLConfig("configure.zcml", rapido.core)()
        XMLConfig("configure.zcml", rapido.souper)()
        XMLConfig("configure.zcml", rapido.core.tests)()
        root = SiteNode()
        root['myapp'] = SimpleRapidoApplication("testapp", root)
        self.app_obj = root['myapp']
        self.app_obj.settings = """debug: true
acl:
  rights:
    author: [isaac.newton]
    editor: [marie.curie]
    reader: []
  roles: {"boss": ["marie.curie"], "biology": ["FamousDiscoverers"]}"""
        self.app = IRapidoApplication(self.app_obj)
        self.app.initialize()

    def test_no_code(self):
        block = self.app.get_block('frmBook3')
        self.assertEquals(block.code, '# no code')

    def test_display(self):
        block = self.app.get_block('frmBook')
        self.assertEquals(
            block.display(None, edit=True),
            u'<form\n    name="frmBook"\n    class="rapido-block '
            'rapido-target-ajax"\n    action="http://here/blocks/frmBook"\n'
            '    rapido-settings=\'{"app": {"url": "http://here", '
            '"debug": true}, "target": "ajax", "id": "frmBook"}\'\n'
            '    method="POST">Author: <input type="text"\n'
            '        name="author" value="Victor Hugo" />\n<footer>'
            'Powered by Rapido</footer></form>\n'
        )

    def test_display_element_items(self):
        block = self.app.get_block('frmBook6')
        self.assertEquals(
            block.display(None, edit=True),
            u'<form\n'
            '    name="frmBook6"\n'
            '    class="rapido-block"\n'
            '    action="http://here/blocks/frmBook6"\n'
            '    rapido-settings=\''
            '{"app": {"url": "http://here", "debug": true}}\'\n'
            '    method="POST"><h1>The Force awakens</h1>\n'
            '<p>No spoil</p></form>\n'

        )

    def test_display_record(self):
        block = self.app.get_block('frmBook')
        record = self.app.create_record(id='record_1')
        record['author'] = "Joseph Conrad"
        self.assertEquals(
            block.display(record),
            u'<form\n    name="frmBook"\n    class="rapido-block '
            'rapido-target-ajax"\n    action="http://here/record/record_1"\n'
            '    rapido-settings=\'{"app": {"url": "http://here", '
            '"debug": true}, "target": "ajax", "id": "frmBook"}\'\n'
            '    method="POST">Author: Joseph Conrad\n<footer>'
            'Powered by Rapido</footer></form>\n'
        )
        self.assertTrue(
            u"""<input type="text"
        name="author" value="Joseph Conrad" />""" in block.display(
                record, edit=True))

    def test_on_save(self):
        block = self.app.get_block('frmBook')
        record = self.app.create_record(id='record_1')
        record['author'] = "Joseph Conrad"
        record.save(block=block)
        self.assertEquals(
            record['author'],
            'JOSEPH CONRAD'
        )

    def test_on_delete(self):
        record_1 = self.app.create_record(id='record_1')
        record_2 = self.app.create_record(id='record_2')
        record_2.set_block('frmBook2')
        self.app.delete_record(record=record_2)
        self.assertEquals(
            record_1['message'],
            'Good bye'
        )

    def test_compute_id(self):
        block = self.app.get_block('frmBook2')
        record2 = self.app.create_record()
        record2.save({'author': "John DosPassos"}, block=block, creation=True)
        self.assertEquals(record2.id, 'my-id')
        record3 = self.app.create_record()
        record3.save(
            {'author': "John DosPassos"},
            block_id="frmBook2",
            creation=True)
        self.assertTrue(record3.id.startswith('my-id-'))

    def test_python_compilation_errors(self):
        block = self.app.get_block('frmBook4')
        block.display(None, edit=True)
        self.assertEquals(
            self.app.messages[0],
            "Rapido compilation error - testapp\nin frmBook4.py, at line 3:"
            "\n    returm 'hello'\n-----------------^\ninvalid syntax"
        )
        self.app_obj.set_fake_block_data('frmBook4', 'py', """
def author(context):
    return context.not_a_method()""")
        del self.app._blocks['frmBook4']
        block = self.app.get_block('frmBook4')
        block.display(None, edit=True)
        self.assertEquals(
            self.app.messages[1],
            'Rapido execution error - testapp\n  File "frmBook4.py", line 3, '
            'in author\nAttributeError: \'Context\' object has no attribute \''
            'not_a_method\''
        )

    def test_undefined_element(self):
        self.app_obj.set_fake_block_data('frmBook2', 'html', """Author: {author}
{summary}<footer>Powered by Rapido</footer>""")
        if 'frmBook2' in self.app._blocks:
            del self.app._blocks['frmBook2']
        block = self.app.get_block('frmBook2')
        self.assertTrue(u'UNDEFINED ELEMENT' in block.display(None, edit=True))

    def test_undefined_element_type(self):
        block = self.app.get_block('frmBook5')
        self.assertTrue(u'UNKNOWN ELEMENT TYPE' in block.display(
            None, edit=True))

    def test_render_action(self):
        self.app_obj.set_fake_block_data('frmBook2', 'html', """Author: {author}
 {do_something} {_save}<footer>Powered by Rapido</footer>""")
        if 'frmBook2' in self.app._blocks:
            del self.app._blocks['frmBook2']
        block = self.app.get_block('frmBook2')
        html = block.display(None, edit=True)
        self.assertTrue('<input type="submit"\n'
            '        name="action.do_something" value="Do" />' in html)

    def test_render_special_action(self):
        block = self.app.get_block('frmBook5')
        html = block.display(None, edit=True)
        self.assertTrue('<input type="submit"\n'
            '        name="_save" value="Save" />' in html)

    def test_python_not_mandatory(self):
        block = self.app.get_block('frmBook3')
        self.assertTrue(
            u'Author: <input type="text"\n'
            '        name="author" value="" />' in block.display(
                None, edit=True)
        )

    def test_callable_layout(self):
        block = self.app.get_block('frmBook8')
        self.assertTrue(
            u'France is bacon' in block.display(None, edit=True)
        )

    def test_safe_modules(self):
        import datetime
        # add a new safe module
        import re
        from rapido.core import app
        app.safe_modules.re = re
        block = self.app.get_block('frmBook9')
        today = datetime.date.today().strftime("%Y-%m-%d")
        self.assertTrue(
            u'Random: 0.' in block.display(None, edit=True)
        )
        self.assertTrue(
            u'Date: %s' % today in block.display(None, edit=True)
        )

    def test_on_display(self):
        block = self.app.get_block('block10')
        self.assertIn(
            "You know nothing, John Snow",
            block.display(None)
        )
Beispiel #5
0
class TestCase(unittest.TestCase):

    def setUp(self):
        XMLConfig("meta.zcml", zope.component)()
        XMLConfig("meta.zcml", zope.browserpage)()
        XMLConfig("configure.zcml", zope.annotation)()
        XMLConfig("configure.zcml", rapido.core)()
        XMLConfig("configure.zcml", rapido.souper)()
        XMLConfig("configure.zcml", rapido.core.tests)()
        root = SiteNode()
        root['myapp'] = SimpleRapidoApplication("testapp", root)
        self.app_obj = root['myapp']
        self.app_obj.settings = """debug: true
acl:
  rights:
    author: [isaac.newton]
    editor: [marie.curie]
    reader: []
  roles: {"boss": ["marie.curie"], "biology": ["FamousDiscoverers"]}"""
        self.app = IRapidoApplication(self.app_obj)
        self.app.initialize()

    def test_get_app(self):
        rest = IRest(self.app)
        result = rest.GET([], "")
        self.assertEquals(result,
            {
                'debug': True,
                'acl': {
                    'roles': {
                        'biology': ['FamousDiscoverers'],
                        'boss': ['marie.curie']
                    },
                    'rights': {
                        'author': ['isaac.newton'],
                        'editor': ['marie.curie'],
                        'reader': []
                    }
                }
            })

    def test_get_bad_directive(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.GET,
            ['bad_directive'],
            ""
        )

    def test_get_block(self):
        rest = IRest(self.app)
        result = rest.GET(['blocks', 'frmBook2'], "")
        self.assertEquals(result,
            {
                'elements': {
                    'do_something': {
                        'type': 'ACTION',
                        'label': 'Do'
                    },
                    'author': {'type': 'TEXT'}
                },
                'target': 'ajax'
            }
        )

    def test_get_element(self):
        rest = IRest(self.app)
        result = rest.GET(['blocks', 'frmBook', 'something_computed'], "")
        self.assertEquals(result,
            {'one': 1, 'two': 2.0}
        )

    def test_post_element(self):
        rest = IRest(self.app)
        result = rest.POST(['blocks', 'frmBook', 'something_computed'], "")
        self.assertEquals(result,
            {'one': 1, 'two': 2.0}
        )

    def test_post_without_element(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.POST,
            ['blocks', 'frmBook'],
            ""
        )

    def test_get_block_too_many_param(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.GET,
            ['blocks', 'frmBook', 'what', 'ever'],
            ""
        )

    def test_get_not_defined_block(self):
        rest = IRest(self.app)
        result = rest.GET(['blocks', 'anything'], "")
        self.assertEquals(result,
            {'elements': {}, 'id': 'anything'}
        )

    def test_get_records(self):
        self.app.create_record(id='record_1')
        self.app.create_record(id='record_2')
        rest = IRest(self.app)
        self.assertEquals(len(rest.GET(['records'], "")), 2)

    def test_get_records_not_reader(self):
        self.app.create_record(id='record_1')
        self.app.create_record(id='record_2')
        rest = IRest(self.app)
        self.app_obj.set_fake_user("nobody")
        self.assertRaises(
            Unauthorized,
            rest.GET,
            ['records'],
            ""
        )

    def test_get_record_without_param(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.GET,
            ['record'],
            ""
        )

    def test_get_not_existing_record(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotFound,
            rest.GET,
            ['record', 'not_existing'],
            ""
        )

    def test_get_record(self):
        record = self.app.create_record(id='record_1')
        record.save(
            {'author': "John DosPassos"},
            block_id="frmBook",
            creation=True)
        self.app.settings['acl']['rights']['reader'] = ['*']
        self.app_obj.set_fake_user("anybody")
        self.assertTrue(self.app.acl.has_access_right("reader"))
        rest = IRest(self.app)
        result = rest.GET(['record', 'record_1'], "")
        self.assertEquals(
            result,
            {
                'forever': 'I will never change.',
                'famous_quote': 'A good plan violently executed now is '
                'better than a perfect plan executed next week.',
                'id': 'record_1',
                'block': 'frmBook',
                'author': 'JOHN DOSPASSOS'
            }
        )

    def test_get_record_not_reader(self):
        self.app.create_record(id='record_1')
        rest = IRest(self.app)
        self.app_obj.set_fake_user("nobody")
        self.assertRaises(
            Unauthorized,
            rest.GET,
            ['record', 'record_1'],
            ""
        )

    def test_post_new_record(self):
        rest = IRest(self.app)
        result = rest.POST([], '{"item1": "value1"}')
        self.assertEquals(result['success'], 'created')
        self.assertIn('path', result)
        self.assertIn('id', result)

    def test_post_new_record_not_author(self):
        self.app_obj.set_fake_user("anybody")
        self.assertFalse(self.app.acl.has_access_right("author"))
        rest = IRest(self.app)
        self.assertRaises(
            Unauthorized,
            rest.POST,
            [],
            '{"item1": "value1"}'
        )

    def test_post_existing_record(self):
        record = self.app.create_record(id='record_1')
        rest = IRest(self.app)
        result = rest.POST(['record', 'record_1'], '{"item1": "value1"}')
        self.assertEquals(result, {'success': 'updated'})
        self.assertEquals(record['item1'], 'value1')

    def test_post_existing_record_not_author(self):
        self.app.create_record(id='record_1')
        self.app_obj.set_fake_user("anybody")
        rest = IRest(self.app)
        self.assertRaises(
            Unauthorized,
            rest.POST,
            ['record', 'record_1'],
            '{"item1": "value1"}'
        )

    def test_post_not_existing_record(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotFound,
            rest.POST,
            ['record', 'not_existing'],
            '{"item1": "value1"}'
        )

    def test_post_bulk(self):
        rest = IRest(self.app)
        result = rest.POST(['records'],
            '[{"item1": "new value"}, {"item1": "other value"}]')
        self.assertEquals(result, {'total': 2, 'success': 'created'})
        self.assertEquals(len(self.app.records()), 2)

    def test_post_bulk_not_author(self):
        self.app_obj.set_fake_user("anybody")
        rest = IRest(self.app)
        self.assertRaises(
            Unauthorized,
            rest.POST,
            ['records'],
            '[{"item1": "new value"}, {"item1": "other value"}]'
        )

    def test_search(self):
        record_1 = self.app.create_record(id='record_1')
        record_1.save(
            {'author': "John DosPassos"},
            block_id="frmBook",
            creation=True)
        record_2 = self.app.create_record(id='record_2')
        record_2.save(
            {'author': "J. Conrad"},
            block_id="frmBook",
            creation=True)
        rest = IRest(self.app)
        result = rest.POST(['search'], '{"query": "author==\'J. CONRAD\'"}')
        self.assertEquals(len(result), 1)

    def test_search_not_reader(self):
        self.app.create_record(id='record_1')
        rest = IRest(self.app)
        self.app_obj.set_fake_user("nobody")
        self.assertRaises(
            Unauthorized,
            rest.POST,
            ['search'],
            '{"query": "author==\'J. CONRAD\'"}'
        )

    def test_refresh(self):
        rest = IRest(self.app)
        result = rest.POST(['refresh'], '')
        self.assertEquals(result,
            {'success': 'refresh', 'indexes': ['author', u'id']}
        )

    def test_refresh_with_rebuild(self):
        rest = IRest(self.app)
        result = rest.POST(['refresh'], '{"rebuild": true}')
        self.assertEquals(result,
            {'success': 'refresh', 'indexes': ['author', u'id']}
        )

    def test_refresh_not_reader(self):
        rest = IRest(self.app)
        self.app_obj.set_fake_user("marie.curie")
        self.assertRaises(
            Unauthorized,
            rest.POST,
            ['refresh'],
            ''
        )

    def test_post_record_without_param(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.POST,
            ['record'],
            ""
        )

    def test_post_bad_directive(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.POST,
            ['bad_directive'],
            ""
        )

    def test_delete_bad_param(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.DELETE,
            ['whatever'],
            ""
        )

    def test_delete_record_no_id(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.DELETE,
            ['record'],
            ""
        )

    def test_delete_record(self):
        self.app.create_record(id='record_1')
        rest = IRest(self.app)
        result = rest.DELETE(['record', 'record_1'], "")
        self.assertEquals(result,
            {'success': 'deleted'}
        )
        self.assertEquals(len(self.app.records()), 0)

    def test_delete_record_not_author(self):
        self.app.create_record(id='record_1')
        rest = IRest(self.app)
        self.app_obj.set_fake_user("anybody")
        self.assertRaises(
            Unauthorized,
            rest.DELETE,
            ['record', 'record_1'],
            ''
        )

    def test_delete_not_existing_record(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotFound,
            rest.DELETE,
            ['record', 'not_existing'],
            ""
        )

    def test_delete_all_records(self):
        self.app.create_record(id='record_1')
        self.app.create_record(id='record_2')
        rest = IRest(self.app)
        result = rest.DELETE(['records'], "")
        self.assertEquals(result,
            {'success': 'deleted'}
        )
        self.assertEquals(len(self.app.records()), 0)

    def test_delete_all_records_not_editor(self):
        self.app.create_record(id='record_1')
        rest = IRest(self.app)
        self.app_obj.set_fake_user("isaac.newton")
        self.assertRaises(
            Unauthorized,
            rest.DELETE,
            ['records'],
            ''
        )

    def test_put_bad_directive(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.PUT,
            ['bad_directive'],
            '{"item1": "value1"}'
        )

    def test_put_new_record(self):
        rest = IRest(self.app)
        result = rest.PUT(['record', 'record_1'], '{"item1": "value1"}')
        self.assertEquals(result['success'], 'created')

    def test_put_new_record_not_author(self):
        rest = IRest(self.app)
        self.app_obj.set_fake_user("anybody")
        self.assertRaises(
            Unauthorized,
            rest.PUT,
            ['record', 'record_1'],
            '{"item1": "value1"}'
        )

    def test_put_existing_record(self):
        self.app.create_record(id='record_1')
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.PUT,
            ['record', 'record_1'],
            '{"item1": "value1"}'
        )

    def test_put_without_id(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.PUT,
            ['record'],
            '{"item1": "value1"}'
        )

    def test_patch_record(self):
        record = self.app.create_record(id='record_1')
        rest = IRest(self.app)
        result = rest.PATCH(['record', 'record_1'], '{"item1": "value1"}')
        self.assertEquals(result,
            {'success': 'updated'}
        )
        self.assertEquals(record['item1'], 'value1')

    def test_patch_record_not_author(self):
        self.app.create_record(id='record_1')
        rest = IRest(self.app)
        self.app_obj.set_fake_user("anybody")
        self.assertRaises(
            Unauthorized,
            rest.PATCH,
            ['record', 'record_1'],
            '{"item1": "value1"}'
        )

    def test_patch_without_id(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.PATCH,
            ['record'],
            '{"item1": "value1"}'
        )

    def test_patch_bad_directive(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.PATCH,
            ['bad_directive'],
            '{"item1": "value1"}'
        )

    def test_patch_record_without_param(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotAllowed,
            rest.PATCH,
            ['record'],
            ""
        )

    def test_patch_not_existing_record(self):
        rest = IRest(self.app)
        self.assertRaises(
            NotFound,
            rest.PATCH,
            ['record', 'not_existing'],
            ""
        )
Beispiel #6
0
class TestCase(unittest.TestCase):

    def setUp(self):
        XMLConfig("meta.zcml", zope.component)()
        XMLConfig("meta.zcml", zope.browserpage)()
        XMLConfig("configure.zcml", zope.annotation)()
        XMLConfig("configure.zcml", rapido.core)()
        XMLConfig("configure.zcml", rapido.souper)()
        XMLConfig("configure.zcml", rapido.core.tests)()
        root = SiteNode()
        root['myapp'] = SimpleRapidoApplication("testapp", root)
        self.app_obj = root['myapp']
        self.app_obj.settings = """debug: true
acl:
  rights:
    author: [isaac.newton]
    editor: [marie.curie]
    reader: []
  roles: {"boss": ["marie.curie"], "biology": ["FamousDiscoverers"]}"""
        self.app = IRapidoApplication(self.app_obj)
        self.app.initialize()

    def test_get_block(self):
        display = IDisplay(self.app)
        result = display.GET(['testapp', 'blocks', 'frmBook'], {})
        self.assertEquals(result,
            (u'<form\n    name="frmBook"\n    class="rapido-block '
            'rapido-target-ajax"\n    action="http://here/blocks/frmBook"\n'
            '    rapido-settings=\'{"app": {"url": "http://here", '
            '"debug": true}, "target": "ajax", "id": "frmBook"}\'\n'
            '    method="POST">Author: <input type="text"\n'
            '        name="author" value="Victor Hugo" />\n<footer>'
            'Powered by Rapido</footer></form>\n', '')
        )

    def test_get_block_with_permission(self):
        self.app_obj.set_fake_user("isaac.newton")
        display = IDisplay(self.app)
        result = display.GET(['testapp', 'blocks', 'block11'], {})
        self.assertTrue('You know nothing, John Snow' in result[0])

    def test_get_block_without_permission(self):
        self.app_obj.set_fake_user("marie.curie")
        display = IDisplay(self.app)
        self.assertRaises(
            Unauthorized,
            display.GET,
            ['testapp', 'blocks', 'block11'],
            {}
        )

    def test_get_block_element(self):
        display = IDisplay(self.app)
        result = display.GET(
            ['testapp', 'blocks', 'frmBook', 'famous_quote'], {})
        self.assertEquals(result,
            ('A good plan violently executed now is better than a perfect '
            'plan executed next week.', '')
        )

    def test_get_block_element_with_error(self):
        display = IDisplay(self.app)
        result = display.GET(
            ['testapp', 'blocks', 'frmBook4', 'author'], {})
        self.assertEquals(result,
            ('Rapido execution error - testapp\n  File "frmBook4.py", line 3, '
            'in author\nAttributeError: \'Context\' object has no attribute '
            '\'not_a_method\'', None)
        )

    def test_get_block_element_with_permission(self):
        self.app_obj.set_fake_user("isaac.newton")
        display = IDisplay(self.app)
        result = display.GET(['testapp', 'blocks', 'block11', 'message'], {})
        self.assertTrue('You know nothing, John Snow' in result[0])

    def test_get_block_element_without_permission(self):
        self.app_obj.set_fake_user("marie.curie")
        display = IDisplay(self.app)
        self.assertRaises(
            Unauthorized,
            display.GET,
            ['testapp', 'blocks', 'block11', 'message'],
            {}
        )

    def test_get_block_action_element(self):
        display = IDisplay(self.app)
        result = display.GET(
            ['testapp', 'blocks', 'frmBook', 'go_to_bed'], {})
        self.assertEquals(result,
            ('', 'http://localhost/bed')
        )

    def test_get_not_existing_block(self):
        display = IDisplay(self.app)
        self.assertRaises(
            NotFound,
            display.GET,
            ['testapp', 'blocks', 'not_existing'],
            {}
        )

    def test_get_record(self):
        record = self.app.create_record(id='record_1')
        record.save(
            {'author': "John DosPassos"},
            block_id="frmBook",
            creation=True)
        display = IDisplay(self.app)
        result = display.GET(['testapp', 'record', 'record_1'], {})
        self.assertTrue('Author: JOHN DOSPASSOS' in result[0])

    def test_get_record_not_reader(self):
        record = self.app.create_record(id='record_1')
        record.save(
            {'author': "John DosPassos"},
            block_id="frmBook",
            creation=True)
        self.app_obj.set_fake_user("nobody")
        self.assertFalse(self.app.acl.has_access_right("reader"))
        display = IDisplay(self.app)
        self.assertRaises(
            Unauthorized,
            display.GET,
            ['testapp', 'record', 'record_1'],
            {}
        )

    def test_get_record_edit(self):
        record = self.app.create_record(id='record_1')
        record.save(
            {'author': "John DosPassos"},
            block_id="frmBook",
            creation=True)
        display = IDisplay(self.app)
        result = display.GET(['testapp', 'record', 'record_1', 'edit'], {})
        self.assertTrue('Author: <input type="text"\n'
        '        name="author" value="JOHN DOSPASSOS" />' in result[0])

    def test_get_not_existing_record(self):
        display = IDisplay(self.app)
        self.assertRaises(
            NotFound,
            display.GET,
            ['testapp', 'record', 'record_1_not_existing'],
            {}
        )

    def test_refresh(self):
        display = IDisplay(self.app)
        result = display.GET(['testapp', 'refresh'], {})
        self.assertEquals(result,
            (u'Refreshed (author, id)', ''))

    def test_refresh_not_manager(self):
        display = IDisplay(self.app)
        self.app_obj.set_fake_user("marie.curie")
        self.assertRaises(
            Unauthorized,
            display.GET,
            ['testapp', 'refresh'],
            {}
        )

    def test_get_bad_directive(self):
        display = IDisplay(self.app)
        self.assertRaises(
            NotAllowed,
            display.GET,
            ['testapp', 'bad_directive'],
            {}
        )

    def test_post_block(self):
        display = IDisplay(self.app)
        result = display.POST(['testapp', 'blocks', 'frmBook'], {})
        self.assertTrue('Author: <input type="text"\n'
        '        name="author" value="Victor Hugo" />' in result[0])

    def test_post_not_existing_block(self):
        display = IDisplay(self.app)
        self.assertRaises(
            NotFound,
            display.POST,
            ['testapp', 'blocks', 'not_existing'],
            {}
        )

    def test_post_block_action(self):
        display = IDisplay(self.app)
        display.POST([
            'testapp', 'blocks', 'frmBook2'],
            {'action.do_something': True})
        self.assertEquals(
            self.app.messages[-1],
            'Hello'
        )

    def test_post_block_element(self):
        display = IDisplay(self.app)
        result = display.POST(
            ['testapp', 'blocks', 'frmBook', 'famous_quote'], {})
        self.assertEquals(result,
            ('A good plan violently executed now is better than a perfect '
            'plan executed next week.', '')
        )

    def test_post_block_action_element(self):
        display = IDisplay(self.app)
        result = display.POST(
            ['testapp', 'blocks', 'frmBook', 'go_to_bed'], {})
        self.assertEquals(result,
            ('', 'http://localhost/bed')
        )

    def test_post_block_bad_action_element(self):
        display = IDisplay(self.app)
        self.assertRaises(
            NotFound,
            display.POST,
            ['testapp', 'blocks', 'frmBook', 'go_to_home'], {})

    def test_post_block_save(self):
        display = IDisplay(self.app)
        display.POST([
            'testapp', 'blocks', 'frmBook2'],
            {'_save': True, 'author': 'J. Conrad'})
        self.assertEquals(
            len(self.app.records()),
            1
        )

    def test_post_block_save_redirect(self):
        display = IDisplay(self.app)
        result = display.POST([
            'testapp', 'blocks', 'frmBook2'],
            {'_save': True, 'author': 'J. Conrad'})
        self.assertEquals(
            result[1],
            "http://somewhere"
        )

    def test_post_block_save_not_author(self):
        display = IDisplay(self.app)
        self.app_obj.set_fake_user("nobody")
        self.assertRaises(
            Unauthorized,
            display.POST,
            ['testapp', 'blocks', 'frmBook2'],
            {'_save': True, 'author': 'J. Conrad'}
        )

    def test_post_record(self):
        record = self.app.create_record(id='record_1')
        record.save(
            {'author': "John DosPassos"},
            block_id="frmBook",
            creation=True)
        display = IDisplay(self.app)
        html = display.POST(
            ['testapp', 'record', 'record_1'],
            {})
        self.assertTrue('JOHN DOSPASSOS' in html[0])

    def test_post_record_not_reader(self):
        record = self.app.create_record(id='record_1')
        record.save(
            {'author': "John DosPassos"},
            block_id="frmBook",
            creation=True)
        display = IDisplay(self.app)
        self.app_obj.set_fake_user("nobody")
        self.assertRaises(
            Unauthorized,
            display.POST,
            ['testapp', 'record', 'record_1'],
            {}
        )

    def test_post_record_save_editor(self):
        record = self.app.create_record(id='record_1')
        record.save(
            {'author': "John DosPassos"},
            block_id="frmBook",
            creation=True)
        self.app_obj.set_fake_user("marie.curie")
        display = IDisplay(self.app)
        display.POST(
            ['testapp', 'record', 'record_1'],
            {'_save': True, 'author': 'J. Conrad'})
        self.assertEquals(record['author'], 'J. CONRAD')

    def test_post_record_save_author(self):
        self.app_obj.set_fake_user("isaac.newton")
        display = IDisplay(self.app)
        display.POST([
            'testapp', 'blocks', 'frmBook2'],
            {'_save': True, 'author': 'John DosPassos'})
        record = self.app.records()[0]
        self.assertEquals(record['_author'], ['isaac.newton'])
        display.POST(
            ['testapp', 'record', record.id],
            {'_save': True, 'author': 'J. Conrad'})
        self.assertEquals(record['author'], 'J. Conrad')

    def test_post_record_save_not_author(self):
        record = self.app.create_record(id='record_1')
        record.save(
            {'author': "John DosPassos"},
            block_id="frmBook",
            creation=True)
        self.assertTrue('_author' not in record)
        display = IDisplay(self.app)
        self.app_obj.set_fake_user("isaac.newton")
        self.assertRaises(
            Unauthorized,
            display.POST,
            ['testapp', 'record', 'record_1'],
            {'_save': True, 'author': 'J. Conrad'}
        )

    def test_post_not_existing_record(self):
        display = IDisplay(self.app)
        self.assertRaises(
            NotFound,
            display.POST,
            ['testapp', 'record', 'record_not_existing'],
            {'_save': True, 'author': 'J. Conrad'}
        )

    def test_post_record_edit_action(self):
        record = self.app.create_record(id='record_1')
        record.save(
            {'author': "John DosPassos"},
            block_id="frmBook",
            creation=True)
        display = IDisplay(self.app)
        result = display.POST(
            ['testapp', 'record', 'record_1'],
            {'_edit': True})
        self.assertTrue('Author: <input type="text"\n'
        '        name="author" value="JOHN DOSPASSOS" />' in result[0])

    def test_post_record_edit_not_author(self):
        record = self.app.create_record(id='record_1')
        record.save(
            {'author': "John DosPassos"},
            block_id="frmBook",
            creation=True)
        display = IDisplay(self.app)
        self.app_obj.set_fake_user("nobody")
        self.assertRaises(
            Unauthorized,
            display.POST,
            ['testapp', 'record', 'record_1'],
            {'_edit': True}
        )

    def test_post_record_action(self):
        record = self.app.create_record(id='record_1')
        record.set_block("frmBook")
        display = IDisplay(self.app)
        display.POST(
            ['testapp', 'record', 'record_1'],
            {'action.add_note': True})
        self.assertEquals(
            record['note'],
            "That's a good book"
        )

    def test_post_bad_directive(self):
        display = IDisplay(self.app)
        self.assertRaises(
            NotAllowed,
            display.POST,
            ['testapp', 'bad_directive'],
            {}
        )

    def test_delete_record(self):
        self.app.create_record(id='record_1')
        display = IDisplay(self.app)
        display.POST(
            ['testapp', 'record', 'record_1'],
            {'_delete': True})
        self.assertEquals(len(self.app.records()), 0)

    def test_delete_record_not_author(self):
        self.app.create_record(id='record_1')
        display = IDisplay(self.app)
        self.app_obj.set_fake_user("nobody")
        self.assertRaises(
            Unauthorized,
            display.POST,
            ['testapp', 'record', 'record_1'],
            {'_delete': True}
        )

    def test_delete_record_redirect(self):
        record = self.app.create_record(id='record_1')
        record.set_block('frmBook2')
        display = IDisplay(self.app)
        result = display.POST(
            ['testapp', 'record', 'record_1'],
            {'_delete': True})
        self.assertEquals(result[1], "http://somewhere")
Beispiel #7
0
class TestCase(unittest.TestCase):

    def setUp(self):
        XMLConfig("meta.zcml", zope.component)()
        XMLConfig("meta.zcml", zope.browserpage)()
        XMLConfig("configure.zcml", zope.annotation)()
        XMLConfig("configure.zcml", rapido.core)()
        XMLConfig("configure.zcml", rapido.souper)()
        XMLConfig("configure.zcml", rapido.core.tests)()
        root = SiteNode()
        root['myapp'] = SimpleRapidoApplication("testapp", root)
        self.app_obj = root['myapp']
        self.app_obj.settings = """debug: true
acl:
  rights:
    author: [isaac.newton]
    editor: [marie.curie]
    reader: [FamousDiscoverers]
  roles: {"boss": ["marie.curie"], "biology": ["FamousDiscoverers"]}"""
        self.app = IRapidoApplication(self.app_obj)
        self.app.initialize()

    def test_settings(self):
        self.assertEquals(
            self.app.settings,
            {
                'debug': True,
                'acl': {
                    'roles': {
                        'biology': ['FamousDiscoverers'],
                        'boss': ['marie.curie']
                    },
                    'rights': {
                        'author': ['isaac.newton'],
                        'editor': ['marie.curie'],
                        'reader': ['FamousDiscoverers']
                    }
                }
            }
        )

    def test_default_settings(self):
        self.app_obj.settings = 'no_settings: {}'
        self.app = IRapidoApplication(self.app_obj)
        self.app.initialize()
        self.assertEquals(
            self.app.settings,
            {
                'no_settings': {},
                'acl': {
                    'roles': {},
                    'rights': {
                        'author': [],
                        'editor': [],
                        'reader': []
                    }
                }
            }
        )

    def test_acl(self):
        self.assertEquals(
            self.app.acl.roles(),
            {'biology': ['FamousDiscoverers'], 'boss': ['marie.curie']}
        )

    def test_access_rights(self):
        self.app_obj.set_fake_user("marie.curie")
        self.assertTrue(self.app.acl.has_access_right('editor'))
        self.app_obj.set_fake_user("anybody")
        self.assertFalse(self.app.acl.has_access_right('reader'))
        self.app_obj.set_fake_groups(["FamousDiscoverers"])
        self.assertTrue(self.app.acl.has_access_right('reader'))

    def test_roles(self):
        self.app_obj.set_fake_user("marie.curie")
        self.assertTrue(self.app.acl.has_role('boss'))
        self.app_obj.set_fake_user("isaac.newton")
        self.assertFalse(self.app.acl.has_role('boss'))
        self.assertFalse(self.app.acl.has_role('not_a_role'))
        self.app_obj.set_fake_groups(["FamousDiscoverers"])
        self.assertTrue(self.app.acl.has_role('biology'))
Beispiel #8
0
class TestCase(unittest.TestCase):

    def setUp(self):
        XMLConfig("meta.zcml", zope.component)()
        XMLConfig("meta.zcml", zope.browserpage)()
        XMLConfig("configure.zcml", zope.annotation)()
        XMLConfig("configure.zcml", rapido.core)()
        XMLConfig("configure.zcml", rapido.souper)()
        XMLConfig("configure.zcml", rapido.core.tests)()
        root = SiteNode()
        root['myapp'] = SimpleRapidoApplication("testapp", root)
        self.app_obj = root['myapp']
        self.app_obj.settings = """debug: true
acl:
  rights:
    author: [isaac.newton]
    editor: [marie.curie]
    reader: []
  roles: {"boss": ["marie.curie"], "biology": ["FamousDiscoverers"]}"""
        self.app = IRapidoApplication(self.app_obj)
        self.app.initialize()

    def test_create_record(self):
        record = self.app.create_record(id='record_1')
        self.assertEquals(record.id, 'record_1')

    def test_record_items(self):
        record = self.app.create_record(id='record_1')
        record['author'] = "Joseph Conrad"
        record['book_tile'] = "Lord Jim"
        self.assertEquals(record['author'], 'Joseph Conrad')
        record['not_important'] = 2
        self.assertTrue('not_important' in record)
        del record['not_important']
        self.assertEquals(
            [key for key in record],
            ['book_tile', 'id', 'author']
        )

    def test_find_by_uid(self):
        record = self.app.create_record(id='record_1')
        uid = record.uid
        self.assertEquals(
            self.app.get_record(uid).uid,
            self.app.get_record('record_1').uid
        )

    def test_unique_id(self):
        self.app.create_record(id='record_1')
        record_bis = self.app.create_record(id='record_1')
        self.assertTrue(record_bis.id != 'record_1')
        self.assertTrue(record_bis.id.startswith('record_1-'))

    def test_search(self):
        record = self.app.create_record(id='record_1')
        # by calling the block we make sure indexes are built
        self.app.get_block('frmBook')
        record['author'] = "Joseph Conrad"
        record.reindex()
        self.assertEquals(
            [rec['author'] for rec in self.app.search('id=="record_1"')],
            ['Joseph Conrad']
        )
        self.assertEquals(
            [rec['author']
                for rec in self.app.search('author=="Joseph Conrad"')],
            ['Joseph Conrad']
        )
        self.assertEquals(
            [rec['author'] for rec in self.app.search('"joseph" in author')],
            ['Joseph Conrad']
        )

    def test_delete(self):
        record = self.app.create_record()
        the_id = record.id
        self.app.delete_record(record=record)
        self.assertTrue(self.app.get_record(the_id) is None)
        record2 = self.app.create_record()
        the_id = record2.id
        self.app.delete_record(id=the_id)
        self.assertTrue(self.app.get_record(the_id) is None)

    def test_save_from_dict(self):
        record = self.app.create_record()
        record.save({'author': "John DosPassos"})
        self.assertEquals(record['author'], "John DosPassos")

    def test_save_from_request(self):
        record = self.app.create_record()
        request = TestRequest()
        self.assertRaises(
            Exception,
            record.save,
            request
        )
        request = TestRequest(form=dict(
            block='frmBook',
            author='J. DosPassos',
            year='2015',
            publication='2015-11-15',
            weight='1.3',
        ))
        record.save(request)
        self.assertEquals(record['author'], "J. DOSPASSOS")
        self.assertEquals(record['year'], 2015)
        self.assertEquals(record['weight'], 1.3)
        self.assertEquals(
            record['publication'],
            datetime.strptime('2015-11-15', "%Y-%m-%d")
        )

    def test_compute_element_on_save(self):
        record = self.app.create_record()
        record.save(block_id="frmBook")
        self.assertEquals(
            record['famous_quote'],
            'A good plan violently executed now is better than a perfect plan '
            'executed next week.'
        )

    def test_render_number_elements(self):
        self.app_obj.set_fake_block_data('frmBook', 'html', """Author: {author}
 {year} {weight}<footer>Powered by Rapido</footer>""")
        if 'frmBook' in self.app._blocks:
            del self.app._blocks['frmBook']
        block = self.app.get_block('frmBook')
        record = self.app.create_record()
        record.save({
            'year': 1845,
            'weight': 3.2,
        })
        html = block.display(record, edit=True)
        self.assertTrue('<input type="number"\n'
            '        name="year" value="1845" />' in html)
        self.assertTrue('<input type="number"\n'
            '        name="weight" value="3.2" />' in html)

    def test_render_datetime_elements(self):
        self.app_obj.set_fake_block_data('frmBook', 'html', """Author: {author}
 {publication}<footer>Powered by Rapido</footer>""")
        if 'frmBook' in self.app._blocks:
            del self.app._blocks['frmBook']
        block = self.app.get_block('frmBook')
        html = block.display(None, edit=True)
        self.assertTrue('<input type="date"\n'
            '        name="publication" value="" />' in html)
        record = self.app.create_record()
        record.save({
            'publication': datetime.strptime('2015-11-15', "%Y-%m-%d"),
        })
        html = block.display(record, edit=True)
        self.assertTrue('<input type="date"\n'
            '        name="publication" value="2015-11-15" />' in html)

    def test_compute_element_using_record_data(self):
        block = self.app.get_block('frmBook7')
        record = self.app.create_record()
        record.save({
            'author': 'John DosPassos',
        })
        html = block.display(record, edit=True)
        self.assertTrue('Bonjour John DosPassos' in html)