From 40616ead94dbc19964a0ebff27dad9341cfa51da Mon Sep 17 00:00:00 2001 From: uo287841 Date: Sat, 27 Apr 2024 18:46:03 +0200 Subject: [PATCH 1/2] e2e login and add user --- users/authservice/auth-service.js | 1 + users/userservice/user-service.js | 2 + webapp/e2e/features/login-form.feature | 8 +- webapp/e2e/steps/login-form.steps.js | 97 +++++++++++-------------- webapp/e2e/steps/register-form.steps.js | 42 ++++------- webapp/e2e/test-environment-setup.js | 21 ++++++ 6 files changed, 83 insertions(+), 88 deletions(-) diff --git a/users/authservice/auth-service.js b/users/authservice/auth-service.js index 73b9a4d..b4549e1 100644 --- a/users/authservice/auth-service.js +++ b/users/authservice/auth-service.js @@ -12,6 +12,7 @@ app.use(express.json()); // Connect to MongoDB const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/wiq-en1a-users'; + mongoose.connect(mongoUri); // Function to validate required fields in the request body diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index a8989c6..b86eb56 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -15,6 +15,7 @@ app.use(bodyParser.json()); // Connect to MongoDB const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/wiq-en1a-users'; mongoose.connect(mongoUri); +console.log(mongoUri); @@ -127,6 +128,7 @@ app.get('/ranking/user', async (req, res) => { app.post('/adduser', async (req, res) => { try { + console.log(mongoUri); // Check if required fields are present in the request body validateRequiredFields(req, ['username','email', 'password']); diff --git a/webapp/e2e/features/login-form.feature b/webapp/e2e/features/login-form.feature index 0060f6f..a086ff7 100644 --- a/webapp/e2e/features/login-form.feature +++ b/webapp/e2e/features/login-form.feature @@ -1,6 +1,6 @@ -Feature: Registering a new user +Feature: Login a registered user Scenario: The user is registered in the site - Given A registered user - When I fill the data in the form and press submit - Then is logged \ No newline at end of file + Given An registered user + When I fill the data in the form to log in + Then is taken to the home page \ No newline at end of file diff --git a/webapp/e2e/steps/login-form.steps.js b/webapp/e2e/steps/login-form.steps.js index 0f30b9f..e949b11 100644 --- a/webapp/e2e/steps/login-form.steps.js +++ b/webapp/e2e/steps/login-form.steps.js @@ -3,69 +3,54 @@ const { defineFeature, loadFeature }=require('jest-cucumber'); const setDefaultOptions = require('expect-puppeteer').setDefaultOptions const feature = loadFeature('./features/login-form.feature'); -const axios = require('axios'); -const MockAdapter = require('axios-mock-adapter'); -const mockAxios = new MockAdapter(axios); let page; let browser; defineFeature(feature, test => { - beforeAll(async () => { - browser = process.env.GITHUB_ACTIONS - ? await puppeteer.launch() - : await puppeteer.launch({ headless: false, slowMo:40 }); - page = await browser.newPage(); - //Way of setting up the timeout - setDefaultOptions({ timeout: 10000 }) - - await page - .goto("http://localhost:3000", { - waitUntil: "networkidle0", - }) - .catch(() => {}); - - - }); - - beforeEach(async () => { - // Reset any state or actions before each test - await page.reload({ waitUntil: 'networkidle0' }); + beforeAll(async () => { + + browser = process.env.GITHUB_ACTIONS + ? await puppeteer.launch() + : await puppeteer.launch({ headless: false, slowMo: 30 }); + page = await browser.newPage(); + //Way of setting up the timeout + setDefaultOptions({ timeout: 10000 }) + + await page + .goto("http://localhost:3000/login", { + waitUntil: "networkidle0", + }) + .catch(() => {}); + }); + + test('The user is registered in the site', ({given,when,then}) => { + + let username; + let password; + + given('An registered user', async () => { + username = "testUser" + password = "testpass"; + }); - - test('The user is registered in the site', ({given,when,then}) => { - - let username; - let password; - let email - - given('A registered user', async () => { - username = "t1" - password = "t1pass" - - await expect(page).toClick("button", { text: "Create account" }); - }); - - when('I fill the data in the form and press submit', async () => { - await expect(page).toClick('a', { text: 'Already have an account? Log in here.' }); + when('I fill the data in the form to log in', async () => { await expect(page).toFill('input[name="username"]', username); await expect(page).toFill('input[name="password"]', password); - - mockAxios.onPost('http://localhost:8000/login').reply(200, { username:"t1",email:"t1email",createdAt: '2024-01-01T12:34:56Z',token: 'testToken'}); - - - await expect(page).toClick('button', { text: 'Login' }) - }); - - then('is logged', async () => { - await expect(page).toMatchElement("div", { text: "Welcome back, " + username + "!" }); - }); - }) - - afterAll(async ()=>{ - browser.close() - }) - - }); \ No newline at end of file + await expect(page).toClick("button", { text: "Log In" }); + }); + + then('is taken to the home page', async () => { + await page.waitForNavigation({ waitUntil: "networkidle0" }); + await expect(page).toMatchElement("h1", { text: "Welcome back, " + username + "!" }); + }); + + }); + + afterAll(async ()=>{ + browser.close() + }) + +}); \ No newline at end of file diff --git a/webapp/e2e/steps/register-form.steps.js b/webapp/e2e/steps/register-form.steps.js index ad9e64f..7e87115 100644 --- a/webapp/e2e/steps/register-form.steps.js +++ b/webapp/e2e/steps/register-form.steps.js @@ -3,9 +3,6 @@ const { defineFeature, loadFeature }=require('jest-cucumber'); const setDefaultOptions = require('expect-puppeteer').setDefaultOptions const feature = loadFeature('./features/register-form.feature'); -const axios = require('axios'); -const MockAdapter = require('axios-mock-adapter'); -const mockAxios = new MockAdapter(axios); let page; let browser; @@ -13,58 +10,47 @@ let browser; defineFeature(feature, test => { beforeAll(async () => { + browser = process.env.GITHUB_ACTIONS ? await puppeteer.launch() - : await puppeteer.launch({ headless: false, slowMo:60 }); + : await puppeteer.launch({ headless: false, slowMo: 30 }); page = await browser.newPage(); //Way of setting up the timeout setDefaultOptions({ timeout: 10000 }) await page - .goto("http://localhost:3000", { + .goto("http://localhost:3000/login", { waitUntil: "networkidle0", }) .catch(() => {}); - - - }); - - beforeEach(async () => { - // Reset any state or actions before each test - await page.reload({ waitUntil: 'networkidle0' }); }); test('The user is not registered in the site', ({given,when,then}) => { let username; let password; - let email given('An unregistered user', async () => { - username = "t1" - email = "t1email" - password = "t1pass" - - await expect(page).toClick("button", { text: "Create account" }); + username = "newUser" + password = "newUser" + await expect(page).toClick("a", { text: "Create account" }); }); when('I fill the data in the form and press submit', async () => { + await expect(page).toFill('input[name="username"]', username); - await expect(page).toFill('input[name="email"]', email); + await expect(page).toFill('input[name="email"]', username + "@" + "gmail.com"); await expect(page).toFill('input[name="password"]', password); - await expect(page).toFill('input[name="cpassword"]', password); - - // mockAxios.onPost('http://localhost:8000/adduser').reply(200, { username: "t1", email: "t1email", password: "t1pass" }); - mockAxios.onPost('http://localhost:8000/adduser').reply(200, { username:"t1",email:"t1email",password: 't1pass'}); - - - await expect(page).toClick('button', { text: 'Register' }) + await expect(page).toFill('input[name="cpassword"]', password); + await expect(page).toClick("button", { text: "Register" }); }); then('is taken to login', async () => { - //await expect(page).toMatchElement("div", { text: "Login" }); + await page.waitForNavigation({ waitUntil: "networkidle0" }); + await expect(page).toMatchElement("h1", { text: "Access WIQ" }); }); - }) + + }); afterAll(async ()=>{ browser.close() diff --git a/webapp/e2e/test-environment-setup.js b/webapp/e2e/test-environment-setup.js index 7b7ed51..9c14242 100644 --- a/webapp/e2e/test-environment-setup.js +++ b/webapp/e2e/test-environment-setup.js @@ -1,4 +1,6 @@ const { MongoMemoryServer } = require('mongodb-memory-server'); +const User = require('../../users/userservice/user-model'); +const axios = require('axios'); let mongoserver; @@ -9,11 +11,30 @@ let gatewayservice; async function startServer() { console.log('Starting MongoDB memory server...'); mongoserver = await MongoMemoryServer.create(); + const mongoUri = mongoserver.getUri(); process.env.MONGODB_URI = mongoUri; userservice = await require("../../users/userservice/user-service"); authservice = await require("../../users/authservice/auth-service"); gatewayservice = await require("../../gatewayservice/gateway-service"); + + // Add test user + await addUser('testUser', 'test@email', 'testpass'); + } + async function addUser(username, email, password) { + try { + const response = await axios.post('http://localhost:8001/adduser', { + username: username, + email: email, + password: password + }); + } catch (error) { + console.error('Error adding user:', error.response.data); + } +} + + + startServer(); From e85433a730625a83a16fb0423d323bc84c83acb9 Mon Sep 17 00:00:00 2001 From: uo287841 Date: Sat, 27 Apr 2024 21:50:22 +0200 Subject: [PATCH 2/2] user progule e2e --- webapp/e2e/features/login-form.feature | 12 ++- webapp/e2e/features/userprofile-form.feature | 17 +++ webapp/e2e/steps/login-form.steps.js | 35 ++++++ webapp/e2e/steps/userprofile-form.steps.js | 107 +++++++++++++++++++ webapp/e2e/test-environment-setup.js | 7 ++ 5 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 webapp/e2e/features/userprofile-form.feature create mode 100644 webapp/e2e/steps/userprofile-form.steps.js diff --git a/webapp/e2e/features/login-form.feature b/webapp/e2e/features/login-form.feature index a086ff7..d8bdf51 100644 --- a/webapp/e2e/features/login-form.feature +++ b/webapp/e2e/features/login-form.feature @@ -3,4 +3,14 @@ Feature: Login a registered user Scenario: The user is registered in the site Given An registered user When I fill the data in the form to log in - Then is taken to the home page \ No newline at end of file + Then is taken to the home page + +Scenario: User logs in with invalid credentials + Given a registered user with username "testUser" and password "testpass" + When I fill the login form with username "testUser" and incorrect password "wrongpass" + And I remain on the login page + +Scenario: User attempts to login without entering credentials + Given a registered user with username "testUser" and password "testpass" + When I attempt to log in without entering any credentials + And I remain on the login page \ No newline at end of file diff --git a/webapp/e2e/features/userprofile-form.feature b/webapp/e2e/features/userprofile-form.feature new file mode 100644 index 0000000..467d758 --- /dev/null +++ b/webapp/e2e/features/userprofile-form.feature @@ -0,0 +1,17 @@ +Feature: View and Change User Quiz Rankings + +Scenario: Viewing Global Rankings + Given the user navigates to their profile + When they select the "Global" category + Then they see their performance statistics for global quizzes + +Scenario: Switching Category to Flags + Given the user is on their profile page + When they click on the "Flags" category + Then they view their performance metrics for flag-related quizzes + +Scenario: Switching Category to Food + Given the user is on their profile page + When they click on the "Food" category + Then they view their performance metrics for food-related quizzes + diff --git a/webapp/e2e/steps/login-form.steps.js b/webapp/e2e/steps/login-form.steps.js index e949b11..239389b 100644 --- a/webapp/e2e/steps/login-form.steps.js +++ b/webapp/e2e/steps/login-form.steps.js @@ -45,10 +45,45 @@ defineFeature(feature, test => { then('is taken to the home page', async () => { await page.waitForNavigation({ waitUntil: "networkidle0" }); await expect(page).toMatchElement("h1", { text: "Welcome back, " + username + "!" }); + await expect(page).toClick("button", { text: "Log out" }); }); }); + test('User logs in with invalid credentials', ({ given, when, then }) => { + given('a registered user with username "testUser" and password "testpass"', async () => { + // No specific action needed since the user is already registered + }); + + when('I fill the login form with username "testUser" and incorrect password "wrongpass"', async () => { + await expect(page).toFill('input[name="username"]', 'testUser'); + await expect(page).toFill('input[name="password"]', 'wrongpass'); + await expect(page).toClick("button", { text: "Log In" }); + }); + + + then('I remain on the login page', async () => { + await expect(page).toMatchElement("h1", { text: "Access WIQ" }); + }); + }); + + test('User attempts to login without entering credentials', ({ given, when, then }) => { + given('a registered user with username "testUser" and password "testpass"', async () => { + // No specific action needed since the user is already registered + }); + + when('I attempt to log in without entering any credentials', async () => { + await expect(page).toFill('input[name="username"]', ''); + await expect(page).toFill('input[name="password"]', ''); + await expect(page).toClick("button", { text: "Log In" }); + }); + + + then('I remain on the login page', async () => { + await expect(page).toMatchElement("h1", { text: "Access WIQ" }); + }); + }); + afterAll(async ()=>{ browser.close() }) diff --git a/webapp/e2e/steps/userprofile-form.steps.js b/webapp/e2e/steps/userprofile-form.steps.js new file mode 100644 index 0000000..6232c9c --- /dev/null +++ b/webapp/e2e/steps/userprofile-form.steps.js @@ -0,0 +1,107 @@ +const puppeteer = require('puppeteer'); +const { defineFeature, loadFeature }=require('jest-cucumber'); +const setDefaultOptions = require('expect-puppeteer').setDefaultOptions +const feature = loadFeature('./features/userprofile-form.feature'); + + +let page; +let browser; + +defineFeature(feature, test => { + + beforeAll(async () => { + + browser = process.env.GITHUB_ACTIONS + ? await puppeteer.launch() + : await puppeteer.launch({ headless: false, slowMo: 20 }); + page = await browser.newPage(); + //Way of setting up the timeout + setDefaultOptions({ timeout: 10000 }) + + await page + .goto("http://localhost:3000/login", { + waitUntil: "networkidle0", + }) + .catch(() => {}); + }); + + test('Viewing Global Rankings', ({given,when,then}) => { + + let username; + let password; + + given('the user navigates to their profile', async () => { + username = "testUser" + password = "testpass"; + await expect(page).toClick("a", { text: "Log In" }); + await expect(page).toFill('input[name="username"]', username); + await expect(page).toFill('input[name="password"]', password); + await expect(page).toClick("button", { text: "Log In" }); + await expect(page).toClick("button", { text: "My stats" }); + }); + + when('they select the "Global" category', async () => { + + await expect(page).toMatchElement("h2", { text: "Username: " + username }); + await expect(page).toClick("button", { text: "Flags" }); + await expect(page).toClick("button", { text: "Global" }); + }); + + then('they see their performance statistics for global quizzes', async () => { + await expect(page).toMatchElement('.ranking h3', { text: "global Ranking" }); + await expect(page).toMatchElement(".ranking p:nth-child(1)", { text: "Total Answered Questions: " + 1 }); + }); + + }); + + test('Switching Category to Flags', ({given,when,then}) => { + + let username; + let password; + + given('the user is on their profile page', async () => { + username = "testUser" + password = "testpass"; + await expect(page).toMatchElement("h2", { text: "Username: " + username }); + }); + + when('they click on the "Flags" category', async () => { + + await expect(page).toClick("button", { text: "Flags" }); + }); + + then('they view their performance metrics for flag-related quizzes', async () => { + await expect(page).toMatchElement('.ranking h3', { text: "flags Ranking" }); + await expect(page).toMatchElement(".ranking p:nth-child(1)", { text: "Total Answered Questions: " + 1 }); + }); + + }); + + test('Switching Category to Food', ({given,when,then}) => { + + let username; + let password; + + given('the user is on their profile page', async () => { + username = "testUser" + password = "testpass"; + await expect(page).toMatchElement("h2", { text: "Username: " + username }); + }); + + when('they click on the "Food" category', async () => { + + await expect(page).toClick("button", { text: "Food" }); + }); + + then('they view their performance metrics for food-related quizzes', async () => { + await expect(page).toMatchElement('.ranking h3', { text: "foods Ranking" }); + await expect(page).toMatchElement(".ranking p:nth-child(1)", { text: "Total Answered Questions: " + 0 }); + }); + + }); + + afterAll(async ()=>{ + browser.close() + }) + +}); \ No newline at end of file diff --git a/webapp/e2e/test-environment-setup.js b/webapp/e2e/test-environment-setup.js index 9c14242..8a43a0f 100644 --- a/webapp/e2e/test-environment-setup.js +++ b/webapp/e2e/test-environment-setup.js @@ -30,6 +30,13 @@ async function startServer() { email: email, password: password }); + + const pints = await axios.post('http://localhost:8001/addpoints', { + username: username, + category: "flags", + correct: "true" + }); + } catch (error) { console.error('Error adding user:', error.response.data); }