Skip to content

Commit 6ed56a4

Browse files
committed
admin routes tesed
1 parent 29513dd commit 6ed56a4

File tree

8 files changed

+259
-15
lines changed

8 files changed

+259
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/
2+
jest_0/

config/default.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"jwtPrivatekey": ""
2+
"jwtPrivatekey": "",
3+
"db": "mongodb://localhost:27017/turing"
34
}

config/test.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"jwtPrivatekey": "hello",
3+
"db": "mongodb://localhost:27017/turing_test"
4+
}

controllers/admin.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const Admin = require('../models/Admin');
22
const joi = require('joi');
33
const bcrypt = require('bcrypt');
4+
const mongoose = require('mongoose')
45

56
const registerValidator = (data) => {
67
const schema = {
7-
name: joi.string().max(55).required(),
8-
userName: joi.string().max(100).required(),
9-
password: joi.string().required(),
8+
name: joi.string().min(3).max(55).required(),
9+
userName: joi.string().min(3).max(55).required(),
10+
password: joi.string().min(3).required(),
1011
confirmPassword: joi.string().required()
1112
}
1213

@@ -15,10 +16,10 @@ const registerValidator = (data) => {
1516

1617
const updateValidator = data => {
1718
const schema = {
18-
name: joi.string().max(55).required(),
19-
userName: joi.string().max(100).required(),
20-
oldPassword: joi.string().required(),
21-
newPassword: joi.string().required()
19+
name: joi.string().min(3).max(55).required(),
20+
userName: joi.string().min(3).max(100).required(),
21+
oldPassword: joi.string().min(3).required(),
22+
newPassword: joi.string().min(3).required()
2223
}
2324
return joi.validate(data,schema);
2425
}
@@ -92,6 +93,9 @@ const getMe = async (req,res) => {
9293
}
9394

9495
const deleteAdmin = async (req,res) => {
96+
if(!mongoose.Types.ObjectId.isValid(req.params.adminId)){
97+
return res.status(400).send("id is not valid")
98+
}
9599
const admin = await Admin.findByIdAndDelete(req.params.adminId);
96100
if(!admin) return res.status(500).send("something wrong");
97101
return res.status(200).send(admin);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"start": "nodemon server.js"
7+
"start": "nodemon server.js",
8+
"test": "jest --watchAll --verbose"
89
},
910
"keywords": [],
1011
"author": "",

server.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const express = require('express');
22
const app = express();
3-
const port = process.env.PORT || 1000 ;
43

54
require('./startup/db')();
65
require('./startup/config')();
76
require('./startup/routes')(app);
87

98
app.use(express.static('uploads'))
109

11-
app.listen(port,() => {
12-
console.log(`port is running port ${port}`)
10+
const PORT = process.env.PORT || 1000;
11+
const server = app.listen(PORT,() => {
12+
console.log(`Listening on port ${PORT}`);
1313
})
14+
module.exports = server

startup/db.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const mongoose = require('mongoose')
2-
2+
const config = require('config')
33

44
module.exports = function() {
55

66
mongoose.connect(
7-
'mongodb://localhost:27017/turing',
7+
config.get('db'),
88
{useNewUrlParser: true},
99
() => {
10-
console.log('Database connection established succesfully');
10+
console.log('Database connection established '+config.get('db'));
1111
}
1212
)
1313
}

tests/integration/admin.test.js

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
const Admin = require('../../models/Admin');
2+
const request = require('supertest');
3+
const config = require('config')
4+
const jwt = require('jsonwebtoken')
5+
let server;
6+
7+
describe('Admin Routes',() => {
8+
beforeEach(() => {
9+
server = require('../../server')
10+
userName = 'faisal'
11+
})
12+
afterEach(async () => {
13+
server.close()
14+
await Admin.remove({})
15+
})
16+
17+
let userName;
18+
19+
const exec = async () => {
20+
return await request(server)
21+
.post('/api/admin/register')
22+
.send({userName,name: 'Sharia Emon',password: 'faisal',confirmPassword: 'faisal'})
23+
}
24+
25+
const loginExec = async () => {
26+
return await request(server)
27+
.post('/api/admin/login')
28+
.send({userName,password: 'faisal'})
29+
}
30+
31+
describe('Post /api/admin/',() => {
32+
33+
describe('/register',() => {
34+
it('should return status 201 if all input is ok as expected',async () => {
35+
const res = await exec();
36+
expect(res.status).toBe(201)
37+
})
38+
39+
it('should save data',async () => {
40+
await exec();
41+
const data = await Admin.findOne({userName: 'faisal'})
42+
expect(data).toHaveProperty('_id')
43+
expect(data).toHaveProperty('userName','faisal')
44+
})
45+
46+
it('should return 400 error if username input value length is less than 3 characters',async () => {
47+
userName = 'oo'
48+
const res = await exec();
49+
expect(res.status).toBe(400)
50+
})
51+
52+
it('should return 400 error if username input value length is greater than 55 characters',async () => {
53+
userName = new Array(57).join('a')
54+
const res = await exec();
55+
expect(res.status).toBe(400)
56+
})
57+
58+
it('should return 400 error if userName is not defined',async () => {
59+
userName = ''
60+
const res = await exec();
61+
expect(res.status).toBe(400)
62+
})
63+
})
64+
65+
describe('/login',() => {
66+
67+
it('should status code 200 if username and password match',async () => {
68+
await exec();
69+
const res = await loginExec()
70+
expect(res.status).toBe(200)
71+
expect(res.error).toBeFalsy()
72+
})
73+
74+
it('should return 400 error if username or password not defined',async () => {
75+
await exec();
76+
userName = ''
77+
const res = await loginExec()
78+
expect(res.status).toBe(400)
79+
expect(res.error).toBeTruthy()
80+
})
81+
82+
it('should return valid jwt token',async () => {
83+
await exec();
84+
const res = await loginExec()
85+
const token = res.text;
86+
const decoded = jwt.verify(token,config.get('jwtPrivatekey'))
87+
expect(res.status).toBe(200)
88+
expect(decoded).toHaveProperty('_id')
89+
})
90+
91+
it('should return 400 error if userName or password is invalid',async () => {
92+
await exec();
93+
userName = 'faissal'
94+
const res = await loginExec()
95+
expect(res.status).toBe(400)
96+
})
97+
98+
99+
})
100+
})
101+
102+
describe('Get /api/admin',() => {
103+
104+
it('should return 200',async () => {
105+
const res = await request(server).get('/api/admin')
106+
expect(res.status).toBe(200)
107+
})
108+
109+
it('Get/ me: should return loged admin data',async () => {
110+
await exec();
111+
const login = await loginExec()
112+
const res = await request(server)
113+
.get('/api/admin/me')
114+
.set('admin_token',login.text)
115+
expect(res.status).toBe(200)
116+
expect(res.body).toHaveProperty('_id')
117+
expect(res.body).toHaveProperty('userName','faisal')
118+
})
119+
120+
it('should return 401 if token is not provided',async () => {
121+
await exec();
122+
const login = await loginExec()
123+
const res = await request(server)
124+
.get('/api/admin/me')
125+
expect(res.status).toBe(401)
126+
})
127+
128+
it('should return 400 if token is not valid',async () => {
129+
await exec();
130+
const login = await loginExec()
131+
const res = await request(server)
132+
.get('/api/admin/me')
133+
.set('admin_token','hads32')
134+
expect(res.status).toBe(400)
135+
})
136+
137+
138+
})
139+
140+
describe('Put /api/admin',() => {
141+
let name;
142+
let userName;
143+
144+
const updateExec = async () => {
145+
const admin = await exec()
146+
const login = await loginExec();
147+
return await request(server)
148+
.put('/api/admin')
149+
.set('admin_token',login.text)
150+
.send({userName,name,oldPassword: 'faisal',newPassword: 'farhad'})
151+
}
152+
153+
beforeEach(() => {
154+
name = 'Sharia Emon Faisal'
155+
userName = 'faisal'
156+
})
157+
158+
it('should return 200 if everything is ok as expected',async () => {
159+
const res = await updateExec()
160+
expect(res.status).toBe(200)
161+
expect(res.body).toHaveProperty('userName','faisal')
162+
expect(res.body).toHaveProperty('name','Sharia Emon Faisal')
163+
})
164+
165+
it('should return 400 error if input length is less than 3 characters',async () => {
166+
name = 'fa'
167+
const res = await updateExec()
168+
expect(res.status).toBe(400)
169+
})
170+
171+
it('should return 401 error if token is not provided',async () => {
172+
const admin = await exec()
173+
const login = await loginExec();
174+
const res = await request(server)
175+
.put('/api/admin')
176+
.send({userName,name,oldPassword: 'faisal',newPassword: 'farhad'})
177+
expect(res.status).toBe(401)
178+
})
179+
180+
it('should return 400 error if token is not valid',async () => {
181+
const admin = await exec()
182+
const login = await loginExec();
183+
const res = await request(server)
184+
.put('/api/admin')
185+
.set('admin_token','invalid token')
186+
.send({userName,name,oldPassword: 'faisal',newPassword: 'farhad'})
187+
expect(res.status).toBe(400)
188+
})
189+
})
190+
191+
describe('Delete /api/admin',() => {
192+
193+
it('should return 200 if data has been deleted',async () => {
194+
await exec()
195+
const login = await loginExec()
196+
const me = await request(server)
197+
.get('/api/admin/me')
198+
.set('admin_token',login.text)
199+
const res = await request(server)
200+
.delete('/api/admin/'+me.body._id)
201+
202+
expect(res.status).toBe(200)
203+
})
204+
205+
it('should return 400 error if ObjectId is invalid',async () => {
206+
await exec()
207+
const login = await loginExec()
208+
const me = await request(server)
209+
.get('/api/admin/me')
210+
.set('admin_token',login.text)
211+
const res = await request(server)
212+
.delete('/api/admin/23')
213+
214+
expect(res.status).toBe(400)
215+
})
216+
217+
it('should return 404 error if ObjectId is not provided',async () => {
218+
await exec()
219+
const login = await loginExec()
220+
const me = await request(server)
221+
.get('/api/admin/me')
222+
.set('admin_token',login.text)
223+
const res = await request(server)
224+
.delete('/api/admin/')
225+
226+
expect(res.status).toBe(404)
227+
})
228+
229+
})
230+
231+
232+
})

0 commit comments

Comments
 (0)