Skip to content

Commit

Permalink
E2e testing
Browse files Browse the repository at this point in the history
E2e test
  • Loading branch information
andrrsin committed Apr 27, 2024
2 parents 0c44e16 + e85433a commit 2357ae4
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 88 deletions.
1 change: 1 addition & 0 deletions users/authservice/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);



Expand Down Expand Up @@ -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']);

Expand Down
18 changes: 14 additions & 4 deletions webapp/e2e/features/login-form.feature
Original file line number Diff line number Diff line change
@@ -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
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
17 changes: 17 additions & 0 deletions webapp/e2e/features/userprofile-form.feature
Original file line number Diff line number Diff line change
@@ -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

132 changes: 76 additions & 56 deletions webapp/e2e/steps/login-form.steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

});
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()
})

});
42 changes: 14 additions & 28 deletions webapp/e2e/steps/register-form.steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,54 @@ 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;

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()
Expand Down
Loading

0 comments on commit 2357ae4

Please sign in to comment.