From 67c5a64e34abe467c4d6c94fd584068365066fa7 Mon Sep 17 00:00:00 2001 From: Emanuel Dima Date: Thu, 11 May 2017 11:11:02 +0200 Subject: [PATCH 1/3] add basic end-to-end test with nightmare+jest+enzyme --- webui/.babelrc | 3 ++ webui/package.json | 15 ++++-- webui/src/__tests__/homepage.js | 85 +++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 webui/.babelrc create mode 100644 webui/src/__tests__/homepage.js diff --git a/webui/.babelrc b/webui/.babelrc new file mode 100644 index 0000000000..c213aff130 --- /dev/null +++ b/webui/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015", "react"] +} \ No newline at end of file diff --git a/webui/package.json b/webui/package.json index 0443ee4796..05f4799b51 100644 --- a/webui/package.json +++ b/webui/package.json @@ -6,14 +6,21 @@ "license": "GPL-2.0", "repository": "https://github.com/EUDAT-B2SHARE/b2share.git", "scripts": { - "postinstall": "./copy_files.sh" + "postinstall": "./copy_files.sh", + "test": "jest" }, "devDependencies": { "babel-core": "^6.2.1", + "babel-jest": "^20.0.0", "babel-loader": "^6.2.0", "babel-preset-es2015": "^6.1.18", "babel-preset-react": "^6.1.18", "css-loader": "^0.23.0", + "enzyme": "^2.8.1", + "enzyme-to-json": "^1.5.1", + "jest": "^20.0.0", + "nightmare": "^2.10.0", + "react-addons-test-utils": "^15.5.1", "style-loader": "^0.13.0", "uglify-loader": "^1.3.0", "webpack": "^1.12.14", @@ -26,9 +33,9 @@ "immutable": "^3.7.6", "moment": "^2.13.0", "pluralize": "^1.2.1", - "react": "^15.0.1", - "react-dom": "^15.0.1", - "react-addons-shallow-compare": "^15.0.1", + "react": "^15.5.0", + "react-dom": "^15.5.0", + "react-addons-shallow-compare": "^15.5.0", "react-router": "^2.1.1", "react-toggle": "^2.2.0", "react-widgets": "^3.2.4", diff --git a/webui/src/__tests__/homepage.js b/webui/src/__tests__/homepage.js new file mode 100644 index 0000000000..901e55d1af --- /dev/null +++ b/webui/src/__tests__/homepage.js @@ -0,0 +1,85 @@ +import Nightmare from 'nightmare' + +jasmine.DEFAULT_TIMEOUT_INTERVAL=20*1000; + +const B2ACCESS_AUTH_URL = "https://unity.eudat-aai.fz-juelich.de:8443/oauth2-as/oauth2-authz-web-entry" + +const user = process.env.AUTOTEST_USER; +const pass = process.env.AUTOTEST_PASS; +const email = process.env.AUTOTEST_EMAIL; + +if (!user || !pass || !email) { + console.log("user, pass and email environment variables not defined!"); + console.log(user, pass, email); +} + +var nightmare = Nightmare({ + show: true, + switches: { + 'ignore-certificate-errors': true, + }, +}); + +function step(msg) { + console.log(msg, "...") +} + +async function assertPageContains(page, text) { + let body = await page.evaluate(()=>document.body.textContent).end(); + expect(body).toContain(text); +} + +async function assertElementText(page, selector, text) { + const fn = (selector) => { + document.querySelector(selector).innerText + }; + let elementText = await page.evaluate(fn).end(); + expect(elementText == text); +} + +describe('Smoke test', function () { + let page = nightmare.goto('https://localhost/'); + + test('well formed homepage', async function () { + step("test homepage title") + expect(page.title() == 'B2SHARE'); + + step("wait for menu") + await page.wait('#header-navbar-collapse a'); + + step("wait for records") + await page.wait('#page .home-page .record a'); + }); + + test('user can login', async function () { + step("click on Login") + await page.click('#header-navbar-collapse > .user > li > a'); + + step("check if redirected to B2ACCESS") + await expect(page.url() == B2ACCESS_AUTH_URL); + + step("check if we are on B2ACCESS unity auth page") + await page.wait('#AuthenticationUI\\.username'); + await assertPageContains(page, "Login to UNITY OAuth2 Authorization Server"); + + step("authenticate") + await page.type('#AuthenticationUI\\.username', user) + .type('#WebPasswordRetrieval\\.password', pass) + .click('#AuthenticationUI\\.authnenticateButton'); + + step("check if we are on B2ACCESS unity confirmation page") + await assertPageContains(page, "A remote client has requested your authorization"); + + step("confirm") + await page.click('#IdpButtonsBar\\.confirmButton'); + + step("check if we are back to B2SHARE") + expect(page.title() == 'B2SHARE'); + + step("wait for menu") + await page.wait('#header-navbar-collapse a'); + + step("check that the test user is logged in") + await assertElementText(page, '#header-navbar-collapse > .user > li > a', email); + }); +}) From 67bfb1b0f3eedb4d35a4023be8138f0ed0491726 Mon Sep 17 00:00:00 2001 From: Emanuel Dima Date: Wed, 17 May 2017 15:16:31 +0200 Subject: [PATCH 2/3] fix b2access authentication workflow in ui tests --- webui/src/__tests__/homepage.js | 40 ++++++++++++++++----------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/webui/src/__tests__/homepage.js b/webui/src/__tests__/homepage.js index 901e55d1af..b4309bfa69 100644 --- a/webui/src/__tests__/homepage.js +++ b/webui/src/__tests__/homepage.js @@ -14,7 +14,7 @@ if (!user || !pass || !email) { } var nightmare = Nightmare({ - show: true, + // show: true, switches: { 'ignore-certificate-errors': true, }, @@ -25,20 +25,20 @@ function step(msg) { } async function assertPageContains(page, text) { - let body = await page.evaluate(()=>document.body.textContent).end(); - expect(body).toContain(text); + let body = await page.evaluate(()=>document.body.textContent); + await expect(body).toContain(text); } async function assertElementText(page, selector, text) { const fn = (selector) => { document.querySelector(selector).innerText }; - let elementText = await page.evaluate(fn).end(); - expect(elementText == text); + let elementText = await page.evaluate(fn, selector); + await expect(elementText == text); } describe('Smoke test', function () { - let page = nightmare.goto('https://localhost/'); + let page = nightmare.goto('https://b2share.local/'); test('well formed homepage', async function () { step("test homepage title") @@ -52,34 +52,32 @@ describe('Smoke test', function () { }); test('user can login', async function () { - step("click on Login") + step("click on Login"); await page.click('#header-navbar-collapse > .user > li > a'); - step("check if redirected to B2ACCESS") + step("check if redirected to B2ACCESS"); await expect(page.url() == B2ACCESS_AUTH_URL); - step("check if we are on B2ACCESS unity auth page") + step("check if we are on B2ACCESS unity auth page"); await page.wait('#AuthenticationUI\\.username'); await assertPageContains(page, "Login to UNITY OAuth2 Authorization Server"); - step("authenticate") + step("authenticate with user1"); await page.type('#AuthenticationUI\\.username', user) - .type('#WebPasswordRetrieval\\.password', pass) - .click('#AuthenticationUI\\.authnenticateButton'); + .type('#WebPasswordRetrieval\\.password', pass) + .click('#AuthenticationUI\\.authnenticateButton') + .wait('#IdpButtonsBar\\.confirmButton') + .click('#IdpButtonsBar\\.confirmButton'); - step("check if we are on B2ACCESS unity confirmation page") - await assertPageContains(page, "A remote client has requested your authorization"); - - step("confirm") - await page.click('#IdpButtonsBar\\.confirmButton'); - - step("check if we are back to B2SHARE") + step("check if we are back to B2SHARE"); expect(page.title() == 'B2SHARE'); - step("wait for menu") + step("wait for menu"); await page.wait('#header-navbar-collapse a'); - step("check that the test user is logged in") + step("check that the test user is logged in"); await assertElementText(page, '#header-navbar-collapse > .user > li > a', email); + + await page.end(); }); }) From 113bf3595d7a54f52fd5384af1ff9e0942574d14 Mon Sep 17 00:00:00 2001 From: Emanuel Dima Date: Thu, 18 May 2017 12:36:34 +0200 Subject: [PATCH 3/3] fix async tests --- webui/src/__tests__/homepage.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/webui/src/__tests__/homepage.js b/webui/src/__tests__/homepage.js index b4309bfa69..d7888e3085 100644 --- a/webui/src/__tests__/homepage.js +++ b/webui/src/__tests__/homepage.js @@ -2,6 +2,7 @@ import Nightmare from 'nightmare' jasmine.DEFAULT_TIMEOUT_INTERVAL=20*1000; +const B2SHARE_URL = "https://b2share.local/" const B2ACCESS_AUTH_URL = "https://unity.eudat-aai.fz-juelich.de:8443/oauth2-as/oauth2-authz-web-entry" const user = process.env.AUTOTEST_USER; @@ -23,6 +24,11 @@ var nightmare = Nightmare({ function step(msg) { console.log(msg, "...") } +function print_obj(o) { + for (var p in o) { + console.log(p) + } +} async function assertPageContains(page, text) { let body = await page.evaluate(()=>document.body.textContent); @@ -34,15 +40,15 @@ async function assertElementText(page, selector, text) { document.querySelector(selector).innerText }; let elementText = await page.evaluate(fn, selector); - await expect(elementText == text); + return expect(elementText == text); } describe('Smoke test', function () { - let page = nightmare.goto('https://b2share.local/'); - test('well formed homepage', async function () { - step("test homepage title") - expect(page.title() == 'B2SHARE'); + let page = nightmare.goto(B2SHARE_URL); + + step("test homepage title"); + expect(await page.title() == 'B2SHARE'); step("wait for menu") await page.wait('#header-navbar-collapse a'); @@ -50,10 +56,15 @@ describe('Smoke test', function () { step("wait for records") await page.wait('#page .home-page .record a'); }); +}); +describe('Smoke test2', function () { test('user can login', async function () { + let page = nightmare.goto(B2SHARE_URL); + step("click on Login"); - await page.click('#header-navbar-collapse > .user > li > a'); + await page.wait('#header-navbar-collapse > .user > li > a') + .click('#header-navbar-collapse > .user > li > a'); step("check if redirected to B2ACCESS"); await expect(page.url() == B2ACCESS_AUTH_URL); @@ -77,7 +88,5 @@ describe('Smoke test', function () { step("check that the test user is logged in"); await assertElementText(page, '#header-navbar-collapse > .user > li > a', email); - - await page.end(); }); -}) +});