moin-2.0 / MoinMoin / storage / middleware / _tests / test_protecting.py

# Copyright: 2011 MoinMoin:ThomasWaldmann
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
MoinMoin - protecting middleware tests
"""


from __future__ import absolute_import, division

from StringIO import StringIO

import pytest

from ..protecting import ProtectingMiddleware, AccessDenied

from .test_indexing import TestIndexingMiddleware

UNPROTECTED = u'unprotected'
PROTECTED = u'protected'

UNPROTECTED_CONTENT = 'unprotected content'
PROTECTED_CONTENT = 'protected content'

acl_mapping = [
    ('', dict(before=u'', default=u'All:read,write,create', after=u'', hierarchic=False)),
]


class FakeUser(object):
    """
    fake user object, just to give user.name
    """
    def __init__(self, name):
        self.name = [name, ]

    @property
    def name0(self):
        return self.name[0]


class TestProtectingMiddleware(TestIndexingMiddleware):
    def setup_method(self, method):
        super(TestProtectingMiddleware, self).setup_method(method)
        self.imw = ProtectingMiddleware(self.imw, FakeUser(u'joe'), acl_mapping=acl_mapping)

    def teardown_method(self, method):
        self.imw = self.imw.indexer
        super(TestProtectingMiddleware, self).teardown_method(method)

    def _dummy(self):
        # replacement for tests that use unsupported methods / attributes
        pass

    test_index_rebuild = _dummy
    test_index_update = _dummy
    test_indexed_content = _dummy

    def make_items(self, unprotected_acl, protected_acl):
        items = [(UNPROTECTED, unprotected_acl, UNPROTECTED_CONTENT),
                 (PROTECTED, protected_acl, PROTECTED_CONTENT),
                ]
        revids = []
        for item_name, acl, content in items:
            item = self.imw[item_name]
            r = item.store_revision(dict(name=[item_name, ], acl=acl, contenttype=u'text/plain;charset=utf-8'),
                                    StringIO(content), return_rev=True)
            revids.append(r.revid)
        return revids

    def test_documents(self):
        revid_unprotected, revid_protected = self.make_items(u'joe:read', u'boss:read')
        revids = [rev.revid for rev in self.imw.documents()]
        assert revids == [revid_unprotected]  # without revid_protected!

    def test_getitem(self):
        revid_unprotected, revid_protected = self.make_items(u'joe:read', u'boss:read')
        # now testing:
        item = self.imw[UNPROTECTED]
        r = item[revid_unprotected]
        assert r.data.read() == UNPROTECTED_CONTENT
        item = self.imw[PROTECTED]
        with pytest.raises(AccessDenied):
            r = item[revid_protected]

    def test_write(self):
        revid_unprotected, revid_protected = self.make_items(u'joe:write', u'boss:write')
        # now testing:
        item = self.imw[UNPROTECTED]
        item.store_revision(dict(name=[UNPROTECTED, ], acl=u'joe:write', contenttype=u'text/plain;charset=utf-8'), StringIO(UNPROTECTED_CONTENT))
        item = self.imw[PROTECTED]
        with pytest.raises(AccessDenied):
            item.store_revision(dict(name=[PROTECTED, ], acl=u'boss:write', contenttype=u'text/plain;charset=utf-8'), StringIO(UNPROTECTED_CONTENT))

    def test_write_create(self):
        # now testing:
        item_name = u'newitem'
        item = self.imw[item_name]
        item.store_revision(dict(name=[item_name, ], contenttype=u'text/plain;charset=utf-8'), StringIO('new content'))

    def test_overwrite_revision(self):
        revid_unprotected, revid_protected = self.make_items(u'joe:write,destroy', u'boss:write,destroy')
        # now testing:
        item = self.imw[UNPROTECTED]
        item.store_revision(dict(name=[UNPROTECTED, ], acl=u'joe:write,destroy', contenttype=u'text/plain;charset=utf-8', revid=revid_unprotected),
                            StringIO(UNPROTECTED_CONTENT), overwrite=True)
        item = self.imw[PROTECTED]
        with pytest.raises(AccessDenied):
            item.store_revision(dict(name=[PROTECTED, ], acl=u'boss:write,destroy', contenttype=u'text/plain;charset=utf-8', revid=revid_protected),
                                StringIO(UNPROTECTED_CONTENT), overwrite=True)

    def test_destroy_revision(self):
        revid_unprotected, revid_protected = self.make_items(u'joe:destroy', u'boss:destroy')
        # now testing:
        item = self.imw[UNPROTECTED]
        item.destroy_revision(revid_unprotected)
        item = self.imw[PROTECTED]
        with pytest.raises(AccessDenied):
            item.destroy_revision(revid_protected)

    def test_destroy_item(self):
        revid_unprotected, revid_protected = self.make_items(u'joe:destroy', u'boss:destroy')
        # now testing:
        item = self.imw[UNPROTECTED]
        item.destroy_all_revisions()
        item = self.imw[PROTECTED]
        with pytest.raises(AccessDenied):
            item.destroy_all_revisions()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.