## $Id: test_interfaces.py 7193 2011-11-25 07:21:29Z henrik $
##
## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##

# Tests for interfaces and included components.
import grok
import unittest
from waeup.sirp.interfaces import DuplicationError, RoleSource
from waeup.sirp.testing import FunctionalLayer, FunctionalTestCase

class DuplicationErrorTests(unittest.TestCase):

    def test_is_exception(self):
        exc = DuplicationError('Some message')
        self.assertEqual(exc.msg, 'Some message')
        self.assertEqual(exc.entries, [])

    def test_as_string(self):
        exc = DuplicationError('Some message')
        self.assertEqual(exc.__str__(), "'Some message'")


class RoleSourceTests(FunctionalTestCase):

    layer = FunctionalLayer

    def setUp(self):
        super(RoleSourceTests, self).setUp()
        # Register a role
        class SomeRole(grok.Role):
            grok.name('waeup.testrole')
            grok.title('RoleWith.DotName')
            grok.permissions('waeup.Public')
        # Register a role not visible to waeup portal as its name does
        # not start with 'waeup.'
        class NonWAeUPRole(grok.Role):
            grok.name('nonwaeup.testrole')
            grok.title('Role not suitable for waeup')
            grok.permissions('waeup.Public')
        grok.testing.grok_component('SomeRole', SomeRole)
        grok.testing.grok_component('NonWAeUPRole', NonWAeUPRole)
        return

    def test_getValues(self):
        source = RoleSource()
        result = source.factory.getValues()
        self.assertTrue(u'waeup.testrole' in result)
        self.assertTrue(u'waeup.PortalManager' in result)
        self.assertTrue(u'nonwaeup.testrole' not in result)

    def test_getTitle(self):
        source = RoleSource()
        result = source.factory.getTitle(u'waeup.PortalManager')
        self.assertEqual(result, 'Portal Manager')

    def test_getTitleWithDot(self):
        # If there is a dot in the role title, we get only the last part
        source = RoleSource()
        result = source.factory.getTitle(u'waeup.testrole')
        self.assertEqual(result, 'DotName')
