From ff81ceb4669a4928a149380725797d736eedd1ef Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Sat, 25 Jan 2020 14:47:24 -0800 Subject: [PATCH 1/8] first commit --- tests/authentication.test.js | 107 +++++++++++++++++++++++++++++++++-- 1 file changed, 103 insertions(+), 4 deletions(-) diff --git a/tests/authentication.test.js b/tests/authentication.test.js index f186814..9eb5aee 100644 --- a/tests/authentication.test.js +++ b/tests/authentication.test.js @@ -18,8 +18,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 +55,111 @@ describe("Sign up and Login", () => { }) }) + after(async () => { try { - await db.collection("users").remove({ username: username }) + await db.collection("users").deleteOne({ username: username }) console.log("Processed after()"); } catch (err) { console.log("Failed to delete created user"); } }) -}) \ 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(); + }) + }) + + 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}]; + console.log(respondersToAddArr); + chai.request(app) + .post("/users/${userId}/responders") + .set("Authorization", `Bearer ${token}`) + .send({respondersToAdd: respondersToAddArr}) + .end((err, res) => { + res.should.have.status(200); + res.body.should.be.a("object"); + done(); + }) + }) + + +after(async () => { + try { + await db.collection("users").deleteMany({username: { $in: ["Busername","Ausername"]}}); + console.log("Processed after()"); + } catch (err) { + console.log("Failed to delete created user"); + } + +}) + +}) From 1bad0c17f8dd4f2311258886bc16984bbc322d9d Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Sat, 21 Mar 2020 12:29:45 -0700 Subject: [PATCH 2/8] change directory --- tests/authentication.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/authentication.test.js b/tests/authentication.test.js index 9eb5aee..2df6bce 100644 --- a/tests/authentication.test.js +++ b/tests/authentication.test.js @@ -1,7 +1,7 @@ import chai from "chai"; import chaiHttp from "chai-http"; import app from "../server"; -var database = require("../database"); +var database = require("../database/mongoose"); var db = database.getdb(); chai.use(chaiHttp); From adfa4d0b36edfa122137222cb8fc869e97b041d7 Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Sat, 21 Mar 2020 15:07:32 -0700 Subject: [PATCH 3/8] json send bug --- controllers/user.js | 4 ++-- services/availability.service.js | 2 +- tests/authentication.test.js | 26 ++++++++++++++++++++------ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/controllers/user.js b/controllers/user.js index 1a8b120..f214f0e 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -140,7 +140,7 @@ async function getResponders(req, res) { var responder = await UserModel.findOne({ _id: new ObjectId(r.id) }).lean(); - let availbilityStatus = await AvailbilityService.checkAvailabilityStatus(r.id); + let availbilityStatus = await AvailbilityService(r.id); returnInfo.push({ id: r.id, username: responder.username, @@ -165,7 +165,7 @@ async function getResponderCount(req, res) { var responder = await UserModel.findOne({ _id: new ObjectId(r.id) }).lean(); - let availbilityStatus = await AvailbilityService.checkAvailabilityStatus(r.id); + let availbilityStatus = await AvailbilityService(r.id); if (availbilityStatus == true) count++; } res.status(200).json({ count: count }); 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 2df6bce..0481f68 100644 --- a/tests/authentication.test.js +++ b/tests/authentication.test.js @@ -1,8 +1,13 @@ import chai from "chai"; import chaiHttp from "chai-http"; import app from "../server"; -var database = require("../database/mongoose"); -var db = database.getdb(); +import metrics from "../database/postgres" + +const InitializationService = require("../services/initialization.service"); +InitializationService.initialize(); + +var metricdb = metrics.getMetrics(); +var UserModel = require("../models/user").model; chai.use(chaiHttp); chai.should(); @@ -58,10 +63,14 @@ describe("Sign up and Login", () => { after(async () => { try { - await db.collection("users").deleteOne({ 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) } }) }) @@ -139,12 +148,13 @@ describe("Create 2 accounts and tests for search and responding",() => { it("should add B as a responder for user A", (done) =>{ let respondersToAddArr = [{id: BuserID}]; - console.log(respondersToAddArr); + console.log(respondersToAddArr) chai.request(app) .post("/users/${userId}/responders") .set("Authorization", `Bearer ${token}`) - .send({respondersToAdd: respondersToAddArr}) + .send({respondersToAdd : respondersToAddArr}) .end((err, res) => { + console.log(res.body); res.should.have.status(200); res.body.should.be.a("object"); done(); @@ -154,10 +164,14 @@ describe("Create 2 accounts and tests for search and responding",() => { after(async () => { try { - await db.collection("users").deleteMany({username: { $in: ["Busername","Ausername"]}}); + 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) } }) From e071d63dcdc48dc3268a858da2a533e04e6c4784 Mon Sep 17 00:00:00 2001 From: Asiapenguin Date: Sat, 21 Mar 2020 15:16:35 -0700 Subject: [PATCH 4/8] Change string interpolation --- tests/authentication.test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/authentication.test.js b/tests/authentication.test.js index 0481f68..ae4ef12 100644 --- a/tests/authentication.test.js +++ b/tests/authentication.test.js @@ -3,8 +3,8 @@ import chaiHttp from "chai-http"; import app from "../server"; import metrics from "../database/postgres" -const InitializationService = require("../services/initialization.service"); -InitializationService.initialize(); +// const InitializationService = require("../services/initialization.service"); +// InitializationService.initialize(); var metricdb = metrics.getMetrics(); var UserModel = require("../models/user").model; @@ -148,11 +148,11 @@ describe("Create 2 accounts and tests for search and responding",() => { it("should add B as a responder for user A", (done) =>{ let respondersToAddArr = [{id: BuserID}]; - console.log(respondersToAddArr) + chai.request(app) - .post("/users/${userId}/responders") + .post(`/users/${userId}/responders`) .set("Authorization", `Bearer ${token}`) - .send({respondersToAdd : respondersToAddArr}) + .send({respondersToAdd: respondersToAddArr}) .end((err, res) => { console.log(res.body); res.should.have.status(200); From bdf9246b10ef37f045a204ee5f93d2e3e7228afe Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Sat, 28 Mar 2020 14:01:36 -0700 Subject: [PATCH 5/8] adding responder_count test --- tests/authentication.test.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/authentication.test.js b/tests/authentication.test.js index ae4ef12..c01e57e 100644 --- a/tests/authentication.test.js +++ b/tests/authentication.test.js @@ -3,9 +3,6 @@ import chaiHttp from "chai-http"; import app from "../server"; import metrics from "../database/postgres" -// const InitializationService = require("../services/initialization.service"); -// InitializationService.initialize(); - var metricdb = metrics.getMetrics(); var UserModel = require("../models/user").model; @@ -161,6 +158,19 @@ describe("Create 2 accounts and tests for search and responding",() => { }) }) + 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 { From 50de6d0ad6906e18f50acce2467d107ef828a48a Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Sat, 28 Mar 2020 14:32:34 -0700 Subject: [PATCH 6/8] added changes --- controllers/user.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers/user.js b/controllers/user.js index ea0b6fa..b326867 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -141,7 +141,7 @@ async function getResponders(req, res) { var responder = await UserModel.findOne({ _id: new ObjectId(r.id) }).lean(); - let availbilityStatus = await AvailbilityService(r.id); + let availbilityStatus = await AvailbilityService.checkAvailabilityStatus(r.id); returnInfo.push({ id: r.id, username: responder.username, @@ -166,7 +166,7 @@ async function getResponderCount(req, res) { var responder = await UserModel.findOne({ _id: new ObjectId(r.id) }).lean(); - let availbilityStatus = await AvailbilityService(r.id); + let availbilityStatus = await AvailbilityService.checkAvailabilityStatus(r.id); if (availbilityStatus == true) count++; } res.status(200).json({ count: count }); From f9c242e83b94fb844a42fde858ff7b908ae5f375 Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Sat, 28 Mar 2020 14:34:52 -0700 Subject: [PATCH 7/8] added changes --- controllers/user.js | 11 +++++++++++ tests/authentication.test.js | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/controllers/user.js b/controllers/user.js index b326867..029d1bf 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -162,12 +162,23 @@ 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(); +<<<<<<< Updated upstream 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); + } +>>>>>>> Stashed changes } res.status(200).json({ count: count }); } else { diff --git a/tests/authentication.test.js b/tests/authentication.test.js index c01e57e..dacfa8e 100644 --- a/tests/authentication.test.js +++ b/tests/authentication.test.js @@ -123,6 +123,16 @@ describe("Create 2 accounts and tests for search and responding",() => { }) }) + 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; From 94f8f2dcf692a5dc13435d7722f10b47ec439f7c Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Sat, 28 Mar 2020 14:36:12 -0700 Subject: [PATCH 8/8] added changes --- controllers/user.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/controllers/user.js b/controllers/user.js index 029d1bf..f69a864 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -167,10 +167,6 @@ async function getResponderCount(req, res) { var responder = await UserModel.findOne({ _id: new ObjectId(r.id) }).lean(); -<<<<<<< Updated upstream - let availbilityStatus = await AvailbilityService.checkAvailabilityStatus(r.id); - if (availbilityStatus == true) count++; -======= try{ let availbilityStatus = await AvailbilityService.checkAvailabilityStatus(r.id); if (availbilityStatus) count++; @@ -178,7 +174,6 @@ async function getResponderCount(req, res) { catch(err){ console.log(err); } ->>>>>>> Stashed changes } res.status(200).json({ count: count }); } else {