Commits
Bruno Roemers authored 3b07738d4dc
1 - | import { TestBed } from '@angular/core/testing'; |
2 - | |
3 1 | import { GroupPageGuard } from './group-page.guard'; |
2 + | import { HALEndpointService } from '../../core/shared/hal-endpoint.service'; |
3 + | import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service'; |
4 + | import { ActivatedRouteSnapshot, Router } from '@angular/router'; |
5 + | import { of as observableOf } from 'rxjs'; |
6 + | import { AuthService } from '../../core/auth/auth.service'; |
7 + | import { FeatureID } from '../../core/data/feature-authorization/feature-id'; |
4 8 | |
5 9 | describe('GroupPageGuard', () => { |
10 + | const groupsEndpointUrl = 'https://test.org/api/eperson/groups'; |
11 + | const groupUuid = '0d6f89df-f95a-4829-943c-f21f434fb892'; |
12 + | const groupEndpointUrl = `${groupsEndpointUrl}/${groupUuid}`; |
13 + | const routeSnapshotWithGroupId = { |
14 + | params: { |
15 + | groupId: groupUuid, |
16 + | } |
17 + | } as unknown as ActivatedRouteSnapshot; |
18 + | |
6 19 | let guard: GroupPageGuard; |
20 + | let halEndpointService: HALEndpointService; |
21 + | let authorizationService: AuthorizationDataService; |
22 + | let router: Router; |
23 + | let authService: AuthService; |
7 24 | |
8 25 | beforeEach(() => { |
9 - | TestBed.configureTestingModule({}); |
10 - | guard = TestBed.inject(GroupPageGuard); |
26 + | halEndpointService = jasmine.createSpyObj(['getEndpoint']); |
27 + | (halEndpointService as any).getEndpoint.and.returnValue(observableOf(groupsEndpointUrl)); |
28 + | |
29 + | authorizationService = jasmine.createSpyObj(['isAuthorized']); |
30 + | // NOTE: value is set in beforeEach |
31 + | |
32 + | router = jasmine.createSpyObj(['parseUrl']); |
33 + | (router as any).parseUrl.and.returnValue = {}; |
34 + | |
35 + | authService = jasmine.createSpyObj(['isAuthenticated']); |
36 + | (authService as any).isAuthenticated.and.returnValue(observableOf(true)); |
37 + | |
38 + | guard = new GroupPageGuard(halEndpointService, authorizationService, router, authService); |
11 39 | }); |
12 40 | |
13 41 | it('should be created', () => { |
14 42 | expect(guard).toBeTruthy(); |
15 43 | }); |
44 + | |
45 + | describe('canActivate', () => { |
46 + | describe('when the current user can manage the group', () => { |
47 + | beforeEach(() => { |
48 + | (authorizationService as any).isAuthorized.and.returnValue(observableOf(true)); |
49 + | }); |
50 + | |
51 + | it('should return true', (done) => { |
52 + | guard.canActivate( |
53 + | routeSnapshotWithGroupId, { url: 'current-url'} as any |
54 + | ).subscribe((result) => { |
55 + | expect(authorizationService.isAuthorized).toHaveBeenCalledWith( |
56 + | FeatureID.CanManageGroup, groupEndpointUrl, undefined |
57 + | ); |
58 + | expect(result).toBeTrue(); |
59 + | done(); |
60 + | }); |
61 + | }); |
62 + | }); |
63 + | |
64 + | describe('when the current user can not manage the group', () => { |
65 + | beforeEach(() => { |
66 + | (authorizationService as any).isAuthorized.and.returnValue(observableOf(false)); |
67 + | }); |
68 + | |
69 + | it('should not return true', (done) => { |
70 + | guard.canActivate( |
71 + | routeSnapshotWithGroupId, { url: 'current-url'} as any |
72 + | ).subscribe((result) => { |
73 + | expect(authorizationService.isAuthorized).toHaveBeenCalledWith( |
74 + | FeatureID.CanManageGroup, groupEndpointUrl, undefined |
75 + | ); |
76 + | expect(result).not.toBeTrue(); |
77 + | done(); |
78 + | }); |
79 + | }); |
80 + | }); |
81 + | }); |
82 + | |
16 83 | }); |