Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #20 from admin-ch/logout
Browse files Browse the repository at this point in the history
Logout
  • Loading branch information
gillerr committed Nov 17, 2020
2 parents 1a04080 + 660e873 commit c3d0d23
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ha-ui",
"version": "1.0.7",
"version": "1.0.8",
"scripts": {
"start": "ng serve",
"build": "ng build",
Expand Down
5 changes: 5 additions & 0 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const routes: Routes = [
path: 'auth',
loadChildren: () => import('./auth/auth.module').then(m => m.AuthModule)
},
{
path: 'logout',
loadChildren: () => import('./home/home.module').then(m => m.HomeModule),
data: {logout: true}
},
{
path: 'home',
loadChildren: () => import('./home/home.module').then(m => m.HomeModule)
Expand Down
4 changes: 1 addition & 3 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import {AfterViewInit, Component} from '@angular/core';
import {NavigationEnd, Router} from '@angular/router';
import {Observable} from 'rxjs';
import {delay, filter, map, startWith, takeUntil} from 'rxjs/operators';
import {OidcSecurityService} from 'angular-auth-oidc-client';
import {ObHttpApiInterceptorEvents, ObOffCanvasService, ObSpinnerService, ObUnsubscribable} from '@oblique/oblique';
import {ObHttpApiInterceptorEvents, ObOffCanvasService, ObUnsubscribable} from '@oblique/oblique';
import {OauthService} from './auth/oauth.service';
import {OpenIdConfigService} from './auth/open-id-config-service';

@Component({
selector: 'ha-root',
Expand Down
127 changes: 87 additions & 40 deletions src/app/home/home.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,107 @@ import {LangChangeEvent, TranslateService} from '@ngx-translate/core';
import {ObliqueTestingModule} from '@oblique/oblique';
import {HomeComponent} from './home.component';
import {skip} from 'rxjs/operators';
import {OauthService} from '../auth/oauth.service';
import {ActivatedRoute} from '@angular/router';
import {of} from 'rxjs';

describe('HomeComponent', () => {
let component: HomeComponent;
let fixture: ComponentFixture<HomeComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, ObliqueTestingModule],
providers: [
{
provide: TranslateService,
useValue: {
onLangChange: new EventEmitter<LangChangeEvent>(),
currentLang: 'en'
}
}
],
schemas: [NO_ERRORS_SCHEMA],
declarations: [HomeComponent]
}).compileComponents();
}));
describe('normal access', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, ObliqueTestingModule],
providers: [
{
provide: TranslateService,
useValue: {
onLangChange: new EventEmitter<LangChangeEvent>(),
currentLang: 'en'
}
},
{provide: OauthService, useValue: {logout: jest.fn()}}
],
schemas: [NO_ERRORS_SCHEMA],
declarations: [HomeComponent]
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(HomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
beforeEach(() => {
fixture = TestBed.createComponent(HomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
it('should create', () => {
expect(component).toBeTruthy();
});

describe('lang$', () => {
it('should be defined', () => {
expect(component.lang$).toBeDefined();
it('should not logout', () => {
const oauth = TestBed.inject(OauthService);
expect(oauth.logout).not.toHaveBeenCalled();
});

it('should emit an url', done => {
component.lang$.subscribe(url => {
expect(url).toBe('en');
done();
describe('lang$', () => {
it('should be defined', () => {
expect(component.lang$).toBeDefined();
});
});

it('should use emitted language', done => {
const translate = TestBed.inject(TranslateService);
component.lang$.pipe(skip(1)).subscribe(url => {
expect(url).toBe('de');
done();
it('should emit an url', done => {
component.lang$.subscribe(url => {
expect(url).toBe('en');
done();
});
});
translate.onLangChange.emit({
lang: 'de',
translations: {}

it('should use emitted language', done => {
const translate = TestBed.inject(TranslateService);
component.lang$.pipe(skip(1)).subscribe(url => {
expect(url).toBe('de');
done();
});
translate.onLangChange.emit({
lang: 'de',
translations: {}
});
});
});
});

describe('logout access', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ObliqueTestingModule],
providers: [
{
provide: TranslateService,
useValue: {
onLangChange: new EventEmitter<LangChangeEvent>(),
currentLang: 'en'
}
},
{provide: OauthService, useValue: {logout: jest.fn()}},
{provide: ActivatedRoute, useValue: {data: of({logout: true})}}
],
schemas: [NO_ERRORS_SCHEMA],
declarations: [HomeComponent]
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(HomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});

it('should logout', () => {
const oauth = TestBed.inject(OauthService);
expect(oauth.logout).toHaveBeenCalled();
});
});
});
7 changes: 5 additions & 2 deletions src/app/home/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {Component} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {map, startWith} from 'rxjs/operators';
import {ActivatedRoute} from '@angular/router';
import {filter, map, startWith} from 'rxjs/operators';
import {Observable} from 'rxjs';
import {OauthService} from '../auth/oauth.service';
import {environment} from '../../environments/environment';

@Component({
Expand All @@ -15,7 +17,8 @@ export class HomeComponent {
lang$: Observable<string>;
showWarning = environment.showWarning;

constructor(translate: TranslateService) {
constructor(translate: TranslateService, route: ActivatedRoute, oauthService: OauthService) {
route.data.pipe(filter(data => data.logout)).subscribe(() => oauthService.logout());
this.lang$ = translate.onLangChange.pipe(
map(lang => lang.lang),
startWith(translate.currentLang)
Expand Down

0 comments on commit c3d0d23

Please sign in to comment.