Skip to content

Commit 9f92079

Browse files
authored
Merge pull request Azure-Samples#214 from Azure-Samples/fix-ci-script
Fix failing CI tests
2 parents 91fe453 + 86ee7a0 commit 9f92079

File tree

19 files changed

+387
-58
lines changed

19 files changed

+387
-58
lines changed

.github/workflows/node.js.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,52 +28,52 @@ jobs:
2828
cd 1-Authentication/1-sign-in/SPA
2929
npm ci
3030
npm audit --production
31-
npm run test --watch=false --no-progress --browsers=ChromeHeadlessCI
31+
npm run test
3232
3333
- run: |
3434
cd 1-Authentication/2-sign-in-b2c/SPA
3535
npm ci
3636
npm audit --production
37-
npm run test --watch=false --no-progress --browsers=ChromeHeadlessCI
37+
npm run test
3838
3939
- run: |
4040
cd 2-Authorization-I/1-call-graph/SPA
4141
npm ci
4242
npm audit --production
43-
npm run test --watch=false --no-progress --browsers=ChromeHeadlessCI
43+
npm run test
4444
4545
- run: |
4646
cd 3-Authorization-II/1-call-api/SPA
4747
npm ci
4848
npm audit --production
49-
npm run test --watch=false --no-progress --browsers=ChromeHeadlessCI
49+
npm run test
5050
5151
- run: |
5252
cd 3-Authorization-II/2-call-api-b2c/SPA
5353
npm ci
5454
npm audit --production
55-
npm run test --watch=false --no-progress --browsers=ChromeHeadlessCI
55+
npm run test
5656
5757
- run: |
5858
cd 5-AccessControl/1-call-api-roles/SPA
5959
npm ci
6060
npm audit --production
61-
npm run test --watch=false --no-progress --browsers=ChromeHeadlessCI
61+
npm run test
6262
6363
- run: |
6464
cd 5-AccessControl/2-call-api-groups/SPA
6565
npm ci
6666
npm audit --production
67-
npm run test --watch=false --no-progress --browsers=ChromeHeadlessCI
67+
npm run test
6868
6969
- run: |
7070
cd 6-AdvancedScenarios/1-call-api-obo/SPA
7171
npm ci
7272
npm audit --production
73-
npm run test --watch=false --no-progress --browsers=ChromeHeadlessCI
73+
npm run test
7474
7575
- run: |
7676
cd 6-AdvancedScenarios/2-call-api-mt/SPA
7777
npm ci
7878
npm audit --production
79-
npm run test --watch=false --no-progress --browsers=ChromeHeadlessCI
79+
npm run test

1-Authentication/1-sign-in/SPA/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"ng": "ng",
77
"start": "ng serve",
88
"build": "ng build",
9-
"test": "ng test",
9+
"test": "ng test --watch=false --no-progress --browsers=ChromeHeadlessCI",
1010
"lint": "ng lint",
1111
"e2e": "ng e2e"
1212
},

1-Authentication/2-sign-in-b2c/SPA/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"ng": "ng",
77
"start": "ng serve",
88
"build": "ng build",
9-
"test": "ng test",
9+
"test": "ng test --watch=false --no-progress --browsers=ChromeHeadlessCI",
1010
"lint": "ng lint",
1111
"e2e": "ng e2e"
1212
},

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/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"start": "ng serve",
77
"build": "ng build",
88
"watch": "ng build --watch --configuration development",
9-
"test": "ng test"
9+
"test": "ng test --watch=false --no-progress --browsers=ChromeHeadlessCI"
1010
},
1111
"private": true,
1212
"dependencies": {

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/1-call-api/SPA/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"start": "ng serve",
77
"build": "ng build",
88
"watch": "ng build --watch --configuration development",
9-
"test": "ng test"
9+
"test": "ng test --watch=false --no-progress --browsers=ChromeHeadlessCI"
1010
},
1111
"private": true,
1212
"dependencies": {

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',

0 commit comments

Comments
 (0)