Skip to content

Commit

Permalink
Arreglar tests i funció id users
Browse files Browse the repository at this point in the history
  • Loading branch information
Indigestion1 committed Apr 30, 2024
1 parent 28f722b commit fd200a7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
24 changes: 21 additions & 3 deletions back_api/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const router = express.Router()
router.use(express.json());

const { db } = require('../firebaseConfig');
const { checkUserAndFetchData, checkUsername } = require('./middleware');
const { checkUserAndFetchData, checkUsername, checkAdmin } = require('./middleware');

const crypto = require('crypto');
const algorithm = 'aes-256-cbc';
Expand Down Expand Up @@ -34,9 +34,10 @@ router.get('/read/users', checkUserAndFetchData, async (req, res) => {
}
});

router.get('/info', async (req, res) => {

router.get('/infoToken', async (req, res) => {
try {
id = req.headers.authorization.split(' ')[1];
const id = req.headers.authorization.split(' ')[1];
const docRef = db.collection('users').doc(id);
const response = await docRef.get();
if (response.exists) {
Expand All @@ -52,6 +53,23 @@ router.get('/info', async (req, res) => {
});


router.get('/:id', checkAdmin, async (req, res) => {
try {
const id = req.params.id;
const docRef = db.collection('users').doc(id);
const response = await docRef.get();
if (response.exists) {
res.status(200).send(response.data());
} else {
res.status(404).send('Usuario no encontrado');
}
} catch (error){
res.send(error);
}
});



router.post('/create', async(req, res) => {
try {
const { uid, username, email, favcategories } = req.body;
Expand Down
63 changes: 59 additions & 4 deletions back_api/tests/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,45 @@ function encrypt(text) {
encrypted = Buffer.concat([encrypted, cipher.final()]);
return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') };
}
describe('GET /users/infoToken', () => {
const testUsers = [
usertes1 = {
id: 'useridTest1',
username: 'username',
email: 'email',
token: "10b79ecbebb4da0fedff89edf6a504f5",
activitats: ['1', '2', '3'],
},
usertest2 = {
id: 'useridTest2',
username: 'username2',
email: 'email2',
token: "10b79ecbebb4da0fedff89edf6a504f5",
activitats: ['1', '2', '3'],
}
]
beforeEach(async () => {
for(const user of testUsers) {
const userRef = db.collection('users').doc(user.id);
await userRef.set(user);
}
});
it('should return 200 and user data', async () => {
const user = testUsers[0];
const response = await request(app)
.get(`/users/infoToken`)
.set('Authorization', `Bearer useridTest1`);
expect(response.status).toBe(200);
expect(response.body).toEqual(user);
});
it('should return 404 if user does not exist', async () => {
const response = await request(app)
.get(`/users/infoToken`)
.set('Authorization', `Bearer invalid`);
expect(response.status).toBe(404);
expect(response.text).toBe('Usuario no encontrado');
});
});
describe('GET /users/:id', () => {
const testUsers = [
usertes1 = {
Expand All @@ -31,27 +70,43 @@ describe('GET /users/:id', () => {
}
]
beforeEach(async () => {
const adminUser = {
uid: 'adminUid',
username: 'adminUsername',
};
// Then add the admin user to the 'admin' collection
await db.collection('administradors').doc(adminUser.uid).set({'username': adminUser.username});
for(const user of testUsers) {
const userRef = db.collection('users').doc(user.id);
await userRef.set(user);
}

});
it('should return 200 and user data', async () => {
const user = testUsers[0];
const response = await request(app)
.get(`/users/info`)
.set('Authorization', `Bearer ${user.id}`);
.get(`/users/${user.id}`)
.set('Authorization', `Bearer ${encrypt('adminUid').encryptedData}`);
expect(response.status).toBe(200);
expect(response.body).toEqual(user);
});
it('should return 401 if token is invalid', async () => {
const response = await request(app)
.get(`/users/${testUsers[0].id}`)
.set('Authorization', 'Bearer invalidToken');
expect(response.status).toBe(401);
expect(response.text).toBe('Token inválido');
});
it('should return 404 if user does not exist', async () => {
const response = await request(app)
.get(`/users/info`)
.set('Authorization', `Bearer ${'invaldId'}`);
.get(`/users/invalidId`)
.set('Authorization', `Bearer ${encrypt('adminUid').encryptedData}`);
expect(response.status).toBe(404);
expect(response.text).toBe('Usuario no encontrado');
});
});


describe('GET /users/read/users', () => {
const testUsers = [
usertes1 = {
Expand Down

0 comments on commit fd200a7

Please sign in to comment.