Commit 5d2df69d authored by Sonia Zorba's avatar Sonia Zorba Committed by zonia3000
Browse files

Fixed bug in PermissionsService; Added tests

parent af35db24
const BASE_API_URL = process.env.VUE_APP_API_BASE_URL;
function apiRequest(url, data) {
function apiRequest(url, options) {
loading(true);
return new Promise((resolve, reject) => {
fetch(url, data)
fetch(url, options)
.then(response => {
loading(false);
if([200, 201, 204, 400].includes(response.status)) { // valid status codes
......
......@@ -88,22 +88,21 @@ public class PermissionsDAO {
});
}
public Optional<PermissionEntity> findPermissionEntity(String groupId, String userId, Permission permission) {
public Optional<PermissionEntity> findPermissionEntity(String groupId, String userId) {
String sql = "SELECT group_path FROM gms_permission WHERE group_id = ? AND user_id = ? AND permission = ?";
String sql = "SELECT group_path, permission FROM gms_permission WHERE group_id = ? AND user_id = ?";
return jdbcTemplate.query(conn -> {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, groupId);
ps.setString(2, userId);
ps.setObject(3, permission.toString(), Types.OTHER);
return ps;
}, resultSet -> {
if (resultSet.next()) {
PermissionEntity permissionEntity = new PermissionEntity();
permissionEntity.setGroupId(groupId);
permissionEntity.setUserId(userId);
permissionEntity.setPermission(permission);
permissionEntity.setPermission(Permission.valueOf(resultSet.getString("permission")));
permissionEntity.setGroupPath(resultSet.getString("group_path"));
return Optional.of(permissionEntity);
}
......
......@@ -66,19 +66,28 @@ public class PermissionsService {
public PermissionEntity addPermission(GroupEntity group, String userId, Permission permission) {
PermissionEntity permissionEntity = new PermissionEntity();
permissionEntity.setGroupId(group.getId());
permissionEntity.setUserId(userId);
permissionEntity.setPermission(permission);
permissionEntity.setGroupPath(group.getPath());
Optional<PermissionEntity> existingPermissionEntity = permissionsDAO.findPermissionEntity(group.getId(), userId);
PermissionEntity permissionEntity;
if (existingPermissionEntity.isPresent()) {
permissionEntity = existingPermissionEntity.get();
Permission resultingPermission = Permission.addPermission(permissionEntity.getPermission(), permission);
if (resultingPermission == permissionEntity.getPermission()) {
return permissionEntity;
} else {
permissionEntity.setPermission(resultingPermission);
}
} else {
permissionEntity = new PermissionEntity();
permissionEntity.setGroupId(group.getId());
permissionEntity.setUserId(userId);
permissionEntity.setPermission(permission);
permissionEntity.setGroupPath(group.getPath());
}
return permissionsDAO.createOrUpdatePermission(permissionEntity);
}
public Optional<PermissionEntity> findPermissionEntity(String groupId, String userId, Permission permission) {
return permissionsDAO.findPermissionEntity(groupId, userId, permission);
}
public void movePermissions(String fromUserId, String toUserId) {
permissionsDAO.movePermissions(fromUserId, toUserId);
}
......
package it.inaf.ia2.gms.controller;
import it.inaf.ia2.gms.authn.SessionData;
import it.inaf.ia2.gms.model.GroupNode;
import it.inaf.ia2.gms.model.Permission;
import it.inaf.ia2.gms.model.response.GroupsTabResponse;
import it.inaf.ia2.gms.model.response.PaginatedData;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
import it.inaf.ia2.gms.service.GroupsService;
import it.inaf.ia2.gms.service.GroupsTreeBuilder;
import it.inaf.ia2.gms.service.PermissionsService;
import java.util.ArrayList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
......@@ -15,11 +10,9 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.Mockito.when;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
......@@ -31,19 +24,6 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
public class GroupsControllerTest {
@Mock
private SessionData session;
@Mock
private GroupsService groupsService;
@Mock
private PermissionsService permissionsService;
@Mock
private GroupsTreeBuilder groupsTreeBuilder;
@Spy
@InjectMocks
private GroupsTabResponseBuilder groupsTabResponseBuilder;
@InjectMocks
......@@ -59,20 +39,12 @@ public class GroupsControllerTest {
@Test
public void testGetGroups() throws Exception {
when(session.getUserId()).thenReturn("admin_id");
GroupEntity root = new GroupEntity();
root.setId("ROOT");
root.setName("ROOT");
root.setPath("");
when(groupsService.getGroupById(eq("ROOT"))).thenReturn(root);
when(permissionsService.getUserPermissionForGroup(eq(root), eq("admin_id")))
.thenReturn(Permission.ADMIN);
GroupsTabResponse response = new GroupsTabResponse();
response.setBreadcrumbs(new ArrayList<>());
response.setGroupsPanel(new PaginatedData<>(new ArrayList<>(), 1, 10));
response.setPermission(Permission.ADMIN);
PaginatedData<GroupNode> groupsPanel = new PaginatedData<>(new ArrayList<>(), 1, 10);
when(groupsTreeBuilder.listSubGroups(eq("ROOT"), eq("admin_id"), any())).thenReturn(groupsPanel);
when(groupsTabResponseBuilder.getGroupsTab(any())).thenReturn(response);
mockMvc.perform(get("/groups?groupId=ROOT&paginatorPageSize=20&paginatorPage=1"))
.andExpect(status().isOk())
......
package it.inaf.ia2.gms.controller;
import it.inaf.ia2.gms.authn.SessionData;
import it.inaf.ia2.gms.model.GroupNode;
import it.inaf.ia2.gms.model.Permission;
import it.inaf.ia2.gms.model.request.TabRequest;
import it.inaf.ia2.gms.model.response.GroupsTabResponse;
import it.inaf.ia2.gms.model.response.PaginatedData;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
import it.inaf.ia2.gms.service.GroupsService;
import it.inaf.ia2.gms.service.GroupsTreeBuilder;
import it.inaf.ia2.gms.service.PermissionsService;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.Mockito.when;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class GroupsTabResponseBuilderTest {
@Mock
private SessionData session;
@Mock
private GroupsService groupsService;
@Mock
private PermissionsService permissionsService;
@Mock
private GroupsTreeBuilder groupsTreeBuilder;
@InjectMocks
private GroupsTabResponseBuilder groupsTabResponseBuilder;
@Test
public void testGetGroupsTab() {
when(session.getUserId()).thenReturn("admin_id");
GroupEntity root = new GroupEntity();
root.setId("ROOT");
root.setName("ROOT");
root.setPath("");
when(groupsService.getGroupById(eq("ROOT"))).thenReturn(root);
when(permissionsService.getUserPermissionForGroup(eq(root), eq("admin_id")))
.thenReturn(Permission.ADMIN);
PaginatedData<GroupNode> groupsPanel = new PaginatedData<>(new ArrayList<>(), 1, 10);
when(groupsTreeBuilder.listSubGroups(eq("ROOT"), eq("admin_id"), any())).thenReturn(groupsPanel);
TabRequest request = new TabRequest();
request.setGroupId("ROOT");
request.setPaginatorPage(1);
request.setPaginatorPageSize(10);
GroupsTabResponse response = groupsTabResponseBuilder.getGroupsTab(request);
assertNotNull(response.getBreadcrumbs());
assertNotNull(response.getGroupsPanel());
assertEquals(Permission.ADMIN, response.getPermission());
}
}
package it.inaf.ia2.gms.service;
import it.inaf.ia2.gms.model.Permission;
import it.inaf.ia2.gms.persistence.PermissionsDAO;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
import it.inaf.ia2.gms.persistence.model.PermissionEntity;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class PermissionsServiceTest {
@Mock
private PermissionsDAO permissionsDAO;
@InjectMocks
private PermissionsService permissionsService;
@Test
public void testAddPermissionOverrideOldOne() {
GroupEntity root = new GroupEntity();
root.setId("ROOT");
PermissionEntity existingEntity = new PermissionEntity();
existingEntity.setGroupId("ROOT");
existingEntity.setPermission(Permission.VIEW_MEMBERS);
when(permissionsDAO.findPermissionEntity(eq("ROOT"), eq("user_id")))
.thenReturn(Optional.of(existingEntity));
when(permissionsDAO.createOrUpdatePermission(any())).thenAnswer(invocation -> invocation.getArgument(0));
PermissionEntity result = permissionsService.addPermission(root, "user_id", Permission.ADMIN);
ArgumentCaptor<PermissionEntity> entityCaptor = ArgumentCaptor.forClass(PermissionEntity.class);
verify(permissionsDAO, times(1)).createOrUpdatePermission(entityCaptor.capture());
assertEquals(Permission.ADMIN, entityCaptor.getValue().getPermission());
assertEquals(Permission.ADMIN, result.getPermission());
}
@Test
public void testAddPermissionNotOverrideOldOne() {
GroupEntity root = new GroupEntity();
root.setId("ROOT");
PermissionEntity existingEntity = new PermissionEntity();
existingEntity.setGroupId("ROOT");
existingEntity.setPermission(Permission.MANAGE_MEMBERS);
when(permissionsDAO.findPermissionEntity(eq("ROOT"), eq("user_id")))
.thenReturn(Optional.of(existingEntity));
PermissionEntity result = permissionsService.addPermission(root, "user_id", Permission.VIEW_MEMBERS);
verify(permissionsDAO, never()).createOrUpdatePermission(any());
assertEquals(Permission.MANAGE_MEMBERS, result.getPermission());
}
@Test
public void testAddSamePermissionReturnExisting() {
GroupEntity root = new GroupEntity();
root.setId("ROOT");
PermissionEntity existingEntity = new PermissionEntity();
existingEntity.setGroupId("ROOT");
existingEntity.setPermission(Permission.VIEW_MEMBERS);
when(permissionsDAO.findPermissionEntity(eq("ROOT"), eq("user_id")))
.thenReturn(Optional.of(existingEntity));
PermissionEntity result = permissionsService.addPermission(root, "user_id", Permission.VIEW_MEMBERS);
verify(permissionsDAO, never()).createOrUpdatePermission(any());
assertEquals(Permission.VIEW_MEMBERS, result.getPermission());
}
}
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