Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/Arquisoft/wiq_es6b into Jesu…
Browse files Browse the repository at this point in the history
…s-e2e-Tests
  • Loading branch information
uo285427 committed May 1, 2024
2 parents 091c544 + d75cce0 commit 323bb7e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 32 deletions.
5 changes: 4 additions & 1 deletion users/userservice/.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ TEST_USER="testuser"
TEST_PASSWORD="testpassword"

TEST_USER2="testuser2"
TEST_PASSWORD2="testpassword2"
TEST_PASSWORD2="testpassword2"

TEST_USER3="testuser3"
EMPTY_PASSWORD=' '
5 changes: 4 additions & 1 deletion users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ mongoose.connect(mongoUri);

// Function to validate required fields in the request body
function validateRequiredFields(req, requiredFields) {
for (const field of requiredFields) {
for (const field of requiredFields) {
if (!(field in req.body)) {
throw new Error(`Missing required field: ${field}`);
}
if (req.body[field].trim() == '' || req.body[field] == null || req.body[field] === undefined) {
throw new Error(`Field ${field} must not be empty`);
}
}
}

Expand Down
96 changes: 66 additions & 30 deletions users/userservice/user-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,76 @@ describe('User Service', () => {
expect(response.body).toHaveProperty('username', 'testuser');
});

//prueba get
it('should get all users on GET /getAllUsers', async () => {

// Agrego primero usuarios
await request(app).post('/adduser').send({
username: process.env.TEST_USER,
password: process.env.TEST_PASSWORD,
});

await request(app).post('/adduser').send({
username: process.env.TEST_USER2,
password: process.env.TEST_PASSWORD2,

//prueba get
it('should get all users on GET /getAllUsers', async () => {

// Agrego primero usuarios
await request(app).post('/adduser').send({
username: process.env.TEST_USER,
password: process.env.TEST_PASSWORD,
});
});
// llamo al get
const response = await request(app).get('/getAllUsers');
expect(response.status).toBe(200);
expect(response.body).toBeInstanceOf(Array);//obtengo elementos
// miro que esten los dos añadidos
const usernames = response.body.map(user => user.username);
expect(usernames).toContain('testuser');
expect(usernames).toContain('testuser2');

await request(app).post('/adduser').send({
username: process.env.TEST_USER2,
password: process.env.TEST_PASSWORD2,
});

test('should throw an error when a required field is missing', async () => {
const response = await request(app)
.post('/adduser')
.send({ username: 'testuser' }); // password field is missing

});
// llamo al get
const response = await request(app).get('/getAllUsers');
expect(response.status).toBe(200);
expect(response.body).toBeInstanceOf(Array);//obtengo elementos
// miro que esten los dos añadidos
const usernames = response.body.map(user => user.username);
expect(usernames).toContain('testuser');
expect(usernames).toContain('testuser2');
expect(response.status).toBe(400);
expect(response.body.error).toBe('Missing required field: password');
});

});
describe('should throw an error when a required field is present but empty', () => {
test('password is empty and it throws an error', async () => {
const response = await request(app)
.post('/adduser')
.send({ username: process.env.TEST_USER3, password: ''}); // password field is empty

expect(response.status).toBe(400);
expect(response.body.error).toBe('Field password must not be empty');
});

test('should throw an error when a required field is missing', async () => {
const response = await request(app)
.post('/adduser')
.send({ username: 'testuser' }); // password field is missing

expect(response.status).toBe(400);
expect(response.body.error).toBe('Missing required field: password');
});

test('password is empty and it throws an error', async () => {
const response = await request(app)
.post('/adduser')
.send({ username: process.env.TEST_USER3, password: process.env.EMPTY_PASSWORD}); // password field is empty

expect(response.status).toBe(400);
expect(response.body.error).toBe('Field password must not be empty');
});

test('username is empty and it throws an error', async () => {
const response = await request(app)
.post('/adduser')
.send({ username: '', password: process.env.TEST_PASSWORD}); // username field is empty

expect(response.status).toBe(400);
expect(response.body.error).toBe('Field username must not be empty');
});

test('username is empty and it throws an error', async () => {
const response = await request(app)
.post('/adduser')
.send({ username: ' ', password: process.env.TEST_PASSWORD}); // username field is empty

expect(response.status).toBe(400);
expect(response.body.error).toBe('Field username must not be empty');
});
});

});

0 comments on commit 323bb7e

Please sign in to comment.