-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from pes2324q2-gei-upc/notificacions
Notificacions
- Loading branch information
Showing
6 changed files
with
157 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const admin = require('firebase-admin') | ||
const express = require('express') | ||
const router = express.Router() | ||
|
||
router.use(express.json()); | ||
|
||
const checkUserAndFetchData = require('./middleware').checkUserAndFetchData; | ||
|
||
//penjar notificacions | ||
router.post('/enviar', checkUserAndFetchData, async (req, res) => { | ||
const { title, mensaje, token } = req.body; | ||
|
||
const message = { | ||
notification: { | ||
title: title, | ||
body: mensaje, | ||
}, | ||
token: token, | ||
}; | ||
|
||
admin.messaging().send(message) | ||
.then((response) => { | ||
res.status(201).send('Notificacio enviada'); | ||
}) | ||
.catch((error) => { | ||
res.status(500).send('Error enviant la notificació'); | ||
}); | ||
|
||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const request = require('supertest'); | ||
const app = require('../app'); | ||
const admin = require('firebase-admin'); | ||
|
||
const crypto = require('crypto'); | ||
const algorithm = 'aes-256-cbc'; | ||
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex'); | ||
const iv = Buffer.from(process.env.ENCRYPTION_IV, 'hex'); | ||
|
||
function encrypt(text) { | ||
let cipher = crypto.createCipheriv(algorithm, Buffer.from(key), iv); | ||
let encrypted = cipher.update(text); | ||
encrypted = Buffer.concat([encrypted, cipher.final()]); | ||
return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') }; | ||
} | ||
|
||
jest.mock('firebase-admin', () => { | ||
const messaging = { | ||
send: jest.fn(), | ||
}; | ||
return { | ||
messaging: () => messaging, | ||
}; | ||
}); | ||
|
||
describe('POST /notificacio/enviar', () => { | ||
const users = [ | ||
user1 = { | ||
id: "user1", | ||
username: "user1", | ||
email: "", | ||
blockedUsers: ["user2"] | ||
}, | ||
]; | ||
|
||
beforeEach(async () => { | ||
for (const user of users) { | ||
await db.collection('users').doc(user.id).set(user); | ||
} | ||
}); | ||
|
||
it('should create a notification', async () => { | ||
admin.messaging().send.mockResolvedValue('Successfully sent message'); | ||
const body = { | ||
title: 'titleTest', | ||
mensaje: 'mensajeTest', | ||
token: 'tokenTest' | ||
}; | ||
|
||
const res = await request(app) | ||
.post('/notificacio/enviar') | ||
.set('Authorization', `Bearer ${encrypt('user1').encryptedData}`) | ||
.send(body); | ||
|
||
expect(res.statusCode).toEqual(201); | ||
expect(res.text).toBe('Notificacio enviada'); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters