-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cypress: login via azure active directory
- Loading branch information
1 parent
c836525
commit ed1bf69
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
describe('Azure Active Directory Authentication', () => { | ||
beforeEach(() => { | ||
// log into Azure Active Directory through our sample SPA using our custom command | ||
cy.loginToAAD(Cypress.env('aad_username'), Cypress.env('aad_password')) | ||
cy.visit('/') | ||
}) | ||
|
||
it('verifies the user logged in has the correct name', () => { | ||
cy.get('#table-body-div td:contains("name") + td').should( | ||
'contain', | ||
`${Cypress.env('aad_name')}` | ||
) | ||
}) | ||
|
||
it('verifies the user logged in has the correct preferred name', () => { | ||
cy.get('#table-body-div td:contains("preferred_username") + td').should( | ||
'contain', | ||
`${Cypress.env('aad_username')}` | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// @ts-check | ||
///<reference path="../global.d.ts" /> | ||
|
||
// *********************************************** | ||
// This example commands.ts shows you how to | ||
// create various custom commands and overwrite | ||
// existing commands. | ||
// | ||
// For more comprehensive examples of custom | ||
// commands please read more here: | ||
// https://on.cypress.io/custom-commands | ||
// *********************************************** | ||
// | ||
// | ||
// -- This is a parent command -- | ||
// Cypress.Commands.add('login', (email, password) => { ... }) | ||
// | ||
// | ||
// -- This is a child command -- | ||
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... }) | ||
// | ||
// | ||
// -- This is a dual command -- | ||
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... }) | ||
// | ||
// | ||
// -- This will overwrite an existing command -- | ||
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... }) | ||
// | ||
// declare global { | ||
// namespace Cypress { | ||
// interface Chainable { | ||
// login(email: string, password: string): Chainable<void> | ||
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element> | ||
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element> | ||
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element> | ||
// } | ||
// } | ||
// } | ||
|
||
function loginViaAAD(username: string, password: string) { | ||
cy.visit('/') | ||
cy.get('button#signIn').click() | ||
|
||
// Login to your AAD tenant. | ||
cy.origin( | ||
'login.microsoftonline.com', | ||
{ | ||
args: { | ||
username, | ||
}, | ||
}, | ||
({ username }) => { | ||
cy.get('input[type="email"]').type(username, { | ||
log: false, | ||
}) | ||
cy.get('input[type="submit"]').click() | ||
} | ||
) | ||
|
||
// depending on the user and how they are registered with Microsoft, the origin may go to live.com | ||
cy.origin( | ||
'login.live.com', | ||
{ | ||
args: { | ||
password, | ||
}, | ||
}, | ||
({ password }) => { | ||
cy.get('input[type="password"]').type(password, { | ||
log: false, | ||
}) | ||
cy.get('button[type="submit"]').click() | ||
cy.get('#acceptButton').click() | ||
} | ||
) | ||
|
||
// Ensure Microsoft has redirected us back to the sample app with our logged in user. | ||
cy.location().should('equal', 'http://localhost:3000/') | ||
cy.get('#welcome-div').should( | ||
'contain', | ||
`Welcome ${Cypress.env('aad_username')}!` | ||
) | ||
} | ||
|
||
Cypress.Commands.add('loginToAAD', (username: string, password: string) => { | ||
cy.session( | ||
`aad-${username}`, | ||
() => { | ||
const log = Cypress.log({ | ||
displayName: 'Azure Active Directory Login', | ||
message: [`🔐 Authenticating | ${username}`], | ||
// @ts-ignore | ||
autoEnd: false, | ||
}) | ||
|
||
log.snapshot('before') | ||
|
||
loginViaAAD(username, password) | ||
|
||
log.snapshot('after') | ||
log.end() | ||
}, | ||
{ | ||
validate: () => { | ||
// this is a very basic form of session validation for this demo. | ||
// depending on your needs, something more verbose might be needed | ||
cy.visit('/') | ||
cy.get('#welcome-div').should( | ||
'contain', | ||
`Welcome ${Cypress.env('aad_username')}!` | ||
) | ||
}, | ||
} | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// *********************************************************** | ||
// This example support/e2e.ts is processed and | ||
// loaded automatically before your test files. | ||
// | ||
// This is a great place to put global configuration and | ||
// behavior that modifies Cypress. | ||
// | ||
// You can change the location of this file or turn off | ||
// automatically serving support files with the | ||
// 'supportFile' configuration option. | ||
// | ||
// You can read more here: | ||
// https://on.cypress.io/configuration | ||
// *********************************************************** | ||
|
||
// Import commands.js using ES2015 syntax: | ||
import './commands' | ||
|
||
// Alternatively you can use CommonJS syntax: | ||
// require('./commands') |