Skip to content

Commit

Permalink
Add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Jan 7, 2025
1 parent 20143b5 commit 2707eb8
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('ConfirmTotpComponent', () => {

fixture = TestBed.createComponent(ConfirmTotpComponent);
component = fixture.componentInstance;
fixture.detectChanges();
await fixture.whenStable();
});

it('should create', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/auth/features/register/register.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('RegisterComponent', () => {

fixture = TestBed.createComponent(RegisterComponent);
component = fixture.componentInstance;
fixture.detectChanges();
await fixture.whenStable();
});

it('should create', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('SetupTotpComponent', () => {

fixture = TestBed.createComponent(SetupTotpComponent);
component = fixture.componentInstance;
fixture.detectChanges();
await fixture.whenStable();
});

it('should create', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class AuthService {
}

/**
* Login user via OIDC
* Logout user via OIDC
*
* This returns a promise to trigger a redirect of the current window
* to the authorization endpoint of the OIDC provider.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('BrowseComponent', () => {

fixture = TestBed.createComponent(MetadataBrowserComponent);
component = fixture.componentInstance;
fixture.detectChanges();
await fixture.whenStable();
});

it('should create', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('HomePageComponent', () => {

fixture = TestBed.createComponent(HomePageComponent);
component = fixture.componentInstance;
fixture.detectChanges();
await fixture.whenStable();
});

it('should create', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('SiteFooterComponent', () => {

fixture = TestBed.createComponent(SiteFooterComponent);
component = fixture.componentInstance;
fixture.detectChanges();
await fixture.whenStable();
});

it('should create', () => {
Expand Down
63 changes: 57 additions & 6 deletions src/app/portal/features/site-header/site-header.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @license Apache-2.0
*/

import { signal } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

Expand All @@ -16,12 +17,20 @@ import { SiteHeaderComponent } from './site-header.component';
*/
class MockAuthService {
/**
* Pretend to be logged in
* @returns always true
* Initiate login
*/
isLoggedIn(): boolean {
return true;
login() {
this.isLoggedIn.update(() => true);
}

/**
* Initiate logout
*/
logout(): void {
this.isLoggedIn.set(false);
}

isLoggedIn = signal(false);
}

describe('SiteHeaderComponent', () => {
Expand All @@ -45,13 +54,55 @@ describe('SiteHeaderComponent', () => {
}).compileComponents();
});

beforeEach(() => {
beforeEach(async () => {
fixture = TestBed.createComponent(SiteHeaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
await fixture.whenStable();
});

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

it('should display a toolbar', () => {
const compiled = fixture.nativeElement as HTMLElement;
const toolbar = compiled.querySelector('mat-toolbar');
expect(toolbar).toBeTruthy();
});

it('should have a nav list inside the toolbar', () => {
const compiled = fixture.nativeElement as HTMLElement;
const toolbar = compiled.querySelector('mat-toolbar mat-nav-list');
expect(toolbar).toBeTruthy();
});

it('should login and logout', async () => {
const authService = TestBed.inject(AuthService);
expect(authService.isLoggedIn()).toBe(false);

const compiled = fixture.nativeElement as HTMLElement;

const loginButton: HTMLButtonElement = compiled.querySelector(
'mat-toolbar button[mat-icon-button]',
)!;
expect(loginButton).toBeTruthy();
expect(loginButton.textContent).toContain('login');

loginButton.click();

expect(authService.isLoggedIn()).toBe(true);

await fixture.whenStable();

const logoutButton: HTMLButtonElement = compiled.querySelector(
'mat-toolbar button[mat-icon-button]',
)!;
expect(logoutButton).toBeTruthy();
expect(loginButton).not.toBe(logoutButton);
expect(logoutButton.textContent).toContain('logout');

logoutButton.click();

expect(authService.isLoggedIn()).toBe(false);
});
});
60 changes: 60 additions & 0 deletions src/app/shared/services/config.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,75 @@ import { TestBed } from '@angular/core/testing';

import { ConfigService } from './config.service';

const mockConfig = {
base_url: 'https://portal.test',
auth_url: '/test/auth',
mass_url: '/test/mass',
metldata_url: '/test/metldata',
oidc_client_id: 'test-oidc-client-id',
oidc_redirect_url: 'test/redirect',
oidc_scope: 'some scope',
oidc_authority_url: 'https://login.test',
oidc_authorization_url: 'test/authorize',
oidc_token_url: 'test/token',
oidc_userinfo_url: 'test/userinfo',
oidc_use_discovery: true,
};

describe('ConfigService', () => {
let service: ConfigService;

beforeEach(() => {
window.config = mockConfig;

TestBed.configureTestingModule({});
service = TestBed.inject(ConfigService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});

it('should provide the base URL', () => {
expect(service.baseUrl).toBe('https://portal.test');
});

it('should provide the auth service URL', () => {
expect(service.authUrl).toBe('https://portal.test/test/auth');
});

it('should provide the MASS URL', () => {
expect(service.massUrl).toBe('https://portal.test/test/mass');
});

it('should provide the metldata service URL', () => {
expect(service.metldataUrl).toBe('https://portal.test/test/metldata');
});

it('should provide the OID client ID', () => {
expect(service.oidcClientId).toBe('test-oidc-client-id');
});

it('should provide the OIDC redirect URL', () => {
expect(service.oidcRedirectUrl).toBe('https://portal.test/test/redirect');
});

it('should provide the OIDC scope', () => {
expect(service.oidcScope).toBe('some scope');
});

it('should provide the OIDC authority URL', () => {
expect(service.oidcAuthorityUrl).toBe('https://login.test');
});

it('should provide the OIDC authorization URL', () => {
expect(service.oidcAuthorizationUrl).toBe('https://login.test/test/authorize');
});
it('should provide the OIDC token URL', () => {
expect(service.oidcTokenUrl).toBe('https://login.test/test/token');
});

it('should provide the OIDC userinfo URL', () => {
expect(service.oidcUserInfoUrl).toBe('https://login.test/test/userinfo');
});
});

0 comments on commit 2707eb8

Please sign in to comment.