File: test_wizard.py

package info (click to toggle)
tryton-server 7.0.40-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,748 kB
  • sloc: python: 53,502; xml: 5,194; sh: 803; sql: 217; makefile: 28
file content (154 lines) | stat: -rw-r--r-- 5,501 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# This file is part of Tryton.  The COPYRIGHT file at the top level of this
# repository contains the full copyright notices and license terms.
import unittest

from trytond.model.exceptions import AccessError
from trytond.pool import Pool
from trytond.tests.test_tryton import activate_module, with_transaction
from trytond.transaction import Transaction


class WizardTestCase(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        activate_module('tests')

    @with_transaction()
    def test_create(self):
        'Create Session Wizard'
        pool = Pool()
        Wizard = pool.get('test.test_wizard', type='wizard')

        session_id, start_state, end_state = Wizard.create()
        self.assertEqual(start_state, 'start')
        self.assertEqual(end_state, 'end')
        self.assertTrue(session_id)

    @with_transaction()
    def test_delete(self):
        'Delete Session Wizard'
        pool = Pool()
        Wizard = pool.get('test.test_wizard', type='wizard')

        session_id, _, _ = Wizard.create()
        Wizard.delete(session_id)

    @with_transaction()
    def test_session(self):
        'Session Wizard'
        pool = Pool()
        Wizard = pool.get('test.test_wizard', type='wizard')
        Session = pool.get('ir.session.wizard')
        Group = pool.get('res.group')
        transaction = Transaction()

        session_id, = Session.create([{}])
        session = Wizard(session_id)
        self.assertEqual(session.start.id, None)
        self.assertRaises(AttributeError, getattr, session.start, 'name')
        self.assertEqual(hasattr(session.start, 'name'), False)
        session.start.name = 'Test'
        self.assertRaises(AttributeError, getattr, session.start, 'user')
        self.assertEqual(hasattr(session.start, 'user'), False)
        session.start.user = transaction.user
        group_a, = Group.create([{
                    'name': 'Group A',
                    }])
        group_b, = Group.create([{
                    'name': 'Group B',
                    }])
        session.start.groups = [
            group_a,
            group_b,
            ]
        session._save()
        session = Wizard(session_id)
        self.assertEqual(session.start.id, None)
        self.assertEqual(session.start.name, 'Test')
        self.assertEqual(session.start.user.id, transaction.user)
        self.assertEqual(session.start.user.login, 'admin')
        group_a, group_b = session.start.groups
        self.assertEqual(group_a.name, 'Group A')
        self.assertEqual(group_b.name, 'Group B')

    @with_transaction()
    def test_execute(self):
        'Execute Wizard'
        pool = Pool()
        Wizard = pool.get('test.test_wizard', type='wizard')

        session_id, start_state, end_state = Wizard.create()
        result = Wizard.execute(session_id, {}, start_state)
        self.assertEqual(list(result.keys()), ['view'])
        self.assertEqual(result['view']['defaults'], {
                'name': 'Test wizard',
                })
        self.assertEqual(result['view']['buttons'], [
                {
                    'state': 'end',
                    'states': '{}',
                    'icon': 'tryton-cancel',
                    'default': False,
                    'string': 'Cancel',
                    'validate': False,
                    },
                {
                    'state': 'next_',
                    'states': '{}',
                    'icon': 'tryton-next',
                    'default': True,
                    'string': 'Next',
                    'validate': True,
                    },
                ])
        result = Wizard.execute(session_id, {
                start_state: {
                    'name': 'Test Update',
                    }}, 'next_')
        self.assertEqual(len(result['actions']), 1)

    @with_transaction()
    def test_execute_without_access(self):
        "Execute wizard without model access"
        pool = Pool()
        Wizard = pool.get('test.test_wizard', type='wizard')
        Model = pool.get('ir.model')
        ModelAccess = pool.get('ir.model.access')
        model, = Model.search([('model', '=', 'test.access')])
        ModelAccess.create([{
                    'model': model.id,
                    'perm_write': False,
                    }])

        session_id, start_state, end_state = Wizard.create()

        with self.assertRaises(AccessError):
            with Transaction().set_context(active_model='test.access'):
                Wizard.execute(session_id, {}, start_state)

    @with_transaction()
    def test_execute_without_read_access(self):
        "Execute wizard without read access"
        pool = Pool()
        Wizard = pool.get('test.test_wizard', type='wizard')

        session_id, start_state, end_state = Wizard.create()

        with self.assertRaises(AccessError):
            with Transaction().set_context(
                    active_model='test.access', active_id=1):
                Wizard.execute(session_id, {}, start_state)

    @with_transaction()
    def test_execute_wrong_model(self):
        "Execute wizard on wrong model"
        pool = Pool()
        Wizard = pool.get('test.test_wizard', type='wizard')

        session_id, start_state, end_state = Wizard.create()

        with self.assertRaises(AccessError):
            with Transaction().set_context(
                    active_model='test.test_wizard.start'):
                Wizard.execute(session_id, {}, start_state)