Skip to content

Commit 84770d7

Browse files
committed
disAccount
1 parent a0298aa commit 84770d7

File tree

8 files changed

+165
-90
lines changed

8 files changed

+165
-90
lines changed

package-lock.json

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"@types/jsonwebtoken": "^9.0.1",
6161
"@types/nodemailer": "^6.4.7",
6262
"@types/supertest": "^2.0.12",
63+
"@types/swagger-jsdoc": "^6.0.1",
6364
"@types/swagger-ui-express": "^4.1.3",
6465
"@typescript-eslint/eslint-plugin": "^5.50.0",
6566
"@typescript-eslint/parser": "^5.50.0",
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// TODO replace the test below with your tests
2-
import {request} from 'express'
2+
import request from 'supertest'
33
import app from '../../app'
4+
import users from '../../db/models/users'
45
import router from '../../routes/authroutes'
56
import {add, multiply} from '../../totest'
67
import supertest from 'supertest'
@@ -13,66 +14,101 @@ describe('Math functions', () => {
1314
const result = multiply(5, 3)
1415
expect(result).toEqual(15)
1516
})
17+
describe('disabling account', () => {
18+
jest.setTimeout(20000)
1619

17-
it('should add 5 by 3', () => {
18-
const result = add(5, 3)
19-
expect(result).toEqual(8)
20+
test('200 status for disabling account', async () => {
21+
const signUp = await request(app).post('/signup').send({
22+
firstName: 'Festo',
23+
lastName: 'kabab',
24+
25+
password: 'admin1',
26+
})
27+
console.log(signUp.body.token)
28+
29+
const login = await request(app).post('/login').send({
30+
31+
password: 'admin1',
32+
})
33+
const users = await request(app).get('/users')
34+
console.log(users)
35+
const user = users.body.users[0]
36+
37+
const disable = await request(app)
38+
.post(`/users/${user.id}/disable-account`)
39+
.set('Authorization', `Bearer ${login.body.token}`)
40+
.send()
41+
expect(disable.statusCode).toBe(200)
42+
})
43+
test('404 status for unexisting user', async () => {
44+
const login = await request(app).post('/login').send({
45+
46+
password: 'admin1',
47+
})
48+
console.log(login.body)
49+
const unExist = await request(app)
50+
.post(`/users/234567/disable-account`)
51+
.set('Authorization', `Bearer ${login.body.token}`)
52+
.send()
53+
console.log(unExist.body)
54+
expect(unExist.statusCode).toBe(404)
55+
})
2056
})
21-
})
22-
// reset password coontroller tests
23-
describe('reset password', () => {
24-
describe('send link to email', () => {
25-
test('incase of unregistered email', async () => {
57+
// reset password coontroller tests
58+
describe('reset password', () => {
59+
describe('send link to email', () => {
60+
test('incase of unregistered email', async () => {
61+
const response = await supertest(app)
62+
.post('/resetpassword/link')
63+
.send({email: '[email protected]'})
64+
expect(response.status).toBe(400)
65+
}, 10000) // timeout 10 seconds
66+
})
67+
test('incase of a registered email', async () => {
2668
const response = await supertest(app)
2769
.post('/resetpassword/link')
28-
.send({email: '[email protected]'})
29-
expect(response.status).toBe(400)
30-
}, 10000) // timeout 10 seconds
31-
})
32-
test('incase of a registered email', async () => {
33-
const response = await supertest(app)
34-
.post('/resetpassword/link')
35-
.send({email: '[email protected]'})
36-
expect(response.status).toBe(200)
37-
}, 20000)
38-
test('incase invalid email input', async () => {
39-
const response = await supertest(app)
40-
.post('/resetpassword/link')
41-
.send({email: 'rukundjoseph'})
42-
expect(response.status).toBe(400)
43-
}, 20000)
44-
describe('add token and change password', () => {
45-
test('incase incorrect token', async () => {
46-
const response = await supertest(app)
47-
.patch('/changepassword/[email protected]/65328dba23')
48-
.send({newpassword: 'newpassword', confirmpass: 'newpassword'})
49-
expect(response.status).toBe(400)
70+
.send({email: '[email protected]'})
71+
expect(response.status).toBe(200)
5072
}, 20000)
51-
test('incase incorrect token', async () => {
73+
test('incase invalid email input', async () => {
5274
const response = await supertest(app)
53-
.patch('/changepassword/[email protected]/65328dba23')
54-
.send({newpassword: 'newpassword', confirmpass: 'newpassword'})
75+
.post('/resetpassword/link')
76+
.send({email: 'rukundjoseph'})
5577
expect(response.status).toBe(400)
5678
}, 20000)
57-
test('incase of a unmatching passwords', async () => {
58-
const user: any = await USER.findOne({
59-
where: {email: '[email protected]'},
79+
describe('add token and change password', () => {
80+
test('incase incorrect token', async () => {
81+
const response = await supertest(app)
82+
.patch('/changepassword/[email protected]/65328dba23')
83+
.send({newpassword: 'newpassword', confirmpass: 'newpassword'})
84+
expect(response.status).toBe(400)
85+
}, 20000)
86+
test('incase incorrect token', async () => {
87+
const response = await supertest(app)
88+
.patch('/changepassword/[email protected]/65328dba23')
89+
.send({newpassword: 'newpassword', confirmpass: 'newpassword'})
90+
expect(response.status).toBe(400)
91+
}, 20000)
92+
test('incase of a unmatching passwords', async () => {
93+
const user: any = await USER.findOne({
94+
where: {email: '[email protected]'},
95+
})
96+
const token: any = await Tokens.findOne({where: {userId: `${user.id}`}})
97+
const response = await supertest(app)
98+
.patch(`/changepassword/[email protected]/${token.token}`)
99+
.send({newpassword: 'newpas', confirmpass: 'newpaa'})
100+
expect(response.status).toBe(400)
60101
})
61-
const token: any = await Tokens.findOne({where: {userId: `${user.id}`}})
62-
const response = await supertest(app)
63-
.patch(`/changepassword/[email protected]/${token.token}`)
64-
.send({newpassword: 'newpas', confirmpass: 'newpaa'})
65-
expect(response.status).toBe(400)
66-
})
67-
test('incase of a valid token and email', async () => {
68-
const user: any = await USER.findOne({
69-
where: {email: '[email protected]'},
102+
test('incase of a valid token and email', async () => {
103+
const user: any = await USER.findOne({
104+
where: {email: '[email protected]'},
105+
})
106+
const token: any = await Tokens.findOne({where: {userId: `${user.id}`}})
107+
const response = await supertest(app)
108+
.patch(`/changepassword/[email protected]/${token.token}`)
109+
.send({newpassword: 'newpas', confirmpass: 'newpas'})
110+
expect(response.status).toBe(200)
70111
})
71-
const token: any = await Tokens.findOne({where: {userId: `${user.id}`}})
72-
const response = await supertest(app)
73-
.patch(`/changepassword/[email protected]/${token.token}`)
74-
.send({newpassword: 'newpas', confirmpass: 'newpas'})
75-
expect(response.status).toBe(200)
76112
})
77113
})
78114
})

src/controllers/authController.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class auth {
261261
} else {
262262
res.status(404).json({
263263
statusCode: 404,
264-
message: 'Product with provided id is not exist',
264+
message: 'User with provided id is not exist',
265265
})
266266
}
267267
} catch (error: any) {

src/db/models/users.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
'use strict';
21
const {
32
Model
43
} = require('sequelize');

src/docs/swagger.ts

+28-28
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
import { Application } from 'express'
1+
import {Application} from 'express'
22
import swaggerJsdoc from 'swagger-jsdoc'
33
import swaggerUi from 'swagger-ui-express'
44

5-
const options= {
6-
definition: {
7-
openapi: '3.0.0',
8-
info: {
9-
title: 'E-commerce API',
10-
description: 'e-commerce API',
11-
version: '1.0.0',
12-
},
13-
components: {
14-
securitySchemes: {
15-
authsecurity: {
16-
type: 'http',
17-
scheme: 'bearer',
18-
in: 'header',
19-
bearerFormat: 'JWT'
20-
}
21-
}
5+
const options = {
6+
definition: {
7+
openapi: '3.0.0',
8+
info: {
9+
title: 'E-commerce API',
10+
description: 'e-commerce API',
11+
version: '1.0.0',
12+
},
13+
components: {
14+
securitySchemes: {
15+
authsecurity: {
16+
type: 'http',
17+
scheme: 'bearer',
18+
in: 'header',
19+
bearerFormat: 'JWT',
20+
},
2221
},
23-
security: [
24-
{
25-
authsecurity: []
26-
}
27-
],
2822
},
29-
// looks for configuration in specified directories
30-
apis: ['./src/routes/*.ts'],
31-
}
32-
const swaggerSpec = swaggerJsdoc(options)
23+
security: [
24+
{
25+
authsecurity: [],
26+
},
27+
],
28+
},
29+
// looks for configuration in specified directories
30+
apis: ['./src/routes/*.ts'],
31+
}
32+
const swaggerSpec = swaggerJsdoc(options)
3333
function swaggerDocs(app: Application) {
3434
// Swagger Page
3535
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec))
@@ -39,4 +39,4 @@ function swaggerDocs(app: Application) {
3939
res.send(swaggerSpec)
4040
})
4141
}
42-
export default swaggerDocs
42+
export default swaggerDocs

src/routes/authroutes.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@ router.get('/users', auth.getAlluser)
4747
router.delete('/delete/:id', auth.deleteUser)
4848
router.get('/sendcode/:phone', auth.sendCode)
4949
router.get('/verify/:phone/:code', auth.verify2FA)
50-
// router.post('/logout', auth.logout)
5150
router.post('/logout', auth.logout)
5251
router.post('/authorize', auth.authorize)
5352
router.post('/resetpassword/link', resetpass.sendlink)
5453
router.patch('/changepassword/:useremail/:token', resetpass.changepassword)
5554

5655
/* this delete user route is not protected it is just for testing and setting up the project*/
57-
router.get(
56+
router.post(
5857
'/users/:id/disable-account',
5958
verifyToken,
6059
isAdmin,

src/routes/docs.ts

+34-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import auth from '../controllers/authController'
2+
import signupValidation from '../middlewares/signupValidation'
3+
import router from './authroutes'
4+
15
/**
26
* @swagger
37
* /signup:
@@ -12,26 +16,27 @@
1216
* application/json:
1317
* schema:
1418
* type: object
15-
* required:
19+
* required:
1620
* - email
1721
* - firstName
1822
* - lastName
19-
* - password
20-
* properties:
23+
* - password
24+
* properties:
2125
* email:
2226
* type: string
2327
* firstname:
2428
* type: string
2529
* lastName:
2630
* type: string
27-
* password:
28-
* type : string
31+
* password:
32+
* type : string
2933
* responses:
3034
* 201:
3135
* description: successfully logged in;
32-
*
36+
*
3337
* */
3438

39+
router.post('/signup', signupValidation, auth.signup)
3540

3641
/**
3742
* @swagger
@@ -137,6 +142,7 @@
137142

138143
/**
139144
* @swagger
145+
<<<<<<< HEAD
140146
* /profile/edit:
141147
* patch:
142148
* summary: Update user profile details
@@ -257,4 +263,25 @@
257263
* description: An array of user profile objects
258264
* '500':
259265
* description: Internal server error
260-
*/
266+
*/
267+
=======
268+
* /users/{id}/disable-account:
269+
* post:
270+
* tags:
271+
* - users
272+
* summary:
273+
* security: []
274+
* consumes:
275+
* - application/json
276+
* parameters:
277+
* - name: id
278+
*
279+
* in: path
280+
* required: true
281+
* responses:
282+
* 200:
283+
* description: Successfully Deleted.
284+
* 400:
285+
* description: Bad request.
286+
* */
287+
>>>>>>> 333dbd3 (disAccount)

0 commit comments

Comments
 (0)