diff --git a/server/controller/comments.js b/server/controller/comments.js index 1a4864a..66646f9 100644 --- a/server/controller/comments.js +++ b/server/controller/comments.js @@ -1,25 +1,30 @@ /* eslint-disable eol-last */ // import CommentsModel from '../models/comments'; import QuestionModel from '../models/question'; +import CommentsModel from '../models/comments'; +import { + errorResponse, + successResponse, +} from '../utilities/responseformat'; + const commentsController = { async createComment(req, res) { - const comment = req.body; - const checkquestion = await QuestionModel.getcommentById(comment.commentId); - if (!checkquestion) { - res.status(404).json({ - status: 404, - error: 'comment does not exist', - }); + const newComment = req.body; - comment.title = checkquestion.title; - comment.body = checkquestion.body; - comment.userid = req.user.id; + const checkquestion = await QuestionModel.getQuestionById(newComment.questionId); - const newComment = new CommentsModel(comment); - const createdComment = await CommentsModel.createComment(newComment); + if (!checkquestion) { + return errorResponse(res, 404, 'Question does not exist'); } + + newComment.userId = req.user.id; + newComment.comment = newComment.comment.replace(/[^A-Z0-9]/ig, ''); + + const latestComment = new CommentsModel(newComment); + const createdComment = await latestComment.createComment(); + return successResponse(res, 201, 'Comment posted', createdComment); }, }; diff --git a/server/controller/questions.js b/server/controller/questions.js index 8dc597b..beee565 100644 --- a/server/controller/questions.js +++ b/server/controller/questions.js @@ -11,11 +11,11 @@ export default { const { id, } = req.user; - req.body.userid = id; + req.body.userId = id; const question = new Question(req.body); - const meetupExists = await Meetup.retrieveSingleMeetup(question.meetupid); + const meetupExists = await Meetup.retrieveSingleMeetup(question.meetupId); if (!meetupExists) { return errorResponse(res, 404, 'Meetup not found'); } diff --git a/server/controller/user.js b/server/controller/user.js index dc45b89..4f13aec 100644 --- a/server/controller/user.js +++ b/server/controller/user.js @@ -29,7 +29,7 @@ const UserController = { const createdUser = await newUser.newUserSignUp(); - return successResponse(res, 201, 'User Created', createdUser); + return successResponse(res, 201, 'You have created an account', createdUser); }, async loginUser(req, res) { diff --git a/server/db/migrationsCreateTables.js b/server/db/migrationsCreateTables.js index 9aa6231..0999b03 100644 --- a/server/db/migrationsCreateTables.js +++ b/server/db/migrationsCreateTables.js @@ -13,14 +13,14 @@ console.log('Creating tables...'); console.log('Creating users table...'); await pool.query(`CREATE TABLE IF NOT EXISTS users( id SERIAL PRIMARY KEY, - firstname VARCHAR(50) NOT NULL, - lastname VARCHAR(50) NOT NULL, - username VARCHAR(50) UNIQUE NOT NULL, + first_name VARCHAR(50) NOT NULL, + last_name VARCHAR(50) NOT NULL, + user_name VARCHAR(50) UNIQUE NOT NULL, email VARCHAR(100) UNIQUE NOT NULL, - phonenumber VARCHAR(11) NOT NULL, + phone_number VARCHAR(11) NOT NULL, password VARCHAR(100) NOT NULL, admin BOOLEAN DEFAULT FALSE, - createdon TIMESTAMPTZ DEFAULT NOW())`); + created_on TIMESTAMPTZ DEFAULT NOW())`); const password = bcrypt.hashPassword(process.env.ADMIN_PASSWORD); const Admin = { @@ -32,8 +32,8 @@ console.log('Creating tables...'); phonenumber: '08135266484', }; - const queryPlaceholder = `INSERT INTO users (firstname, lastname, username, email, password, phonenumber) - VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, firstname, lastname, username, email, phonenumber`; + const queryPlaceholder = `INSERT INTO users (first_name, last_name, user_name, email, password, phone_number) + VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, first_name, last_name, user_name, email, phone_number`; const values = [Admin.firstname, Admin.lastname, Admin.username, Admin.email, Admin.password, Admin.phonenumber, ]; @@ -50,53 +50,51 @@ console.log('Creating tables...'); id SERIAL PRIMARY KEY, topic VARCHAR(255) NOT NULL, location TEXT NOT NULL, - happeningon VARCHAR(50) NOT NULL, + happening_on VARCHAR(50) NOT NULL, image VARCHAR(50), - createdon TIMESTAMPTZ DEFAULT NOW())`); + created_on TIMESTAMPTZ DEFAULT NOW())`); console.log('Creating questions table...'); await pool.query(`CREATE TABLE IF NOT EXISTS questions( id SERIAL PRIMARY KEY, - meetupid INT NOT NULL, + meetup_id INT NOT NULL, title VARCHAR(255) NOT NULL, body TEXT NOT NULL, - upvotes INT DEFAULT 0, - downvotes INT DEFAULT 0, - userid INT NOT NULL, - createdon TIMESTAMPTZ DEFAULT NOW(), - FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE, - FOREIGN KEY (meetupid) REFERENCES meetups (id) ON DELETE CASCADE)`); + up_votes INT DEFAULT 0, + down_votes INT DEFAULT 0, + user_id INT NOT NULL, + created_on TIMESTAMPTZ DEFAULT NOW(), + FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, + FOREIGN KEY (meetup_id) REFERENCES meetups (id) ON DELETE CASCADE)`); console.log('Creating rsvps table...'); await pool.query(`CREATE TABLE IF NOT EXISTS rsvps( id SERIAL, - meetupid INT NOT NULL, - userid INT NOT NULL, + meetup_id INT NOT NULL, + user_id INT NOT NULL, response VARCHAR(5) NOT NULL, - PRIMARY KEY(meetupid, userid), - FOREIGN KEY (meetupid) REFERENCES meetups (id) ON DELETE CASCADE, - FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE)`); + PRIMARY KEY(meetup_id, user_id), + FOREIGN KEY (meetup_id) REFERENCES meetups (id) ON DELETE CASCADE, + FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE)`); console.log('Creating comments table...'); await pool.query(`CREATE TABLE IF NOT EXISTS comments( id SERIAL PRIMARY KEY, - meetupid INT NOT NULL, - questionid INT NOT NULL, - body TEXT NOT NULL, - userid INT NOT NULL, - createdon TIMESTAMPTZ DEFAULT NOW(), - FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE, - FOREIGN KEY (questionid) REFERENCES questions (id) ON DELETE CASCADE, - FOREIGN KEY (meetupid) REFERENCES meetups (id) ON DELETE CASCADE)`); + question_id INT NOT NULL, + comment TEXT NOT NULL, + user_id INT NOT NULL, + created_on TIMESTAMPTZ DEFAULT NOW(), + FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, + FOREIGN KEY (question_id) REFERENCES questions (id) ON DELETE CASCADE)`); console.log('Creating votes table...'); await pool.query(`CREATE TABLE IF NOT EXISTS votes( id SERIAL PRIMARY KEY, - userid INT NOT NULL, - questionid INT NOT NULL, + user_id INT NOT NULL, + question_id INT NOT NULL, vote VARCHAR(8) NOT NULL, - FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE, - FOREIGN KEY (questionid) REFERENCES questions (id) ON DELETE CASCADE)`); + FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, + FOREIGN KEY (question_id) REFERENCES questions (id) ON DELETE CASCADE)`); } catch (error) { console.log(error); } diff --git a/server/db/migrationsDropTables.js b/server/db/migrationsDropTables.js index 872946e..4af9ea9 100644 --- a/server/db/migrationsDropTables.js +++ b/server/db/migrationsDropTables.js @@ -1,3 +1,4 @@ +/* eslint-disable eol-last */ import pool from './index'; console.log('Dropping tables...'); diff --git a/server/middleware/validatecomment.js b/server/middleware/validatecomment.js new file mode 100644 index 0000000..0ef67bd --- /dev/null +++ b/server/middleware/validatecomment.js @@ -0,0 +1,25 @@ +/* eslint-disable eol-last */ +import Validator from 'validatorjs'; +import { + errorResponse, +} from '../utilities/responseformat'; +import customErrorMessages from '../utilities/errorresponses'; + + +export default class CommentValidation { + static validComment(req, res, next) { + const comments = req.body; + + const commentProperties = { + questionId: 'required|numeric', + comment: 'required|string|max:500', + }; + + const validator = new Validator(comments, commentProperties, customErrorMessages); + validator.passes(() => next()); + validator.fails(() => { + const errors = validator.errors.all(); + return errorResponse(res, 400, errors); + }); + } +} \ No newline at end of file diff --git a/server/middleware/validateid.js b/server/middleware/validateid.js new file mode 100644 index 0000000..4addbd1 --- /dev/null +++ b/server/middleware/validateid.js @@ -0,0 +1,17 @@ +/* eslint-disable eol-last */ +import { + errorResponse, +} from '../utilities/responseformat'; + +export default (req, res, next) => { + const { + id, + } = req.params; + const newId = parseInt(id, 10); + + if (!newId) { + return errorResponse(res, 422, 'Invalid id.'); + } + req.params.id = newId; + return next(); +}; \ No newline at end of file diff --git a/server/middleware/validatemeetups.js b/server/middleware/validatemeetups.js index 84a8d89..5d1ad24 100644 --- a/server/middleware/validatemeetups.js +++ b/server/middleware/validatemeetups.js @@ -12,7 +12,7 @@ export default class MeetupValidation { const meetupProperties = { topic: 'required|string|min:1|max:255', location: 'required|string|min:1', - happeningon: ['required', 'date', 'regex:/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}/'], + happeningOn: ['required', 'date', 'regex:/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}/'], images: 'url', }; @@ -26,10 +26,10 @@ export default class MeetupValidation { static checkDate(req, res, next) { const { - happeningon, + happeningOn, } = req.body; const currentDate = new Date(Date.now()); - const meetupDate = new Date(happeningon); + const meetupDate = new Date(happeningOn); if (currentDate > meetupDate) { return errorResponse(res, 400, `Your Date should be greater than ${currentDate}.`); diff --git a/server/middleware/validatequestion.js b/server/middleware/validatequestion.js index c158ecd..5a1bb16 100644 --- a/server/middleware/validatequestion.js +++ b/server/middleware/validatequestion.js @@ -10,7 +10,7 @@ export default class QuestionValidation { const question = req.body; const questionProperties = { - meetupid: 'required|numeric', + meetupId: 'required|numeric', title: 'required|string|max:200', body: 'required|string|max:500', }; diff --git a/server/models/admin.js b/server/models/admin.js deleted file mode 100644 index b20f7ed..0000000 --- a/server/models/admin.js +++ /dev/null @@ -1,46 +0,0 @@ -/* eslint-disable eol-last */ -import pool from '../db/index'; - -class AdminModel { - constructor(user) { - this.firstname = user.firstname; - this.lastname = user.lastname; - this.username = user.username; - this.email = user.email; - this.password = user.password; - this.phonenumber = user.phonenumber; - } - - async newAdminSignUp() { - const queryPlaceholder = `INSERT INTO admin (firstname, lastname, - username, email, password, phonenumber) - VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, firstname, lastname, username, email, phonenumber`; - const values = [this.firstname, this.lastname, this.username, - this.email, this.password, this.phonenumber, - ]; - const { - rows, - } = await pool.query(queryPlaceholder, values); - return rows[0]; - } - - static async findAdminByEmail(email) { - const queryPlaceholder = 'SELECT * FROM admin WHERE email = $1'; - const values = [email]; - const { - rows, - } = await pool.query(queryPlaceholder, values); - return rows[0]; - } - - static async findAdminByUsername(username) { - const queryPlaceholder = 'SELECT * FROM admin WHERE username = $1'; - const values = [username]; - const { - rows, - } = await pool.query(queryPlaceholder, values); - return rows[0]; - } -} - -export default AdminModel; \ No newline at end of file diff --git a/server/models/comments.js b/server/models/comments.js index c9e3086..5d2fee4 100644 --- a/server/models/comments.js +++ b/server/models/comments.js @@ -1,30 +1,29 @@ -// import pool from '../db/index'; +/* eslint-disable eol-last */ +import pool from '../db/index'; -// export default class Comment { -// constructor(usercomment) { -// this.questionid = usercomment.questionid; -// this.title = usercomment.title; -// this.body = usercomment.body; -// this.comment = usercomment.comment; -// this.userid = usercomment.userid; -// } +export default class Comment { + constructor(userComment) { + this.questionId = userComment.questionId; + this.comment = userComment.comment; + this.userId = userComment.userId; + } -// async createComment() { -// const queryPlaceholder = `INSERT INTO comments (questionid, title, body, comment, userid) -// queryValues ($1, $2, $3, $4, $5) RETURNING *`; -// const queryValues = [this.questionid, this.title, this.body, this.comment, this.userid]; -// const { -// rows, -// } = await pool.query(queryPlaceholder, queryValues); -// return rows[0]; -// } + async createComment() { + const queryPlaceholder = `INSERT INTO comments (question_id, comment, user_id) + VALUES ($1, $2, $3) RETURNING *`; + const queryValues = [this.questionId, this.comment, this.userId]; + const { + rows, + } = await pool.query(queryPlaceholder, queryValues); + return rows[0]; + } -// static async getCommentsByQuestion(id) { -// const queryPlaceholder = 'SELECT * FROM comments WHERE questionid = $1'; -// const queryValues = [id]; -// const { -// rows, -// } = await pool.query(queryPlaceholder, queryValues); -// return rows; -// } -// } \ No newline at end of file + static async getCommentsByQuestion(id) { + const queryPlaceholder = 'SELECT * FROM comments WHERE questionid = $1'; + const queryValues = [id]; + const { + rows, + } = await pool.query(queryPlaceholder, queryValues); + return rows; + } +} \ No newline at end of file diff --git a/server/models/meetup.js b/server/models/meetup.js index a207c85..83a131f 100644 --- a/server/models/meetup.js +++ b/server/models/meetup.js @@ -6,14 +6,14 @@ class Meetup { constructor(meetup) { this.topic = meetup.topic; this.location = meetup.location; - this.happeningon = meetup.happeningon; + this.happeningOn = meetup.happeningOn; this.image = meetup.image; } async createMeetup() { - const queryPlaceholder = `INSERT INTO meetups (topic, location, happeningon, + const queryPlaceholder = `INSERT INTO meetups (topic, location, happening_on, image) VALUES ($1, $2, $3, $4) RETURNING *`; - const entryValues = [this.topic, this.location, this.happeningon, + const entryValues = [this.topic, this.location, this.happeningOn, this.image, ]; const { @@ -40,7 +40,7 @@ class Meetup { } static async retrieveUpcomingMeetups(currentDate) { - const queryPlaceholder = 'SELECT * FROM meetups WHERE happeningon > $1 ORDER BY happeningon'; + const queryPlaceholder = 'SELECT * FROM meetups WHERE happening_on > $1 ORDER BY happening_on'; const queryValues = [currentDate]; const { rows, diff --git a/server/models/question.js b/server/models/question.js index 349f144..a8adeed 100644 --- a/server/models/question.js +++ b/server/models/question.js @@ -3,16 +3,16 @@ import pool from '../db/index'; class Question { constructor(question) { - this.meetupid = question.meetupid; + this.meetupId = question.meetupId; this.title = question.title; this.body = question.body; - this.userid = question.userid; + this.userId = question.userId; } async postQuestion() { - const queryString = `INSERT INTO questions (meetupid, title, body, userid) + const queryString = `INSERT INTO questions (meetup_id, title, body, user_id) VALUES ($1, $2, $3, $4) RETURNING *`; - const values = [this.meetupid, this.title, this.body, this.userid]; + const values = [this.meetupId, this.title, this.body, this.userId]; const { rows, } = await pool.query(queryString, values); @@ -20,6 +20,7 @@ class Question { } static async getQuestionById(id) { + console.log('Hitting here'); const queryPlaceholder = 'SELECT * FROM questions WHERE id = $1'; const queryValues = [id]; const { @@ -46,17 +47,17 @@ class Question { return rows[0]; } - static async updateVotesTable(userid, questionid, vote) { - const queryPlaceholder = `INSERT INTO votes (userid, questionid, vote) queryValues + static async updateVotesTable(userId, questionId, vote) { + const queryPlaceholder = `INSERT INTO votes (user_id, question_id, vote) VALUES ($1, $2, $3)`; - const queryValues = [userid, questionid, vote]; + const queryValues = [userId, questionId, vote]; const result = await pool.query(queryPlaceholder, queryValues); return result; } - static async ifVoted(userid, questionid) { - const queryPlaceholder = 'SELECT * FROM votes WHERE userid = $1 AND questionid = $2'; - const queryValues = [userid, questionid]; + static async ifVoted(userId, questionId) { + const queryPlaceholder = 'SELECT * FROM votes WHERE user_id = $1 AND question_id = $2'; + const queryValues = [userId, questionId]; const { rows, } = await pool.query(queryPlaceholder, queryValues); diff --git a/server/models/rsvp.js b/server/models/rsvp.js index 3144798..bd2801e 100644 --- a/server/models/rsvp.js +++ b/server/models/rsvp.js @@ -6,7 +6,7 @@ export default class Rsvp { } async responseToMeetup(meetupId, userId) { - const queryPlaceholder = 'INSERT INTO rsvps (meetupid, userid, response) queryValues ($1, $2, $3) RETURNING *'; + const queryPlaceholder = 'INSERT INTO rsvps (meetup_id, user_id, response) queryValues ($1, $2, $3) RETURNING *'; const queryValues = [meetupId, userId, this.response]; const { rows, @@ -15,7 +15,7 @@ export default class Rsvp { } static async allUserResponse(userId, response) { - const queryPlaceholder = 'SELECT * FROM rsvps WHERE userid =$1 AND response = $2'; + const queryPlaceholder = 'SELECT * FROM rsvps WHERE user_id =$1 AND response = $2'; const queryValues = [userId, response]; const { rows, @@ -24,7 +24,7 @@ export default class Rsvp { } static async getRsvpResponse(meetupId, userId) { - const queryPlaceholder = 'SELECT * FROM rsvps WHERE meetupid = $1 AND userid = $2'; + const queryPlaceholder = 'SELECT * FROM rsvps WHERE meetup_id = $1 AND user_id = $2'; const queryValues = [meetupId, userId]; const { rows, diff --git a/server/models/user.js b/server/models/user.js index 9cccc94..413ca9c 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -12,9 +12,9 @@ class UserModel { } async newUserSignUp() { - const queryPlaceholder = `INSERT INTO users (firstname, lastname, - username, email, password, phonenumber) - VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, firstname, lastname, username, email, phonenumber`; + const queryPlaceholder = `INSERT INTO users (first_name, last_name, + user_name, email, password, phone_number) + VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, first_name, last_name, user_name, email, phone_number`; const values = [this.firstname, this.lastname, this.username, this.email, this.password, this.phonenumber, ]; @@ -34,7 +34,7 @@ class UserModel { } static async findUserByUsername(username) { - const queryPlaceholder = 'SELECT * FROM users WHERE username = $1'; + const queryPlaceholder = 'SELECT * FROM users WHERE user_name = $1'; const values = [username]; const { rows, @@ -43,7 +43,7 @@ class UserModel { } static async logIn(email) { - const queryPlaceholder = 'SELECT id, firstname, lastname, username, email, phonenumber FROM users WHERE email = $1'; + const queryPlaceholder = 'SELECT id, first_name, last_name, user_name, email, phone_number FROM users WHERE email = $1'; const values = [email]; const { rows, diff --git a/server/routes/comments.js b/server/routes/comments.js index e282557..b1656a7 100644 --- a/server/routes/comments.js +++ b/server/routes/comments.js @@ -6,7 +6,8 @@ const router = express.Router(); import Comments from '../controller/comments'; import tryCatch from '../utilities/trycatch'; +import validate from '../middleware/validatecomment'; -// router.post('/auth/signup', tryCatch(Comments.createComment)); +router.post('/', validate.validComment, tryCatch(Comments.createComment)); export default router; \ No newline at end of file diff --git a/server/routes/index.js b/server/routes/index.js index 71b9f9f..8f7f4fc 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -5,12 +5,14 @@ import { import userRoutes from './user'; import meetupRoutes from './meetups'; import questionsRoute from './questions'; -// import commentRoutes from './routes/comments'; +import commentRoutes from './comments'; +import Auth from '../middleware/Auth'; const router = new Router(); router.use('/auth', userRoutes); -router.use('/meetups', meetupRoutes); -router.use('/questions', questionsRoute); +router.use('/meetups', Auth.verifyToken, meetupRoutes); +router.use('/questions', Auth.verifyToken, questionsRoute); +router.use('/comments', Auth.verifyToken, commentRoutes); export default router; \ No newline at end of file diff --git a/server/routes/meetups.js b/server/routes/meetups.js index fb0ba55..1e9a267 100644 --- a/server/routes/meetups.js +++ b/server/routes/meetups.js @@ -12,12 +12,13 @@ import rsvpController from '../controller/rsvp'; import Auth from '../middleware/Auth'; import meetupValidation from '../middleware/validatemeetups'; import tryCatch from '../utilities/trycatch'; +import validateId from '../middleware/validateid'; -router.post('/', Auth.verifyToken, Auth.adminAuth, meetupValidation.validCreateMeetup, meetupValidation.checkDate, tryCatch(meetupController.createMeetup)); -router.get('/upcoming/', Auth.verifyToken, tryCatch(meetupController.getUpcomingMeetups)); -router.get('/:id', Auth.verifyToken, tryCatch(meetupController.getSingleMeetup)); -router.get('/', Auth.verifyToken, tryCatch(meetupController.getAllMeetups)); -router.patch('/:meetupId/rsvps', Auth.verifyToken, tryCatch(rsvpController.respondToRsvp)); -router.delete('/:id', Auth.verifyToken, Auth.adminAuth, tryCatch(meetupController.deleteMeetup)); +router.post('/', Auth.adminAuth, meetupValidation.validCreateMeetup, meetupValidation.checkDate, tryCatch(meetupController.createMeetup)); +router.get('/upcoming/', tryCatch(meetupController.getUpcomingMeetups)); +router.get('/:id', validateId, tryCatch(meetupController.getSingleMeetup)); +router.get('/', tryCatch(meetupController.getAllMeetups)); +router.patch('/:meetupId/rsvps', validateId, tryCatch(rsvpController.respondToRsvp)); +router.delete('/:id', Auth.adminAuth, validateId, tryCatch(meetupController.deleteMeetup)); export default router; \ No newline at end of file diff --git a/server/routes/questions.js b/server/routes/questions.js index 0d35fc1..233715b 100644 --- a/server/routes/questions.js +++ b/server/routes/questions.js @@ -6,12 +6,11 @@ const router = express.Router(); import questionsController from '../controller/questions'; // import voteController from '../controller/vote'; -import Auth from '../middleware/Auth'; import validate from '../middleware/validatequestion'; import tryCatch from '../utilities/trycatch'; -router.post('/', Auth.verifyToken, validate.validateQuestion, tryCatch(questionsController.createQuestion)); +router.post('/', validate.validateQuestion, tryCatch(questionsController.createQuestion)); // router.patch('/:questionId/upvote', Auth.verifyToken, voteController.upvote); // router.patch('/:questionId/downvote', Auth.verifyToken, voteController.downvote); diff --git a/server/test/testData.js b/server/test/testData.js index 7a0d6fe..eb59fa7 100644 --- a/server/test/testData.js +++ b/server/test/testData.js @@ -32,17 +32,22 @@ const invalidUser = { const createMeetup = { topic: 'Montypoint tech meetup', location: 'Yaba, Lagos state', - happeningon: '2025-01-23T22:00', + happeningOn: '2025-01-23T22:00', image: 'heroku.com', }; const askQuestion = { - meetupid: '1', + meetupId: '1', title: 'title of question', body: 'body of question', }; +const postComment = { + questionId: '1', + comment: 'comment', +}; + export { loginAdmin, createUser, @@ -50,4 +55,5 @@ export { invalidUser, createMeetup, askQuestion, + postComment, }; \ No newline at end of file diff --git a/server/test/users.spec.js b/server/test/users.spec.js index ee48712..dcadb32 100644 --- a/server/test/users.spec.js +++ b/server/test/users.spec.js @@ -15,6 +15,7 @@ import { userLogin, invalidUser, askQuestion, + postComment, } from './testData'; should(); @@ -32,7 +33,7 @@ describe('Create a user', async () => { .end((err, res) => { if (err) return done(err); const userid = res.body.data.id; - const userFirstname = res.body.data.firstname; + const userFirstname = res.body.data.first_name; const arrayProp = res.body.data; expect(res.body.status).to.equal(201); expect(userid).to.equal(2); @@ -97,4 +98,23 @@ describe('Create ask questions', async () => { return done(); }); }); +}); + +describe('Create post comments', async () => { + it('A user should be able to comment on a question', (done) => { + request(server) + .post('/api/v1/comments') + .send(postComment) + .set('Accept', 'application/json') + .set({ + Authorization: `Bearer ${userToken}`, + }) + .expect('Content-Type', /json/) + .expect(201) + .end((err, res) => { + if (err) return done(err); + expect(res.body.status).to.equal(201); + return done(); + }); + }); }); \ No newline at end of file