-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial groundwork for event log viewing
- Loading branch information
1 parent
114083c
commit 520e355
Showing
8 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
116 changes: 116 additions & 0 deletions
116
src/SIL.XForge.Scripture/ClientApp/src/app/event-metrics/event-metrics-auth.guard.spec.ts
This file contains 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,116 @@ | ||
import { fakeAsync, TestBed, tick } from '@angular/core/testing'; | ||
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; | ||
import { SystemRole } from 'realtime-server/lib/esm/common/models/system-role'; | ||
import { SFProjectRole } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-role'; | ||
import { createTestProjectProfile } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-test-data'; | ||
import { of } from 'rxjs'; | ||
import { anything, mock, when } from 'ts-mockito'; | ||
import { AuthGuard } from 'xforge-common/auth.guard'; | ||
import { AuthService } from 'xforge-common/auth.service'; | ||
import { configureTestingModule } from 'xforge-common/test-utils'; | ||
import { UserService } from 'xforge-common/user.service'; | ||
import { SFProjectProfileDoc } from '../core/models/sf-project-profile-doc'; | ||
import { SFProjectService } from '../core/sf-project.service'; | ||
import { EventMetricsAuthGuard } from './event-metrics-auth.guard'; | ||
|
||
const mockedAuthGuard = mock(AuthGuard); | ||
const mockedAuthService = mock(AuthService); | ||
const mockedProjectService = mock(SFProjectService); | ||
const mockedUserService = mock(UserService); | ||
|
||
describe('EventMetricsAuthGuard', () => { | ||
const project01 = 'project01'; | ||
const project02 = 'project02'; | ||
const user01 = 'user01'; | ||
configureTestingModule(() => ({ | ||
providers: [ | ||
{ provide: AuthGuard, useMock: mockedAuthGuard }, | ||
{ provide: AuthService, useMock: mockedAuthService }, | ||
{ provide: SFProjectService, useMock: mockedProjectService }, | ||
{ provide: UserService, useMock: mockedUserService } | ||
] | ||
})); | ||
|
||
it('can activate if user is logged in and has ServalAdmin role', fakeAsync(() => { | ||
const env = new TestEnvironment(true, SystemRole.ServalAdmin); | ||
|
||
env.service.canActivate(env.getActivatedRouteSnapshot(project02), {} as RouterStateSnapshot).subscribe(result => { | ||
expect(result).toBe(true); | ||
}); | ||
|
||
env.wait(); | ||
})); | ||
|
||
it('can activate if user is logged in and has SystemAdmin role', fakeAsync(() => { | ||
const env = new TestEnvironment(true, SystemRole.SystemAdmin); | ||
|
||
env.service.canActivate(env.getActivatedRouteSnapshot(project02), {} as RouterStateSnapshot).subscribe(result => { | ||
expect(result).toBe(true); | ||
}); | ||
|
||
env.wait(); | ||
})); | ||
|
||
it('can activate if user is logged in and has pt_administrator role', fakeAsync(() => { | ||
const env = new TestEnvironment(true, SystemRole.User); | ||
|
||
env.service.canActivate(env.getActivatedRouteSnapshot(project01), {} as RouterStateSnapshot).subscribe(result => { | ||
expect(result).toBe(true); | ||
}); | ||
|
||
env.wait(); | ||
})); | ||
|
||
it('cannot activate if user is not logged in', fakeAsync(() => { | ||
const env = new TestEnvironment(false, SystemRole.None); | ||
|
||
env.service.canActivate(env.getActivatedRouteSnapshot(project02), {} as RouterStateSnapshot).subscribe(result => { | ||
expect(result).toBe(false); | ||
}); | ||
|
||
env.wait(); | ||
})); | ||
|
||
it('cannot activate if user is logged in but does not have any administrator role', fakeAsync(() => { | ||
const env = new TestEnvironment(true, SystemRole.User); | ||
|
||
env.service.canActivate(env.getActivatedRouteSnapshot(project02), {} as RouterStateSnapshot).subscribe(result => { | ||
expect(result).toBe(false); | ||
}); | ||
|
||
env.wait(); | ||
})); | ||
|
||
class TestEnvironment { | ||
readonly service: EventMetricsAuthGuard; | ||
|
||
constructor(isLoggedIn: boolean, role: SystemRole) { | ||
this.service = TestBed.inject(EventMetricsAuthGuard); | ||
when(mockedAuthGuard.canActivate(anything(), anything())).thenReturn(of(isLoggedIn)); | ||
when(mockedAuthGuard.allowTransition()).thenReturn(of(isLoggedIn)); | ||
when(mockedAuthService.currentUserRoles).thenReturn([role]); | ||
when(mockedUserService.currentUserId).thenReturn(user01); | ||
|
||
when(mockedProjectService.getProfile(project01)).thenReturn( | ||
Promise.resolve({ | ||
data: createTestProjectProfile({ userRoles: { user01: SFProjectRole.ParatextAdministrator } }) | ||
} as SFProjectProfileDoc) | ||
); | ||
when(mockedProjectService.getProfile(project02)).thenReturn( | ||
Promise.resolve({ | ||
data: createTestProjectProfile() | ||
} as SFProjectProfileDoc) | ||
); | ||
} | ||
|
||
getActivatedRouteSnapshot(projectId: string): ActivatedRouteSnapshot { | ||
const snapshot = new ActivatedRouteSnapshot(); | ||
snapshot.params = { projectId: projectId }; | ||
return snapshot; | ||
} | ||
|
||
wait(): void { | ||
tick(); | ||
} | ||
} | ||
}); |
32 changes: 32 additions & 0 deletions
32
src/SIL.XForge.Scripture/ClientApp/src/app/event-metrics/event-metrics-auth.guard.ts
This file contains 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,32 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { SystemRole } from 'realtime-server/lib/esm/common/models/system-role'; | ||
import { SFProjectRole } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-role'; | ||
import { AuthGuard } from 'xforge-common/auth.guard'; | ||
import { AuthService } from 'xforge-common/auth.service'; | ||
import { UserService } from 'xforge-common/user.service'; | ||
import { SFProjectProfileDoc } from '../core/models/sf-project-profile-doc'; | ||
import { SFProjectService } from '../core/sf-project.service'; | ||
import { RouterGuard } from '../shared/project-router.guard'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class EventMetricsAuthGuard extends RouterGuard { | ||
constructor( | ||
readonly authGuard: AuthGuard, | ||
private readonly authService: AuthService, | ||
readonly projectService: SFProjectService, | ||
private readonly userService: UserService | ||
) { | ||
super(authGuard, projectService); | ||
} | ||
|
||
check(projectDoc: SFProjectProfileDoc): boolean { | ||
return ( | ||
this.authService.currentUserRoles.includes(SystemRole.ServalAdmin) || | ||
this.authService.currentUserRoles.includes(SystemRole.SystemAdmin) || | ||
(projectDoc.data != null && | ||
projectDoc.data.userRoles[this.userService.currentUserId] === SFProjectRole.ParatextAdministrator) | ||
); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/SIL.XForge.Scripture/ClientApp/src/app/event-metrics/event-metrics.component.html
This file contains 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 @@ | ||
<h1>Event Log</h1> |
Empty file.
36 changes: 36 additions & 0 deletions
36
src/SIL.XForge.Scripture/ClientApp/src/app/event-metrics/event-metrics.component.spec.ts
This file contains 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,36 @@ | ||
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'; | ||
import { provideHttpClientTesting } from '@angular/common/http/testing'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { NoopAnimationsModule } from '@angular/platform-browser/animations'; | ||
import { TestRealtimeModule } from 'xforge-common/test-realtime.module'; | ||
import { configureTestingModule, TestTranslocoModule } from 'xforge-common/test-utils'; | ||
import { SF_TYPE_REGISTRY } from '../core/models/sf-type-registry'; | ||
import { EventMetricsComponent } from './event-metrics.component'; | ||
|
||
describe('EventMetricsComponent', () => { | ||
configureTestingModule(() => ({ | ||
imports: [ | ||
EventMetricsComponent, | ||
NoopAnimationsModule, | ||
TestTranslocoModule, | ||
TestRealtimeModule.forRoot(SF_TYPE_REGISTRY) | ||
], | ||
providers: [provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting()] | ||
})); | ||
|
||
it('should be created', () => { | ||
const env = new TestEnvironment(); | ||
expect(env.component).toBeTruthy(); | ||
}); | ||
|
||
class TestEnvironment { | ||
readonly component: EventMetricsComponent; | ||
readonly fixture: ComponentFixture<EventMetricsComponent>; | ||
|
||
constructor() { | ||
this.fixture = TestBed.createComponent(EventMetricsComponent); | ||
this.component = this.fixture.componentInstance; | ||
this.fixture.detectChanges(); | ||
} | ||
} | ||
}); |
10 changes: 10 additions & 0 deletions
10
src/SIL.XForge.Scripture/ClientApp/src/app/event-metrics/event-metrics.component.ts
This file contains 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,10 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-event-metrics', | ||
templateUrl: './event-metrics.component.html', | ||
styleUrls: ['./event-metrics.component.scss'], | ||
standalone: true, | ||
imports: [] | ||
}) | ||
export class EventMetricsComponent {} |
This file contains 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