Commit d92e704b authored by Piotr Gawron's avatar Piotr Gawron
Browse files

additional tests for plugin API

parent 3c1a2f05
......@@ -9,6 +9,8 @@ import java.util.Arrays;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -20,6 +22,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.transaction.annotation.Transactional;
import com.google.gson.JsonParser;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.model.plugin.Plugin;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.persist.dao.plugin.PluginDao;
......@@ -30,6 +36,8 @@ import lcsb.mapviewer.services.interfaces.IUserService;
@Rollback
public class PluginControllerIntegrationTest extends ControllerIntegrationTest {
Logger logger = LogManager.getLogger();
private static final String TEST_USER_PASSWORD = "test_pass";
private static final String TEST_USER_LOGIN = "test_user";
......@@ -110,4 +118,100 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest {
assertEquals(1, pluginDao.getAll().size());
}
@Test
public void setAndGetPluginData() throws Exception {
MockHttpSession session = createSession(TEST_USER_LOGIN, TEST_USER_PASSWORD);
String body = "value=xxx";
Plugin plugin = createPlugin();
RequestBuilder request = post("/plugins/" + plugin.getHash() + "/data/users/" + TEST_USER_LOGIN + "/key/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(session);
mockMvc.perform(request)
.andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString();
request = get("/plugins/" + plugin.getHash() + "/data/users/" + TEST_USER_LOGIN + "/key/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(session);
String response = mockMvc.perform(request)
.andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString();
String result = new JsonParser()
.parse(response)
.getAsJsonObject()
.get("value")
.getAsString();
assertEquals("xxx", result);
}
@Test
public void setAndGetGlobalPluginData() throws Exception {
String body = "value=xxx";
Plugin plugin = createPlugin();
RequestBuilder request = post("/plugins/" + plugin.getHash() + "/data/global/key/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body);
mockMvc.perform(request)
.andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString();
MockHttpSession session = createSession(TEST_USER_LOGIN, TEST_USER_PASSWORD);
request = get("/plugins/" + plugin.getHash() + "/data/global/key/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(session);
String response = mockMvc.perform(request)
.andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString();
String result = new JsonParser()
.parse(response)
.getAsJsonObject()
.get("value")
.getAsString();
assertEquals("xxx", result);
}
@Test
public void forbidAccessingOtherUserPluginData() throws Exception {
String body = "value=xxx";
Plugin plugin = createPlugin();
RequestBuilder request = post("/plugins/" + plugin.getHash() + "/data/users/" + Configuration.ANONYMOUS_LOGIN + "/key/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body);
mockMvc.perform(request)
.andExpect(status().is2xxSuccessful());
MockHttpSession session = createSession(TEST_USER_LOGIN, TEST_USER_PASSWORD);
request = get("/plugins/" + plugin.getHash() + "/data/users/" + Configuration.ANONYMOUS_LOGIN + "/key/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(session);
mockMvc.perform(request)
.andExpect(status().isForbidden());
}
private Plugin createPlugin() {
Plugin plugin = new Plugin();
plugin.setHash("XYZ");
plugin.setName("a");
plugin.setVersion("0.0.1");
pluginDao.add(plugin);
return plugin;
}
}
Supports Markdown
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