本文介绍了运行Angular e2e测试时如何禁用或绕过MSAL身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的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身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 13:59