Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 05a599e9 authored by Joze RIHTARSIC's avatar Joze RIHTARSIC
Browse files

Merge pull request #155 in EDELIVERY/smp from development to master

* commit '9023bbe9713d2f150031d465d9aaf4a05af6537c':
  add unit tests
  add unit tests
  Increase code coverage
  fix jacoco java option export
parents e29439ad 41dff3b3
No related branches found
No related tags found
No related merge requests found
......@@ -52,7 +52,6 @@ RUN apt-get update \
&& rm tomcat.zip \
&& mkdir -p $SMP_HOME/apache-tomcat-$TOMCAT_VERSION/smp/conf/ \
&& echo "export CLASSPATH=$SMP_HOME/apache-tomcat-$TOMCAT_VERSION/smp/conf" > $SMP_HOME/apache-tomcat-$TOMCAT_VERSION/bin/setenv.sh \
# && echo "export JAVA_OPTS=$JAVA_OPTS -Djdk.http.auth.tunneling.disabledSchemes= -Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true" >> $SMP_HOME/apache-tomcat-$TOMCAT_VERSION/bin/setenv.sh \
&& sed -i -e "s/<\/Context>/<Resource name=\"jdbc\/eDeliverySmpDs\" auth=\"Container\" type=\"javax.sql.DataSource\" maxTotal=\"100\" maxIdle=\"30\" maxWaitMillis=\"10000\" username=\"$DB_USER\" password=\"$DB_USER\" driverClassName=\"com.mysql.jdbc.Driver\" url=\"jdbc:mysql:\/\/localhost:3306\/$DB_SCHEMA?useSSL=false\&amp;characterEncoding=UTF-8\&amp;useUnicode=true\"\/><\/Context>/g" "$SMP_HOME/apache-tomcat-$TOMCAT_VERSION/conf/context.xml" \
&& sed -i -e "s/<Connector /<Connector URIEncoding=\"UTF-8\" /g" "$SMP_HOME/apache-tomcat-$TOMCAT_VERSION/conf/server.xml" \
&& echo "datasource.jndi=java:comp/env/jdbc/eDeliverySmpDs" > $SMP_HOME/apache-tomcat-$TOMCAT_VERSION/smp/conf/smp.config.properties \
......@@ -74,9 +73,7 @@ RUN unzip /tmp/artefacts/smp-setup.zip -d /tmp/ \
&& mv /tmp/smp-$SMP_VERSION/* /tmp/artefacts/ \
&& rm -rf /tmp/smp-$SMP_VERSION \
&& mv /tmp/artefacts/smp.war $SMP_HOME/apache-tomcat-$TOMCAT_VERSION/webapps/ \
&& chmod u+x /sbin/entrypoint.sh \
&& echo "export JAVA_OPTS='$JAVA_OPTS -Djdk.http.auth.tunneling.disabledSchemes='' -Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true'" >> $SMP_HOME/apache-tomcat-$TOMCAT_VERSION/bin/setenv.sh
&& chmod u+x /sbin/entrypoint.sh
ENTRYPOINT ["/sbin/entrypoint.sh"]
......
......@@ -23,7 +23,10 @@ fi
init_tomcat() {
# add java code coverage angent to image
export JAVA_OPTS="$JAVA_OPTS -javaagent:/opt/jacoco/jacoco-agent.jar=output=tcpserver,address=*,port=6901"
JAVA_OPTS="-javaagent:/opt/jacoco/jacoco-agent.jar=output=tcpserver,address=*,port=6901 $JAVA_OPTS"
# add allow encoded slashes and disable scheme for proxy
JAVA_OPTS="$JAVA_OPTS -Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true -Djdk.http.auth.tunneling.disabledSchemes="
export JAVA_OPTS
echo "[INFO] init tomcat folders: $tfile"
......
package eu.europa.ec.edelivery.smp.config;
import eu.europa.ec.edelivery.security.BlueCoatAuthenticationFilter;
import eu.europa.ec.edelivery.smp.data.dao.ConfigurationDao;
import eu.europa.ec.edelivery.smp.data.ui.enums.SMPPropertyEnum;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.test.util.ReflectionTestUtils;
import java.io.IOException;
import static org.junit.Assert.*;
public class SMPPropertyUpdateListenerTest {
SMPPropertyUpdateListener testInstance = new SMPPropertyUpdateListener();
BlueCoatAuthenticationFilter blueCoatAuthenticationFilter = Mockito.mock(BlueCoatAuthenticationFilter.class);
ConfigurationDao configurationDao = Mockito.mock(ConfigurationDao.class);
@Before
public void before() throws IOException {
ReflectionTestUtils.setField(testInstance,"blueCoatAuthenticationFilter",blueCoatAuthenticationFilter);
ReflectionTestUtils.setField(testInstance,"configurationDao",configurationDao);
}
@Test
public void testInit() {
testInstance.init();
Mockito.verify(configurationDao, Mockito.times(1)).addPropertyUpdateListener(testInstance);
}
@Test
public void propertiesUpdateTrue() {
Mockito.doReturn(Boolean.TRUE ).when(configurationDao).getCachedPropertyValue(SMPPropertyEnum.BLUE_COAT_ENABLED);
testInstance.propertiesUpdate();
Mockito.verify(blueCoatAuthenticationFilter, Mockito.times(1)).setBlueCoatEnabled(true);
}
@Test
public void propertiesUpdateFalse() {
Mockito.doReturn(Boolean.FALSE ).when(configurationDao).getCachedPropertyValue(SMPPropertyEnum.BLUE_COAT_ENABLED);
testInstance.propertiesUpdate();
Mockito.verify(blueCoatAuthenticationFilter, Mockito.times(1)).setBlueCoatEnabled(false);
}
}
\ No newline at end of file
......@@ -5,6 +5,7 @@ import eu.europa.ec.edelivery.smp.config.PropertiesTestConfig;
import eu.europa.ec.edelivery.smp.config.SmpAppConfig;
import eu.europa.ec.edelivery.smp.config.SmpWebAppConfig;
import eu.europa.ec.edelivery.smp.config.SpringSecurityConfig;
import eu.europa.ec.edelivery.smp.data.ui.SmpConfigRO;
import eu.europa.ec.edelivery.smp.data.ui.SmpInfoRO;
import org.junit.Before;
import org.junit.Test;
......@@ -27,7 +28,7 @@ import org.springframework.web.context.WebApplicationContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
......@@ -53,6 +54,10 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
public class ApplicationResourceTest {
private static final String PATH = "/ui/rest/application";
private static final RequestPostProcessor SMP_ADMIN_CREDENTIALS = httpBasic("smp_admin", "test123");
private static final RequestPostProcessor SG_ADMIN_CREDENTIALS = httpBasic("sg_admin", "test123");
private static final RequestPostProcessor SYSTEM_CREDENTIALS = httpBasic("sys_admin", "test123");
@Autowired
private WebApplicationContext webAppContext;
......@@ -80,7 +85,7 @@ public class ApplicationResourceTest {
}
@Test
public void getName() throws Exception {
public void testGetName() throws Exception {
String value = mvc.perform(get(PATH + "/name"))
.andExpect(status().isOk())
.andReturn()
......@@ -92,7 +97,7 @@ public class ApplicationResourceTest {
}
@Test
public void getRootContext() throws Exception {
public void testGetRootContext() throws Exception {
String value = mvc.perform(get(PATH + "/rootContext"))
.andExpect(status().isOk())
.andReturn()
......@@ -102,6 +107,61 @@ public class ApplicationResourceTest {
assertEquals("/", value);
}
@Test
public void testGetApplicationConfigNotAuthorized() throws Exception {
// when
mvc.perform(get(PATH + "/config"))
.andExpect(status().isUnauthorized())
.andReturn()
.getResponse();
}
@Test
public void testGetApplicationConfigAuthorized() throws Exception {
// SMP admin
String val = mvc.perform(get(PATH + "/config").with(SMP_ADMIN_CREDENTIALS))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString();
assertNotNull(val);
// service group
val = mvc.perform(get(PATH + "/config").with(SG_ADMIN_CREDENTIALS))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString();
assertNotNull(val);
// system admin
val = mvc.perform(get(PATH + "/config").with(SYSTEM_CREDENTIALS))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString();
assertNotNull(val);
}
@Test
public void testGetApplicationConfigSMPAdmin() throws Exception {
// when
String value = mvc.perform(get(PATH + "/config").with(SMP_ADMIN_CREDENTIALS))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString();
// then
ObjectMapper mapper = new ObjectMapper();
SmpConfigRO res = mapper.readValue(value, SmpConfigRO.class);
assertNotNull(res);
assertEquals("Participant scheme must start with:urn:oasis:names:tc:ebcore:partyid-type:(iso6523:|unregistered:) OR must be up to 25 characters long with form [domain]-[identifierArea]-[identifierType] (ex.: 'busdox-actorid-upis') and may only contain the following characters: [a-z0-9].",res.getParticipantSchemaRegExpMessage());
assertEquals("^((?!^.{26})([a-z0-9]+-[a-z0-9]+-[a-z0-9]+)|urn:oasis:names:tc:ebcore:partyid-type:(iso6523|unregistered)(:.+)?$)",res.getParticipantSchemaRegExp());
assertFalse(res.isSmlIntegrationOn());
assertFalse(res.isSmlParticipantMultiDomainOn());
}
@Test
public void getDisplayName() throws Exception {
String value = applicationResource.getDisplayVersion();
......
package eu.europa.ec.edelivery.smp.ui;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.europa.ec.edelivery.smp.config.PropertiesTestConfig;
import eu.europa.ec.edelivery.smp.config.SmpAppConfig;
import eu.europa.ec.edelivery.smp.config.SmpWebAppConfig;
import eu.europa.ec.edelivery.smp.config.SpringSecurityConfig;
import eu.europa.ec.edelivery.smp.data.ui.UserRO;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -16,6 +18,7 @@ import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.RequestPostProcessor;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.ContextLoaderListener;
......@@ -24,10 +27,11 @@ import org.springframework.web.context.WebApplicationContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import javax.ws.rs.core.MediaType;
import static org.junit.Assert.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
......@@ -82,6 +86,7 @@ public class AuthenticationResourceTest {
assertNotNull(session);
}
@Test
public void authenticateInvalidPasswordTest() throws Exception {
......
......@@ -5,12 +5,10 @@ import eu.europa.ec.edelivery.smp.config.PropertiesTestConfig;
import eu.europa.ec.edelivery.smp.config.SmpAppConfig;
import eu.europa.ec.edelivery.smp.config.SmpWebAppConfig;
import eu.europa.ec.edelivery.smp.config.SpringSecurityConfig;
import eu.europa.ec.edelivery.smp.data.ui.CertificateRO;
import eu.europa.ec.edelivery.smp.data.ui.ServiceGroupRO;
import eu.europa.ec.edelivery.smp.data.ui.ServiceResult;
import eu.europa.ec.edelivery.smp.data.ui.UserRO;
import eu.europa.ec.edelivery.smp.data.ui.*;
import eu.europa.ec.edelivery.smp.testutils.X509CertificateTestUtils;
import org.apache.commons.io.IOUtils;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -25,20 +23,26 @@ import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.RequestPostProcessor;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import javax.ws.rs.core.MediaType;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
......@@ -65,6 +69,7 @@ public class UserResourceTest {
private MockMvc mvc;
private static final RequestPostProcessor ADMIN_CREDENTIALS = httpBasic("smp_admin", "test123");
private static final RequestPostProcessor SYSTEM_CREDENTIALS = httpBasic("sys_admin", "test123");
private static final RequestPostProcessor SG_ADMIN_CREDENTIALS = httpBasic("sg_admin", "test123");
@Before
public void setup() {
mvc = MockMvcBuilders.webAppContextSetup(webAppContext)
......@@ -101,6 +106,121 @@ public class UserResourceTest {
});
}
@Test
public void testUpdateCurrentUserOK() throws Exception {
// given when - log as SMP admin
MvcResult result = mvc.perform(post("/ui/rest/security/authentication")
.header("Content-Type","application/json")
.content("{\"username\":\"smp_admin\",\"password\":\"test123\"}"))
.andExpect(status().isOk()).andReturn();
ObjectMapper mapper = new ObjectMapper();
UserRO userRO = mapper.readValue(result.getResponse().getContentAsString(), UserRO.class);
assertNotNull(userRO);
// when
userRO.setActive(!userRO.isActive());
userRO.setEmailAddress("test@mail.com");
userRO.setPassword(UUID.randomUUID().toString());
if (userRO.getCertificate()==null) {
userRO.setCertificate(new CertificateRO());
}
userRO.getCertificate().setCertificateId(UUID.randomUUID().toString());
mvc.perform(put(PATH+"/"+userRO.getId()).with(ADMIN_CREDENTIALS)
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(userRO))
).andExpect(status().isOk()).andReturn();
}
@Test
public void testUpdateCurrentUserNotAuthenticatedUser() throws Exception {
// given when - log as SMP admin
// then change values and list uses for changed value
MvcResult result = mvc.perform(post("/ui/rest/security/authentication")
.header("Content-Type","application/json")
.content("{\"username\":\"smp_admin\",\"password\":\"test123\"}"))
.andExpect(status().isOk()).andReturn();
ObjectMapper mapper = new ObjectMapper();
UserRO userRO = mapper.readValue(result.getResponse().getContentAsString(), UserRO.class);
assertNotNull(userRO);
// when
userRO.setActive(!userRO.isActive());
userRO.setEmailAddress("test@mail.com");
userRO.setPassword(UUID.randomUUID().toString());
if (userRO.getCertificate()==null) {
userRO.setCertificate(new CertificateRO());
}
userRO.getCertificate().setCertificateId(UUID.randomUUID().toString());
mvc.perform(put(PATH+"/"+userRO.getId()).with(SYSTEM_CREDENTIALS)
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(userRO))
).andExpect(status().isUnauthorized());
}
@Test
public void testUpdateUserList() throws Exception {
// given when
MvcResult result = mvc.perform(get(PATH).with(SYSTEM_CREDENTIALS)).
andExpect(status().isOk()).andReturn();
ObjectMapper mapper = new ObjectMapper();
ServiceResult res = mapper.readValue(result.getResponse().getContentAsString(), ServiceResult.class);
assertNotNull(res);
assertFalse(res.getServiceEntities().isEmpty());
UserRO userRO = mapper.convertValue(res.getServiceEntities().get(0), UserRO.class);
// then
userRO.setActive(!userRO.isActive());
userRO.setEmailAddress("test@mail.com");
userRO.setPassword(UUID.randomUUID().toString());
if (userRO.getCertificate()==null) {
userRO.setCertificate(new CertificateRO());
}
userRO.getCertificate().setCertificateId(UUID.randomUUID().toString());
mvc.perform(put(PATH)
.with(SYSTEM_CREDENTIALS).contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(Arrays.asList(userRO)))
).andExpect(status().isOk());
}
@Test
public void testUpdateUserListWrongAuthentication() throws Exception {
// given when
MvcResult result = mvc.perform(get(PATH).with(SYSTEM_CREDENTIALS)).
andExpect(status().isOk()).andReturn();
ObjectMapper mapper = new ObjectMapper();
ServiceResult res = mapper.readValue(result.getResponse().getContentAsString(), ServiceResult.class);
assertNotNull(res);
assertFalse(res.getServiceEntities().isEmpty());
UserRO userRO = mapper.convertValue(res.getServiceEntities().get(0), UserRO.class);
// then
userRO.setActive(!userRO.isActive());
userRO.setEmailAddress("test@mail.com");
userRO.setPassword(UUID.randomUUID().toString());
if (userRO.getCertificate()==null) {
userRO.setCertificate(new CertificateRO());
}
userRO.getCertificate().setCertificateId(UUID.randomUUID().toString());
// anonymous
mvc.perform(put(PATH)
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(Arrays.asList(userRO)))
).andExpect(status().isUnauthorized());
mvc.perform(put(PATH)
.with(ADMIN_CREDENTIALS).contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(Arrays.asList(userRO)))
).andExpect(status().isUnauthorized());
mvc.perform(put(PATH)
.with(SG_ADMIN_CREDENTIALS).contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(Arrays.asList(userRO)))
).andExpect(status().isUnauthorized());
}
@Test
public void uploadCertificateSystemAdmin() throws Exception {
byte[] buff = IOUtils.toByteArray(UserResourceTest.class.getResourceAsStream("/SMPtest.crt"));
......@@ -123,6 +243,19 @@ public class UserResourceTest {
assertEquals("sno=3&subject=1.2.840.113549.1.9.1%3D%23160c736d7040746573742e636f6d%2CCN%3DSMP+test%2CO%3DDIGIT%2CC%3DBE&validfrom=May+22+20%3A59%3A00+2018+GMT&validto=May+22+20%3A56%3A00+2019+GMT&issuer=CN%3DIntermediate+CA%2CO%3DDIGIT%2CC%3DBE", res.getBlueCoatHeader());
}
@Test
public void uploadInvalidCertificate() throws Exception {
byte[] buff = (new String("Not a certficate :) ")).getBytes();
// given when
mvc.perform(post(PATH+"/1098765430/certdata")
.with(SYSTEM_CREDENTIALS)
.content(buff))
.andExpect(status().is5xxServerError())
.andExpect(content().string(CoreMatchers.containsString(" The certificate is not valid")));
}
@Test
public void uploadCertificateIdWithEmailSerialNumberInSubjectCertIdTest() throws Exception {
String subject = "CN=common name,emailAddress=CEF-EDELIVERY-SUPPORT@ec.europa.eu,serialNumber=1,O=org,ST=My town,postalCode=2151, L=GreatTown,street=My Street. 20, C=BE";
......@@ -189,4 +322,38 @@ public class UserResourceTest {
}
@Test
public void testValidateDeleteUserOK() throws Exception {
MvcResult result = mvc.perform(post(PATH+"/validateDelete")
.with(SYSTEM_CREDENTIALS)
.contentType(org.springframework.http.MediaType.APPLICATION_JSON)
.content("[5]"))
.andExpect(status().isOk()).andReturn();
ObjectMapper mapper = new ObjectMapper();
DeleteEntityValidation res = mapper.readValue(result.getResponse().getContentAsString(), DeleteEntityValidation.class);
assertFalse(res.getListIds().isEmpty());
assertTrue(res.getListDeleteNotPermitedIds().isEmpty());
assertEquals(5, res.getListIds().get(0).intValue());
}
@Test
public void testValidateDeleteUserNotOK() throws Exception {
// note system credential has id 3!
MvcResult result = mvc.perform(post(PATH+"/validateDelete")
.with(SYSTEM_CREDENTIALS)
.contentType(org.springframework.http.MediaType.APPLICATION_JSON)
.content("[3]"))
.andExpect(status().isOk())
.andReturn();
ObjectMapper mapper = new ObjectMapper();
DeleteEntityValidation res = mapper.readValue(result.getResponse().getContentAsString(), DeleteEntityValidation.class);
assertTrue(res.getListIds().isEmpty());
assertEquals("Could not delete logged user!",res.getStringMessage());
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment