Skip to content

Commit c5089ba

Browse files
authored
Merge pull request #10 from JawherKl/feature/11-create-other-test-cases
add more other test case for update, delete and getUserById
2 parents a33b406 + 82ed0a3 commit c5089ba

File tree

2 files changed

+79
-47
lines changed

2 files changed

+79
-47
lines changed

models/user.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class User {
2222

2323
static async getById(id) {
2424
const result = await pool.query('SELECT * FROM users WHERE id = $1', [id]);
25+
if (result.rows.length === 0) {
26+
return null;
27+
}
2528
return result.rows[0];
2629
}
2730

test/userRoutes.test.mjs

Lines changed: 76 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,39 @@ describe('User API', () => {
4141
});
4242
});
4343

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+
4477
describe('POST /users', () => {
4578
it('should create a new user with valid data and token', async () => {
4679
const res = await request(app)
@@ -72,64 +105,61 @@ describe('User API', () => {
72105
});
73106
});
74107

75-
/*
76108
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]' };
80112

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);
92117

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+
});
96124

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' };
101128

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);
105133

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+
});
109137

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]' };
114141

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);
118146

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+
});
122150

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);
126158

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+
});
129162
});
130-
});
131-
*/
132-
/*
133163

134164
describe('DELETE /users/:id', () => {
135165
it('should delete a user with valid ID', async () => {
@@ -164,7 +194,6 @@ describe('User API', () => {
164194
expect(res.body).to.have.property('error', 'Unauthorized');
165195
});
166196
});
167-
*/
168197

169198

170199
// Close the server after tests

0 commit comments

Comments
 (0)