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

fix(test): Fixed some duplicated tests and deprecated tests #54

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 8 additions & 9 deletions __tests__/middleware/adminOnly.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
const request = require('supertest');
const express = require('express');
const router = require('../../routes/courseRoutes'); // Import your router file here
const router = require('../../routes/testRoutes'); // Import your router file here
const connectDb = require('../fixtures/db');
const { signAccessToken } = require('../../helpers/token');
const mongoose = require('mongoose');

const app = express();
app.use(express.json());
app.use('/api', router); // Mount the router under '/api' path
app.use('/api/test', router); // Mount the router under '/api' path

// Start the Express app on a specific port for testing
const PORT = 5022; // Choose a port for testing
const server = app.listen(PORT, () => {
console.log(`Express server is running on port ${PORT}`);
});
const server = app.listen(PORT);

// Mocked token secret
const TOKEN_SECRET = 'test';
Expand All @@ -36,9 +34,10 @@ describe('Admin token verify', () => {
it('Return an error if no valid admin token is present on private route', async () => {
const token = 'ImAnInvalidToken';
const response = await request(`http://localhost:${PORT}`)
.get('/api/courses')
.get('/api/test/adminOnly')
.set('token', token)
.expect(401);

console.log(response.body.error)

expect(response.body.error).toBeDefined();
});
Expand All @@ -48,15 +47,15 @@ describe('Admin token verify', () => {

// mock that token is valid
const response = await request(`http://localhost:${PORT}`)
.get('/api/courses')
.get('/api/test/adminOnly')
.set('token', token)
.expect(200);
});

it('Test for non-algorithm attack', async () => {
const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.' + btoa(`{"id":1,"iat":${'' + Date.now()},"exp":999999999999}`) + '.';
const response = await request(`http://localhost:${PORT}`)
.get('/api/courses')
.get('/api/test/adminOnly')
.set('token', token)
.expect(401);
});
Expand Down
31 changes: 0 additions & 31 deletions __tests__/routes/courseRoutes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,6 @@ describe('Course Routes', () => {

});

describe('GET /courses', () => {
it('should get all courses', async () => {

const response = await request(`http://localhost:${PORT}`)
.get('/api/courses');
expect(response.status).toBe(200);
expect(response.body).toBeInstanceOf(Array);

// error handling for when there is no courses can be found in the bottom
});

});

describe('GET /courses/:id', () => {


Expand Down Expand Up @@ -476,24 +463,6 @@ describe('Course Routes', () => {
});
});

describe('GET /courses, error handling', () => {

it('should handle no courses not found', async () => {

// delete all courses
await db.collection('courses').deleteMany({});

// send request with no courses in db
const response = await request(`http://localhost:${PORT}`)
.get('/api/courses');

expect(response.status).toBe(404);
expect(response.body.error.code).toBe('E0005');
});

});



afterAll(async () => {
await db.collection('users').deleteMany({}); // Delete all documents in the 'users' collection
Expand Down
15 changes: 14 additions & 1 deletion __tests__/routes/courseRoutes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const errorCodes = require('../../helpers/errorCodes')

const app = express();
app.use(express.json());
app.use('/api', router); // Add your router to the Express app
app.use('/api/courses', router); // Add your router to the Express app

// Mock Google OAuth2 clientID
jest.mock('../../config/keys', () => {
Expand Down Expand Up @@ -157,6 +157,19 @@ describe('Get all courses route', () => {
expect(result.error).toStrictEqual(errorCodes['E0002']);
});

it('returns error 404 if no courses are found', async () => {

// delete all courses
await db.collection('courses').deleteMany({});

// send request with no courses in db
const response = await request(`http://localhost:${PORT}`)
.get('/api/courses')
.set('token', signAccessToken({ id: ADMIN_ID }))
.expect(404)
expect(response.body.error.code).toBe('E0005');
});

afterAll(async () => {
await db.collection('users').deleteMany({}); // Delete all documents in the 'users' collection
await db.collection('courses').deleteMany({}); // Delete all documents in the 'courses' collection
Expand Down
1 change: 0 additions & 1 deletion routes/authRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const { authEndpointHandler } = require('../auth');
const { signAccessToken } = require('../helpers/token');
const { compare, encrypt } = require('../helpers/password');
const errorCodes = require('../helpers/errorCodes');
const send = require('send');
const { sendResetPasswordEmail } = require('../helpers/email');
const { PasswordResetToken } = require('../models/PasswordResetToken');

Expand Down
12 changes: 4 additions & 8 deletions routes/courseRoutes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const router = require('express').Router();
const errorCodes = require('../helpers/errorCodes');
const express = require('express');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const adminOnly = require("../middlewares/adminOnly");

// Models
Expand All @@ -22,21 +18,21 @@ const { IdentityStore } = require("aws-sdk");
/*** COURSE, SECTIONS AND EXERCISE ROUTES ***/

// Get all courses
router.get('/courses', adminOnly, async (req, res) => {
/*router.get('/', adminOnly, async (req, res) => {
const result = await CourseModel.find({});
res.send(result);
});
});*/

// Get all courses for one user
router.get('/courses/creator/:id', requireLogin, async (req, res) => {
router.get('/creator/:id', requireLogin, async (req, res) => {
const id = req.params.id; // Get user id from request
const courses = await CourseModel.find({creator: id}); // Find courses for a specific user

res.send(courses); // Send response
});

//Get all courses
router.get('', async (req, res) => {
router.get('/', adminOnly, async (req, res) => {

try {
// find all courses in the database
Expand Down
5 changes: 5 additions & 0 deletions routes/testRoutes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const router = require('express').Router();
const adminOnly = require('../middlewares/adminOnly');
const requireLogin = require('../middlewares/requireLogin');

// Route for testing JWT verification on private routes
Expand All @@ -8,4 +9,8 @@ router.get('/require-jwt', requireLogin, (req, res) => {
res.status(200).send(req.body);
});

router.get('/adminOnly', adminOnly, (req, res) => {
res.status(200).send(req.body);
})

module.exports = router;
Loading