##############################################################################
# Copyright (c) 2004-2005 Black Rock City, LLC
# Copyright (c) 2004-2005 ObjectRealms, LLC
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
##############################################################################
"""
$Id: $
"""
#
# Testing the terrible secrets of Space
#

import os, sys
if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))

# use Set class instead of set type b/c that's what PAS is returning
from sets import Set as set

from Acquisition import aq_base
from AccessControl import Unauthorized
from TeamTestCase import *
from Products.TeamSpace import PKG_NAME, GLOBALS
from AccessControl.SecurityManagement import newSecurityManager

class TestSpace(TeamTestCase):

    def afterSetUp(self):
        self.installer       = self.portal.portal_quickinstaller
        self.portal_types    = self.portal.portal_types
        self.pm_tool         = self.portal.portal_membership
        userdir              = self.portal.acl_users
        TeamTestCase.afterSetUp(self)

    def addTeamSpace(self, id):
        self.portal.portal_types.constructContent('TeamSpace', self.folder, id)

    def testInstallSpaceInPlone(self):
        installer = self.installer
        p_types = self.portal_types
        caninstall  = installer.isProductInstallable
        isinstalled = installer.isProductInstalled

        self.failUnless(caninstall)

        installer.installProduct(PKG_NAME)
        self.failUnless(isinstalled(PKG_NAME))

    def testAddDeleteSpace(self):
        self.portal.portal_types.constructContent('TeamSpace', self.folder, 'TS')
        self.failUnless(hasattr(self.folder, 'TS'), 'TeamSpace not added')

        # Delete the teamspace
        self.folder.manage_delObjects(ids=['TS'])
        self.failIf(hasattr(aq_base(self.folder),'TS'), 'TeamSpace not deleted')

    def testOwnerLocalRoleIsSet(self):
        self.portal.portal_types.constructContent('TeamSpace', self.folder, 'TS')
        space = self.folder.TS
        owners = space.users_with_local_role('Owner')
        self.failUnless(len(owners) == 1)
        self.failUnless(owners[0] == 'portal_owner')

    def prepPolicyTests(self, activate_all=0):

        self.addAndPopulateTeam(TEAM_1)
        self.addAndPopulateTeam(TEAM_2)
        t1_id = '%s_space' %TEAM_1.id
        t2_id = '%s_space' %TEAM_2.id

        self.addTeamSpace(t1_id)
        self.addTeamSpace(t2_id)
        self.teamspace_1 = getattr(self.folder, t1_id)
        self.teamspace_2 = getattr(self.folder, t2_id)
        self.teamspace_1.editTeams([TEAM_1.id])
        self.teamspace_2.editTeams([TEAM_2.id])

        if activate_all:
            [self.setStatusForMembership(TEAM) for TEAM in (TEAM_1, TEAM_2)]


    def testPolicyOnInactiveMembers(self):
        self.prepPolicyTests()

        uf = self.uf
        teamspace_1 = self.teamspace_1

        folder = 'folder_1'
        newSecurityManager(None, uf.getUserById(notmember))
        self.failUnlessRaises(Unauthorized, teamspace_1.invokeFactory, 'Folder', id=folder)

        newSecurityManager(None, uf.getUserById('member_1'))
        self.failUnlessRaises(Unauthorized, teamspace_1.invokeFactory, 'Folder', id=folder)

        newSecurityManager(None, uf.getUserById('member_4'))
        self.failUnlessRaises(Unauthorized, teamspace_1.invokeFactory, 'Folder', id=folder)

    def testSpaceSchema(self):

        self.addAndPopulateTeam(TEAM_1)
        self.addAndPopulateTeam(TEAM_2)
        self.addTeamSpace('TEST')
        space = self.folder.TEST
        schema = space.Schema()
        space.getSpaceTeamsDisplayList()

    def testPolicyOnActiveMembers(self):
        self.loginAsPortalOwner()
        uf = self.uf
        self.prepPolicyTests(activate_all=1)
        
        teamspace_1 = self.teamspace_1
        teamspace_2 = self.teamspace_2

        # add portal member who belongs to neither in TEAM_1 or TEAM_2
        self.pm_tool.addMember( notmember, notmember, ('Member',), None )

        # check the roles
        local_roles = uf.getAllLocalRoles(teamspace_1)
        self.assertEqual(local_roles['member_1'], set(['Member',]))
        self.assertEqual(local_roles['member_4'], set(['Member', 'Manager']))

        local_roles = uf.getAllLocalRoles(teamspace_2)
        self.assertEqual(local_roles['member_2'], set(['Member',]))
        self.assertEqual(local_roles['member_4'], set(['Member', 'Manager']))
        

if __name__ == '__main__':
    framework()
else:
    from unittest import TestSuite, makeSuite
    def test_suite():
        suite = TestSuite()
        suite.addTest(makeSuite(TestSpace))
        return suite

