<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Frank Lores-Penalver on Medium]]></title>
        <description><![CDATA[Stories by Frank Lores-Penalver on Medium]]></description>
        <link>https://medium.com/@franklores?source=rss-ae43242297ab------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*LpUevtilO7Fh1e9a</url>
            <title>Stories by Frank Lores-Penalver on Medium</title>
            <link>https://medium.com/@franklores?source=rss-ae43242297ab------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 19:29:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@franklores/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using Cypress to test Azure Active Directory protected Single-Page Applications]]></title>
            <link>https://medium.com/version-1/using-cypress-to-test-azure-active-directory-protected-spas-47d04f5add9?source=rss-ae43242297ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/47d04f5add9</guid>
            <category><![CDATA[azure-active-directory]]></category>
            <category><![CDATA[cypress-testing]]></category>
            <category><![CDATA[single-page-applications]]></category>
            <dc:creator><![CDATA[Frank Lores-Penalver]]></dc:creator>
            <pubDate>Wed, 17 Aug 2022 09:35:05 GMT</pubDate>
            <atom:updated>2023-01-25T12:06:51.835Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*zD2pOejxiQYdVyMHZBxCdg.png" /></figure><p>Testing Single-Page Applications that use the OAuth2 Authorisation Code flow can be challenging because the user is redirected from the application&#39;s main page to the authorisation server. In this article, we will discuss how to overcome this challenge when testing Azure Active Directory-protected Single-Page Applications using Cypress.</p><p><strong>These are the components for the test scenario:</strong></p><p><a href="https://www.cypress.io/"><strong>Cypress</strong></a> is an all-in-one, end-to-end testing framework and assertion library which allows you to automate tests for the user interface of your web application.</p><p><a href="https://azure.microsoft.com/en-gb/services/active-directory/"><strong>Azure AD</strong></a> is a cloud-based identity and access management service which implements the OAuth2 specification.</p><p><a href="https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-js-initializing-client-applications"><strong>MSAL.js</strong></a> is a javascript library, written and maintained by Microsoft that allows SPAs to authenticate users with Azure AD.</p><p>The authorisation flow recommended by OAuth2 for SPAs is the Authorisation Code flow which is summarised below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*bGTIvzTvrSDDFn_soIfj9g.png" /><figcaption>MSAL.js acquires tokens using Authorisation Code with PKCE flow.</figcaption></figure><ol><li>The user navigates to the single-page app.</li><li>If the user is not authenticated, they are redirected to Azure AD to authenticate.</li><li>After successful authentication, Azure AD returns an authorisation code to the SPA.</li><li>The SPA exchanges the authorisation code for an id token, an access token and a refresh token.</li><li>The SPA securely stores the tokens in the local storage.</li><li>The SPA passes the access token on request to the backend API, also protected by Azure AD.</li></ol><p>When we attempt to write Cypress automated tests for an Azure AD-protected Single-Page Application (SPA), we need to replicate the process of acquiring tokens without a user present. This, however, presents a challenge…</p><blockquote><strong><em>Related reads:</em></strong> <a href="https://www.version1.com/about-us/our-technology-partnerships/microsoft/">Why Choose Version 1 as your Microsoft Partner?</a></blockquote><p>We first considered writing a test for the login page, however, <a href="https://docs.cypress.io/guides/references/best-practices#Visiting-external-sites">Cypress discourages testing third-party external sites</a> (Azure AD login page) for several reasons such as:</p><ol><li>We have no control over the design of the said page, and our test could break if the 3rd party changes the layout/elements of the page.</li><li>The 3rd party site may be having issues outside of our control.</li></ol><p>Secondly, we considered trying to replace the redirection with our code, login and get the access token before we run our tests; there are several articles on this subject on how to achieve this. Still, these articles describe using either the OAuth Implicit or the OAuth Client Credentials flow; both are unsuitable for our scenario for the reasons outlined below:</p><ul><li>The OAuth Implicit flow presents security issues and is <a href="https://datatracker.ietf.org/doc/html/draft-ietf-oauth-security-topics-09#section-2.1.2">no longer recommended for use</a>.</li><li>The OAuth Client Credentials flow does not give us the necessary claims that our API backend needs, specifically scope claims.</li></ul><p>Finally, we settled on using the Resource Owner Password Credentials (ROPC) flow. This flow is not without its drawbacks, and <a href="https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth-ropc">Microsoft does not recommend</a> its use unless there is a high degree of trust in the application. However, we considered automated Cypress tests to be secure, as there are no users present.</p><p>The approach we followed replaces MSAL.js for acquiring the token with our code and saves the tokens into local storage in the shape/format that MSAL.js expects, hence tricking the MSAL.js library into thinking that the user has already authenticated and not redirecting the user to the Microsoft login page.</p><p>The steps of the Resource Owner Password Credentials flow are summarised below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*OR1EPvlR-puzf3ZVcFCaxA.png" /><figcaption>Cypress acquiries tokens using ROPC Flow</figcaption></figure><ol><li>Cypress script sends authentication details to Azure AD’s token endpoint using the ROPC flow.</li><li>Azure AD returns an id token, an access token and a refresh token.</li><li>Cypress script stores the tokens in local storage in the format expected by the MSAL.js library.</li><li>Cypress runs automated test scripts on the UI.</li><li>UI, using MSAL.js, collects the tokens from local storage.</li><li>UI passes the access token on the requests sent to the backend API.</li></ol><h4><strong>To follow this approach you will need the following information:</strong></h4><ul><li>An Azure AD account with MFA disabled, this account should only have the minimum access required to test the application.</li><li>Azure AD Application details for your UI: Directory Id/Tenant Id, Application Id/Client Id, Client Secret.</li><li>Azure AD Application details for your API: Directory Id/Tenant Id, Application Id/Client Id, App ID URI and Scope ID.</li></ul><p>In your Cypress project add json configuration file to save the authentication settings:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a7852458d99f4d1b81de8e7e63f5e79/href">https://medium.com/media/3a7852458d99f4d1b81de8e7e63f5e79/href</a></iframe><p>Next, create a support module for authorisation, this module will handle the authentication and will save the authorisation details to local storage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2019a8a60b122c100575ba338210e2e6/href">https://medium.com/media/2019a8a60b122c100575ba338210e2e6/href</a></iframe><p>The format for the key-value pairs added to local storage are as follows:</p><pre><strong>Id Token key:</strong><br>[homeAccountId]-[environment]-idtoken-[clientId]-[tenantId]-</pre><pre><strong>Id Token value:</strong><br>clientId: [your app id]<br>credentialType: &quot;IdToken&quot;<br>environment: &quot;login.windows.net&quot;<br>homeAccountId: [oid or sid].[tid] //these values come from the id-token<br>realm: [tid]</pre><pre>--------------------------------------------------------------------</pre><pre><strong>Access Token key:</strong><br>[homeAccountId]-[environment]-accesstoken-[clientId]-[tenantId]-[api-scope (lowercase)]</pre><pre><strong>Access Token value:</strong><br>cachedAt: [timestamp]<br>clientId: [your app id]<br>credentialType: &quot;AccessToken&quot;<br>environment: &quot;login.windows.net&quot;<br>expiresOn: [timestamp + expiresIn] //expiresIn is the expire_in from the token response<br>extendedExpiresOn: [timestamp + extendedExpiresIn] //ext_expires_in from the token response<br>homeAccountId: [oid or sid].[tid], //these values come from the access_token<br>realm: [tid]<br>secret: [access_token] //from token response<br>target: [api_scope]<br>tokenType: &quot;Bearer&quot;</pre><pre>--------------------------------------------------------------------</pre><pre><strong>Refresh Token key:<br></strong>[homeAccountId]-[environment]-refreshtoken-[clientId]--</pre><pre><strong>Refresh Token value:<br></strong>clientId: [your app id]<br>credentialType: &quot;RefreshToken&quot;<br>environment: &quot;login.windows.net&quot;<br>homeAccountId: [oid or sid].[tid], //these values come from the access token or id token<br>secret: [refresh_token] //from token response</pre><pre>--------------------------------------------------------------------</pre><pre><strong>Account key:</strong><br>[homeAccountId]-[environment]-[tenantId]</pre><pre><strong>Account value:<br>authorityType:</strong> &quot;MSSTS&quot;<br><strong>clientInfo:</strong> &quot;&quot;<br><strong>environment:</strong> &quot;login.windows.net&quot;<br><strong>homeAccountId:</strong> [oid or sid].[tid], //these values come from the access token or id token<br><strong>idTokenClaims:</strong> map of id_token claims (aud, exp, iat, iss, name, nbf, nonce, oir, preferred_username, rh, sub, tid, uti, ver)<br><strong>localAccountId:</strong> [oid or sid]<br><strong>name:</strong> name claim from id_token<br><strong>realm:</strong> [tid]<br><strong>username:</strong> preferred_name claim from id_token</pre><p>The information from which these values are derived are found in the id_token and access_token returned from the authentication process:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*3ELLlqbiMypybbNo5ebhJw.png" /><figcaption>id_token</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*LVgsiliyejUJ_S8Hry6B8w.png" /><figcaption>access_token</figcaption></figure><p>Next, we can create a cypress command to invoke our login function from cypress tests:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/332ec15b077036b89a23fa3c4008b88b/href">https://medium.com/media/332ec15b077036b89a23fa3c4008b88b/href</a></iframe><p>Finally, write your tests invoking the login command:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed384c7e880e9b0f2f62743710595c3a/href">https://medium.com/media/ed384c7e880e9b0f2f62743710595c3a/href</a></iframe><p>The main inspiration for this article is this <a href="https://www.youtube.com/watch?v=OZh5RmCztrU">excellent talk</a> by Joonas Westlin.</p><p>All done!</p><p><strong>References</strong></p><ul><li><a href="https://www.youtube.com/watch?v=OZh5RmCztrU">UI testing Azure AD protected single-page applications with Cypress — Joonas Westlin</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Saz8YlWzGPLBPNOSMUlbHg.jpeg" /></figure><p><strong>About the Author:<br></strong>Frank Lores-Penalver is a solution Architect here at Version 1.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47d04f5add9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/version-1/using-cypress-to-test-azure-active-directory-protected-spas-47d04f5add9">Using Cypress to test Azure Active Directory protected Single-Page Applications</a> was originally published in <a href="https://medium.com/version-1">Version 1</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>