Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyg37 committed Apr 2, 2024
1 parent b76ebe3 commit 842ada0
Show file tree
Hide file tree
Showing 7 changed files with 4,252 additions and 1,163 deletions.
5,251 changes: 4,168 additions & 1,083 deletions backend/package-lock.json

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"bcrypt": "^5.1.1",
"chai": "^5.1.0",
"express": "^4.19.2",
"jest": "^29.7.0",
"jsonwebtoken": "^9.0.2",
"mocha": "^10.4.0",
"mongoose": "^8.2.3",
"swagger": "^0.0.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0",
"ts-mocha": "^10.0.0"
"ts-jest": "^29.1.2"
},
"devDependencies": {
"@types/bcrypt": "^5.0.2",
Expand All @@ -20,16 +21,22 @@
"@types/mocha": "^10.0.6",
"@types/sinon": "^17.0.3",
"@types/supertest": "^6.0.2",
"mocha": "^10.4.0",
"sinon": "^17.0.1",
"supertest": "^6.3.4",
"ts-mocha": "^10.0.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.3"
},
"scripts": {
"start": "node dist/index.js",
"dev": "ts-node src/index.ts",
"build": "tsc",
"test": "mocha --require ts-node/register ./src/test/*.test.ts"
"test": "mocha --require ts-node/register ./src/**/*.test.ts"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"testMatch": [
"**/*.test.ts"
]
}
}
30 changes: 30 additions & 0 deletions backend/src/models/details.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import mongoose from "mongoose";

export interface IDetails extends Document{
firstName: string,
lastName: string,
phone: string,
socials: string[]
}

const detailSchema = new mongoose.Schema({
firstName: { type: String},
lastName: {type: String},
phone: {type: String},
socials: {type: [String]}
},
{
toJSON: {
transform: (doc, ret) => {
return {
firstName: ret.firstName,
lastName: ret.lastName,
phone: ret.phone,
socials: ret.socials
}
}
}
}
);

export default mongoose.model<IDetails>('Details', detailSchema);
21 changes: 21 additions & 0 deletions backend/src/routes/detailsRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import express, { Request, Response } from "express";
import Details from "../models/details";

const router = express.Router()

router.get('details', async(res: Response, req: Request) => {
const info = await Details.find()
res.send(info)
})

router.post('/details/edit', async(req: Request, res: Response) => {
const {firstName, lastName, phone, socials} = req.params;
const info = new Details ({
firstName,
lastName,
phone,
socials
})

res.json(info)
})
58 changes: 20 additions & 38 deletions backend/src/test/authRoutes.test.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,28 @@
import { expect } from 'chai';
import chai from 'chai';
import request from 'supertest';
import express, { Express } from 'express';
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import User from '../models/auth';
import authRoutes from '../routes/authRoutes';

describe('authRoutes', () => {
let app: Express;

before(() => {
app = express();
app.use('/', require('../routes/auth').default);
});

it('should get user data', async () => {
const res = await request(app).get('/data');
expect(res.status).to.equal(200);
});

it('should create a new user', async () => {
const password = 'password';
const hashedPassword = await bcrypt.hash(password, 10)
const res = await request(app)
.post('/signup')
.send({
account: 'admin',
username: 'test',
email: '[email protected]',
password: hashedPassword,
});
expect(res.status).to.equal(200);
expect(res.body).to.have.property('message', 'User created successfully');
});

it('should login a user', async () => {
const password = 'password';
const hashedPassword = await bcrypt.hash(password, 10);
const user = new User({
account: 'admin',
username: 'test',
password: hashedPassword,
describe('Auth Routes', () => {
let app: Express;
before(() => {
app = express();
app.use('/v1/auth', authRoutes);
});
it('should get user data', async () => {
const res = await request(app).get('/data');
});
it('should create a new user', async () => {
const res = await request(app).post('/signup').send({
account: 'admin',
username: 'admin',
email: '[email protected]',
password: 'password',
confirmPassword: 'password',
});

});
});
});

36 changes: 0 additions & 36 deletions backend/src/test/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +0,0 @@
import { expect } from 'chai';
import request from 'supertest';
import mongoose from 'mongoose';
import express from 'express';
import bodyParser from 'body-parser';
import authRoutes from '../routes/authRoutes';
import blogRoutes from '../routes/blogRoutes';
import contactRoutes from '../routes/contactRoutes';

describe('Server', () => {
let app: express.Express;

before(() => {
mongoose.connect('mongodb://localhost:27017/test');

app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/v1/auth', authRoutes);
app.use('/v1/blog', blogRoutes);
app.use('/v1/feedback', contactRoutes);
});

after(async () => {
await mongoose.connection.close();
});

describe('GET /v1/auth/data', () => {
it('should return user data', async () => {
const res = await request(app).get('/v1/auth/data');
expect(res.status).to.equal(200);
});
});


});
2 changes: 1 addition & 1 deletion backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */

/* Language and Environment */
"target": "ES6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "ES2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
Expand Down

0 comments on commit 842ada0

Please sign in to comment.