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

finished user routes #151

Merged
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
13 changes: 10 additions & 3 deletions back-end/Routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ router.post("/login", async (req, res) => {
try{
const user = UsersList.find(user => user.email === req.body.email)
if (!user){
return res.status(400).json("You Have Not Registered with us yet!")
return res.status(400).json({success: false, message: `${req.body.email} is not registered with us yet!`})
}
const validatePassword = await bcrypt.compare(req.body.password, user.password)
if (!validatePassword){
return res.status(400).json("Wrong Username and/or Password!")
return res.status(400).json({success: false, message: "Wrong Username and/or Password!"})
}
const newUser = {
success: true,
message: "Successful Login!",
user: user.user,
_id: user._id,
email: user.email,
name: {
Expand All @@ -66,9 +69,13 @@ router.post("/login", async (req, res) => {
router.get("/:id", async (req, res) => {
try{
const userFind = UsersList.find(user => user._id == req.params.id)
if (!userFind){
return res.status(400).json({success: false, message: `User ID: ${req.params.id} is not valid!`})
}
// EXCLUDE PASSWORD
const user = {
_id: userFind._id,
user: userFind.user,
email: userFind.email,
name: {
first: userFind.name.first,
Expand All @@ -81,7 +88,7 @@ router.get("/:id", async (req, res) => {
following: userFind.following,
followers: userFind.followers
}
res.status(200).json(user)
return res.status(200).json(user)
} catch (err){
console.log(err)
res.status(500).json(err)
Expand Down
24 changes: 14 additions & 10 deletions back-end/Test/artworks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,25 +252,29 @@ describe('The "/artworks" route', () => {
})
})
describe('The "/activeStatus/:status" route GET function for all artworks with a certain status', () => {
const random = Math.floor(Math.random() * 2) + 1
const status = "sold"
if (random === 1){
status = "available"
}
it('should be an array', (done) => {
it('should return an array', (done) => {
chai.request(server)
.get(`/artworks/activeStatus/${status}`)
.get(`/artworks/activeStatus/available`)
.end((err, res) => {
res.body.should.be.a('array')
done()
})
})
it(`should find all artworks with ${status} status`, (done) => {
it(`should find all artworks with correct status`, (done) => {
chai.request(server)
.get(`/artworks/activeStatus/available`)
.end((err, res) => {
res.should.have.status(200)
res.body.forEach(artwork => artwork.status.should.equal("available"))
done()
})
})
it(`should find all artworks with correct status`, (done) => {
chai.request(server)
.get(`/artworks/activeStatus/${status}`)
.get(`/artworks/activeStatus/sold`)
.end((err, res) => {
res.should.have.status(200)
res.body.forEach(artwork => artwork.status.should.be.equals(status))
res.body.forEach(artwork => artwork.status.should.equal("sold"))
done()
})
})
Expand Down
184 changes: 157 additions & 27 deletions back-end/Test/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('The "/users" route', () => {
"_id": 5,
"user": "Artist",
"name": {
"first": "Anh",
"last": "Tran"
"first": "Random",
"last": "Userrr"
},
"email": "[email protected]",
"password": "123456",
Expand All @@ -24,58 +24,188 @@ describe('The "/users" route', () => {
"following" : [],
"followers" : []
}
it('should be an object', (done) => {
it('should respond the user object with successful status', (done) => {
chai.request(server)
.post('/users/register')
.send(newUser)
.end((err, res) => {
res.body.should.be.a('object')
res.should.have.status(200)
res.body.should.be.a("object")
res.body.should.have.property("_id")
res.body._id.should.be.a("number")
res.body.should.have.property("user")
res.body.user.should.be.a("string")

res.body.should.have.property("name")
res.body.name.should.be.a("object")
res.body.name.should.have.property("first")
res.body.name.first.should.be.a("string")
res.body.name.should.have.property("last")
res.body.name.last.should.be.a("string")
res.body.name.should.have.property("full")
res.body.name.full.should.be.a("string")

res.body.should.have.property("password")
res.body.password.should.be.a("string")

res.body.should.have.property("products_uploaded")
res.body.products_uploaded.should.be.a("array")
res.body.should.have.property("cart")
res.body.cart.should.be.a("array")
res.body.should.have.property("saved")
res.body.saved.should.be.a("array")

res.body.should.have.property("following")
res.body.following.should.be.a("array")
res.body.should.have.property("followers")
res.body.followers.should.be.a("array")
done()
})
})
it('should return all 15 artworks stored successfully', (done) => {
it('should hash users password before saving', (done) => {
chai.request(server)
.post('/users/register')
.send(newUser)
.end((err, res) => {
res.body.should.have.status(200)
res.body
(res.body.password !== newUser.password).should.be.true
done()
})
})
})
describe('The "/login" route POST function for registering a new user', () => {
const newUser = {
"_id": 5,
"user": "Artist",
"name": {
"first": "Anh",
"last": "Tran"
},
"email": "[email protected]",
"password": "123456",
"products_uploaded": [],
"cart": [],
"saved": [],
"following" : [],
"followers" : []
"email": "[email protected]",
"password": "123456"
}
const nonExistantUser = {
"email": "[email protected]",
"password": "123456"
}
const wrongPasswordUser = {
"email": "[email protected]",
"password": "12345678"
}
const chooseUser = Math.floor(Math.random() * 3) + 1
if(chooseUser === 2){
it('should respond with an object', (done) => {
chai.request(server)
.post('/users/login')
.send(wrongPasswordUser)
.end((err, res) => {
res.body.should.be.a('object')
done()
})
})
}
else if(chooseUser === 3){
it('should respond with an object', (done) => {
chai.request(server)
.post('/users/login')
.send(nonExistantUser)
.end((err, res) => {
res.body.should.be.a('object')
done()
})
})
}
else{
it('should respond with an object', (done) => {
chai.request(server)
.post('/users/login')
.send(newUser)
.end((err, res) => {
res.body.should.be.a('object')
done()
})
})
}
it('should be an object', (done) => {
it('should return correct message and status on successful login', (done) => {
chai.request(server)
.post('/users/login')
.send(newUser)
.end((err, res) => {
res.body.should.be.a('object')
res.should.have.status(200)
res.body.success.should.equal(true)
res.body.message.should.include("Successful Login!")
done()
})
})
})
it('should return all 15 artworks stored successfully', (done) => {
it('should return correct message and status on a non existant user login', (done) => {
chai.request(server)
.post('/users/login')
.send(newUser)
.send(nonExistantUser)
.end((err, res) => {
res.should.have.status(400)
res.body.success.should.equal(false)
res.body.message.should.include("[email protected] is not registered with us yet!")
done()
})
})
it('should return correct message and status on a wrong password user login', (done) => {
chai.request(server)
.post('/users/login')
.send(wrongPasswordUser)
.end((err, res) => {
res.should.have.status(400)
res.body.success.should.equal(false)
res.body.message.should.include("Wrong Username and/or Password!")
done()
})
})
})
describe('The "/:id" route GET function for getting a single user by ID', () => {
it('should return the user object with successful status', (done) => {
chai.request(server)
.get('/users/3')
.end((err, res) => {
res.should.have.status(200)
res.body.should.be.a("object")
res.body.should.have.property("_id")
res.body._id.should.be.a("number")
res.body.should.have.property("user")
res.body.user.should.be.a("string")

res.body.should.have.property("name")
res.body.name.should.be.a("object")
res.body.name.should.have.property("first")
res.body.name.first.should.be.a("string")
res.body.name.should.have.property("last")
res.body.name.last.should.be.a("string")
res.body.name.should.have.property("full")
res.body.name.full.should.be.a("string")

res.body.should.have.property("products_uploaded")
res.body.products_uploaded.should.be.a("array")
res.body.should.have.property("cart")
res.body.cart.should.be.a("array")
res.body.should.have.property("saved")
res.body.saved.should.be.a("array")

res.body.should.have.property("following")
res.body.following.should.be.a("array")
res.body.should.have.property("followers")
res.body.followers.should.be.a("array")
done()
})
})
it('should not return users password', (done) => {
chai.request(server)
.get('/users/3')
.end((err, res) => {
res.should.have.status(200)
res.body.should.not.have.property("password")
done()
})
})
it('should return correct message and status on a non existant user ID get request', (done) => {
chai.request(server)
.get('/users/6')
.end((err, res) => {
res.body.should.have.status(200)
res.body
res.should.have.status(400)
res.body.should.have.property("success")
res.body.success.should.equal(false)
res.body.should.have.property("message")
res.body.message.should.include("User ID: 6 is not valid!")
done()
})
})
Expand Down
42 changes: 0 additions & 42 deletions front-end/src/SchemaSamples/AllCategories.js

This file was deleted.