@@ -41,6 +41,39 @@ describe('User API', () => {
41
41
} ) ;
42
42
} ) ;
43
43
44
+ describe ( 'GET /users/:id' , ( ) => {
45
+ 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 res = await request ( app )
48
+ . get ( `/users/${ userId } ` )
49
+ . set ( 'Authorization' , `Bearer ${ token } ` ) ;
50
+
51
+ expect ( res . status ) . to . equal ( 200 ) ;
52
+ expect ( res . body ) . to . have . property ( 'id' , userId ) ;
53
+ expect ( res . body ) . to . have . property ( 'name' ) ;
54
+ expect ( res . body ) . to . have . property ( 'email' ) ;
55
+ } ) ;
56
+
57
+ it ( 'should return 404 if user not found' , async ( ) => {
58
+ const invalidUserId = 'invalid-user-id' ;
59
+ const res = await request ( app )
60
+ . get ( `/users/${ invalidUserId } ` )
61
+ . set ( 'Authorization' , `Bearer ${ token } ` ) ;
62
+
63
+ expect ( res . status ) . to . equal ( 404 ) ;
64
+ expect ( res . body ) . to . have . property ( 'message' , 'User not found' ) ;
65
+ } ) ;
66
+
67
+ it ( 'should return 401 without a token' , async ( ) => {
68
+ const userId = 'some-valid-user-id' ;
69
+ const res = await request ( app )
70
+ . get ( `/users/${ userId } ` ) ;
71
+
72
+ expect ( res . status ) . to . equal ( 401 ) ;
73
+ expect ( res . body ) . to . have . property ( 'error' , 'Unauthorized' ) ;
74
+ } ) ;
75
+ } ) ;
76
+
44
77
describe ( 'POST /users' , ( ) => {
45
78
it ( 'should create a new user with valid data and token' , async ( ) => {
46
79
const res = await request ( app )
@@ -72,64 +105,61 @@ describe('User API', () => {
72
105
} ) ;
73
106
} ) ;
74
107
75
- /*
76
108
describe ( 'PUT /users/:id' , ( ) => {
77
- it('should update a user with valid data', async () => {
78
- const userId = 'some-valid-user-id'; // Replace with an actual user ID
79
- const updatedData = { name: 'John Updated', email: 'john.updated@example .com' };
109
+ 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] ' } ;
80
112
81
- const res = await request(app)
82
- .put(`/users/${userId}`)
83
- .set('Authorization', `Bearer ${token}`)
84
- .send(updatedData);
85
-
86
- expect(res.status).to.equal(200);
87
- expect(res.body).to.have.property('message', `User modified with ID: ${userId}`);
88
- expect(res.body).to.have.property('user');
89
- expect(res.body.user).to.have.property('name', 'John Updated');
90
- expect(res.body.user).to.have.property('email', '[email protected] ');
91
- });
113
+ const res = await request ( app )
114
+ . put ( `/users/${ userId } ` )
115
+ . set ( 'Authorization' , `Bearer ${ token } ` )
116
+ . send ( updatedData ) ;
92
117
93
- it('should return 400 for invalid user data', async () => {
94
- const userId = 'some-valid-user-id';
95
- const updatedData = { name: 'JD', email: 'not-an-email' };
118
+ expect ( res . status ) . to . equal ( 200 ) ;
119
+ expect ( res . body ) . to . have . property ( 'message' , `User modified with ID: ${ userId } ` ) ;
120
+ expect ( res . body ) . to . have . property ( 'user' ) ;
121
+ expect ( res . body . user ) . to . have . property ( 'name' , 'John Updated' ) ;
122
+ expect ( res . body . user ) . to . have . property ( 'email' , '[email protected] ' ) ;
123
+ } ) ;
96
124
97
- const res = await request(app)
98
- .put(`/users/${userId}`)
99
- .set('Authorization', `Bearer ${token}`)
100
- .send(updatedData);
125
+ 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' } ;
101
128
102
- expect(res.status).to.equal(400);
103
- expect(res.body).to.have.property('error').that.includes('Invalid email format');
104
- });
129
+ const res = await request ( app )
130
+ . put ( `/users/${ userId } ` )
131
+ . set ( 'Authorization' , `Bearer ${ token } ` )
132
+ . send ( updatedData ) ;
105
133
106
- it('should return 404 if user not found', async () => {
107
- const invalidUserId = 'invalid-user-id' ;
108
- const updatedData = { name: 'Nonexistent User', email: 'nonexistent @example .com' } ;
134
+ expect ( res . status ) . to . equal ( 400 ) ;
135
+ expect ( res . body ) . to . have . property ( 'error' ) . that . includes ( 'Invalid email format' ) ;
136
+ } ) ;
109
137
110
- const res = await request(app)
111
- .put(`/users/${invalidUserId}`)
112
- .set('Authorization', `Bearer ${token}`)
113
- .send(updatedData);
138
+ it ( 'should return 404 if user not found' , async ( ) => {
139
+ const invalidUserId = 'invalid-user-id' ;
140
+ const updatedData = { name :
'Nonexistent User' , email :
'[email protected] ' } ;
114
141
115
- expect(res.status).to.equal(404);
116
- expect(res.body).to.have.property('error', 'User not found');
117
- });
142
+ const res = await request ( app )
143
+ . put ( `/users/${ invalidUserId } ` )
144
+ . set ( 'Authorization' , `Bearer ${ token } ` )
145
+ . send ( updatedData ) ;
118
146
119
- it('should return 401 without a token', async () => {
120
- const userId = 'some-valid-user-id' ;
121
- const updatedData = { name: 'John Doe', email: 'john.doe @example .com' } ;
147
+ expect ( res . status ) . to . equal ( 404 ) ;
148
+ expect ( res . body ) . to . have . property ( 'error' , 'User not found' ) ;
149
+ } ) ;
122
150
123
- const res = await request(app)
124
- .put(`/users/${userId}`)
125
- .send(updatedData);
151
+ it ( 'should return 401 without a token' , async ( ) => {
152
+ const userId = 'some-valid-user-id' ;
153
+ const updatedData = { name :
'John Doe' , email :
'[email protected] ' } ;
154
+
155
+ const res = await request ( app )
156
+ . put ( `/users/${ userId } ` )
157
+ . send ( updatedData ) ;
126
158
127
- expect(res.status).to.equal(401);
128
- expect(res.body).to.have.property('error', 'Unauthorized');
159
+ expect ( res . status ) . to . equal ( 401 ) ;
160
+ expect ( res . body ) . to . have . property ( 'error' , 'Unauthorized' ) ;
161
+ } ) ;
129
162
} ) ;
130
- });
131
- */
132
- /*
133
163
134
164
describe ( 'DELETE /users/:id' , ( ) => {
135
165
it ( 'should delete a user with valid ID' , async ( ) => {
@@ -164,7 +194,6 @@ describe('User API', () => {
164
194
expect ( res . body ) . to . have . property ( 'error' , 'Unauthorized' ) ;
165
195
} ) ;
166
196
} ) ;
167
- */
168
197
169
198
170
199
// Close the server after tests
0 commit comments