问题描述
我想为我的 Angular 应用程序设置一些端到端测试,这需要使用 MSAL 库来对一些下游服务进行身份验证.当我尝试在本地运行 e2e 测试时,MSAL 库强制我使用用户名/密码进行身份验证.
I want to set up some end to end tests for my Angular application, which requires the use of the MSAL library to authenticate with some downstream services. When I try to run my e2e tests locally, the MSAL library is forcing me to authenticate with a username/password.
这是一个问题,因为我们的 CI/CD e2e 测试不应该有任何人为干预;因此,我正在寻找一种方法来绕过 MSAL 身份验证或设置服务帐户进行登录.不幸的是,关于 Angular 的 MSAL 的文档并不多(尤其是在 e2e 测试方面),但这似乎是其他人可能遇到的常见问题.
This is a problem because our CI/CD e2e testing should not have any human-intervention; thus I am looking for a way to either bypass the MSAL authentication or set up a service-account to login. Unfortunately there is not a lot of documentation surrounding MSAL for Angular (especially when it comes to e2e testing), but this seems like a common issue that others may have run into.
我已经尝试从我们的 app.module.ts 文件中禁用 MsalModule,但是当我尝试运行该应用程序时仍然提示我登录.我还看到一些文章试图以编程方式登录,但这对我们不起作用,因为从技术上讲,MSAL 不是我们能够接触的 Angular 组件.
I've tried to disable the MsalModule from our app.module.ts file but I am still prompted for login when I try to run the application. I've also seen some articles trying to programmatically log in, but this does not work for us as MSAL is technically not an Angular component we are able to touch.
app.module.ts:
app.module.ts:
@NgModule({
...
imports: [
...
MsalModule.forRoot({
clientID: '<client_id>',
authority: <microsoft_authority_url>,
validateAuthority: true,
redirectUri: "http://localhost:4200/",
cacheLocation : "localStorage",
postLogoutRedirectUri: "http://localhost:4200/",
navigateToLoginRequestUrl: true,
popUp: true,
consentScopes: [ "user.read"],
unprotectedResources: ["https://www.microsoft.com/en-us/"],
protectedResourceMap: protectedResourceMap,
logger: loggerCallback,
correlationId: '1234',
level: LogLevel.Info,
piiLoggingEnabled: true
})
],
entryComponents: [SaveDialogComponent,
GenericDialog, MassChangeDialogComponent],
providers: [TitleCasePipe,
{provide: HTTP_INTERCEPTORS, useClass: MsalInterceptor, multi: true}],
bootstrap: [AppComponent]
})
export class AppModule { }
预期结果:删除 MSAL 身份验证模块应该允许我们的应用程序运行而无需登录.
Expected result: removing the MSAL authentication module should allow our application to run without needing to log in.
实际结果:应用仍在提示登录,或无法正确呈现.
Actual result: application is still prompting for log in, or does not render correctly.
推荐答案
为了解决这个问题,我们设置了另一个配置文件来运行 e2e 测试.
In order to resolve this issue, we set up another config file for running e2e tests.
我们使用了一个不包含属性 canActivate 的自定义 ngular.routing.module:[MsalGuard]
We utilized a custom ngular.routing.module that does not contain the property canActivate: [MsalGuard]
这篇关于运行 Angular e2e 测试时如何禁用或绕过 MSAL 身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!