diff --git a/test/newImplementedFeatures/annonymousFeature.js b/test/newImplementedFeatures/annonymousFeature.js new file mode 100644 index 0000000000..c8af957376 --- /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; + 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; + 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 new file mode 100644 index 0000000000..aed3ce89cb --- /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 + }); +}); diff --git a/test/newImplementedFeatures/instructorApproved.js b/test/newImplementedFeatures/instructorApproved.js new file mode 100644 index 0000000000..a078c844eb --- /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); + }); + }); +});