- Odoo 11 Development Essentials(Third Edition)
- Daniel Reis
- 266字
- 2021-08-27 19:32:45
Testing access security
In fact, our tests should be failing right now due to the missing access rules. They aren't because they are done with the Admin user. In databases with demo data enabled, we also make available a Demo user, with "typical" backend access. We should change the tests so that they are performed using the Demo user.
We can modify our tests to take this into account. Edit the tests/test_todo.py file to add a setUp method:
def setUp(self, *args, **kwargs): result = super(TestTodo, self).setUp(*args, **kwargs) user_demo = self.env.ref('base.user_demo') self.env= self.env(user=user_demo) return result
This first instruction calls the setUp code of the parent class. The next ones change the environment used to run the tests, self.env, to a new one using the Demo user. No further changes are needed for the tests we already wrote.
We should also add a test case to make sure that users can see only their own tasks. To do this, first add an additional import at the top:
from odoo.exceptions import AccessError
Next, add an additional method to the test class:
def test_record_rule(self): "Test per user record rules" Todo = self.env['todo.task'] task = Todo.sudo().create({'name': 'Admin Task'}) with self.assertRaises(AccessError): Todo.browse([task.id]).name
Since our env method is now using the Demo user, we use the sudo() method to change the context to the admin user. We then use it to create a task that should not be accessible to the Demo user.
When trying to access this task data, we expect an AccessError exception to be raised.
If we run the tests now, they should fail, so let's take care of that.
- Excel 2010中文版完全自學手冊
- Word排版技術大全(第2版)
- Excel 2013使用詳解
- 計算機應用基礎Office 2010實踐指導
- Excel職場辦公:表格范例大全
- 天天用的Office一百招
- PowerPoint 2010辦公應用實戰從入門到精通
- Excel 2013從入門到精通
- Excel效率手冊:早做完,不加班 ( 精華版·函數篇)(升級版)
- 快·易·通:2天學會Word/Excel綜合辦公應用(2016版)
- 微軟辦公軟件國際認證(MOS)Office 2010大師級通關教程(第2版)
- 中文PowerPoint 2003應用基礎
- 大話PPT:幻燈片制作真經
- 用圖表說話:Excel精美實用圖表大制作
- Word/Excel/PowerPoint 2007三合一辦公應用實戰從入門到精通