Skip to content

Commit

Permalink
support okta with cypress tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atomfrede committed Jan 19, 2021
1 parent c7b5fb1 commit 31977cd
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 117 deletions.
1 change: 1 addition & 0 deletions generators/client/templates/angular/package.json.ejs
Expand Up @@ -87,6 +87,7 @@
<%_ } _%>
<%_ if (cypressTests) { _%>
"cypress": "VERSION_MANAGED_BY_CLIENT_COMMON",
"puppeteer": "VERSION_MANAGED_BY_CLIENT_COMMON",
<%_ } _%>
<%_ if (enableTranslation) { _%>
"merge-jsons-webpack-plugin": "VERSION_MANAGED_BY_CLIENT_ANGULAR",
Expand Down
1 change: 1 addition & 0 deletions generators/client/templates/common/package.json
Expand Up @@ -2,6 +2,7 @@
"devDependencies": {
"concurrently": "5.3.0",
"cypress": "6.2.1",
"puppeteer": "5.5.0",
"wait-on": "5.2.1",
"husky": "4.3.8",
"lint-staged": "10.5.3"
Expand Down
1 change: 1 addition & 0 deletions generators/client/templates/react/package.json.ejs
Expand Up @@ -162,6 +162,7 @@
<%_ } _%>
<%_ if (cypressTests) { _%>
"cypress": "VERSION_MANAGED_BY_CLIENT_COMMON",
"puppeteer": "VERSION_MANAGED_BY_CLIENT_COMMON",
<%_ } _%>
"typescript": "VERSION_MANAGED_BY_CLIENT_REACT",
<%_ if (protractorTests) { _%>
Expand Down
1 change: 1 addition & 0 deletions generators/client/templates/vue/package.json.ejs
Expand Up @@ -119,6 +119,7 @@
<%_ } _%>
<%_ if (cypressTests) { _%>
"cypress": "VERSION_MANAGED_BY_CLIENT_COMMON",
"puppeteer": "VERSION_MANAGED_BY_CLIENT_COMMON",
<%_ } _%>
"rimraf": "VERSION_MANAGED_BY_CLIENT_VUE",
"sass": "VERSION_MANAGED_BY_CLIENT_VUE",
Expand Down
3 changes: 1 addition & 2 deletions generators/cypress/files.js
Expand Up @@ -42,7 +42,6 @@ const cypressFiles = {
condition: generator => generator.cypressTests,
path: TEST_SRC_DIR,
templates: [
'cypress/fixtures/users/user.json',
'cypress/fixtures/integration-test.png',
'cypress/plugins/index.ts',
'cypress/integration/administration/administration.spec.ts',
Expand Down Expand Up @@ -71,7 +70,7 @@ const cypressFiles = {
{
condition: generator => generator.cypressTests && generator.authenticationType === 'oauth2',
path: TEST_SRC_DIR,
templates: ['cypress/support/keycloak-oauth2.ts'],
templates: ['cypress/support/oauth2.ts'],
},
],
};
Expand Down
6 changes: 6 additions & 0 deletions generators/cypress/index.js
Expand Up @@ -93,6 +93,12 @@ module.exports = class extends BaseBlueprintGenerator {
// Public API method used by the getter and also by Blueprints
_writing() {
return {
cleanup() {
if (this.isJhipsterVersionLessThan('7.0.0-beta.1') && this.jhipsterConfig.cypressTests) {
this.removeFile(`${this.TEST_SRC_DIR}/cypress/support/keycloak-oauth2.ts`);
this.removeFile(`${this.TEST_SRC_DIR}/cypress/fixtures/users/user.json`);
}
},
...writeFiles(),
...super._missingPostWriting(),
};
Expand Down
Expand Up @@ -13,17 +13,11 @@ import {
describe('/admin', () => {
<%_ if (authenticationType === 'oauth2') { _%>
beforeEach(() => {
cy.getOauth2Data();
cy.get('@oauth2Data').then(oauth2Data => {
cy.keycloackLogin(oauth2Data, 'user');
});
cy.oauthLogin(Cypress.env('E2E_USERNAME') || "admin", Cypress.env('E2E_PASSWORD') || "admin");
});

afterEach(() => {
cy.get('@oauth2Data').then(oauth2Data => {
cy.keycloackLogout(oauth2Data);
});
cy.clearCache();
cy.oauthLogout();
});
<%_ } else { _%>
before(() => {
Expand Down
Expand Up @@ -23,4 +23,43 @@ module.exports = (on, config) => {
return launchOptions
}
});

on('task', {
login({ baseUrl, username, password }) {
return (async () => {
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
devtools: true,
ignoreHTTPSErrors: true,
headless: true });

const page = await browser.newPage();

await page.goto(`${baseUrl}/oauth2/authorization/oidc`, { // The app redirects to the login-page
waitUntil: 'networkidle2' // Wait until login-page has been reached
});

await page.type('[name="username"]', username); // Insert username in form
await page.type('[name="password"]', password); // Insert password
await page.click('input[type="submit"]'); // Click login button

await page.waitForNavigation({ waitUntil: 'networkidle2' }); // Wait until redirected back to the app

const authCookies = await page.cookies();

await browser.close(); // Close puppeteer
return authCookies;
})();
},
logout(baseUrl) {
return (async () => {
const browser = await puppeteer.launch({ ignoreHTTPSErrors: true });
const page = await browser.newPage();
await page.goto(`${baseUrl}/api/logout`, { // The app redirects to the login-page
waitUntil: 'networkidle2' // Wait until login-page has been reached
});
return true;
})
}
});
}
Expand Up @@ -17,7 +17,7 @@ import './commands';
import './navbar';
import './entity';
<%_ if (authenticationType === 'oauth2') { _%>
import './keycloak-oauth2';
import './oauth2';
<%_ } _%>
<%_ if (authenticationType === 'session') { _%>
Cypress.Cookies.defaults({
Expand Down

This file was deleted.

@@ -0,0 +1,47 @@

/* eslint-disable @typescript-eslint/camelcase */
/* eslint-disable @typescript-eslint/no-namespace */
/* eslint-disable @typescript-eslint/no-use-before-define */
// eslint-disable-next-line spaced-comment
/// <reference types="cypress" />

Cypress.Commands.add('oauthLogin', (username, password) => {
cy.task('login', {baseUrl: Cypress.config().baseUrl, username: username, password: password}).then(cookies => {
cookies.forEach(c => {
const name = c.name;
const value = c.value;
const options = {
...c
}
cy.setCookie(name, value, options);
});
cy.visit('/');
});
});

Cypress.Commands.add('oauthLogout', (username, password) => {
cy.task('logout', {baseUrl: Cypress.config().baseUrl}).then(() => {
cy.clearCookies();
});
});

Cypress.Commands.add('clearCache', () => {
cy.clearCookies();
cy.clearLocalStorage();
cy.window().then(win => {
win.sessionStorage.clear();
});
});

declare global {
namespace Cypress {
interface Chainable<Subject> {
oauthLogin(username: string, password: string): Cypress.Chainable;
oauthLogout(): Cypress.Chainable;
clearCache(): Cypress.Chainable;
}
}
}

// Convert this to a module instead of script (allows import/export)
export {};
Expand Up @@ -39,10 +39,7 @@ describe('<%= entityClass %> e2e test', () => {

<%_ if (authenticationType === 'oauth2') { _%>
beforeEach(() => {
cy.getOauth2Data();
cy.get('@oauth2Data').then(oauth2Data => {
cy.keycloackLogin(oauth2Data, 'user');
});
cy.oauthLogin(Cypress.env('E2E_USERNAME') || "admin", Cypress.env('E2E_PASSWORD') || "admin");
cy.intercept('GET', '/<%= baseApi + entityApiUrl %>*').as('entitiesRequest');
cy.visit('');
cy.clickOnEntityMenuItem('<%= entityStateName %>');
Expand All @@ -52,10 +49,7 @@ describe('<%= entityClass %> e2e test', () => {
});

afterEach(() => {
cy.get('@oauth2Data').then(oauth2Data => {
cy.keycloackLogout(oauth2Data);
});
cy.clearCache();
cy.oauthLogout();
});
<%_ } else { _%>
before(() => {
Expand Down

0 comments on commit 31977cd

Please sign in to comment.