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..d8bdf51 100644 --- a/webapp/e2e/features/login-form.feature +++ b/webapp/e2e/features/login-form.feature @@ -1,6 +1,16 @@ -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 + +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 0f30b9f..239389b 100644 --- a/webapp/e2e/steps/login-form.steps.js +++ b/webapp/e2e/steps/login-form.steps.js @@ -3,69 +3,89 @@ 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 + "!" }); + 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() + }) + +}); \ 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/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 7b7ed51..8a43a0f 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,37 @@ 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 + }); + + 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); + } +} + + + startServer();