@@ -2,6 +2,7 @@ import chai from 'chai';
2
2
import chaiHttp from 'chai-http' ;
3
3
import request from 'supertest' ;
4
4
import app from '../index.js' ;
5
+ import bcrypt from 'bcryptjs' ;
5
6
6
7
chai . use ( chaiHttp ) ;
7
8
const { expect } = chai ;
@@ -43,7 +44,7 @@ describe('User API', () => {
43
44
44
45
describe ( 'GET /users/:id' , ( ) => {
45
46
it ( 'should return a user by ID with a valid token' , async ( ) => {
46
- const userId = 'some-valid-user-id' ; // Replace with an actual user ID
47
+ const userId = 9 ; // Replace with an actual valid numeric user ID in your database
47
48
const res = await request ( app )
48
49
. get ( `/users/${ userId } ` )
49
50
. set ( 'Authorization' , `Bearer ${ token } ` ) ;
@@ -55,7 +56,7 @@ describe('User API', () => {
55
56
} ) ;
56
57
57
58
it ( 'should return 404 if user not found' , async ( ) => {
58
- const invalidUserId = 'invalid-user-id' ;
59
+ const invalidUserId = 999999 ; // Replace with a numeric ID that doesn't exist in your database
59
60
const res = await request ( app )
60
61
. get ( `/users/${ invalidUserId } ` )
61
62
. set ( 'Authorization' , `Bearer ${ token } ` ) ;
@@ -65,7 +66,7 @@ describe('User API', () => {
65
66
} ) ;
66
67
67
68
it ( 'should return 401 without a token' , async ( ) => {
68
- const userId = 'some- valid- user-id' ;
69
+ const userId = 1 ; // Replace with an actual valid numeric user ID in your database
69
70
const res = await request ( app )
70
71
. get ( `/users/${ userId } ` ) ;
71
72
@@ -74,12 +75,16 @@ describe('User API', () => {
74
75
} ) ;
75
76
} ) ;
76
77
78
+ /*
77
79
describe('POST /users', () => {
78
80
it('should create a new user with valid data and token', async () => {
79
81
const res = await request(app)
80
82
.post('/users')
81
83
.set('Authorization', `Bearer ${token}`)
82
- . send ( { name :
'John Doe' , email :
'[email protected] ' , password :
'password123' } ) ;
84
+ .field('name', 'John Doe')
85
+ .field('email', '[email protected] ')
86
+ .field('password', 'password123')
87
+ .attach('picture', 'test/fixtures/sample-profile-pic.jpg'); // Attach a file
83
88
84
89
expect(res.status).to.equal(201);
85
90
expect(res.body).to.have.property('message', 'User added');
@@ -90,7 +95,7 @@ describe('User API', () => {
90
95
.post('/users')
91
96
.set('Authorization', `Bearer ${token}`)
92
97
.send({ name: 'JD', email: 'not-an-email', password: '123' });
93
-
98
+
94
99
expect(res.status).to.equal(400);
95
100
expect(res.body).to.have.property('error').that.includes('"name" length must be at least 3 characters long');
96
101
});
@@ -99,103 +104,108 @@ describe('User API', () => {
99
104
const res = await request(app)
100
105
.post('/users')
101
106
.send({ name: 'Jane Doe', email: 'jane.doe@example .com', password: 'password123' });
102
-
107
+
103
108
expect(res.status).to.equal(401);
104
109
expect(res.body).to.have.property('error', 'Unauthorized');
105
110
});
106
111
});
112
+ */
107
113
108
114
describe ( 'PUT /users/:id' , ( ) => {
109
115
it ( 'should update a user with valid data' , async ( ) => {
110
- const userId = 'some-valid-user-id' ; // Replace with an actual user ID
111
- const updatedData = { name :
'John Updated' , email :
'[email protected] ' } ;
116
+ const userId = 44 ; // Use a valid integer ID
117
+ const updatedData = { name :
'John Updated' , email :
'[email protected] ' , password : 'password123' } ;
112
118
113
119
const res = await request ( app )
114
- . put ( `/users/${ userId } ` )
115
- . set ( 'Authorization' , `Bearer ${ token } ` )
116
- . send ( updatedData ) ;
120
+ . put ( `/users/${ userId } ` )
121
+ . set ( 'Authorization' , `Bearer ${ token } ` )
122
+ . send ( updatedData ) ;
117
123
118
124
expect ( res . status ) . to . equal ( 200 ) ;
119
125
expect ( res . body ) . to . have . property ( 'message' , `User modified with ID: ${ userId } ` ) ;
120
126
expect ( res . body ) . to . have . property ( 'user' ) ;
121
127
expect ( res . body . user ) . to . have . property ( 'name' , 'John Updated' ) ;
122
128
expect ( res . body . user ) . to . have . property ( 'email' , '[email protected] ' ) ;
129
+
130
+ const isPasswordCorrect = await bcrypt . compare ( updatedData . password , res . body . user . password ) ;
131
+ expect ( isPasswordCorrect ) . to . be . true ;
123
132
} ) ;
124
133
125
134
it ( 'should return 400 for invalid user data' , async ( ) => {
126
- const userId = 'some- valid- user-id' ;
127
- const updatedData = { name : 'JD' , email : 'not-an-email' } ;
135
+ const userId = 44 ; // Use a valid user ID
136
+ const updatedData = { name : 'JD' , email : 'not-an-email' , password : '123 ' } ;
128
137
129
- const res = await request ( app )
130
- . put ( `/users/${ userId } ` )
131
- . set ( 'Authorization' , `Bearer ${ token } ` )
132
- . send ( updatedData ) ;
138
+ const res = await request ( app )
139
+ . put ( `/users/${ userId } ` )
140
+ . set ( 'Authorization' , `Bearer ${ token } ` )
141
+ . send ( updatedData ) ;
133
142
134
- expect ( res . status ) . to . equal ( 400 ) ;
135
- expect ( res . body ) . to . have . property ( 'error' ) . that . includes ( 'Invalid email format' ) ;
143
+ expect ( res . status ) . to . equal ( 400 ) ;
144
+ expect ( res . body ) . to . have . property ( 'error' ) . that . includes ( 'Invalid email format' ) ;
136
145
} ) ;
137
146
138
147
it ( 'should return 404 if user not found' , async ( ) => {
139
- const invalidUserId = 'invalid- user-id' ;
140
- const updatedData = { name :
'Nonexistent User' , email :
'[email protected] ' } ;
148
+ const invalidUserId = 9999 ; // Use a non-existing user ID
149
+ const updatedData = { name :
'Nonexistent User' , email :
'[email protected] ' , password : 'password123 ' } ;
141
150
142
- const res = await request ( app )
143
- . put ( `/users/${ invalidUserId } ` )
144
- . set ( 'Authorization' , `Bearer ${ token } ` )
145
- . send ( updatedData ) ;
151
+ const res = await request ( app )
152
+ . put ( `/users/${ invalidUserId } ` )
153
+ . set ( 'Authorization' , `Bearer ${ token } ` )
154
+ . send ( updatedData ) ;
146
155
147
- expect ( res . status ) . to . equal ( 404 ) ;
148
- expect ( res . body ) . to . have . property ( 'error' , 'User not found' ) ;
156
+ expect ( res . status ) . to . equal ( 404 ) ;
157
+ expect ( res . body ) . to . have . property ( 'error' , 'User not found' ) ;
149
158
} ) ;
150
159
151
160
it ( 'should return 401 without a token' , async ( ) => {
152
- const userId = 'some- valid- user-id' ;
153
- const updatedData = { name :
'John Doe' , email :
'[email protected] ' } ;
161
+ const userId = 44 ; // Use a valid user ID
162
+ const updatedData = { name :
'John Doe' , email :
'[email protected] ' , password : 'password123 ' } ;
154
163
155
- const res = await request ( app )
156
- . put ( `/users/${ userId } ` )
157
- . send ( updatedData ) ;
164
+ const res = await request ( app )
165
+ . put ( `/users/${ userId } ` )
166
+ . send ( updatedData ) ;
158
167
159
- expect ( res . status ) . to . equal ( 401 ) ;
160
- expect ( res . body ) . to . have . property ( 'error' , 'Unauthorized' ) ;
168
+ expect ( res . status ) . to . equal ( 401 ) ;
169
+ expect ( res . body ) . to . have . property ( 'error' , 'Unauthorized' ) ;
161
170
} ) ;
162
171
} ) ;
163
172
173
+
164
174
describe ( 'DELETE /users/:id' , ( ) => {
165
175
it ( 'should delete a user with valid ID' , async ( ) => {
166
- const userId = 'some-valid-user-id' ; // Replace with an actual user ID
167
-
176
+ const userId = 45 ; // Replace with an actual user ID (integer)
177
+
168
178
const res = await request ( app )
169
179
. delete ( `/users/${ userId } ` )
170
180
. set ( 'Authorization' , `Bearer ${ token } ` ) ;
171
-
181
+
172
182
expect ( res . status ) . to . equal ( 200 ) ;
173
- expect ( res . body ) . to . have . property ( 'message' , 'User deleted' ) ;
183
+ expect ( res . body ) . to . have . property ( 'message' , 'User soft deleted with ID: ' + userId ) ;
174
184
} ) ;
175
185
176
186
it ( 'should return 404 if user not found' , async ( ) => {
177
- const invalidUserId = 'invalid-user-id' ;
178
-
187
+ const invalidUserId = 999999 ; // Use a non-existent integer ID
188
+
179
189
const res = await request ( app )
180
190
. delete ( `/users/${ invalidUserId } ` )
181
191
. set ( 'Authorization' , `Bearer ${ token } ` ) ;
182
-
192
+
183
193
expect ( res . status ) . to . equal ( 404 ) ;
184
194
expect ( res . body ) . to . have . property ( 'error' , 'User not found' ) ;
185
195
} ) ;
186
196
187
197
it ( 'should return 401 without a token' , async ( ) => {
188
- const userId = 'some-valid-user-id' ;
189
-
198
+ const userId = 45 ; // Replace with a valid integer ID
199
+
190
200
const res = await request ( app )
191
201
. delete ( `/users/${ userId } ` ) ;
192
-
202
+
193
203
expect ( res . status ) . to . equal ( 401 ) ;
194
204
expect ( res . body ) . to . have . property ( 'error' , 'Unauthorized' ) ;
195
205
} ) ;
196
206
} ) ;
197
207
198
-
208
+
199
209
// Close the server after tests
200
210
after ( async ( ) => {
201
211
if ( server ) {
0 commit comments