diff --git a/controllers/user.js b/controllers/user.js index b326867..f69a864 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -162,12 +162,18 @@ async function getResponderCount(req, res) { if (user) { let responders = user.responders; let count = 0; + console.log(responders); for (let r of responders) { var responder = await UserModel.findOne({ _id: new ObjectId(r.id) }).lean(); - let availbilityStatus = await AvailbilityService.checkAvailabilityStatus(r.id); - if (availbilityStatus == true) count++; + try{ + let availbilityStatus = await AvailbilityService.checkAvailabilityStatus(r.id); + if (availbilityStatus) count++; + } + catch(err){ + console.log(err); + } } res.status(200).json({ count: count }); } else { diff --git a/services/availability.service.js b/services/availability.service.js index aa4f54b..e04e1eb 100644 --- a/services/availability.service.js +++ b/services/availability.service.js @@ -19,7 +19,7 @@ async function setUnavailable(userId) { async function checkAvailabilityStatus(userId) { try { - const res = await redis.sismemberAsync(availableUsers, userId); + const res = await redis.sismemberAsync(availableUsers, userId.toString() ); return res ? true : false; } catch(err) { console.log("redis checkAvailabilityStatus error: ", err.message); diff --git a/tests/authentication.test.js b/tests/authentication.test.js index f186814..dacfa8e 100644 --- a/tests/authentication.test.js +++ b/tests/authentication.test.js @@ -1,8 +1,10 @@ import chai from "chai"; import chaiHttp from "chai-http"; import app from "../server"; -var database = require("../database"); -var db = database.getdb(); +import metrics from "../database/postgres" + +var metricdb = metrics.getMetrics(); +var UserModel = require("../models/user").model; chai.use(chaiHttp); chai.should(); @@ -18,8 +20,8 @@ describe("Sign up and Login", () => { it("should sign up user", (done) => { chai.request(app) .post("/signup") - .send({ - username: username, + .send({ + username: username, email: email, password: plainTextPassword, phone: phone, @@ -55,12 +57,143 @@ describe("Sign up and Login", () => { }) }) + after(async () => { try { - await db.collection("users").remove({ username: username }) + await UserModel.deleteMany({username:username}); + await metricdb('users') + .whereIn('username', [username]) + .del() console.log("Processed after()"); } catch (err) { console.log("Failed to delete created user"); + console.log(err) } }) -}) \ No newline at end of file +}) + +describe("Create 2 accounts and tests for search and responding",() => { + + var token = ""; + var userId = ""; + + it("should sign up user A", (done) => { + chai.request(app) + .post("/signup") + .send({ + username: "Ausername", + email: "A@gmail.com", + password: "Auserpassword", + phone: "12345678" + }) + .end((err, res) => { + res.should.have.status(200); + res.body.should.be.a("object"); + done(); + }) + }) + + it("should sign up user B", (done) => { + chai.request(app) + .post("/signup") + .send({ + username: "Busername", + email: "B@gmail.com", + password: "Buserpassword", + phone: "988655332" + }) + .end((err, res) => { + res.should.have.status(200); + res.body.should.be.a("object"); + done(); + }) + }) + + + it("should authenticate A with correct credentials", (done) => { + chai.request(app) + .post("/login") + .send({ username: "Ausername", password: "Auserpassword" }) + .end((err, res) => { + res.should.have.status(200); + res.body.should.be.a("object"); + userId = res.body.id; + token = res.body.token; + done(); + }) + }) + + it("should authenticate B with correct credentials and turn it online", (done) => { + chai.request(app) + .post("/login") + .send({ username: "Busername", password: "Buserpassword" }) + .end((err, res) => { + res.should.have.status(200); + res.body.should.be.a("object"); + done(); + }) + }) + var users = []; + var BuserID; + + it("should have B as one of the users being shown up", (done) =>{ + chai.request(app) + .get("/users/search") + .set("Authorization", `Bearer ${token}`) + .end((err,res)=> { + res.should.have.status(200); + console.log(res.body); + res.body.forEach((user)=>{ + users.push(user.username); + if(user.username==="Busername") + BuserID = user._id; + }); + users.should.include("Busername"); + done(); + }) + }) + + it("should add B as a responder for user A", (done) =>{ + let respondersToAddArr = [{id: BuserID}]; + + chai.request(app) + .post(`/users/${userId}/responders`) + .set("Authorization", `Bearer ${token}`) + .send({respondersToAdd: respondersToAddArr}) + .end((err, res) => { + console.log(res.body); + res.should.have.status(200); + res.body.should.be.a("object"); + done(); + }) + }) + + it("should have 1 responder count for user A", (done) => { + chai.request(app) + .get(`/users/${userId}/responders/count`) + .set("Authorization", `Bearer ${token}`) + .end((err, res) => { + console.log(res.body); + res.should.have.status(200); + res.body.should.be.a("object"); + console.log(res.body); + done(); + }) + }) + + +after(async () => { + try { + await UserModel.deleteMany({username: { $in: ["Busername","Ausername"]}}); + await metricdb('users') + .whereIn('username', ["Busername","Ausername"]) + .del() + console.log("Processed after()"); + } catch (err) { + console.log("Failed to delete created user"); + console.log(err) + } + +}) + +})