forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 2
JCU/Prevent an admin from deleting their own account #1447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
src/app/access-control/epeople-registry/eperson-delete-guard.service.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import { | ||
| fakeAsync, | ||
| TestBed, | ||
| tick, | ||
| } from '@angular/core/testing'; | ||
| import { TranslateService } from '@ngx-translate/core'; | ||
| import { | ||
| of, | ||
| throwError as observableThrowError, | ||
| } from 'rxjs'; | ||
|
|
||
| import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service'; | ||
| import { FeatureID } from '../../core/data/feature-authorization/feature-id'; | ||
| import { buildPaginatedList } from '../../core/data/paginated-list.model'; | ||
| import { DSpaceObject } from '../../core/shared/dspace-object.model'; | ||
| import { PageInfo } from '../../core/shared/page-info.model'; | ||
| import { SearchService } from '../../core/shared/search/search.service'; | ||
| import { WorkflowItemDataService } from '../../core/submission/workflowitem-data.service'; | ||
| import { WorkspaceitemDataService } from '../../core/submission/workspaceitem-data.service'; | ||
| import { NotificationsService } from '../../shared/notifications/notifications.service'; | ||
| import { | ||
| createFailedRemoteDataObject$, | ||
| createSuccessfulRemoteDataObject$, | ||
| } from '../../shared/remote-data.utils'; | ||
| import { SearchObjects } from '../../shared/search/models/search-objects.model'; | ||
| import { EPersonMock } from '../../shared/testing/eperson.mock'; | ||
| import { NotificationsServiceStub } from '../../shared/testing/notifications-service.stub'; | ||
| import { EPersonDeleteGuardService } from './eperson-delete-guard.service'; | ||
|
|
||
| describe('EPersonDeleteGuardService', () => { | ||
| let service: EPersonDeleteGuardService; | ||
| let authorizationService: jasmine.SpyObj<AuthorizationDataService>; | ||
| let workspaceItemDataService: jasmine.SpyObj<WorkspaceitemDataService>; | ||
| let workflowItemDataService: jasmine.SpyObj<WorkflowItemDataService>; | ||
| let searchService: jasmine.SpyObj<SearchService>; | ||
| let notificationsService: NotificationsServiceStub; | ||
| let translateService: jasmine.SpyObj<TranslateService>; | ||
|
|
||
| const remoteList = (totalElements: number) => createSuccessfulRemoteDataObject$( | ||
| buildPaginatedList(new PageInfo({ elementsPerPage: 1, totalElements, totalPages: 1, currentPage: 1 }), []), | ||
| ); | ||
| const searchObjects = (totalElements: number) => createSuccessfulRemoteDataObject$(Object.assign( | ||
| new SearchObjects<DSpaceObject>(), | ||
| buildPaginatedList(new PageInfo({ elementsPerPage: 1, totalElements, totalPages: 1, currentPage: 1 }), []), | ||
| )); | ||
|
|
||
| beforeEach(() => { | ||
| authorizationService = jasmine.createSpyObj('authorizationService', ['isAuthorized']); | ||
| authorizationService.isAuthorized.and.returnValue(of(false)); | ||
| workspaceItemDataService = jasmine.createSpyObj('workspaceItemDataService', ['searchBy']); | ||
| workspaceItemDataService.searchBy.and.returnValue(remoteList(0)); | ||
| workflowItemDataService = jasmine.createSpyObj('workflowItemDataService', ['searchBy']); | ||
| workflowItemDataService.searchBy.and.returnValue(remoteList(0)); | ||
| searchService = jasmine.createSpyObj('searchService', ['search']); | ||
| searchService.search.and.returnValue(searchObjects(0)); | ||
| notificationsService = new NotificationsServiceStub(); | ||
| translateService = jasmine.createSpyObj('translateService', ['get']); | ||
| translateService.get.and.callFake((key: string) => of(key)); | ||
|
|
||
| TestBed.configureTestingModule({ | ||
| providers: [ | ||
| EPersonDeleteGuardService, | ||
| { provide: AuthorizationDataService, useValue: authorizationService }, | ||
| { provide: WorkspaceitemDataService, useValue: workspaceItemDataService }, | ||
| { provide: WorkflowItemDataService, useValue: workflowItemDataService }, | ||
| { provide: SearchService, useValue: searchService }, | ||
| { provide: NotificationsService, useValue: notificationsService }, | ||
| { provide: TranslateService, useValue: translateService }, | ||
| ], | ||
| }); | ||
| service = TestBed.inject(EPersonDeleteGuardService); | ||
| }); | ||
|
|
||
| describe('isCurrentUser', () => { | ||
| it('is true only when the ids match', () => { | ||
| expect(service.isCurrentUser(EPersonMock, EPersonMock.id)).toBeTrue(); | ||
| expect(service.isCurrentUser(EPersonMock, 'someone-else')).toBeFalse(); | ||
| expect(service.isCurrentUser(undefined, EPersonMock.id)).toBeFalsy(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getDeleteWarningLabel', () => { | ||
| it('returns undefined when the user is neither a submitter nor an admin', fakeAsync(() => { | ||
| let label: string | undefined = 'unset'; | ||
| service.getDeleteWarningLabel(EPersonMock).subscribe((value) => label = value); | ||
| tick(); | ||
| expect(label).toBeUndefined(); | ||
| })); | ||
|
|
||
| it('returns the submitter warning when the user has submitted items', fakeAsync(() => { | ||
| workspaceItemDataService.searchBy.and.returnValue(remoteList(1)); | ||
| let label: string; | ||
| service.getDeleteWarningLabel(EPersonMock).subscribe((value) => label = value); | ||
| tick(); | ||
| expect(label).toBe('admin.access-control.epeople.delete.warning.submitter'); | ||
| })); | ||
|
|
||
| it('returns the admin warning, querying the AdministratorOf feature for the target user', fakeAsync(() => { | ||
| authorizationService.isAuthorized.and.returnValue(of(true)); | ||
| let label: string; | ||
| service.getDeleteWarningLabel(EPersonMock).subscribe((value) => label = value); | ||
| tick(); | ||
| expect(authorizationService.isAuthorized).toHaveBeenCalledWith(FeatureID.AdministratorOf, undefined, EPersonMock.id); | ||
| expect(label).toBe('admin.access-control.epeople.delete.warning.admin'); | ||
| })); | ||
|
|
||
| it('returns the combined warning when both apply', fakeAsync(() => { | ||
| workspaceItemDataService.searchBy.and.returnValue(remoteList(1)); | ||
| authorizationService.isAuthorized.and.returnValue(of(true)); | ||
| let label: string; | ||
| service.getDeleteWarningLabel(EPersonMock).subscribe((value) => label = value); | ||
| tick(); | ||
| expect(label).toBe('admin.access-control.epeople.delete.warning.submitterAndAdmin'); | ||
| })); | ||
|
|
||
| it('degrades each probe to false on error so a failed lookup never blocks the delete', fakeAsync(() => { | ||
| workspaceItemDataService.searchBy.and.returnValue(observableThrowError(() => new Error('boom'))); | ||
| searchService.search.and.returnValue(observableThrowError(() => new Error('boom'))); | ||
| authorizationService.isAuthorized.and.returnValue(observableThrowError(() => new Error('boom'))); | ||
| let emitted = false; | ||
| let label: string | undefined = 'unset'; | ||
| service.getDeleteWarningLabel(EPersonMock).subscribe((value) => { | ||
| emitted = true; | ||
| label = value; | ||
| }); | ||
| tick(); | ||
| expect(emitted).toBeTrue(); | ||
| expect(label).toBeUndefined(); | ||
| })); | ||
| }); | ||
|
|
||
| describe('isSelfDeletionError', () => { | ||
| it('recognises the backend self-delete rejection', fakeAsync(() => { | ||
| let rd; | ||
| createFailedRemoteDataObject$('You, as admin user, cannot delete yourself', 400).subscribe((value) => rd = value); | ||
| tick(); | ||
| expect(service.isSelfDeletionError(rd)).toBeTrue(); | ||
| })); | ||
|
|
||
| it('ignores other failures', fakeAsync(() => { | ||
| let rd; | ||
| createFailedRemoteDataObject$('server error', 500).subscribe((value) => rd = value); | ||
| tick(); | ||
| expect(service.isSelfDeletionError(rd)).toBeFalsy(); | ||
| expect(service.isSelfDeletionError(null)).toBeFalsy(); | ||
| })); | ||
| }); | ||
|
|
||
| describe('showSelfDeleteNotification', () => { | ||
| it('emits the self-delete error notification', () => { | ||
| service.showSelfDeleteNotification(); | ||
| expect(notificationsService.error).toHaveBeenCalled(); | ||
| let translatedKey: string; | ||
| notificationsService.error.calls.mostRecent().args[0].subscribe((value) => translatedKey = value); | ||
| expect(translatedKey).toBe('admin.access-control.epeople.notification.deleted.forbidden.self'); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.