(Linux)
cd frontend
docker run -e "JSON_CONFIG_PATH=/mnt/conf/config.json" -v `pwd`/cypress/auth_server:/mnt/conf -p 8080:8080 ghcr.io/navikt/mock-oauth2-server:0.3.4
cd backend/api
ASPNETCORE_ENVIRONMENT=Test dotnet run --no-launch-profile
API_URL needs to be set to run cypress tests. Either at startup, or in the .env file in frontend folder.
cd frontend
API_URL=http://localhost:5000 npm start
cd frontend
npm run cyrun
npm run cyopento open Cypress Runnernpm run cyrunto properly run tests
If multiple users/environments are needed, customize bmt commands by providing
FRONTEND_URL, API_URL and AUTH_URL environment variables:
backend:
FRONTEND_URL=http://localhost:3009 dotnet run --urls=http://localhost:5009
frontend:
API_URL=http://localhost:5009 npm start -- -p 3009
cypress:
npm run cyopen -- --env FRONTEND_URL=http://localhost:3009,API_URL=http://localhost:5009,AUTH_URL=http://localhost:8080
As we can't disable security during tests and are not allowed to create test users in real Azure Directory, we have to mock AAD Authentication. To achieve that we use mock-oauth2-server.
It can be run as a standalone server for example like that:
docker run -e "JSON_CONFIG_PATH=/mnt/conf/config.json" -v /your/path/to/frontend/cypress/auth_server:/mnt/conf -p 8080:8080 ghcr.io/navikt/mock-oauth2-server:0.3.4
Server runs by default on http://localhost:8080. It doesn't matter what we
call the issuer used instead of Microsoft, so
common was set
up.
Properties of interest in cypress/auth_server/config.json:
"interactiveLogin": falseallows authentication without providing any credentials"requestParam": "mock_claims_for"and"match": "user_identification"allow us to get the token with desired data"claims"is a mix of standard claims, claims required by Microsoft Authentication and current fusion version. Claims will have to be extended once fusion validations are updated.
Environment variable ASPNETCORE_ENVIRONMENT=Test must be set for backend to
pick up appsettings.Test.json configuration, and dotnet must be run with no
profile as Properties/launchSettings.json overrides environment.
Fields of interest in "AzureAd" section:
Authoritybetter match"iss"value inauth_server/config.jsonAudiencemust match"aud"value inauth_server/config.jsonMetadataAddressproperty must be set explicitly as it is not resolved correctly by MicrosoftIdentityWebApi by defaultRequireHttpsMetadatamust be disabled unless"ssl"section is configured inauth_server/config.json, keys/certificates are provided and all the code is updated to use https
Cypress has one major known limitation: it can't handle redirection to a totally different domain. Thus Cypress own docs recommend similar flows:
- conditionally disable real authentication code in the real app
- authenticate programmatically in Cypress
- save token in local storage
We can't fully use that flow because BMT authentication is fully handled by fusion. In dev environment (fusion-cli/start-app) authentication is required even before control returns to BMT. Hence current fusion code has at least 3 testability issues, which must be taken into account to minimize problems with future upgrades:
- automatic redirect to a different domain [Cypress limitation]
- hard-coded Microsoft as login authority
- old Implicit Grant flow [unsupported by mock-oauth2-server]
At the moment, with cache set correctly, Fusion performs only limited set of validations on data, like checking for token expiry. Hence we can bypass all the limitations by
- programmatically acquiring the token from mock-oauth2-server
- correctly setting FUSION_AUTH_CACHE to mock data and tokens
- only then navigating to frontend page
Note that restricted resources will still perform all the validations and reject our token.