Commit 4b4d5688 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

error info about invalid credentials

parent 2a13b43d
......@@ -31,9 +31,10 @@
"uglifyjs": "^2.4.10"
},
"dependencies": {
"http-status-codes": "^1.1.6",
"js-cookie": "^2.1.3",
"log4js": "0.6.38",
"pileup": "^0.6.8",
"js-cookie": "^2.1.3",
"request": "^2.79.0"
}
}
"use strict";
/* exported logger */
var logger = require('./logger');
function InvalidCredentialsError(message) {
this.name = "InvalidCredentialsError";
this.message = message;
this.stack = (new Error()).stack;
}
module.exports = InvalidCredentialsError;
"use strict";
/* exported logger */
var logger = require('./logger');
function NetworkError(message, connectionParams) {
this.name = "NetworkError";
this.message = message;
this.stack = (new Error()).stack;
if (connectionParams === undefined || connectionParams === null) {
connectionParams = {};
}
this.statusCode = connectionParams.statusCode;
this.url = connectionParams.url;
this.content = connectionParams.content;
}
module.exports = NetworkError;
......@@ -8,6 +8,8 @@ var logger = require('./logger');
var request = require('request');
var HttpStatus = require('http-status-codes');
var Alias = require('./map/data/Alias');
var Chemical = require('./map/data/Chemical');
var Comment = require('./map/data/Comment');
......@@ -15,10 +17,12 @@ var Configuration = require('./Configuration');
var Drug = require('./map/data/Drug');
var ConfigurationType = require('./ConfigurationType');
var IdentifiedElement = require('./map/data/IdentifiedElement');
var InvalidCredentialsError = require('./InvalidCredentialsError');
var LayoutAlias = require('./map/data/LayoutAlias');
var LayoutData = require('./map/data/LayoutData');
var LayoutReaction = require('./map/data/LayoutReaction');
var MiRna = require('./map/data/MiRna');
var NetworkError = require('./NetworkError');
var Project = require('./map/data/Project');
var Reaction = require('./map/data/Reaction');
var ReferenceGenome = require('./map/data/ReferenceGenome');
......@@ -117,7 +121,7 @@ ServerConnector._readFile = function(url) {
};
ServerConnector.sendPostRequest = function(url, params) {
var self = this;
var self = this;
if (self.getSessionData().getToken() === undefined) {
self.getSessionData().setLogin(undefined);
window.location.reload(false);
......@@ -128,11 +132,17 @@ ServerConnector.sendPostRequest = function(url, params) {
form : params
}, function(error, response, body) {
if (error) {
reject(error);
reject(new NetworkError(error, {
content : body,
url : url
}));
} else if (response.statusCode !== 200) {
var error = new Error(url + " rejected with status code: " + response.statusCode);
reject(error);
reject(new NetworkError(url + " rejected with status code: " + response.statusCode, {
content : body,
url : url,
statusCode : response.statusCode
}));
} else {
resolve(body);
}
......@@ -141,7 +151,7 @@ ServerConnector.sendPostRequest = function(url, params) {
};
ServerConnector.sendPutRequest = function(url, params) {
var self = this;
var self = this;
if (self.getSessionData().getToken() === undefined) {
self.getSessionData().setLogin(undefined);
window.location.reload(false);
......@@ -164,7 +174,7 @@ ServerConnector.sendPutRequest = function(url, params) {
};
ServerConnector.sendDeleteRequest = function(url, params) {
var self = this;
var self = this;
if (self.getSessionData().getToken() === undefined) {
self.getSessionData().setLogin(undefined);
window.location.reload(false);
......@@ -188,7 +198,7 @@ ServerConnector.sendDeleteRequest = function(url, params) {
};
ServerConnector.sendPatchRequest = function(url, params) {
var self = this;
var self = this;
if (self.getSessionData().getToken() === undefined) {
self.getSessionData().setLogin(undefined);
window.location.reload(false);
......@@ -865,6 +875,12 @@ ServerConnector.login = function(login, password) {
return self.sendPostRequest(self.loginUrl(), params).then(function() {
self.getSessionData().setLogin(params.login);
return Promise.resolve(self.getSessionData().getToken());
}, function(error) {
if (error instanceof NetworkError && error.statusCode === HttpStatus.FORBIDDEN) {
throw new InvalidCredentialsError("Invalid credentials");
} else {
throw error;
}
});
};
......
......@@ -4,6 +4,7 @@ var Promise = require("bluebird");
/* exported logger */
var InvalidCredentialsError = require('../../InvalidCredentialsError');
var Panel = require('./Panel');
var PanelControlElementType = require('../PanelControlElementType');
......@@ -26,7 +27,13 @@ function UserPanel(params) {
return ServerConnector.login(login, password).then(function() {
window.location.reload(false);
}).then(null, GuiConnector.alert);
}).then(null, function(error){
if (error instanceof InvalidCredentialsError) {
GuiConnector.alert("invalid credentials");
} else {
GuiConnector.alert(error);
}
});
};
}
......
......@@ -2,9 +2,12 @@
require("./mocha-config.js");
var HttpStatus = require('http-status-codes');
var Alias = require('../../main/js/map/data/Alias');
var Configuration = require('../../main/js/Configuration');
var LayoutAlias = require('../../main/js/map/data/LayoutAlias');
var NetworkError = require('../../main/js/NetworkError');
var Project = require('../../main/js/map/data/Project');
var Reaction = require('../../main/js/map/data/Reaction');
var ServerConnector = require('../../main/js/ServerConnector');
......@@ -104,7 +107,7 @@ describe('ServerConnector', function() {
return ServerConnector.readFile(url);
});
});
it('getModelDownloadUrl', function() {
var modelId = 15781;
return ServerConnector.getModelDownloadUrl({
......@@ -116,16 +119,13 @@ describe('ServerConnector', function() {
return ServerConnector.readFile(url);
});
});
it('getProjectSourceDownloadUrl', function() {
return ServerConnector.getProjectSourceDownloadUrl().then(function(url) {
assert.ok(url);
return ServerConnector.readFile(url);
});
});
it('addOverlay', function() {
return ServerConnector.addOverlay({
......@@ -175,10 +175,25 @@ describe('ServerConnector', function() {
it('getConfiguration', function() {
return ServerConnector.getConfiguration().then(function(configuration) {
assert.ok(configuration instanceof Configuration);
assert.ok(configuration.getElementTypes().length>0);
assert.ok(configuration.getReactionTypes().length>0);
assert.ok(configuration.getElementTypes().length > 0);
assert.ok(configuration.getReactionTypes().length > 0);
});
});
it('login with invalid credentials', function() {
var method = ServerConnector.sendPostRequest;
ServerConnector.sendPostRequest = function() {
return Promise.reject(new NetworkError("xxx", {
statusCode : HttpStatus.FORBIDDEN
}));
}
return ServerConnector.login("blabla", "blablabla").then(function() {
ServerConnector.sendPostRequest = method;
assert.ok(false);
}, function(error) {
ServerConnector.sendPostRequest = method;
assert.ok(error.message.indexOf("credentials") >= 0);
});
});
});
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