Skip to content

Commit 620b857

Browse files
committed
[FIX] web: tests - allow custom responses in onRpc
Before this commit, when an 'onRpc' handler would return a 'Response' object, it would still be wrapped in a JSON-RPC payload (under the 'result' key) if the "content-type" header specified that it was a JSON-RPC. However, if a 'Response' object is returned by the handler, it usually means that the response should be that object as-is, as it was created with the desired final parameters. This commit ensures that 'Response' values are returned as they are, instead of being wrapped in a JSON-RPC payload object. Since this is also in the scope of mock server responses, this commit removes the use of '{ pure: true }' RPC handlers that are not necessary. closes odoo#233199 X-original-commit: a2cab03 Signed-off-by: Aaron Bohy (aab) <aab@odoo.com> Signed-off-by: Julien Mougenot (jum) <jum@odoo.com>
1 parent 1128361 commit 620b857

File tree

8 files changed

+35
-40
lines changed

8 files changed

+35
-40
lines changed

addons/calendar/static/tests/calendar.test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ test("Many2ManyAttendee: basic rendering", async () => {
5656
});
5757

5858
test("[Offline] Many2ManyAttendee: basic rendering", async () => {
59-
onRpc(
60-
"/web/dataset/call_kw/res.partner/get_attendee_detail",
61-
() => new Response("", { status: 502 }),
62-
{ pure: true }
63-
);
59+
onRpc("res.partner", "get_attendee_detail", () => new Response("", { status: 502 }));
6460
await mountView({
6561
type: "form",
6662
resModel: "calendar.event",

addons/web/static/tests/_framework/mock_server/mock_server.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,7 @@ export class MockServer {
541541
// Set default routes
542542
this._onRoute(["/web/action/load"], this.loadAction);
543543
this._onRoute(["/web/action/load_breadcrumbs"], this.loadActionBreadcrumbs);
544-
this._onRoute(["/web/bundle/<string:bundle_name>"], this.loadBundle, {
545-
pure: true,
546-
});
544+
this._onRoute(["/web/bundle/<string:bundle_name>"], this.loadBundle);
547545
this._onRoute(["/web/dataset/call_kw", "/web/dataset/call_kw/<path:path>"], this.callKw, {
548546
final: true,
549547
});
@@ -553,15 +551,9 @@ export class MockServer {
553551
{ final: true }
554552
);
555553
this._onRoute(["/web/dataset/resequence"], this.resequence);
556-
this._onRoute(["/web/image/<string:model>/<int:id>/<string:field>"], this.loadImage, {
557-
pure: true,
558-
});
559-
this._onRoute(["/web/webclient/load_menus"], this.loadMenus, {
560-
pure: true,
561-
});
562-
this._onRoute(["/web/webclient/translations"], this.loadTranslations, {
563-
pure: true,
564-
});
554+
this._onRoute(["/web/image/<string:model>/<int:id>/<string:field>"], this.loadImage);
555+
this._onRoute(["/web/webclient/load_menus"], this.loadMenus);
556+
this._onRoute(["/web/webclient/translations"], this.loadTranslations);
565557

566558
// Register ambiant parameters
567559
await this.configure(getCurrentParams());
@@ -794,7 +786,7 @@ export class MockServer {
794786
error = err instanceof Error ? err : new Error(err);
795787
}
796788
if (final || error || (result !== null && result !== undefined)) {
797-
if (pure) {
789+
if (pure || result instanceof Response) {
798790
jsonRpcParams = null;
799791
}
800792
break;

addons/web/static/tests/mock_server/mock_server.test.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,15 @@ defineModels([Partner, Bar, Foo]);
153153
* kwargs: Record<string, any>;
154154
* [key: string]: any;
155155
* }} params
156-
* @returns
157156
*/
158-
const ormRequest = async (params) => {
159-
const response = await fetch(`/web/dataset/call_kw/${params.model}/${params.method}`, {
157+
function fetchCallKw(params) {
158+
return fetch(`/web/dataset/call_kw/${params.model}/${params.method}`, {
160159
method: "POST",
161160
headers: {
162161
"Content-Type": "application/json",
163162
},
164163
body: JSON.stringify({
164+
id: nextJsonRpcId++,
165165
jsonrpc: "2.0",
166166
method: "call",
167167
params: {
@@ -171,6 +171,19 @@ const ormRequest = async (params) => {
171171
},
172172
}),
173173
});
174+
}
175+
176+
/**
177+
* @param {{
178+
* model: string;
179+
* method: string;
180+
* args: any[];
181+
* kwargs: Record<string, any>;
182+
* [key: string]: any;
183+
* }} params
184+
*/
185+
const ormRequest = async (params) => {
186+
const response = await fetchCallKw(params);
174187
const { error, result } = await response.json();
175188
if (error) {
176189
console.error(error);
@@ -188,6 +201,7 @@ const JSON_RPC_BASIC_PARAMS = {
188201
["Content-Type"]: "application/json",
189202
},
190203
};
204+
let nextJsonRpcId = 0;
191205

192206
describe.current.tags("headless");
193207

@@ -320,6 +334,13 @@ test("rpc: calls on mock server", async () => {
320334
);
321335
});
322336

337+
test("performRPC: custom response", async () => {
338+
const customResponse = new Response("{}", { status: 418 });
339+
onRpc(() => customResponse);
340+
await makeMockServer();
341+
await expect(fetchCallKw({})).resolves.toBe(customResponse);
342+
});
343+
323344
test("performRPC: search with active_test=false", async () => {
324345
await makeMockServer();
325346
const result = await ormRequest({

addons/web/static/tests/views/fields/badge_selection_field.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ test("BadgeSelectionField widget on a many2one in a new record", async () => {
7575
});
7676

7777
test("[Offline] BadgeSelectionField widget on a many2one", async () => {
78-
onRpc("/web/dataset/call_kw/product/name_search", () => new Response("", { status: 502 }), {
79-
pure: true,
80-
});
78+
onRpc("product", "name_search", () => new Response("", { status: 502 }));
8179
await mountView({
8280
resModel: "res.partner",
8381
resId: 2,

addons/web/static/tests/views/fields/many2many_checkboxes_field.test.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,7 @@ test("Many2ManyCheckBoxesField", async () => {
7676
});
7777

7878
test("[Offline] Many2ManyCheckBoxesField", async () => {
79-
onRpc(
80-
"/web/dataset/call_kw/partner.type/name_search",
81-
() => new Response("", { status: 502 }),
82-
{
83-
pure: true,
84-
}
85-
);
79+
onRpc("partner.type", "name_search", () => new Response("", { status: 502 }));
8680
Partner._records[0].timmy = [12];
8781
await mountView({
8882
type: "form",

addons/web/static/tests/views/fields/radio_field.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ test("radio field on a many2one in a new record", async () => {
7171

7272
test("[Offline] radio field on a many2one", async () => {
7373
Partner._records[0].product_id = 37;
74-
onRpc("/web/dataset/call_kw/product/web_search_read", () => new Response("", { status: 502 }), {
75-
pure: true,
76-
});
74+
onRpc("product", "web_search_read", () => new Response("", { status: 502 }));
7775
await mountView({
7876
type: "form",
7977
resModel: "partner",

addons/web/static/tests/views/fields/selection_field.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ test("SelectionField, edition and on many2one field", async () => {
114114

115115
test.tags("desktop");
116116
test("[Offline] SelectionField on many2one field", async () => {
117-
onRpc("/web/dataset/call_kw/product/name_search", () => new Response("", { status: 502 }), {
118-
pure: true,
119-
});
117+
onRpc("product", "name_search", () => new Response("", { status: 502 }));
120118
Partner._onChanges.product_id = () => {};
121119
Partner._records[0].product_id = 37;
122120
Partner._records[0].trululu = false;

addons/web/static/tests/views/fields/statusbar_field.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,7 @@ test("[Offline] static statusbar widget on many2one field", async () => {
137137
domain: "[('bar', '=', True)]",
138138
});
139139
Partner._records[1].bar = false;
140-
onRpc("/web/dataset/call_kw/partner/search_read", () => new Response("", { status: 502 }), {
141-
pure: true,
142-
});
140+
onRpc("partner", "search_read", () => new Response("", { status: 502 }));
143141

144142
await mountView({
145143
type: "form",

0 commit comments

Comments
 (0)