[FIX] web: prevent crash when giving wrong id in url

The mock server had to be modified in order to test this behavior
since it used to throw an error for invalid ids, while the actual
server just ignores it and continues forth with the other ids.
This commit is contained in:
David Monjoie
2017-06-19 07:56:42 +02:00
parent 584c60f1ba
commit b8c1571d94
4 changed files with 28 additions and 7 deletions
@@ -307,7 +307,7 @@ var ViewManager = Widget.extend(ControlPanelMixin, {
self.trigger('switch_mode', view_type, view_options);
}).fail(function(e) {
if (!(e && e.code === 200 && e.data.exception_type)) {
self.do_warn(_t("Error"), view.controller.display_name + _t(" view couldn't be loaded"));
self.do_warn(_t("Error"), view.label + _t(" view couldn't be loaded"));
}
// Restore internal state
self.active_view = old_view;
@@ -1399,7 +1399,8 @@ var BasicModel = AbstractModel.extend({
* @param {string} [optinos.viewType] the type of view for which the record
* is fetched (usefull to load the adequate fields), by defaults, uses
* record.viewType
* @returns {Deferred -> Object} resolves to the record
* @returns {Deferred -> Object} resolves to the record or is rejected in
* case no id given were valid ids
*/
_fetchRecord: function (record, options) {
var self = this;
@@ -1412,6 +1413,9 @@ var BasicModel = AbstractModel.extend({
context: _.extend({}, record.context, {bin_size: true}),
})
.then(function (result) {
if (result.length === 0) {
return $.Deferred().reject();
}
result = result[0];
record.data = _.extend({}, record.data, result);
})
@@ -537,11 +537,11 @@ var MockServer = Class.extend({
ids = [ids];
}
var fields = args[1] && args[1].length ? _.uniq(args[1].concat(['id'])) : Object.keys(this.data[model].fields);
var results = _.map(ids, function (id) {
var record = _.findWhere(self.data[model].records, {id: id});
if (!record) {
throw "mock read: id does not exist...";
}
var records = _.reduce(ids, function (records, id) {
var record = _.findWhere(self.data[model].records, {id: id});
return record ? records.concat(record) : records
}, []);
var results = _.map(records, function (record) {
var result = {};
for (var i = 0; i < fields.length; i++) {
var field = self.data[model].fields[fields[i]];
@@ -92,6 +92,23 @@ QUnit.module('Views', {
model.destroy();
});
QUnit.test('rejects loading a record with invalid id', function (assert) {
assert.expect(1);
this.params.res_id = 99;
var model = createModel({
Model: BasicModel,
data: this.data,
});
model.load(this.params).always(function (error) {
assert.strictEqual(this.state(), 'rejected',
"load should return a rejected deferred for an invalid id")
});
model.destroy();
});
QUnit.test('notify change with many2one', function (assert) {
assert.expect(2);