Skip to content

Commit e2255a0

Browse files
committed
Adapt DSO selectors to use new authorization methods
1 parent 04f0c58 commit e2255a0

19 files changed

+316
-48
lines changed

src/app/shared/dso-selector/dso-selector/authorized-collection-selector/authorized-collection-selector.component.spec.ts

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { RouterTestingModule } from '@angular/router/testing';
88
import { CollectionDataService } from '@dspace/core/data/collection-data.service';
99
import { NotificationsService } from '@dspace/core/notification-system/notifications.service';
10+
import { ActionType } from '@dspace/core/resource-policy/models/action-type.model';
1011
import { Collection } from '@dspace/core/shared/collection.model';
1112
import { DSpaceObjectType } from '@dspace/core/shared/dspace-object-type.model';
1213
import { createPaginatedList } from '@dspace/core/testing/utils.test';
@@ -33,7 +34,9 @@ describe('AuthorizedCollectionSelectorComponent', () => {
3334
id: 'authorized-collection',
3435
});
3536
collectionService = jasmine.createSpyObj('collectionService', {
36-
getAuthorizedCollection: createSuccessfulRemoteDataObject$(createPaginatedList([collection])),
37+
getSubmitAuthorizedCollection: createSuccessfulRemoteDataObject$(createPaginatedList([collection])),
38+
getAdminAuthorizedCollection: createSuccessfulRemoteDataObject$(createPaginatedList([collection])),
39+
getEditAuthorizedCollection: createSuccessfulRemoteDataObject$(createPaginatedList([collection])),
3740
getAuthorizedCollectionByEntityType: createSuccessfulRemoteDataObject$(createPaginatedList([collection])),
3841
});
3942
notificationsService = jasmine.createSpyObj('notificationsService', ['error']);
@@ -60,42 +63,57 @@ describe('AuthorizedCollectionSelectorComponent', () => {
6063
});
6164

6265
describe('search', () => {
63-
describe('when has no entity type', () => {
64-
it('should call getAuthorizedCollection and return the authorized collection in a SearchResult', (done) => {
65-
component.search('', 1).subscribe((resultRD) => {
66-
expect(collectionService.getAuthorizedCollection).toHaveBeenCalled();
67-
expect(resultRD.payload.page.length).toEqual(1);
68-
expect(resultRD.payload.page[0].indexableObject).toEqual(collection);
69-
done();
66+
describe('when action type is ADD', () => {
67+
describe('when has no entity type', () => {
68+
it('should call getSubmitAuthorizedCollection and return the authorized collection in a SearchResult', (done) => {
69+
component.action = ActionType.ADD;
70+
fixture.detectChanges();
71+
component.search('', 1).subscribe((resultRD) => {
72+
expect(collectionService.getSubmitAuthorizedCollection).toHaveBeenCalled();
73+
expect(resultRD.payload.page.length).toEqual(1);
74+
expect(resultRD.payload.page[0].indexableObject).toEqual(collection);
75+
done();
76+
});
77+
});
78+
});
79+
80+
describe('when has entity type', () => {
81+
it('should call getAuthorizedCollectionByEntityType and return the authorized collection in a SearchResult', (done) => {
82+
component.entityType = 'test';
83+
component.action = ActionType.ADD;
84+
fixture.detectChanges();
85+
component.search('', 1).subscribe((resultRD) => {
86+
expect(collectionService.getAuthorizedCollectionByEntityType).toHaveBeenCalled();
87+
expect(resultRD.payload.page.length).toEqual(1);
88+
expect(resultRD.payload.page[0].indexableObject).toEqual(collection);
89+
done();
90+
});
7091
});
7192
});
7293
});
7394

74-
describe('when has entity type', () => {
75-
it('should call getAuthorizedCollectionByEntityType and return the authorized collection in a SearchResult', (done) => {
76-
component.entityType = 'test';
95+
describe('when action type is WRITE', () => {
96+
it('should call getEditAuthorizedCollection', (done) => {
97+
component.action = ActionType.WRITE;
7798
fixture.detectChanges();
7899
component.search('', 1).subscribe((resultRD) => {
79-
expect(collectionService.getAuthorizedCollectionByEntityType).toHaveBeenCalled();
100+
expect(collectionService.getEditAuthorizedCollection).toHaveBeenCalled();
80101
expect(resultRD.payload.page.length).toEqual(1);
81102
expect(resultRD.payload.page[0].indexableObject).toEqual(collection);
82103
done();
83104
});
84105
});
85106
});
86107

87-
describe('when using searchHref', () => {
88-
it('should call getAuthorizedCollection with "findAdminAuthorized" when overridden', (done) => {
89-
component.searchHref = 'findAdminAuthorized';
90-
91-
component.search('', 1).subscribe(() => {
92-
expect(collectionService.getAuthorizedCollection).toHaveBeenCalledWith(
93-
'', jasmine.any(Object), true, false, 'findAdminAuthorized', jasmine.anything(),
94-
);
108+
describe('when action is not provided', () => {
109+
it('should call getAdminAuthorizedCollection', (done) => {
110+
component.search('', 1).subscribe((resultRD) => {
111+
expect(collectionService.getAdminAuthorizedCollection).toHaveBeenCalled();
112+
expect(resultRD.payload.page.length).toEqual(1);
113+
expect(resultRD.payload.page[0].indexableObject).toEqual(collection);
95114
done();
96115
});
97116
});
98117
});
99-
100118
});
101119
});

src/app/shared/dso-selector/dso-selector/authorized-collection-selector/authorized-collection-selector.component.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from '@dspace/core/data/paginated-list.model';
2020
import { RemoteData } from '@dspace/core/data/remote-data';
2121
import { NotificationsService } from '@dspace/core/notification-system/notifications.service';
22+
import { ActionType } from '@dspace/core/resource-policy/models/action-type.model';
2223
import { Collection } from '@dspace/core/shared/collection.model';
2324
import { DSpaceObject } from '@dspace/core/shared/dspace-object.model';
2425
import { followLink } from '@dspace/core/shared/follow-link-config.model';
@@ -66,10 +67,9 @@ export class AuthorizedCollectionSelectorComponent extends DSOSelectorComponent
6667
@Input() entityType: string;
6768

6869
/**
69-
* Search endpoint to use for finding authorized collections.
70-
* Defaults to 'findSubmitAuthorized', but can be overridden (e.g. to 'findAdminAuthorized')
70+
* The action type to determine which authorized collections to fetch, defaults to ADMIN
7171
*/
72-
@Input() searchHref = 'findSubmitAuthorized';
72+
@Input() action: ActionType = ActionType.ADMIN;
7373

7474
constructor(
7575
protected searchService: SearchService,
@@ -101,15 +101,24 @@ export class AuthorizedCollectionSelectorComponent extends DSOSelectorComponent
101101
elementsPerPage: this.defaultPagination.pageSize,
102102
};
103103

104-
if (this.entityType) {
104+
if (this.action === ActionType.WRITE) {
105105
searchListService$ = this.collectionDataService
106-
.getAuthorizedCollectionByEntityType(
107-
query,
108-
this.entityType,
109-
findOptions);
106+
.getEditAuthorizedCollection(query, findOptions, useCache, false, followLink('parentCommunity'));
107+
} else if (this.action === ActionType.ADD) {
108+
if (this.entityType) {
109+
searchListService$ = this.collectionDataService
110+
.getAuthorizedCollectionByEntityType(
111+
query,
112+
this.entityType,
113+
findOptions);
114+
} else {
115+
searchListService$ = this.collectionDataService
116+
.getSubmitAuthorizedCollection(query, findOptions, useCache, false, followLink('parentCommunity'));
117+
}
110118
} else {
119+
// By default, search for admin authorized collections
111120
searchListService$ = this.collectionDataService
112-
.getAuthorizedCollection(query, findOptions, useCache, false, this.searchHref, followLink('parentCommunity'));
121+
.getAdminAuthorizedCollection(query, findOptions, useCache, false, followLink('parentCommunity'));
113122
}
114123
return searchListService$.pipe(
115124
getFirstCompletedRemoteData(),

src/app/shared/dso-selector/dso-selector/authorized-community-selector/authorized-community-selector.component.spec.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '@angular/core/testing';
77
import { RouterTestingModule } from '@angular/router/testing';
88
import { NotificationsService } from '@dspace/core/notification-system/notifications.service';
9+
import { ActionType } from '@dspace/core/resource-policy/models/action-type.model';
910
import { createPaginatedList } from '@dspace/core/testing/utils.test';
1011
import { createSuccessfulRemoteDataObject$ } from '@dspace/core/utilities/remote-data.utils';
1112
import { TranslateModule } from '@ngx-translate/core';
@@ -33,7 +34,9 @@ describe('AuthorizedCommunitySelectorComponent', () => {
3334
id: 'authorized-community',
3435
});
3536
communityService = jasmine.createSpyObj('communityService', {
36-
getAuthorizedCommunity: createSuccessfulRemoteDataObject$(createPaginatedList([community])),
37+
getAddAuthorizedCommunity: createSuccessfulRemoteDataObject$(createPaginatedList([community])),
38+
getEditAuthorizedCommunity: createSuccessfulRemoteDataObject$(createPaginatedList([community])),
39+
getAdminAuthorizedCommunity: createSuccessfulRemoteDataObject$(createPaginatedList([community])),
3740
});
3841
notificationsService = jasmine.createSpyObj('notificationsService', ['error']);
3942
TestBed.configureTestingModule({
@@ -59,12 +62,38 @@ describe('AuthorizedCommunitySelectorComponent', () => {
5962
});
6063

6164
describe('search', () => {
62-
it('should call getAuthorizedCommunity and return the authorized community in a SearchResult', (done) => {
63-
component.search('', 1).subscribe((resultRD) => {
64-
expect(communityService.getAuthorizedCommunity).toHaveBeenCalled();
65-
expect(resultRD.payload.page.length).toEqual(1);
66-
expect(resultRD.payload.page[0].indexableObject).toEqual(community);
67-
done();
65+
describe('when action type is ADD', () => {
66+
it('should call getAddAuthorizedCommunity and return the authorized community in a SearchResult', (done) => {
67+
component.action = ActionType.ADD;
68+
fixture.detectChanges();
69+
component.search('', 1).subscribe((resultRD) => {
70+
expect(communityService.getAddAuthorizedCommunity).toHaveBeenCalled();
71+
expect(resultRD.payload.page.length).toEqual(1);
72+
expect(resultRD.payload.page[0].indexableObject).toEqual(community);
73+
done();
74+
});
75+
});
76+
});
77+
describe('when action type is WRITE', () => {
78+
it('should call getEditAuthorizedCommunity and return the authorized community in a SearchResult', (done) => {
79+
component.action = ActionType.WRITE;
80+
fixture.detectChanges();
81+
component.search('', 1).subscribe((resultRD) => {
82+
expect(communityService.getEditAuthorizedCommunity).toHaveBeenCalled();
83+
expect(resultRD.payload.page.length).toEqual(1);
84+
expect(resultRD.payload.page[0].indexableObject).toEqual(community);
85+
done();
86+
});
87+
});
88+
});
89+
describe('when action type is not provided', () => {
90+
it('should call getAdminAuthorizedCommunity and return the authorized community in a SearchResult', (done) => {
91+
component.search('', 1).subscribe((resultRD) => {
92+
expect(communityService.getAdminAuthorizedCommunity).toHaveBeenCalled();
93+
expect(resultRD.payload.page.length).toEqual(1);
94+
expect(resultRD.payload.page[0].indexableObject).toEqual(community);
95+
done();
96+
});
6897
});
6998
});
7099
});

src/app/shared/dso-selector/dso-selector/authorized-community-selector/authorized-community-selector.component.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import {
22
AsyncPipe,
33
NgClass,
44
} from '@angular/common';
5-
import { Component } from '@angular/core';
5+
import {
6+
Component,
7+
Input,
8+
} from '@angular/core';
69
import {
710
FormsModule,
811
ReactiveFormsModule,
912
} from '@angular/forms';
1013
import { NotificationsService } from '@dspace/core/notification-system/notifications.service';
14+
import { ActionType } from '@dspace/core/resource-policy/models/action-type.model';
1115
import { followLink } from '@dspace/core/shared/follow-link-config.model';
1216
import { CommunitySearchResult } from '@dspace/core/shared/object-collection/community-search-result.model';
1317
import { SearchResult } from '@dspace/core/shared/search/models/search-result.model';
@@ -57,9 +61,11 @@ import { DSOSelectorComponent } from '../dso-selector.component';
5761
* Component rendering a list of communities to select from
5862
*/
5963
export class AuthorizedCommunitySelectorComponent extends DSOSelectorComponent {
64+
6065
/**
61-
* If present this value is used to filter community list by entity type
66+
* The action type to determine which authorized communities to fetch
6267
*/
68+
@Input() action: ActionType = ActionType.ADMIN;
6369

6470
constructor(
6571
protected searchService: SearchService,
@@ -91,9 +97,17 @@ export class AuthorizedCommunitySelectorComponent extends DSOSelectorComponent {
9197
elementsPerPage: this.defaultPagination.pageSize,
9298
};
9399

94-
searchListService$ = this.communityDataService
95-
.getAuthorizedCommunity(query, findOptions, useCache, false, followLink('parentCommunity'));
96-
100+
if (this.action === ActionType.WRITE) {
101+
searchListService$ = this.communityDataService
102+
.getEditAuthorizedCommunity(query, findOptions, useCache, false, followLink('parentCommunity'));
103+
} else if (this.action === ActionType.ADD) {
104+
searchListService$ = this.communityDataService
105+
.getAddAuthorizedCommunity(query, findOptions, useCache, false, followLink('parentCommunity'));
106+
} else {
107+
// By default, search for admin authorized communities
108+
searchListService$ = this.communityDataService
109+
.getAdminAuthorizedCommunity(query, findOptions, useCache, false, followLink('parentCommunity'));
110+
}
97111
return searchListService$.pipe(
98112
getFirstCompletedRemoteData(),
99113
map((rd) => Object.assign(new RemoteData(null, null, null, null), rd, {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { NO_ERRORS_SCHEMA } from '@angular/core';
2+
import {
3+
ComponentFixture,
4+
TestBed,
5+
waitForAsync,
6+
} from '@angular/core/testing';
7+
import { RouterTestingModule } from '@angular/router/testing';
8+
import { NotificationsService } from '@dspace/core/notification-system/notifications.service';
9+
import { createPaginatedList } from '@dspace/core/testing/utils.test';
10+
import { createSuccessfulRemoteDataObject$ } from '@dspace/core/utilities/remote-data.utils';
11+
import { TranslateModule } from '@ngx-translate/core';
12+
import { ItemDataService } from 'src/app/core/data/item-data.service';
13+
import { Item } from 'src/app/core/shared/item.model';
14+
import { SearchService } from 'src/app/shared/search/search.service';
15+
16+
import { DSpaceObjectType } from '../../../../core/shared/dspace-object-type.model';
17+
import { ThemedLoadingComponent } from '../../../loading/themed-loading.component';
18+
import { ListableObjectComponentLoaderComponent } from '../../../object-collection/shared/listable-object/listable-object-component-loader.component';
19+
import { VarDirective } from '../../../utils/var.directive';
20+
import { AuthorizedItemSelectorComponent } from './authorized-item-selector.component';
21+
22+
describe('AuthorizedItemSelectorComponent', () => {
23+
let component: AuthorizedItemSelectorComponent;
24+
let fixture: ComponentFixture<AuthorizedItemSelectorComponent>;
25+
26+
let itemService;
27+
let item;
28+
29+
let notificationsService: NotificationsService;
30+
31+
beforeEach(waitForAsync(() => {
32+
item = Object.assign(new Item(), {
33+
id: 'authorized-item',
34+
});
35+
itemService = jasmine.createSpyObj('itemService', {
36+
findEditAuthorized: createSuccessfulRemoteDataObject$(createPaginatedList([item])),
37+
});
38+
notificationsService = jasmine.createSpyObj('notificationsService', ['error']);
39+
TestBed.configureTestingModule({
40+
imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), AuthorizedItemSelectorComponent, VarDirective],
41+
providers: [
42+
{ provide: SearchService, useValue: {} },
43+
{ provide: ItemDataService, useValue: itemService },
44+
{ provide: NotificationsService, useValue: notificationsService },
45+
],
46+
schemas: [NO_ERRORS_SCHEMA],
47+
})
48+
.overrideComponent(AuthorizedItemSelectorComponent, {
49+
remove: { imports: [ListableObjectComponentLoaderComponent, ThemedLoadingComponent] },
50+
})
51+
.compileComponents();
52+
}));
53+
54+
beforeEach(() => {
55+
fixture = TestBed.createComponent(AuthorizedItemSelectorComponent);
56+
component = fixture.componentInstance;
57+
component.types = [DSpaceObjectType.ITEM];
58+
fixture.detectChanges();
59+
});
60+
61+
describe('search', () => {
62+
it('should call findEditAuthorized and return the authorized item in a SearchResult', (done) => {
63+
component.search('', 1).subscribe((resultRD) => {
64+
expect(itemService.findEditAuthorized).toHaveBeenCalled();
65+
expect(resultRD.payload.page.length).toEqual(1);
66+
expect(resultRD.payload.page[0].indexableObject).toEqual(item);
67+
done();
68+
});
69+
});
70+
});
71+
});

0 commit comments

Comments
 (0)