Commit 6a1d0556 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

Merge branch...

Merge branch '674-when-creating-a-project-or-user-that-already-exists-return-proper-http-status-code' into 'master'

Resolve "when creating a project or user that already exists return proper http status code"

Closes #674

See merge request minerva/core!738
parents d8511866 968327d9
......@@ -1173,8 +1173,8 @@ ServerConnector.addProject = function (options) {
self._projectsById[project.getProjectId()] = project;
}
return project;
}).then(null, function (error) {
return self._processUpdateError(error);
}).catch(function (error) {
return self.processNetworkError(error);
});
};
......@@ -1413,11 +1413,14 @@ ServerConnector.addUser = function (user) {
password: user.getPassword(),
email: user.getEmail()
};
return self.sendPostRequest(self.getUserUrl(queryParams), filterParams).then(function () {
return self.getConfiguration();
}).then(function (configuration) {
return self.updateUserPrivileges({user: user, privileges: user.privilegesToExport(configuration)});
});
return self.sendPostRequest(self.getUserUrl(queryParams), filterParams)
.catch(function (error) {
return self.processNetworkError(error);
}).then(function () {
return self.getConfiguration();
}).then(function (configuration) {
return self.updateUserPrivileges({user: user, privileges: user.privilegesToExport(configuration)});
});
};
/**
......
......@@ -18,6 +18,7 @@ var logger = require('../../logger');
var guiUtils = new (require('../leftPanel/GuiUtils'))();
var ObjectExistsError = require("../../ObjectExistsError");
var UserPreferences = require("../../map/data/UserPreferences");
var ValidationError = require("../../ValidationError");
......@@ -1012,7 +1013,7 @@ AddProjectDialog.prototype.getDisease = function () {
/**
*
* @returns {string}
* @returns {number}
*/
AddProjectDialog.prototype.getOrganism = function () {
return $("[name='project-organism']", this.getElement()).val();
......@@ -1194,6 +1195,11 @@ AddProjectDialog.prototype.onSaveClicked = function () {
"zip-entries": self.getZipEntries()
};
return ServerConnector.addProject(options);
}).catch(function (error) {
if (error instanceof ObjectExistsError) {
return Promise.reject(new ValidationError("Project with given id already exists"));
}
return Promise.reject(error);
}).then(function (project) {
self.callListeners("onProjectAdd", project);
}).finally(function () {
......@@ -1233,11 +1239,7 @@ AddProjectDialog.prototype.checkValidity = function () {
isValid = false;
}
GuiConnector.showProcessing();
return self.getServerConnector().getProject(self.getProjectId()).then(function (project) {
if (project !== null) {
error += "<li>Project with given id already exists</li>";
isValid = false;
}
return Promise.resolve().then(function () {
if (self.getDisease() !== "") {
return self.getServerConnector().getMesh({id: self.getDisease()}).catch(function () {
error += "<li>Invalid mesh id: " + self.getDisease() + "</li>";
......
......@@ -6,6 +6,7 @@ var xss = require('xss');
var AbstractGuiElement = require('../AbstractGuiElement');
var GuiConnector = require('../../GuiConnector');
var ValidationError = require('../../ValidationError');
var ObjectExistsError = require('../../ObjectExistsError');
var Functions = require('../../Functions');
// noinspection JSUnusedLocalSymbols
......@@ -592,7 +593,12 @@ EditUserDialog.prototype.onSaveClicked = function () {
user.setName(self.getName());
user.setSurname(self.getSurname());
if (self.getIsNewUser()) {
return self.getServerConnector().addUser(user);
return self.getServerConnector().addUser(user).catch(function (error) {
if (error instanceof ObjectExistsError) {
return Promise.reject(new ValidationError("User with given login already exists"));
}
return Promise.reject(error);
});
} else {
return self.getServerConnector().updateUser(user);
}
......@@ -617,22 +623,11 @@ EditUserDialog.prototype.checkValidity = function () {
error += "<li>Login must not be empty</li>";
isValid = false;
}
var promise = Promise.resolve();
if (self.getUser().getLogin() !== self.getLogin()) {
promise = ServerConnector.getUser(self.getLogin()).then(function (remoteUser) {
if (remoteUser !== null) {
error += "<li>Login already used</li>";
isValid = false;
}
});
if (isValid) {
return Promise.resolve(true);
} else {
return Promise.reject(new ValidationError(error));
}
return promise.then(function () {
if (isValid) {
return Promise.resolve(true);
} else {
return Promise.reject(new ValidationError(error));
}
});
};
/**
......
......@@ -187,12 +187,12 @@ public class UserController extends BaseController {
@RequestMapping(value = "/users/{login:.+}", method = { RequestMethod.POST }, produces = {
MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> addOverlay(
public Map<String, Object> addUser(
@RequestBody MultiValueMap<String, Object> formData,
@PathVariable(value = "login") String login,
@CookieValue(value = Configuration.AUTH_TOKEN) String token
) throws SecurityException, IOException, QueryException {
return userRest.addProject(token, login, formData);
return userRest.addUser(token, login, formData);
}
......
......@@ -16,6 +16,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MultiValueMap;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.ObjectExistsException;
import lcsb.mapviewer.api.ObjectNotFoundException;
import lcsb.mapviewer.api.OperationNotAllowedException;
import lcsb.mapviewer.api.QueryException;
......@@ -753,7 +754,7 @@ public class UserRestImpl extends BaseRestImpl {
return getUser(token, login, "");
}
public Map<String, Object> addProject(String token, String login, MultiValueMap<String, Object> userData)
public Map<String, Object> addUser(String token, String login, MultiValueMap<String, Object> userData)
throws QueryException, SecurityException {
if (!getUserService().userHasPrivilege(token, PrivilegeType.USER_MANAGEMENT)) {
throw new SecurityException("Access denied");
......@@ -761,7 +762,7 @@ public class UserRestImpl extends BaseRestImpl {
User user = getUserService().getUserByLogin(login);
if (user != null) {
throw new QueryException("user exists");
throw new ObjectExistsException("user exists");
}
user = new User();
user.setLogin(login);
......
......@@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.HashMap;
......@@ -17,6 +18,7 @@ import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
......@@ -24,6 +26,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;
import lcsb.mapviewer.api.ObjectExistsException;
import lcsb.mapviewer.api.RestTestFunctions;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.model.map.MiriamType;
......@@ -65,6 +68,29 @@ public class UserRestImplTest extends RestTestFunctions {
}
}
@Test
public void testAddExistingUser() throws Exception {
try {
userRestImpl.addUser(adminToken, Configuration.ANONYMOUS_LOGIN, new LinkedMultiValueMap<>());
fail("Exception expected");
} catch (ObjectExistsException e) {
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testAddUser() throws Exception {
try {
Object response = userRestImpl.addUser(adminToken, "test_user", new LinkedMultiValueMap<>());
assertNotNull(response);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testUpdatePrivileges() throws Exception {
try {
......
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