added all testing files

This commit is contained in:
Vishal Vunnam
2024-04-10 21:53:51 -06:00
parent 7cae7b841a
commit ffc272852c
6 changed files with 2442 additions and 68 deletions

Binary file not shown.

View File

@@ -33,6 +33,6 @@ services:
volumes: volumes:
- ../ProjectSourceCode:/home/node/app # Mount ProjectSourceCode directory - ../ProjectSourceCode:/home/node/app # Mount ProjectSourceCode directory
- ../ProjectSourceCode/node_modules:/home/node/app/node_modules # Mount node_modules directory - ../ProjectSourceCode/node_modules:/home/node/app/node_modules # Mount node_modules directory
command: 'npm start' command: 'npm run testandrun'
volumes: volumes:
group-project: group-project:

File diff suppressed because it is too large Load Diff

View File

@@ -56,6 +56,9 @@ app.set('views', path.join(__dirname, 'views'));
app.use(bodyParser.json()); // specify the usage of JSON for parsing request body. app.use(bodyParser.json()); // specify the usage of JSON for parsing request body.
// initialize session variables // initialize session variables
app.get('/welcome', (req, res) => {
res.json({status: 'success', message: 'Welcome!'});
});
app.use( app.use(
session({ session({
secret: process.env.SESSION_SECRET, secret: process.env.SESSION_SECRET,
@@ -137,20 +140,27 @@ app.get('/', (req, res) => {
// Trigger Registration Form to Post // Trigger Registration Form to Post
app.post('/register', async (req, res) => { app.post('/register', async (req, res) => {
try { try {
if (!req.body.username || !req.body.password) {
// If username or password is missing, respond with status 400 and an error message
return res.status(400).json({status: 'error', message: 'Invalid input'});
}
// Hash the password using bcrypt library // Hash the password using bcrypt library
const hash = await bcrypt.hash(req.body.password, 10); const hash = await bcrypt.hash(req.body.password, 10);
// Insert username and hashed password into the 'users' table // Insert username and hashed password into the 'users' table
await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]); await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]);
//res.status(200);
res.json({status: 'success', message: 'Success'});
// Direct user to login screen after data has been inserted successfully // Direct user to login screen after data has been inserted successfully
res.redirect('/login'); res.redirect('/login');
} catch (error) { } catch (error) {
// If the insert fails, redirect to GET /register route // If the insert fails, redirect to GET /register route
res.status(400);
//res.json({status: 'success', message: 'Invalid input'});
res.redirect('/register'); res.redirect('/register');
} }
}); });
/************************ /************************
Discover Page Routes Discover Page Routes
*************************/ *************************/
@@ -165,5 +175,5 @@ app.get('/', (req, res) => {
// <!-- Section 5 : Start Server--> // <!-- Section 5 : Start Server-->
// ***************************************************** // *****************************************************
// starting the server and keeping the connection open to listen for more requests // starting the server and keeping the connection open to listen for more requests
app.listen(3000); module.exports = app.listen(3000);
console.log('Server is listening on port 3000'); console.log('Server is listening on port 3000');

View File

@@ -1,4 +1,12 @@
CREATE TABLE users ( CREATE TABLE users (
username VARCHAR(50) PRIMARY KEY, UserID SERIAL PRIMARY KEY,
password CHAR(60) NOT NULL Username VARCHAR(50) UNIQUE NOT NULL,
); Password CHAR(60) NOT NULL
);
CREATE TABLE FavoriteTeam (
UserID INT REFERENCES users(UserID) ON DELETE CASCADE ON UPDATE CASCADE,
TeamID INT,
TeamName VARCHAR(50),
PRIMARY KEY (UserID, TeamID)
);

View File

@@ -0,0 +1,82 @@
// ********************** Initialize server **********************************
const server = require('../src/index.js'); //TODO: Make sure the path to your index.js is correctly added
// ********************** Import Libraries ***********************************
const chai = require('chai'); // Chai HTTP provides an interface for live integration testing of the API's.
const chaiHttp = require('chai-http');
chai.should();
chai.use(chaiHttp);
const {assert, expect} = chai;
// ********************** DEFAULT WELCOME TESTCASE ****************************
describe('Server!', () => {
// Sample test case given to test / endpoint.
it('Returns the default welcome message', done => {
chai
.request(server)
.get('/welcome')
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.status).to.equals('success');
assert.strictEqual(res.body.message, 'Welcome!');
done();
});
});
});
// *********************** TODO: WRITE 2 UNIT TESTCASES **************************
// ********************************************************************************
describe('Testing Add User API', () => {
it('positive: /register', done => {
// Define mock user data
const userData = {
username: 'Vishal Vunnam',
password: '123456'
};
// Make a POST request to /register with mock user data
chai.request(server)
.post('/register')
.send(userData)
.end((err, res) => {
// Assertions
expect(res).to.have.status(200);
done();
});
});
});
//We are checking POST /add_user API by passing the user info in in incorrect manner (name cannot be an integer). This test case should pass and return a status 400 along with a "Invalid input" message.
describe('Testing Add User API', () => {
it('Negative: /register. Checking invalid name', done => {
chai.request(server)
.post('/register')
.send({ Username: 10, Password: '2020-02-20'})
.end((err, res) => {
// Assertions
expect(res).to.have.status(400);
expect(res.body.message).to.equals('Invalid input');
done();
});
});
});
describe('Testing Render', () => {
// Sample test case given to test /test endpoint.
it('test "/login" route should render with an html response', done => {
chai
.request(server)
.get('/login') // for reference, see lab 8's login route (/login) which renders home.hbs
.end((err, res) => {
res.should.have.status(200); // Expecting a success status code
res.should.be.html; // Expecting a HTML response
done();
});
});
});