Merge "Fix sql injection vulnerability"
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / UserRolesCommonServiceImplTest.java
index 87abdbb..d3ac4b9 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START==========================================
  * ONAP Portal
  * ===================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ===================================================================
  *
  * Unless otherwise specified, all software contained herein is licensed
@@ -55,6 +55,7 @@ import java.util.TreeSet;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.cxf.transport.http.HTTPException;
+import org.drools.core.command.assertion.AssertEquals;
 import org.hibernate.Query;
 import org.hibernate.SQLQuery;
 import org.hibernate.Session;
@@ -237,6 +238,27 @@ public class UserRolesCommonServiceImplTest {
                return mockRoleInAppForUserList;
        }
 
+       @SuppressWarnings("unchecked")
+       @Test
+       public void checkTheProtectionAgainstSQLInjection() throws Exception {
+               EPUser user = mockUser.mockEPUser();
+               user.setId(1l);
+               user.setOrgId(2l);
+               Query epUserQuery = Mockito.mock(Query.class);
+               List<EPUser> mockEPUserList = new ArrayList<>();
+               mockEPUserList.add(user);
+
+               Mockito.when(session.createQuery("from :name where orgUserId=:userId")).thenReturn(epUserQuery);
+               Mockito.when(epUserQuery.setParameter("name",EPUser.class.getName())).thenReturn(epUserQuery);
+               Mockito.when(epUserQuery.setParameter("userId",user.getOrgUserId() + "; select * from " + EPUser.class.getName() +";")).thenReturn(epUserQuery);
+               userRolesCommonServiceImpl.createLocalUserIfNecessary(user.getOrgUserId(),true);
+
+               Mockito.when(session.createQuery("from :name where orgUserId=:userId")).thenReturn(epUserQuery);
+               Mockito.when(epUserQuery.setParameter("name",EPUser.class.getName())).thenReturn(epUserQuery);
+               Mockito.when(epUserQuery.setParameter("userId",user.getOrgUserId())).thenReturn(epUserQuery);
+               userRolesCommonServiceImpl.createLocalUserIfNecessary(user.getOrgUserId(),true);
+       }
+
        @SuppressWarnings("unchecked")
        @Test
        public void getAppRolesForUserNonCentralizedForPortal() throws Exception {
@@ -1319,7 +1341,7 @@ public class UserRolesCommonServiceImplTest {
                EPUserAppRolesRequest mockEpAppRolesRequestData = new EPUserAppRolesRequest();
                Mockito.doNothing().when(dataAccessService).saveDomainObject(mockEpAppRolesRequestData, null);
                final Map<String, Long> params = new HashMap<>();
-               params.put("appId", appWithRolesForUser.appId);
+               params.put("appId", appWithRolesForUser.getAppId());
                params.put("appRoleId", roleInAppForUser.roleId);
                Mockito.when((List<EPUserAppRoles>) dataAccessService.executeNamedQuery("appRoles", params, null))
                                .thenReturn(epUserAppRolesList);