Skip to content

Commit

Permalink
Added more tests with auth - bug fix broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
Shay2Shay committed May 2, 2024
1 parent 0d0fe24 commit 7da61d4
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 18 deletions.
62 changes: 62 additions & 0 deletions demo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ const { expect } = require("expect")
const supertest = require('supertest')
const {app} = require('./server/test_server.js')

async function logAndToken(){
const response = await supertest(app).post("/api/consumer/login/")
.send({
email: "[email protected]",
password: "abCD12#$"
})
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')

return response._body.data
}





test('demo testing for sum', () => {
expect(1 + 2).toBe(3)
})
Expand All @@ -13,4 +29,50 @@ test('testing get all services', async () => {
return
})

test('testing login user', async () => {
const response = await supertest(app).post("/api/consumer/login/")
.send({
email: "[email protected]",
password: "abCD12#$"
})
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
expect(response._body.data).not.toBe(null)
})

test('getting profile data', async () => {
const tokn = await logAndToken()
const response = await supertest(app).get("/api/consumer/profile")
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.set("Authorization", `Bearer ${tokn}`)
// console.log(response)
expect(response._body.data).not.toBe(null)
})

test('getting list of broadcast', async () => {
const tkn = await logAndToken();
const response = await supertest(app).get("/api/consumer/viewbroadcast")
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.set("Authorization", `Bearer ${tkn}`)

console.log(response._body)
expect(response._body.data).not.toBe(null)
})

test('fetching seller details for customer', async () => {
const tkn = await logAndToken();
const response = await supertest(app).post("/api/consumer/sellerprofileview")
.send({
pointer: "seller-id"
})
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.set("Authorization", `Bearer ${tkn}`)

// console.log(response._body)
expect(response._body.data).toBe(undefined)
})

// Finalise
4 changes: 2 additions & 2 deletions server/Controllers/ConsumerLoginController.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function consumerloginController (req, res){
req.body.password,
consumer.password
);
console.log(validPassword);
// console.log(validPassword);

if (!validPassword){

Expand All @@ -35,7 +35,7 @@ async function consumerloginController (req, res){

res.status(200).send({ data: token, message: "logged in successfully" });
} catch (error) {
console.log(error)
// console.log(error)
res.status(500).send({ message: "Internal Server Error" });
}
}
Expand Down
8 changes: 4 additions & 4 deletions server/Controllers/GetSellerProfileConsumerSideController.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ async function sellerProfilefromConsumer (req, res){
try{


console.log("here7")
console.log(req.body)
// console.log("here7")
// console.log(req.body)
// console.log(req.user.email)


const user=await Seller.findOne({_id:req.body.pointer});
console.log(user)
// console.log(user)
// if(!user.acknowledged)
// {
// res.status(500).send({message:"User not found error"});
Expand All @@ -32,7 +32,7 @@ async function sellerProfilefromConsumer (req, res){

res.status(201).send({data:user,message:"Seller Info"}) ;
}catch(e){
console.log(e)
// console.log(e)
res.status(500).send({message:"internl server error"})
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/Controllers/ViewBroadCastConsumerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function viewBroadcastController (req, res){
try {


const broadcastmess = await Broadcast.find({$or:[{pointer:"consumer"},{pointer:"all"}] });
const broadcastmess = await Broadcast.find({$or:[{pointer:"consumers"},{pointer:"all"}] });


res.status(201).send({ data: broadcastmess ,message: "Successful" });
Expand Down
8 changes: 4 additions & 4 deletions server/Controllers/consumerprofileviewcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ async function consumerProfileViewController (req, res){
try{


console.log("here7")
// console.log("here7")
// console.log(error)




// const{tokn}=checktokenMiddleware(req,res)

console.log("verified")
console.log(req.user.email);
// console.log("verified")
// console.log(req.user.email);


res.status(201).send({message:"founduser",data:req.user}) ;
}catch(e){
console.log(e)
// console.log(e)
res.status(500).send({message:"internl server error"})
}
}
Expand Down
10 changes: 5 additions & 5 deletions server/Middleware/CheckTokenLocally.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function authenticateToken(req,res,next)
}

const token=authHeader.split(" ")[1];
console.log(token)
// console.log(token)
jwt.verify(token,process.env.JWTPRIVATEKEY,async (err,decoded)=>
{
if(err)
Expand All @@ -20,17 +20,17 @@ function authenticateToken(req,res,next)
return res.status(430).json({ error: err });
}

console.log("verified1")
// console.log("verified1")

const userId=decoded._id;

console.log(userId)
// console.log(userId)

const user=await Consumer.findById(userId);

req.user=user;
console.log("heyhere");
console.log(req.user);
// console.log("heyhere");
// console.log(req.user);
next();
});
}
Expand Down
1 change: 0 additions & 1 deletion server/Utils/loginValidation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const Joi = require("joi");

const validate = (data) => {
console.log(data);
const schema = Joi.object({
email: Joi.string().email().required().label("Email"),
password: Joi.string().required().label("Password"),
Expand Down
2 changes: 1 addition & 1 deletion server/test_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,4 @@ app.use('/api/seller/sendchat',SellerSendChatRoute)

setTimeout(()=>{
mongoose.connection.close()
}, 5000)
}, 10000)

0 comments on commit 7da61d4

Please sign in to comment.