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

Stash to fix mappingstorage tests.

parent 6caa5ab4
......@@ -21,20 +21,14 @@
'/user/{id}/', '/user/{user_id}/device/',
'/user/{user_id}/device/{device_id}/', '/vehicle/{id}/', '/vehicle/',
'/vehicle/{vehicle_id}/mil/'],*/
AUTOMATIC_VALID_ENDPOINT_REGEXES = [/^\/trip(\/T_\w*|)\/$/,
/^\/user\/me(\/device(\/\w*|)|)\/$/,
/^\/vehicle(\/C_\w*(\/mil|)|)\/$/],
AUTOMATIC_VALID_ENDPOINTID_REGEXES = [/^\/trip\/T_\w*\/$/,
/^\/user\/me\/device\/\w*\/$/,
/^\/vehicle\/C_\w*\/$/],
AUTOMATIC_VALID_ENDPOINT_REGEXES = [/^\/(U_\w*|all)\/trip(\/T_\w*|)\/$/,
/^\/(U_\w*|all)\/user\/me(\/device(\/\w*|)|)\/$/,
/^\/(U_\w*|all)\/vehicle(\/C_\w*(\/mil|)|)\/$/],
AUTOMATIC_VALID_ENDPOINTID_REGEXES = [/^\/(U_\w*|all)\/trip\/T_\w*\/$/,
/^\/(U_\w*|all)\/user\/me\/device\/\w*\/$/,
/^\/(U_\w*|all)\/vehicle\/C_\w*\/$/],
automatic_template = UriTemplate.parse(AUTOMATIC_BASE_URI + '{endpoint}');
function _queryAutomaticAPI(endpoint, filters, jio) {
var result = [],
type,
promises,
user_dict = {},
temp;
// Check validity of endpoint.
function checkEndpoint(endpoint) {
var regex;
......@@ -46,32 +40,68 @@
}
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) {
var result = [],
type,
promises,
user_dict = {},
temp,
i;
if (!checkEndpoint(endpoint)) {
throw new jIO.util.jIOError('Wrong Automatic API query. (usually ' +
'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).
function treatNext(returned) {
var path,
data,
user_id;
user_id,
next;
data = returned[0];
user_id = user_dict[returned[1]];
if (data.error === 'err_unauthorized') {
throw new jIO.util.jIOError('Invalid token provided, Unauthorized.',
401);
}
if (!checkEndpointAsId(endpoint)) {
if (data._metadata === undefined) {
throw new jIO.util.jIOError('Malformed Automatic API result.', 500);
}
path = URI(data.results.url).path();
temp = {'path': path,
next = data._metadata.next;
data = data.results;
}
for (i = 0; i < data.length; i += 1) {
path = URI(data[i].url).path();
temp = {
'path': path,
'reference': user_id + path,
'type': type,
'started_at': data.results.started_at || null,
'ended_at': data.results.ended_at || null,
'user': user_id};
'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('/', '', JSON.stringify(data.results));
if (data._metadata.next === undefined) {
return result;
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({
'type': 'GET',
......@@ -99,18 +129,24 @@
'xhrFields': {withCredentials: true}
}).push(function (usr) {
user_dict[token] = JSON.parse(usr).id;
temp = endpoint.split('/')[1];
if (temp === 'all' || temp === user_dict[token]) {
return jIO.util.ajax({
'type': 'GET',
'url': automatic_template.search(filters).expand(endpoint),
'headers': {'Authorization': 'Bearer ' + token},
'xhrFields': {withCredentials: true}
});
}
throw new jIO.util.jIOError('Ignored.', 200);
}).push(function (response) {
return [JSON.parse(response.target.responseText), token];
}).push(treatNext);
});
});
return RSVP.all(promises);
return RSVP.all(promises).push(function () {
return result;
});
}
/**
* The Automatic API Storage extension
......@@ -126,7 +162,7 @@
for (i = 0; i < spec.access_tokens.length; i += 1) {
if (typeof spec.access_tokens[i] !== 'string' || !spec.access_tokens[i]) {
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 @@
}
AutomaticAPIStorage.prototype.get = function (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;
if (id.indexOf('/') !== 0) {
throw new jIO.util.jIOError('id ' + id +
' is forbidden (not starting with /)', 400);
}
}
return false;
if (id.lastIndexOf("/") !== (id.length - 1)) {
throw new jIO.util.jIOError('id ' + id +
' is forbidden (not ending with /)', 400);
}
if (!checkEndpointAsId(id)) {
throw new jIO.util.jIOError('Invalid id.', 400);
......@@ -151,7 +185,14 @@
return this._cache.get(id).push(function (res) {
return res;
}, 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