Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing completed for responder endpoints #75

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i believe this is not needed (try catch block)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was something I had during debugging, I'll take it away

let availbilityStatus = await AvailbilityService.checkAvailabilityStatus(r.id);
if (availbilityStatus) count++;
}
catch(err){
console.log(err);
}
}
res.status(200).json({ count: count });
} else {
Expand Down
2 changes: 1 addition & 1 deletion services/availability.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
145 changes: 139 additions & 6 deletions tests/authentication.test.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
})
})
})

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: "[email protected]",
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: "[email protected]",
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)
}

})

})