Skip to content

Commit 8d16794

Browse files
committed
RSDEV-446: moving failing junit tests to IT tests (part 4 - final)
1 parent e405dd7 commit 8d16794

File tree

2 files changed

+206
-98
lines changed

2 files changed

+206
-98
lines changed

src/test/java/com/researchspace/service/RecordSharingIT.java

+206-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import static org.junit.Assert.assertTrue;
88

99
import com.researchspace.Constants;
10-
import com.researchspace.dao.GroupDao;
10+
import com.researchspace.core.util.ISearchResults;
11+
import com.researchspace.core.util.SearchResultsImpl;
1112
import com.researchspace.dao.RecordGroupSharingDao;
1213
import com.researchspace.model.Community;
1314
import com.researchspace.model.EditStatus;
@@ -16,6 +17,7 @@
1617
import com.researchspace.model.Role;
1718
import com.researchspace.model.RoleInGroup;
1819
import com.researchspace.model.User;
20+
import com.researchspace.model.core.RecordType;
1921
import com.researchspace.model.dtos.ShareConfigElement;
2022
import com.researchspace.model.permissions.ConstraintBasedPermission;
2123
import com.researchspace.model.permissions.DefaultPermissionFactory;
@@ -35,6 +37,9 @@
3537
import com.researchspace.testutils.RSpaceTestUtils;
3638
import com.researchspace.testutils.RealTransactionSpringTestBase;
3739
import com.researchspace.testutils.SpringTransactionalTest;
40+
import com.researchspace.webapp.controller.ActionPermissionsDTO;
41+
import com.researchspace.webapp.controller.WorkspacePermissionsDTOBuilder;
42+
import java.util.Arrays;
3843
import java.util.HashSet;
3944
import java.util.LinkedList;
4045
import java.util.List;
@@ -43,6 +48,7 @@
4348
import org.junit.Before;
4449
import org.junit.Test;
4550
import org.springframework.beans.factory.annotation.Autowired;
51+
import org.springframework.ui.ExtendedModelMap;
4652

4753
/**
4854
* This is for tests with object relations and multiple transactions that are too complicated to run
@@ -52,13 +58,16 @@ public class RecordSharingIT extends RealTransactionSpringTestBase {
5258

5359
private @Autowired RecordGroupSharingDao groupShareDao;
5460
private @Autowired RecordSharingManager recordSharingManager;
61+
private @Autowired WorkspacePermissionsDTOBuilder permBuilder;
62+
5563
private Group group;
5664
private User piUser;
5765
private User other;
5866

5967
@Before
6068
public void setUp() throws Exception {
6169
super.setUp();
70+
permBuilder.setRecMgr(recordMgr);
6271
piUser = doCreateAndInitUser(getRandomAlphabeticString("pi"), Constants.PI_ROLE);
6372
initialiseContentWithEmptyContent(piUser);
6473
}
@@ -333,7 +342,7 @@ public void deletionOfDocumentSharedIntoOtherNotebook() throws Exception {
333342
addUsersToGroup(pi, labGroup2, user);
334343

335344
// user creates a notebook and shares it for write with both groups
336-
user = userMgr.get(user.getId());
345+
user = reloadUser(user);
337346
logoutAndLoginAs(user);
338347
Long userRootId = user.getRootFolder().getId();
339348

@@ -354,7 +363,7 @@ public void deletionOfDocumentSharedIntoOtherNotebook() throws Exception {
354363
sharedNotebook = folderMgr.getNotebook(sharedNotebook.getId());
355364
labGroup = grpMgr.getGroup(labGroup.getId());
356365
labGroup2 = grpMgr.getGroup(labGroup2.getId());
357-
pi = userMgr.get(pi.getId());
366+
pi = reloadUser(pi);
358367

359368
shareRecordIntoGroupNotebook(doc, sharedNotebook, labGroup, pi);
360369
shareRecordIntoGroupNotebook(doc, sharedNotebook, labGroup2, pi);
@@ -498,8 +507,201 @@ public void movePermissionsInGrpFolder() throws Exception {
498507
recordMgr.move(others.getId(), subf.getId(), grpFolder.getId(), piUser).isSucceeded());
499508
}
500509

510+
@Test
511+
public void ownerDeletesNotebookAndGroupCanNoLongerView() throws Exception {
512+
User owner = createAndSaveUserIfNotExists(getRandomAlphabeticString("owner"), Constants.USER_ROLE);
513+
User sharee = createAndSaveUserIfNotExists(getRandomAlphabeticString("sharee"), Constants.USER_ROLE);
514+
515+
openTransaction();
516+
initialiseContentWithEmptyContent(owner, sharee);
517+
commitTransaction();
518+
519+
User sysadmin = logoutAndLoginAsSysAdmin();
520+
group = createGroupForUsers(sysadmin, piUser.getUsername(), "", owner, sharee, piUser);
521+
assertTrue(group.getPiusers().contains(piUser));
522+
523+
524+
// login as owner, create a notebook and share it
525+
logoutAndLoginAs(owner);
526+
owner = reloadUser(owner);
527+
openTransaction();
528+
Notebook notebook = createNotebookWithNEntries(owner.getRootFolder().getId(), "any", 2, owner);
529+
StructuredDocument notebookEntry = getFirstEntryInNotebook(notebook);
530+
commitTransaction();
531+
shareNotebookWithGroup(owner, notebook, group, "write");
532+
533+
// sharee can see notebook and entry
534+
logoutAndLoginAs(sharee);
535+
sharee = reloadUser(sharee);
536+
537+
openTransaction();
538+
notebook = folderMgr.getNotebook(notebook.getId());
539+
notebookEntry = getFirstEntryInNotebook(notebook);
540+
userCanViewNotebookAndEntry(sharee, notebook, notebookEntry);
541+
commitTransaction();
542+
543+
// owner *deletes* the record
544+
logoutAndLoginAs(owner);
545+
openTransaction();
546+
owner = reloadUser(owner);
547+
recordDeletionMgr.deleteFolder(owner.getRootFolder().getId(), notebook.getId(), owner);
548+
commitTransaction();
549+
550+
// sharee cannot see notebook and entry->deletion == unsharing as well
551+
logoutAndLoginAs(sharee);
552+
sharee = reloadUser(sharee);
553+
554+
openTransaction();
555+
notebook = folderMgr.getNotebook(notebook.getId());
556+
notebookEntry = getFirstEntryInNotebook(notebook);
557+
userCannotViewNotebookOrEntry(sharee, notebook, notebookEntry);
558+
commitTransaction();
559+
}
560+
561+
@Test
562+
public void shareUnshareNotebookAndNotebookEntry() throws InterruptedException {
563+
User owner = createAndSaveUserIfNotExists(getRandomAlphabeticString("owner"), Constants.USER_ROLE);
564+
User sharee = createAndSaveUserIfNotExists(getRandomAlphabeticString("sharee"), Constants.USER_ROLE);
565+
566+
openTransaction();
567+
initialiseContentWithEmptyContent(owner, sharee);
568+
commitTransaction();
569+
570+
User sysadmin = logoutAndLoginAsSysAdmin();
571+
group = createGroupForUsers(sysadmin, piUser.getUsername(), "", owner, sharee, piUser);
572+
assertTrue(group.getPiusers().contains(piUser));
573+
574+
// login as owner, create a notebook
575+
logoutAndLoginAs(owner);
576+
owner = reloadUser(owner);
577+
578+
openTransaction();
579+
Notebook nb = createNotebookWithNEntries(owner.getRootFolder().getId(), "any", 2, owner);
580+
StructuredDocument notebookentry = getFirstEntryInNotebook(nb);
581+
assertEquals(0, folderDao.getSharedFolderForGroup(group).getChildrens().size());
582+
commitTransaction();
583+
584+
// share an entry
585+
shareRecordWithGroup(owner, group, notebookentry);
586+
587+
openTransaction();
588+
assertTrue(folderDao.getSharedFolderForGroup(group).getChildrens().contains(notebookentry));
589+
commitTransaction();
590+
591+
// check shared entry can be moved by pi.
592+
logoutAndLoginAs(piUser);
593+
594+
openTransaction();
595+
Folder shared = folderDao.getSharedFolderForGroup(group);
596+
commitTransaction();;
597+
598+
List<BaseRecord> reslist = Arrays.asList(new BaseRecord[] {notebookentry});
599+
ISearchResults<BaseRecord> res = new SearchResultsImpl<BaseRecord>(reslist, 0, 1L, 1);
600+
piUser = reloadPiUser(); // refresh
601+
602+
// share the whole notebook
603+
logoutAndLoginAs(owner);
604+
shareNotebookWithGroup(owner, nb, group, "write");
605+
606+
openTransaction();
607+
assertTrue(folderDao.getSharedFolderForGroup(group).getChildrens().contains(notebookentry));
608+
assertEquals(2, folderDao.getSharedFolderForGroup(group).getChildrens().size());
609+
commitTransaction();
610+
611+
// now unshare the notebook.. should unshare the entry as well
612+
unshareRecordORNotebookWithGroup(owner, nb, group, "write");
613+
614+
openTransaction();
615+
assertEquals(0, folderDao.getSharedFolderForGroup(group).getChildrens().size());
616+
// neither are shared
617+
assertFalse(
618+
groupShareDao.isRecordAlreadySharedInGroup(group.getId(), nb.getId(), notebookentry.getId()));
619+
nb = folderMgr.getNotebook(nb.getId());
620+
assertEquals(2, nb.getChildren().size()); // still has 2 entries in
621+
commitTransaction();
622+
// original notebook
623+
}
624+
625+
@Test
626+
public void basicShareUnshareWithGroupAffectsShareesViewPermissions()
627+
throws InterruptedException {
628+
User owner = createAndSaveUserIfNotExists(getRandomAlphabeticString("owner"), Constants.USER_ROLE);
629+
User sharee = createAndSaveUserIfNotExists(getRandomAlphabeticString("sharee"), Constants.USER_ROLE);
630+
631+
openTransaction();
632+
initialiseContentWithEmptyContent(owner, sharee);
633+
commitTransaction();
634+
635+
User sysadmin = logoutAndLoginAsSysAdmin();
636+
group = createGroupForUsers(sysadmin, piUser.getUsername(), "", owner, sharee, piUser);
637+
assertTrue(group.getPiusers().contains(piUser));
638+
639+
logoutAndLoginAs(owner);
640+
owner = reloadUser(owner);
641+
642+
openTransaction();
643+
Notebook notebook = createNotebookWithNEntries(owner.getRootFolder().getId(), "any", 2, owner);
644+
StructuredDocument notebookentry = (StructuredDocument) notebook.getChildren().iterator().next().getRecord();
645+
commitTransaction();
646+
647+
// sharee can't see owner's notebook or entry because it's not shared
648+
// yet
649+
logoutAndLoginAs(sharee);
650+
sharee = reloadUser(sharee);
651+
userCannotViewNotebookOrEntry(sharee, notebook, notebookentry);
652+
653+
// login as owner and share notebook
654+
logoutAndLoginAs(owner);
655+
notebook = shareNotebookWithGroup(owner, notebook, group, "write").get();
656+
// should not acquire shared folder status
657+
assertFalse(notebook.hasType(RecordType.SHARED_FOLDER));
658+
assertTrue(notebook.hasType(RecordType.NOTEBOOK));
659+
660+
// sharee can see notebook and entry
661+
logoutAndLoginAs(sharee);
662+
663+
openTransaction();
664+
sharee = reloadUser(sharee);
665+
notebook = folderMgr.getNotebook(notebook.getId());
666+
notebookentry = (StructuredDocument) notebook.getChildren().iterator().next().getRecord();
667+
userCanViewNotebookAndEntry(sharee, notebook, notebookentry);
668+
commitTransaction();
669+
670+
// owner unshares the record
671+
logoutAndLoginAs(owner);
672+
unshareRecordORNotebookWithGroup(owner, notebook, group, "write");
673+
logoutAndLoginAs(sharee);
674+
675+
openTransaction();
676+
sharee = reloadUser(sharee);
677+
notebook = folderMgr.getNotebook(notebook.getId());
678+
notebookentry = (StructuredDocument) notebook.getChildren().iterator().next().getRecord();
679+
userCannotViewNotebookOrEntry(sharee, notebook, notebookentry);
680+
commitTransaction();
681+
}
682+
683+
private User reloadUser(User userToReload) {
684+
return userMgr.get(userToReload.getId());
685+
}
686+
501687
private User reloadPiUser() {
502-
return userMgr.get(piUser.getId());
688+
return reloadUser(piUser);
689+
}
690+
691+
private StructuredDocument getFirstEntryInNotebook(Notebook nb) {
692+
return (StructuredDocument) nb.getChildren().iterator().next().getRecord();
693+
}
694+
695+
private void userCanViewNotebookAndEntry(
696+
User subject, Notebook nb, StructuredDocument notebookentry) {
697+
assertTrue(permissionUtils.isPermitted(notebookentry, PermissionType.READ, subject));
698+
assertTrue(permissionUtils.isPermitted(nb, PermissionType.READ, subject));
699+
}
700+
701+
private void userCannotViewNotebookOrEntry(
702+
User sharee, Notebook nb, StructuredDocument notebookentry) {
703+
assertFalse(permissionUtils.isPermitted(nb, PermissionType.READ, sharee));
704+
assertFalse(permissionUtils.isPermitted(notebookentry, PermissionType.READ, sharee));
503705
}
504706

505707
private ShareConfigElement[] getGrpShareCommand(Group g, String perm) {

0 commit comments

Comments
 (0)