Skip to content

Commit 86ee7a0

Browse files
committed
fix failing tests
1 parent aae29d4 commit 86ee7a0

File tree

2 files changed

+222
-16
lines changed

2 files changed

+222
-16
lines changed

6-AdvancedScenarios/2-call-api-mt/SPA/package-lock.json

Lines changed: 108 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { Router } from '@angular/router';
2+
import { ComponentFixture, TestBed } from '@angular/core/testing';
3+
import { RouterTestingModule } from '@angular/router/testing';
4+
5+
import { MSAL_GUARD_CONFIG, MsalGuardConfiguration} from '@azure/msal-angular';
6+
import { InteractionType } from '@azure/msal-browser';
7+
8+
import { msalConfig } from './auth-config';
9+
import { AppComponent } from './app.component';
10+
import { AppModule } from './app.module';
11+
12+
describe('Sanitize the configuration object', () => {
13+
14+
it('should define the config object', () => {
15+
expect(msalConfig).toBeDefined();
16+
expect(msalConfig.auth.clientId).toBeDefined();
17+
expect(msalConfig.auth.authority).toBeDefined();
18+
expect(msalConfig.auth.redirectUri).toBeDefined();
19+
});
20+
21+
it('should not contain credentials', () => {
22+
const regexGuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
23+
expect(regexGuid.test(msalConfig.auth.clientId)).toBe(false);
24+
});
25+
26+
it('should contain authority uri', () => {
27+
const regexUri = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi;
28+
expect(regexUri.test(msalConfig.auth.authority!)).toBe(true);
29+
});
30+
31+
it('should not contain tenant id', () => {
32+
const regexGuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
33+
expect(regexGuid.test(msalConfig.auth.authority!.split(".com/")[1])).toBe(false);
34+
});
35+
});
36+
37+
describe('Ensure that the app starts', () => {
38+
it('should boot the app', () => {
39+
const bootApplication = () => {
40+
const { router, run } = setup();
41+
42+
run(() => router.initialNavigation());
43+
};
44+
45+
expect(bootApplication).not.toThrow();
46+
});
47+
48+
it(`should have as title 'Microsoft identity platform'`, async () => {
49+
const { fixture } = setup();
50+
const app = fixture.debugElement.componentInstance;
51+
expect(app.title).toEqual('Microsoft identity platform');
52+
});
53+
54+
it('should navigate to unguarded route', async () => {
55+
const { router, run } = setup();
56+
57+
const canNavigate = await run(() => router.navigateByUrl('/'));
58+
59+
expect(canNavigate).toBe(true);
60+
});
61+
62+
it('should not navigate to guarded component', async () => {
63+
const { router, run } = setup();
64+
65+
const canNavigate = await run(() => router.navigateByUrl('/todo-view'));
66+
67+
expect(canNavigate).toBe(false);
68+
});
69+
});
70+
71+
function setup() {
72+
73+
function MSALGuardConfigFactory(): MsalGuardConfiguration {
74+
return {
75+
interactionType: InteractionType.Redirect,
76+
};
77+
}
78+
79+
TestBed.configureTestingModule({
80+
imports: [
81+
AppModule,
82+
RouterTestingModule,
83+
],
84+
providers: [
85+
{
86+
provide: MSAL_GUARD_CONFIG,
87+
useFactory: MSALGuardConfigFactory
88+
}
89+
]
90+
}).compileComponents();
91+
92+
let rootFixture: ComponentFixture<AppComponent>;
93+
const initializeRootFixture = () => {
94+
if (rootFixture == null) {
95+
rootFixture = TestBed.createComponent(AppComponent);
96+
}
97+
};
98+
99+
return {
100+
get router() {
101+
initializeRootFixture();
102+
103+
return TestBed.inject(Router);
104+
},
105+
run<TResult>(task: () => TResult) {
106+
initializeRootFixture();
107+
108+
return rootFixture.ngZone == null
109+
? task()
110+
: rootFixture.ngZone.run(task);
111+
},
112+
fixture: TestBed.createComponent(AppComponent)
113+
};
114+
}

0 commit comments

Comments
 (0)