Commit aefd3402 authored by Aurélien Vermylen's avatar Aurélien Vermylen

Stash to fix mappingstorage tests.

parent 6caa5ab4
...@@ -21,57 +21,87 @@ ...@@ -21,57 +21,87 @@
'/user/{id}/', '/user/{user_id}/device/', '/user/{id}/', '/user/{user_id}/device/',
'/user/{user_id}/device/{device_id}/', '/vehicle/{id}/', '/vehicle/', '/user/{user_id}/device/{device_id}/', '/vehicle/{id}/', '/vehicle/',
'/vehicle/{vehicle_id}/mil/'],*/ '/vehicle/{vehicle_id}/mil/'],*/
AUTOMATIC_VALID_ENDPOINT_REGEXES = [/^\/trip(\/T_\w*|)\/$/, AUTOMATIC_VALID_ENDPOINT_REGEXES = [/^\/(U_\w*|all)\/trip(\/T_\w*|)\/$/,
/^\/user\/me(\/device(\/\w*|)|)\/$/, /^\/(U_\w*|all)\/user\/me(\/device(\/\w*|)|)\/$/,
/^\/vehicle(\/C_\w*(\/mil|)|)\/$/], /^\/(U_\w*|all)\/vehicle(\/C_\w*(\/mil|)|)\/$/],
AUTOMATIC_VALID_ENDPOINTID_REGEXES = [/^\/trip\/T_\w*\/$/, AUTOMATIC_VALID_ENDPOINTID_REGEXES = [/^\/(U_\w*|all)\/trip\/T_\w*\/$/,
/^\/user\/me\/device\/\w*\/$/, /^\/(U_\w*|all)\/user\/me\/device\/\w*\/$/,
/^\/vehicle\/C_\w*\/$/], /^\/(U_\w*|all)\/vehicle\/C_\w*\/$/],
automatic_template = UriTemplate.parse(AUTOMATIC_BASE_URI + '{endpoint}'); automatic_template = UriTemplate.parse(AUTOMATIC_BASE_URI + '{endpoint}');
// Check validity of endpoint.
function checkEndpoint(endpoint) {
var regex;
for (regex in AUTOMATIC_VALID_ENDPOINT_REGEXES) {
if (AUTOMATIC_VALID_ENDPOINT_REGEXES.hasOwnProperty(regex)
&& regex.test(endpoint)) {
return true;
}
}
return false;
}
// Check that ids match a valid Automatic API id
function checkEndpointAsId(endpoint) {
var regex;
for (regex in AUTOMATIC_VALID_ENDPOINTID_REGEXES) {
if (AUTOMATIC_VALID_ENDPOINTID_REGEXES.hasOwnProperty(regex)
&& regex.test(endpoint)) {
return true;
}
}
return false;
}
function _queryAutomaticAPI(endpoint, filters, jio) { function _queryAutomaticAPI(endpoint, filters, jio) {
var result = [], var result = [],
type, type,
promises, promises,
user_dict = {}, user_dict = {},
temp; temp,
// Check validity of endpoint. i;
function checkEndpoint(endpoint) {
var regex;
for (regex in AUTOMATIC_VALID_ENDPOINT_REGEXES) {
if (AUTOMATIC_VALID_ENDPOINT_REGEXES.hasOwnProperty(regex)
&& regex.test(endpoint)) {
return true;
}
}
return false;
}
if (!checkEndpoint(endpoint)) { if (!checkEndpoint(endpoint)) {
throw new jIO.util.jIOError('Wrong Automatic API query. (usually ' + throw new jIO.util.jIOError('Wrong Automatic API query. (usually ' +
'caused by wrong "id" or "type")', 400); 'caused by wrong "id" or "type")', 400);
} }
type = endpoint.split('/')[1]; type = endpoint.split('/')[2];
// Promise chain to handle multi-part response ("_metadata"->"next" parts). // Promise chain to handle multi-part response ("_metadata"->"next" parts).
function treatNext(returned) { function treatNext(returned) {
var path, var path,
data, data,
user_id; user_id,
next;
data = returned[0]; data = returned[0];
user_id = user_dict[returned[1]]; user_id = user_dict[returned[1]];
if (data._metadata === undefined) { if (data.error === 'err_unauthorized') {
throw new jIO.util.jIOError('Malformed Automatic API result.', 500); throw new jIO.util.jIOError('Invalid token provided, Unauthorized.',
401);
} }
path = URI(data.results.url).path(); if (!checkEndpointAsId(endpoint)) {
temp = {'path': path, if (data._metadata === undefined) {
'type': type, throw new jIO.util.jIOError('Malformed Automatic API result.', 500);
'started_at': data.results.started_at || null, }
'ended_at': data.results.ended_at || null, next = data._metadata.next;
'user': user_id}; data = data.results;
result.push(temp); }
jio._cache.put('/' + user_id + path, temp); for (i = 0; i < data.length; i += 1) {
jio._cache.putAttachment('/', '', JSON.stringify(data.results)); path = URI(data[i].url).path();
if (data._metadata.next === undefined) { temp = {
return result; 'path': path,
'reference': user_id + path,
'type': type,
'started_at': data[i].started_at || null,
'ended_at': data[i].ended_at || null,
'user': user_id
};
result.push(temp);
jio._cache.put('/' + user_id + path, temp);
jio._cache.putAttachment('/' + user_id + path, 'data',
new Blob([JSON.stringify(data[i].results)], {type:
'application/text'}));
}
if (next === undefined) {
return true;
} }
return jIO.util.ajax({ return jIO.util.ajax({
'type': 'GET', 'type': 'GET',
...@@ -99,18 +129,24 @@ ...@@ -99,18 +129,24 @@
'xhrFields': {withCredentials: true} 'xhrFields': {withCredentials: true}
}).push(function (usr) { }).push(function (usr) {
user_dict[token] = JSON.parse(usr).id; user_dict[token] = JSON.parse(usr).id;
return jIO.util.ajax({ temp = endpoint.split('/')[1];
'type': 'GET', if (temp === 'all' || temp === user_dict[token]) {
'url': automatic_template.search(filters).expand(endpoint), return jIO.util.ajax({
'headers': {'Authorization': 'Bearer ' + token}, 'type': 'GET',
'xhrFields': {withCredentials: true} 'url': automatic_template.search(filters).expand(endpoint),
}); 'headers': {'Authorization': 'Bearer ' + token},
'xhrFields': {withCredentials: true}
});
}
throw new jIO.util.jIOError('Ignored.', 200);
}).push(function (response) { }).push(function (response) {
return [JSON.parse(response.target.responseText), token]; return [JSON.parse(response.target.responseText), token];
}).push(treatNext); }).push(treatNext);
}); });
}); });
return RSVP.all(promises); return RSVP.all(promises).push(function () {
return result;
});
} }
/** /**
* The Automatic API Storage extension * The Automatic API Storage extension
...@@ -126,7 +162,7 @@ ...@@ -126,7 +162,7 @@
for (i = 0; i < spec.access_tokens.length; i += 1) { for (i = 0; i < spec.access_tokens.length; i += 1) {
if (typeof spec.access_tokens[i] !== 'string' || !spec.access_tokens[i]) { if (typeof spec.access_tokens[i] !== 'string' || !spec.access_tokens[i]) {
throw new jIO.util.jIOError('Access Tokens must be' + throw new jIO.util.jIOError('Access Tokens must be' +
'an array of non-empty strings.', 400); ' an array of non-empty strings.', 400);
} }
} }
...@@ -135,15 +171,13 @@ ...@@ -135,15 +171,13 @@
} }
AutomaticAPIStorage.prototype.get = function (id) { AutomaticAPIStorage.prototype.get = function (id) {
function checkEndpointAsId(endpoint) { if (id.indexOf('/') !== 0) {
var regex; throw new jIO.util.jIOError('id ' + id +
for (regex in AUTOMATIC_VALID_ENDPOINTID_REGEXES) { ' is forbidden (not starting with /)', 400);
if (AUTOMATIC_VALID_ENDPOINTID_REGEXES.hasOwnProperty(regex) }
&& regex.test(endpoint)) { if (id.lastIndexOf("/") !== (id.length - 1)) {
return true; throw new jIO.util.jIOError('id ' + id +
} ' is forbidden (not ending with /)', 400);
}
return false;
} }
if (!checkEndpointAsId(id)) { if (!checkEndpointAsId(id)) {
throw new jIO.util.jIOError('Invalid id.', 400); throw new jIO.util.jIOError('Invalid id.', 400);
...@@ -151,7 +185,14 @@ ...@@ -151,7 +185,14 @@
return this._cache.get(id).push(function (res) { return this._cache.get(id).push(function (res) {
return res; return res;
}, function () { }, function () {
_queryAutomaticAPI(id, {}, this); return _queryAutomaticAPI(id, {}, this).push(function (res) {
if (typeof res === 'object' && res.length > 0) {
return res[0];
}
throw new jIO.util.jIOError('Cannot find document: ' + id, 404);
}, function () {
throw new jIO.util.jIOError('Cannot find document: ' + id, 404);
});
}); });
}; };
......
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