From 14c075bfabb2d7dca69ec2088339e29e9fb86c04 Mon Sep 17 00:00:00 2001 From: Job Dufitumukiza Date: Sun, 20 Oct 2024 21:28:06 +0300 Subject: [PATCH 1/2] Implemented a new test suite to cover for the anonymous feature, instrustor-approved and estimatedReadingPost. they are all under test/newImprementedFeature file path --- .../annonymousFeature.js | 97 +++++++++++++++++++ .../estimatedTimeForReadingPost.js | 45 +++++++++ .../instructorApproved.js | 96 ++++++++++++++++++ 3 files changed, 238 insertions(+) create mode 100644 test/newImplementedFeatures/annonymousFeature.js create mode 100644 test/newImplementedFeatures/estimatedTimeForReadingPost.js create mode 100644 test/newImplementedFeatures/instructorApproved.js diff --git a/test/newImplementedFeatures/annonymousFeature.js b/test/newImplementedFeatures/annonymousFeature.js new file mode 100644 index 0000000000..9675c1b5ea --- /dev/null +++ b/test/newImplementedFeatures/annonymousFeature.js @@ -0,0 +1,97 @@ +'use strict'; + +const { JSDOM } = require('jsdom'); +const { expect } = require('chai'); +const request = require('supertest'); +const app = require('../../app'); +const user = require('../../src/user'); +const topics = require('../../src/topics'); +const posts = require('../../src/posts'); + +describe('Anonymous Feature - Share Post Anonymously', () => { + let studentToken; + let postId; + let topicId; + + before(async () => { + // Create a student user + const student = await user.create({ username: 'student', password: 'password' }); + + // Log in as student to get token + studentToken = await loginUser('student', 'password'); + + // Create a topic and a post + const topic = await topics.post({ + title: 'Test Topic', + content: 'Test Content', + uid: student.uid, + cid: 1, + }); + topicId = topic.tid; + const post = await posts.create({ + content: 'Test Post', + uid: student.uid, + tid: topic.tid, + annonymousType: 'student', + }); + postId = post.pid; + }); + + async function loginUser(username, password) { + const res = await request(app) + .post('/api/v1/login') + .send({ username, password }); + return res.body.token; + } + + describe('UI Elements', () => { + it('should display the anonymous type dropdown', () => { + const dom = new JSDOM(` +
+ + +
+ `); + const document = dom.window.document; + const dropdown = document.getElementById('anonymousDropdown'); + expect(dropdown).to.not.be.null; + expect(dropdown.options.length).to.equal(3); + }); + }); + + describe('Form Submission', () => { + it('should include the anonymous type in the form submission', async () => { + const res = await request(app) + .post('/api/v1/topics') + .set('Authorization', `Bearer ${studentToken}`) + .send({ + title: 'Anonymous Post', + content: 'This is an anonymous post.', + cid: 1, + annonymousType: 'student', + }) + .expect(200); + + expect(res.body.topic.annonymousType).to.equal('student'); + }); + }); + + describe('Backend Handling', () => { + it('should display the post anonymously based on the selected type', async () => { + const res = await request(app) + .get(`/api/v1/posts/${postId}`) + .expect(200); + + const post = res.body.post; + if (post.annonymousType === 'none') { + expect(post.user.username).to.not.be.null; + } else { + expect(post.user.username).to.be.null; + } + }); + }); +}); \ No newline at end of file diff --git a/test/newImplementedFeatures/estimatedTimeForReadingPost.js b/test/newImplementedFeatures/estimatedTimeForReadingPost.js new file mode 100644 index 0000000000..69d615191c --- /dev/null +++ b/test/newImplementedFeatures/estimatedTimeForReadingPost.js @@ -0,0 +1,45 @@ +'use strict'; + +const { JSDOM } = require('jsdom'); +const { expect } = require('chai'); + +describe('Estimated Time for Reading Post', () => { + let document; + + beforeEach(() => { + const dom = new JSDOM(` +
This is a test post content with a few words.
+ + `); + document = dom.window.document; + }); + + it('should calculate the word count correctly', () => { + const postContentElement = document.getElementById('post-content-1'); + const postContent = postContentElement.textContent; + const wordCount = postContent.trim().split(/\s+/).length; + + expect(wordCount).to.equal(9); // Adjust the expected word count based on the content + }); + + it('should estimate the reading time correctly', () => { + const wordCount = 9; // Use the word count from the previous test + const wordsPerMinute = 200; + const readingTime = Math.ceil(wordCount / wordsPerMinute); + + expect(readingTime).to.equal(1); // Adjust the expected reading time based on the word count + }); + + it('should display the estimated reading time in the DOM', () => { + const postContentElement = document.getElementById('post-content-1'); + const readingTimeElement = document.getElementById('reading-time-1'); + + const postContent = postContentElement.textContent; + const wordCount = postContent.trim().split(/\s+/).length; + const readingTime = Math.ceil(wordCount / 200); + + readingTimeElement.textContent = 'Estimated reading time: ' + readingTime + ' min'; + + expect(readingTimeElement.textContent).to.equal('Estimated reading time: 1 min'); // Adjust the expected text based on the reading time + }); +}); \ No newline at end of file diff --git a/test/newImplementedFeatures/instructorApproved.js b/test/newImplementedFeatures/instructorApproved.js new file mode 100644 index 0000000000..531798962c --- /dev/null +++ b/test/newImplementedFeatures/instructorApproved.js @@ -0,0 +1,96 @@ +'use strict'; + +const assert = require('assert'); +const request = require('supertest'); +const app = require('../../app'); +const db = require('../../src/database'); +const user = require('../../src/user'); +const topics = require('../../src/topics'); +const posts = require('../../src/posts'); +const categories = require('../../src/categories'); + +describe('Instructor Approved Posts', () => { + let instructorToken; + let studentToken; + let postId; + let topicId; + + before(async () => { + // Create an instructor user and a student user + const instructor = await user.create({ username: 'instructor', password: 'password', isAdmin: true }); + const student = await user.create({ username: 'student', password: 'password' }); + + // Log in as instructor and student to get tokens + instructorToken = await loginUser('instructor', 'password'); + studentToken = await loginUser('student', 'password'); + + // Create a category, topic, and a post + const category = await categories.create({ + name: 'Test Category', + description: 'Test category created by testing script', + }); + const topic = await topics.post({ + title: 'Test Topic', + content: 'Test Content', + uid: student.uid, + cid: category.cid, + }); + topicId = topic.tid; + const post = await posts.create({ + content: 'Test Post', + uid: student.uid, + tid: topic.tid, + }); + postId = post.pid; + }); + + async function loginUser(username, password) { + const res = await request(app) + .post('/api/v1/login') + .send({ username, password }); + return res.body.token; + } + + describe('PUT /api/v1/posts/:pid/approve', () => { + it('should allow an instructor to approve a post', async () => { + const res = await request(app) + .put(`/api/v1/posts/${postId}/approve`) + .set('Authorization', `Bearer ${instructorToken}`) + .expect(200); + + assert.strictEqual(res.body.message, 'Post approved successfully'); + + // Verify the post is marked as approved in the database + const post = await posts.getPost(postId); + assert.strictEqual(post.isApproved, true); + }); + + it('should not allow a student to approve a post', async () => { + const res = await request(app) + .put(`/api/v1/posts/${postId}/approve`) + .set('Authorization', `Bearer ${studentToken}`) + .expect(403); + + assert.strictEqual(res.body.error, 'You do not have permission to approve posts'); + }); + + it('should return an error if the post does not exist', async () => { + const res = await request(app) + .put('/api/v1/posts/99999/approve') + .set('Authorization', `Bearer ${instructorToken}`) + .expect(404); + + assert.strictEqual(res.body.error, 'Post not found'); + }); + }); + + describe('GET /api/v1/posts/:pid', () => { + it('should retrieve the post with approval status', async () => { + const res = await request(app) + .get(`/api/v1/posts/${postId}`) + .expect(200); + + assert.strictEqual(res.body.post.isApproved, true); + }); + }); +}); \ No newline at end of file From 3eddc7faa8b046091d954da44487cb767bdc2db6 Mon Sep 17 00:00:00 2001 From: Job Dufitumukiza Date: Sun, 20 Oct 2024 22:02:15 +0300 Subject: [PATCH 2/2] fixed lint test for new features --- .../annonymousFeature.js | 136 ++++++++--------- .../estimatedTimeForReadingPost.js | 54 +++---- .../instructorApproved.js | 144 +++++++++--------- 3 files changed, 167 insertions(+), 167 deletions(-) diff --git a/test/newImplementedFeatures/annonymousFeature.js b/test/newImplementedFeatures/annonymousFeature.js index 9675c1b5ea..c8af957376 100644 --- a/test/newImplementedFeatures/annonymousFeature.js +++ b/test/newImplementedFeatures/annonymousFeature.js @@ -9,44 +9,44 @@ const topics = require('../../src/topics'); const posts = require('../../src/posts'); describe('Anonymous Feature - Share Post Anonymously', () => { - let studentToken; - let postId; - let topicId; + let studentToken; + let postId; + let topicId; - before(async () => { - // Create a student user - const student = await user.create({ username: 'student', password: 'password' }); + before(async () => { + // Create a student user + const student = await user.create({ username: 'student', password: 'password' }); - // Log in as student to get token - studentToken = await loginUser('student', 'password'); + // Log in as student to get token + studentToken = await loginUser('student', 'password'); - // Create a topic and a post - const topic = await topics.post({ - title: 'Test Topic', - content: 'Test Content', - uid: student.uid, - cid: 1, - }); - topicId = topic.tid; - const post = await posts.create({ - content: 'Test Post', - uid: student.uid, - tid: topic.tid, - annonymousType: 'student', - }); - postId = post.pid; - }); + // Create a topic and a post + const topic = await topics.post({ + title: 'Test Topic', + content: 'Test Content', + uid: student.uid, + cid: 1, + }); + topicId = topic.tid; + const post = await posts.create({ + content: 'Test Post', + uid: student.uid, + tid: topic.tid, + annonymousType: 'student', + }); + postId = post.pid; + }); - async function loginUser(username, password) { - const res = await request(app) - .post('/api/v1/login') - .send({ username, password }); - return res.body.token; - } + async function loginUser(username, password) { + const res = await request(app) + .post('/api/v1/login') + .send({ username, password }); + return res.body.token; + } - describe('UI Elements', () => { - it('should display the anonymous type dropdown', () => { - const dom = new JSDOM(` + describe('UI Elements', () => { + it('should display the anonymous type dropdown', () => { + const dom = new JSDOM(`
`); - const document = dom.window.document; - const dropdown = document.getElementById('anonymousDropdown'); - expect(dropdown).to.not.be.null; - expect(dropdown.options.length).to.equal(3); - }); - }); + const { document } = dom.window; + const dropdown = document.getElementById('anonymousDropdown'); + expect(dropdown).to.not.be.null; + expect(dropdown.options.length).to.equal(3); + }); + }); - describe('Form Submission', () => { - it('should include the anonymous type in the form submission', async () => { - const res = await request(app) - .post('/api/v1/topics') - .set('Authorization', `Bearer ${studentToken}`) - .send({ - title: 'Anonymous Post', - content: 'This is an anonymous post.', - cid: 1, - annonymousType: 'student', - }) - .expect(200); + describe('Form Submission', () => { + it('should include the anonymous type in the form submission', async () => { + const res = await request(app) + .post('/api/v1/topics') + .set('Authorization', `Bearer ${studentToken}`) + .send({ + title: 'Anonymous Post', + content: 'This is an anonymous post.', + cid: 1, + annonymousType: 'student', + }) + .expect(200); - expect(res.body.topic.annonymousType).to.equal('student'); - }); - }); + expect(res.body.topic.annonymousType).to.equal('student'); + }); + }); - describe('Backend Handling', () => { - it('should display the post anonymously based on the selected type', async () => { - const res = await request(app) - .get(`/api/v1/posts/${postId}`) - .expect(200); + describe('Backend Handling', () => { + it('should display the post anonymously based on the selected type', async () => { + const res = await request(app) + .get(`/api/v1/posts/${postId}`) + .expect(200); - const post = res.body.post; - if (post.annonymousType === 'none') { - expect(post.user.username).to.not.be.null; - } else { - expect(post.user.username).to.be.null; - } - }); - }); -}); \ No newline at end of file + const { post } = res.body; + if (post.annonymousType === 'none') { + expect(post.user.username).to.not.be.null; + } else { + expect(post.user.username).to.be.null; + } + }); + }); +}); diff --git a/test/newImplementedFeatures/estimatedTimeForReadingPost.js b/test/newImplementedFeatures/estimatedTimeForReadingPost.js index 69d615191c..aed3ce89cb 100644 --- a/test/newImplementedFeatures/estimatedTimeForReadingPost.js +++ b/test/newImplementedFeatures/estimatedTimeForReadingPost.js @@ -4,42 +4,42 @@ const { JSDOM } = require('jsdom'); const { expect } = require('chai'); describe('Estimated Time for Reading Post', () => { - let document; + let document; - beforeEach(() => { - const dom = new JSDOM(` + beforeEach(() => { + const dom = new JSDOM(`
This is a test post content with a few words.
`); - document = dom.window.document; - }); + document = dom.window.document; + }); - it('should calculate the word count correctly', () => { - const postContentElement = document.getElementById('post-content-1'); - const postContent = postContentElement.textContent; - const wordCount = postContent.trim().split(/\s+/).length; + it('should calculate the word count correctly', () => { + const postContentElement = document.getElementById('post-content-1'); + const postContent = postContentElement.textContent; + const wordCount = postContent.trim().split(/\s+/).length; - expect(wordCount).to.equal(9); // Adjust the expected word count based on the content - }); + expect(wordCount).to.equal(9); // Adjust the expected word count based on the content + }); - it('should estimate the reading time correctly', () => { - const wordCount = 9; // Use the word count from the previous test - const wordsPerMinute = 200; - const readingTime = Math.ceil(wordCount / wordsPerMinute); + it('should estimate the reading time correctly', () => { + const wordCount = 9; // Use the word count from the previous test + const wordsPerMinute = 200; + const readingTime = Math.ceil(wordCount / wordsPerMinute); - expect(readingTime).to.equal(1); // Adjust the expected reading time based on the word count - }); + expect(readingTime).to.equal(1); // Adjust the expected reading time based on the word count + }); - it('should display the estimated reading time in the DOM', () => { - const postContentElement = document.getElementById('post-content-1'); - const readingTimeElement = document.getElementById('reading-time-1'); + it('should display the estimated reading time in the DOM', () => { + const postContentElement = document.getElementById('post-content-1'); + const readingTimeElement = document.getElementById('reading-time-1'); - const postContent = postContentElement.textContent; - const wordCount = postContent.trim().split(/\s+/).length; - const readingTime = Math.ceil(wordCount / 200); + const postContent = postContentElement.textContent; + const wordCount = postContent.trim().split(/\s+/).length; + const readingTime = Math.ceil(wordCount / 200); - readingTimeElement.textContent = 'Estimated reading time: ' + readingTime + ' min'; + readingTimeElement.textContent = `Estimated reading time: ${readingTime} min`; - expect(readingTimeElement.textContent).to.equal('Estimated reading time: 1 min'); // Adjust the expected text based on the reading time - }); -}); \ No newline at end of file + expect(readingTimeElement.textContent).to.equal('Estimated reading time: 1 min'); // Adjust the expected text based on the reading time + }); +}); diff --git a/test/newImplementedFeatures/instructorApproved.js b/test/newImplementedFeatures/instructorApproved.js index 531798962c..a078c844eb 100644 --- a/test/newImplementedFeatures/instructorApproved.js +++ b/test/newImplementedFeatures/instructorApproved.js @@ -2,7 +2,7 @@ const assert = require('assert'); const request = require('supertest'); -const app = require('../../app'); +const app = require('../../app'); const db = require('../../src/database'); const user = require('../../src/user'); const topics = require('../../src/topics'); @@ -10,87 +10,87 @@ const posts = require('../../src/posts'); const categories = require('../../src/categories'); describe('Instructor Approved Posts', () => { - let instructorToken; - let studentToken; - let postId; - let topicId; + let instructorToken; + let studentToken; + let postId; + let topicId; - before(async () => { - // Create an instructor user and a student user - const instructor = await user.create({ username: 'instructor', password: 'password', isAdmin: true }); - const student = await user.create({ username: 'student', password: 'password' }); + before(async () => { + // Create an instructor user and a student user + const instructor = await user.create({ username: 'instructor', password: 'password', isAdmin: true }); + const student = await user.create({ username: 'student', password: 'password' }); - // Log in as instructor and student to get tokens - instructorToken = await loginUser('instructor', 'password'); - studentToken = await loginUser('student', 'password'); + // Log in as instructor and student to get tokens + instructorToken = await loginUser('instructor', 'password'); + studentToken = await loginUser('student', 'password'); - // Create a category, topic, and a post - const category = await categories.create({ - name: 'Test Category', - description: 'Test category created by testing script', - }); - const topic = await topics.post({ - title: 'Test Topic', - content: 'Test Content', - uid: student.uid, - cid: category.cid, - }); - topicId = topic.tid; - const post = await posts.create({ - content: 'Test Post', - uid: student.uid, - tid: topic.tid, - }); - postId = post.pid; - }); + // Create a category, topic, and a post + const category = await categories.create({ + name: 'Test Category', + description: 'Test category created by testing script', + }); + const topic = await topics.post({ + title: 'Test Topic', + content: 'Test Content', + uid: student.uid, + cid: category.cid, + }); + topicId = topic.tid; + const post = await posts.create({ + content: 'Test Post', + uid: student.uid, + tid: topic.tid, + }); + postId = post.pid; + }); - async function loginUser(username, password) { - const res = await request(app) - .post('/api/v1/login') - .send({ username, password }); - return res.body.token; - } + async function loginUser(username, password) { + const res = await request(app) + .post('/api/v1/login') + .send({ username, password }); + return res.body.token; + } - describe('PUT /api/v1/posts/:pid/approve', () => { - it('should allow an instructor to approve a post', async () => { - const res = await request(app) - .put(`/api/v1/posts/${postId}/approve`) - .set('Authorization', `Bearer ${instructorToken}`) - .expect(200); + describe('PUT /api/v1/posts/:pid/approve', () => { + it('should allow an instructor to approve a post', async () => { + const res = await request(app) + .put(`/api/v1/posts/${postId}/approve`) + .set('Authorization', `Bearer ${instructorToken}`) + .expect(200); - assert.strictEqual(res.body.message, 'Post approved successfully'); + assert.strictEqual(res.body.message, 'Post approved successfully'); - // Verify the post is marked as approved in the database - const post = await posts.getPost(postId); - assert.strictEqual(post.isApproved, true); - }); + // Verify the post is marked as approved in the database + const post = await posts.getPost(postId); + assert.strictEqual(post.isApproved, true); + }); - it('should not allow a student to approve a post', async () => { - const res = await request(app) - .put(`/api/v1/posts/${postId}/approve`) - .set('Authorization', `Bearer ${studentToken}`) - .expect(403); + it('should not allow a student to approve a post', async () => { + const res = await request(app) + .put(`/api/v1/posts/${postId}/approve`) + .set('Authorization', `Bearer ${studentToken}`) + .expect(403); - assert.strictEqual(res.body.error, 'You do not have permission to approve posts'); - }); + assert.strictEqual(res.body.error, 'You do not have permission to approve posts'); + }); - it('should return an error if the post does not exist', async () => { - const res = await request(app) - .put('/api/v1/posts/99999/approve') - .set('Authorization', `Bearer ${instructorToken}`) - .expect(404); + it('should return an error if the post does not exist', async () => { + const res = await request(app) + .put('/api/v1/posts/99999/approve') + .set('Authorization', `Bearer ${instructorToken}`) + .expect(404); - assert.strictEqual(res.body.error, 'Post not found'); - }); - }); + assert.strictEqual(res.body.error, 'Post not found'); + }); + }); - describe('GET /api/v1/posts/:pid', () => { - it('should retrieve the post with approval status', async () => { - const res = await request(app) - .get(`/api/v1/posts/${postId}`) - .expect(200); + describe('GET /api/v1/posts/:pid', () => { + it('should retrieve the post with approval status', async () => { + const res = await request(app) + .get(`/api/v1/posts/${postId}`) + .expect(200); - assert.strictEqual(res.body.post.isApproved, true); - }); - }); -}); \ No newline at end of file + assert.strictEqual(res.body.post.isApproved, true); + }); + }); +});