diff --git a/back-end/App.js b/back-end/App.js index e642722..b625576 100644 --- a/back-end/App.js +++ b/back-end/App.js @@ -1,13 +1,12 @@ const express = require("express"); -const cors = require('cors') // middleware for enabling CORS (Cross-Origin Resource Sharing) requests. +const cors = require('cors'); // middleware for enabling CORS (Cross-Origin Resource Sharing) requests. const app = express(); -app.use(cors()) // allow cross-origin resource sharing +app.use(cors()); // allow cross-origin resource sharing - -app.use(express.json()) // decode JSON-formatted incoming POST data -app.use(express.urlencoded({ extended: true })) // decode url-encoded incoming POST data -console.log("created backend server!!!!!!!!!!!!!!!!") +app.use(express.json()); // decode JSON-formatted incoming POST data +app.use(express.urlencoded({ extended: true })); // decode url-encoded incoming POST data +console.log("created backend server!!!!!!!!!!!!!!!!"); let surveyDataArray = []; //This will store new incoming survey data. Its purpose is to simuate the new survey data being sent to the backend let edit_profile_array = []; //This is basically just the survey responses, for now we have just 1 @@ -96,6 +95,7 @@ app.get('/matches', async (req, res) => { } }); +//returns a bunch of json objects as an array app.get('/chatlist', async (req, res) => { try { //Here, we will send a request to the database, searching for users that the user currently has an active chat with (not sure that determiend at the moment) @@ -184,6 +184,8 @@ app.get('/chatlist', async (req, res) => { } }); +//expecting json object with handle sumbit attruputes -- in Survey.js +//should push to surveyData arr app.post('/survey', (req, res) => { const surveyData = req.body; surveyDataArray.push(surveyData); diff --git a/back-end/package.json b/back-end/package.json index 6e47ca3..2a23cc7 100644 --- a/back-end/package.json +++ b/back-end/package.json @@ -4,15 +4,19 @@ "description": "The back-end of your project will live in this directory.", "main": "Server.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", + "test": "mocha --timeout 10000", "start": "nodemon Server" }, "author": "", "license": "ISC", "dependencies": { + "cors": "^2.8.5", "express": "^4.18.3" }, "devDependencies": { + "chai": "^4.2.0", + "chai-http": "^4.4.0", + "mocha": "^10.4.0", "nodemon": "^3.1.0" } } diff --git a/back-end/test.js b/back-end/test.js new file mode 100644 index 0000000..f06597e --- /dev/null +++ b/back-end/test.js @@ -0,0 +1,171 @@ +const app = require('./App.js'); +const chai = require('chai'); +const chaiHttp = require('chai-http'); +const { ChildProcess } = require('child_process'); + +const expect = chai.expect; +const should = chai.should(); +chai.use(chaiHttp); + +describe("/GET profile info", () => { + it("should GET json with profile info", (done) => { + chai.request(app) + .get('/profile') + .end((err, res) => { + res.should.have.status(200); + res.body.should.be.a('object'); + done(); + }); + }); + it("json file should have all profile information fields", (done) => { + chai.request(app) + .get('/profile') + .end((err, res) => { + res.body.should.have.property('bio'); + res.body.should.have.property('imagePath'); + res.body.should.have.property('user_id'); + res.body.should.have.property('name'); + res.body.should.have.property('pets'); + res.body.should.have.property('guests'); + res.body.should.have.property('rent_max'); + res.body.should.have.property('rent_min'); + res.body.should.have.property('bedtime'); + done(); + }); + }); + it("all fields in the profile json file should have the correct data types", (done) => { + chai.request(app) + .get('/profile') + .end((err, res) => { + res.body.bio.should.be.a('string'); + res.body.user_id.should.be.a('string'); + res.body.name.should.be.a('string'); + res.body.pets.should.be.a('string'); + res.body.guests.should.be.a('string'); + res.body.rent_max.should.be.a('number'); + res.body.rent_min.should.be.a('number'); + res.body.bedtime.should.be.a('string'); + done(); + }); + }); +}); + +describe("/GET my Preferences info", () => { + it("should successfully GET a json file with preferences info", (done) => { + chai.request(app) + .get('/mypreferences') + .end((err, res) => { + res.should.have.status(200); + res.body.should.be.a('object'); + done(); + }); + }); + it("json file should have all of the expected preferences fields", (done) => + { + chai.request(app) + .get('/mypreferences') + .end((err, res) => { + res.body.should.have.property('bio'); + res.body.should.have.property('imagePath'); + res.body.should.have.property('user_id'); + res.body.should.have.property('name'); + res.body.should.have.property('pets'); + res.body.should.have.property('guests'); + res.body.should.have.property('rent_max'); + res.body.should.have.property('rent_min'); + res.body.should.have.property('bedtime'); + res.body.should.have.property('roommates'); + done(); + }); + }); + it("all fields in the preferences json file should have the correct data types", (done) => { + chai.request(app) + .get('/mypreferences') + .end((err, res) => { + res.body.bio.should.be.a('string'); + res.body.imagePath.should.be.a('string'); + res.body.user_id.should.be.a('string'); + res.body.name.should.be.a('string'); + res.body.pets.should.be.a('string'); + res.body.guests.should.be.a('string'); + res.body.rent_max.should.be.a('number'); + res.body.rent_min.should.be.a('number'); + res.body.bedtime.should.be.a('string'); + res.body.roommates.should.be.a('number'); + done(); + }); + }); +}); + +describe("/POST Survey data", () => { + it("should acknowledge successful POSt requests to the survey", (done) => { + const testSurvey = { + petsPreference: 'yes', + guestPreference: 'no', + minRent: 900, + maxRent: 1500, + desiredRoommates: 1, + bedtime: '3am' + } + chai.request(app) + .post('/survey') + .send(testSurvey) + .end((err, res) => { + res.should.have.status(200); + res.body.should.be.a('object'); + //this will need to be updated if the survey route returns anything + done(); + }); + }); +}); + +describe("/GET Chat list info", () => { + it("should GET an array of all currently active chats", (done) => { + chai.request(app) + .get('/chatlist') + .end((err, res) => { + res.should.have.status(200); + res.body.should.be.a('array'); + done(); + }); + }); +}); + +describe("/GET Matches info", () => { + it("should GET an array of all matches", (done) => { + chai.request(app) + .get('/matches') + .end((err, res) => { + res.should.have.status(200); + res.body.should.be.a('array'); + done(); + }); + }); + it("the matches array should not be empty", (done) => { + chai.request(app) + .get('/matches') + .end((err, res) => { + res.body.should.not.be.empty; + done(); + }) + }) +}); + +//this will definitly need more unit tests in the future +describe("/POST updates to user profile info", () => { + it("should acknowledge successful POST requests to edit profile", (done) => { + const testProfile = { + new_password: 'password10', + new_username: 'Computer', + old_password: 'password7', + new_bio: 'hello world :)' + } + chai.request(app) + .post('/editprofile') + .send(testProfile) + .end((err, res) => { + res.should.have.status(200); + done(); + }); + }); +}); \ No newline at end of file