diff --git a/back-end/Routes/users.js b/back-end/Routes/users.js index de97967..a331861 100644 --- a/back-end/Routes/users.js +++ b/back-end/Routes/users.js @@ -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: { @@ -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, @@ -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) diff --git a/back-end/Test/artworks.test.js b/back-end/Test/artworks.test.js index eab07af..be7577e 100644 --- a/back-end/Test/artworks.test.js +++ b/back-end/Test/artworks.test.js @@ -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() }) }) diff --git a/back-end/Test/users.test.js b/back-end/Test/users.test.js index 05400a5..303b9e8 100644 --- a/back-end/Test/users.test.js +++ b/back-end/Test/users.test.js @@ -13,8 +13,8 @@ describe('The "/users" route', () => { "_id": 5, "user": "Artist", "name": { - "first": "Anh", - "last": "Tran" + "first": "Random", + "last": "Userrr" }, "email": "artist5@artist.com", "password": "123456", @@ -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": "artist5@artist.com", - "password": "123456", - "products_uploaded": [], - "cart": [], - "saved": [], - "following" : [], - "followers" : [] + "email": "artist1@artist.com", + "password": "123456" + } + const nonExistantUser = { + "email": "artist6@artist.com", + "password": "123456" + } + const wrongPasswordUser = { + "email": "artist1@artist.com", + "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("artist6@artist.com 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() }) }) diff --git a/front-end/src/SchemaSamples/AllCategories.js b/front-end/src/SchemaSamples/AllCategories.js deleted file mode 100644 index fabe13f..0000000 --- a/front-end/src/SchemaSamples/AllCategories.js +++ /dev/null @@ -1,42 +0,0 @@ -export default [ - { - "_id": 1, - "name": "Modern", - "products_id": [ - 1, - 3, - 9, - 11, - 15 - ] - }, - { - "_id": 2, - "name": "Classic", - "products_id": [ - 2, - 3, - 4, - 5, - 6, - 9, - 10, - 12, - 14 - ] - }, - { - "_id": 3, - "name": "Contemporary", - "products_id": [ - 1, - 5, - 6, - 7, - 8, - 12, - 13, - 14 - ] - } -] \ No newline at end of file