Commit 7e85fd8e authored by Romain Courteaud's avatar Romain Courteaud

ERP5: add post method

portal_type and parent_relative_url properties are required.
parent cbdf41ad
...@@ -142,6 +142,35 @@ ...@@ -142,6 +142,35 @@
}); });
}; };
ERP5Storage.prototype.post = function (data) {
var context = this,
new_id;
return getSiteDocument(this)
.push(function (site_hal) {
var form_data = new FormData();
form_data.append("portal_type", data.portal_type);
form_data.append("parent_relative_url", data.parent_relative_url);
return jIO.util.ajax({
type: "POST",
url: site_hal._actions.add.href,
data: form_data,
xhrFields: {
withCredentials: true
}
});
})
.push(function (evt) {
var location = evt.target.getResponseHeader("X-Location"),
uri = new URI(location);
new_id = uri.segment(2);
return context.put(new_id, data);
})
.push(function () {
return new_id;
});
};
ERP5Storage.prototype.put = function (id, data) { ERP5Storage.prototype.put = function (id, data) {
var context = this; var context = this;
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
domain = "https://example.org", domain = "https://example.org",
traverse_template = domain + "?mode=traverse{&relative_url,view}", traverse_template = domain + "?mode=traverse{&relative_url,view}",
search_template = domain + "?mode=search{&query,select_list*,limit*}", search_template = domain + "?mode=search{&query,select_list*,limit*}",
add_url = domain + "lets?add=somedocument",
root_hateoas = JSON.stringify({ root_hateoas = JSON.stringify({
"_links": { "_links": {
traverse: { traverse: {
...@@ -23,6 +24,11 @@ ...@@ -23,6 +24,11 @@
href: search_template, href: search_template,
templated: true templated: true
} }
},
"_actions": {
add: {
href: add_url
}
} }
}); });
...@@ -1259,4 +1265,177 @@ ...@@ -1259,4 +1265,177 @@
}); });
}); });
/////////////////////////////////////////////////////////////////
// erp5Storage.post
/////////////////////////////////////////////////////////////////
module("erp5Storage.post", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.spy = sinon.spy(FormData.prototype, "append");
this.jio = jIO.createJIO({
type: "erp5",
url: domain,
default_view_reference: "bar_view"
});
},
teardown: function () {
this.server.restore();
delete this.server;
this.spy.restore();
delete this.spy;
}
});
test("post ERP5 document", function () {
var id = "person_module/20150119_azerty",
context = this,
traverse_url = domain + "?mode=traverse&relative_url=" +
encodeURIComponent(id) + "&view=bar_view",
put_url = domain + "azertytrea?f=g",
document_hateoas = JSON.stringify({
// Kept property
"title": "foo",
// Remove all _ properties
"_bar": "john doo",
"_links": {
type: {
name: "Person"
}
},
"_embedded": {
"_view": {
form_id: {
key: "form_id",
"default": "Base_view"
},
my_title: {
key: "field_my_title",
"default": "foo",
editable: true,
type: "StringField"
},
my_id: {
key: "field_my_id",
"default": "",
editable: true,
type: "StringField"
},
my_title_non_editable: {
key: "field_my_title_non_editable",
"default": "foo",
editable: false,
type: "StringField"
},
my_start_date: {
key: "field_my_start_date",
"default": "foo",
editable: true,
type: "DateTimeField"
},
your_reference: {
key: "field_your_title",
"default": "bar",
editable: true,
type: "StringField"
},
sort_index: {
key: "field_sort_index",
"default": "foobar",
editable: true,
type: "StringField"
},
"_actions": {
put: {
href: put_url
}
}
}
}
}),
server = this.server;
this.server.respondWith("GET", domain, [200, {
"Content-Type": "application/hal+json"
}, root_hateoas]);
this.server.respondWith("GET", traverse_url, [200, {
"Content-Type": "application/hal+json"
}, document_hateoas]);
this.server.respondWith("POST", put_url, [204, {
"Content-Type": "text/html"
}, ""]);
this.server.respondWith("POST", add_url, [201, {
"Content-Type": "text/html",
"X-Location": "urn:jio:get:" + id
}, ""]);
stop();
expect(33);
this.jio.post({
title: "barè",
id: "foo",
portal_type: "Foo",
parent_relative_url: "foo_module"
})
.then(function (result) {
equal(result, id);
equal(server.requests.length, 5);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, true);
equal(server.requests[1].method, "POST");
equal(server.requests[1].url, add_url);
ok(server.requests[1].requestBody instanceof FormData);
equal(server.requests[1].withCredentials, true);
equal(server.requests[2].method, "GET");
equal(server.requests[2].url, domain);
equal(server.requests[2].requestBody, undefined);
equal(server.requests[2].withCredentials, true);
equal(server.requests[3].method, "GET");
equal(server.requests[3].url, traverse_url);
equal(server.requests[3].requestBody, undefined);
equal(server.requests[3].withCredentials, true);
equal(server.requests[4].method, "POST");
equal(server.requests[4].url, put_url);
ok(server.requests[4].requestBody instanceof FormData);
equal(server.requests[4].withCredentials, true);
equal(context.spy.callCount, 5, "FormData.append count");
equal(context.spy.firstCall.args[0], "portal_type",
"First append call");
equal(context.spy.firstCall.args[1], "Foo", "First append call");
equal(context.spy.secondCall.args[0], "parent_relative_url",
"Second append call");
equal(context.spy.secondCall.args[1], "foo_module",
"Second append call");
equal(context.spy.thirdCall.args[0], "form_id", "Third append call");
equal(context.spy.thirdCall.args[1], "Base_view", "Third append call");
equal(context.spy.getCall(3).args[0], "field_my_title",
"Fourthappend call");
equal(context.spy.getCall(3).args[1], "barè", "Fourth append call");
equal(context.spy.getCall(4).args[0], "field_my_id",
"Fifth append call");
equal(context.spy.getCall(4).args[1], "foo", "Fifth append call");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob, sinon, encodeURIComponent, FormData)); }(jIO, QUnit, Blob, sinon, encodeURIComponent, FormData));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment