Giter Club home page Giter Club logo

azureaduitestautomation's Introduction

UI testing an Azure AD protected single page app sample

This sample showcases how an Azure AD protected single page application using MSAL.js 2.x can be tested. It uses the ROPC authentication flow to acquire tokens for a test user account, and injects them into browser local storage before running the tests. This way MSAL.js does not attempt to acquire tokens as it already has them in cache.

Repository contents:

  • UiTestAutomation: ASP.NET Core application with API and React single page app
  • UiTestAutomation.Cypress: UI tests for the app built with Cypress and JavaScript
  • UiTestAutomation.Tests: UI tests for the app using Selenium and C#

Setup

To run this application, you will need to create an app registration and a user account in an Azure AD tenant. It is recommended that all of this is done in a development/test tenant and not your production environment.

Information for app registration:

  • Supported accounts: Accounts in this directory only
  • Redirect URI: https://localhost:44338 (Single Page Application type)
  • Add a client secret
  • Define a scope on the Expose an API tab
    • Accept the default app ID URI when asked or define your own
    • Name: Data.Read

Take note of:

  • Directory id/tenant id
  • Application id/client id
  • Client secret
  • App ID URI
  • Scope id (shown after creating a scope, it's app ID URI + scope name)

The user account should be a local user in the AAD tenant with no MFA enabled. Set a secure password on it. Take note of:

  • Username
  • Password

Now that we have all of this info, we can configure the app.

To configure the API, open UiTestAutomation/appsettings.json and specify values in the Authentication section:

  • Authority: https://login.microsoftonline.com/ + your directory id/tenant id
  • ClientId: your application id/client id
  • AppIdUri: your App ID URI

To configure the single page app, open UiTestAutomation/ClientApp/src/auth.js and specify:

  • clientId: your application id/client id
  • authority: https://login.microsoftonline.com/ + your directory id/tenant id
  • scopes: array with the scope id of the scope you defined earlier

Now the app can be run and tested. You should be able to view the weather forecast page that fetches data from the API. If you get the data, everything is working correctly.

To configure the Cypress tests, create UiTestAutomation.Cypress/cypress/support/authsettings.json, and fill it in based on the authsettings.sample.json in the support folder:

  • authority: https://login.microsoftonline.com/ + your directory id/tenant id
  • clientId: your application id/client id
  • clientSecret: your application client secret
  • apiScopes: array with the scope id of the scope you defined earlier
  • username: Username of the created user
  • password: Password of the created user

Configuring the Selenium tests is done through user secrets. The values are the same as Cypress tests, except apiScopes needs to be scopes instead in the user secrets.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.