Commit 035eae94 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

Merge branch '816-validation-of-integer-parameters' into 'devel_13.1.x'

Resolve "MINERVANET - Error Report 70"

See merge request !792
parents d2c901d4 36a09479
......@@ -6,9 +6,11 @@ var ConfigurationType = require('../../ConfigurationType');
var Functions = require('../../Functions');
var GuiConnector = require('../../GuiConnector');
var NetworkError = require('../../NetworkError');
var ValidationError = require('../../ValidationError');
var logger = require('../../logger');
var HttpStatus = require('http-status-codes');
var Promise = require("bluebird");
var xss = require("xss");
......@@ -253,9 +255,11 @@ ConfigurationAdminPanel.prototype.saveOption = function (type) {
value = element.val();
}
var oldVal;
GuiConnector.showProcessing();
return self.getServerConnector().getConfiguration().then(function (configuration) {
option = configuration.getOption(type);
oldVal = option.getValue();
if (option === undefined) {
return Promise.reject(new ValidationError("Unknown configuration type: " + type));
}
......@@ -317,7 +321,16 @@ ConfigurationAdminPanel.prototype.saveOption = function (type) {
}
});
}
}).catch(GuiConnector.alert).finally(GuiConnector.hideProcessing);
}).catch(function (e) {
if (e instanceof NetworkError && e.statusCode === HttpStatus.BAD_REQUEST) {
var content = e.content;
GuiConnector.alert(new ValidationError(content.reason));
option.setValue(oldVal);
element.val(oldVal);
} else {
GuiConnector.alert(e);
}
}).finally(GuiConnector.hideProcessing);
};
/**
......
......@@ -3,8 +3,9 @@ package lcsb.mapviewer.api;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.TreeMap;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import org.apache.log4j.Logger;
import org.springframework.http.HttpHeaders;
......@@ -37,7 +38,8 @@ public abstract class BaseController {
} else if (e instanceof ObjectExistsException) {
return createErrorResponse("Object already exists.", e.getMessage(), new HttpHeaders(), HttpStatus.CONFLICT);
} else if (e instanceof OperationNotAllowedException) {
return createErrorResponse("Operation not allowed.", e.getMessage(), new HttpHeaders(), HttpStatus.METHOD_NOT_ALLOWED);
return createErrorResponse("Operation not allowed.", e.getMessage(), new HttpHeaders(),
HttpStatus.METHOD_NOT_ALLOWED);
} else if (e instanceof QueryException) {
logger.error(e, e);
return createErrorResponse("Query server error.", e.getMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST);
......@@ -53,8 +55,10 @@ public abstract class BaseController {
private ResponseEntity<Object> createErrorResponse(String errorMessage, String error, HttpHeaders httpHeaders,
HttpStatus status) {
return new ResponseEntity<Object>(
"{\"error\" : \"" + errorMessage + "\",\"reason\":" + new Gson().toJson(error) + "}", httpHeaders, status);
Map<String, String> response = new HashMap<>();
response.put("error", errorMessage);
response.put("reason", error);
return new ResponseEntity<Object>(new Gson().toJson(response), httpHeaders, status);
}
public Map<String, Object> parseBody(String body) throws IOException, JsonParseException, JsonMappingException {
......
......@@ -73,7 +73,7 @@ public class ConfigurationController extends BaseController {
@RequestMapping(value = "/configuration/options/{option}", method = { RequestMethod.PATCH }, produces = {
MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> getOption(
public Map<String, Object> updateOption(
@RequestBody String body,
@CookieValue(value = Configuration.AUTH_TOKEN) String token,
@PathVariable(value = "option") String option
......
......@@ -293,7 +293,7 @@ public class ConfigurationRestImpl extends BaseRestImpl {
try {
configurationService.setConfigurationValue(type, value);
} catch (InvalidArgumentException e) {
throw new QueryException(e);
throw new QueryException(e.getMessage(), e);
}
return optionToMap(configurationService.getValue(type));
}
......
......@@ -5,7 +5,9 @@ import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ ConfigurationRestImplTest.class })
@SuiteClasses({
ConfigurationControllerTest.class,
ConfigurationRestImplTest.class })
public class AllConfigurationTests {
}
package lcsb.mapviewer.api.configuration;
import static org.junit.Assert.fail;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.google.gson.Gson;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.api.SpringRestApiTestConfig;
import lcsb.mapviewer.model.user.ConfigurationElementType;
import lcsb.mapviewer.services.interfaces.IUserService;
@ContextConfiguration(classes = SpringRestApiTestConfig.class)
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ConfigurationControllerTest {
@Autowired
ConfigurationController configurationController;
@Autowired
IUserService userService;
@Test
public void testSetSmtpPortToInvalid() throws Exception {
try {
// assume that we have admin account with all the privileges
String adminToken = userService.login("admin", "admin");
Map<String, Object> option= new HashMap<>();
option.put("value", "not a number");
option.put("type", "not a number");
Map<String, Object> data= new HashMap<>();
data.put("option", option);
String body = new Gson().toJson(data);
configurationController.updateOption(body, adminToken, ConfigurationElementType.MAX_NUMBER_OF_MAP_LEVELS.name());
fail("Exception expected");
} catch (QueryException e) {
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
......@@ -65,6 +65,7 @@ public class ConfigurationService implements IConfigurationService {
}
@Override
@Transactional(noRollbackFor = InvalidArgumentException.class)
public void setConfigurationValue(ConfigurationElementType type, String value) {
if (value == null) {
throw new InvalidArgumentException("null is not a proper value");
......
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