Skip to content

Commit

Permalink
test: auth utils
Browse files Browse the repository at this point in the history
Co-authored-by: Dario G.Mori <[email protected]>
  • Loading branch information
GOLASOOO committed May 1, 2024
1 parent 7b015a0 commit ac2747e
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 7 deletions.
29 changes: 29 additions & 0 deletions webapp/e2e/e2e_utils/e2e_utils_login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Note: e2e Testing purposes only!
* Auxiliar function for login an user using its credentials from the root directory of the website.
* It also ensures the task has been performed successfully.
*
* @param username The username for the user. Currently we are using codes for each test case.
* @param email The email for the user. If none is defined, the username (a code) + '@gmail.com' is used
* @param password The password for the user. If none is defined, the username (a code) + '.ps' is used
* Beware of constraits for the user password.
* @param page The website
*/
async function loginUserFromRootDirectory(username, email = username + "@gmail.com", password = username + ".ps", page) {

// login process
await expect(page).toClick("button[data-testid='Login'");
await expect(page).toFill("#user", email);
await expect(page).toFill("#password", password);
await expect(page).toClick("button[data-testid='Login'");

// Checking for the process to be correct
await new Promise(resolve => setTimeout(resolve, 6000)); // Waiting for page to fully load
let header = await page.$eval("h2", (element) => {
return element.innerHTML
})
let value = header === "Bienvenid@ " + username || header === "Welcome " + username;
expect(value).toBeTruthy();

}
modules.export ={loginUserFromRootDirectory};
26 changes: 26 additions & 0 deletions webapp/e2e/e2e_utils/e2e_utils_logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const {waitForPageToLoad} = require('../e2e_utils/e2e_utils_timeout.js');

/**
* Note: e2e Testing purposes only!
* Auxiliar function for logging out an user from any directory of the user.
* Beware if the user is playing a game when logging out
* It also ensures the task has been performed successfully.
*
* @param {*} page The website
*/
async function logOutUser(page) {
// Logging out
await expect(page).toClick("#lateralMenuButton");
await expect(page).toClick("button[data-testid='LogOut']");

// Checking for the log out to be sucessful
waitForPageToLoad();
let header = await page.$eval("button[data-testid='Login']", (element) => {
return element.innerHTML
})
let value = header === "Login" || "Iniciar sesión";

expect(value).toBeTruthy();
}
modules.export = {logOutUser}

34 changes: 34 additions & 0 deletions webapp/e2e/e2e_utils/e2e_utils_register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Note: e2e Testing purposes only!
* Auxiliar function for registering a new user from the root directory of the website.
* It also ensures the task has been performed successfully.
*
* @param {*} username The username for the new user. Currently we are using codes for each test case.
* @param {*} page The website
* @returns An array with the credentials of the user created [email, username]
*/
async function registerUserFromRootDirectory(username, page) {
// Credentials for the new user
let email = username + "@email.com"
let password = username + "ps"

// Registeing process
await expect(page).toClick("span[class='chakra-link css-1bicqx'");
await expect(page).toFill("input[id='user'", email);
await expect(page).toFill("input[id='username'", username);
await expect(page).toFill("#password", password);
await expect(page).toFill("input[id='field-:r5:']", password);
await expect(page).toClick("button[data-testid='Sign up'");

// Checking for the process to be correct
await new Promise(resolve => setTimeout(resolve, 6000)); // Waiting for page to fully load
let header = await page.$eval("h2", (element) => {
return element.innerHTML
})
let value = header === "Bienvenid@ " + username || header === "Welcome " + username;
expect(value).toBeTruthy();

return [email, password];
}

modules.export ={registerUserFromRootDirectory};
11 changes: 11 additions & 0 deletions webapp/e2e/e2e_utils/e2e_utils_timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Note: e2e Testing purposes only!
* Auxiliar function that times out the tests for some time, so the page can be fully loaded.
* @param {*} timeout_ms Amount of ms to wait.
*/
async function waitForPageToLoad(timeout_ms = 6000) {
await new Promise(resolve => setTimeout(resolve, timeout_ms));

}

modules.export = {waitForPageToLoad}
27 changes: 20 additions & 7 deletions webapp/e2e/steps/login_positive.steps.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { registerUserFromRootDirectory}from '../e2e_utils/e2e_utils_register.js';
import { logOutUser } from '../e2e_utils/e2e_utils_logout.js';
import { waitForPageToLoad } from '../e2e_utils/e2e_utils_timeout.js';

const { defineFeature, loadFeature }=require('jest-cucumber');
const puppeteer = require('puppeteer');
const setDefaultOptions = require("expect-puppeteer").setDefaultOptions;
Expand All @@ -7,6 +11,9 @@ let browser;


defineFeature(feature, test => {
let username = "t.regis.pos";
let email;
let password;

beforeAll(async () => {
browser = process.env.GITHUB_ACTIONS
Expand All @@ -21,15 +28,21 @@ defineFeature(feature, test => {
waitUntil: "networkidle0",
})
.catch(() => {});


// Registering the user before the tests
let credentials = registerUserFromRootDirectory(username, page);
email = credentials[0];
username = credentials[1];

// Logging it out
logOutUser(page);
});

test("A registered user wants to log in using his correct credentials", ({given,when,and,then}) => {
let username = "pepe"
let user = username + "@pepe.com"
let password = "pepe"

given('A registered user in the root screen', async () => {
await new Promise(resolve => setTimeout(resolve, 6000)); // Waiting for page to fully load
waitForPageToLoad();
let header = await page.$eval("button[data-testid='Login']", (element) => {
return element.innerHTML
})
Expand All @@ -43,7 +56,7 @@ defineFeature(feature, test => {
});

and('User enters in the log in screen', async() => {
await new Promise(resolve => setTimeout(resolve, 6000)); // Waiting for page to fully load
waitForPageToLoad();
let header = await page.$eval("h2", (element) => {
return element.innerHTML
})
Expand All @@ -53,7 +66,7 @@ defineFeature(feature, test => {
});

and('User fills the form with his credentials properly', async() => {
await expect(page).toFill("#user", user);
await expect(page).toFill("#user", email);
await expect(page).toFill("#password", password);
});

Expand All @@ -62,7 +75,7 @@ defineFeature(feature, test => {
});

then('The main menu screen shows on the user device', async() => {
await new Promise(resolve => setTimeout(resolve, 6000)); // Waiting for page to fully load
waitForPageToLoad();
let header = await page.$eval("h2", (element) => {
return element.innerHTML
})
Expand Down

0 comments on commit ac2747e

Please sign in to comment.