Skip to content

Commit aae29d4

Browse files
committed
fix failing tests
1 parent 03a4427 commit aae29d4

File tree

7 files changed

+147
-24
lines changed

7 files changed

+147
-24
lines changed

2-Authorization-I/1-call-graph/SPA/karma.conf.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ module.exports = function (config) {
3737
colors: true,
3838
logLevel: config.LOG_INFO,
3939
autoWatch: true,
40-
browsers: ["Chrome"],
40+
browsers: ['Chrome', 'ChromeHeadless', 'ChromeHeadlessCI'],
41+
customLaunchers: {
42+
ChromeHeadlessCI: {
43+
base: 'ChromeHeadless',
44+
flags: ['--no-sandbox']
45+
}
46+
},
4147
singleRun: false,
4248
restartOnFileChange: true,
4349
});

2-Authorization-I/1-call-graph/SPA/src/app/app.module.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ import { HomeComponent } from './home/home.component';
1818
import { ContactsComponent } from './contacts/contacts.component';
1919
import { AccountSwitchComponent } from './account-switch/account-switch.component';
2020

21-
22-
23-
2421
import {
2522
IPublicClientApplication,
2623
PublicClientApplication,
@@ -58,14 +55,14 @@ export function MSALInstanceFactory(): IPublicClientApplication {
5855
*/
5956
export function MsalGuardConfigurationFactory(): MsalGuardConfiguration {
6057
return {
61-
interactionType: InteractionType.Redirect,
58+
interactionType: InteractionType.Popup,
6259
authRequest: loginRequest
6360
};
6461
}
6562

6663
/**
67-
* MSAL Angular will automatically retrieve tokens for resources
68-
* added to protectedResourceMap. For more info, visit:
64+
* MSAL Angular will automatically retrieve tokens for resources
65+
* added to protectedResourceMap. For more info, visit:
6966
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-angular/docs/v2-docs/initialization.md#get-tokens-for-web-api-calls
7067
*/
7168
export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
@@ -100,18 +97,6 @@ export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
10097
};
10198
}
10299

103-
104-
/**
105-
* Set your default interaction type for MSALGuard here. If you have any
106-
* additional scopes you want the user to consent upon login, add them here as well.
107-
*/
108-
export function MSALGuardConfigFactory(): MsalGuardConfiguration {
109-
return {
110-
interactionType: InteractionType.Redirect,
111-
authRequest: loginRequest
112-
};
113-
}
114-
115100
@NgModule({
116101
declarations: [
117102
AppComponent,
@@ -157,6 +142,6 @@ export function MSALGuardConfigFactory(): MsalGuardConfiguration {
157142
MsalGuard,
158143
GraphService,
159144
],
160-
bootstrap: [AppComponent, MsalRedirectComponent],
145+
bootstrap: [AppComponent],
161146
})
162147
export class AppModule {}
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, MsalRedirectComponent} 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('/profile'));
66+
67+
expect(canNavigate).toBe(false);
68+
});
69+
});
70+
71+
function setup() {
72+
73+
function MSALGuardConfigFactory(): MsalGuardConfiguration {
74+
return {
75+
interactionType: InteractionType.Popup,
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+
}

3-Authorization-II/1-call-api/SPA/karma.conf.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ module.exports = function (config) {
3737
colors: true,
3838
logLevel: config.LOG_INFO,
3939
autoWatch: true,
40-
browsers: ['Chrome'],
40+
browsers: ['Chrome', 'ChromeHeadless', 'ChromeHeadlessCI'],
41+
customLaunchers: {
42+
ChromeHeadlessCI: {
43+
base: 'ChromeHeadless',
44+
flags: ['--no-sandbox']
45+
}
46+
},
4147
singleRun: false,
4248
restartOnFileChange: true
4349
});

3-Authorization-II/2-call-api-b2c/SPA/karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = function (config) {
2828
colors: true,
2929
logLevel: config.LOG_INFO,
3030
autoWatch: true,
31-
browsers: ['ChromeHeadlessCI'],
31+
browsers: ['Chrome', 'ChromeHeadless', 'ChromeHeadlessCI'],
3232
customLaunchers: {
3333
ChromeHeadlessCI: {
3434
base: 'ChromeHeadless',

5-AccessControl/1-call-api-roles/SPA/karma.conf.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ module.exports = function (config) {
3737
colors: true,
3838
logLevel: config.LOG_INFO,
3939
autoWatch: true,
40-
browsers: ['Chrome'],
40+
browsers: ['Chrome', 'ChromeHeadless', 'ChromeHeadlessCI'],
41+
customLaunchers: {
42+
ChromeHeadlessCI: {
43+
base: 'ChromeHeadless',
44+
flags: ['--no-sandbox']
45+
}
46+
},
4147
singleRun: false,
4248
restartOnFileChange: true
4349
});

5-AccessControl/2-call-api-groups/SPA/karma.conf.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ module.exports = function (config) {
3737
colors: true,
3838
logLevel: config.LOG_INFO,
3939
autoWatch: true,
40-
browsers: ['Chrome'],
40+
browsers: ['Chrome', 'ChromeHeadless', 'ChromeHeadlessCI'],
41+
customLaunchers: {
42+
ChromeHeadlessCI: {
43+
base: 'ChromeHeadless',
44+
flags: ['--no-sandbox']
45+
}
46+
},
4147
singleRun: false,
4248
restartOnFileChange: true
4349
});

0 commit comments

Comments
 (0)