Best Python code snippet using keyboard
test_tier_validation.py
Source:test_tier_validation.py
1# Copyright 2018-19 ForgeFlow S.L. (https://www.forgeflow.com)2# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).3from lxml import etree4from odoo.exceptions import ValidationError5from odoo.tests.common import Form, tagged6from .common import CommonTierValidation7@tagged("post_install", "-at_install")8class TierTierValidation(CommonTierValidation):9 def test_01_auto_validation(self):10 """When the user can validate all future reviews, it is not needed11 to request a validation, the action can be done straight forward."""12 self.test_record.with_user(self.test_user_1.id).action_confirm()13 self.assertEqual(self.test_record.state, "confirmed")14 def test_02_no_auto_validation(self):15 """User with no right to validate future reviews must request a16 validation."""17 with self.assertRaises(ValidationError):18 self.test_record.with_user(self.test_user_2.id).action_confirm()19 def test_03_request_validation_approved(self):20 """User 2 request a validation and user 1 approves it."""21 self.assertFalse(self.test_record.review_ids)22 reviews = self.test_record.with_user(self.test_user_2.id).request_validation()23 self.assertTrue(reviews)24 record = self.test_record.with_user(self.test_user_1.id)25 record.invalidate_cache()26 record.validate_tier()27 self.assertTrue(record.validated)28 def test_04_request_validation_rejected(self):29 """Request validation, rejection and reset."""30 self.assertFalse(self.test_record.review_ids)31 reviews = self.test_record.with_user(self.test_user_2.id).request_validation()32 self.assertTrue(reviews)33 record = self.test_record.with_user(self.test_user_1.id)34 record.invalidate_cache()35 record.reject_tier()36 self.assertTrue(record.review_ids)37 self.assertTrue(record.rejected)38 record.restart_validation()39 self.assertFalse(record.review_ids)40 def test_05_under_validation(self):41 """Write is forbidden in a record under validation."""42 self.assertFalse(self.test_record.review_ids)43 reviews = self.test_record.with_user(self.test_user_2.id).request_validation()44 self.assertTrue(reviews)45 record = self.test_record.with_user(self.test_user_1.id)46 record.invalidate_cache()47 with self.assertRaises(ValidationError):48 record.write({"test_field": 0.5})49 def test_06_validation_process_open(self):50 """Operation forbidden while a validation process is open."""51 self.assertFalse(self.test_record.review_ids)52 reviews = self.test_record.with_user(self.test_user_2.id).request_validation()53 self.assertTrue(reviews)54 record = self.test_record.with_user(self.test_user_1.id)55 record.invalidate_cache()56 with self.assertRaises(ValidationError):57 record.action_confirm()58 def test_07_search_reviewers(self):59 """Test search methods."""60 reviews = self.test_record.with_user(self.test_user_2.id).request_validation()61 self.assertTrue(reviews)62 record = self.test_record.with_user(self.test_user_1.id)63 record.invalidate_cache()64 self.assertIn(self.test_user_1, record.reviewer_ids)65 res = self.test_model.search([("reviewer_ids", "in", self.test_user_1.id)])66 self.assertTrue(res)67 def test_08_search_validated(self):68 """Test for the validated search method."""69 self.test_record.with_user(self.test_user_2.id).request_validation()70 self.test_record.invalidate_cache()71 res = self.test_model.with_user(self.test_user_1.id).search(72 [("validated", "=", False)]73 )74 self.assertTrue(res)75 def test_09_search_rejected(self):76 """Test for the rejected search method."""77 self.test_record.with_user(self.test_user_2.id).request_validation()78 self.test_record.invalidate_cache()79 res = self.test_model.with_user(self.test_user_1.id).search(80 [("rejected", "=", False)]81 )82 self.assertTrue(res)83 def test_10_systray_counter(self):84 # Create new test record85 test_record = self.test_model.create({"test_field": 2.5})86 # Create tier definitions for both tester models87 self.tier_def_obj.create(88 {89 "model_id": self.tester_model.id,90 "review_type": "individual",91 "reviewer_id": self.test_user_1.id,92 "definition_domain": "[('test_field', '>', 1.0)]",93 }94 )95 self.tier_def_obj.create(96 {97 "model_id": self.tester_model.id,98 "review_type": "individual",99 "reviewer_id": self.test_user_1.id,100 "definition_domain": "[('test_field', '>', 1.0)]",101 }102 )103 self.tier_def_obj.create(104 {105 "model_id": self.tester_model_2.id,106 "review_type": "individual",107 "reviewer_id": self.test_user_1.id,108 "definition_domain": "[('test_field', '>', 1.0)]",109 }110 )111 # Request validation112 self.test_record.with_user(self.test_user_2.id).request_validation()113 self.test_record.invalidate_cache()114 test_record.with_user(self.test_user_2.id).request_validation()115 test_record.invalidate_cache()116 self.test_record_2.with_user(self.test_user_2.id).request_validation()117 self.test_record_2.invalidate_cache()118 # Get review user count as systray icon would do and check count value119 docs = self.test_user_1.with_user(self.test_user_1).review_user_count()120 for doc in docs:121 if doc.get("name") == "tier.validation.tester2":122 self.assertEqual(doc.get("pending_count"), 1)123 else:124 self.assertEqual(doc.get("pending_count"), 2)125 def test_11_add_comment(self):126 # Create new test record127 test_record = self.test_model.create({"test_field": 2.5})128 # Create tier definitions129 self.tier_def_obj.create(130 {131 "model_id": self.tester_model.id,132 "review_type": "individual",133 "reviewer_id": self.test_user_1.id,134 "definition_domain": "[('test_field', '>', 1.0)]",135 "has_comment": True,136 }137 )138 # Request validation139 review = test_record.with_user(self.test_user_2.id).request_validation()140 self.assertTrue(review)141 record = test_record.with_user(self.test_user_1.id)142 record.invalidate_cache()143 res = record.validate_tier()144 ctx = res.get("context")145 wizard = Form(self.env["comment.wizard"].with_context(**ctx))146 wizard.comment = "Test Comment"147 wiz = wizard.save()148 wiz.add_comment()149 self.assertTrue(test_record.review_ids.mapped("comment"))150 # Check notify151 comment = test_record.with_user(152 self.test_user_1.id153 )._notify_accepted_reviews_body()154 self.assertEqual(comment, "A review was accepted. (Test Comment)")155 comment = test_record.with_user(156 self.test_user_1.id157 )._notify_rejected_review_body()158 self.assertEqual(comment, "A review was rejected by John. (Test Comment)")159 def test_11_add_comment_rejection(self):160 # Create new test record161 test_record = self.test_model.create({"test_field": 2.5})162 # Create tier definitions163 self.tier_def_obj.create(164 {165 "model_id": self.tester_model.id,166 "review_type": "individual",167 "reviewer_id": self.test_user_1.id,168 "definition_domain": "[('test_field', '>', 1.0)]",169 "has_comment": True,170 }171 )172 # Request validation173 review = test_record.with_user(self.test_user_2.id).request_validation()174 self.assertTrue(review)175 record = test_record.with_user(self.test_user_1.id)176 record.invalidate_cache()177 res = record.reject_tier() # Rejection178 ctx = res.get("context")179 wizard = Form(self.env["comment.wizard"].with_context(**ctx))180 wizard.comment = "Test Comment"181 wiz = wizard.save()182 wiz.add_comment()183 self.assertTrue(test_record.review_ids.mapped("comment"))184 # Check notify185 comment = test_record.with_user(186 self.test_user_1.id187 )._notify_accepted_reviews_body()188 self.assertEqual(comment, "A review was accepted. (Test Comment)")189 comment = test_record.with_user(190 self.test_user_1.id191 )._notify_rejected_review_body()192 self.assertEqual(comment, "A review was rejected by John. (Test Comment)")193 def test_12_approve_sequence(self):194 # Create new test record195 test_record = self.test_model.create({"test_field": 2.5})196 # Create tier definitions197 self.tier_def_obj.create(198 {199 "model_id": self.tester_model.id,200 "review_type": "individual",201 "reviewer_id": self.test_user_1.id,202 "definition_domain": "[('test_field', '>', 1.0)]",203 "approve_sequence": True,204 "sequence": 30,205 }206 )207 self.tier_def_obj.create(208 {209 "model_id": self.tester_model.id,210 "review_type": "individual",211 "reviewer_id": self.test_user_2.id,212 "definition_domain": "[('test_field', '>', 1.0)]",213 "approve_sequence": True,214 "sequence": 10,215 }216 )217 # Request validation218 self.assertFalse(self.test_record.review_ids)219 reviews = test_record.with_user(self.test_user_2.id).request_validation()220 self.assertTrue(reviews)221 docs1 = self.test_user_2.with_user(self.test_user_1).review_user_count()222 for doc in docs1:223 self.assertEqual(doc.get("pending_count"), 1)224 docs2 = self.test_user_2.with_user(self.test_user_2).review_user_count()225 for doc in docs2:226 self.assertEqual(doc.get("pending_count"), 0)227 record1 = test_record.with_user(self.test_user_1.id)228 record1.invalidate_cache()229 self.assertTrue(record1.can_review)230 record2 = test_record.with_user(self.test_user_2.id)231 record2.invalidate_cache()232 self.assertFalse(record2.can_review)233 # User 1 validates the record, 2 review should be approved.234 self.assertFalse(any(r.status == "approved" for r in record1.review_ids))235 record1.validate_tier()236 self.assertTrue(any(r.status == "approved" for r in record1.review_ids))237 def test_12_approve_sequence_same_user(self):238 """Similar to test_12_approve_sequence, but all same users,239 the approve_sequence still apply correctly"""240 # Create new test record241 test_record = self.test_model.create({"test_field": 2.5})242 # Create tier definitions243 self.tier_def_obj.create(244 {245 "model_id": self.tester_model.id,246 "review_type": "individual",247 "reviewer_id": self.test_user_1.id,248 "definition_domain": "[('test_field', '>', 1.0)]",249 "approve_sequence": True,250 "sequence": 20,251 }252 )253 self.tier_def_obj.create(254 {255 "model_id": self.tester_model.id,256 "review_type": "individual",257 "reviewer_id": self.test_user_1.id,258 "definition_domain": "[('test_field', '>', 1.0)]",259 "approve_sequence": True,260 "sequence": 10,261 }262 )263 # Request validation264 self.assertFalse(self.test_record.review_ids)265 reviews = test_record.with_user(self.test_user_1.id).request_validation()266 self.assertTrue(reviews)267 record1 = test_record.with_user(self.test_user_1.id)268 record1.invalidate_cache()269 self.assertTrue(record1.can_review)270 # Validation will be all by sequence271 self.assertEqual(272 3, len(record1.review_ids.filtered(lambda l: l.status == "pending"))273 )274 record1.validate_tier()275 self.assertEqual(276 2, len(record1.review_ids.filtered(lambda l: l.status == "pending"))277 )278 record1.validate_tier()279 self.assertEqual(280 1, len(record1.review_ids.filtered(lambda l: l.status == "pending"))281 )282 record1.validate_tier()283 self.assertEqual(284 0, len(record1.review_ids.filtered(lambda l: l.status == "pending"))285 )286 def test_13_onchange_review_type(self):287 tier_def_id = self.tier_def_obj.create(288 {289 "model_id": self.tester_model.id,290 "review_type": "individual",291 "reviewer_id": self.test_user_1.id,292 "definition_domain": "[('test_field', '>', 1.0)]",293 "approve_sequence": True,294 }295 )296 self.assertTrue(tier_def_id.reviewer_id)297 tier_def_id.review_type = "group"298 tier_def_id.onchange_review_type()299 self.assertFalse(tier_def_id.reviewer_id)300 def test_14_onchange_review_type(self):301 tier_def_id = self.tier_def_obj.create(302 {303 "model_id": self.tester_model.id,304 "review_type": "individual",305 "reviewer_id": self.test_user_1.id,306 "definition_domain": "[('test_field', '>', 1.0)]",307 "approve_sequence": True,308 }309 )310 self.assertTrue(tier_def_id.reviewer_id)311 tier_def_id.review_type = "group"312 tier_def_id.onchange_review_type()313 self.assertFalse(tier_def_id.reviewer_id)314 def test_15_review_user_count(self):315 # Create new test record316 test_record = self.test_model.create({"test_field": 2.5})317 # Create tier definitions318 self.tier_def_obj.create(319 {320 "model_id": self.tester_model.id,321 "review_type": "individual",322 "reviewer_id": self.test_user_1.id,323 "definition_domain": "[('test_field', '>', 1.0)]",324 "has_comment": True,325 }326 )327 # Request validation328 review = test_record.with_user(self.test_user_2).request_validation()329 self.assertTrue(review)330 self.assertTrue(self.test_user_1.get_reviews({"res_ids": review.ids}))331 self.assertTrue(self.test_user_1.review_ids)332 test_record.invalidate_cache()333 self.assertTrue(test_record.review_ids)334 # Used by front-end335 count = self.test_user_1.with_user(self.test_user_1).review_user_count()336 self.assertEqual(len(count), 1)337 # False Review338 self.assertFalse(self.test_record._calc_reviews_validated(False))339 self.assertIn("requested", self.test_record._notify_requested_review_body())340 self.assertIn("rejected", self.test_record._notify_rejected_review_body())341 self.assertIn("accepted", self.test_record._notify_accepted_reviews_body())342 def test_16_review_user_count_on_rejected(self):343 """If document is rejected, it should always removed from tray"""344 # Create new test record345 test_record = self.test_model.create({"test_field": 2.5})346 # Create tier definitions347 self.tier_def_obj.create(348 {349 "model_id": self.tester_model.id,350 "review_type": "individual",351 "reviewer_id": self.test_user_2.id,352 "definition_domain": "[('test_field', '>', 1.0)]",353 }354 )355 test_record.with_user(self.test_user_2).request_validation()356 record1 = test_record.with_user(self.test_user_1)357 record1.invalidate_cache()358 self.assertTrue(record1.can_review)359 self.assertTrue(360 self.test_user_1.with_user(self.test_user_1).review_user_count()361 )362 self.assertTrue(363 self.test_user_2.with_user(self.test_user_2).review_user_count()364 )365 # user 1 reject first tier366 record1.reject_tier()367 record1.invalidate_cache()368 self.assertFalse(record1.can_review)369 # both user 1 and 2 has nothing left in tray370 self.assertFalse(371 self.test_user_1.with_user(self.test_user_1).review_user_count()372 )373 self.assertFalse(374 self.test_user_2.with_user(self.test_user_2).review_user_count()375 )376 def test_17_search_records_no_validation(self):377 """Search for records that have no validation process started"""378 records = self.env["tier.validation.tester"].search(379 [("reviewer_ids", "=", False)]380 )381 self.assertEqual(len(records), 1)382 self.test_record.with_user(self.test_user_2.id).request_validation()383 record = self.test_record.with_user(self.test_user_1.id)384 record.invalidate_cache()385 records = self.env["tier.validation.tester"].search(386 [("reviewer_ids", "=", False)]387 )388 self.assertEqual(len(records), 0)389 def test_18_test_review_by_res_users_field(self):390 selected_field = self.env["ir.model.fields"].search(391 [("model", "=", self.test_model._name), ("name", "=", "user_id")]392 )393 test_record = self.test_model.create(394 {"test_field": 2.5, "user_id": self.test_user_2.id}395 )396 definition = self.env["tier.definition"].create(397 {398 "model_id": self.tester_model.id,399 "review_type": "field",400 "reviewer_field_id": selected_field.id,401 "definition_domain": "[('test_field', '>', 1.0)]",402 "approve_sequence": True,403 }404 )405 reviews = test_record.request_validation()406 review = reviews.filtered(lambda r: r.definition_id == definition)407 self.assertTrue(review)408 self.assertEqual(review.reviewer_ids, self.test_user_2)409@tagged("at_install")410class TierTierValidationView(CommonTierValidation):411 def test_view_manual(self):412 # We need to add a view in order to ensure that an automatic view with all413 # fields is not created414 self.env["ir.ui.view"].create(415 {416 "model": self.test_record._name,417 "name": "Demo view",418 "arch": """<form>419 <header>420 <button name="action_confirm" type="object" string="Confirm" />421 <field name="state" widget="statusbar" />422 </header>423 <sheet>424 <field name="test_field" />425 </sheet>426 </form>""",427 }428 )429 with Form(self.test_record) as f:430 self.assertNotIn("review_ids", f._values)431 form = etree.fromstring(f._view["arch"])432 self.assertFalse(form.xpath("//field[@name='review_ids']"))433 self.assertFalse(form.xpath("//field[@name='can_review']"))434 self.assertFalse(form.xpath("//button[@name='request_validation']"))435 def test_view_automatic(self):436 # We need to add a view in order to ensure that an automatic view with all437 # fields is not created438 self.env["ir.ui.view"].create(439 {440 "model": self.test_record_2._name,441 "name": "Demo view",442 "arch": """<form>443 <header>444 <button name="action_confirm" type="object" string="Confirm" />445 <field name="state" widget="statusbar" />446 </header>447 <sheet>448 <field name="test_field" />449 </sheet>450 </form>""",451 }452 )453 with Form(self.test_record_2) as f:454 self.assertIn("review_ids", f._values)455 form = etree.fromstring(f._view["arch"])456 self.assertTrue(form.xpath("//field[@name='review_ids']"))457 self.assertTrue(form.xpath("//field[@name='can_review']"))...
test_mail_followers.py
Source:test_mail_followers.py
1# -*- coding: utf-8 -*-2# Part of Odoo. See LICENSE file for full copyright and licensing details.3from psycopg2 import IntegrityError4from odoo.addons.test_mail.tests import common5from odoo.addons.test_mail.tests.common import mail_new_test_user6from odoo.tools.misc import mute_logger7class BaseFollowersTest(common.BaseFunctionalTest):8 @classmethod9 def setUpClass(cls):10 super(BaseFollowersTest, cls).setUpClass()11 Subtype = cls.env['mail.message.subtype']12 cls.mt_mg_def = Subtype.create({'name': 'mt_mg_def', 'default': True, 'res_model': 'mail.test.simple'})13 cls.mt_cl_def = Subtype.create({'name': 'mt_cl_def', 'default': True, 'res_model': 'mail.test'})14 cls.mt_al_def = Subtype.create({'name': 'mt_al_def', 'default': True, 'res_model': False})15 cls.mt_mg_nodef = Subtype.create({'name': 'mt_mg_nodef', 'default': False, 'res_model': 'mail.test.simple'})16 cls.mt_al_nodef = Subtype.create({'name': 'mt_al_nodef', 'default': False, 'res_model': False})17 cls.mt_mg_def_int = cls.env['mail.message.subtype'].create({'name': 'mt_mg_def', 'default': True, 'res_model': 'mail.test.simple', 'internal': True})18 cls.default_group_subtypes = Subtype.search([('default', '=', True), '|', ('res_model', '=', 'mail.test.simple'), ('res_model', '=', False)])19 cls.default_group_subtypes_portal = Subtype.search([('internal', '=', False), ('default', '=', True), '|', ('res_model', '=', 'mail.test.simple'), ('res_model', '=', False)])20 def test_field_message_is_follower(self):21 test_record = self.test_record.sudo(self.user_employee)22 followed_before = test_record.search([('message_is_follower', '=', True)])23 self.assertFalse(test_record.message_is_follower)24 test_record.message_subscribe(partner_ids=[self.user_employee.partner_id.id])25 followed_after = test_record.search([('message_is_follower', '=', True)])26 self.assertTrue(test_record.message_is_follower)27 self.assertEqual(followed_before | test_record, followed_after)28 def test_field_followers(self):29 test_record = self.test_record.sudo(self.user_employee)30 test_record.message_subscribe(partner_ids=[self.user_employee.partner_id.id, self.user_admin.partner_id.id], channel_ids=[self.channel_listen.id])31 followers = self.env['mail.followers'].search([32 ('res_model', '=', 'mail.test.simple'),33 ('res_id', '=', test_record.id)])34 self.assertEqual(followers, test_record.message_follower_ids)35 self.assertEqual(test_record.message_partner_ids, self.user_employee.partner_id | self.user_admin.partner_id)36 self.assertEqual(test_record.message_channel_ids, self.channel_listen)37 def test_followers_subtypes_default(self):38 test_record = self.test_record.sudo(self.user_employee)39 test_record.message_subscribe(partner_ids=[self.user_employee.partner_id.id])40 self.assertEqual(test_record.message_partner_ids, self.user_employee.partner_id)41 follower = self.env['mail.followers'].search([42 ('res_model', '=', 'mail.test.simple'),43 ('res_id', '=', test_record.id),44 ('partner_id', '=', self.user_employee.partner_id.id)])45 self.assertEqual(follower, test_record.message_follower_ids)46 self.assertEqual(follower.subtype_ids, self.default_group_subtypes)47 def test_followers_subtypes_default_internal(self):48 user_portal = mail_new_test_user(self.env, login='chell', groups='base.group_portal', name='Chell Gladys')49 test_record = self.test_record.sudo(self.user_employee)50 test_record.message_subscribe(partner_ids=[user_portal.partner_id.id])51 self.assertEqual(test_record.message_partner_ids, user_portal.partner_id)52 follower = self.env['mail.followers'].search([53 ('res_model', '=', 'mail.test.simple'),54 ('res_id', '=', test_record.id),55 ('partner_id', '=', user_portal.partner_id.id)])56 self.assertEqual(follower.subtype_ids, self.default_group_subtypes_portal)57 def test_followers_subtypes_specified(self):58 test_record = self.test_record.sudo(self.user_employee)59 test_record.message_subscribe(partner_ids=[self.user_employee.partner_id.id], subtype_ids=[self.mt_mg_nodef.id])60 self.assertEqual(test_record.message_partner_ids, self.user_employee.partner_id)61 follower = self.env['mail.followers'].search([62 ('res_model', '=', 'mail.test.simple'),63 ('res_id', '=', test_record.id),64 ('partner_id', '=', self.user_employee.partner_id.id)])65 self.assertEqual(follower, test_record.message_follower_ids)66 self.assertEqual(follower.subtype_ids, self.mt_mg_nodef)67 def test_followers_multiple_subscription_force(self):68 test_record = self.test_record.sudo(self.user_employee)69 test_record.message_subscribe(partner_ids=[self.user_admin.partner_id.id], subtype_ids=[self.mt_mg_nodef.id])70 self.assertEqual(test_record.message_partner_ids, self.user_admin.partner_id)71 self.assertEqual(test_record.message_channel_ids, self.env['mail.channel'])72 self.assertEqual(test_record.message_follower_ids.subtype_ids, self.mt_mg_nodef)73 test_record.message_subscribe(partner_ids=[self.user_admin.partner_id.id], subtype_ids=[self.mt_mg_nodef.id, self.mt_al_nodef.id])74 self.assertEqual(test_record.message_partner_ids, self.user_admin.partner_id)75 self.assertEqual(test_record.message_channel_ids, self.env['mail.channel'])76 self.assertEqual(test_record.message_follower_ids.subtype_ids, self.mt_mg_nodef | self.mt_al_nodef)77 def test_followers_multiple_subscription_noforce(self):78 test_record = self.test_record.sudo(self.user_employee)79 test_record.message_subscribe(partner_ids=[self.user_admin.partner_id.id], subtype_ids=[self.mt_mg_nodef.id, self.mt_al_nodef.id])80 self.assertEqual(test_record.message_partner_ids, self.user_admin.partner_id)81 self.assertEqual(test_record.message_channel_ids, self.env['mail.channel'])82 self.assertEqual(test_record.message_follower_ids.subtype_ids, self.mt_mg_nodef | self.mt_al_nodef)83 # set new subtypes with force=False, meaning no rewriting of the subscription is done -> result should not change84 test_record.message_subscribe(partner_ids=[self.user_admin.partner_id.id])85 self.assertEqual(test_record.message_partner_ids, self.user_admin.partner_id)86 self.assertEqual(test_record.message_channel_ids, self.env['mail.channel'])87 self.assertEqual(test_record.message_follower_ids.subtype_ids, self.mt_mg_nodef | self.mt_al_nodef)88 def test_followers_no_DID(self):89 """Test that a follower cannot suffer from dissociative identity disorder.90 It cannot be both a partner and a channel.91 """92 with self.assertRaises(IntegrityError), mute_logger('odoo.sql_db'):93 self.env['mail.followers'].create({94 'res_model': self.test_record._name,95 'res_id': self.test_record.id,96 'partner_id': self.user_employee.partner_id.id,97 'channel_id': self.channel_listen.id,98 })99class AdvancedFollowersTest(common.BaseFunctionalTest):100 @classmethod101 def setUpClass(cls):102 super(AdvancedFollowersTest, cls).setUpClass()103 cls.user_portal = mail_new_test_user(cls.env, login='chell', groups='base.group_portal', name='Chell Gladys')104 cls.test_track = cls.env['mail.test.track'].sudo(cls.user_employee).create({105 'name': 'Test',106 })107 Subtype = cls.env['mail.message.subtype']108 # clean demo data to avoid interferences109 Subtype.search([('res_model', 'in', ['mail.test', 'mail.test.track'])]).unlink()110 cls.sub_nodef = Subtype.create({'name': 'Sub NoDefault', 'default': False, 'res_model': 'mail.test'})111 cls.sub_umb1 = Subtype.create({'name': 'Sub Umbrella1', 'default': False, 'res_model': 'mail.test.track'})112 cls.sub_umb2 = Subtype.create({'name': 'Sub Umbrella2', 'default': False, 'res_model': 'mail.test.track'})113 cls.umb_def = Subtype.create({'name': 'Umbrella Default', 'default': True, 'res_model': 'mail.test'})114 # create subtypes for auto subscription from umbrella to sub records115 cls.umb_sub_def = Subtype.create({116 'name': 'Umbrella Sub1', 'default': True, 'res_model': 'mail.test',117 'parent_id': cls.sub_umb1.id, 'relation_field': 'umbrella_id'})118 cls.umb_sub_nodef = Subtype.create({119 'name': 'Umbrella Sub2', 'default': False, 'res_model': 'mail.test',120 'parent_id': cls.sub_umb2.id, 'relation_field': 'umbrella_id'})121 def test_auto_subscribe_create(self):122 """ Creator of records are automatically added as followers """123 self.assertEqual(self.test_track.message_partner_ids, self.user_employee.partner_id)124 def test_auto_subscribe_post(self):125 """ People posting a message are automatically added as followers """126 self.test_track.sudo(self.user_admin).message_post(body='Coucou hibou', message_type='comment')127 self.assertEqual(self.test_track.message_partner_ids, self.user_employee.partner_id | self.user_admin.partner_id)128 def test_auto_subscribe_post_email(self):129 """ People posting an email are automatically added as followers """130 self.test_track.sudo(self.user_admin).message_post(body='Coucou hibou', message_type='email')131 self.assertEqual(self.test_track.message_partner_ids, self.user_employee.partner_id | self.user_admin.partner_id)132 def test_auto_subscribe_not_on_notification(self):133 """ People posting an automatic notification are not subscribed """134 self.test_track.sudo(self.user_admin).message_post(body='Coucou hibou', message_type='notification')135 self.assertEqual(self.test_track.message_partner_ids, self.user_employee.partner_id)136 def test_auto_subscribe_responsible(self):137 """ Responsibles are tracked and added as followers """138 sub = self.env['mail.test.track'].sudo(self.user_employee).create({139 'name': 'Test',140 'user_id': self.user_admin.id,141 })142 self.assertEqual(sub.message_partner_ids, (self.user_employee.partner_id | self.user_admin.partner_id))143 def test_auto_subscribe_defaults(self):144 """ Test auto subscription based on an umbrella record. This mimics145 the behavior of addons like project and task where subscribing to146 some project's subtypes automatically subscribe the follower to its tasks.147 Functional rules applied here148 * subscribing to an umbrella subtype with parent_id / relation_field set149 automatically create subscription with matching subtypes150 * subscribing to a sub-record as creator applies default subtype values151 * portal user should not have access to internal subtypes152 """153 umbrella = self.env['mail.test'].with_context(common.BaseFunctionalTest._test_context).create({154 'name': 'Project-Like',155 })156 umbrella.message_subscribe(partner_ids=[self.user_portal.partner_id.id])157 self.assertEqual(umbrella.message_partner_ids, self.user_portal.partner_id)158 sub1 = self.env['mail.test.track'].sudo(self.user_employee).create({159 'name': 'Task-Like Test',160 'umbrella_id': umbrella.id,161 })162 all_defaults = self.env['mail.message.subtype'].search([('default', '=', True), '|', ('res_model', '=', 'mail.test.track'), ('res_model', '=', False)])163 external_defaults = all_defaults.filtered(lambda subtype: not subtype.internal)164 self.assertEqual(sub1.message_partner_ids, self.user_portal.partner_id | self.user_employee.partner_id)165 self.assertEqual(166 sub1.message_follower_ids.filtered(lambda fol: fol.partner_id == self.user_portal.partner_id).subtype_ids,167 external_defaults | self.sub_umb1)168 self.assertEqual(169 sub1.message_follower_ids.filtered(lambda fol: fol.partner_id == self.user_employee.partner_id).subtype_ids,...
test_mail_activity.py
Source:test_mail_activity.py
1# -*- coding: utf-8 -*-2# Part of Odoo. See LICENSE file for full copyright and licensing details.3from datetime import date, datetime4from dateutil.relativedelta import relativedelta5from unittest.mock import patch6from unittest.mock import DEFAULT7import pytz8from odoo import exceptions, tests9from odoo.addons.test_mail.tests.common import BaseFunctionalTest10from odoo.addons.test_mail.tests.common import mail_new_test_user11from odoo.addons.test_mail.models.test_mail_models import MailTestActivity12class TestActivityCommon(BaseFunctionalTest):13 @classmethod14 def setUpClass(cls):15 super(TestActivityCommon, cls).setUpClass()16 cls.test_record = cls.env['mail.test.activity'].with_context(BaseFunctionalTest._test_context).create({'name': 'Test'})17 # reset ctx18 cls.test_record = cls.test_record.with_context(19 mail_create_nolog=False,20 mail_create_nosubscribe=False,21 mail_notrack=False22 )23@tests.tagged('mail_activity')24class TestActivityRights(TestActivityCommon):25 def test_activity_security_user_access(self):26 activity = self.test_record.activity_schedule(27 'test_mail.mail_act_test_todo',28 user_id=self.user_employee.id)29 activity2 = self.test_record.activity_schedule('test_mail.mail_act_test_todo')30 activity2.write({'user_id': self.user_employee.id})31 def test_activity_security_user_noaccess(self):32 def _employee_crash(*args, **kwargs):33 """ If employee is test employee, consider he has no access on document """34 recordset = args[0]35 if recordset.env.uid == self.user_employee.id:36 raise exceptions.AccessError('Hop hop hop Ernest, please step back.')37 return DEFAULT38 with patch.object(MailTestActivity, 'check_access_rights', autospec=True, side_effect=_employee_crash):39 with self.assertRaises(exceptions.UserError):40 activity = self.test_record.activity_schedule(41 'test_mail.mail_act_test_todo',42 user_id=self.user_employee.id)43 activity2 = self.test_record.activity_schedule('test_mail.mail_act_test_todo')44 with self.assertRaises(exceptions.UserError):45 activity2.write({'user_id': self.user_employee.id})46@tests.tagged('mail_activity')47class TestActivityFlow(TestActivityCommon):48 def test_activity_flow_employee(self):49 with self.sudoAs('ernest'):50 test_record = self.env['mail.test.activity'].browse(self.test_record.id)51 self.assertEqual(test_record.activity_ids, self.env['mail.activity'])52 # employee record an activity and check the deadline53 self.env['mail.activity'].create({54 'summary': 'Test Activity',55 'date_deadline': date.today() + relativedelta(days=1),56 'activity_type_id': self.env.ref('mail.mail_activity_data_email').id,57 'res_model_id': self.env['ir.model']._get(test_record._name).id,58 'res_id': test_record.id,59 })60 self.assertEqual(test_record.activity_summary, 'Test Activity')61 self.assertEqual(test_record.activity_state, 'planned')62 test_record.activity_ids.write({'date_deadline': date.today() - relativedelta(days=1)})63 test_record.invalidate_cache() # TDE note: should not have to do it I think64 self.assertEqual(test_record.activity_state, 'overdue')65 test_record.activity_ids.write({'date_deadline': date.today()})66 test_record.invalidate_cache() # TDE note: should not have to do it I think67 self.assertEqual(test_record.activity_state, 'today')68 # activity is done69 test_record.activity_ids.action_feedback(feedback='So much feedback')70 self.assertEqual(test_record.activity_ids, self.env['mail.activity'])71 self.assertEqual(test_record.message_ids[0].subtype_id, self.env.ref('mail.mt_activities'))72 def test_activity_flow_portal(self):73 portal_user = mail_new_test_user(self.env, login='chell', groups='base.group_portal', name='Chell Gladys')74 with self.sudoAs('chell'):75 test_record = self.env['mail.test.activity'].browse(self.test_record.id)76 with self.assertRaises(exceptions.AccessError):77 self.env['mail.activity'].create({78 'summary': 'Test Activity',79 'activity_type_id': self.env.ref('mail.mail_activity_data_email').id,80 'res_model_id': self.env['ir.model']._get(test_record._name).id,81 'res_id': test_record.id,82 })83 def test_activity_notify_other_user(self):84 self.user_admin.notification_type = 'email'85 rec = self.test_record.sudo(self.user_employee)86 with self.assertNotifications(partner_admin=(1, 'email', 'read')):87 activity = rec.activity_schedule(88 'test_mail.mail_act_test_todo',89 user_id=self.user_admin.id)90 self.assertEqual(activity.create_user_id, self.user_employee)91 self.assertEqual(activity.user_id, self.user_admin)92 def test_activity_notify_same_user(self):93 self.user_employee.notification_type = 'email'94 rec = self.test_record.sudo(self.user_employee)95 with self.assertNotifications(partner_employee=(0, 'email', 'read')):96 activity = rec.activity_schedule(97 'test_mail.mail_act_test_todo',98 user_id=self.user_employee.id)99 self.assertEqual(activity.create_user_id, self.user_employee)100 self.assertEqual(activity.user_id, self.user_employee)101@tests.tagged('mail_activity')102class TestActivityMixin(TestActivityCommon):103 def test_activity_mixin(self):104 self.user_employee.tz = self.user_admin.tz105 with self.sudoAs('ernest'):106 self.assertEqual(self.test_record.env.user, self.user_employee)107 now_utc = datetime.now(pytz.UTC)108 now_user = now_utc.astimezone(pytz.timezone(self.env.user.tz or 'UTC'))109 today_user = now_user.date()110 # Test various scheduling of activities111 act1 = self.test_record.activity_schedule(112 'test_mail.mail_act_test_todo',113 today_user + relativedelta(days=1),114 user_id=self.user_admin.id)115 self.assertEqual(act1.automated, True)116 act_type = self.env.ref('test_mail.mail_act_test_todo')117 self.assertEqual(self.test_record.activity_summary, act_type.summary)118 self.assertEqual(self.test_record.activity_state, 'planned')119 self.assertEqual(self.test_record.activity_user_id, self.user_admin)120 act2 = self.test_record.activity_schedule(121 'test_mail.mail_act_test_meeting',122 today_user + relativedelta(days=-1))123 self.assertEqual(self.test_record.activity_state, 'overdue')124 self.assertEqual(self.test_record.activity_user_id, self.user_employee)125 act3 = self.test_record.activity_schedule(126 'test_mail.mail_act_test_todo',127 today_user + relativedelta(days=3),128 user_id=self.user_employee.id)129 self.assertEqual(self.test_record.activity_state, 'overdue')130 self.assertEqual(self.test_record.activity_user_id, self.user_employee)131 self.test_record.invalidate_cache(ids=self.test_record.ids)132 self.assertEqual(self.test_record.activity_ids, act1 | act2 | act3)133 # Perform todo activities for admin134 self.test_record.activity_feedback(135 ['test_mail.mail_act_test_todo'],136 user_id=self.user_admin.id,137 feedback='Test feedback',)138 self.assertEqual(self.test_record.activity_ids, act2 | act3)139 # Reschedule all activities, should update the record state140 self.assertEqual(self.test_record.activity_state, 'overdue')141 self.test_record.activity_reschedule(142 ['test_mail.mail_act_test_meeting', 'test_mail.mail_act_test_todo'],143 date_deadline=today_user + relativedelta(days=3)144 )145 self.assertEqual(self.test_record.activity_state, 'planned')146 # Perform todo activities for remaining people147 self.test_record.activity_feedback(148 ['test_mail.mail_act_test_todo'],149 feedback='Test feedback')150 # Setting activities as done should delete them and post messages151 self.assertEqual(self.test_record.activity_ids, act2)152 self.assertEqual(len(self.test_record.message_ids), 2)153 self.assertEqual(self.test_record.message_ids.mapped('subtype_id'), self.env.ref('mail.mt_activities'))154 # Perform meeting activities155 self.test_record.activity_unlink(['test_mail.mail_act_test_meeting'])156 # Canceling activities should simply remove them157 self.assertEqual(self.test_record.activity_ids, self.env['mail.activity'])158 self.assertEqual(len(self.test_record.message_ids), 2)159 def test_activity_mixin_archive(self):160 rec = self.test_record.sudo(self.user_employee)161 new_act = rec.activity_schedule(162 'test_mail.mail_act_test_todo',163 user_id=self.user_admin.id)164 self.assertEqual(rec.activity_ids, new_act)165 rec.toggle_active()166 self.assertEqual(rec.active, False)167 self.assertEqual(rec.activity_ids, self.env['mail.activity'])168 rec.toggle_active()169 self.assertEqual(rec.active, True)170 self.assertEqual(rec.activity_ids, self.env['mail.activity'])171 def test_activity_mixin_reschedule_user(self):172 rec = self.test_record.sudo(self.user_employee)173 rec.activity_schedule(174 'test_mail.mail_act_test_todo',175 user_id=self.user_admin.id)176 self.assertEqual(rec.activity_ids[0].user_id, self.user_admin)177 # reschedule its own should not alter other's activities178 rec.activity_reschedule(179 ['test_mail.mail_act_test_todo'],180 user_id=self.user_employee.id,181 new_user_id=self.user_employee.id)182 self.assertEqual(rec.activity_ids[0].user_id, self.user_admin)183 rec.activity_reschedule(184 ['test_mail.mail_act_test_todo'],185 user_id=self.user_admin.id,186 new_user_id=self.user_employee.id)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!