[FW][REF] (website_)knowledge: lint and cleanup tests
This commit contains some post-merge cleanup in knowledge tests before adding
some new tests
* remove an unused common file, holding unused data;
* some rephrasing and cleanup in comments;
* move fields specific test into an "internal" file that holds tests for
computed fields notably;
Task-2859616
X-original-commit: odoo/odoo@bad3ee069e
Part-of: odoo/odoo#95471
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
from . import test_knowledge_article_business
|
||||
from . import test_knowledge_article_constraints
|
||||
from . import test_knowledge_article_internals
|
||||
from . import test_knowledge_article_permissions
|
||||
from . import test_knowledge_article_sequence
|
||||
from . import test_knowledge_form_ui
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from freezegun import freeze_time
|
||||
|
||||
from odoo import exceptions
|
||||
from odoo.addons.knowledge.tests.common import KnowledgeCommonWData
|
||||
from odoo.tests.common import tagged, users
|
||||
@@ -505,120 +502,6 @@ class TestKnowledgeArticleBusiness(KnowledgeCommonWData):
|
||||
self.assertEqual(workspace_info['root_article_id'], (article_workspace.id, f'📄 {article_workspace.name}'))
|
||||
|
||||
|
||||
@tagged('knowledge_internals')
|
||||
class TestKnowledgeArticleFields(KnowledgeCommonWData):
|
||||
""" Test fields and their management. """
|
||||
|
||||
@users('employee')
|
||||
def test_favorites(self):
|
||||
""" Testing the API for toggling favorites. """
|
||||
playground_articles = (self.article_workspace + self.workspace_children).with_env(self.env)
|
||||
self.assertEqual(playground_articles.mapped('is_user_favorite'), [False, False, False])
|
||||
|
||||
playground_articles[0].write({'favorite_ids': [(0, 0, {'user_id': self.env.uid})]})
|
||||
self.assertEqual(playground_articles.mapped('is_user_favorite'), [True, False, False])
|
||||
self.assertEqual(playground_articles.mapped('user_favorite_sequence'), [1, -1, -1])
|
||||
favorites = self.env['knowledge.article.favorite'].sudo().search([('user_id', '=', self.env.uid)])
|
||||
self.assertEqual(favorites.article_id, playground_articles[0])
|
||||
self.assertEqual(favorites.sequence, 1)
|
||||
|
||||
playground_articles[1].action_toggle_favorite()
|
||||
self.assertEqual(playground_articles.mapped('is_user_favorite'), [True, True, False])
|
||||
self.assertEqual(playground_articles.mapped('user_favorite_sequence'), [1, 2, -1])
|
||||
favorites = self.env['knowledge.article.favorite'].sudo().search([('user_id', '=', self.env.uid)])
|
||||
self.assertEqual(favorites.article_id, playground_articles[0:2])
|
||||
self.assertEqual(favorites.mapped('sequence'), [1, 2])
|
||||
|
||||
playground_articles[2].with_user(self.user_employee2).action_toggle_favorite()
|
||||
favorites = self.env['knowledge.article.favorite'].sudo().search([('user_id', '=', self.user_employee2.id)])
|
||||
self.assertEqual(favorites.article_id, playground_articles[2])
|
||||
self.assertEqual(favorites.sequence, 1, 'Favorite: should not be impacted by other people sequence')
|
||||
|
||||
@users('employee')
|
||||
def test_fields_edition(self):
|
||||
_reference_dt = datetime(2022, 5, 31, 10, 0, 0)
|
||||
body_values = [False, '', '<p><br /></p>', '<p>MyBody</p>']
|
||||
|
||||
for index, body in enumerate(body_values):
|
||||
self.patch(self.env.cr, 'now', lambda: _reference_dt)
|
||||
with freeze_time(_reference_dt):
|
||||
article = self.env['knowledge.article'].create({
|
||||
'body': body,
|
||||
'internal_permission': 'write',
|
||||
'name': 'MyArticle,'
|
||||
})
|
||||
self.assertEqual(article.last_edition_uid, self.env.user)
|
||||
self.assertEqual(article.last_edition_date, _reference_dt)
|
||||
|
||||
self.patch(self.env.cr, 'now', lambda: _reference_dt + timedelta(days=1))
|
||||
|
||||
# fields that does not change content
|
||||
with freeze_time(_reference_dt + timedelta(days=1)):
|
||||
article.with_user(self.user_employee2).write({
|
||||
'name': 'NoContentEdition'
|
||||
})
|
||||
self.assertEqual(article.last_edition_uid, self.env.user)
|
||||
self.assertEqual(article.last_edition_date, _reference_dt)
|
||||
|
||||
# fields that change content
|
||||
with freeze_time(_reference_dt + timedelta(days=1)):
|
||||
article.with_user(self.user_employee2).write({
|
||||
'body': body_values[(index + 1) if index < (len(body_values)-1) else 0]
|
||||
})
|
||||
# the with_user() below is necessary for the test to succeed,
|
||||
# and that's kind of a bad smell...
|
||||
article.with_user(self.user_employee2).flush_model()
|
||||
self.assertEqual(article.last_edition_uid, self.user_employee2)
|
||||
self.assertEqual(article.last_edition_date, _reference_dt + timedelta(days=1))
|
||||
|
||||
|
||||
@tagged('knowledge_internals', 'knowledge_management')
|
||||
class TestKnowledgeCommonWDataInitialValue(KnowledgeCommonWData):
|
||||
""" Test initial values or our test data once so that other tests do not have
|
||||
to do it. """
|
||||
|
||||
def test_initial_values(self):
|
||||
""" Ensure all tests have the same basis (global values computed as root) """
|
||||
# root
|
||||
article_workspace = self.article_workspace
|
||||
self.assertTrue(article_workspace.category, 'workspace')
|
||||
self.assertEqual(article_workspace.sequence, 999)
|
||||
article_shared = self.article_shared
|
||||
self.assertTrue(article_shared.category, 'shared')
|
||||
self.assertTrue(article_shared.sequence, 998)
|
||||
|
||||
# workspace children
|
||||
workspace_children = article_workspace.child_ids
|
||||
self.assertEqual(
|
||||
workspace_children.mapped('inherited_permission'),
|
||||
['write', 'write']
|
||||
)
|
||||
self.assertEqual(workspace_children.inherited_permission_parent_id, article_workspace)
|
||||
self.assertEqual(
|
||||
workspace_children.mapped('internal_permission'),
|
||||
[False, False]
|
||||
)
|
||||
self.assertEqual(workspace_children.root_article_id, article_workspace)
|
||||
self.assertEqual(workspace_children.mapped('sequence'), [0, 1])
|
||||
|
||||
@mute_logger('odoo.addons.base.models.ir_rule')
|
||||
@users('employee')
|
||||
def test_initial_values_as_employee(self):
|
||||
""" Ensure all tests have the same basis (user specific computed as
|
||||
employee for acl-dependent tests) """
|
||||
article_workspace = self.article_workspace.with_env(self.env)
|
||||
self.assertTrue(article_workspace.user_has_access)
|
||||
self.assertTrue(article_workspace.user_has_write_access)
|
||||
|
||||
article_shared = self.article_shared.with_env(self.env)
|
||||
self.assertTrue(article_shared.user_has_access)
|
||||
self.assertFalse(article_shared.user_has_write_access)
|
||||
|
||||
article_private = self.article_private_manager.with_env(self.env)
|
||||
with self.assertRaises(exceptions.AccessError):
|
||||
self.assertFalse(article_private.body)
|
||||
|
||||
|
||||
@tagged('post_install', '-at_install', 'knowledge_internals', 'knowledge_management')
|
||||
class TestKnowledgeShare(KnowledgeCommonWData):
|
||||
""" Test share feature. """
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from freezegun import freeze_time
|
||||
|
||||
from odoo import exceptions
|
||||
from odoo.addons.knowledge.tests.common import KnowledgeCommonWData
|
||||
from odoo.tests.common import tagged, users
|
||||
from odoo.tools import mute_logger
|
||||
|
||||
|
||||
@tagged('knowledge_internals')
|
||||
class TestKnowledgeArticleFields(KnowledgeCommonWData):
|
||||
""" Test fields and their management. """
|
||||
|
||||
@users('employee')
|
||||
def test_favorites(self):
|
||||
""" Testing the API for toggling favorites. """
|
||||
playground_articles = (self.article_workspace + self.workspace_children).with_env(self.env)
|
||||
self.assertEqual(playground_articles.mapped('is_user_favorite'), [False, False, False])
|
||||
|
||||
playground_articles[0].write({'favorite_ids': [(0, 0, {'user_id': self.env.uid})]})
|
||||
self.assertEqual(playground_articles.mapped('is_user_favorite'), [True, False, False])
|
||||
self.assertEqual(playground_articles.mapped('user_favorite_sequence'), [1, -1, -1])
|
||||
favorites = self.env['knowledge.article.favorite'].sudo().search([('user_id', '=', self.env.uid)])
|
||||
self.assertEqual(favorites.article_id, playground_articles[0])
|
||||
self.assertEqual(favorites.sequence, 1)
|
||||
|
||||
playground_articles[1].action_toggle_favorite()
|
||||
self.assertEqual(playground_articles.mapped('is_user_favorite'), [True, True, False])
|
||||
self.assertEqual(playground_articles.mapped('user_favorite_sequence'), [1, 2, -1])
|
||||
favorites = self.env['knowledge.article.favorite'].sudo().search([('user_id', '=', self.env.uid)])
|
||||
self.assertEqual(favorites.article_id, playground_articles[0:2])
|
||||
self.assertEqual(favorites.mapped('sequence'), [1, 2])
|
||||
|
||||
playground_articles[2].with_user(self.user_employee2).action_toggle_favorite()
|
||||
favorites = self.env['knowledge.article.favorite'].sudo().search([('user_id', '=', self.user_employee2.id)])
|
||||
self.assertEqual(favorites.article_id, playground_articles[2])
|
||||
self.assertEqual(favorites.sequence, 1, 'Favorite: should not be impacted by other people sequence')
|
||||
|
||||
@users('employee')
|
||||
def test_fields_edition(self):
|
||||
_reference_dt = datetime(2022, 5, 31, 10, 0, 0)
|
||||
body_values = [False, '', '<p><br /></p>', '<p>MyBody</p>']
|
||||
|
||||
for index, body in enumerate(body_values):
|
||||
self.patch(self.env.cr, 'now', lambda: _reference_dt)
|
||||
with freeze_time(_reference_dt):
|
||||
article = self.env['knowledge.article'].create({
|
||||
'body': body,
|
||||
'internal_permission': 'write',
|
||||
'name': 'MyArticle,'
|
||||
})
|
||||
self.assertEqual(article.last_edition_uid, self.env.user)
|
||||
self.assertEqual(article.last_edition_date, _reference_dt)
|
||||
|
||||
self.patch(self.env.cr, 'now', lambda: _reference_dt + timedelta(days=1))
|
||||
|
||||
# fields that does not change content
|
||||
with freeze_time(_reference_dt + timedelta(days=1)):
|
||||
article.with_user(self.user_employee2).write({
|
||||
'name': 'NoContentEdition'
|
||||
})
|
||||
self.assertEqual(article.last_edition_uid, self.env.user)
|
||||
self.assertEqual(article.last_edition_date, _reference_dt)
|
||||
|
||||
# fields that change content
|
||||
with freeze_time(_reference_dt + timedelta(days=1)):
|
||||
article.with_user(self.user_employee2).write({
|
||||
'body': body_values[(index + 1) if index < (len(body_values)-1) else 0]
|
||||
})
|
||||
# the with_user() below is necessary for the test to succeed,
|
||||
# and that's kind of a bad smell...
|
||||
article.with_user(self.user_employee2).flush_model()
|
||||
self.assertEqual(article.last_edition_uid, self.user_employee2)
|
||||
self.assertEqual(article.last_edition_date, _reference_dt + timedelta(days=1))
|
||||
|
||||
|
||||
@tagged('knowledge_internals', 'knowledge_management')
|
||||
class TestKnowledgeCommonWDataInitialValue(KnowledgeCommonWData):
|
||||
""" Test initial values or our test data once so that other tests do not have
|
||||
to do it. """
|
||||
|
||||
def test_initial_values(self):
|
||||
""" Ensure all tests have the same basis (global values computed as root) """
|
||||
# root
|
||||
article_workspace = self.article_workspace
|
||||
self.assertTrue(article_workspace.category, 'workspace')
|
||||
self.assertEqual(article_workspace.sequence, 999)
|
||||
article_shared = self.article_shared
|
||||
self.assertTrue(article_shared.category, 'shared')
|
||||
self.assertTrue(article_shared.sequence, 998)
|
||||
|
||||
# workspace children
|
||||
workspace_children = article_workspace.child_ids
|
||||
self.assertEqual(
|
||||
workspace_children.mapped('inherited_permission'),
|
||||
['write', 'write']
|
||||
)
|
||||
self.assertEqual(workspace_children.inherited_permission_parent_id, article_workspace)
|
||||
self.assertEqual(
|
||||
workspace_children.mapped('internal_permission'),
|
||||
[False, False]
|
||||
)
|
||||
self.assertEqual(workspace_children.root_article_id, article_workspace)
|
||||
self.assertEqual(workspace_children.mapped('sequence'), [0, 1])
|
||||
|
||||
@mute_logger('odoo.addons.base.models.ir_rule')
|
||||
@users('employee')
|
||||
def test_initial_values_as_employee(self):
|
||||
""" Ensure all tests have the same basis (user specific computed as
|
||||
employee for acl-dependent tests) """
|
||||
article_workspace = self.article_workspace.with_env(self.env)
|
||||
self.assertTrue(article_workspace.user_has_access)
|
||||
self.assertTrue(article_workspace.user_has_write_access)
|
||||
|
||||
article_shared = self.article_shared.with_env(self.env)
|
||||
self.assertTrue(article_shared.user_has_access)
|
||||
self.assertFalse(article_shared.user_has_write_access)
|
||||
|
||||
article_private = self.article_private_manager.with_env(self.env)
|
||||
with self.assertRaises(exceptions.AccessError):
|
||||
self.assertFalse(article_private.body)
|
||||
@@ -1,30 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo.addons.knowledge.tests.common import KnowledgeCommonWData
|
||||
|
||||
|
||||
class WKnowledgeCommonWData(KnowledgeCommonWData):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
|
||||
# - Playground (workspace, writable)
|
||||
# - Child1
|
||||
# - Child2
|
||||
# - Private (shared)
|
||||
# - Child1
|
||||
# - ChildPublished
|
||||
cls.shared_children += cls.env['knowledge.article'].create([
|
||||
{'article_member_ids': [
|
||||
(0, 0, {'partner_id': cls.partner_admin.id,
|
||||
'permission': 'write',
|
||||
}),
|
||||
],
|
||||
'internal_permission': 'none',
|
||||
'is_published': True,
|
||||
'name': 'Private Child1',
|
||||
'parent_id': cls.article_shared.id,
|
||||
}
|
||||
])
|
||||
@@ -2,19 +2,19 @@
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import exceptions
|
||||
from odoo.addons.knowledge.tests.common import KnowledgeArticlePermissionsCase
|
||||
from odoo.addons.knowledge.tests.common import KnowledgeCommonWData
|
||||
from odoo.tests.common import tagged, users
|
||||
from odoo.tools import mute_logger
|
||||
|
||||
|
||||
@tagged('knowledge_acl')
|
||||
class TestWKnowledgeSecurity(KnowledgeArticlePermissionsCase):
|
||||
class TestWKnowledgeSecurity(KnowledgeCommonWData):
|
||||
|
||||
@mute_logger('odoo.addons.base.models.ir_model', 'odoo.addons.base.models.ir_rule')
|
||||
@users('user_public')
|
||||
def test_models_as_public(self):
|
||||
""" Test publish flag giving read access to articles """
|
||||
article_shared = self.article_roots[2].with_env(self.env)
|
||||
article_shared = self.article_shared.with_env(self.env)
|
||||
with self.assertRaises(exceptions.AccessError,
|
||||
msg="ACLs: Internal permission 'none', not for public"):
|
||||
article_shared.body # access body should trigger acls
|
||||
@@ -30,7 +30,7 @@ class TestWKnowledgeSecurity(KnowledgeArticlePermissionsCase):
|
||||
@users('portal_test')
|
||||
def test_models_as_portal(self):
|
||||
""" Test publish flag giving read access to articles """
|
||||
article_shared = self.article_roots[2].with_env(self.env)
|
||||
article_shared = self.article_shared.with_env(self.env)
|
||||
with self.assertRaises(exceptions.AccessError,
|
||||
msg="ACLs: Internal permission 'none', not for portal"):
|
||||
article_shared.body # access body should trigger acls
|
||||
@@ -48,7 +48,7 @@ class TestWKnowledgeSecurity(KnowledgeArticlePermissionsCase):
|
||||
@users('portal_test')
|
||||
def test_models_as_user(self):
|
||||
""" Test publish flag giving read access to articles """
|
||||
article_hidden = self.article_read_contents[3].with_env(self.env)
|
||||
article_hidden = self.private_children[0].with_env(self.env)
|
||||
with self.assertRaises(exceptions.AccessError,
|
||||
msg="ACLs: 'none' internal permission"):
|
||||
article_hidden.body # access body should trigger acls
|
||||
|
||||
Reference in New Issue
Block a user