Commit c9fc030f authored by Romain Courteaud's avatar Romain Courteaud

[erp5storage] add access token support

parent c865b62d
...@@ -31,15 +31,32 @@ ...@@ -31,15 +31,32 @@
SimpleQuery, ComplexQuery) { SimpleQuery, ComplexQuery) {
"use strict"; "use strict";
function ajax(storage, options) {
if (options === undefined) {
options = {};
}
if (options.xhrFields === undefined) {
options.xhrFields = {};
}
if (storage._access_token !== undefined) {
if (options.headers === undefined) {
options.headers = {};
}
options.headers['X-ACCESS-TOKEN'] = storage._access_token;
options.xhrFields.withCredentials = false;
} else {
options.xhrFields.withCredentials = true;
}
return jIO.util.ajax(options);
}
function getSiteDocument(storage) { function getSiteDocument(storage) {
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.ajax({ return ajax(storage, {
"type": "GET", "type": "GET",
"url": storage._url, "url": storage._url
"xhrFields": {
withCredentials: true
}
}); });
}) })
.push(function (event) { .push(function (event) {
...@@ -56,16 +73,13 @@ ...@@ -56,16 +73,13 @@
// XXX need to get modified metadata // XXX need to get modified metadata
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.ajax({ return ajax(storage, {
"type": "GET", "type": "GET",
"url": UriTemplate.parse(site_hal._links.traverse.href) "url": UriTemplate.parse(site_hal._links.traverse.href)
.expand({ .expand({
relative_url: id, relative_url: id,
view: options._view view: options._view
}), })
"xhrFields": {
withCredentials: true
}
}); });
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
...@@ -160,6 +174,9 @@ ...@@ -160,6 +174,9 @@
} }
this._url = spec.url; this._url = spec.url;
this._default_view_reference = spec.default_view_reference; this._default_view_reference = spec.default_view_reference;
if (spec.hasOwnProperty('access_token')) {
this._access_token = spec.access_token;
}
} }
function convertJSONToGet(json) { function convertJSONToGet(json) {
...@@ -192,13 +209,10 @@ ...@@ -192,13 +209,10 @@
var form_data = new FormData(); var form_data = new FormData();
form_data.append("portal_type", data.portal_type); form_data.append("portal_type", data.portal_type);
form_data.append("parent_relative_url", data.parent_relative_url); form_data.append("parent_relative_url", data.parent_relative_url);
return jIO.util.ajax({ return ajax(context, {
type: "POST", type: "POST",
url: site_hal._actions.add.href, url: site_hal._actions.add.href,
data: form_data, data: form_data
xhrFields: {
withCredentials: true
}
}); });
}) })
.push(function (evt) { .push(function (evt) {
...@@ -273,6 +287,7 @@ ...@@ -273,6 +287,7 @@
}; };
ERP5Storage.prototype.getAttachment = function (id, action, options) { ERP5Storage.prototype.getAttachment = function (id, action, options) {
var storage = this;
if (options === undefined) { if (options === undefined) {
options = {}; options = {};
} }
...@@ -317,10 +332,7 @@ ...@@ -317,10 +332,7 @@
request_options = { request_options = {
"type": "GET", "type": "GET",
"dataType": "blob", "dataType": "blob",
"url": action, "url": action
"xhrFields": {
withCredentials: true
}
}; };
if (options.start !== undefined || options.end !== undefined) { if (options.start !== undefined || options.end !== undefined) {
start = options.start || 0; start = options.start || 0;
...@@ -342,7 +354,7 @@ ...@@ -342,7 +354,7 @@
} }
request_options.headers = {Range: range}; request_options.headers = {Range: range};
} }
return jIO.util.ajax(request_options); return ajax(storage, request_options);
}) })
.push(function (evt) { .push(function (evt) {
if (evt.target.response === undefined) { if (evt.target.response === undefined) {
...@@ -359,6 +371,7 @@ ...@@ -359,6 +371,7 @@
}; };
ERP5Storage.prototype.putAttachment = function (id, name, blob) { ERP5Storage.prototype.putAttachment = function (id, name, blob) {
var storage = this;
// Assert we use a callable on a document from the ERP5 site // Assert we use a callable on a document from the ERP5 site
if (name.indexOf(this._url) !== 0) { if (name.indexOf(this._url) !== 0) {
throw new jIO.util.jIOError("Can not store outside ERP5: " + throw new jIO.util.jIOError("Can not store outside ERP5: " +
...@@ -394,14 +407,11 @@ ...@@ -394,14 +407,11 @@
} }
} }
} }
return jIO.util.ajax({ return ajax(storage, {
"type": "POST", "type": "POST",
"url": name, "url": name,
"data": data, "data": data,
"dataType": "blob", "dataType": "blob"
"xhrFields": {
withCredentials: true
}
}); });
}); });
}; };
...@@ -460,6 +470,7 @@ ...@@ -460,6 +470,7 @@
} }
ERP5Storage.prototype.buildQuery = function (options) { ERP5Storage.prototype.buildQuery = function (options) {
var storage = this;
// if (typeof options.query !== "string") { // if (typeof options.query !== "string") {
// options.query = (options.query ? // options.query = (options.query ?
// jIO.Query.objectToSearchText(options.query) : // jIO.Query.objectToSearchText(options.query) :
...@@ -552,7 +563,7 @@ ...@@ -552,7 +563,7 @@
selection_domain = JSON.stringify(selection_domain); selection_domain = JSON.stringify(selection_domain);
} }
return jIO.util.ajax({ return ajax(storage, {
"type": "GET", "type": "GET",
"url": UriTemplate.parse(site_hal._links.raw_search.href) "url": UriTemplate.parse(site_hal._links.raw_search.href)
.expand({ .expand({
...@@ -563,10 +574,7 @@ ...@@ -563,10 +574,7 @@
sort_on: sort_list, sort_on: sort_list,
local_roles: local_roles, local_roles: local_roles,
selection_domain: selection_domain selection_domain: selection_domain
}), })
"xhrFields": {
withCredentials: true
}
}); });
}) })
.push(function (response) { .push(function (response) {
......
...@@ -67,6 +67,21 @@ ...@@ -67,6 +67,21 @@
equal(jio.__type, "erp5"); equal(jio.__type, "erp5");
deepEqual(jio.__storage._url, domain); deepEqual(jio.__storage._url, domain);
deepEqual(jio.__storage._default_view_reference, "bar_view"); deepEqual(jio.__storage._default_view_reference, "bar_view");
equal(jio.__storage._access_token, undefined);
});
test("Storage store access_token", function () {
var jio = jIO.createJIO({
type: "erp5",
url: domain,
default_view_reference: "bar_view",
access_token: 'foo'
});
equal(jio.__type, "erp5");
deepEqual(jio.__storage._url, domain);
deepEqual(jio.__storage._default_view_reference, "bar_view");
equal(jio.__storage._access_token, 'foo');
}); });
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -120,6 +135,75 @@ ...@@ -120,6 +135,75 @@
}); });
}); });
test("get ERP5 document with access token", function () {
var id = "person_module/20150119_azerty",
traverse_url = domain + "?mode=traverse&relative_url=" +
encodeURIComponent(id) + "&view=bar_view",
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"
}
}
}
}),
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]);
stop();
expect(12);
this.jio = jIO.createJIO({
type: "erp5",
url: domain,
default_view_reference: "bar_view",
access_token: 'footoken'
});
this.jio.get(id)
.then(function (result) {
deepEqual(result, {
portal_type: "Person"
}, "Check document");
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, false);
deepEqual(server.requests[0].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken'});
equal(server.requests[1].method, "GET");
equal(server.requests[1].url, traverse_url);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, false);
deepEqual(server.requests[1].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken'});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get ERP5 document with empty form", function () { test("get ERP5 document with empty form", function () {
var id = "person_module/20150119_azerty", var id = "person_module/20150119_azerty",
traverse_url = domain + "?mode=traverse&relative_url=" + traverse_url = domain + "?mode=traverse&relative_url=" +
...@@ -153,7 +237,7 @@ ...@@ -153,7 +237,7 @@
}, document_hateoas]); }, document_hateoas]);
stop(); stop();
expect(10); expect(12);
this.jio.get(id) this.jio.get(id)
.then(function (result) { .then(function (result) {
...@@ -165,10 +249,12 @@ ...@@ -165,10 +249,12 @@
equal(server.requests[0].url, domain); equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined); equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, true); equal(server.requests[0].withCredentials, true);
deepEqual(server.requests[0].requestHeaders, {});
equal(server.requests[1].method, "GET"); equal(server.requests[1].method, "GET");
equal(server.requests[1].url, traverse_url); equal(server.requests[1].url, traverse_url);
equal(server.requests[1].requestBody, undefined); equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, true); equal(server.requests[1].withCredentials, true);
deepEqual(server.requests[1].requestHeaders, {});
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -335,6 +421,66 @@ ...@@ -335,6 +421,66 @@
}); });
}); });
test("allAttachments ERP5 document with access token", function () {
var id = "person_module/20150119_azerty",
traverse_url = domain + "?mode=traverse&relative_url=" +
encodeURIComponent(id),
document_hateoas = JSON.stringify({
// Kept property
"title": "foo",
// Remove all _ properties
"_bar": "john doo",
"_links": {
type: {
name: "Person"
}
}
}),
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]);
stop();
expect(12);
this.jio = jIO.createJIO({
type: "erp5",
url: domain,
access_token: 'footoken'
});
this.jio.allAttachments(id)
.then(function (result) {
deepEqual(result, {
links: {}
}, "Check document");
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, false);
deepEqual(server.requests[0].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken'});
equal(server.requests[1].method, "GET");
equal(server.requests[1].url, traverse_url);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, false);
deepEqual(server.requests[1].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken'});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("allAttachments ERP5 document", function () { test("allAttachments ERP5 document", function () {
var id = "person_module/20150119_azerty", var id = "person_module/20150119_azerty",
traverse_url = domain + "?mode=traverse&relative_url=" + traverse_url = domain + "?mode=traverse&relative_url=" +
...@@ -360,7 +506,7 @@ ...@@ -360,7 +506,7 @@
}, document_hateoas]); }, document_hateoas]);
stop(); stop();
expect(10); expect(12);
this.jio.allAttachments(id) this.jio.allAttachments(id)
.then(function (result) { .then(function (result) {
...@@ -372,10 +518,12 @@ ...@@ -372,10 +518,12 @@
equal(server.requests[0].url, domain); equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined); equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, true); equal(server.requests[0].withCredentials, true);
deepEqual(server.requests[0].requestHeaders, {});
equal(server.requests[1].method, "GET"); equal(server.requests[1].method, "GET");
equal(server.requests[1].url, traverse_url); equal(server.requests[1].url, traverse_url);
equal(server.requests[1].requestBody, undefined); equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, true); equal(server.requests[1].withCredentials, true);
deepEqual(server.requests[1].requestHeaders, {});
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -489,6 +637,65 @@ ...@@ -489,6 +637,65 @@
}); });
}); });
test("putAttachment submit ERP5 form with access token", function () {
var submit_url = domain + "/Form_view/Base_edit",
id = "fake",
form_json = {
"my_title": "fooé",
"your_reference": "barè"
// XXX Check FileUpload
},
context = this,
server = this.server;
this.server.respondWith("POST", submit_url, [204, {
"Content-Type": "text/xml"
}, ""]);
stop();
expect(13);
this.jio = jIO.createJIO({
type: "erp5",
url: domain,
access_token: 'footoken'
});
this.jio.putAttachment(
id,
submit_url,
new Blob([JSON.stringify(form_json)])
)
.then(function () {
equal(server.requests.length, 1);
equal(server.requests[0].method, "POST");
equal(server.requests[0].url, submit_url);
equal(server.requests[0].status, 204);
equal(server.requests[0].responseType, "blob");
ok(server.requests[0].requestBody instanceof FormData);
ok(context.spy.calledTwice, "FormData.append count " +
context.spy.callCount);
equal(context.spy.firstCall.args[0], "my_title", "First append call");
equal(context.spy.firstCall.args[1], "fooé", "First append call");
equal(context.spy.secondCall.args[0], "your_reference",
"Second append call");
equal(context.spy.secondCall.args[1], "barè", "Second append call");
equal(server.requests[0].withCredentials, false);
deepEqual(server.requests[0].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken',
'Content-Type': 'text/plain;charset=utf-8'});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("putAttachment submit ERP5 form", function () { test("putAttachment submit ERP5 form", function () {
var submit_url = domain + "/Form_view/Base_edit", var submit_url = domain + "/Form_view/Base_edit",
id = "fake", id = "fake",
...@@ -505,7 +712,7 @@ ...@@ -505,7 +712,7 @@
}, ""]); }, ""]);
stop(); stop();
expect(12); expect(13);
this.jio.putAttachment( this.jio.putAttachment(
id, id,
...@@ -529,6 +736,8 @@ ...@@ -529,6 +736,8 @@
equal(context.spy.secondCall.args[1], "barè", "Second append call"); equal(context.spy.secondCall.args[1], "barè", "Second append call");
equal(server.requests[0].withCredentials, true); equal(server.requests[0].withCredentials, true);
deepEqual(server.requests[0].requestHeaders,
{'Content-Type': 'text/plain;charset=utf-8'});
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -712,6 +921,72 @@ ...@@ -712,6 +921,72 @@
}); });
}); });
test("getAttachment: view uses default form with access token", function () {
var id = "person_module/1",
traverse_url = domain + "?mode=traverse&relative_url=" +
encodeURIComponent(id) + "&view=foo_view",
document_hateoas = JSON.stringify({
"title": "foo",
"_bar": "john doo",
"_embedded": "youhou",
"_links": {
type: {
name: "Person"
}
}
}),
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]);
stop();
expect(14);
this.jio = jIO.createJIO({
type: "erp5",
url: domain,
default_view_reference: "foo_view",
access_token: 'footoken'
});
this.jio.getAttachment(id, "view")
.then(function (result) {
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, false);
deepEqual(server.requests[0].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken'});
equal(server.requests[1].method, "GET");
equal(server.requests[1].url, traverse_url);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, false);
deepEqual(server.requests[1].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken'});
ok(result instanceof Blob, "Data is Blob");
deepEqual(result.type, "application/hal+json", "Check mimetype");
return jIO.util.readBlobAsText(result);
})
.then(function (result) {
var expected = JSON.parse(document_hateoas);
deepEqual(JSON.parse(result.target.result), expected,
"Attachment correctly fetched");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("getAttachment: view uses default form", function () { test("getAttachment: view uses default form", function () {
var id = "person_module/1", var id = "person_module/1",
traverse_url = domain + "?mode=traverse&relative_url=" + traverse_url = domain + "?mode=traverse&relative_url=" +
...@@ -736,7 +1011,7 @@ ...@@ -736,7 +1011,7 @@
}, document_hateoas]); }, document_hateoas]);
stop(); stop();
expect(12); expect(14);
this.jio.getAttachment(id, "view") this.jio.getAttachment(id, "view")
.then(function (result) { .then(function (result) {
...@@ -745,10 +1020,12 @@ ...@@ -745,10 +1020,12 @@
equal(server.requests[0].url, domain); equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined); equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, true); equal(server.requests[0].withCredentials, true);
deepEqual(server.requests[0].requestHeaders, {});
equal(server.requests[1].method, "GET"); equal(server.requests[1].method, "GET");
equal(server.requests[1].url, traverse_url); equal(server.requests[1].url, traverse_url);
equal(server.requests[1].requestBody, undefined); equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, true); equal(server.requests[1].withCredentials, true);
deepEqual(server.requests[1].requestHeaders, {});
ok(result instanceof Blob, "Data is Blob"); ok(result instanceof Blob, "Data is Blob");
deepEqual(result.type, "application/hal+json", "Check mimetype"); deepEqual(result.type, "application/hal+json", "Check mimetype");
...@@ -1035,7 +1312,8 @@ ...@@ -1035,7 +1312,8 @@
} }
}); });
test("get all documents", function () {
test("get all documents with access token", function () {
var search_url = domain + "?mode=search&select_list=title" + var search_url = domain + "?mode=search&select_list=title" +
"&select_list=reference", "&select_list=reference",
search_hateoas = JSON.stringify({ search_hateoas = JSON.stringify({
...@@ -1072,7 +1350,13 @@ ...@@ -1072,7 +1350,13 @@
}, search_hateoas]); }, search_hateoas]);
stop(); stop();
expect(10); expect(12);
this.jio = jIO.createJIO({
type: "erp5",
url: domain,
access_token: 'footoken'
});
this.jio.allDocs() this.jio.allDocs()
.then(function (result) { .then(function (result) {
...@@ -1097,11 +1381,15 @@ ...@@ -1097,11 +1381,15 @@
equal(server.requests[0].method, "GET"); equal(server.requests[0].method, "GET");
equal(server.requests[0].url, domain); equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined); equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, true); equal(server.requests[0].withCredentials, false);
deepEqual(server.requests[0].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken'});
equal(server.requests[1].method, "GET"); equal(server.requests[1].method, "GET");
equal(server.requests[1].url, search_url); equal(server.requests[1].url, search_url);
equal(server.requests[1].requestBody, undefined); equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, true); equal(server.requests[1].withCredentials, false);
deepEqual(server.requests[1].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken'});
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -1111,13 +1399,91 @@ ...@@ -1111,13 +1399,91 @@
}); });
}); });
test("filter documents", function () { test("get all documents", function () {
var search_url = domain + "?mode=search&query=title%3A%20%22two%22&" + var search_url = domain + "?mode=search&select_list=title" +
"select_list=destination&select_list=source&limit=5&" + "&select_list=reference",
"sort_on=%5B%22title%22%2C%22descending%22%5D&" + search_hateoas = JSON.stringify({
"sort_on=%5B%22id%22%2C%22descending%22%5D",
search_hateoas = JSON.stringify({ "_embedded": {
"contents": [
{
"_links": {
"self": {
"href": "urn:jio:get:person_module/2"
}
},
"reference": "foo2",
"title": "bar2"
},
{
"_links": {
"self": {
"href": "urn:jio:get:organisation_module/3"
}
},
"title": "bar3"
}
]
}
}),
server = this.server;
this.server.respondWith("GET", domain, [200, {
"Content-Type": "application/hal+json"
}, root_hateoas]);
this.server.respondWith("GET", search_url, [200, {
"Content-Type": "application/hal+json"
}, search_hateoas]);
stop();
expect(12);
this.jio.allDocs()
.then(function (result) {
deepEqual(result, {
data: {
rows: [{
id: "person_module/2",
value: {
reference: "foo2",
title: "bar2"
}
}, {
id: "organisation_module/3",
value: {
title: "bar3"
}
}],
total_rows: 2
}
}, "Check document");
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, true);
deepEqual(server.requests[0].requestHeaders, {});
equal(server.requests[1].method, "GET");
equal(server.requests[1].url, search_url);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, true);
deepEqual(server.requests[1].requestHeaders, {});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("filter documents", function () {
var search_url = domain + "?mode=search&query=title%3A%20%22two%22&" +
"select_list=destination&select_list=source&limit=5&" +
"sort_on=%5B%22title%22%2C%22descending%22%5D&" +
"sort_on=%5B%22id%22%2C%22descending%22%5D",
search_hateoas = JSON.stringify({
"_embedded": { "_embedded": {
"contents": [ "contents": [
{ {
...@@ -1723,6 +2089,145 @@ ...@@ -1723,6 +2089,145 @@
}); });
}); });
test("put ERP5 document with access token", 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_reference",
"default": "bar",
editable: true,
type: "StringField"
},
your_reference_non_editable: {
key: "field_your_reference_non_editable",
"default": "bar",
editable: false,
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"
}, ""]);
stop();
expect(26);
this.jio = jIO.createJIO({
type: "erp5",
url: domain,
default_view_reference: "bar_view",
access_token: 'footoken'
});
this.jio.put(id, {title: "barè", id: "foo", reference: "bar2"})
.then(function (result) {
equal(result, id);
equal(server.requests.length, 3);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, false);
deepEqual(server.requests[0].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken'});
equal(server.requests[1].method, "GET");
equal(server.requests[1].url, traverse_url);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, false);
deepEqual(server.requests[1].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken'});
equal(server.requests[2].method, "POST");
equal(server.requests[2].url, put_url);
ok(server.requests[2].requestBody instanceof FormData);
equal(server.requests[2].withCredentials, false);
deepEqual(server.requests[2].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken',
'Content-Type': 'text/plain;charset=utf-8'});
equal(context.spy.callCount, 4, "FormData.append count");
equal(context.spy.firstCall.args[0], "form_id", "First append call");
equal(context.spy.firstCall.args[1], "Base_view", "First append call");
equal(context.spy.secondCall.args[0], "field_my_title",
"Second append call");
equal(context.spy.secondCall.args[1], "barè", "Second append call");
equal(context.spy.thirdCall.args[0], "field_my_id",
"Third append call");
equal(context.spy.thirdCall.args[1], "foo", "Third append call");
equal(context.spy.getCall(3).args[0], "field_your_reference",
"Fourth append call");
equal(context.spy.getCall(3).args[1], "bar2", "Fourth append call");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("put ERP5 document", function () { test("put ERP5 document", function () {
var id = "person_module/20150119_azerty", var id = "person_module/20150119_azerty",
context = this, context = this,
...@@ -1808,7 +2313,7 @@ ...@@ -1808,7 +2313,7 @@
}, ""]); }, ""]);
stop(); stop();
expect(23); expect(26);
this.jio.put(id, {title: "barè", id: "foo", reference: "bar2"}) this.jio.put(id, {title: "barè", id: "foo", reference: "bar2"})
.then(function (result) { .then(function (result) {
...@@ -1818,14 +2323,18 @@ ...@@ -1818,14 +2323,18 @@
equal(server.requests[0].url, domain); equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined); equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, true); equal(server.requests[0].withCredentials, true);
deepEqual(server.requests[0].requestHeaders, {});
equal(server.requests[1].method, "GET"); equal(server.requests[1].method, "GET");
equal(server.requests[1].url, traverse_url); equal(server.requests[1].url, traverse_url);
equal(server.requests[1].requestBody, undefined); equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, true); equal(server.requests[1].withCredentials, true);
deepEqual(server.requests[1].requestHeaders, {});
equal(server.requests[2].method, "POST"); equal(server.requests[2].method, "POST");
equal(server.requests[2].url, put_url); equal(server.requests[2].url, put_url);
ok(server.requests[2].requestBody instanceof FormData); ok(server.requests[2].requestBody instanceof FormData);
equal(server.requests[2].withCredentials, true); equal(server.requests[2].withCredentials, true);
deepEqual(server.requests[2].requestHeaders,
{'Content-Type': 'text/plain;charset=utf-8'});
equal(context.spy.callCount, 4, "FormData.append count"); equal(context.spy.callCount, 4, "FormData.append count");
equal(context.spy.firstCall.args[0], "form_id", "First append call"); equal(context.spy.firstCall.args[0], "form_id", "First append call");
...@@ -2042,6 +2551,176 @@ ...@@ -2042,6 +2551,176 @@
} }
}); });
test("post ERP5 document with access token", 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_reference",
"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(40);
this.jio = jIO.createJIO({
type: "erp5",
url: domain,
default_view_reference: "bar_view",
access_token: 'footoken'
});
this.jio.post({
title: "barè",
id: "foo",
portal_type: "Foo",
parent_relative_url: "foo_module",
reference: "bar2"
})
.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, false);
deepEqual(server.requests[0].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken'});
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, false);
deepEqual(server.requests[1].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken',
'Content-Type': 'text/plain;charset=utf-8'});
equal(server.requests[2].method, "GET");
equal(server.requests[2].url, domain);
equal(server.requests[2].requestBody, undefined);
equal(server.requests[2].withCredentials, false);
deepEqual(server.requests[2].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken'});
equal(server.requests[3].method, "GET");
equal(server.requests[3].url, traverse_url);
equal(server.requests[3].requestBody, undefined);
equal(server.requests[3].withCredentials, false);
deepEqual(server.requests[3].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken'});
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, false);
deepEqual(server.requests[4].requestHeaders,
{'X-ACCESS-TOKEN': 'footoken',
'Content-Type': 'text/plain;charset=utf-8'});
equal(context.spy.callCount, 6, "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");
equal(context.spy.getCall(5).args[0], "field_your_reference",
"Sixth append call");
equal(context.spy.getCall(5).args[1], "bar2", "Sixth append call");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("post ERP5 document", function () { test("post ERP5 document", function () {
var id = "person_module/20150119_azerty", var id = "person_module/20150119_azerty",
context = this, context = this,
...@@ -2125,7 +2804,7 @@ ...@@ -2125,7 +2804,7 @@
}, ""]); }, ""]);
stop(); stop();
expect(35); expect(40);
this.jio.post({ this.jio.post({
title: "barè", title: "barè",
...@@ -2142,26 +2821,33 @@ ...@@ -2142,26 +2821,33 @@
equal(server.requests[0].url, domain); equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined); equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, true); equal(server.requests[0].withCredentials, true);
deepEqual(server.requests[0].requestHeaders, {});
equal(server.requests[1].method, "POST"); equal(server.requests[1].method, "POST");
equal(server.requests[1].url, add_url); equal(server.requests[1].url, add_url);
ok(server.requests[1].requestBody instanceof FormData); ok(server.requests[1].requestBody instanceof FormData);
equal(server.requests[1].withCredentials, true); equal(server.requests[1].withCredentials, true);
deepEqual(server.requests[1].requestHeaders,
{'Content-Type': 'text/plain;charset=utf-8'});
equal(server.requests[2].method, "GET"); equal(server.requests[2].method, "GET");
equal(server.requests[2].url, domain); equal(server.requests[2].url, domain);
equal(server.requests[2].requestBody, undefined); equal(server.requests[2].requestBody, undefined);
equal(server.requests[2].withCredentials, true); equal(server.requests[2].withCredentials, true);
deepEqual(server.requests[2].requestHeaders, {});
equal(server.requests[3].method, "GET"); equal(server.requests[3].method, "GET");
equal(server.requests[3].url, traverse_url); equal(server.requests[3].url, traverse_url);
equal(server.requests[3].requestBody, undefined); equal(server.requests[3].requestBody, undefined);
equal(server.requests[3].withCredentials, true); equal(server.requests[3].withCredentials, true);
deepEqual(server.requests[3].requestHeaders, {});
equal(server.requests[4].method, "POST"); equal(server.requests[4].method, "POST");
equal(server.requests[4].url, put_url); equal(server.requests[4].url, put_url);
ok(server.requests[4].requestBody instanceof FormData); ok(server.requests[4].requestBody instanceof FormData);
equal(server.requests[4].withCredentials, true); equal(server.requests[4].withCredentials, true);
deepEqual(server.requests[4].requestHeaders,
{'Content-Type': 'text/plain;charset=utf-8'});
equal(context.spy.callCount, 6, "FormData.append count"); equal(context.spy.callCount, 6, "FormData.append count");
......
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