Skip to content

Commit 0bcd536

Browse files
committed
add test
- routes - Mongo
1 parent 384abb8 commit 0bcd536

File tree

12 files changed

+6503
-739
lines changed

12 files changed

+6503
-739
lines changed

globalConfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"mongoUri":"mongodb://127.0.0.1:51986/jest?","mongoDBName":"jest"}

jest-mongodb-config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
mongodbMemoryServerOptions: {
3+
instance: {
4+
dbName: 'jest'
5+
},
6+
binary: {
7+
version: '4.0.2', // Version of MongoDB
8+
skipMD5: true
9+
},
10+
autoStart: false
11+
}
12+
};

jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
timers: 'fake',
4+
preset: '@shelf/jest-mongodb'
5+
}

package-lock.json

Lines changed: 6299 additions & 730 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "server/server.js",
66
"scripts": {
77
"dev": "nodemon server/server.js",
8-
"start": "node server/server.js"
8+
"start": "node server/server.js",
9+
"test": "jest --forceExit"
910
},
1011
"repository": {
1112
"type": "git",
@@ -32,7 +33,16 @@
3233
"underscore": "^1.11.0"
3334
},
3435
"devDependencies": {
36+
"@shelf/jest-mongodb": "^1.2.4",
3537
"dotenv": "^8.2.0",
36-
"nodemon": "^2.0.4"
38+
"jest": "^26.6.3",
39+
"nodemon": "^2.0.4",
40+
"supertest": "^6.1.3"
41+
},
42+
"jest": {
43+
"testEnvironment": "node",
44+
"coveragePathIgnorePatterns": [
45+
"/node_modules/"
46+
]
3747
}
3848
}

server/middlewares/authentication.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const config = require('../config/config');
44
let checkToken = (req, res, next) => {
55

66
let token = req.get('token');
7+
78
jwt.verify(token, config.dev, (err, decoded) => {
89
if (err) {
910
return res.status(401).json({

server/middlewares/counter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let app = express();
44

55
app.use((req, res, next)=>{
66
app.locals.counter = (++app.locals.counter || 1);
7-
console.log(`Visitor counter ${app.locals.counter}`);
7+
//console.log(`Visitor counter ${app.locals.counter}`);
88
next();
99
});
1010

server/routes/category.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ app.get('/category', (req, res) => {
1515
err
1616
})
1717
};
18+
console.log(categories)
1819
res.status(200).json({
1920
ok: true,
2021
categories

server/routes/login.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ app.post('/login', (req, res) => {
4040

4141
}
4242

43-
console.log(config.TOKEN_EXPIRATION);
43+
// console.log(config.TOKEN_EXPIRATION);
4444
let token = jwt.sign({
4545
userDB
4646
}, config.dev, { expiresIn: config.TOKEN_EXPIRATION });
47-
console.log(token);
47+
// console.log(token);
4848

4949
res.status(200).json({
5050
oK: true,

server/routes/products.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ app.get('/products/searchCategoryDesc/:word', (req, res) => {
115115
.populate('category')
116116
.exec((err, products) => {
117117
if (err) {
118-
return res.status(500).json({
118+
return res.status(404).json({
119119
ok: false,
120120
err
121121
})

server/server.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ mongoose
3939

4040
app.listen(config.port, () => {
4141

42-
console.log(`API Server Listening on http://localhost:${config.port}`);
43-
console.log(`Environment: ${config.dev}`);
42+
// console.log(`API Server Listening on http://localhost:${config.port}`);
43+
// console.log(`Environment: ${config.dev}`);
4444

45-
});
45+
});
46+
47+
module.exports = app;

tests/devarc.test.js

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
const config = require('../server/config/config');
2+
const request = require('supertest');
3+
const app = require('../server/server');
4+
const mongoose = require('mongoose');
5+
const categoryModel = require('../server/models/category');
6+
7+
var id;
8+
var desc;
9+
10+
let token;
11+
describe('Devarc Test... ', () => {
12+
13+
beforeAll(async (done) => {
14+
await mongoose.connect(config.urlDB, { useNewUrlParser: true, useCreateIndex: true }, (err) => {
15+
if (err) {
16+
console.error(err);
17+
process.exit(1);
18+
}
19+
done();
20+
});
21+
22+
});
23+
24+
25+
beforeAll((done) => {
26+
request(app)
27+
.post('/login')
28+
.send({
29+
30+
password: '',
31+
})
32+
.end((err, response) => {
33+
token = response.body.token; // save the token!
34+
done();
35+
});
36+
37+
});
38+
39+
40+
test('should return status 200 and text message on the initial page', async () => {
41+
const res = await request(app).get('/');
42+
expect(res.statusCode).toEqual(200);
43+
expect(res.text).toBe('Devarc is online');
44+
});
45+
46+
test('should return status 200 and return products', async () => {
47+
const res = await request(app).get('/products/');
48+
expect(res.statusCode).toEqual(200);
49+
expect(res.body.numProductos).toBeGreaterThan(0);
50+
});
51+
52+
test('should return status 200 and return product 600ec6adddd7fc0015d10a6e', async () => {
53+
const res = await request(app).get('/products/searchByID/600ec6adddd7fc0015d10a6e');
54+
expect(res.statusCode).toEqual(200);
55+
expect(res.body.numProductos).toBeGreaterThan(0);
56+
});
57+
58+
test('should return status 400 for not found product', async () => {
59+
const res = await request(app).get('/products/searchByID/AAA');
60+
expect(res.statusCode).toEqual(400);
61+
});
62+
63+
test('should return status 401 Invalid token', async () => {
64+
const res = await request(app).get('/users');
65+
expect(res.statusCode).toEqual(401);
66+
});
67+
68+
test('should return status 200 Invalid token', async () => {
69+
return request(app)
70+
.get('/users')
71+
.set('token', token)
72+
.then((response) => {
73+
expect(response.statusCode).toBe(200);
74+
expect(response.type).toBe('application/json');
75+
});
76+
});
77+
78+
79+
test('should return users', async () => {
80+
return request(app)
81+
.get('/users')
82+
.set('token', token)
83+
.then((response) => {
84+
expect(response.body.Users).toBeGreaterThan(0);
85+
expect(response.type).toBe('application/json');
86+
});
87+
});
88+
89+
test('should return Lenovo Mouse a1 product test', async () => {
90+
return request(app)
91+
.get('/products/searchByID/5f7701c99449505900f23333')
92+
.set('token', token)
93+
.then((response) => {
94+
expect(response.body.numProductos).toBe(1)
95+
expect(response.type).toBe('application/json');
96+
97+
let product = response.body.products[0];
98+
expect(product.avaiable).toEqual(true);
99+
100+
});
101+
});
102+
103+
104+
// test('should return id category of Test8', async () => {
105+
// var data = { 'description': 'Test8' };
106+
107+
// request(app)
108+
// .post('/category')
109+
// .send(data)
110+
// .then((response) => {
111+
// id = response.body.category._id;
112+
// expect(response.statusCode).toBe(201);
113+
// expect(response.body.category._id).toBe(id);
114+
// expect(response.body.category._id).not.toBeNull();
115+
// });
116+
117+
118+
// });
119+
120+
// it('should remove Category Test8 Category and return null successfully', async () => {
121+
122+
// await categoryModel.findByIdAndRemove({description : 'Test8'})
123+
// const cateDeleted = await categoryModel.findById({_id : id})
124+
// expect(cateDeleted).toBeNull() ;
125+
126+
// });
127+
128+
test('should return description pañales of id 600587e86bd460001551ce6b', async () => {
129+
let descCategory;
130+
//Category.find({})
131+
categoryModel.find({ _id : '600587e86bd460001551ce6b'})
132+
.exec((err, category) => {
133+
if (err) { expect(err).toBe(err); };
134+
descCategory = category[0].description;
135+
expect(descCategory).toEqual('pañales');
136+
});
137+
138+
});
139+
140+
it('should create & save Category Test 2 successfully', async () => {
141+
var data = { 'description': 'Test 2' };
142+
const validCategory = new categoryModel(data);
143+
const savedCategory = await validCategory.save();
144+
id = savedCategory._id;
145+
expect(savedCategory._id).toBeDefined();
146+
expect(savedCategory.description).toBe(validCategory.description);
147+
});
148+
149+
150+
it('should remove Category Test 2 Category and return null successfully', async () => {
151+
152+
await categoryModel.findByIdAndRemove({_id : id})
153+
const cateDeleted = await categoryModel.findById({_id : id})
154+
expect(cateDeleted).toBeNull() ;
155+
156+
});
157+
158+
afterAll(() => {
159+
mongoose.connection.close();
160+
});
161+
162+
163+
})

0 commit comments

Comments
 (0)