From c6c99f43cdd7dd688f624e281658bebb54f3fccf Mon Sep 17 00:00:00 2001 From: Kader1680 Date: Tue, 8 Jul 2025 16:14:12 +0100 Subject: [PATCH] start test authentication --- server/.env | 5 ---- server/.env.example | 5 ++++ server/app.js | 10 ++++--- server/controller/auth.js | 1 + server/package.json | 6 ++-- server/tests/auth.test.js | 61 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 77 insertions(+), 11 deletions(-) delete mode 100644 server/.env create mode 100644 server/.env.example create mode 100644 server/tests/auth.test.js diff --git a/server/.env b/server/.env deleted file mode 100644 index 7429709e..00000000 --- a/server/.env +++ /dev/null @@ -1,5 +0,0 @@ -DATABASE=mongodb://127.0.0.1:27017/ecommerce -PORT=8000 -BRAINTREE_MERCHANT_ID=n74dc2kw9g3ws389 -BRAINTREE_PUBLIC_KEY=bgytmgzhz5f6t2tg -BRAINTREE_PRIVATE_KEY=e6f226166da99d874f00008f0bba14fe diff --git a/server/.env.example b/server/.env.example new file mode 100644 index 00000000..6c2d0e7d --- /dev/null +++ b/server/.env.example @@ -0,0 +1,5 @@ +DATABASE= +PORT= +BRAINTREE_MERCHANT_ID= +BRAINTREE_PUBLIC_KEY= +BRAINTREE_PRIVATE_KEY= diff --git a/server/app.js b/server/app.js index b93cb3bf..07a6bb8b 100644 --- a/server/app.js +++ b/server/app.js @@ -71,7 +71,9 @@ app.use("/api/order", orderRouter); app.use("/api/customize", customizeRouter); // Run Server -const PORT = process.env.PORT || 8000; -app.listen(PORT, () => { - console.log("Server is running on ", PORT); -}); +// const PORT = process.env.PORT || 8000; +// app.listen(PORT, () => { +// console.log("Server is running on ", PORT); +// }); + +module.exports = app; diff --git a/server/controller/auth.js b/server/controller/auth.js index 2957e852..9920d65d 100644 --- a/server/controller/auth.js +++ b/server/controller/auth.js @@ -28,6 +28,7 @@ class Auth { async postSignup(req, res) { let { name, email, password, cPassword } = req.body; let error = {}; + if (!name || !email || !password || !cPassword) { error = { ...error, diff --git a/server/package.json b/server/package.json index 79b214ac..3c51e920 100644 --- a/server/package.json +++ b/server/package.json @@ -4,7 +4,7 @@ "description": "A E-commerce website.", "main": "app.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", + "test": "jest", "start:dev": "nodemon app.js" }, "repository": { @@ -25,11 +25,13 @@ "dotenv": "^8.2.0", "express": "^4.17.1", "i": "^0.3.6", + "jest": "^30.0.4", "jsonwebtoken": "^8.5.1", "mongoose": "^5.9.27", "morgan": "^1.10.0", "multer": "^1.4.2", "nodemon": "^2.0.4", - "npm": "^6.14.8" + "npm": "^6.14.8", + "supertest": "^7.1.3" } } diff --git a/server/tests/auth.test.js b/server/tests/auth.test.js new file mode 100644 index 00000000..e5e0f7d3 --- /dev/null +++ b/server/tests/auth.test.js @@ -0,0 +1,61 @@ +const request = require("supertest") +const app = require("../app") +const userModel = require("../models/users"); + +describe('test Authentication', () => { + it('register user successfully ', async () => { + const res = await request(app) + .post('/api/signup') + .send ( + { + "name" : "testing", + "email" : "testing@gmail.com", + "password" : "testingAZA", + "cPassword" : "testingAZA", + } + ) + expect(res.statusCode).toBe(200); + }); + + + it('login user successfully ', async () => { + const user = await userModel.create({ + name: "loginTest", + email: "testing@gmail.com", + password: "testing", + userRole: 0 + }); + const res = await request(app) + .post('/api/signin') + .send({ + email: user.email, + password: "testing" + }); + expect(res.statusCode).toBe(200); + expect(res.body).toHaveProperty('token'); + }); + + + it('isadmin', async () => { + const user = await userModel.create({ + name: "adminUser", + email: "admin@example.com", + password: "123456344", + userRole: 1 + }); + const res = await request(app) + .post('/api/isadmin') + .send({ loggedInUserId: user._id }); + + expect(res.statusCode).toBe(200); + expect(res.body).toHaveProperty('role'); + expect(res.body.role).toBe(1); + }, 10000); + + + + + + + +});