Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented a new test suite for new Implemented Features under path test/newImplementedFeature :the anonymous feature post, Instructor approval post and Estimanted reading Time #52

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions test/newImplementedFeatures/annonymousFeature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use strict';

const { JSDOM } = require('jsdom');
const { expect } = require('chai');

Check failure on line 4 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module 'chai'
const request = require('supertest');

Check failure on line 5 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module '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;

Check failure on line 12 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
let postId;

Check failure on line 13 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
let topicId;

Check failure on line 14 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces

before(async () => {

Check failure on line 16 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
// Create a student user

Check failure on line 17 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces
const student = await user.create({ username: 'student', password: 'password' });

Check failure on line 18 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces

// Log in as student to get token

Check failure on line 20 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces
studentToken = await loginUser('student', 'password');

Check failure on line 21 in test/newImplementedFeatures/annonymousFeature.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces

// 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.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;
}
});
});
});
45 changes: 45 additions & 0 deletions test/newImplementedFeatures/estimatedTimeForReadingPost.js
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
});
});
96 changes: 96 additions & 0 deletions test/newImplementedFeatures/instructorApproved.js
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);
});
});
});