# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2012-2019  Nexedi SA and Contributors.
#
# This program is free software: you can Use, Study, Modify and Redistribute
# it under the terms of the GNU General Public License version 3, or (at your
# option) any later version, as published by the Free Software Foundation.
#
# You can also Link and Combine this program with other software covered by
# the terms of any of the Free Software licenses or any of the Open Source
# Initiative approved licenses and Convey the resulting work. Corresponding
# source of such a combination shall include the source code for all other
# software used.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
#
##############################################################################
from erp5.component.test.SlapOSTestCaseMixin import SlapOSTestCaseMixinWithAbort
from AccessControl import getSecurityManager

class TestSlapOSGroupRoleSecurityCoverage(SlapOSTestCaseMixinWithAbort):
  maxDiff = None

  def testCoverage(self):
    """ Test which Portal types are not covered by this test.
    """

    test_source_code = ""
    for test_file_id in self.security_group_role_test_id_list:
      test_source_code += self.portal.portal_components[test_file_id].getTextContent()

    test_list = []
    for pt in self.portal.portal_types.objectValues():
      if 0 < len(pt.contentValues(portal_type="Role Information")):
        test_klass = "class Test%s(" % "".join(pt.getId().split(" "))
        if test_klass not in test_source_code:
          test_list.append(pt.getId())
    test_list.sort()
    self.assertEqual(test_list, [])

  def testLocalRoleGroup(self):
    """
    Check that all role definitions use a local role group
    XXX how to check the consistency
    """
    test_list = []
    for pt in self.portal.portal_types.objectValues():
      for role_information in pt.contentValues(portal_type="Role Information"):
        group = role_information.getLocalRoleGroupValue()
        if group is None:
          test_list.append('%s - %s' % (pt.getId(), role_information.getTitle()))
    test_list.sort()
    self.assertEqual(test_list, [])

class TestSlapOSGroupRoleSecurityMixin(SlapOSTestCaseMixinWithAbort):

  def afterSetUp(self):
    SlapOSTestCaseMixinWithAbort.afterSetUp(self)
    self.user_id = getSecurityManager().getUser().getId()

  def _getLocalRoles(self, context):
    return [x[0] for x in context.get_local_roles()]

  def _permissionsOfRole(self, context, role):
    return [x['name'] for x in context.permissionsOfRole(role) \
          if x['selected'] == 'SELECTED']

  def _acquirePermissions(self, context):
    return [x['name'] for x in context.permission_settings() \
          if x['acquire'] == 'CHECKED']

  def assertPermissionsOfRole(self, context, role, permission_list):
    self.assertSameSet(
      permission_list,
      self._permissionsOfRole(context, role))

  def assertAcquiredPermissions(self, context, permission_list):
    self.assertSameSet(
      permission_list,
      self._acquirePermissions(context))

  def assertSecurityGroup(self, context, security_group_list, acquired):
    self.assertEqual(acquired, context._getAcquireLocalRoles())
    self.assertSameSet(
      security_group_list,
      self._getLocalRoles(context)
    )

  def assertRoles(self, context, security_group, role_list):
    self.assertSameSet(
      role_list,
      context.get_local_roles_for_userid(security_group)
    )


class TestAccountModule(TestSlapOSGroupRoleSecurityMixin):
  def test_AccountModule(self):
    module = self.portal.account_module
    self.assertSecurityGroup(module,
        ['F-ACCOUNTING*', module.Base_getOwnerId(), 'R-SHADOW-PERSON'], False)
    self.assertRoles(module, 'F-ACCOUNTING*', ['Auditor', 'Author'])
    self.assertRoles(module, 'R-SHADOW-PERSON', ['Auditor'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])


class TestAccount(TestSlapOSGroupRoleSecurityMixin):
  def test_AccountingFunction(self):
    product = self.portal.account_module.newContent(
        portal_type='Account')
    self.assertSecurityGroup(product,
        ['F-ACCMAN', 'F-ACCAGT', self.user_id, 'R-SHADOW-PERSON'], False)
    self.assertRoles(product, 'F-ACCMAN', ['Assignor'])
    self.assertRoles(product, 'F-ACCAGT', ['Assignee'])
    self.assertRoles(product, 'R-SHADOW-PERSON', ['Auditor'])
    self.assertRoles(product, self.user_id, ['Owner'])


class TestAccountingPeriod(TestSlapOSGroupRoleSecurityMixin):
  def test_AccountingFunction(self):
    product = self.portal.organisation_module.newContent(
        portal_type='Organisation').newContent(
        portal_type='Accounting Period')
    self.assertSecurityGroup(product,
        ['F-ACCMAN', 'F-ACCAGT', self.user_id], False)
    self.assertRoles(product, 'F-ACCMAN', ['Assignor'])
    self.assertRoles(product, 'F-ACCAGT', ['Assignee'])
    self.assertRoles(product, self.user_id, ['Owner'])


class TestAccountingTransactionModule(TestSlapOSGroupRoleSecurityMixin):
  def test_AccountingTransactionModule(self):
    module = self.portal.accounting_module
    self.assertSecurityGroup(module,
        ['F-ACCOUNTING*', 'F-PRODUCTION*', module.Base_getOwnerId(),
         'R-SHADOW-PERSON', 'F-CUSTOMER'], True)
    self.assertRoles(module, 'F-ACCOUNTING*', ['Auditor', 'Author'])
    self.assertRoles(module, 'R-SHADOW-PERSON', ['Assignor'])
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor'])
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])


class TestPaymentTransaction(TestSlapOSGroupRoleSecurityMixin):
  def test_PaymentTransaction_AccountingFunction_LedgerNotAutomated(self):
    product = self.portal.accounting_module.newContent(
        portal_type='Payment Transaction')
    self.assertSecurityGroup(product,
        ['F-ACCMAN', 'F-ACCAGT', self.user_id], False)
    self.assertRoles(product, 'F-ACCMAN', ['Assignor'])
    self.assertRoles(product, 'F-ACCAGT', ['Assignee'])
    self.assertRoles(product, self.user_id, ['Owner'])

  def test_PaymentTransaction_AccountingFunction_LedgerAutomated(self):
    product = self.portal.accounting_module.newContent(
        portal_type='Payment Transaction')
    product.edit(ledger='automated')
    self.assertSecurityGroup(product,
        ['F-ACCOUNTING*', 'R-SHADOW-PERSON', self.user_id], False)
    self.assertRoles(product, 'F-ACCOUNTING*', ['Auditor'])
    self.assertRoles(product, 'R-SHADOW-PERSON', ['Assignee'])
    self.assertRoles(product, self.user_id, ['Owner'])

  def test_PaymentTransaction_UserWithoutLedger(self):
    reference = 'TESTPERSON-%s' % self.generateNewId()
    person = self.portal.person_module.newContent(portal_type='Person',
        reference=reference)
    product = self.portal.accounting_module.newContent(
        portal_type='Payment Transaction')
    product.edit(
        destination_section_value=person,
        )
    self.assertSecurityGroup(product,
        ['F-ACCMAN', 'F-ACCAGT', self.user_id], False)
    self.assertRoles(product, 'F-ACCMAN', ['Assignor'])
    self.assertRoles(product, 'F-ACCAGT', ['Assignee'])
    self.assertRoles(product, self.user_id, ['Owner'])

  def test_PaymentTransaction_UserLedger(self):
    reference = 'TESTPERSON-%s' % self.generateNewId()
    person = self.portal.person_module.newContent(portal_type='Person',
        reference=reference)
    product = self.portal.accounting_module.newContent(
        portal_type='Payment Transaction')
    product.edit(
        destination_section_value=person,
        ledger='automated'
        )
    shadow_user_id = 'SHADOW-%s' % person.getUserId()
    self.assertSecurityGroup(product,
        ['F-ACCOUNTING*', self.user_id, person.getUserId(),
         shadow_user_id], False)
    self.assertRoles(product, 'F-ACCOUNTING*', ['Auditor'])
    self.assertRoles(product, shadow_user_id, ['Auditor'])
    self.assertRoles(product, person.getUserId(), ['Auditor'])
    self.assertRoles(product, self.user_id, ['Owner'])


class TestSaleInvoiceTransaction(TestSlapOSGroupRoleSecurityMixin):
  def test_SaleInvoiceTransaction_AccountingFunction_LedgerNotAutomated(self):
    product = self.portal.accounting_module.newContent(
        portal_type='Sale Invoice Transaction')
    self.assertSecurityGroup(product,
        ['F-ACCMAN', 'F-ACCAGT', self.user_id], False)
    self.assertRoles(product, 'F-ACCMAN', ['Assignor'])
    self.assertRoles(product, 'F-ACCAGT', ['Assignee'])
    self.assertRoles(product, self.user_id, ['Owner'])

  def test_SaleInvoiceTransaction_AccountingFunction_LedgerAutomated(self):
    product = self.portal.accounting_module.newContent(
        portal_type='Sale Invoice Transaction')
    product.edit(ledger='automated')
    self.assertSecurityGroup(product,
        ['F-ACCOUNTING*', self.user_id, 'R-SHADOW-PERSON'], False)
    self.assertRoles(product, 'F-ACCOUNTING*', ['Auditor'])
    self.assertRoles(product, 'R-SHADOW-PERSON', ['Assignee'])
    self.assertRoles(product, self.user_id, ['Owner'])

  def test_SaleInvoiceTransaction_User(self):
    reference = 'TESTPERSON-%s' % self.generateNewId()
    person = self.portal.person_module.newContent(portal_type='Person',
        reference=reference)
    product = self.portal.accounting_module.newContent(
        portal_type='Sale Invoice Transaction')
    product.edit(
        ledger='automated',
        destination_section_value=person,
        )
    self.assertSecurityGroup(product,
        ['F-ACCOUNTING*', self.user_id, person.getUserId(),
         'R-SHADOW-PERSON'], False)
    self.assertRoles(product, 'F-ACCOUNTING*', ['Auditor'])
    self.assertRoles(product, person.getUserId(), ['Auditor'])
    self.assertRoles(product, 'R-SHADOW-PERSON', ['Assignee'])
    self.assertRoles(product, self.user_id, ['Owner'])


class TestAccountingTransaction(TestSlapOSGroupRoleSecurityMixin):
  def test_AccountingTransaction_LedgerNotAutomated(self):
    product = self.portal.accounting_module.newContent(
        portal_type='Accounting Transaction')
    self.assertSecurityGroup(product,
        ['F-ACCMAN', 'F-ACCAGT', self.user_id], False)
    self.assertRoles(product, 'F-ACCMAN', ['Assignor'])
    self.assertRoles(product, 'F-ACCAGT', ['Assignee'])
    self.assertRoles(product, self.user_id, ['Owner'])

  def test_AccountingTransaction_LedgerAutomated(self):
    product = self.portal.accounting_module.newContent(
        portal_type='Accounting Transaction')
    product.edit(ledger='automated')
    self.assertSecurityGroup(product,
        ['F-ACCOUNTING*', self.user_id], False)
    self.assertRoles(product, 'F-ACCOUNTING*', ['Auditor'])
    self.assertRoles(product, self.user_id, ['Owner'])


class TestBalanceTransaction(TestSlapOSGroupRoleSecurityMixin):
  def test_AccountingFunction_LedgerNotAutomated(self):
    product = self.portal.accounting_module.newContent(
        portal_type='Balance Transaction')
    self.assertSecurityGroup(product,
        ['F-ACCMAN', 'F-ACCAGT', self.user_id], False)
    self.assertRoles(product, 'F-ACCMAN', ['Assignor'])
    self.assertRoles(product, 'F-ACCAGT', ['Assignee'])
    self.assertRoles(product, self.user_id, ['Owner'])

  def test_AccountingFunction_LedgerAutomated(self):
    product = self.portal.accounting_module.newContent(
        portal_type='Balance Transaction')
    product.edit(ledger='automated')
    self.assertSecurityGroup(product,
        ['F-ACCOUNTING*', self.user_id], False)
    self.assertRoles(product, 'F-ACCOUNTING*', ['Auditor'])
    self.assertRoles(product, self.user_id, ['Owner'])


class TestPurchaseInvoiceTransaction(TestSlapOSGroupRoleSecurityMixin):
  def test_AccountingFunction_LedgerNotAutomated(self):
    product = self.portal.accounting_module.newContent(
        portal_type='Purchase Invoice Transaction')
    self.assertSecurityGroup(product,
        ['F-ACCMAN', 'F-ACCAGT', self.user_id], False)
    self.assertRoles(product, 'F-ACCMAN', ['Assignor'])
    self.assertRoles(product, 'F-ACCAGT', ['Assignee'])
    self.assertRoles(product, self.user_id, ['Owner'])

  def test_AccountingFunction_LedgerAutomated(self):
    product = self.portal.accounting_module.newContent(
        portal_type='Purchase Invoice Transaction')
    product.edit(ledger='automated')
    self.assertSecurityGroup(product,
        ['F-ACCOUNTING*', self.user_id], False)
    self.assertRoles(product, 'F-ACCOUNTING*', ['Auditor'])
    self.assertRoles(product, self.user_id, ['Owner'])


class TestAllocationSupplyModule(TestSlapOSGroupRoleSecurityMixin):
  def test_AllocationSupplyModule(self):
    module = self.portal.allocation_supply_module
    self.assertSecurityGroup(module,
        ['F-PRODUCTION*', 'F-CUSTOMER', module.Base_getOwnerId()], False)
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor', 'Author'])
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])


class TestAllocationSupply(TestSlapOSGroupRoleSecurityMixin):
  def test_AllocationSupply_default(self):
    supply = self.portal.allocation_supply_module.newContent(
        portal_type='Allocation Supply')
    self.assertSecurityGroup(supply,
        [self.user_id], False)
    self.assertRoles(supply, self.user_id, ['Owner'])

  def test_AllocationSupply_DestinationCustomer(self):
    reference = 'TESTPERSON-%s' % self.generateNewId()
    person = self.portal.person_module.newContent(portal_type='Person',
        reference=reference)

    supply = self.portal.allocation_supply_module.newContent(
        portal_type='Allocation Supply')
    supply.edit(
        destination_value=person,
        )
    self.assertSecurityGroup(supply,
        [self.user_id], False)
    self.assertRoles(supply, self.user_id, ['Owner'])

    supply.validate()
    self.assertSecurityGroup(supply,
        [person.getUserId(), self.user_id], False)
    self.assertRoles(supply, person.getUserId(), ['Auditor'])
    self.assertRoles(supply, self.user_id, ['Owner'])

    supply.invalidate()
    self.assertSecurityGroup(supply,
        [self.user_id], False)
    self.assertRoles(supply, self.user_id, ['Owner'])

  def test_AllocationSupply_DestinationProject(self):
    project = self.addProject()
    supply = self.portal.allocation_supply_module.newContent(
        portal_type='Allocation Supply')
    supply.edit(
        destination_project_value=project,
        )

    self.assertSecurityGroup(supply, [self.user_id,
        '%s_F-PRODAGNT' % project.getReference(),
        '%s_F-PRODMAN' % project.getReference()], False)
    self.assertRoles(supply, self.user_id, ['Owner'])
    self.assertRoles(supply, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(supply, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])

    supply.validate()
    self.assertSecurityGroup(supply, [self.user_id,
        '%s_F-CUSTOMER' % project.getReference(),
        '%s_F-PRODAGNT' % project.getReference(),
        '%s_F-PRODMAN' % project.getReference()], False)
    self.assertRoles(supply, self.user_id, ['Owner'])
    self.assertRoles(supply, '%s_F-CUSTOMER' % project.getReference(), ['Auditor'])
    self.assertRoles(supply, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(supply, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])

    supply.invalidate()
    self.assertSecurityGroup(supply, [self.user_id,
        '%s_F-PRODAGNT' % project.getReference(),
        '%s_F-PRODMAN' % project.getReference()], False)
    self.assertRoles(supply, self.user_id, ['Owner'])
    self.assertRoles(supply, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(supply, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])


class TestAssignment(TestSlapOSGroupRoleSecurityMixin):
  def test_Assignment_Company_Group(self):
    assignment = self.portal.person_module.newContent(
        portal_type='Person').newContent(portal_type='Assignment')
    self.assertSecurityGroup(assignment,
        ['F-IS*', self.user_id], False)
    self.assertRoles(assignment, 'F-IS*', ['Auditor'])
    self.assertRoles(assignment, self.user_id, ['Owner'])


class TestComputeNodeModule(TestSlapOSGroupRoleSecurityMixin):
  def test_ComputeNodeModule(self):
    module = self.portal.compute_node_module
    self.assertSecurityGroup(module,
        ['F-PRODUCTION*', 'R-COMPUTER', 'F-CUSTOMER', 'R-INSTANCE', module.Base_getOwnerId()],
        False)
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor'])
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor', 'Author'])
    self.assertRoles(module, 'R-COMPUTER', ['Auditor'])
    self.assertRoles(module, 'R-INSTANCE', ['Auditor'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])


class TestComputeNode(TestSlapOSGroupRoleSecurityMixin):
  def test_ComputeNode_userId(self):
    compute_node = self.portal.compute_node_module.newContent(portal_type='Compute Node')
    self.assertSecurityGroup(compute_node,
        [self.user_id, compute_node.getUserId()], False)
    self.assertRoles(compute_node, self.user_id, ['Owner'])
    self.assertRoles(compute_node, compute_node.getUserId(), ['Assignor'])

    compute_node.edit(user_id=None)
    self.assertSecurityGroup(compute_node,
        [self.user_id], False)
    self.assertRoles(compute_node, self.user_id, ['Owner'])

  def test_ComputeNode_ProjectMember(self):
    project = self.addProject()
    compute_node = self.portal.compute_node_module.newContent(
        portal_type='Compute Node')
    compute_node.edit(
        follow_up_value=project)

    self.assertSecurityGroup(compute_node, [
      self.user_id,
      compute_node.getUserId(),
      '%s_F-PRODAGNT' % project.getReference(),
      '%s_F-PRODMAN' % project.getReference(),
      '%s_F-CUSTOMER' % project.getReference(),
      '%s_R-INSTANCE' % project.getReference(),
    ], False)
    self.assertRoles(compute_node, self.user_id, ['Owner'])
    self.assertRoles(compute_node, compute_node.getUserId(), ['Assignor'])
    self.assertRoles(compute_node, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])
    self.assertRoles(compute_node, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(compute_node, '%s_F-CUSTOMER' % project.getReference(), ['Auditor'])
    self.assertRoles(compute_node, '%s_R-INSTANCE' % project.getReference(), ['Auditor'])


class TestComputerModelModule(TestSlapOSGroupRoleSecurityMixin):
  def test_ComputerModelModule(self):
    module = self.portal.computer_model_module
    self.assertSecurityGroup(module,
        ['F-PRODUCTION*', 'F-CUSTOMER', 'R-SHADOW-PERSON', module.Base_getOwnerId()],
        False)
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor'])
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor', 'Author'])
    self.assertRoles(module, 'R-SHADOW-PERSON', ['Auditor'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])


class TestComputerModel(TestSlapOSGroupRoleSecurityMixin):
  document_portal_type = 'Computer Model'
  def test_ComputerModel_default(self):
    model = self.portal.getDefaultModuleValue(self.document_portal_type).newContent(
        portal_type=self.document_portal_type)
    self.assertSecurityGroup(model,
        ['R-SHADOW-PERSON', self.user_id], False)
    self.assertRoles(model, 'R-SHADOW-PERSON', ['Auditor'])
    self.assertRoles(model, self.user_id, ['Owner'])

  def test_ComputerModel_ProjectMember(self):
    project = self.addProject()
    compute_node = self.portal.getDefaultModuleValue(self.document_portal_type).newContent(
        portal_type=self.document_portal_type)
    compute_node.edit(
        follow_up_value=project)

    self.assertSecurityGroup(compute_node, [
      self.user_id,
      'R-SHADOW-PERSON',
      '%s_F-PRODAGNT' % project.getReference(),
      '%s_F-PRODMAN' % project.getReference(),
      '%s_F-CUSTOMER' % project.getReference(),
    ], False)
    self.assertRoles(compute_node, self.user_id, ['Owner'])
    self.assertRoles(compute_node, 'R-SHADOW-PERSON', ['Auditor'])
    self.assertRoles(compute_node, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])
    self.assertRoles(compute_node, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(compute_node, '%s_F-CUSTOMER' % project.getReference(), ['Auditor'])


class TestComputerNetworkModule(TestSlapOSGroupRoleSecurityMixin):
  def test_ComputerNetworkModule(self):
    module = self.portal.computer_network_module
    self.assertSecurityGroup(module,
        ['F-PRODUCTION*', 'F-CUSTOMER', 'R-SHADOW-PERSON', module.Base_getOwnerId()],
        False)
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor'])
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor', 'Author'])
    self.assertRoles(module, 'R-SHADOW-PERSON', ['Auditor'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])


class TestComputerNetwork(TestComputerModel):
  document_portal_type = 'Computer Network'



class TestCurrencyModule(TestSlapOSGroupRoleSecurityMixin):
  def test_CurrencyModule(self):
    module = self.portal.currency_module
    self.assertSecurityGroup(module,
        ['F-ACCOUNTING*', 'F-PRODUCTION*', module.Base_getOwnerId(),
         'R-SHADOW-PERSON', 'F-CUSTOMER', 'F-SALE*'], True)
    self.assertRoles(module, 'F-ACCOUNTING*', ['Auditor', 'Author'])
    self.assertRoles(module, 'R-SHADOW-PERSON', ['Auditor'])
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor'])
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor'])
    self.assertRoles(module, 'F-SALE*', ['Auditor'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])


class TestCurrency(TestSlapOSGroupRoleSecurityMixin):
  def test_Currency(self):
    product = self.portal.currency_module.newContent(
        portal_type='Currency')
    self.assertSecurityGroup(product,
        ['F-ACCOUNTING*', 'F-PRODUCTION*', self.user_id,
         'R-SHADOW-PERSON', 'F-CUSTOMER', 'F-SALE*'], False)
    self.assertRoles(product, 'F-ACCOUNTING*', ['Auditor', 'Author'])
    self.assertRoles(product, 'R-SHADOW-PERSON', ['Auditor'])
    self.assertRoles(product, 'F-PRODUCTION*', ['Auditor'])
    self.assertRoles(product, 'F-CUSTOMER', ['Auditor'])
    self.assertRoles(product, 'F-SALE*', ['Auditor'])
    self.assertRoles(product, self.user_id, ['Owner'])


class TestEventModule(TestSlapOSGroupRoleSecurityMixin):
  def test(self):
    module = self.portal.event_module
    self.assertSecurityGroup(module,
        ['F-PRODUCTION*', 'F-CUSTOMER', module.Base_getOwnerId()], True)
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor', 'Author'])
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor', 'Author'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])


class TestMailMessage(TestSlapOSGroupRoleSecurityMixin):
  event_portal_type = 'Mail Message'
  def test_default(self):
    product = self.portal.event_module.newContent(
        portal_type=self.event_portal_type)
    self.assertSecurityGroup(product,
        [self.user_id], False)
    self.assertRoles(product, self.user_id, ['Owner'])

  def test_SourceCustomer(self):
    reference = 'TESTPERSON-%s' % self.generateNewId()
    person = self.portal.person_module.newContent(portal_type='Person',
        reference=reference)
    product = self.portal.event_module.newContent(
        portal_type=self.event_portal_type)
    product.edit(
        source_value=person,
        )
    self.assertSecurityGroup(product,
        [person.getUserId(), self.user_id], False)
    self.assertRoles(product, person.getUserId(), ['Auditor'])
    self.assertRoles(product, self.user_id, ['Owner'])

  def test_DestinationCustomer(self):
    reference = 'TESTPERSON-%s' % self.generateNewId()
    person = self.portal.person_module.newContent(portal_type='Person',
        reference=reference)
    product = self.portal.event_module.newContent(
        portal_type=self.event_portal_type)
    product.edit(
        destination_value=person,
        )
    self.assertSecurityGroup(product,
        [person.getUserId(), self.user_id], False)
    self.assertRoles(product, person.getUserId(), ['Auditor'])
    self.assertRoles(product, self.user_id, ['Owner'])

  def test_SourceProject(self):
    project = self.addProject()
    event = self.portal.event_module.newContent(
        portal_type=self.event_portal_type)
    event.edit(
        source_project_value=project
        )

    self.assertSecurityGroup(event, [self.user_id,
        '%s_F-PRODAGNT' % project.getReference(),
        '%s_F-PRODMAN' % project.getReference()], False)
    self.assertRoles(event, self.user_id, ['Owner'])
    self.assertRoles(event, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(event, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])

  def test_DestinationProject(self):
    project = self.addProject()
    event = self.portal.event_module.newContent(
        portal_type=self.event_portal_type)
    event.edit(
        destination_project_value=project
        )

    self.assertSecurityGroup(event, [self.user_id,
        '%s_F-PRODAGNT' % project.getReference(),
        '%s_F-PRODMAN' % project.getReference()], False)
    self.assertRoles(event, self.user_id, ['Owner'])
    self.assertRoles(event, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(event, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])


class TestShortMessage(TestMailMessage):
  event_portal_type = 'Short Message'

class TestSiteMessage(TestMailMessage):
  event_portal_type = 'Site Message'

class TestWebMessage(TestMailMessage):
  event_portal_type = 'Web Message'

class TestNote(TestMailMessage):
  event_portal_type = 'Note'

class TestPhoneCall(TestMailMessage):
  event_portal_type = 'Phone Call'

class TestVisit(TestMailMessage):
  event_portal_type = 'Visit'

class TestFaxMessage(TestMailMessage):
  event_portal_type = 'Fax Message'

class TestLetter(TestMailMessage):
  event_portal_type = 'Letter'

class TestAcknowledgement(TestMailMessage):
  event_portal_type = 'Acknowledgement'


class TestInstanceTreeModule(TestSlapOSGroupRoleSecurityMixin):
  def test_InstanceTreeModule(self):
    module = self.portal.instance_tree_module
    self.assertSecurityGroup(module,
        ['F-PRODUCTION*', 'R-COMPUTER', 'F-CUSTOMER', 'R-INSTANCE', module.Base_getOwnerId()], False)
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor', 'Author'])
    self.assertRoles(module, 'R-COMPUTER', ['Auditor'])
    self.assertRoles(module, 'R-INSTANCE', ['Auditor'])
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])


class TestInstanceTree(TestSlapOSGroupRoleSecurityMixin):
  def test_InstanceTree_RelatedSoftwareInstanceGroup(self):
    reference = 'TESTHS-%s' % self.generateNewId()
    subscription = self.portal.instance_tree_module.newContent(
        portal_type='Instance Tree')
    subscription.edit(reference=reference)

    self.assertSecurityGroup(subscription, [self.user_id, reference], False)
    self.assertRoles(subscription, reference, ['Assignee'])
    self.assertRoles(subscription, self.user_id, ['Owner'])

  def test_InstanceTree_CustomOfTheInstanceTree(self):
    customer_reference = 'TESTPERSON-%s' % self.generateNewId()
    customer = self.portal.person_module.newContent(
        portal_type='Person', reference=customer_reference)
    reference = 'TESTHS-%s' % self.generateNewId()
    subscription = self.portal.instance_tree_module.newContent(
        portal_type='Instance Tree', reference=reference)
    subscription.edit(
        destination_section_value=customer)

    self.assertSecurityGroup(subscription, [self.user_id, reference,
        customer.getUserId()], False)
    self.assertRoles(subscription, reference, ['Assignee'])
    self.assertRoles(subscription, customer.getUserId(), ['Assignee'])
    self.assertRoles(subscription, self.user_id, ['Owner'])

  def test_InstanceTree_ProjectMember(self):
    project = self.addProject()
    reference = 'TESTHS-%s' % self.generateNewId()
    project = self.portal.project_module.newContent(
        portal_type='Project')
    subscription = self.portal.instance_tree_module.newContent(
        portal_type='Instance Tree', reference=reference)
    subscription.edit(
        follow_up_value=project)
    self.assertSecurityGroup(subscription, [self.user_id, reference,
        '%s_F-PRODAGNT' % project.getReference(),
        '%s_F-PRODMAN' % project.getReference()], False)
    self.assertRoles(subscription, reference, ['Assignee'])
    self.assertRoles(subscription, self.user_id, ['Owner'])
    self.assertRoles(subscription, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(subscription, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])


class TestSoftwareInstallationModule(TestSlapOSGroupRoleSecurityMixin):
  def test_SoftwareInstallationModule(self):
    module = self.portal.software_installation_module
    self.assertSecurityGroup(module,
        ['F-PRODUCTION*', 'F-CUSTOMER', 'R-COMPUTER', module.Base_getOwnerId()], False)
    self.assertRoles(module, 'R-COMPUTER', ['Auditor'])
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor'])
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor', 'Author'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])


class TestSoftwareInstallation(TestSlapOSGroupRoleSecurityMixin):
  def test_SoftwareInstallation_default(self):
    installation = self.portal.software_installation_module.newContent(
        portal_type='Software Installation')

    self.assertSecurityGroup(installation, [self.user_id], False)
    self.assertRoles(installation, self.user_id, ['Owner'])

  def test_SoftwareInstallation_ComputeNode(self):
    compute_node_reference = 'TESTCOMP-%s' % self.generateNewId()
    compute_node = self.portal.compute_node_module.template_compute_node\
        .Base_createCloneDocument(batch_mode=1)
    compute_node.edit(reference=compute_node_reference)

    installation = self.portal.software_installation_module.newContent(
        portal_type='Software Installation')
    installation.edit(
        aggregate_value=compute_node)

    self.assertSecurityGroup(installation, [self.user_id,
        compute_node.getUserId()], False)
    self.assertRoles(installation, compute_node.getUserId(), ['Assignor'])
    self.assertRoles(installation, self.user_id, ['Owner'])

  def test_SoftwareInstallation_ProjectMember(self):
    project = self.addProject()
    installation = self.portal.software_installation_module.newContent(
        portal_type='Software Installation')
    installation.edit(
        follow_up_value=project)

    self.assertSecurityGroup(installation, [
      self.user_id,
      '%s_F-PRODAGNT' % project.getReference(),
      '%s_F-PRODMAN' % project.getReference(),
      '%s_F-CUSTOMER' % project.getReference(),
    ], False)
    self.assertRoles(installation, self.user_id, ['Owner'])
    self.assertRoles(installation, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])
    self.assertRoles(installation, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(installation, '%s_F-CUSTOMER' % project.getReference(), ['Auditor'])


class TestSoftwareInstanceModule(TestSlapOSGroupRoleSecurityMixin):
  def test_SoftwareInstanceModule(self):
    module = self.portal.software_instance_module
    self.assertSecurityGroup(module,
        ['F-PRODUCTION*', 'R-COMPUTER', 'R-INSTANCE', 'F-CUSTOMER', module.Base_getOwnerId()], False)
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor', 'Author'])
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor'])
    self.assertRoles(module, 'R-COMPUTER', ['Auditor'])
    self.assertRoles(module, 'R-INSTANCE', ['Auditor', 'Author'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])


class TestSoftwareInstance(TestSlapOSGroupRoleSecurityMixin):
  def test_SoftwareInstance_default(self):
    instance = self.portal.software_instance_module.newContent(
        portal_type='Software Instance')

    self.assertSecurityGroup(instance, [self.user_id], False)
    self.assertRoles(instance, self.user_id, ['Owner'])

  def test_SoftwareInstance_CustomerOfTheInstance(self):
    customer_reference = 'TESTPERSON-%s' % self.generateNewId()
    customer = self.portal.person_module.newContent(
        portal_type='Person', reference=customer_reference)

    subscription_reference = 'TESTHS-%s ' % self.generateNewId()
    subscription = self.portal.instance_tree_module.newContent(
        portal_type='Instance Tree',
        reference=subscription_reference,
        destination_section=customer.getRelativeUrl())

    instance = self.portal.software_instance_module.newContent(
        portal_type='Software Instance')
    instance.edit(specialise=subscription.getRelativeUrl())

    self.assertSecurityGroup(instance, [customer.getUserId(),
        subscription_reference, self.user_id], False)
    self.assertRoles(instance, customer.getUserId(), ['Assignee'])
    self.assertRoles(instance, subscription_reference, ['Assignee'])
    self.assertRoles(instance, self.user_id, ['Owner'])

  def test_SoftwareInstance_ProjectMember(self):
    project = self.addProject()

    instance = self.portal.software_instance_module.newContent(
      portal_type='Software Instance')
    instance.edit(
      follow_up_value=project
    )

    self.assertSecurityGroup(instance, [
      self.user_id,
      '%s_F-PRODAGNT' % project.getReference(),
      '%s_F-PRODMAN' % project.getReference(),
    ], False)
    self.assertRoles(instance, self.user_id, ['Owner'])
    self.assertRoles(instance, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])
    self.assertRoles(instance, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])

  def test_SoftwareInstance_ComputeNode(self):
    compute_node_reference = 'TESTCOMP-%s' % self.generateNewId()
    compute_node = self.portal.compute_node_module.template_compute_node\
        .Base_createCloneDocument(batch_mode=1)
    compute_node.edit(reference=compute_node_reference)
    partition = compute_node.newContent(portal_type='Compute Partition')

    self.commit()

    instance = self.portal.software_instance_module.newContent(
        portal_type='Software Instance')
    instance.edit(aggregate_value=partition)

    self.assertSecurityGroup(instance, [compute_node.getUserId(),
        self.user_id], False)
    self.assertRoles(instance, compute_node.getUserId(), ['Assignor'])
    self.assertRoles(instance, self.user_id, ['Owner'])


class TestSlaveInstance(TestSlapOSGroupRoleSecurityMixin):
  def test_SlaveInstance_default(self):
    instance = self.portal.software_instance_module.newContent(
        portal_type='Slave Instance')

    self.assertSecurityGroup(instance, [self.user_id], False)
    self.assertRoles(instance, self.user_id, ['Owner'])

  def test_SlaveInstance_CustomerOfTheInstance(self):
    customer_reference = 'TESTPERSON-%s' % self.generateNewId()
    customer = self.portal.person_module.newContent(
        portal_type='Person', reference=customer_reference)

    subscription_reference = 'TESTHS-%s ' % self.generateNewId()
    subscription = self.portal.instance_tree_module.newContent(
        portal_type='Instance Tree',
        reference=subscription_reference,
        destination_section=customer.getRelativeUrl())

    instance = self.portal.software_instance_module.newContent(
        portal_type='Slave Instance')
    instance.edit(specialise=subscription.getRelativeUrl())

    self.assertSecurityGroup(instance, [customer.getUserId(),
        subscription_reference, self.user_id], False)
    self.assertRoles(instance, customer.getUserId(), ['Assignee'])
    self.assertRoles(instance, subscription_reference, ['Assignee'])
    self.assertRoles(instance, self.user_id, ['Owner'])

  def test_SlaveInstance_ProjectMember(self):
    project = self.addProject()
    instance = self.portal.software_instance_module.newContent(
      portal_type='Slave Instance')
    instance.edit(
      follow_up_value=project
    )

    self.assertSecurityGroup(instance, [
      self.user_id,
      '%s_F-PRODAGNT' % project.getReference(),
      '%s_F-PRODMAN' % project.getReference()
    ], False)
    self.assertRoles(instance, self.user_id, ['Owner'])
    self.assertRoles(instance, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])
    self.assertRoles(instance, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])

  def test_SlaveInstance_SoftwareInstanceWhichProvidesThisSlaveInstance(self):
    compute_node_reference = 'TESTCOMP-%s' % self.generateNewId()
    compute_node = self.portal.compute_node_module.template_compute_node\
        .Base_createCloneDocument(batch_mode=1)
    compute_node.edit(reference=compute_node_reference)
    partition = compute_node.newContent(portal_type='Compute Partition')

    provider_reference = 'TESTSI-%s' % self.generateNewId()

    provider = self.portal.software_instance_module\
        .template_software_instance.Base_createCloneDocument(batch_mode=1)

    provider.edit(reference=provider_reference,
        aggregate=partition.getRelativeUrl())
    provider.validate()

    self.tic()

    instance = self.portal.software_instance_module.newContent(
        portal_type='Slave Instance')
    instance.edit(aggregate=partition.getRelativeUrl())
    self.assertSecurityGroup(instance, [provider.getUserId(),
        compute_node.getUserId(), self.user_id], False)
    self.assertRoles(instance, provider.getUserId(), ['Assignor'])
    self.assertRoles(instance, compute_node.getUserId(), ['Assignor'])
    self.assertRoles(instance, self.user_id, ['Owner'])


class TestSoftwareProductModule(TestSlapOSGroupRoleSecurityMixin):
  def test_SoftwareProductModule(self):
    module = self.portal.software_product_module
    self.assertSecurityGroup(module,
        ['F-PRODUCTION*', 'F-CUSTOMER', module.Base_getOwnerId()], False)
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor'])
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor', 'Author'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])


class TestSoftwareProduct(TestSlapOSGroupRoleSecurityMixin):
  def test_SoftwareProduct_default(self):
    product = self.portal.software_product_module.newContent(
        portal_type='Software Product')
    self.assertSecurityGroup(product,
        [self.user_id], False)
    self.assertRoles(product, self.user_id, ['Owner'])

  def test_SoftwareProduct_Project(self):
    project = self.addProject()
    product = self.portal.software_product_module.newContent(
        portal_type='Software Product')
    product.edit(
        follow_up_value=project)
    self.assertSecurityGroup(product, [self.user_id,
        '%s_F-CUSTOMER' % project.getReference(),
        '%s_F-PRODAGNT' % project.getReference(),
        '%s_F-PRODMAN' % project.getReference()], False)
    self.assertRoles(product, self.user_id, ['Owner'])
    self.assertRoles(product, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(product, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])
    self.assertRoles(product, '%s_F-CUSTOMER' % project.getReference(), ['Auditor'])


class TestSupportRequestModule(TestSlapOSGroupRoleSecurityMixin):
  def test_SupportRequestModule(self):
    module = self.portal.support_request_module
    self.assertSecurityGroup(module,
        ['F-PRODUCTION*', 'F-CUSTOMER', module.Base_getOwnerId()], True)
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor', 'Author'])
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor', 'Author'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])


class TestSupportRequest(TestSlapOSGroupRoleSecurityMixin):
  ticket_portal_type = 'Support Request'

  def test_SupportRequest_default(self):
    support_request = self.portal.getDefaultModuleValue(self.ticket_portal_type).newContent(
        portal_type=self.ticket_portal_type)
    self.assertSecurityGroup(support_request,
        [self.user_id], False)
    self.assertRoles(support_request, self.user_id, ['Owner'])

  def test_SupportRequest_Customer(self):
    reference = 'TESTPERSON-%s' % self.generateNewId()
    person = self.portal.person_module.newContent(portal_type='Person',
        reference=reference)
    support_request = self.portal.getDefaultModuleValue(self.ticket_portal_type).newContent(
        portal_type=self.ticket_portal_type)
    support_request.edit(
        destination_decision_value=person,
        )
    self.assertSecurityGroup(support_request,
        [person.getUserId(), self.user_id], False)
    self.assertRoles(support_request, person.getUserId(), ['Auditor'])
    self.assertRoles(support_request, self.user_id, ['Owner'])

  def test_SupportRequest_Template(self):
    support_request = self.portal.restrictedTraverse(getattr(
      self.portal.portal_preferences,
      'getPreferred%sTemplate' % self.ticket_portal_type.title().replace(' ', '')
    )())
    assert support_request.getPortalType() == self.ticket_portal_type
    self.assertSecurityGroup(support_request,
        [support_request.Base_getOwnerId(), 'R-MEMBER'], False)
    self.assertRoles(support_request, support_request.Base_getOwnerId(), ['Owner'])
    self.assertRoles(support_request, 'R-MEMBER', ['Auditor'])
    self.assertPermissionsOfRole(support_request, 'Auditor',
        ['Access contents information', 'View'])

  def test_SupportRequest_SourceProject(self):
    project = self.addProject()
    support_request = self.portal.getDefaultModuleValue(self.ticket_portal_type).newContent(
        portal_type=self.ticket_portal_type)
    support_request.edit(
        source_project_value=project)
    self.assertSecurityGroup(support_request, [self.user_id,
        '%s_F-PRODAGNT' % project.getReference(),
        '%s_F-PRODMAN' % project.getReference()], False)
    self.assertRoles(support_request, self.user_id, ['Owner'])
    self.assertRoles(support_request, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(support_request, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])

  def test_SupportRequest_DestinationProject(self):
    project = self.addProject()
    support_request = self.portal.getDefaultModuleValue(self.ticket_portal_type).newContent(
        portal_type=self.ticket_portal_type)
    support_request.edit(
        destination_project_value=project)
    self.assertSecurityGroup(support_request, [self.user_id,
        '%s_F-PRODAGNT' % project.getReference(),
        '%s_F-PRODMAN' % project.getReference()], False)
    self.assertRoles(support_request, self.user_id, ['Owner'])
    self.assertRoles(support_request, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(support_request, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])


class TestRegularisationRequestModule(TestSlapOSGroupRoleSecurityMixin):
  def test(self):
    module = self.portal.regularisation_request_module
    self.assertSecurityGroup(module,
        ['F-PRODUCTION*', 'F-CUSTOMER', module.Base_getOwnerId()], True)
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor'])
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])

class TestRegularisationRequest(TestSupportRequest):
  ticket_portal_type = 'Regularisation Request'


class TestSystemEventModule(TestSlapOSGroupRoleSecurityMixin):
  def test_SystemEventModule(self):
    module = self.portal.system_event_module
    self.assertSecurityGroup(module,
        ['R-SHADOW-PERSON', module.Base_getOwnerId(), 'F-IS*'], False)
    self.assertRoles(module, 'R-SHADOW-PERSON', ['Author'])
    self.assertRoles(module, 'F-IS*', ['Auditor'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])

class TestPayzenEvent(TestSlapOSGroupRoleSecurityMixin):
  def test_PayzenEvent_default(self):
    event = self.portal.system_event_module.newContent(
        portal_type='Payzen Event')
    self.assertSecurityGroup(event,
        ['F-IS*', self.user_id], False)
    self.assertRoles(event, 'F-IS*', ['Auditor'])
    self.assertRoles(event, self.user_id, ['Owner'])

  def test_PayzenEvent_ShadowUser(self):
    reference = 'TESTPERSON-%s' % self.generateNewId()
    person = self.portal.person_module.newContent(portal_type='Person',
        reference=reference)
    event = self.portal.system_event_module.newContent(
        portal_type='Payzen Event')
    event.edit(
        destination_section_value=person,
        )
    shadow_user_id = 'SHADOW-%s' % person.getUserId()
    self.assertSecurityGroup(event,
        ['F-IS*', self.user_id, shadow_user_id], False)
    self.assertRoles(event, 'F-IS*', ['Auditor'])
    self.assertRoles(event, shadow_user_id, ['Assignee'])
    self.assertRoles(event, self.user_id, ['Owner'])

class TestWechatEvent(TestSlapOSGroupRoleSecurityMixin):
  def test_WechatEvent_default(self):
    event = self.portal.system_event_module.newContent(
        portal_type='Wechat Event')
    self.assertSecurityGroup(event,
        ['F-IS*', self.user_id], False)
    self.assertRoles(event, 'F-IS*', ['Auditor'])
    self.assertRoles(event, self.user_id, ['Owner'])

  def test_WechatEvent_ShadowUser(self):
    reference = 'TESTPERSON-%s' % self.generateNewId()
    person = self.portal.person_module.newContent(portal_type='Person',
        reference=reference)
    event = self.portal.system_event_module.newContent(
        portal_type='Wechat Event')
    event.edit(
        destination_section_value=person,
        )
    shadow_user_id = 'SHADOW-%s' % person.getUserId()
    self.assertSecurityGroup(event,
        ['F-IS*', self.user_id, shadow_user_id], False)
    self.assertRoles(event, 'F-IS*', ['Auditor'])
    self.assertRoles(event, shadow_user_id, ['Assignee'])
    self.assertRoles(event, self.user_id, ['Owner'])


class TestUpgradeDecisionModule(TestSlapOSGroupRoleSecurityMixin):
  def test(self):
    module = self.portal.upgrade_decision_module
    self.assertSecurityGroup(module,
        ['F-PRODUCTION*', 'F-CUSTOMER', module.Base_getOwnerId()], True)
    self.assertRoles(module, 'F-PRODUCTION*', ['Auditor'])
    self.assertRoles(module, 'F-CUSTOMER', ['Auditor'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])

class TestUpgradeDecision(TestSlapOSGroupRoleSecurityMixin):
  ticket_portal_type = 'Upgrade Decision'

  def test_UpgradeDecision_default(self):
    support_request = self.portal.getDefaultModuleValue(self.ticket_portal_type).newContent(
        portal_type=self.ticket_portal_type)
    self.assertSecurityGroup(support_request,
        [self.user_id], False)
    self.assertRoles(support_request, self.user_id, ['Owner'])

  def test_UpgradeDecision_Customer(self):
    reference = 'TESTPERSON-%s' % self.generateNewId()
    person = self.portal.person_module.newContent(portal_type='Person',
        reference=reference)
    support_request = self.portal.getDefaultModuleValue(self.ticket_portal_type).newContent(
        portal_type=self.ticket_portal_type)
    support_request.edit(
        destination_decision_value=person,
        )
    self.assertSecurityGroup(support_request,
        [person.getUserId(), self.user_id], False)
    self.assertRoles(support_request, person.getUserId(), ['Auditor'])
    self.assertRoles(support_request, self.user_id, ['Owner'])

  def test_UpgradeDecision_SourceProject(self):
    project = self.addProject()
    support_request = self.portal.getDefaultModuleValue(self.ticket_portal_type).newContent(
        portal_type=self.ticket_portal_type)
    support_request.edit(
        source_project_value=project)
    self.assertSecurityGroup(support_request, [self.user_id,
        '%s_F-PRODAGNT' % project.getReference(),
        '%s_F-PRODMAN' % project.getReference()], False)
    self.assertRoles(support_request, self.user_id, ['Owner'])
    self.assertRoles(support_request, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(support_request, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])

  def test_UpgradeDecision_DestinationProject(self):
    project = self.addProject()
    support_request = self.portal.getDefaultModuleValue(self.ticket_portal_type).newContent(
        portal_type=self.ticket_portal_type)
    support_request.edit(
        destination_project_value=project)
    self.assertSecurityGroup(support_request, [self.user_id,
        '%s_F-PRODAGNT' % project.getReference(),
        '%s_F-PRODMAN' % project.getReference()], False)
    self.assertRoles(support_request, self.user_id, ['Owner'])
    self.assertRoles(support_request, '%s_F-PRODMAN' % project.getReference(), ['Assignor'])
    self.assertRoles(support_request, '%s_F-PRODAGNT' % project.getReference(), ['Assignee'])


class TestWebPageModule(TestSlapOSGroupRoleSecurityMixin):
  def test(self):
    module = self.portal.web_page_module
    self.assertSecurityGroup(module,
        ['F-MARKETING*', module.Base_getOwnerId()], True)
    self.assertRoles(module, 'F-MARKETING*', ['Auditor', 'Author'])
    self.assertRoles(module, module.Base_getOwnerId(), ['Owner'])

class TestWebPage(TestSlapOSGroupRoleSecurityMixin):
  def test_MarketingFunction(self):
    document = self.portal.web_page_module.newContent(
        portal_type='Web Page')
    self.assertSecurityGroup(document,
        ['F-MARKETING*', self.user_id], False)
    self.assertRoles(document, 'F-MARKETING*', ['Assignor'])
    self.assertRoles(document, self.user_id, ['Owner'])

class TestWebTable(TestSlapOSGroupRoleSecurityMixin):
  def test_MarketingFunction(self):
    document = self.portal.web_page_module.newContent(
        portal_type='Web Table')
    self.assertSecurityGroup(document,
        ['F-MARKETING*', self.user_id], False)
    self.assertRoles(document, 'F-MARKETING*', ['Assignor'])
    self.assertRoles(document, self.user_id, ['Owner'])

class TestWebIllustration(TestSlapOSGroupRoleSecurityMixin):
  def test_MarketingFunction(self):
    document = self.portal.web_page_module.newContent(
        portal_type='Web Illustration')
    self.assertSecurityGroup(document,
        ['F-MARKETING*', self.user_id], False)
    self.assertRoles(document, 'F-MARKETING*', ['Assignor'])
    self.assertRoles(document, self.user_id, ['Owner'])


class TestSecurePaymentTool(TestSlapOSGroupRoleSecurityMixin):
  def test_SecurePaymentTool_no_permissions_for_users(self):
    tool = self.portal.portal_secure_payments
    self.assertPermissionsOfRole(tool, 'Anonymous', [])
    self.assertPermissionsOfRole(tool, 'Assignee', [])
    self.assertPermissionsOfRole(tool, 'Assignor', [])
    self.assertPermissionsOfRole(tool, 'Associate', [])
    self.assertPermissionsOfRole(tool, 'Auditor', [])
    self.assertPermissionsOfRole(tool, 'Authenticated', [])
    self.assertPermissionsOfRole(tool, 'Author', [])
    self.assertPermissionsOfRole(tool, 'Member', [])
    self.assertPermissionsOfRole(tool, 'Owner', [])
    self.assertPermissionsOfRole(tool, 'Reviewer', [])

    self.assertAcquiredPermissions(tool, ['Add ERP5 SQL Methods'])

