test_sales_team_internals.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import exceptions
  4. from odoo.addons.mail.tests.common import mail_new_test_user
  5. from odoo.addons.sales_team.tests.common import TestSalesMC
  6. from odoo.tests.common import users, TransactionCase
  7. from odoo.tools import mute_logger
  8. class TestCornerCases(TransactionCase):
  9. def setUp(self):
  10. super(TestCornerCases, self).setUp()
  11. self.user_sales_leads = mail_new_test_user(
  12. self.env, login='user_sales_leads',
  13. name='Laetitia Sales Leads', email='crm_leads@test.example.com',
  14. company_id=self.env.user.company_id.id,
  15. notification_type='inbox',
  16. groups='sales_team.group_sale_salesman_all_leads,base.group_partner_manager',
  17. )
  18. self.sales_team_1 = self.env['crm.team'].create({
  19. 'name': 'Test Sales Team',
  20. 'sequence': 5,
  21. 'company_id': False,
  22. 'user_id': self.env.user.id,
  23. })
  24. def test_unicity(self):
  25. """ Archived memberships should be removed when detecting duplicates.
  26. Creating duplicates should raise unicity constraint.
  27. Note: redoing the data set to avoid clashing with SavepointCase as
  28. we expect a db-level assert """
  29. sales_team_1_m1 = self.env['crm.team.member'].create({
  30. 'user_id': self.user_sales_leads.id,
  31. 'crm_team_id': self.sales_team_1.id,
  32. })
  33. sales_team_1_m1.write({'active': False})
  34. sales_team_1_m1.flush_recordset()
  35. sales_team_1_m2 = self.env['crm.team.member'].create({
  36. 'user_id': self.user_sales_leads.id,
  37. 'crm_team_id': self.sales_team_1.id,
  38. })
  39. found = self.env['crm.team.member'].search([
  40. ('user_id', '=', self.user_sales_leads.id),
  41. ('crm_team_id', '=', self.sales_team_1.id),
  42. ])
  43. self.assertEqual(found, sales_team_1_m2)
  44. with self.assertRaises(exceptions.UserError), mute_logger('odoo.sql_db'):
  45. self.env['crm.team.member'].create({
  46. 'user_id': self.user_sales_leads.id,
  47. 'crm_team_id': self.sales_team_1.id,
  48. })
  49. def test_unicity_multicreate(self):
  50. """ Test constraint works with creating duplicates in the same create
  51. method. """
  52. with self.assertRaises(exceptions.UserError), mute_logger('odoo.sql_db'):
  53. self.env['crm.team.member'].create([
  54. {'user_id': self.user_sales_leads.id, 'crm_team_id': self.sales_team_1.id},
  55. {'user_id': self.user_sales_leads.id, 'crm_team_id': self.sales_team_1.id}
  56. ])
  57. class TestSecurity(TestSalesMC):
  58. @users('user_sales_leads')
  59. def test_team_access(self):
  60. sales_team = self.sales_team_1.with_user(self.env.user)
  61. sales_team.read(['name'])
  62. for member in sales_team.member_ids:
  63. member.read(['name'])
  64. with self.assertRaises(exceptions.AccessError):
  65. sales_team.write({'name': 'Trolling'})
  66. for membership in sales_team.crm_team_member_ids:
  67. membership.read(['name'])
  68. with self.assertRaises(exceptions.AccessError):
  69. membership.write({'active': False})
  70. with self.assertRaises(exceptions.AccessError):
  71. sales_team.write({'member_ids': [(5, 0)]})
  72. @users('user_sales_leads')
  73. def test_team_multi_company(self):
  74. self.sales_team_1.with_user(self.env.user).read(['name'])
  75. with self.assertRaises(exceptions.AccessError):
  76. self.team_c2.with_user(self.env.user).read(['name'])