##############################################################################
# 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: $
"""
import os, sys
if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))

from TeamTestCase import *
from Products.CMFCore.utils import getToolByName
from Products.TeamSpace.tool import TeamsTool
from Products.TeamSpace.team import Team

try:
    set=set
except NameError:
    from sets import Set as set


class TestTool(TeamTestCase):

    def afterSetUp(self):
        TeamTestCase.afterSetUp(self)
        
    def testTeamToolExists(self):
        tmtool = getattr(self.portal, 'portal_teams', None)
        self.assertNotEqual(tmtool, None)
        self.failUnless(isinstance(tmtool, TeamsTool))
        
    def testAddRemoveTeam(self):
        tmtool = self.tool
        
        #tmtool.addTeam(TEAM_1.id)
        tmtool.invokeFactory('Team', TEAM_1.id)

        team = tmtool.getTeamById(TEAM_1.id)
        self.failUnless(isinstance(team, Team))
        teams = tmtool.getTeams()
        self.failUnless(team in teams)
        # XXX test that team can be deleted

    def testGetTeamsByMemberId(self):
        tmtool = self.tool
        self.addAndPopulateTeam(TEAM_1)
        self.addAndPopulateTeam(TEAM_2)

        mem1id = members[0]  # should be in both teams
        mem1teams = tmtool.getTeamsByMemberId(mem1id)
        mem1tmIds = [team.id for team in mem1teams]
        self.failUnless(TEAM_1.id in mem1tmIds and TEAM_2.id in mem1tmIds)

        mem2id = members[1] # should only be in team 2
        mem2teams = tmtool.getTeamsByMemberId(mem2id)
        mem2tmIds = [team.id for team in mem2teams]
        self.failIf(TEAM_1.id in mem2tmIds)
        self.failUnless(TEAM_2.id in mem2tmIds)

        t2 = tmtool.getTeamById(TEAM_2.id)
        getattr(t2, mem2id).editTeamRoles(['Member'])
        mem2teams = tmtool.getTeamsByMemberId(mem2id, roles=['Member'])
        self.assertEqual(mem2teams[0].id, TEAM_2.id)

        mship = t2.getMemberships()[0]
        member_id = mship.id

        self.portal.portal_workflow.doActionFor(mship, 'activate')
        teams = tmtool.getTeamsByMemberId(mship.id, active=1)
        self.assertEqual([mship.getTeam()], teams)

    def testGetTeamMembershipsFor(self):
        tmtool = self.tool
        self.addAndPopulateTeam(TEAM_1)
        self.addAndPopulateTeam(TEAM_2)
        teamidset = set([TEAM_1.id, TEAM_2.id])

        mem1id = members[0]
        mem1mships = tmtool.getTeamMembershipsFor(mem1id)
        tm_ids = [mship.getTeam().getId() for mship in mem1mships]
        self.assertEqual(set(tm_ids), teamidset)

        mem1mships = tmtool.getTeamMembershipsFor(mem1id, active=True)
        tm_ids = [mship.getTeam().getId() for mship in mem1mships]
        self.failUnless(len(tm_ids) == 0)

        wft = self.portal.portal_workflow
        mem1mships = tmtool.getTeamMembershipsFor(mem1id)
        for mship in mem1mships:
            wft.doActionFor(mship, 'activate')
        mem1mships = tmtool.getTeamMembershipsFor(mem1id, active=True)
        tm_ids = [mship.getTeam().getId() for mship in mem1mships]
        self.assertEqual(set(tm_ids), teamidset)

    def test_SecurityModel(self):
        wf_tool = getToolByName(self.tool, 'portal_workflow')
        self.addAndPopulateTeam( TEAM_1 )
        team = self.tool._getOb( TEAM_1.id )
        memberids = TEAM_1.getMemberIds()
        user = self.portal.acl_users.getUserById('member_1')
        roles = user.getRolesInContext(team)
        self.failIf('Manager' in roles)
        membership = team._getOb('member_1')
        membership.editTeamRoles( ('Manager',) )

        self.loginAsPortalOwner() # so we can publish the membership
        wf_tool.doActionFor(membership, 'activate') # using membership workflow
        roles = user.getRolesInContext(team)
        self.failUnless('Manager' in roles)
        
    
if __name__ == '__main__':
    framework()
else:
    from unittest import TestSuite, makeSuite
    def test_suite():
        suite = TestSuite()
        suite.addTest(makeSuite(TestTool))
        return suite

