Skip to content

Commit

Permalink
Merge pull request #442 from IgniteUI/vslavov/auth-proj-tests
Browse files Browse the repository at this point in the history
test(auth-module): add tests for authentication module project
  • Loading branch information
bazal4o authored Feb 8, 2019
2 parents d0861c7 + 69268cd commit f73abc9
Show file tree
Hide file tree
Showing 10 changed files with 981 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { AuthGuard } from './auth.guard';

describe('AuthGuard', () => {
let mockRouter, mockUserService: any;
beforeEach(() => {
mockRouter = {
navigate: () => {}
};
mockUserService = {
currentUser: true
};
});

it('Should properly initialize', () => {
const authGuard = new AuthGuard(mockRouter, mockUserService);
expect(authGuard).toBeDefined();
});

it(`Should properly call 'canActivate'`, () => {
const authGuard = new AuthGuard(mockRouter, mockUserService);
const mockSpy = jasmine.createSpy('mockSpy');
expect(authGuard.canActivate(<any>mockSpy, <any>mockSpy)).toEqual(true);
});
it(`Should properly call 'canActivate'`, () => {
const authGuard = new AuthGuard(mockRouter, mockUserService);
const mockSpy = jasmine.createSpy('mockSpy');
mockUserService.currentUser = false;
spyOn(mockRouter, 'navigate');
expect(authGuard.canActivate(<any>mockSpy, <any>{ url: 'test'})).toEqual(false);
expect(mockRouter.navigate).toHaveBeenCalled();
expect(mockRouter.navigate).toHaveBeenCalledWith([''], { queryParams: { returnUrl: 'test' } });
});
});

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { NoopAnimationsModule, BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';

Expand All @@ -19,6 +19,8 @@ import {
import { LoginDialogComponent } from '../login-dialog/login-dialog.component';
import { LoginBarComponent } from './login-bar.component';
import { UserService } from '../services/user.service';
import { AuthModule } from 'angular-auth-oidc-client';
import { ExternalAuthService } from '../services/external-auth.service';

@Component({
selector: 'app-login-dialog',
Expand All @@ -34,13 +36,19 @@ describe('LoginBarComponent', () => {
class TestUserServSpy {
logout() {}
get currentUser() { return null; }
clearCurrentUser() { return null; }
}

class TestAuthService {
logout() {}
}

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
NoopAnimationsModule,
AuthModule.forRoot(),
IgxButtonModule,
IgxToggleModule,
IgxIconModule,
Expand All @@ -51,7 +59,8 @@ describe('LoginBarComponent', () => {
],
declarations: [ LoginBarComponent, TestLoginDialogComponent ],
providers: [
{ provide: UserService, useClass: TestUserServSpy }
{ provide: UserService, useClass: TestUserServSpy },
{ provide: ExternalAuthService, useClass: TestAuthService }
]
})
.compileComponents();
Expand Down Expand Up @@ -108,16 +117,19 @@ describe('LoginBarComponent', () => {

it('should handle user menu items', async () => {
const userServ = TestBed.get(UserService);
const authServ = TestBed.get(ExternalAuthService);
const router: Router = TestBed.get(Router);
spyOn(router, 'navigate');
spyOn(userServ, 'logout');
spyOn(userServ, 'clearCurrentUser');
spyOn(authServ, 'logout');

component.igxDropDown.open();
component.igxDropDown.setSelectedItem(0);
expect(router.navigate).toHaveBeenCalledWith(['/profile']);

component.igxDropDown.setSelectedItem(1);
expect(router.navigate).toHaveBeenCalledWith(['/home']);
expect(userServ.logout).toHaveBeenCalled();
expect(userServ.clearCurrentUser).toHaveBeenCalled();
expect(authServ.logout).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { LoginComponent } from './login.component';
import { ExternalAuthService, ExternalAuthProvider } from '../services/external-auth.service';
import { AuthenticationService } from '../services/authentication.service';
import { UserService } from '../services/user.service';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

describe('LoginComponent', () => {
let component: LoginComponent;
Expand All @@ -21,7 +22,8 @@ describe('LoginComponent', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ ReactiveFormsModule, RouterTestingModule, IgxInputGroupModule, IgxButtonModule, IgxIconModule, IgxRippleModule ],
imports: [ ReactiveFormsModule, RouterTestingModule, NoopAnimationsModule,
IgxInputGroupModule, IgxButtonModule, IgxIconModule, IgxRippleModule ],
declarations: [ LoginComponent ],
providers: [
{ provide: ExternalAuthService, useValue: extAuthSpy },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class FacebookProvider implements AuthProvider {

public login() {
this.config();
const self = this;
FB.login((response) => {
if (response.authResponse) {
FB.api(
Expand All @@ -36,7 +35,7 @@ export class FacebookProvider implements AuthProvider {
picture: newResponse.picture,
externalToken: FB.getAuthResponse()['accessToken']
};
self.router.navigate([this.externalStsConfig.redirect_url]);
this.router.navigate([this.externalStsConfig.redirect_url]);
});
} else {
console.log('User cancelled login or did not fully authorize.');
Expand Down
Loading

0 comments on commit f73abc9

Please sign in to comment.