Skip to content
Snippets Groups Projects
Commit b3d8b039 authored by fhurtado14's avatar fhurtado14
Browse files

Merge branch 'feature/bypass-cas-localhost' into 'main'

able to bypass cas in localhost

See merge request !2
parents 88668598 a9eabb0b
No related branches found
No related tags found
1 merge request!2able to bypass cas in localhost
...@@ -5,4 +5,4 @@ DB_PASSWORD=corps_db_password ...@@ -5,4 +5,4 @@ DB_PASSWORD=corps_db_password
DB_NAME=corps_directory_db DB_NAME=corps_directory_db
PORT=3000 PORT=3000
FRONTEND_URL=localhost:8080 FRONTEND_URL=http://localhost:8080
\ No newline at end of file \ No newline at end of file
...@@ -9,6 +9,28 @@ const casLogin = function (req, res, next) { ...@@ -9,6 +9,28 @@ const casLogin = function (req, res, next) {
// hard-coded for now. // hard-coded for now.
const JWT_SECRET = "secret_key"; const JWT_SECRET = "secret_key";
// if on localhost, accept the authentication and give a key
console.log("user: ", process.env.DB_USER);
if (process.env.DB_USER === "corps_directory_dev") {
// payload for the JWT token
const payload = {
id: 1,
role: "admin",
username: "localhostuser",
};
// sign the token with secret key
const token = jwt.sign(payload, JWT_SECRET, { expiresIn: "1h" });
console.log("sending token for use in localhost");
console.log("Frontend: ", process.env.FRONTEND_URL);
res.redirect(`${process.env.FRONTEND_URL}/home?token=${token}`);
return;
}
// call the authenticate function with CAS // call the authenticate function with CAS
passport.authenticate("cas", function (err, user, info) { passport.authenticate("cas", function (err, user, info) {
if (err) { if (err) {
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "nodemon index.js", "start": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "jest"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
...@@ -21,5 +21,9 @@ ...@@ -21,5 +21,9 @@
"passport": "^0.7.0", "passport": "^0.7.0",
"passport-cas": "^0.1.1", "passport-cas": "^0.1.1",
"sequelize": "^6.37.4" "sequelize": "^6.37.4"
},
"devDependencies": {
"jest": "^29.7.0",
"jest-mock": "^29.7.0"
} }
} }
const { pool } = require("../config/database/database.config"); const { pool } = require("../config/database/database.config");
/*
Add address info into address table and peopleXaddress table
*/
async function addAddress(address, peopleId) { async function addAddress(address, peopleId) {
// get all the parts of the address // get all the parts of the address
let { address1, address2, city, state, country, zipCode, preferredAddress } = let { address1, address2, city, state, country, zipCode, preferredAddress } =
...@@ -43,7 +46,6 @@ async function addAddress(address, peopleId) { ...@@ -43,7 +46,6 @@ async function addAddress(address, peopleId) {
INSERT INTO peopleXAddress (peopleId, addressId, preferredAddress) INSERT INTO peopleXAddress (peopleId, addressId, preferredAddress)
VALUES (?, ?, ?) VALUES (?, ?, ?)
`; `;
const [peopleAddressResult] = await pool.query(peopleAddressQuery, [ const [peopleAddressResult] = await pool.query(peopleAddressQuery, [
peopleId, peopleId,
addressId, addressId,
...@@ -54,7 +56,9 @@ async function addAddress(address, peopleId) { ...@@ -54,7 +56,9 @@ async function addAddress(address, peopleId) {
return peopleAddressResult; return peopleAddressResult;
} catch (error) { } catch (error) {
// return null if error
console.error(error); console.error(error);
return null;
} }
} }
......
const { pool } = require("../config/database/database.config");
/*
Function to add a row into the peopleContact
table with the contact info given
*/
async function addContact(contact, peopleId) {
// extract the parts of the contact
let { contactNumber, contactType, preferredContact } = contact;
console.log("adding contact....");
try {
// query for adding the contact
const query = `
INSERT INTO peopleContact (peopleId, contactNumber, contactType, preferredContact)
VALUES (?,?,?,?)
`;
// insert data into db
const [results] = await pool.query(query, [
peopleId,
contactNumber,
contactType,
preferredContact,
]);
return results;
} catch (error) {
console.log(error);
return null;
}
}
module.exports = { addContact };
const { pool } = require("../config/database/database.config"); const { pool } = require("../config/database/database.config");
/*
Add degree information into the database.
*/
async function addDegree(degree, peopleId) { async function addDegree(degree, peopleId) {
// get all the parts of the degree // get all the parts of the degree
let { let {
...@@ -14,8 +17,9 @@ async function addDegree(degree, peopleId) { ...@@ -14,8 +17,9 @@ async function addDegree(degree, peopleId) {
try { try {
// Use the helper function to determine the degreeTypeId // Use the helper function to determine the degreeTypeId
degreeTypeId = await _lookupDegreeTypeId(degreeType); let degreeTypeId = await _lookupDegreeTypeId(degreeType);
// query to insert degree info
const query = ` const query = `
INSERT INTO peopleDegree (peopleId, degreeTypeId, degreeDepartment, degreeCollege, degreeYear, degreeDescription) INSERT INTO peopleDegree (peopleId, degreeTypeId, degreeDepartment, degreeCollege, degreeYear, degreeDescription)
VALUES (?,?,?,?,?,?) VALUES (?,?,?,?,?,?)
...@@ -33,7 +37,9 @@ async function addDegree(degree, peopleId) { ...@@ -33,7 +37,9 @@ async function addDegree(degree, peopleId) {
return results; return results;
} catch (error) { } catch (error) {
// return null if there is an error
console.error(error); console.error(error);
return null;
} }
} }
...@@ -61,4 +67,4 @@ async function _lookupDegreeTypeId(degreeType) { ...@@ -61,4 +67,4 @@ async function _lookupDegreeTypeId(degreeType) {
} }
} }
module.exports = { addDegree }; module.exports = { addDegree, _lookupDegreeTypeId };
...@@ -47,7 +47,12 @@ async function addPerson(person) { ...@@ -47,7 +47,12 @@ async function addPerson(person) {
return results.insertId; return results.insertId;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return null;
} }
} }
async function getPersonById() {}
async function deletePersonById() {}
module.exports = { addPerson }; module.exports = { addPerson };
...@@ -122,13 +122,13 @@ peopleRouter.post("/create", validateCreatePerson, async (req, res) => { ...@@ -122,13 +122,13 @@ peopleRouter.post("/create", validateCreatePerson, async (req, res) => {
const person = await createPerson(req.body); const person = await createPerson(req.body);
console.log("In people routes after add: ", person); console.log("In people routes after add: ", person);
return res.status(201).json({ message: "person added successfully" }); return res
.status(201)
.json({ message: "person added successfully", personId: person });
} catch (error) { } catch (error) {
console.log("error"); console.log("error");
return res.status(500).json({ message: error }); return res.status(500).json({ message: error });
} }
// no logic for now, return 200
return res.status(200).json({ message: "No errors" });
}); });
module.exports = peopleRouter; module.exports = peopleRouter;
const { addAddress } = require("../repository/addressRepository"); const { addAddress } = require("../repository/addressRepository");
const { addContact } = require("../repository/contactRepository");
const { addDegree } = require("../repository/degreeRepository"); const { addDegree } = require("../repository/degreeRepository");
const { addPerson } = require("../repository/peopleRepository"); const { addPerson } = require("../repository/peopleRepository");
...@@ -25,12 +26,17 @@ async function createPerson(person) { ...@@ -25,12 +26,17 @@ async function createPerson(person) {
// insert the contact information into the database // insert the contact information into the database
for (const contact of contacts) { for (const contact of contacts) {
const addedContact = await addContact(contact, newPersonID);
console.log("added contact: ", contact);
} }
// insert the involvment information into the database // insert the involvment information into the database
for (const involvment of involvements) { for (const involvment of involvements) {
console.log("not adding involvements yet.");
} }
// TODO: log database change here
return newPersonID; return newPersonID;
} }
......
// Mock the database's pool and query function
jest.mock("../../config/database/database.config", () => {
const queryMock = jest.fn(); // Mock query function
return { pool: { query: queryMock } }; // Return the mocked pool directly
});
const { pool } = require("../../config/database/database.config");
const { addAddress } = require("../../repository/addressRepository");
/*
Test methods for addressRepository
*/
describe("Testing addressRepository.js", () => {
const mockAddress1 = {
address1: "123 Main St",
address2: "Apt 4",
city: "Springfield",
state: "VA",
country: "United States",
zipCode: "22150",
preferredAddress: true,
};
const mockAddress2 = {
address1: "12 Center St",
address2: "Apt 14",
city: "Centerville",
state: "VA",
country: "United States",
zipCode: "23456",
preferredAddress: true,
};
const mockPeopleId = 1;
describe("addAddress tests", () => {
it("Insert 1 address with no errors", async () => {
const mockAddressId = 2;
const mockPeopleXAddressResult = { affectedRows: 1 };
// Mock database query for inserting address and peopleXAddress
pool.query
.mockResolvedValueOnce([{ insertId: mockAddressId }]) // First query for address insertion
.mockResolvedValueOnce([mockPeopleXAddressResult]); // Second query for peopleXAddress insertion
const result = await addAddress(mockAddress1, mockPeopleId);
// Verify first query was called for address table
expect(pool.query).toHaveBeenCalledWith(expect.any(String), [
"123 Main St",
"Apt 4",
"Springfield",
1, // Hardcoded for now
1, // Hardcoded fow now
"22150",
]);
// Verify second query was called for peopleXAddress table
expect(pool.query).toHaveBeenCalledWith(expect.any(String), [
mockPeopleId,
mockAddressId, // ensure the correct addressID was used
true,
]);
// Expect the function to return the result of the second query
expect(result).toBe(mockPeopleXAddressResult);
});
it("should handle errors during address insertion", async () => {
// Mock database query to throw an error during address insertion
const mockError = new Error("Database query failed");
pool.query.mockRejectedValueOnce(mockError);
// Call addAddress and expect it to handle the error
const result = await addAddress(mockAddress1, mockPeopleId);
expect(result).toBe(null);
});
it("Insert 1 address with no errors", async () => {
const mockAddressId = 2;
const mockPeopleXAddressResult = { affectedRows: 1 };
// Mock database query for inserting address and peopleXAddress
pool.query.mockResolvedValueOnce([{ insertId: mockAddressId }]); // First query for address insertion
// Mock second query to throw an error during peopleXAddress insertion
const mockError = new Error("Failed to insert into peopleXAddress");
pool.query.mockRejectedValueOnce(mockError);
const result = await addAddress(mockAddress1, mockPeopleId);
// Verify first query was called for address table
expect(pool.query).toHaveBeenCalledWith(expect.any(String), [
"123 Main St",
"Apt 4",
"Springfield",
1, // Hardcoded for now
1, // Hardcoded fow now
"22150",
]);
// Verify second query was called for peopleXAddress table
expect(pool.query).toHaveBeenCalledWith(expect.any(String), [
mockPeopleId,
mockAddressId, // ensure the correct addressID was used
true,
]);
// expect a null return after the call
expect(result).toBe(null);
});
});
});
// Mock the database's pool and query function
jest.mock("../../config/database/database.config", () => {
const queryMock = jest.fn(); // Mock query function
return { pool: { query: queryMock } }; // Return the mocked pool directly
});
const { pool } = require("../../config/database/database.config");
const { addContact } = require("../../repository/contactRepository");
/*
Test methods for peopleRepository
*/
describe("Testing peopleRepository.js", () => {
// mock contact value to use while testing
const mockContact = {
contactNumber: "1234567890",
contactType: "mobile",
preferredContact: true,
};
const mockPeopleId = 1;
/*
Tests for the addContact method
*/
describe("addContact Tests", () => {
it("insert a contact with no errors", async () => {
// Mock the database to return a success response
const mockResults = { affectedRows: 1 };
pool.query.mockResolvedValueOnce([mockResults]);
// Call addContact
const result = await addContact(mockContact, mockPeopleId);
// Verify the SQL query and parameters
expect(pool.query).toHaveBeenCalledWith(expect.any(String), [
1,
"1234567890",
"mobile",
true,
]);
// Expect function to return the database result
expect(result).toBe(mockResults);
});
it("handle errors if the query fails", async () => {
// Mock the database to throw an error
const mockError = new Error("Database query failed");
pool.query.mockRejectedValueOnce(mockError);
// Call addContact and expect it to handle the error
const result = await addContact(mockContact, mockPeopleId);
expect(result).toBe(null);
});
});
});
// Mock the database's pool and query function
jest.mock("../../config/database/database.config", () => {
const queryMock = jest.fn(); // Mock query function
return { pool: { query: queryMock } }; // Return the mocked pool directly
});
const { pool } = require("../../config/database/database.config.js");
const { addDegree } = require("../../repository/degreeRepository.js");
/*
Test methods for degreeRepository
*/
describe("Testing degreeRepository.js", () => {
const mockDegree = {
degreeType: "BS",
degreeDepartment: "Computer Science",
degreeCollege: "Engineering",
degreeYear: "2024",
degreeDescription: "Bachelor of Science in Computer Science",
};
const mockPeopleId = 1;
/*
Tests for the addDegree method
*/
describe("addDegree tests", () => {
it("Insert a degree with no errors", async () => {
const mockDegreeTypeId = 1;
const mockResult = { affectedRows: 1 };
// Mock the helper function _lookupDegreeTypeId to return 1
const lookupDegreeTypeIdSpy = jest
.spyOn(
require("../../repository/degreeRepository"),
"_lookupDegreeTypeId"
)
.mockResolvedValueOnce(mockDegreeTypeId);
// Mock the database query for inserting the degree
pool.query.mockResolvedValueOnce([mockResult]);
// Call addDegree
const result = await addDegree(mockDegree, mockPeopleId);
// Verify the SQL query for inserting the degree
expect(pool.query).toHaveBeenCalledWith(expect.any(String), [
mockPeopleId,
null, // null for now
"Computer Science",
"Engineering",
"2024",
"Bachelor of Science in Computer Science",
]);
// Expect the function to return the result from the database
expect(result).toBe(mockResult);
// Clean up spy
lookupDegreeTypeIdSpy.mockRestore();
});
it("error thrown during degree insertion", async () => {
const mockError = new Error("Database query failed");
// Mock the helper function _lookupDegreeTypeId to return 1
const lookupDegreeTypeIdSpy = jest
.spyOn(
require("../../repository/degreeRepository"),
"_lookupDegreeTypeId"
)
.mockResolvedValueOnce(1);
// Mock the database query to throw an error during degree insertion
pool.query.mockRejectedValueOnce(mockError);
// Call addDegree and expect it to handle the error
const result = await addDegree(mockDegree, mockPeopleId);
// Expect the function to return null on error
expect(result).toBeNull();
// Clean up the spy
lookupDegreeTypeIdSpy.mockRestore();
});
it("handle errors during degree type lookup", async () => {
const mockError = new Error("Degree type lookup failed");
// Mock the helper function _lookupDegreeTypeId to throw an error
const lookupDegreeTypeIdSpy = jest
.spyOn(
require("../../repository/degreeRepository"),
"_lookupDegreeTypeId"
)
.mockRejectedValueOnce(mockError);
// Call addDegree and expect it to handle the error
const result = await addDegree(mockDegree, mockPeopleId);
// Expect the function to return null on error
expect(result).toBeNull();
// Clean up the spy
lookupDegreeTypeIdSpy.mockRestore();
});
});
});
// Mock the database's pool and query function
jest.mock("../../config/database/database.config", () => {
const queryMock = jest.fn(); // Mock query function
return { pool: { query: queryMock } }; // Return the mocked pool directly
});
const { pool } = require("../../config/database/database.config");
const { addPerson } = require("../../repository/peopleRepository");
/*
Test methods for peopleRepository
*/
describe("Testing peopleRepository.js", () => {
// mock person for testing
const mockPerson = {
firstName: "John",
lastName: "Doe",
middleName: "A",
maidenName: null,
suffix: null,
nickName: "Johnny",
techAlumniChapter: "Tidewater AC",
classYear: "2024",
gradYear: "2024",
gradSemester: "spring",
gender: "male",
};
/*
Tests for the addPerson method
*/
describe("addPerson Tests", () => {
it("Insert works with no errors", async () => {
// Mock the database to return the new insert ID as 1
const mockInsertId = 1;
pool.query.mockResolvedValueOnce([{ insertId: mockInsertId }]);
// call add person
const result = await addPerson(mockPerson);
// Verify the SQL query and parameters
expect(pool.query).toHaveBeenCalledWith(expect.any(String), [
"John",
"Doe",
"A",
null,
null,
"Johnny",
2, // hardcoded for now
"2024",
"2024",
"spring",
"male",
]);
// expect function to return the new insert id
expect(result).toBe(mockInsertId);
});
it("insert throws an error if query fails", async () => {
// Mock the database to throw an error
const mockError = new Error("Database query failed");
pool.query.mockRejectedValueOnce(mockError);
// call addPerson and expect it to handle the error
const result = await addPerson(mockPerson);
expect(result).toBe(null);
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment