Skip to content

Commit

Permalink
added some things in the auth-service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uo289029 committed Apr 27, 2024
1 parent 687b5eb commit 3e84003
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion users/authservice/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ app.post('/login', async (req, res) => {
app.get('/getUserByUsername', async (req, res) => {
try {
// Obtener el nombre de usuario desde el cuerpo de la solicitud
const username = req.query.username;
const username = req.query.username.toString();
//buscarlo en la base de datos
const user = await User.findOne({ username: username });
// Devolver el usuario encontrado en la respuesta
Expand Down
20 changes: 19 additions & 1 deletion users/authservice/auth-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ const user = {
password: 'testpassword',//NOSONAR
};

let hashedPassword = '';

async function addUser(user){
const hashedPassword = await bcrypt.hash(user.password, 10);
hashedPassword = await bcrypt.hash(user.password, 10);
const newUser = new User({
username: user.username,
password: hashedPassword,
Expand Down Expand Up @@ -42,4 +44,20 @@ describe('Auth Service', () => {
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('username', 'testuser');
});

it('Should perform a getUserByUsername operation /getUserByUsername', async () => {
const response = await request(app).get('/getUserByUsername').query({ username: 'testuser' });
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('user');
expect(response.body.user).toHaveProperty('username', 'testuser');
expect(response.body.user).toHaveProperty('password', hashedPassword);
});

it('Should perform a getAllUsers operation /getAllUsers', async () => {
const response = await request(app).get('/getAllUsers');
expect(response.status).toBe(200);
//array de tamaño 1 porque solo hay un usuario en la base de datos de test
expect(response.body).toHaveLength(1);
});

});

0 comments on commit 3e84003

Please sign in to comment.