forked from CMU-17313Q/cmu-17313q-f24-nodebb-f24-NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from jdufitum/testSuit
Implemented a new test suite for new Implemented Features under path test/newImplementedFeature :the anonymous feature post, Instructor approval post and Estimanted reading Time
- Loading branch information
Showing
3 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(` | ||
<div class="form-group"> | ||
<label for="anonymousDropdown">[[topic:composer.select-anonymous-type]]</label> | ||
<select class="form-control" id="anonymousDropdown" name="anonymousType"> | ||
<option value="none">[[topic:composer-anonymous-none]]</option> | ||
<option value="student">[[topic:composer-anonymous-student]]</option> | ||
<option value="all">[[topic:composer-anonymous-all]]</option> | ||
</select> | ||
</div> | ||
`); | ||
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; | ||
} | ||
}); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
test/newImplementedFeatures/estimatedTimeForReadingPost.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(` | ||
<div id="post-content-1">This is a test post content with a few words.</div> | ||
<span id="reading-time-1" class="text-muted me-3"></span> | ||
`); | ||
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 | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |