Last active
September 26, 2018 13:09
-
-
Save corybsa/035a55737bd88f74d5ba6e5095802163 to your computer and use it in GitHub Desktop.
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
import { AppRoutes } from '../../app.routing'; | |
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; | |
import { ActivatedRouteSnapshot, PreloadAllModules, Router, RouterStateSnapshot } from '@angular/router'; | |
import { getTestBed, inject, TestBed } from '@angular/core/testing'; | |
import { RouterTestingModule } from '@angular/router/testing'; | |
import { AdminLayoutComponent } from '../../layout/admin/admin-layout.component'; | |
import { AuthLayoutComponent } from '../../layout/auth/auth-layout.component'; | |
import { AppComponent } from '../../app.component'; | |
import { StoreActions } from '../../store/app.actions'; | |
import { store } from '../../store/app.store'; | |
import { NgModuleFactoryLoader, NO_ERRORS_SCHEMA } from '@angular/core'; | |
import { HomeModule } from '../../components/home/home.module'; | |
import { AuthGuard } from '../../services/auth/authguard'; | |
import { AuthService } from '../../services/auth/auth.service'; | |
import { DocumentationModule } from '../../components/documentation/documentation.module'; | |
import { TestObjects } from '../test-objects.spec'; | |
import { BusService } from '../../services/bus/bus.service'; | |
import { UserService } from '../../services/user/user.service'; | |
import { UserRoles } from '../../datamodels/user/user-role.model'; | |
import { PagesModule } from '../../layout/pages/pages.module'; | |
import { ChangelogModule } from '../../components/changelog/changelog.module'; | |
import { CommunicationsModule } from '../../components/communications/communications.module'; | |
import { ClassModule } from '../../components/class/class.module'; | |
import { DashboardModule } from '../../components/dashboard/dashboard.module'; | |
import { GroupModule } from '../../components/group/group.module'; | |
import { InfoModule } from '../../components/info/info.module'; | |
import { LessonPlansModule } from '../../components/lesson-plans/lesson-plans.module'; | |
import { MyGroupsModule } from '../../components/my-groups/my-groups.module'; | |
import { MyProfileModule } from '../../components/my-profile/my-profile.module'; | |
import { SettingsModule } from '../../components/settings/settings.module'; | |
import { SchoolModule } from '../../components/school/school.module'; | |
import { AnalyticsModule } from '../../components/analytics/analytics.module'; | |
import { NgGridModule } from '../../components/ng-grid/ng-grid.module'; | |
import { ErrorMainviewModule } from '../../components/error/error-mainview.module'; | |
import { MyStuffModule } from '../../components/my-stuff/my-stuff.module'; | |
import { GridModule } from '../../components/grid/grid.module'; | |
import { ProfileModule } from '../../components/profile/profile.module'; | |
import { ReportsModule } from '../../components/reports/reports.module'; | |
import { SchoolProfileModule } from '../../components/school-profile/school-profile.module'; | |
describe('Tests for AuthGuard', () => { | |
this.injector = null; | |
this.router = null; | |
this.route = null; | |
this.fixture = null; | |
this.location = null; | |
this.auth = null; | |
this.httpMock = null; | |
this.loader = null; | |
this.mockStateSnapshot = jasmine.createSpyObj<RouterStateSnapshot>('RouterStateSnapshot', ['toString']); | |
beforeEach(() => { | |
TestBed.configureTestingModule({ | |
declarations: [ | |
AppComponent, | |
AdminLayoutComponent, | |
AuthLayoutComponent | |
], | |
imports: [ | |
HttpClientTestingModule, | |
RouterTestingModule.withRoutes(AppRoutes, { preloadingStrategy: PreloadAllModules }) | |
], | |
providers: [ | |
AuthService, | |
AuthGuard, | |
BusService, | |
UserService, | |
{ provide: ActivatedRouteSnapshot }, | |
{ provide: RouterStateSnapshot, useValue: this.mockStateSnapshot } | |
], | |
schemas: [NO_ERRORS_SCHEMA] | |
}); | |
this.injector = getTestBed(); | |
this.loader = TestBed.get(NgModuleFactoryLoader); | |
this.router = TestBed.get(Router); | |
this.fixture = TestBed.createComponent(AppComponent); | |
this.auth = TestBed.get(AuthService); | |
this.httpMock = TestBed.get(HttpTestingController); | |
this.loader.stubbedModules = { | |
'./components/changelog/changelog.module#ChangelogModule': ChangelogModule, | |
'./components/class/class.module#ClassModule': ClassModule, | |
'./components/communications/communications.module#CommunicationsModule': CommunicationsModule, | |
'./components/dashboard/dashboard.module#DashboardModule': DashboardModule, | |
'./components/documentation/documentation.module#DocumentationModule': DocumentationModule, | |
'./components/group/group.module#GroupModule': GroupModule, | |
'./components/home/home.module#HomeModule': HomeModule, | |
'./components/info/info.module#InfoModule': InfoModule, | |
'./components/lesson-plans/lesson-plans.module#LessonPlansModule': LessonPlansModule, | |
'./components/my-groups/my-groups.module#MyGroupsModule': MyGroupsModule, | |
'./components/my-profile/my-profile.module#MyProfileModule': MyProfileModule, | |
'./layout/pages/pages.module#PagesModule': PagesModule, | |
'./components/school/school.module#SchoolModule': SchoolModule, | |
'./components/settings/settings.module#SettingsModule': SettingsModule, | |
'./components/analytics/analytics.module#AnalyticsModule': AnalyticsModule, | |
'./components/grid/grid.module#GridModule': GridModule, | |
'./components/ng-grid/ng-grid.module#NgGridModule': NgGridModule, | |
'./components/error/error-mainview.module#ErrorMainviewModule': ErrorMainviewModule, | |
'./components/my-stuff/my-stuff.module#MyStuffModule': MyStuffModule, | |
'./components/profile/profile.module#ProfileModule': ProfileModule, | |
'./components/reports/reports.module#ReportsModule': ReportsModule, | |
'./components/school-profile/school-profile.module#SchoolProfileModule': SchoolProfileModule | |
}; | |
this.router.resetConfig(AppRoutes); | |
this.router.initialNavigation(); | |
}); | |
afterEach(() => { | |
this.fixture.destroy(); | |
document.body.removeChild(this.fixture.debugElement.nativeElement); | |
}); | |
afterAll(() => { | |
store.dispatch(StoreActions.User.setCurrentUser(null)); | |
}); | |
describe('#AuthGuard', () => { | |
it('should return false for non-logged-in users and navigate to /pages/login', inject([AuthGuard], (guard: AuthGuard) => { | |
const spy = spyOn(this.router, 'navigate'); | |
guard.canActivate(new ActivatedRouteSnapshot(), this.mockStateSnapshot).then((value) => { | |
expect(value).toBeFalsy(); | |
expect(spy).toHaveBeenCalledWith(['/pages/login']); | |
}); | |
const req = this.httpMock.expectOne(request => { | |
return request.url === '/api/auth/authenticated'; | |
}); | |
req.flush(null); | |
}) | |
); | |
it('should allow anyone to get through when no data is passed.', inject([AuthGuard], (guard: AuthGuard) => { | |
const route = new ActivatedRouteSnapshot(); | |
guard.canActivate(route, this.mockStateSnapshot).then((value) => { | |
expect(value).toBeTruthy(); | |
}); | |
const req = this.httpMock.expectOne(request => { | |
return request.url === '/api/auth/authenticated'; | |
}); | |
req.flush(TestObjects.User.Developer); | |
}) | |
); | |
it('should allow Global Admins.', inject([AuthGuard], (guard: AuthGuard) => { | |
const route = new ActivatedRouteSnapshot(); | |
route.data = { allow: [UserRoles.GLOBAL_ADMIN] }; | |
guard.canActivate(route, this.mockStateSnapshot).then((value) => { | |
expect(value).toBeTruthy(); | |
}); | |
const req = this.httpMock.expectOne(request => { | |
return request.url === '/api/auth/authenticated'; | |
}); | |
req.flush(TestObjects.User.Developer); | |
}) | |
); | |
it('should allow School Admin, School Staff and School Teacher.', inject([AuthGuard], (guard: AuthGuard) => { | |
const route = new ActivatedRouteSnapshot(); | |
route.data = { allow: [UserRoles.SCHOOL_ADMIN, UserRoles.SCHOOL_STAFF, UserRoles.SCHOOL_TEACHER] }; | |
guard.canActivate(route, this.mockStateSnapshot).then((value) => { | |
expect(value).toBeTruthy(); | |
}); | |
const req = this.httpMock.expectOne(request => { | |
return request.url === '/api/auth/authenticated'; | |
}); | |
req.flush(TestObjects.User.SchoolAdmin); | |
}) | |
); | |
it('should allow Students.', inject([AuthGuard], (guard: AuthGuard) => { | |
const route = new ActivatedRouteSnapshot(); | |
route.data = { allow: [UserRoles.STUDENT] }; | |
guard.canActivate(route, this.mockStateSnapshot).then((value) => { | |
expect(value).toBeTruthy(); | |
}); | |
const req = this.httpMock.expectOne(request => { | |
return request.url === '/api/auth/authenticated'; | |
}); | |
req.flush(TestObjects.User.Student); | |
}) | |
); | |
it('should reject Students.', inject([AuthGuard], (guard: AuthGuard) => { | |
const spy = spyOn(this.router, 'navigate'); | |
const route = new ActivatedRouteSnapshot(); | |
route.data = { reject: [UserRoles.STUDENT] }; | |
guard.canActivate(route, this.mockStateSnapshot).then((value) => { | |
expect(value).toBeFalsy(); | |
expect(spy).toHaveBeenCalled(); | |
expect(this.router.url).toBe('/'); | |
}); | |
const req = this.httpMock.expectOne(request => { | |
return request.url === '/api/auth/authenticated'; | |
}); | |
req.flush(TestObjects.User.Student); | |
}) | |
); | |
}); | |
xdescribe('#ChangelogModule', () => { | |
it('should allow everybody except students.', inject([AuthGuard], (guard: AuthGuard) => { | |
const spy = spyOn(guard, 'canActivate'); | |
store.dispatch(StoreActions.User.setCurrentUser(TestObjects.User.SchoolAdmin)); | |
this.router.navigate(['/changelog']).then((value) => { | |
expect(value).toBeTruthy(); | |
expect(spy).toHaveBeenCalled(); | |
expect(this.router.url).toBe('/changelog'); | |
}); | |
}) | |
); | |
it('should reject students', inject([AuthGuard], (guard: AuthGuard) => { | |
const spy = spyOn(guard, 'canActivate'); | |
store.dispatch(StoreActions.User.setCurrentUser(TestObjects.User.Student)); | |
this.router.navigate(['/changelog']).then((value) => { | |
expect(value).toBeFalsy(); | |
expect(spy).toHaveBeenCalled(); | |
expect(this.router.url).toBe('/'); | |
}); | |
}) | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment