File: test_routes.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 (197 lines) | stat: -rw-r--r-- 6,365 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# 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 base64
import json
import unittest

from trytond.pool import Pool
from trytond.protocols.wrappers import Response
from trytond.tests.test_tryton import DB_NAME, Client, activate_module, drop_db
from trytond.transaction import Transaction
from trytond.wsgi import app


class RoutesTestCase(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        drop_db()
        activate_module(['ir', 'res'], 'fr')
        pool = Pool(DB_NAME)
        with Transaction().start(DB_NAME, 0):
            User = pool.get('res.user')
            admin, = User.search([('login', '=', 'admin')])
            admin.password = 'password'
            admin.save()

    @classmethod
    def tearDownClass(cls):
        super().tearDownClass()
        drop_db()

    @property
    def auth_headers(self):
        return {
            'Authorization': (
                'Basic ' + base64.b64encode(b'admin:password').decode()),
            }

    def data_url(self, model):
        return '/%(database)s/data/%(model)s' % {
            'database': DB_NAME,
            'model': model,
            }

    def test_data_no_field(self):
        "Test GET data without field"
        c = Client(app, Response)

        response = c.get(self.data_url('res.user'), headers=self.auth_headers)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, b'\r\n\r\n')

    def test_data_one_field(self):
        "Test GET data with one field"
        c = Client(app, Response)

        response = c.get(
            self.data_url('res.user'), headers=self.auth_headers,
            query_string=[('f', 'name')])

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, b'Nom\r\nAdministrator\r\n')

    def test_data_multiple_fields(self):
        "Test GET data with multiple fields"
        c = Client(app, Response)

        response = c.get(
            self.data_url('res.user'), headers=self.auth_headers,
            query_string=[('f', 'name'), ('f', 'login')])

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.data, b'Nom,Identifiant\r\nAdministrator,admin\r\n')

    def test_data_language(self):
        "Test GET data with language"
        c = Client(app, Response)

        response = c.get(
            self.data_url('ir.lang'), headers=self.auth_headers,
            query_string=[
                ('f', 'name'),
                ('l', 'fr'),
                ('d', json.dumps([('code', '=', 'fr')])),
                ])

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, 'Nom\r\nFrançais\r\n'.encode('utf-8'))

    def test_data_size(self):
        "Test GET data with size limit"
        c = Client(app, Response)

        response = c.get(
            self.data_url('ir.lang'), headers=self.auth_headers,
            query_string=[
                ('f', 'name'),
                ('s', 5),
                ])

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data.splitlines()), 5 + 1)

    def test_data_page(self):
        "Test GET data with page"
        c = Client(app, Response)

        response0 = c.get(
            self.data_url('ir.lang'), headers=self.auth_headers,
            query_string=[
                ('f', 'name'),
                ('s', 5),
                ('p', 0)
                ])
        response1 = c.get(
            self.data_url('ir.lang'), headers=self.auth_headers,
            query_string=[
                ('f', 'name'),
                ('s', 5),
                ('p', 1)
                ])

        self.assertEqual(response0.status_code, 200)
        self.assertEqual(response1.status_code, 200)
        self.assertNotEqual(response0.data, response1.data)

    def test_data_encoding(self):
        "Test GET data with encoding"
        c = Client(app, Response)

        response = c.get(
            self.data_url('ir.lang'), headers=self.auth_headers,
            query_string=[
                ('f', 'name'),
                ('l', 'fr'),
                ('d', json.dumps([('code', '=', 'fr')])),
                ('enc', 'latin1'),
                ])

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.data, 'Nom\r\nFrançais\r\n'.encode('latin1'))

    def test_data_delimiter(self):
        "Test GET data with delimiter"
        c = Client(app, Response)

        response = c.get(
            self.data_url('res.user'), headers=self.auth_headers,
            query_string=[('f', 'name'), ('f', 'login'), ('dl', '|')])

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.data, b'Nom|Identifiant\r\nAdministrator|admin\r\n')

    def test_data_quotechar(self):
        "Test GET data with quotechar"
        c = Client(app, Response)

        response = c.get(
            self.data_url('res.user'), headers=self.auth_headers,
            query_string=[
                ('f', 'name'), ('f', 'login'), ('dl', 'n'), ('qc', '*')])

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.data,
            b'Nomn*Identifiant*\r\n*Administrator*n*admin*\r\n')

    def test_data_no_header(self):
        "Test GET data without header"
        c = Client(app, Response)

        response = c.get(
            self.data_url('res.user'), headers=self.auth_headers,
            query_string=[('f', 'name'), ('h', 0)])

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, b'Administrator\r\n')

    def test_data_locale_format(self):
        "Test GET data in locale format"
        c = Client(app, Response)

        response_std = c.get(
            self.data_url('res.user'), headers=self.auth_headers,
            query_string=[('f', 'create_date')])
        response_locale = c.get(
            self.data_url('res.user'), headers=self.auth_headers,
            query_string=[('f', 'create_date'), ('loc', 1)])

        self.assertEqual(response_std.status_code, 200)
        self.assertEqual(response_locale.status_code, 200)
        self.assertNotEqual(response_std.data, response_locale.data)