-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
test_send.py
114 lines (104 loc) · 3.81 KB
/
test_send.py
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
# Copyright 2022 Camptocamp SA
# @author Simone Orsi <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import responses
from odoo import exceptions
from .common import TestEDIWebserviceBase
class TestSend(TestEDIWebserviceBase):
@classmethod
def _setup_records(cls):
super()._setup_records()
cls.ws_backend = cls.backend.webservice_backend_id
cls.settings1 = """
components:
send:
usage: webservice.send
work_ctx:
webservice:
_no_method: post
pargs:
- foo
- bar
kwargs:
random: 1
url_params:
endpoint: push/here
"""
cls.settings2 = """
components:
send:
usage: webservice.send
work_ctx:
webservice:
method: post
kwargs:
url_params:
endpoint: push/here
"""
cls.record.type_id.set_settings(cls.settings1)
cls.a_user = (
cls.env["res.users"]
.with_context(no_reset_password=True)
.create(
{
"name": "foo",
"login": "a_user",
"email": "[email protected]",
"groups_id": [
(
6,
0,
(cls.env.ref("base.group_user")).ids,
)
],
}
)
)
def test_find_component(self):
component = self.backend._get_component(self.record, "send")
self.assertEqual(component._name, "edi.webservice.send")
def test_component_settings(self):
component = self.backend._get_component(self.record, "send")
self.assertEqual(
component.ws_settings,
{
"_no_method": "post",
"pargs": ["foo", "bar"],
"kwargs": {
"random": 1,
"url_params": {
"endpoint": "push/here",
},
},
},
)
def test_component_no_method(self):
component = self.backend._get_component(self.record, "send")
msg = "`method` is required in `webservice` type settings"
with self.assertRaisesRegex(exceptions.UserError, msg):
component._get_call_params()
def test_component_params(self):
self.record.type_id.set_settings(self.settings2)
component = self.backend._get_component(self.record, "send")
method, pargs, kwargs = component._get_call_params()
self.assertEqual(method, "post")
self.assertEqual(len(kwargs), 2)
self.assertEqual(kwargs["data"], "This is a simple file")
self.assertEqual(kwargs["url_params"], {"endpoint": "push/here"})
@responses.activate
def test_component_send(self):
self.record.type_id.set_settings(self.settings2)
# Internal user should be able to call the third party webservice
# without read access (no ir.access.model records)
# on `webservice.backend` model which store credentials
record = self.record.with_user(self.a_user)
backend = self.backend.with_user(self.a_user)
url = "https://foo.test/push/here"
responses.add(responses.POST, url, body="{}")
component = backend._get_component(record, "send")
result = component.send()
self.assertEqual(result, b"{}")
self.assertEqual(
responses.calls[0].request.headers["Content-Type"], "application/xml"
)
self.assertEqual(responses.calls[0].request.body, "This is a simple file")