You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
6.5 KiB

  1. # Copyright 2018 David Juaneda - <djuaneda@sdi.es>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo.tests.common import TransactionCase
  4. class TestMailActivityBoardMethods(TransactionCase):
  5. def setUp(self):
  6. super(TestMailActivityBoardMethods, self).setUp()
  7. # Set up activities
  8. # Create a user as 'Crm Salesman' and added few groups
  9. self.employee = self.env['res.users'].create({
  10. 'company_id': self.env.ref("base.main_company").id,
  11. 'name': "Employee",
  12. 'login': "csu",
  13. 'email': "crmuser@yourcompany.com",
  14. 'groups_id': [(6, 0, [self.env.ref('base.group_user').id])]
  15. })
  16. # Create a user who doesn't have access to anything except activities
  17. mail_activity_group = self.create_mail_activity_group()
  18. self.employee2 = self.env['res.users'].create({
  19. 'company_id': self.env.ref("base.main_company").id,
  20. 'name': "Employee2",
  21. 'login': "alien",
  22. 'email': "alien@yourcompany.com",
  23. 'groups_id': [(6, 0, [mail_activity_group.id])],
  24. })
  25. # lead_model_id = self.env['ir.model']._get('crm.lead').id
  26. partner_model_id = self.env['ir.model']._get('res.partner').id
  27. ActivityType = self.env['mail.activity.type']
  28. self.activity1 = ActivityType.create({
  29. 'name': 'Initial Contact',
  30. 'days': 5,
  31. 'summary': 'ACT 1 : Presentation, barbecue, ... ',
  32. 'res_model_id': partner_model_id,
  33. })
  34. self.activity2 = ActivityType.create({
  35. 'name': 'Call for Demo',
  36. 'days': 6,
  37. 'summary': 'ACT 2 : I want to show you my ERP !',
  38. 'res_model_id': partner_model_id,
  39. })
  40. self.activity3 = ActivityType.create({
  41. 'name': 'Celebrate the sale',
  42. 'days': 3,
  43. 'summary': 'ACT 3 : '
  44. 'Beers for everyone because I am a good salesman !',
  45. 'res_model_id': partner_model_id,
  46. })
  47. # I create an opportunity, as employee
  48. self.partner_client = self.env.ref("base.res_partner_1")
  49. # assure there isn't any mail activity yet
  50. self.env['mail.activity'].sudo().search([]).unlink()
  51. self.act1 = self.env['mail.activity'].sudo().create({
  52. 'activity_type_id': self.activity3.id,
  53. 'note': 'Partner activity 1.',
  54. 'res_id': self.partner_client.id,
  55. 'res_model_id': partner_model_id,
  56. 'user_id': self.employee.id
  57. })
  58. self.act2 = self.env['mail.activity'].sudo().create({
  59. 'activity_type_id': self.activity2.id,
  60. 'note': 'Partner activity 2.',
  61. 'res_id': self.partner_client.id,
  62. 'res_model_id': partner_model_id,
  63. 'user_id': self.employee.id
  64. })
  65. self.act3 = self.env['mail.activity'].sudo().create({
  66. 'activity_type_id': self.activity3.id,
  67. 'note': 'Partner activity 3.',
  68. 'res_id': self.partner_client.id,
  69. 'res_model_id': partner_model_id,
  70. 'user_id': self.employee.id
  71. })
  72. def create_mail_activity_group(self):
  73. manager_mail_activity_test_group = self.env['res.groups'].create({
  74. 'name': 'group_manager_mail_activity_test',
  75. })
  76. mail_activity_model_id = self.env['ir.model'].sudo().search(
  77. [('model', '=', 'mail.activity')], limit=1)
  78. access = self.env['ir.model.access'].create({
  79. 'name': 'full_access_mail_activity',
  80. 'model_id': mail_activity_model_id.id,
  81. 'perm_read': True,
  82. 'perm_write': True,
  83. 'perm_create': True,
  84. 'perm_unlink': True,
  85. })
  86. access.group_id = manager_mail_activity_test_group
  87. return manager_mail_activity_test_group
  88. def get_view(self, activity):
  89. action = activity.open_origin()
  90. result = self.env[action.get('res_model')]\
  91. .load_views(action.get('views'))
  92. return result.get('fields_views').get(action.get('view_mode'))
  93. def test_open_origin_res_partner(self):
  94. """ This test case checks
  95. - If the method redirects to the form view of the correct one
  96. of an object of the 'res.partner' class to which the activity
  97. belongs.
  98. """
  99. # Id of the form view for the class 'crm.lead', type 'lead'
  100. form_view_partner_id = self.env.ref('base.view_partner_form').id
  101. # Id of the form view return open_origin()
  102. view = self.get_view(self.act1)
  103. # Check the next view is correct
  104. self.assertEqual(form_view_partner_id, view.get('view_id'))
  105. # Id of the form view return open_origin()
  106. view = self.get_view(self.act2)
  107. # Check the next view is correct
  108. self.assertEqual(form_view_partner_id, view.get('view_id'))
  109. # Id of the form view return open_origin()
  110. view = self.get_view(self.act3)
  111. # Check the next view is correct
  112. self.assertEqual(form_view_partner_id, view.get('view_id'))
  113. def test_redirect_to_activities(self):
  114. """ This test case checks
  115. - if the method returns the correct action,
  116. - if the correct activities are shown.
  117. """
  118. action_id = self.env.ref(
  119. 'mail_activity_board.open_boards_activities').id
  120. action = self.partner_client\
  121. .redirect_to_activities(**{'id': self.partner_client.id})
  122. self.assertEqual(action.get('id'), action_id)
  123. kwargs = {
  124. 'groupby': [
  125. "activity_type_id"
  126. ],
  127. }
  128. kwargs['domain'] = action.get('domain')
  129. result = self.env[action.get('res_model')]\
  130. .load_views(action.get('views'))
  131. fields = result.get('fields_views').get('kanban').get('fields')
  132. kwargs['fields'] = list(fields.keys())
  133. result = self.env['mail.activity'].read_group(**kwargs)
  134. acts = []
  135. for group in result:
  136. records = self.env['mail.activity'].search_read(
  137. domain=group.get('__domain'), fields=kwargs['fields']
  138. )
  139. acts += [record_id.get('id') for record_id in records]
  140. for act in acts:
  141. self.assertIn(act, self.partner_client.activity_ids.ids)
  142. def test_read_permissions(self):
  143. search1 = self.env['mail.activity'].sudo(self.employee).search([])
  144. self.assertEqual(len(search1), 3)
  145. search2 = self.env['mail.activity'].sudo(self.employee2).search([])
  146. self.assertEqual(len(search2), 0)