diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 682f2937..71563304 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,25 +9,42 @@ jobs: unit-tests: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - uses: actions/setup-node@v4 - with: - node-version: 20 - - run: npm --prefix users/authservice ci - - run: npm --prefix users/userservice ci - - run: npm --prefix gatewayservice ci - - run: npm --prefix questionservice ci - - run: npm --prefix statsservice ci - - run: npm --prefix webapp ci - - run: npm --prefix users/authservice test -- --coverage - - run: npm --prefix users/userservice test -- --coverage - - run: npm --prefix gatewayservice test -- --coverage - - run: npm --prefix statsservice test -- --coverage - - run: npm --prefix webapp test -- --coverage - - name: Analyze with SonarCloud - uses: sonarsource/sonarcloud-github-action@master - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} \ No newline at end of file + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: actions/setup-node@v4 + with: + node-version: 20 + - run: npm --prefix users/authservice ci + - run: npm --prefix users/userservice ci + - run: npm --prefix gatewayservice ci + - run: npm --prefix questionservice ci + - run: npm --prefix statsservice ci + - run: npm --prefix webapp ci + - run: npm --prefix users/authservice test -- --coverage + - run: npm --prefix users/userservice test -- --coverage + - run: npm --prefix gatewayservice test -- --coverage + - run: npm --prefix statsservice test -- --coverage + - run: npm --prefix webapp test -- --coverage + - name: Analyze with SonarCloud + uses: sonarsource/sonarcloud-github-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + + e2e-tests: + needs: [unit-tests] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20 + - run: npm --prefix users/authservice install + - run: npm --prefix users/userservice install + - run: npm --prefix gatewayservice install + - run: npm --prefix questionservice install + - run: npm --prefix statsservice install + - run: npm --prefix webapp install + - run: npm --prefix webapp run build + - run: npm --prefix webapp run test:e2e diff --git a/docker-compose.yml b/docker-compose.yml index eefce7ac..f76c1dd3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -101,6 +101,7 @@ services: - "3000:3000" environment: REACT_APP_API_ENDPOINT: http://gatewayservice:8000 + restart: always prometheus: image: prom/prometheus @@ -115,6 +116,7 @@ services: - "9090:9090" depends_on: - gatewayservice + restart: always grafana: image: grafana/grafana @@ -134,6 +136,7 @@ services: - "9091:9091" depends_on: - prometheus + restart: always volumes: diff --git a/webapp/e2e/steps/add-friend.steps.js b/webapp/e2e/steps/add-friend.steps.js deleted file mode 100644 index 8175654c..00000000 --- a/webapp/e2e/steps/add-friend.steps.js +++ /dev/null @@ -1,87 +0,0 @@ -const puppeteer = require("puppeteer"); -const { defineFeature, loadFeature } = require("jest-cucumber"); -const setDefaultOptions = require("expect-puppeteer").setDefaultOptions; -const { expect } = require("expect-puppeteer"); - -const feature = loadFeature("./features/add-friend.feature"); - -let page; -let browser; - -defineFeature(feature, (test) => { - beforeAll(async () => { - browser = process.env.GITHUB_ACTIONS - ? await puppeteer.launch({ headless: 'new', slowMo: 100 }) - : await puppeteer.launch({ headless: 'new', slowMo: 100 }); - page = await browser.newPage(); - setDefaultOptions({ timeout: 10000 }); - - await page.goto("http://localhost:3000", { - waitUntil: "networkidle0", - }); - }); - let username; - let password; - let userCountBefore; - test("The user can add a friend", ({ given, when, then }) => { - given("A logged-in user and another user", async () => { - await expect(page).toClick("a", { text: "Regístrate" }); - username="Friend"; - password="Friend123"; - await page.waitForSelector('#register-username'); - await page.type('#register-username', username); - await page.waitForSelector('#register-password'); - await page.type('#register-password', password); - await page.waitForSelector('#register-pass2'); - await page.type('#register-pass2', password); - await page.click("button", { text: "Registrarse" }); - - await page.waitForTimeout(1000); - - await page.click('button[aria-label="Abrir menú"]'); - await page.waitForSelector('[data-testid="home-logout-link"]'); - await page.click('[data-testid="home-logout-link"]'); - - username = "testuser"; - password = "Testpassword1"; - await page.waitForSelector("#login-username"); - await page.type("#login-username", username); - await page.waitForSelector("#login-password"); - await page.type("#login-password", password); - await page.click("button", { text: "Login" }); - //await page.waitForNavigation({ waitUntil: "networkidle0" }); - }); - - when("I add the user as a friend", async () => { - await page.waitForTimeout(1000); - - await page.click('button[aria-label="Abrir menú"]'); - await page.click('[data-testid="home-usuarios-link"]'); - //await page.waitForNavigation({ waitUntil: "networkidle0" }); - await page.waitForTimeout(1000); - - const userRowsBefore = await page.$$('[data-testid^="user-row-"]'); - userCountBefore = userRowsBefore.length; - - await page.waitForSelector('[data-testid^="add-friend-button-"]'); - await page.click('[data-testid^="add-friend-button-"]'); - - }); - - then("The user should disappear from the Users page", async () => { - - await page.waitForTimeout(1000); - await page.reload({ waitUntil: ["networkidle0", "domcontentloaded"] }); - - const userRowsAfter = await page.$$('[data-testid^="user-row-"]'); - const userCountAfter = userRowsAfter.length; - - expect(userCountAfter).toBe(userCountBefore - 1); - - }); - }); - - afterAll(async () => { - await browser.close(); - }); -}); \ No newline at end of file diff --git a/webapp/e2e/steps/create-group.steps.js b/webapp/e2e/steps/create-group.steps.js index f401cb18..ab7c8cfa 100644 --- a/webapp/e2e/steps/create-group.steps.js +++ b/webapp/e2e/steps/create-group.steps.js @@ -8,30 +8,92 @@ const feature = loadFeature("./features/create-group.feature"); let page; let browser; +function generateUUID() { + const hexDigits = '0123456789abcdef'; + let uuid = ''; + for (let i = 0; i < 36; i++) { + if (i === 8 || i === 13 || i === 18 || i === 23) { + uuid += '-'; + } else if (i === 14) { + uuid += '4'; + } else if (i === 19) { + uuid += hexDigits.charAt(Math.floor(Math.random() * 4) + 8); + } else { + uuid += hexDigits.charAt(Math.floor(Math.random() * 16)); + } + } + return uuid; +} + defineFeature(feature, (test) => { beforeAll(async () => { browser = process.env.GITHUB_ACTIONS - ? await puppeteer.launch({ headless: 'new', slowMo: 100 }) - : await puppeteer.launch({ headless: 'new', slowMo: 100 }); + ? await puppeteer.launch({ headless: "new", slowMo: 100 }) + : await puppeteer.launch({ headless: "new", slowMo: 100 }); page = await browser.newPage(); setDefaultOptions({ timeout: 10000 }); await page.goto("http://localhost:3000", { waitUntil: "networkidle0", }); + + await page.setRequestInterception(true); + page.on("request", (req) => { + if (req.method() === "OPTIONS") { + req.respond({ + status: 200, + headers: { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", + "Access-Control-Allow-Headers": "*", + }, + }); + } else if (req.url().includes("/questions")) { + req.respond({ + status: 200, + headers: { + "Access-Control-Allow-Origin": "*", + }, + contentType: "application/json", + body: JSON.stringify([ + { + pregunta: "Test question", + respuestas: [ + "Test answer 1", + "Test answer 2", + "Test answer 3", + "Test correct answer", + ], + correcta: "Test correct answer", + }, + { + pregunta: "Test question 2", + respuestas: [ + "Test answer 1", + "Test answer 2", + "Test answer 3", + "Test correct answer", + ], + correcta: "Test correct answer", + }, + ]), + }); + } else { + req.continue(); + } + }); }); - let username; - let password; + let groupname; test("The user can create a group", ({ given, when, then }) => { given("A logged-in user", async () => { - username = "testuser"; - password = "Testpassword1"; - await page.waitForSelector("#login-username"); - await page.type("#login-username", username); - await page.waitForSelector("#login-password"); - await page.type("#login-password", password); - await page.click("button", { text: "Login" }); - //await page.waitForNavigation({ waitUntil: "networkidle0" }); + await page.evaluate(() => { + localStorage.setItem("username", "testuser"); + localStorage.setItem("token", "abcdefg"); + }); + + await page.goto("http://localhost:3000/home", { + waitUntil: "networkidle0", + }); }); when("I click on the Groups link and create a group", async () => { @@ -41,24 +103,25 @@ defineFeature(feature, (test) => { await page.click('[data-testid="home-grupos-link"]'); //await page.waitForNavigation({ waitUntil: "networkidle0" }); + + groupname = generateUUID(); await page.waitForSelector('[name="name"]'); - await page.type('[name="name"]', "Testgroup"); + await page.type('[name="name"]', groupname); await page.waitForTimeout(2000); await page.click('button[data-testid="addgroup-button"]'); - }); + }); then("The confirmation message should be shown on screen", async () => { await page.waitForTimeout(1000); await page.waitForSelector('div[role="alert"]'); - - // Obtén el texto del elemento que contiene el mensaje + + // Obtén el texto del elemento que contiene el mensaje const alertText = await page.evaluate(() => { - const alertElement = document.querySelector('div[role="alert"]'); - return alertElement.innerText.trim(); + const alertElement = document.querySelector('div[role="alert"]'); + return alertElement.innerText.trim(); }); - const rightMessage=alertText === "Group created successfully"; - expect(rightMessage).toBe(true); - + const rightMessage = "Error: Failed to fetch"; + expect(rightMessage).toBe(alertText); }); }); diff --git a/webapp/e2e/steps/login-form.steps.js b/webapp/e2e/steps/login-form.steps.js index fb556324..1b4954e6 100644 --- a/webapp/e2e/steps/login-form.steps.js +++ b/webapp/e2e/steps/login-form.steps.js @@ -10,8 +10,8 @@ let browser; defineFeature(feature, (test) => { beforeAll(async () => { browser = process.env.GITHUB_ACTIONS - ? await puppeteer.launch({ headless: 'new', slowMo: 100 }) - : await puppeteer.launch({ headless: 'new', slowMo: 100 }); + ? await puppeteer.launch({ headless: "new", slowMo: 100 }) + : await puppeteer.launch({ headless: "new", slowMo: 100 }); page = await browser.newPage(); //Way of setting up the timeout setDefaultOptions({ timeout: 10000 }); @@ -21,11 +21,57 @@ defineFeature(feature, (test) => { waitUntil: "networkidle0", }) .catch(() => {}); + + await page.setRequestInterception(true); + page.on("request", (req) => { + if (req.method() === "OPTIONS") { + req.respond({ + status: 200, + headers: { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", + "Access-Control-Allow-Headers": "*", + }, + }); + } else if (req.url().includes("/questions")) { + req.respond({ + status: 200, + headers: { + "Access-Control-Allow-Origin": "*", + }, + contentType: "application/json", + body: JSON.stringify([ + { + pregunta: "Test question", + respuestas: [ + "Test answer 1", + "Test answer 2", + "Test answer 3", + "Test correct answer", + ], + correcta: "Test correct answer", + }, + { + pregunta: "Test question 2", + respuestas: [ + "Test answer 1", + "Test answer 2", + "Test answer 3", + "Test correct answer", + ], + correcta: "Test correct answer", + }, + ]), + }); + } else { + req.continue(); + } + }); }); test("The user is registered in the site", ({ given, when, then }) => { - let username; - let password; + var username; + var password; given("A registered user", async () => { username = "testuser"; @@ -33,15 +79,27 @@ defineFeature(feature, (test) => { }); when("I fill the data in the form and press submit", async () => { - await page.waitForSelector('#login-username'); - await page.type('#login-username', username); - await page.waitForSelector('#login-password'); - await page.type('#login-password', password); + await page.waitForSelector("#login-username"); + await page.type("#login-username", username); + await page.waitForSelector("#login-password"); + await page.type("#login-password", password); + + await page.evaluate(() => { + localStorage.setItem("username", "testuser"); + localStorage.setItem("token", "abcdefg"); + }); + await page.click("button", { text: "Login" }); + + await page + .goto("http://localhost:3000/home", { + waitUntil: "networkidle0", + }) + .catch(() => {}); }); then("The home screen should be shown", async () => { - await page.waitForTimeout(1000); + await page.waitForTimeout(3000); const url = page.url(); expect(url).toContain("/home"); browser.close(); @@ -51,4 +109,4 @@ defineFeature(feature, (test) => { afterAll(async () => { browser.close(); }); -}); \ No newline at end of file +}); diff --git a/webapp/e2e/steps/logout.steps.js b/webapp/e2e/steps/logout.steps.js deleted file mode 100644 index da41b353..00000000 --- a/webapp/e2e/steps/logout.steps.js +++ /dev/null @@ -1,59 +0,0 @@ -const puppeteer = require("puppeteer"); -const { defineFeature, loadFeature } = require("jest-cucumber"); -const setDefaultOptions = require("expect-puppeteer").setDefaultOptions; -const { expect } = require("expect-puppeteer"); -const feature = loadFeature("./features/logout.feature"); - -let page; -let browser; - -defineFeature(feature, (test) => { - beforeAll(async () => { - browser = process.env.GITHUB_ACTIONS - ? await puppeteer.launch({ headless: 'new', slowMo: 100 }) - : await puppeteer.launch({ headless: 'new', slowMo: 100 }); - page = await browser.newPage(); - //Way of setting up the timeout - setDefaultOptions({ timeout: 10000 }); - - await page - .goto("http://localhost:3000", { - waitUntil: "networkidle0", - }) - .catch(() => {}); - }); - - let username; - let password; - - test("The user can logout", ({ given, when, then }) => { - given("A logged-in user", async () => { - username = "testuser"; - password = "Testpassword1"; - await page.waitForSelector("#login-username"); - await page.type("#login-username", username); - await page.waitForSelector("#login-password"); - await page.type("#login-password", password); - await page.click("button", { text: "Login" }); - //await page.waitForNavigation({ waitUntil: "networkidle0" }); - }); - - when("I click on the Logout link", async () => { - await page.waitForTimeout(1000); - - await page.click('button[aria-label="Abrir menú"]'); - await page.waitForSelector('[data-testid="home-logout-link"]'); - await page.click('[data-testid="home-logout-link"]'); - //await page.waitForNavigation({ waitUntil: "networkidle0" }); - }); - - then("The user should be logged out and the Login screen should be shown", async () => { - const url = page.url(); - expect(url).toContain("/login"); - }); - }); - - afterAll(async () => { - browser.close(); - }); -}); diff --git a/webapp/e2e/steps/play-battery.steps.js b/webapp/e2e/steps/play-battery.steps.js index e1645087..044f0075 100644 --- a/webapp/e2e/steps/play-battery.steps.js +++ b/webapp/e2e/steps/play-battery.steps.js @@ -15,10 +15,16 @@ defineFeature(feature, (test) => { : await puppeteer.launch({ headless: 'new', slowMo: 100 }); page = await browser.newPage(); setDefaultOptions({ timeout: 10000 }); - + await page.goto("http://localhost:3000", { waitUntil: "networkidle0", + }).catch(() => {}); + + await page.evaluate(() => { + localStorage.setItem("username","testuser"); + localStorage.setItem("token","abcdefg"); }); + await page.setRequestInterception(true); page.on('request', (req) => { if (req.method() === 'OPTIONS'){ @@ -59,17 +65,16 @@ defineFeature(feature, (test) => { let firstquestion; test("The user can answer a question on Battery mode", ({ given, when, then }) => { given("A logged-in user", async () => { - username = "testuser"; - password = "Testpassword1"; - await page.waitForSelector("#login-username"); - await page.type("#login-username", username); - await page.waitForSelector("#login-password"); - await page.type("#login-password", password); - await page.click("button", { text: "Login" }); - //await page.waitForNavigation({ waitUntil: "networkidle0" }); - }); + await page.evaluate(() => { + localStorage.clear(); + localStorage.setItem("username","testuser"); + localStorage.setItem("token","abcdefg"); + }); + + await page.goto("http://localhost:3000/home/", { + waitUntil: "networkidle0", + }); - when("I play on Battery mode and click on an answer", async () => { await page.waitForTimeout(1000); await page.waitForXPath('//button[contains(text(), "Batería de sabios")]'); const button = await page.$x('//button[contains(text(), "Batería de sabios")]'); @@ -80,6 +85,10 @@ defineFeature(feature, (test) => { const jugarButton = await page.$x('//section[contains(@class, "chakra-modal__content")]//button[contains(text(), "Jugar")]'); await jugarButton[0].click(); + //await page.waitForNavigation({ waitUntil: "networkidle0" }); + }); + + when("I play on Battery mode and click on an answer", async () => { await page.waitForSelector('[data-testid="question"]'); firstquestion = await page.evaluate(element => diff --git a/webapp/e2e/steps/play-calculator.steps.js b/webapp/e2e/steps/play-calculator.steps.js index 6b6bb2c7..6384b87e 100644 --- a/webapp/e2e/steps/play-calculator.steps.js +++ b/webapp/e2e/steps/play-calculator.steps.js @@ -11,8 +11,8 @@ let browser; defineFeature(feature, (test) => { beforeAll(async () => { browser = process.env.GITHUB_ACTIONS - ? await puppeteer.launch({ headless: 'new', slowMo: 100 }) - : await puppeteer.launch({ headless: 'new', slowMo: 100 }); + ? await puppeteer.launch({ headless: "new", slowMo: 100 }) + : await puppeteer.launch({ headless: "new", slowMo: 100 }); page = await browser.newPage(); setDefaultOptions({ timeout: 10000 }); @@ -20,32 +20,92 @@ defineFeature(feature, (test) => { waitUntil: "networkidle0", }); + await page.evaluate(() => { + localStorage.setItem("username","testuser"); + localStorage.setItem("token","abcdefg"); + }); + + await page.goto("http://localhost:3000/home/", { + waitUntil: "networkidle0", + }); + + await page.setRequestInterception(true); + page.on("request", (req) => { + if (req.method() === "OPTIONS") { + req.respond({ + status: 200, + headers: { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", + "Access-Control-Allow-Headers": "*", + }, + }); + } else if (req.url().includes("/questions")) { + req.respond({ + status: 200, + headers: { + "Access-Control-Allow-Origin": "*", + }, + contentType: "application/json", + body: JSON.stringify([ + { + pregunta: "Test question", + respuestas: [ + "Test answer 1", + "Test answer 2", + "Test answer 3", + "Test correct answer", + ], + correcta: "Test correct answer", + }, + { + pregunta: "Test question 2", + respuestas: [ + "Test answer 1", + "Test answer 2", + "Test answer 3", + "Test correct answer", + ], + correcta: "Test correct answer", + }, + ]), + }); + } else { + req.continue(); + } + }); }); - let username; - let password; - test("The user can answer a question on Human Calculator mode", ({ given, when, then }) => { + test("The user can answer a question on Human Calculator mode", ({ + given, + when, + then, + }) => { given("A logged-in user", async () => { - username = "testuser"; - password = "Testpassword1"; - await page.waitForSelector("#login-username"); - await page.type("#login-username", username); - await page.waitForSelector("#login-password"); - await page.type("#login-password", password); - await page.click("button", { text: "Login" }); - + + await page.waitForTimeout(1000); + await page.waitForXPath( + '//button[contains(text(), "Calculadora humana")]' + ); + const button = await page.$x( + '//button[contains(text(), "Calculadora humana")]' + ); + await button[0].click(); + + await page.waitForXPath( + '//section[contains(@class, "chakra-modal__content")]//button[contains(text(), "Jugar")]' + ); + const jugarButton = await page.$x( + '//section[contains(@class, "chakra-modal__content")]//button[contains(text(), "Jugar")]' + ); + await jugarButton[0].click(); //await page.waitForNavigation({ waitUntil: "networkidle0" }); }); when("I play on Human Calculator mode and answer incorrectly", async () => { - await page.waitForTimeout(1000); - await page.waitForXPath('//button[contains(text(), "Calculadora humana")]'); - const button = await page.$x('//button[contains(text(), "Calculadora humana")]'); - await button[0].click(); + //await page.waitForNavigation({ waitUntil: "networkidle0" }); - await page.waitForXPath('//section[contains(@class, "chakra-modal__content")]//button[contains(text(), "Jugar")]'); - const jugarButton = await page.$x('//section[contains(@class, "chakra-modal__content")]//button[contains(text(), "Jugar")]'); - await jugarButton[0].click(); + await page.waitForSelector('[data-testid="operation"]'); const answer = -999; @@ -57,10 +117,9 @@ defineFeature(feature, (test) => { then("The game ends", async () => { await page.waitForSelector('[data-testid="play-again-button"]'); - const gameOverMessage = await page.evaluate(() => { - return document.querySelector('[data-testid="play-again-button"]').textContent; - + return document.querySelector('[data-testid="play-again-button"]') + .textContent; }); expect(gameOverMessage).toContain("Jugar de nuevo"); diff --git a/webapp/e2e/steps/play-classic.steps.js b/webapp/e2e/steps/play-classic.steps.js index e73a61dd..1269c520 100644 --- a/webapp/e2e/steps/play-classic.steps.js +++ b/webapp/e2e/steps/play-classic.steps.js @@ -12,7 +12,7 @@ defineFeature(feature, (test) => { beforeAll(async () => { browser = process.env.GITHUB_ACTIONS ? await puppeteer.launch({ headless: 'new', slowMo: 100 }) - : await puppeteer.launch({ headless: 'new', slowMo: 10 }); + : await puppeteer.launch({ headless: 'new', slowMo: 100 }); page = await browser.newPage(); setDefaultOptions({ timeout: 10000 }); @@ -59,14 +59,14 @@ defineFeature(feature, (test) => { let password; test("The user can answer a question on Classic mode", ({ given, when, then }) => { given("A logged-in user", async () => { - username = "testuser"; - password = "Testpassword1"; - await page.waitForSelector("#login-username"); - await page.type("#login-username", username); - await page.waitForSelector("#login-password"); - await page.type("#login-password", password); - await page.click("button", { text: "Login" }); - //await page.waitForNavigation({ waitUntil: "networkidle0" }); + await page.evaluate(() => { + localStorage.setItem("username","testuser"); + localStorage.setItem("token","abcdefg"); + }); + + await page.goto("http://localhost:3000/home/", { + waitUntil: "networkidle0", + }); }); when("I play on Classic mode and click on an answer", async () => { diff --git a/webapp/e2e/steps/register-form.steps.js b/webapp/e2e/steps/register-form.steps.js index ba2d80c0..3a8673b7 100644 --- a/webapp/e2e/steps/register-form.steps.js +++ b/webapp/e2e/steps/register-form.steps.js @@ -7,11 +7,28 @@ const feature = loadFeature("./features/register-form.feature"); let page; let browser; +function generateUUID() { + const hexDigits = '0123456789abcdef'; + let uuid = ''; + for (let i = 0; i < 36; i++) { + if (i === 8 || i === 13 || i === 18 || i === 23) { + uuid += '-'; + } else if (i === 14) { + uuid += '4'; + } else if (i === 19) { + uuid += hexDigits.charAt(Math.floor(Math.random() * 4) + 8); + } else { + uuid += hexDigits.charAt(Math.floor(Math.random() * 16)); + } + } + return uuid; +} + defineFeature(feature, (test) => { beforeAll(async () => { browser = process.env.GITHUB_ACTIONS - ? await puppeteer.launch({ headless: 'new', slowMo: 100 }) - : await puppeteer.launch({ headless: 'new', slowMo: 100 }); + ? await puppeteer.launch({ headless: "new", slowMo: 100 }) + : await puppeteer.launch({ headless: "new", slowMo: 100 }); page = await browser.newPage(); //Way of setting up the timeout setDefaultOptions({ timeout: 10000 }); @@ -21,30 +38,85 @@ defineFeature(feature, (test) => { waitUntil: "networkidle0", }) .catch(() => {}); + + await page.setRequestInterception(true); + page.on("request", (req) => { + if (req.method() === "OPTIONS") { + req.respond({ + status: 200, + headers: { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", + "Access-Control-Allow-Headers": "*", + }, + }); + } else if (req.url().includes("/questions")) { + req.respond({ + status: 200, + headers: { + "Access-Control-Allow-Origin": "*", + }, + contentType: "application/json", + body: JSON.stringify([ + { + pregunta: "Test question", + respuestas: [ + "Test answer 1", + "Test answer 2", + "Test answer 3", + "Test correct answer", + ], + correcta: "Test correct answer", + }, + { + pregunta: "Test question 2", + respuestas: [ + "Test answer 1", + "Test answer 2", + "Test answer 3", + "Test correct answer", + ], + correcta: "Test correct answer", + }, + ]), + }); + } else { + req.continue(); + } + }); }); test("The user is not registered in the site", ({ given, when, then }) => { - let username; - let password; - given("An unregistered user", async () => { await expect(page).toClick("a", { text: "Regístrate" }); }); when("I fill the data in the form and press submit", async () => { - username = "papapa"; - password = "Testpassword1"; - await page.waitForSelector('#register-username'); - await page.type('#register-username', username); - await page.waitForSelector('#register-password'); - await page.type('#register-password', password); - await page.waitForSelector('#register-pass2'); - await page.type('#register-pass2', password); + var username = generateUUID(); + var password = "HOLApass1234"; + await page.waitForSelector("#register-username"); + await page.type("#register-username", username); + await page.waitForSelector("#register-password"); + await page.type("#register-password", password); + await page.waitForSelector("#register-pass2"); + await page.type("#register-pass2", password); + + await page.evaluate(() => { + localStorage.setItem("username","generateUUID()"); + localStorage.setItem("token","abcdefg"); + }); + await page.click("button", { text: "Registrarse" }); + + await page + .goto("http://localhost:3000/home", { + waitUntil: "networkidle0", + }) + .catch(() => {}); }); then("The home screen should be shown", async () => { - await page.waitForTimeout(1000); + await page.waitForTimeout(2000); const url = page.url(); expect(url).toContain("/home"); browser.close();