From d997f14565d12365d4a4c0f254118f5aa99d8e96 Mon Sep 17 00:00:00 2001 From: suzanadirla Date: Fri, 23 Feb 2018 19:38:08 +0200 Subject: [PATCH] =?UTF-8?q?[ADF-2077]=20[Destination=20picker]=20User=20sh?= =?UTF-8?q?ould=20not=20be=20able=20to=20copy=20/=20mo=E2=80=A6=20(#2978)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [ADF-20177] [Destination picker] User should not be able to copy / move items into a site outside of the documentLibrary * [ADF-20177] added unit tests for the change * [ADF-2077] refactor code and unit tests after code review * [ADF-2077] more refactoring to make the unit tests messages more clear --- .../content-node-dialog.service.spec.ts | 86 +++++++++++++++++-- .../content-node-dialog.service.ts | 10 ++- 2 files changed, 89 insertions(+), 7 deletions(-) diff --git a/lib/content-services/content-node-selector/content-node-dialog.service.spec.ts b/lib/content-services/content-node-selector/content-node-dialog.service.spec.ts index dbc2adc35f..b22f1ccbc8 100644 --- a/lib/content-services/content-node-selector/content-node-dialog.service.spec.ts +++ b/lib/content-services/content-node-selector/content-node-dialog.service.spec.ts @@ -55,6 +55,7 @@ describe('ContentNodeDialogService', () => { let documentListService: DocumentListService; let sitesService: SitesService; let materialDialog: MatDialog; + let spyOnDialogOpen: jasmine.Spy; beforeEach(async(() => { TestBed.configureTestingModule({ @@ -76,7 +77,7 @@ describe('ContentNodeDialogService', () => { documentListService = TestBed.get(DocumentListService); materialDialog = TestBed.get(MatDialog); sitesService = TestBed.get(SitesService); - spyOn(materialDialog, 'open').and.stub(); + spyOnDialogOpen = spyOn(materialDialog, 'open').and.stub(); spyOn(materialDialog, 'closeAll').and.stub(); }); @@ -87,14 +88,14 @@ describe('ContentNodeDialogService', () => { it('should be able to open the dialog when node has permission', () => { service.openCopyMoveDialog('fake-action', fakeNode, '!update'); - expect(materialDialog.open).toHaveBeenCalled(); + expect(spyOnDialogOpen).toHaveBeenCalled(); }); it('should NOT be able to open the dialog when node has NOT permission', () => { service.openCopyMoveDialog('fake-action', fakeNode, 'noperm').subscribe( () => { }, (error) => { - expect(materialDialog.open).not.toHaveBeenCalled(); + expect(spyOnDialogOpen).not.toHaveBeenCalled(); expect(JSON.parse(error.message).error.statusCode).toBe(403); }); }); @@ -103,7 +104,7 @@ describe('ContentNodeDialogService', () => { spyOn(documentListService, 'getFolderNode').and.returnValue(Promise.resolve(fakeNode)); service.openFileBrowseDialogByFolderId('fake-folder-id').subscribe(); tick(); - expect(materialDialog.open).toHaveBeenCalled(); + expect(spyOnDialogOpen).toHaveBeenCalled(); })); it('should be able to open the dialog for files using the first user site', fakeAsync(() => { @@ -111,7 +112,7 @@ describe('ContentNodeDialogService', () => { spyOn(documentListService, 'getFolderNode').and.returnValue(Promise.resolve(fakeNode)); service.openFileBrowseDialogBySite().subscribe(); tick(); - expect(materialDialog.open).toHaveBeenCalled(); + expect(spyOnDialogOpen).toHaveBeenCalled(); })); it('should be able to open the dialog for folder using the first user site', fakeAsync(() => { @@ -119,7 +120,7 @@ describe('ContentNodeDialogService', () => { spyOn(documentListService, 'getFolderNode').and.returnValue(Promise.resolve(fakeNode)); service.openFolderBrowseDialogBySite().subscribe(); tick(); - expect(materialDialog.open).toHaveBeenCalled(); + expect(spyOnDialogOpen).toHaveBeenCalled(); })); it('should be able to close the material dialog', () => { @@ -127,4 +128,77 @@ describe('ContentNodeDialogService', () => { expect(materialDialog.closeAll).toHaveBeenCalled(); }); + describe('for the copy/move dialog', () => { + const siteNode: MinimalNodeEntryEntity = { + id: 'site-node-id', + nodeType: 'st:site' + }; + const sites: MinimalNodeEntryEntity = { + id: 'sites-id', + nodeType: 'st:sites' + }; + const site: MinimalNodeEntryEntity = { + id: 'site-id', + guid: 'any-guid' + }; + const nodeEntryWithRightPermissions: MinimalNodeEntryEntity = { + id: 'node-id', + allowableOperations: ['create'] + }; + const nodeEntryNoPermissions: MinimalNodeEntryEntity = { + id: 'node-id', + allowableOperations: [] + }; + + const siteFixture = [ + { + node: siteNode, + expected: false + }, + { + node: sites, + expected: false + }, + { + node: site, + expected: false + } + ]; + + const permissionFixture = [ + { + node: nodeEntryWithRightPermissions, + expected: true + }, + { + node: nodeEntryNoPermissions, + expected: false + } + ]; + + let testContentNodeSelectorComponentData; + + beforeEach(() => { + spyOnDialogOpen.and.callFake((contentNodeSelectorComponent: any, config: any) => { + testContentNodeSelectorComponentData = config.data; + return {componentInstance: {}}; + }); + service.openCopyMoveDialog('fake-action', fakeNode, '!update'); + }); + + it('should NOT allow selection for sites', () => { + expect(spyOnDialogOpen.calls.count()).toEqual(1); + siteFixture.forEach((testData) => { + expect(testContentNodeSelectorComponentData.isSelectionValid(testData.node)).toBe(testData.expected); + }); + }); + + it('should allow selection only for nodes with the right permission', () => { + expect(spyOnDialogOpen.calls.count()).toEqual(1); + permissionFixture.forEach((testData) => { + expect(testContentNodeSelectorComponentData.isSelectionValid(testData.node)).toBe(testData.expected); + }); + }); + }); + }); diff --git a/lib/content-services/content-node-selector/content-node-dialog.service.ts b/lib/content-services/content-node-selector/content-node-dialog.service.ts index 5bd13790fe..ffc56398d7 100644 --- a/lib/content-services/content-node-selector/content-node-dialog.service.ts +++ b/lib/content-services/content-node-selector/content-node-dialog.service.ts @@ -88,7 +88,7 @@ export class ContentNodeDialogService { currentFolderId: contentEntry.parentId, imageResolver: this.imageResolver.bind(this), rowFilter : this.rowFilter.bind(this, contentEntry.id), - isSelectionValid: this.hasEntityCreatePermission.bind(this), + isSelectionValid: this.isCopyMoveSelectionValid.bind(this), select: select }; @@ -188,10 +188,18 @@ export class ContentNodeDialogService { return entry.isFolder; } + private isCopyMoveSelectionValid(entry: MinimalNodeEntryEntity): boolean { + return this.hasEntityCreatePermission(entry) && !this.isSite(entry); + } + private hasEntityCreatePermission(entry: MinimalNodeEntryEntity): boolean { return this.contentService.hasPermission(entry, 'create'); } + private isSite(entry) { + return !!entry.guid || entry.nodeType === 'st:site' || entry.nodeType === 'st:sites'; + } + /** Closes the currently open dialog. */ close() { this.dialog.closeAll();