7
7
import static org .junit .Assert .assertTrue ;
8
8
9
9
import com .researchspace .Constants ;
10
- import com .researchspace .dao .GroupDao ;
10
+ import com .researchspace .core .util .ISearchResults ;
11
+ import com .researchspace .core .util .SearchResultsImpl ;
11
12
import com .researchspace .dao .RecordGroupSharingDao ;
12
13
import com .researchspace .model .Community ;
13
14
import com .researchspace .model .EditStatus ;
16
17
import com .researchspace .model .Role ;
17
18
import com .researchspace .model .RoleInGroup ;
18
19
import com .researchspace .model .User ;
20
+ import com .researchspace .model .core .RecordType ;
19
21
import com .researchspace .model .dtos .ShareConfigElement ;
20
22
import com .researchspace .model .permissions .ConstraintBasedPermission ;
21
23
import com .researchspace .model .permissions .DefaultPermissionFactory ;
35
37
import com .researchspace .testutils .RSpaceTestUtils ;
36
38
import com .researchspace .testutils .RealTransactionSpringTestBase ;
37
39
import com .researchspace .testutils .SpringTransactionalTest ;
40
+ import com .researchspace .webapp .controller .ActionPermissionsDTO ;
41
+ import com .researchspace .webapp .controller .WorkspacePermissionsDTOBuilder ;
42
+ import java .util .Arrays ;
38
43
import java .util .HashSet ;
39
44
import java .util .LinkedList ;
40
45
import java .util .List ;
43
48
import org .junit .Before ;
44
49
import org .junit .Test ;
45
50
import org .springframework .beans .factory .annotation .Autowired ;
51
+ import org .springframework .ui .ExtendedModelMap ;
46
52
47
53
/**
48
54
* 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 {
52
58
53
59
private @ Autowired RecordGroupSharingDao groupShareDao ;
54
60
private @ Autowired RecordSharingManager recordSharingManager ;
61
+ private @ Autowired WorkspacePermissionsDTOBuilder permBuilder ;
62
+
55
63
private Group group ;
56
64
private User piUser ;
57
65
private User other ;
58
66
59
67
@ Before
60
68
public void setUp () throws Exception {
61
69
super .setUp ();
70
+ permBuilder .setRecMgr (recordMgr );
62
71
piUser = doCreateAndInitUser (getRandomAlphabeticString ("pi" ), Constants .PI_ROLE );
63
72
initialiseContentWithEmptyContent (piUser );
64
73
}
@@ -333,7 +342,7 @@ public void deletionOfDocumentSharedIntoOtherNotebook() throws Exception {
333
342
addUsersToGroup (pi , labGroup2 , user );
334
343
335
344
// user creates a notebook and shares it for write with both groups
336
- user = userMgr . get (user . getId () );
345
+ user = reloadUser (user );
337
346
logoutAndLoginAs (user );
338
347
Long userRootId = user .getRootFolder ().getId ();
339
348
@@ -354,7 +363,7 @@ public void deletionOfDocumentSharedIntoOtherNotebook() throws Exception {
354
363
sharedNotebook = folderMgr .getNotebook (sharedNotebook .getId ());
355
364
labGroup = grpMgr .getGroup (labGroup .getId ());
356
365
labGroup2 = grpMgr .getGroup (labGroup2 .getId ());
357
- pi = userMgr . get (pi . getId () );
366
+ pi = reloadUser (pi );
358
367
359
368
shareRecordIntoGroupNotebook (doc , sharedNotebook , labGroup , pi );
360
369
shareRecordIntoGroupNotebook (doc , sharedNotebook , labGroup2 , pi );
@@ -498,8 +507,201 @@ public void movePermissionsInGrpFolder() throws Exception {
498
507
recordMgr .move (others .getId (), subf .getId (), grpFolder .getId (), piUser ).isSucceeded ());
499
508
}
500
509
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
+
501
687
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 ));
503
705
}
504
706
505
707
private ShareConfigElement [] getGrpShareCommand (Group g , String perm ) {
0 commit comments