Skip to content

Commit

Permalink
Refactor import statement for User model to use lowercase filename, c…
Browse files Browse the repository at this point in the history
…hanged index to app, cleaned up pacage.json
  • Loading branch information
OomsOoms committed Jul 31, 2024
1 parent 4b24836 commit 2ee8b4d
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 361 deletions.
777 changes: 434 additions & 343 deletions server/package-lock.json

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
{
"name": "server",
"name": "nettleship-net-api",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"description": "This is the API backend for the nettleship.net website ",
"main": "src/app.js",
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"start": "node src/index",
"dev": "nodemon src/index",
"start": "node src/app",
"dev": "nodemon src/app",
"lint": "eslint .",
"lint:fix": "eslint . --fix"
},
"keywords": [],
"author": "",
"license": "ISC",
"author": "Thomas Nettleship",
"license": "GPL-3.0",
"private": true,
"engines": {
"node": "22.2.0"
},
"dependencies": {
"bcryptjs": "^2.4.3",
"connect-mongo": "^5.1.0",
Expand All @@ -41,4 +45,4 @@
"nodemon": "^3.0.3",
"supertest": "^7.0.0"
}
}
}
2 changes: 1 addition & 1 deletion server/src/api/models/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const User = require('./User.model.js');
const User = require('./user.model.js');

module.exports = {
User,
Expand Down
8 changes: 4 additions & 4 deletions server/src/index.js → server/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ require('dotenv').config();
const express = require('express');

const corsMiddleware = require('./config/corsOptions.js');
const db = require('./config/db');
const sessionConfig = require('./config/sessionConfig');
const { logger, errorHandler } = require('./api/middlewares');
const db = require('./config/db.js');
const sessionConfig = require('./config/sessionConfig.js');
const { logger, errorHandler } = require('./api/middlewares/index.js');

const app = express();
const PORT = process.env.PORT || 8000;
Expand All @@ -23,7 +23,7 @@ app.use(express.json());
app.use(sessionConfig);

// Routes
require('./api/routes')(app);
require('./api/routes/index.js')(app);

// Error handling middleware
app.use(errorHandler);
Expand Down
19 changes: 18 additions & 1 deletion server/tests/integration/userService.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
const request = require('supertest');
const app = require('../../src/index');
const app = require('../../src/app');
const { User } = require('../../src/api/models');
const { generateJwt } = require('../../src/api/helpers');
const nodemailer = require('nodemailer');

let savedEmailText;
jest.mock('nodemailer', () => {
const nodemailerMock = {
sendMail: jest.fn((mailOptions, callback) => {
savedEmailText = mailOptions.text; // Capture the email text
callback(null, {
response: '250 Message accepted',
});
}),
};
return {
createTransport: jest.fn(() => nodemailerMock),
};
})

let token = '';
let user = null;
Expand Down Expand Up @@ -53,6 +69,7 @@ describe('POST /api/users/verify', () => {
const response = await request(app)
.post('/api/users/verify')
.send({ email: '[email protected]' });
console.log(response.body);
expect(response.status).toBe(200);
expect(response.body.message).toBe('Verification email sent');
// should check if the email was sent? seems like effort though and if i write a unit test for the email function then i would be testing it twice
Expand Down
2 changes: 1 addition & 1 deletion server/tests/unit/getAllUsers.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { User } = require('../../src/api/models');
const { userService } = require('../../src/api/services');

jest.mock('../../src/api/models/User.model.js', () => ({
jest.mock('../../src/api/models/user.model.js', () => ({
find: jest.fn().mockResolvedValue([]),
findById: jest.fn(),
}));
Expand Down
2 changes: 1 addition & 1 deletion server/tests/unit/getUserByUsername.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { User } = require('../../src/api/models');
const { userService } = require('../../src/api/services');

jest.mock('../../src/api/models/User.model.js', () => ({
jest.mock('../../src/api/models/user.model.js', () => ({
findOne: jest.fn(),
}));

Expand Down
2 changes: 1 addition & 1 deletion server/tests/unit/requestVerification.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jest.mock('jsonwebtoken', () => ({
sign: jest.fn().mockImplementation(() => 'mockedToken'),
}));

jest.mock('../../src/api/models/User.model.js', () => ({
jest.mock('../../src/api/models/user.model.js', () => ({
findByIdAndUpdate: jest.fn(),
findById: jest.fn(),
findOne: jest.fn(),
Expand Down
2 changes: 1 addition & 1 deletion server/tests/unit/verifyUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jest.mock('jsonwebtoken', () => ({
decode: jest.fn(),
}));

jest.mock('../../src/api/models/User.model.js', () => ({
jest.mock('../../src/api/models/user.model.js', () => ({
findByIdAndUpdate: jest.fn(),
findById: jest.fn(),
findOne: jest.fn(),
Expand Down

0 comments on commit 2ee8b4d

Please sign in to comment.