Skip to content

Commit

Permalink
feat: Update handling of user sessions to use signals
Browse files Browse the repository at this point in the history
  • Loading branch information
jrassa committed May 7, 2024
1 parent 4cb25b6 commit 0d3f154
Show file tree
Hide file tree
Showing 79 changed files with 737 additions and 696 deletions.
22 changes: 13 additions & 9 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import { DialogModule } from '@angular/cdk/dialog';
import { provideHttpClient } from '@angular/common/http';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { signal } from '@angular/core';
import { TestBed, waitForAsync } from '@angular/core/testing';

import { AppComponent } from './app.component';
import { APP_CONFIG } from './core/config.service';
import { provideCdkDialog } from './common/dialog/provider';
import { APP_CONFIG } from './core';
import { provideSession } from './core/provider';

describe('AppComponent', () => {
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [DialogModule, AppComponent],
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [AppComponent],
providers: [
{ provide: APP_CONFIG, useValue: signal({}) },
{ provide: APP_CONFIG, useValue: signal({ roles: { user: true } }) },
// { provide: APP_SESSION, useValue: signal(new Session({})) },
provideSession(),
provideCdkDialog(),
provideHttpClient(),
provideHttpClientTesting()
]
}).compileComponents();
}));
it('should create the app', waitForAsync(() => {
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
});
it('should include a router outlet', waitForAsync(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
Expand Down
4 changes: 3 additions & 1 deletion src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
provideAppConfig,
provideCoreRoutes,
provideNavigationService,
provideSession,
provideViewportScroller
} from './core/provider';
import { provideExampleRoutes } from './site/example/provider';
Expand All @@ -44,8 +45,8 @@ export const appConfig: ApplicationConfig = {
provideHttpClient(
withInterceptors([
signinInterceptor,
euaInterceptor,
authInterceptor,
euaInterceptor,
masqueradeInterceptor
]),
// Ensures any legacy class based interceptors are used.
Expand All @@ -63,6 +64,7 @@ export const appConfig: ApplicationConfig = {
provideExampleRoutes(),
provideAppConfig(),
provideNavigationService(),
provideSession(),
{
provide: TitleStrategy,
useClass: PageTitleStrategy
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/about.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChangeDetectionStrategy, Component, computed, effect, inject } from '@angular/core';

import { APP_CONFIG } from './config.service';
import { APP_CONFIG } from './tokens';

@Component({
template: `
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/admin/admin-routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Routes } from '@angular/router';

import { authGuard } from '../auth/auth.guard';
import { authGuard } from '../auth';
import { AdminComponent } from './admin.component';
import { ADMIN_CACHE_ENTRIES_ROUTES } from './cache-entries/admin-cache-entries-routes';
import { ADMIN_EUA_ROUTES } from './end-user-agreement/admin-eua-routes';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { of } from 'rxjs';

import { PagingResults } from '../../../../common/paging.model';
import { User } from '../../../auth/user.model';
import { User } from '../../../auth';
import { ExportConfigService } from '../../../export-config.service';
import { FeedbackService } from '../../../feedback/feedback.service';
import { AdminUsersService } from '../../user/admin-users.service';
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/admin/user/admin-users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { catchError, map } from 'rxjs/operators';

import { NULL_PAGING_RESULTS, PagingOptions, PagingResults } from '../../../common/paging.model';
import { SystemAlertService } from '../../../common/system-alert/system-alert.service';
import { User } from '../../auth/user.model';
import { User } from '../../auth';
import { ErrorState } from '../../errors/error-state.model';

export const userResolver: ResolveFn<User | null> = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { RouterModule } from '@angular/router';
import { of } from 'rxjs';

import { PagingResults } from '../../../../common/paging.model';
import { User } from '../../../auth/user.model';
import { APP_CONFIG } from '../../../config.service';
import { User } from '../../../auth';
import { ExportConfigService } from '../../../export-config.service';
import { APP_CONFIG } from '../../../tokens';
import { AdminUsersService } from '../admin-users.service';
import { AdminListUsersComponent } from './admin-list-users.component';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ import {
SidebarComponent,
TextColumnComponent
} from '../../../../common/table';
import { Role } from '../../../auth/role.model';
import { User } from '../../../auth/user.model';
import { APP_CONFIG } from '../../../config.service';
import { Role, User } from '../../../auth';
import { ExportConfigService } from '../../../export-config.service';
import { APP_CONFIG } from '../../../tokens';
import { AdminUsersService } from '../admin-users.service';
import { UserRoleFilterDirective } from './user-role-filter.directive';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Directive, OnInit, computed, inject } from '@angular/core';

import { AsyHeaderListFilterComponent, ListFilterOption } from '../../../../common/table';
import { Role } from '../../../auth/role.model';
import { APP_CONFIG } from '../../../config.service';
import { Role } from '../../../auth';
import { APP_CONFIG } from '../../../tokens';

@Directive({
selector: 'asy-header-filter[list-filter][user-role-filter]',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { signal } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router';

import { APP_CONFIG } from '../../../config.service';
import { APP_CONFIG } from '../../../tokens';
import { ManageUserComponent } from './manage-user.component';

describe('ManageUserComponent', () => {
Expand Down
5 changes: 2 additions & 3 deletions src/app/core/admin/user/manage-user/manage-user.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import { TooltipModule } from 'ngx-bootstrap/tooltip';

import { SystemAlertComponent } from '../../../../common/system-alert/system-alert.component';
import { SystemAlertService } from '../../../../common/system-alert/system-alert.service';
import { Role } from '../../../auth/role.model';
import { EditUser } from '../../../auth/user.model';
import { APP_CONFIG } from '../../../config.service';
import { EditUser, Role } from '../../../auth';
import { APP_CONFIG } from '../../../tokens';
import { AdminUsersService } from '../admin-users.service';

@Component({
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/audit/audit-routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { authGuard } from '../auth/auth.guard';
import { authGuard } from '../auth';
import { AuditService } from './audit.service';
import { ListAuditEntriesComponent } from './list-audit-entries/list-audit-entries.component';

Expand Down
2 changes: 1 addition & 1 deletion src/app/core/audit/audit.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import _cloneDeep from 'lodash/cloneDeep';

import { PagingOptions, PagingResults } from '../../common/paging.model';
import { SystemAlertService } from '../../common/system-alert/system-alert.service';
import { User } from '../auth/user.model';
import { User } from '../auth';
import { AuditService } from './audit.service';

describe('Audit Service', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/audit/audit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { catchError, map } from 'rxjs/operators';

import { NULL_PAGING_RESULTS, PagingOptions, PagingResults } from '../../common/paging.model';
import { SystemAlertService } from '../../common/system-alert/system-alert.service';
import { User } from '../auth/user.model';
import { User } from '../auth';
import { AuditActionTypes } from './audit.classes';

@Injectable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { of } from 'rxjs';

import { PagingResults } from '../../../common/paging.model';
import { SystemAlertService } from '../../../common/system-alert/system-alert.service';
import { APP_CONFIG } from '../../config.service';
import { APP_CONFIG } from '../../tokens';
import { AuditService } from '../audit.service';
import { ListAuditEntriesComponent } from './list-audit-entries.component';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import {
AsyTableEmptyStateComponent,
PaginatorComponent
} from '../../../common/table';
import { APP_CONFIG } from '../../config.service';
import { ExportConfigService } from '../../export-config.service';
import { APP_CONFIG } from '../../tokens';
import { AuditObjectComponent } from '../audit-object.component';
import { AuditViewChangeModalComponent } from '../audit-view-change-modal/audit-view-change-modal.component';
import { AuditViewDetailsModalComponent } from '../audit-view-details-modal/audit-view-details-modal.component';
Expand Down
34 changes: 14 additions & 20 deletions src/app/core/auth/auth.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/r

import { of } from 'rxjs';

import { APP_CONFIG } from '../config.service';
import { AuthGuard, authGuard } from './auth.guard';
import { EndUserAgreement } from '../admin/end-user-agreement/eua.model';
import { provideSession } from '../provider';
import { APP_CONFIG } from '../tokens';
import { authGuard } from './auth.guard';
import { AuthenticationService } from './authentication.service';
import { AuthorizationService } from './authorization.service';
import { Session } from './session.model';
import { SessionService } from './session.service';

Expand All @@ -26,26 +27,17 @@ describe('AuthGuard', () => {
'getCurrentEua'
]);
authServiceSpy.reloadCurrentUser.and.returnValue(of({}));
authServiceSpy.getCurrentEua.and.returnValue(of(null));
authServiceSpy.getCurrentEua.and.returnValue(of(undefined));

TestBed.configureTestingModule({
providers: [
{ provide: APP_CONFIG, useValue: signal(defaultConfig) },
{ provide: AuthenticationService, useValue: authServiceSpy },
{ provide: SessionService },
{ provide: AuthorizationService },
// eslint-disable-next-line deprecation/deprecation
{ provide: AuthGuard }
provideSession(),
{ provide: AuthenticationService, useValue: authServiceSpy }
]
});
});

it('should be created', () => {
// eslint-disable-next-line deprecation/deprecation
const guard = TestBed.inject(AuthGuard);
expect(guard).toBeTruthy();
});

describe('canActivate', () => {
it('should return true if authentication not required', (done) => {
const route = {
Expand Down Expand Up @@ -75,7 +67,7 @@ describe('AuthGuard', () => {
});
});

it('should return false for authenticated user without user role', (done) => {
it('should redirect to /unauthorized for authenticated user without user role', (done) => {
authServiceSpy.reloadCurrentUser.and.returnValue(
of({
name: 'test',
Expand Down Expand Up @@ -113,7 +105,7 @@ describe('AuthGuard', () => {
});
});

it('should return false for authenticated user without required roles', (done) => {
it('should redirect to /unauthorized for authenticated user without required roles', (done) => {
authServiceSpy.reloadCurrentUser.and.returnValue(
of({
name: 'test',
Expand All @@ -135,7 +127,7 @@ describe('AuthGuard', () => {
});
});

it('should return false for authenticated user with none of the required roles', (done) => {
it('should redirect to /unauthorized for authenticated user with none of the required roles', (done) => {
authServiceSpy.reloadCurrentUser.and.returnValue(
of({
name: 'test',
Expand Down Expand Up @@ -199,15 +191,17 @@ describe('AuthGuard', () => {
});
});

it('should redirect to eua page for authenticated user who has not accepted latest eua', (done) => {
it('should redirect to /eua for authenticated user who has not accepted latest eua', (done) => {
authServiceSpy.reloadCurrentUser.and.returnValue(
of({
name: 'test',
username: 'test',
roles: { user: true }
})
);
authServiceSpy.getCurrentEua.and.returnValue(of({ published: 1 }));
authServiceSpy.getCurrentEua.and.returnValue(
of(new EndUserAgreement({ published: 1 }))
);

const route = { data: { requiresEua: true } } as unknown as ActivatedRouteSnapshot;

Expand Down
Loading

0 comments on commit 0d3f154

Please sign in to comment.