diff --git a/.gitignore b/.gitignore index 55371e5..76efb07 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ node_modules -.vscode \ No newline at end of file +.vscode diff --git a/all the about b/all the about new file mode 100644 index 0000000..d76aa9a --- /dev/null +++ b/all the about @@ -0,0 +1,147 @@ +const express = require('express'); +const mongoose = require('mongoose'); +const bodyParser = require('body-parser'); +const jwt = require('jsonwebtoken'); +const Subreddit = require('./models/subreddit'); + +const app = express(); +app.use(bodyParser.json()); + +// Connect to MongoDB +mongoose.connect('mongodb://localhost:27017/myapp', { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => console.log('MongoDB connected')) + .catch(err => console.error('MongoDB connection error:', err)); + +async function authenticateUser(req, res, next) { + try { + + const token = req.headers.authorization ? req.headers.authorization.split(' ')[1] : req.cookies.jwt; + + if (!token) { + return res.status(401).json({ error: 'Unauthorized: No token provided' }); + } + + const decoded = jwt.verify(token, process.env.JWT_SECRET); + + const user = await userModel.findById(decoded.userID); + + if (!user) { + return res.status(401).json({ error: 'Unauthorized: User not found' }); + } + + req.user = user; + + next(); + } catch (error) { + console.error('Authentication error:', error); + return res.status(401).json({ error: 'Unauthorized: Invalid token' }); + } +} + +// Task: about/Where +app.post('/about/where', authenticateUser, async (req, res) => { + try { + + const location = req.user.location; + return res.status(200).json({ location }); + } catch (error) { + console.error('Error:', error); + return res.status(500).json({ error: 'Internal server error' }); + } + }); + + // Task: about/Banned + app.post('/about/banned', authenticateUser, async (req, res) => { + try { + + const bannedUsers = await Ban.find(); + return res.status(200).json({ bannedUsers }); + } catch (error) { + console.error('Error:', error); + return res.status(500).json({ error: 'Internal server error' }); + } + }); + + // Task: about/Muted + app.post('/about/muted', authenticateUser, async (req, res) => { + try { + + const mutedUsers = await Mute.find(); + return res.status(200).json({ mutedUsers }); + } catch (error) { + console.error('Error:', error); + return res.status(500).json({ error: 'Internal server error' }); + } + }); + + // Task: about/Moderators + app.post('/about/moderators', authenticateUser, async (req, res) => { + try { + + const moderators = await Moderator.find({ subreddit: req.body.subredditId }); + return res.status(200).json({ moderators }); + } catch (error) { + console.error('Error:', error); + return res.status(500).json({ error: 'Internal server error' }); + } + }); + + // Task: about/Location + app.post('/about/location', authenticateUser, async (req, res) => { + try { + + const locationData = await Location.find({ coordinates: req.body.coordinates }); + return res.status(200).json({ locationData }); + } catch (error) { + console.error('Error:', error); + return res.status(500).json({ error: 'Internal server error' }); + } + }); + + // Task: about/Reports + app.post('/about/reports', authenticateUser, async (req, res) => { + try { + + const reports = await Report.find(); + return res.status(200).json({ reports }); + } catch (error) { + console.error('Error:', error); + return res.status(500).json({ error: 'Internal server error' }); + } + }); + + // Task: about/Spam + app.post('/about/spam', authenticateUser, async (req, res) => { + try { + + const spam = await Spam.find(); + return res.status(200).json({ spam }); + } catch (error) { + console.error('Error:', error); + return res.status(500).json({ error: 'Internal server error' }); + } + }); + + // Task: about/Modqueue + app.post('/about/modqueue', authenticateUser, async (req, res) => { + try { + + const modqueue = await Modqueue.find(); + return res.status(200).json({ modqueue }); + } catch (error) { + console.error('Error:', error); + return res.status(500).json({ error: 'Internal server error' }); + } + }); + + // Task: about/Unmoderated + app.post('/about/unmoderated', authenticateUser, async (req, res) => { + try { + + const unmoderated = await Unmoderated.find(); + return res.status(200).json({ unmoderated }); + } catch (error) { + console.error('Error:', error); + return res.status(500).json({ error: 'Internal server error' }); + } + }); diff --git a/approve,remove,show_comment,leavemoderator,accept_moderator_invite b/approve,remove,show_comment,leavemoderator,accept_moderator_invite new file mode 100644 index 0000000..1835c78 --- /dev/null +++ b/approve,remove,show_comment,leavemoderator,accept_moderator_invite @@ -0,0 +1,108 @@ +const express = require('express'); +const mongoose = require('mongoose'); +const bodyParser = require('body-parser'); +const jwt = require('jsonwebtoken'); +const User = require('./models/user'); +const Item = require('./models/item'); +const Comment = require('./models/comment'); +const Moderator = require('./models/moderator'); + +const app = express(); +app.use(bodyParser.json()); + +// Connect to MongoDB +mongoose.connect('mongodb://localhost:27017/myapp', { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => console.log('MongoDB connected')) + .catch(err => console.error('MongoDB connection error:', err)); + +async function authenticateUser(req, res, next) { + try { + const token = req.headers.authorization ? req.headers.authorization.split(' ')[1] : req.cookies.jwt; + + if (!token) { + return res.status(401).json({ error: 'Unauthorized: No token provided' }); + } + + const decoded = jwt.verify(token, process.env.JWT_SECRET); + const user = await User.findById(decoded.userID); + + if (!user) { + return res.status(401).json({ error: 'Unauthorized: User not found' }); + } + + req.user = user; + + next(); + } catch (error) { + console.error('Authentication error:', error); + return res.status(401).json({ error: 'Unauthorized: Invalid token' }); + } +} + +// Task: approve +app.put('/approve', authenticateUser, async (req, res) => { + try { + const { itemId } = req.body; + + await Item.findByIdAndUpdate(itemId, { approved: true }); + return res.status(200).json({ message: 'Approved successfully' }); + } catch (error) { + console.error('Error approving:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +// Task: remove +app.put('/remove', authenticateUser, async (req, res) => { + try { + const { itemId } = req.body; + + await Item.findByIdAndDelete(itemId); + return res.status(200).json({ message: 'Removed successfully' }); + } catch (error) { + console.error('Error removing:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +// Task: show_comment +app.get('/show_comment', authenticateUser, async (req, res) => { + try { + const { commentId } = req.query; + + const comment = await Comment.findById(commentId); + return res.status(200).json({ message: 'Comment shown successfully', comment }); + } catch (error) { + console.error('Error showing comment:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +// Task: leavemoderator +app.post('/leavemoderator', authenticateUser, async (req, res) => { + try { + const { subredditId } = req.body; + + await Moderator.findOneAndRemove({ user: req.user._id, subreddit: subredditId }); + return res.status(200).json({ message: 'Moderator left successfully' }); + } catch (error) { + console.error('Error leaving moderator:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +// Task: accept_moderator_invite +app.post('/accept_moderator_invite', authenticateUser, async (req, res) => { + try { + const { subredditId } = req.body; + + const moderator = new Moderator({ user: req.user._id, subreddit: subredditId }); + await moderator.save(); + return res.status(200).json({ message: 'Moderator invitation accepted successfully' }); + } catch (error) { + console.error('Error accepting moderator invitation:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +module.exports = app; diff --git a/delete_sr_banner,delete_sr_icon,upload_sr_icon b/delete_sr_banner,delete_sr_icon,upload_sr_icon new file mode 100644 index 0000000..07f4413 --- /dev/null +++ b/delete_sr_banner,delete_sr_icon,upload_sr_icon @@ -0,0 +1,110 @@ +const express = require('express'); +const mongoose = require('mongoose'); +const bodyParser = require('body-parser'); +const jwt = require('jsonwebtoken'); +const Subreddit = require('./models/subreddit'); + +const app = express(); +app.use(bodyParser.json()); + +// Connect to MongoDB +mongoose.connect('mongodb://localhost:27017/myapp', { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => console.log('MongoDB connected')) + .catch(err => console.error('MongoDB connection error:', err)); + +async function authenticateUser(req, res, next) { + try { + + const token = req.headers.authorization ? req.headers.authorization.split(' ')[1] : req.cookies.jwt; + + if (!token) { + return res.status(401).json({ error: 'Unauthorized: No token provided' }); + } + + const decoded = jwt.verify(token, process.env.JWT_SECRET); + + const user = await userModel.findById(decoded.userID); + + if (!user) { + return res.status(401).json({ error: 'Unauthorized: User not found' }); + } + + req.user = user; + + next(); + } catch (error) { + console.error('Authentication error:', error); + return res.status(401).json({ error: 'Unauthorized: Invalid token' }); + } +} + +// Task: delete_sr_banner +app.delete('/delete_sr_banner', authenticateUser, async (req, res) => { + try { + const { subredditId } = req.body; + // Check if the request body contains required fields + if (!subredditId) { + return res.status(400).json({ error: 'Subreddit ID is required' }); + } + // Find the subreddit by ID + const subreddit = await Subreddit.findById(subredditId); + if (!subreddit) { + return res.status(404).json({ error: 'Subreddit not found' }); + } + // Delete the subreddit's banner + subreddit.banner = null; + await subreddit.save(); + return res.status(200).json({ message: 'Subreddit banner deleted successfully' }); + } catch (error) { + console.error('Error deleting subreddit banner:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +// Task: delete_sr_icon +app.delete('/delete_sr_icon', authenticateUser, async (req, res) => { + try { + const { subredditId } = req.body; + // Check if the request body contains required fields + if (!subredditId) { + return res.status(400).json({ error: 'Subreddit ID is required' }); + } + // Find the subreddit by ID + const subreddit = await Subreddit.findById(subredditId); + if (!subreddit) { + return res.status(404).json({ error: 'Subreddit not found' }); + } + // Delete the subreddit's icon + subreddit.icon = null; + await subreddit.save(); + return res.status(200).json({ message: 'Subreddit icon deleted successfully' }); + } catch (error) { + console.error('Error deleting subreddit icon:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +// Task: upload_sr_icon +app.post('/upload_sr_icon', authenticateUser, async (req, res) => { + try { + const { subredditId, icon } = req.body; + // Check if the request body contains required fields + if (!subredditId || !icon) { + return res.status(400).json({ error: 'Subreddit ID and icon data are required' }); + } + // Find the subreddit by ID + const subreddit = await Subreddit.findById(subredditId); + if (!subreddit) { + return res.status(404).json({ error: 'Subreddit not found' }); + } + // Update the subreddit's icon + subreddit.icon = icon; + await subreddit.save(); + return res.status(200).json({ message: 'Subreddit icon uploaded successfully' }); + } catch (error) { + console.error('Error uploading subreddit icon:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +module.exports = app; diff --git a/mine;where,mine;moderator b/mine;where,mine;moderator new file mode 100644 index 0000000..47fc7d8 --- /dev/null +++ b/mine;where,mine;moderator @@ -0,0 +1,85 @@ +const express = require('express'); +const mongoose = require('mongoose'); +const bodyParser = require('body-parser'); +const jwt = require('jsonwebtoken'); +const Subreddit = require('./models/subreddit'); + +const app = express(); +app.use(bodyParser.json()); + +// Connect to MongoDB +mongoose.connect('mongodb://localhost:27017/myapp', { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => console.log('MongoDB connected')) + .catch(err => console.error('MongoDB connection error:', err)); + + +async function authenticateUser(req, res, next) { + try { + + const token = req.headers.authorization ? req.headers.authorization.split(' ')[1] : req.cookies.jwt; + + if (!token) { + return res.status(401).json({ error: 'Unauthorized: No token provided' }); + } + + const decoded = jwt.verify(token, process.env.JWT_SECRET); + + const user = await userModel.findById(decoded.userID); + + if (!user) { + return res.status(401).json({ error: 'Unauthorized: User not found' }); + } + + req.user = user; + + next(); + } catch (error) { + console.error('Authentication error:', error); + return res.status(401).json({ error: 'Unauthorized: Invalid token' }); + } +} + +// Task: mine/where +app.post('/mine/where', authenticateUser, async (req, res) => { + try { + const { subredditId, filter } = req.body; + // Check if the request body contains required fields + if (!subredditId || !filter) { + return res.status(400).json({ error: 'Subreddit ID and filter are required' }); + } + // Find the subreddit by ID + const subreddit = await Subreddit.findById(subredditId); + if (!subreddit) { + return res.status(404).json({ error: 'Subreddit not found' }); + } + + let filteredPosts; + if (filter === 'new') { + filteredPosts = await Post.find({ subreddit: subredditId }).sort({ createdAt: -1 }); + } else if (filter === 'top') { + filteredPosts = await Post.find({ subreddit: subredditId }).sort({ upvotes: -1 }); + } else { + return res.status(400).json({ error: 'Invalid filter criteria' }); + } + return res.status(200).json({ posts: filteredPosts }); + } catch (error) { + console.error('Error in mine/where:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +// Task: mine/moderator +app.get('/mine/moderator', authenticateUser, async (req, res) => { + try { + // Find subreddits where the current user is a moderator + const moderatorSubreddits = await Subreddit.find({ moderators: req.user._id }); + return res.status(200).json({ subreddits: moderatorSubreddits }); + } catch (error) { + console.error('Error in mine/moderator:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +app.listen(3000, () => { + console.log('Server is running on port 3000'); +}); diff --git a/saved_categories,sendreplies,set_suggested_sort b/saved_categories,sendreplies,set_suggested_sort new file mode 100644 index 0000000..51568b7 --- /dev/null +++ b/saved_categories,sendreplies,set_suggested_sort @@ -0,0 +1,116 @@ +const express = require('express'); +const mongoose = require('mongoose'); +const bodyParser = require('body-parser'); +const jwt = require('jsonwebtoken'); +const Subreddit = require('./models/subreddit'); + +const app = express(); +app.use(bodyParser.json()); + +// Connect to MongoDB +mongoose.connect('mongodb://localhost:27017/myapp', { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => console.log('MongoDB connected')) + .catch(err => console.error('MongoDB connection error:', err)); + + + async function authenticateUser(req, res, next) { + try { + + const token = req.headers.authorization ? req.headers.authorization.split(' ')[1] : req.cookies.jwt; + + if (!token) { + return res.status(401).json({ error: 'Unauthorized: No token provided' }); + } + + const decoded = jwt.verify(token, process.env.JWT_SECRET); + const user = await userModel.findById(decoded.userID); + + if (!user) { + return res.status(401).json({ error: 'Unauthorized: User not found' }); + } + + req.user = user; + next(); + } catch (error) { + console.error('Authentication error:', error); + return res.status(401).json({ error: 'Unauthorized: Invalid token' }); + } + } +// Task: saved_categories +app.post('/saved_categories', authenticateUser, async (req, res) => { + try { + const { subredditId, categories } = req.body; + // Check if the request body contains required fields + if (!subredditId || !categories) { + return res.status(400).json({ error: 'Subreddit ID and categories are required' }); + } + // Find the subreddit by ID + const subreddit = await Subreddit.findById(subredditId); + if (!subreddit) { + return res.status(404).json({ error: 'Subreddit not found' }); + } + // Update the subreddit's saved categories + subreddit.savedCategories = categories; + await subreddit.save(); + return res.status(200).json({ message: 'Saved categories updated successfully' }); + } catch (error) { + console.error('Error saving categories:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +// Task: sendreplies +app.post('/sendreplies', authenticateUser, async (req, res) => { + try { + const { postId, reply } = req.body; + // Check if the request body contains required fields + if (!postId || !reply) { + return res.status(400).json({ error: 'Post ID and reply content are required' }); + } + // Find the post by ID + const post = await Post.findById(postId); + if (!post) { + return res.status(404).json({ error: 'Post not found' }); + } + // Create a new reply document + const newReply = new Reply({ + user: req.user._id, + post: postId, + content: reply, + }); + // Save the new reply + await newReply.save(); + // Add the reply to the post's replies array + post.replies.push(newReply._id); + await post.save(); + // Return success message + return res.status(200).json({ message: 'Reply sent successfully' }); + } catch (error) { + console.error('Error sending reply:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + + +// Task: set_suggested_sort +app.put('/set_suggested_sort', authenticateUser, async (req, res) => { + try { + const { subredditId, suggestedSort } = req.body; + // Check if the request body contains required fields + if (!subredditId || !suggestedSort) { + return res.status(400).json({ error: 'Subreddit ID and suggested sort are required' }); + } + // Find the subreddit by ID + const subreddit = await Subreddit.findById(subredditId); + if (!subreddit) { + return res.status(404).json({ error: 'Subreddit not found' }); + } + // Update the subreddit's suggested sort + subreddit.suggestedSort = suggestedSort; + await subreddit.save(); + return res.status(200).json({ message: 'Suggested sort updated successfully' }); + } catch (error) { + console.error('Error setting suggested sort:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); diff --git a/site_admin;about;edite b/site_admin;about;edite new file mode 100644 index 0000000..765f33b --- /dev/null +++ b/site_admin;about;edite @@ -0,0 +1,76 @@ +const express = require('express'); +const mongoose = require('mongoose'); +const bodyParser = require('body-parser'); +const jwt = require('jsonwebtoken'); +const Subreddit = require('./models/subreddit'); + +const app = express(); +app.use(bodyParser.json()); + +// Connect to MongoDB +mongoose.connect('mongodb://localhost:27017/myapp', { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => console.log('MongoDB connected')) + .catch(err => console.error('MongoDB connection error:', err)); + +async function authenticateUser(req, res, next) { + try { + + const token = req.headers.authorization ? req.headers.authorization.split(' ')[1] : req.cookies.jwt; + + if (!token) { + return res.status(401).json({ error: 'Unauthorized: No token provided' }); + } + + const decoded = jwt.verify(token, process.env.JWT_SECRET); + + + const user = await userModel.findById(decoded.userID); + + if (!user) { + return res.status(401).json({ error: 'Unauthorized: User not found' }); + } + + + req.user = user; + + next(); + } catch (error) { + console.error('Authentication error:', error); + return res.status(401).json({ error: 'Unauthorized: Invalid token' }); + } +} + +// Task: site_admin +app.post('/site_admin', authenticateUser, async (req, res) => { + try { + + if (req.user.isAdmin) { + return res.status(200).json({ message: 'Site admin task completed successfully' }); + } else { + return res.status(403).json({ error: 'Forbidden: User is not a site admin' }); + } + } catch (error) { + console.error('Error in site_admin task:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +// Task: /about/edit +app.put('/about/edit', authenticateUser, async (req, res) => { + try { + + const { subredditId, aboutText } = req.body; + const subreddit = await Subreddit.findByIdAndUpdate(subredditId, { about: aboutText }); + if (!subreddit) { + return res.status(404).json({ error: 'Subreddit not found' }); + } + return res.status(200).json({ message: 'About section updated successfully' }); + } catch (error) { + console.error('Error in /about/edit task:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + + +module.exports = app; + diff --git a/subreddit info,report b/subreddit info,report new file mode 100644 index 0000000..1275e29 --- /dev/null +++ b/subreddit info,report @@ -0,0 +1,64 @@ +const express = require('express'); +const mongoose = require('mongoose'); +const bodyParser = require('body-parser'); +const jwt = require('jsonwebtoken'); +const Subreddit = require('./models/subreddit'); + +const app = express(); +app.use(bodyParser.json()); + +// Connect to MongoDB +mongoose.connect('mongodb://localhost:27017/myapp', { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => console.log('MongoDB connected')) + .catch(err => console.error('MongoDB connection error:', err)); + +async function authenticateUser(req, res, next) { + try { + const token = req.headers.authorization ? req.headers.authorization.split(' ')[1] : req.cookies.jwt; + if (!token) { + return res.status(401).json({ error: 'Unauthorized: No token provided' }); + } + const decoded = jwt.verify(token, process.env.JWT_SECRET); + const user = await userModel.findById(decoded.userID); + if (!user) { + return res.status(401).json({ error: 'Unauthorized: User not found' }); + } + req.user = user; + next(); + } catch (error) { + console.error('Authentication error:', error); + return res.status(401).json({ error: 'Unauthorized: Invalid token' }); + } +} +// Task:subreddit_info +app.get('/subreddit_info/:id', authenticateUser, async (req, res) => { + try { + const subredditId = req.params.id; + const subreddit = await Subreddit.findById(subredditId); + if (!subreddit) { + return res.status(404).json({ error: 'Subreddit not found' }); + } + return res.status(200).json(subreddit); + } catch (error) { + console.error('Error fetching subreddit information:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +// Task:report +app.post('/report', authenticateUser, async (req, res) => { + try { + const { postId, reason } = req.body; + if (!postId || !reason) { + return res.status(400).json({ error: 'Post ID and reason are required' }); + } + + return res.status(200).json({ message: 'Post reported successfully' }); + } catch (error) { + console.error('Error reporting post:', error); + return res.status(500).json({ error: 'Internal server error' }); + } +}); + +// Export the app for testing +module.exports = app; diff --git a/test all the about b/test all the about new file mode 100644 index 0000000..8dd3968 --- /dev/null +++ b/test all the about @@ -0,0 +1,138 @@ +const request = require('supertest'); +const app = require('./app'); +const Ban = require('./models/ban'); +const Mute = require('./models/mute'); +const Moderator = require('./models/moderator'); +const LocationData = require('./models/locationData'); +const Report = require('./models/report'); +const Spam = require('./models/spam'); +const Modqueue = require('./models/modqueue'); +const Unmoderated = require('./models/unmoderated'); + +describe('POST /about/where', () => { + it('should return user location', async () => { + // Make a POST request to get user location + const response = await request(app) + .post('/about/where') + .set('Authorization', 'Bearer YOUR_TOKEN_HERE'); + + // Expect response status to be 200 and check the location + expect(response.status).toBe(200); + expect(response.body.location).toBeDefined(); + + }); +}); + +describe('GET /about/banned', () => { + it('should return list of banned users', async () => { + // Make a GET request to get list of banned users + const response = await request(app) + .get('/about/banned') + .set('Authorization', 'Bearer YOUR_TOKEN_HERE'); + + // Expect response status to be 200 and check the list of banned users + expect(response.status).toBe(200); + expect(response.body.bannedUsers).toBeDefined(); + + }); +}); + +describe('GET /about/muted', () => { + it('should return list of muted users', async () => { + // Make a GET request to get list of muted users + const response = await request(app) + .get('/about/muted') + .set('Authorization', 'Bearer YOUR_TOKEN_HERE'); + + // Expect response status to be 200 and check the list of muted users + expect(response.status).toBe(200); + expect(response.body.mutedUsers).toBeDefined(); + + }); +}); + +describe('GET /about/moderators', () => { + it('should return list of moderators', async () => { + // Make a GET request to get list of moderators + const response = await request(app) + .get('/about/moderators') + .set('Authorization', 'Bearer YOUR_TOKEN_HERE'); + + // Expect response status to be 200 and check the list of moderators + expect(response.status).toBe(200); + expect(response.body.moderators).toBeDefined(); + + }); +}); + +describe('GET /about/location', () => { + it('should return location details', async () => { + // Make a GET request to get location details + const response = await request(app) + .get('/about/location') + .set('Authorization', 'Bearer YOUR_TOKEN_HERE'); + + // Expect response status to be 200 and check the location details + expect(response.status).toBe(200); + expect(response.body.locationDetails).toBeDefined(); + + }); +}); + +describe('GET /about/reports', () => { + it('should return list of reports', async () => { + // Make a GET request to get list of reports + const response = await request(app) + .get('/about/reports') + .set('Authorization', 'Bearer YOUR_TOKEN_HERE'); + + // Expect response status to be 200 and check the list of reports + expect(response.status).toBe(200); + expect(response.body.reports).toBeDefined(); + + }); +}); + +describe('GET /about/spam', () => { + it('should return list of spam posts', async () => { + // Make a GET request to get list of spam posts + const response = await request(app) + .get('/about/spam') + .set('Authorization', 'Bearer YOUR_TOKEN_HERE'); + + // Expect response status to be 200 and check the list of spam posts + expect(response.status).toBe(200); + expect(response.body.spamPosts).toBeDefined(); + + }); +}); + +describe('GET /about/modqueue', () => { + it('should return list of posts in moderation queue', async () => { + // Make a GET request to get list of posts in moderation queue + const response = await request(app) + .get('/about/modqueue') + .set('Authorization', 'Bearer YOUR_TOKEN_HERE'); + + // Expect response status to be 200 and check the list of posts in modqueue + expect(response.status).toBe(200); + expect(response.body.modqueuePosts).toBeDefined(); + + }); +}); + +describe('GET /about/unmoderated', () => { + it('should return list of unmoderated posts', async () => { + // Make a GET request to get list of unmoderated posts + const response = await request(app) + .get('/about/unmoderated') + .set('Authorization', 'Bearer YOUR_TOKEN_HERE'); + + // Expect response status to be 200 and check the list of unmoderated posts + expect(response.status).toBe(200); + expect(response.body.unmoderatedPosts).toBeDefined(); + + }); +}); + + diff --git a/test approve,remove,show_comment,leavemoderator,accept_moderator_invite b/test approve,remove,show_comment,leavemoderator,accept_moderator_invite new file mode 100644 index 0000000..c940bae --- /dev/null +++ b/test approve,remove,show_comment,leavemoderator,accept_moderator_invite @@ -0,0 +1,78 @@ +const request = require('supertest'); +const app = require('./app'); +const Subreddit = require('./models/subreddit'); + +describe('POST /approve', () => { + it('should approve something', async () => { + // Make a POST request to approve something + const response = await request(app) + .post('/approve') + .send({ itemId: 'ITEM_ID_TO_APPROVE' }); + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('Approved successfully'); + + + }); +}); + +describe('POST /remove', () => { + it('should remove something', async () => { + // Make a POST request to remove something + const response = await request(app) + .post('/remove') + .send({ itemId: 'ITEM_ID_TO_REMOVE' }); + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('Removed successfully'); + + + }); +}); + +describe('GET /show_comment', () => { + it('should show a comment', async () => { + // Make a GET request to show a comment + const response = await request(app) + .get('/show_comment') + .query({ commentId: 'COMMENT_ID_TO_SHOW' }); + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('Comment shown successfully'); + + + }); +}); + +describe('POST /leavemoderator', () => { + it('should leave moderator role', async () => { + // Make a POST request to leave moderator role + const response = await request(app) + .post('/leavemoderator') + .send({ subredditId: 'SUBREDDIT_ID_TO_LEAVE_MODERATOR' }); + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('Moderator left successfully'); + + + }); +}); + +describe('POST /accept_moderator_invite', () => { + it('should accept moderator invitation', async () => { + // Make a POST request to accept moderator invitation + const response = await request(app) + .post('/accept_moderator_invite') + .send({ subredditId: 'SUBREDDIT_ID_TO_ACCEPT_INVITE' }); + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('Moderator invitation accepted successfully'); + + + }); +}); diff --git a/test delete_sr_banner,delete_sr_icon,upload_sr_icon b/test delete_sr_banner,delete_sr_icon,upload_sr_icon new file mode 100644 index 0000000..827b6d9 --- /dev/null +++ b/test delete_sr_banner,delete_sr_icon,upload_sr_icon @@ -0,0 +1,70 @@ +const request = require('supertest'); +const app = require('./app'); +const Subreddit = require('./models/subreddit'); + +describe('DELETE /delete_sr_banner', () => { + it('should delete the subreddit banner', async () => { + // Create a test subreddit + const subreddit = new Subreddit({ name: 'test_subreddit' }); + await subreddit.save(); + + // Make a DELETE request to delete the subreddit banner + const response = await request(app) + .delete('/delete_sr_banner') + .send({ subredditId: subreddit._id }); + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('Subreddit banner deleted successfully'); + + // Fetch the subreddit from the database and check if the banner is deleted + const updatedSubreddit = await Subreddit.findById(subreddit._id); + expect(updatedSubreddit.banner).toBeNull(); + }); +}); + +describe('DELETE /delete_sr_icon', () => { + it('should delete the subreddit icon', async () => { + // Create a test subreddit + const subreddit = new Subreddit({ name: 'test_subreddit' }); + await subreddit.save(); + + // Make a DELETE request to delete the subreddit icon + const response = await request(app) + .delete('/delete_sr_icon') + .send({ subredditId: subreddit._id }); + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('Subreddit icon deleted successfully'); + + // Fetch the subreddit from the database and check if the icon is deleted + const updatedSubreddit = await Subreddit.findById(subreddit._id); + expect(updatedSubreddit.icon).toBeNull(); + }); +}); + +describe('POST /upload_sr_icon', () => { + it('should upload the subreddit icon', async () => { + // Create a test subreddit + const subreddit = new Subreddit({ name: 'test_subreddit' }); + await subreddit.save(); + + // Icon data to be sent in the request + const iconData = 'icon_data_here'; + + // Make a POST request to upload the subreddit icon + const response = await request(app) + .post('/upload_sr_icon') + .send({ subredditId: subreddit._id, icon: iconData }); + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('Subreddit icon uploaded successfully'); + + // Fetch the subreddit from the database and check if the icon is uploaded + const updatedSubreddit = await Subreddit.findById(subreddit._id); + expect(updatedSubreddit.icon).toBe(iconData); + }); +}); + diff --git a/test info,report b/test info,report new file mode 100644 index 0000000..10bfe05 --- /dev/null +++ b/test info,report @@ -0,0 +1,63 @@ +const request = require('supertest'); +const app = require('./app'); +const Subreddit = require('./models/subreddit'); + +describe('GET /subreddit_info/:id', () => { + it('should return subreddit information', async () => { + // Create a test subreddit + const subreddit = new Subreddit({ name: 'test_subreddit', description: 'Test description' }); + await subreddit.save(); + + // Make a GET request to retrieve subreddit information + const response = await request(app).get(`/subreddit_info/${subreddit._id}`); + + // Expect response status to be 200 and check the subreddit data + expect(response.status).toBe(200); + expect(response.body.name).toBe('test_subreddit'); + expect(response.body.description).toBe('Test description'); + }); + + it('should return 404 if subreddit ID does not exist', async () => { + // Make a GET request with a non-existing subreddit ID + const response = await request(app).get('/subreddit_info/123456789012345678901234'); + + // Expect response status to be 404 + expect(response.status).toBe(404); + }); +}); + +describe('POST /report', () => { + it('should report a post', async () => { + // Assuming postId is a valid post ID + const postId = '123456789012345678901234'; + const reason = 'Inappropriate content'; + + // Make a POST request to report a post + const response = await request(app) + .post('/report') + .send({ postId, reason }); + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('Post reported successfully'); + }); + + it('should return 400 if postId or reason is missing', async () => { + // Make a POST request with missing postId + const response1 = await request(app) + .post('/report') + .send({ reason: 'Inappropriate content' }); + + // Expect response status to be 400 + expect(response1.status).toBe(400); + + // Make a POST request with missing reason + const response2 = await request(app) + .post('/report') + .send({ postId: '123456789012345678901234' }); + + // Expect response status to be 400 + expect(response2.status).toBe(400); + }); +}); + diff --git a/test mine;where,mine;moderator b/test mine;where,mine;moderator new file mode 100644 index 0000000..9b451f9 --- /dev/null +++ b/test mine;where,mine;moderator @@ -0,0 +1,47 @@ +//test +const request = require('supertest'); +const app = require('./app'); +const Subreddit = require('./models/subreddit'); +const Post = require('./models/post'); + +describe('POST /mine/where', () => { + it('should filter posts based on the provided criteria', async () => { + // Create a test subreddit + const subreddit = new Subreddit({ name: 'test_subreddit' }); + await subreddit.save(); + + // Create test posts for the subreddit + const post1 = new Post({ title: 'Post 1', subreddit: subreddit._id }); + const post2 = new Post({ title: 'Post 2', subreddit: subreddit._id }); + await post1.save(); + await post2.save(); + + // Make a POST request to filter posts + const response = await request(app) + .post('/mine/where') + .send({ subredditId: subreddit._id, filter: 'new' }); + + // Expect response status to be 200 and check the number of returned posts + expect(response.status).toBe(200); + expect(response.body.posts.length).toBe(2); + }); +}); + +describe('GET /mine/moderator', () => { + it('should return subreddits where the user is a moderator', async () => { + // Create test subreddits + const subreddit1 = new Subreddit({ name: 'subreddit1', moderators: ['user1'] }); + const subreddit2 = new Subreddit({ name: 'subreddit2', moderators: ['user2'] }); + await subreddit1.save(); + await subreddit2.save(); + + // Make a GET request to fetch moderator subreddits + const response = await request(app) + .get('/mine/moderator') + .set('Authorization', 'Bearer '); + + // Expect response status to be 200 and check the number of returned subreddits + expect(response.status).toBe(200); + expect(response.body.subreddits.length).toBe(1); + }); +}); diff --git a/test saved_categories,sendreplies,set_suggested_sort b/test saved_categories,sendreplies,set_suggested_sort new file mode 100644 index 0000000..d16c153 --- /dev/null +++ b/test saved_categories,sendreplies,set_suggested_sort @@ -0,0 +1,69 @@ +const request = require('supertest'); +const app = require('./app'); +const Subreddit = require('./models/subreddit'); +const Post = require('./models/post'); +const Reply = require('./models/reply'); + +describe('POST /saved_categories', () => { + it('should save categories to a subreddit', async () => { + // Create a test subreddit + const subreddit = new Subreddit({ name: 'test_subreddit' }); + await subreddit.save(); + + // Make a POST request to save categories + const response = await request(app) + .post('/saved_categories') + .send({ subredditId: subreddit._id, categories: ['category1', 'category2'] }); + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('Saved categories updated successfully'); + + // Fetch the subreddit from the database and check if categories are updated + const updatedSubreddit = await Subreddit.findById(subreddit._id); + expect(updatedSubreddit.savedCategories).toEqual(['category1', 'category2']); + }); +}); + +describe('POST /sendreplies', () => { + it('should send a reply to a post', async () => { + // Create a test post + const post = new Post({ title: 'Test Post', content: 'This is a test post' }); + await post.save(); + + // Make a POST request to send a reply + const response = await request(app) + .post('/sendreplies') + .send({ postId: post._id, reply: 'This is a test reply' }); + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('Reply sent successfully'); + + // Fetch the post from the database and check if the reply is added + const updatedPost = await Post.findById(post._id).populate('replies'); + expect(updatedPost.replies.length).toBe(1); + expect(updatedPost.replies[0].content).toBe('This is a test reply'); + }); +}); + +describe('PUT /set_suggested_sort', () => { + it('should set suggested sort for a subreddit', async () => { + // Create a test subreddit + const subreddit = new Subreddit({ name: 'test_subreddit' }); + await subreddit.save(); + + // Make a PUT request to set suggested sort + const response = await request(app) + .put('/set_suggested_sort') + .send({ subredditId: subreddit._id, suggestedSort: 'new' }); + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('Suggested sort updated successfully'); + + // Fetch the subreddit from the database and check if suggested sort is updated + const updatedSubreddit = await Subreddit.findById(subreddit._id); + expect(updatedSubreddit.suggestedSort).toBe('new'); + }); +}); diff --git a/test site_admin;about;edite b/test site_admin;about;edite new file mode 100644 index 0000000..26ba3f3 --- /dev/null +++ b/test site_admin;about;edite @@ -0,0 +1,48 @@ +const request = require('supertest'); +const app = require('./app'); +const UserModel = require('./models/user'); +const AboutModel = require('./models/about'); + +describe('POST /site_admin', () => { + it('should perform site admin tasks', async () => { + + const user = new UserModel({ username: 'admin', isAdmin: true }); + await user.save(); + + // Authenticate the user and get the JWT token + const token = jwt.sign({ userID: user._id }, process.env.JWT_SECRET); + + // Make a POST request to perform site admin tasks + const response = await request(app) + .post('/site_admin') + .set('Authorization', `Bearer ${token}`) + .send({ userId: 'user_id_here', role: 'admin' }); // Example request body + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('Site admin task completed successfully'); + + + }); + }); + + describe('PUT /about/edit', () => { + it('should edit the about page', async () => { + + const aboutContent = 'This is the updated about page content'; + + // Make a PUT request to edit the about page + const response = await request(app) + .put('/about/edit') + .send({ content: aboutContent }); // Example request body + + // Expect response status to be 200 and check the message + expect(response.status).toBe(200); + expect(response.body.message).toBe('About page updated successfully'); + + // Fetch the about page content from the database and check if it's updated + const updatedAbout = await AboutModel.findOne(); + expect(updatedAbout.content).toBe(aboutContent); + }); + }); +