Skip to content

Commit

Permalink
Merge pull request #192 from Arquisoft/users_management_tests
Browse files Browse the repository at this point in the history
/getAllUsers method added to userservice
  • Loading branch information
plg22 authored Apr 12, 2024
2 parents 5b3633e + 409ed7d commit eb1e0f7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 deletions.
2 changes: 1 addition & 1 deletion users/authservice/auth-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let mongoServer;

// user that already exists in the database
const user = {
username: 'trogui',
username: 'rita',
password: '0000',
};

Expand Down
53 changes: 40 additions & 13 deletions users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ const User = require('./user-model')





// Function to validate required fields in the request body
function validateRequiredFields(req, requiredFields) {
for (const field of requiredFields) {
if (!(field in req.body)) {
return res.status(400).json({ error: 'Username and password are required' });
}
}
}


// GET route to retrieve an specific user by username
// 'http://localhost:8002/getOneUser?username=nombre_de_usuario'

Expand Down Expand Up @@ -34,17 +46,9 @@ router.get('/getUser', async (req, res) => {
});



// Function to validate required fields in the request body
function validateRequiredFields(req, requiredFields) {
for (const field of requiredFields) {
if (!(field in req.body)) {
return res.status(400).json({ error: 'Username and password are required' });
}
}
}


/**
* POST route to add a new user to the database
*/
router.post('/adduser', async (req, res) => {
try {

Expand Down Expand Up @@ -76,8 +80,9 @@ router.post('/adduser', async (req, res) => {




// edit a user to update the total and correct question answered
/**
* POST route to edit a user to update the total and correct question answered
*/
router.post('/editUser', async (req, res) => {
try {

Expand All @@ -104,5 +109,27 @@ router.post('/editUser', async (req, res) => {
});


/**
* GET route to retrieve all the users in the database
*/
router.get('/getAllUsers', async (req, res) => {
try {
// access the UsersDB database
const db = mongoose.connection.useDb("UsersDB");

// access the User collection
const userCollection = db.collection('User');

// Find all users
const users = await userCollection.find({}).toArray();

res.json(users);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});




module.exports = router;
29 changes: 26 additions & 3 deletions users/userservice/user-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const user = {
};

beforeAll(async () => {
jest.setTimeout(30000);
mongoServer = await MongoMemoryServer.create();
//jest.setTimeout(30000);
mongoServer = await MongoMemoryServer.create(); // database in memory
const mongoUri = mongoServer.getUri();
process.env.MONGODB_URI = mongoUri;
},30000);

afterAll(async () => {
jest.setTimeout(30000);
//jest.setTimeout(30000);
await mongoose.connection.close();
await mongoServer.stop();
});
Expand Down Expand Up @@ -140,6 +140,29 @@ describe('User Service', () => {
expect(response.status).toBe(404);
expect(response.body).toHaveProperty('error');
});


// --- TESTS FOR /getAllUsers ---


// TESTS TO GET ALL THE USERS
it('should get all users on GET /user/getAllUsers', async () => {
const response = await request(app).get('/user/getAllUsers');

expect(response.status).toBe(200);
expect(Array.isArray(response.body)).toBe(true);
});

it('should return 500 if there is an internal server error', async () => {
// We simulate an error in the database by closing the connection before the request
await mongoose.connection.close();

const response = await request(app).get('/user/getAllUsers');


expect(response.status).toBe(500);
expect(response.body).toHaveProperty('error');
});


});

0 comments on commit eb1e0f7

Please sign in to comment.