|
| 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