@@ -2,20 +2,31 @@ const request = require('supertest');
2
2
const express = require ( 'express' ) ;
3
3
const mongoose = require ( 'mongoose' ) ;
4
4
const { MongoMemoryServer } = require ( 'mongodb-memory-server' ) ;
5
+ const jwt = require ( 'jsonwebtoken' ) ;
5
6
const DefectConfigModel = require ( '../models/DefectConfigModel' ) ;
6
- const defectConfigRoutes = require ( '../routes/defectConfigRoutes' ) ;
7
+ const { getDefectConfig, updateDefectConfig } = require ( '../controllers/defectConfigsController' ) ;
8
+ const { auth } = require ( '../middlewares/auth' ) ;
7
9
8
10
// Set up an Express app for testing
9
11
const app = express ( ) ;
10
12
app . use ( express . json ( ) ) ;
11
- app . use ( '/defect-config' , defectConfigRoutes ) ;
13
+
14
+ // Apply auth middleware
15
+ app . get ( '/defect-config' , auth , getDefectConfig ) ;
16
+ app . post ( '/defect-config' , auth , updateDefectConfig ) ;
17
+
18
+ const JWT_SECRET = 'test_secret' ;
12
19
13
20
let mongoServer ;
21
+ let userToken ;
14
22
15
23
beforeAll ( async ( ) => {
16
24
mongoServer = await MongoMemoryServer . create ( ) ;
17
25
const uri = mongoServer . getUri ( ) ;
18
- await mongoose . connect ( uri , { useNewUrlParser : true , useUnifiedTopology : true } ) ;
26
+ await mongoose . connect ( uri , { } ) ;
27
+ process . env . JWT_SECRET = JWT_SECRET ;
28
+ // Generate a mock token for a regular user
29
+ userToken = jwt . sign ( { email :
'[email protected] ' , isAdmin :
false } , process . env . JWT_SECRET ) ;
19
30
} ) ;
20
31
21
32
afterAll ( async ( ) => {
@@ -41,64 +52,86 @@ beforeEach(async () => {
41
52
42
53
describe ( 'Defect Config Endpoints' , ( ) => {
43
54
describe ( 'GET /defect-config' , ( ) => {
44
- it ( 'should return the defect configuration ' , async ( ) => {
55
+ it ( 'should return 401 if no token is provided ' , async ( ) => {
45
56
const res = await request ( app ) . get ( '/defect-config' ) ;
57
+ expect ( res . statusCode ) . toBe ( 401 ) ;
58
+ expect ( res . body . message ) . toBe ( 'Access denied. No token provided.' ) ;
59
+ } ) ;
60
+
61
+ it ( 'should return 200 and the defect config if token is valid' , async ( ) => {
62
+ const res = await request ( app )
63
+ . get ( '/defect-config' )
64
+ . set ( 'Authorization' , `Bearer ${ userToken } ` ) ;
46
65
47
66
expect ( res . statusCode ) . toBe ( 200 ) ;
48
- expect ( res . body ) . toHaveProperty ( 'greening' , 0.1 ) ;
49
- expect ( res . body ) . toHaveProperty ( 'dryRot' , 0.2 ) ;
50
- expect ( res . body ) . toHaveProperty ( 'wetRot' , 0.3 ) ;
51
- expect ( res . body ) . toHaveProperty ( 'wireWorm' , 0.4 ) ;
52
- expect ( res . body ) . toHaveProperty ( 'malformed' , 0.5 ) ;
53
- expect ( res . body ) . toHaveProperty ( 'growthCrack' , 0.6 ) ;
54
- expect ( res . body ) . toHaveProperty ( 'mechanicalDamage' , 0.7 ) ;
55
- expect ( res . body ) . toHaveProperty ( 'dirtClod' , 0.8 ) ;
56
- expect ( res . body ) . toHaveProperty ( 'stone' , 0.9 ) ;
67
+ expect ( res . body ) . toEqual ( expect . objectContaining ( {
68
+ greening : 0.1 ,
69
+ dryRot : 0.2 ,
70
+ wetRot : 0.3 ,
71
+ wireWorm : 0.4 ,
72
+ malformed : 0.5 ,
73
+ growthCrack : 0.6 ,
74
+ mechanicalDamage : 0.7 ,
75
+ dirtClod : 0.8 ,
76
+ stone : 0.9 ,
77
+ } ) ) ;
57
78
} ) ;
58
79
} ) ;
59
80
60
81
describe ( 'POST /defect-config' , ( ) => {
61
- it ( 'should update the defect configuration successfully' , async ( ) => {
82
+ it ( 'should return 401 if no token is provided' , async ( ) => {
83
+ const res = await request ( app )
84
+ . post ( '/defect-config' )
85
+ . send ( { greening : 0.15 } ) ;
86
+
87
+ expect ( res . statusCode ) . toBe ( 401 ) ;
88
+ expect ( res . body . message ) . toBe ( 'Access denied. No token provided.' ) ;
89
+ } ) ;
90
+
91
+ it ( 'should update the defect config if token is valid' , async ( ) => {
62
92
const updateData = {
63
93
greening : 0.15 ,
64
94
dryRot : 0.25 ,
65
- wetRot : 0.35 ,
66
95
} ;
67
96
68
97
const res = await request ( app )
69
98
. post ( '/defect-config' )
99
+ . set ( 'Authorization' , `Bearer ${ userToken } ` )
70
100
. send ( updateData ) ;
71
101
72
102
expect ( res . statusCode ) . toBe ( 200 ) ;
73
103
expect ( res . body . message ) . toBe ( 'Defect configuration updated successfully' ) ;
74
- expect ( res . body . data . greening ) . toBe ( 0.15 ) ;
75
- expect ( res . body . data . dryRot ) . toBe ( 0.25 ) ;
76
- expect ( res . body . data . wetRot ) . toBe ( 0.35 ) ;
104
+ expect ( res . body . data ) . toEqual ( expect . objectContaining ( updateData ) ) ;
105
+
106
+ const updatedConfig = await DefectConfigModel . findOne ( ) ;
107
+ expect ( updatedConfig . greening ) . toBe ( 0.15 ) ;
108
+ expect ( updatedConfig . dryRot ) . toBe ( 0.25 ) ;
77
109
} ) ;
78
110
79
- it ( 'should return 404 if no defect configuration exists ' , async ( ) => {
111
+ it ( 'should return 404 if defect config not found ' , async ( ) => {
80
112
await DefectConfigModel . deleteMany ( { } ) ;
81
113
82
114
const res = await request ( app )
83
115
. post ( '/defect-config' )
116
+ . set ( 'Authorization' , `Bearer ${ userToken } ` )
84
117
. send ( { greening : 0.15 } ) ;
85
118
86
119
expect ( res . statusCode ) . toBe ( 404 ) ;
87
120
expect ( res . body . message ) . toBe ( 'Defect configuration not found' ) ;
88
121
} ) ;
89
122
90
- it ( 'should return 500 if validation fails ' , async ( ) => {
91
- const invalidUpdateData = {
92
- greening : 1.5 , // Invalid: exceeds max value of 1
93
- } ;
123
+ it ( 'should handle errors during update ' , async ( ) => {
124
+ jest . spyOn ( DefectConfigModel . prototype , 'save' ) . mockImplementationOnce ( ( ) => {
125
+ throw new Error ( 'Database error' ) ;
126
+ } ) ;
94
127
95
128
const res = await request ( app )
96
129
. post ( '/defect-config' )
97
- . send ( invalidUpdateData ) ;
130
+ . set ( 'Authorization' , `Bearer ${ userToken } ` )
131
+ . send ( { greening : 0.15 } ) ;
98
132
99
133
expect ( res . statusCode ) . toBe ( 500 ) ;
100
134
expect ( res . body . message ) . toBe ( 'Error updating defect configuration' ) ;
101
- expect ( res . body . error ) . toContain ( 'validation failed' ) ;
102
135
} ) ;
103
136
} ) ;
104
137
} ) ;
0 commit comments