Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danizavtz committed Oct 4, 2023
1 parent 2b4d02c commit 271c9f8
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 85 deletions.
7 changes: 3 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require('dotenv').config()
const express = require('express');
const cors = require('cors');
const expressJwt = require('express-jwt');
const { expressjwt: jwt } = require('express-jwt');

const app = express();
app.use('/secure', expressJwt({ secret: process.env.SECRET, algorithms: ['HS256'] }));
cors({ credentials: true, origin: true });
app.use(cors());
app.use(cors({ credentials: true, origin: true }));
app.use('/secure', jwt({ secret: process.env.SECRET, algorithms: ['HS256'] }));
app.use(express.json());

app.use(require('./server/index'));
Expand Down
131 changes: 59 additions & 72 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"express-jwt": "^6.1.2",
"express-jwt": "^8.4.1",
"express-validator": "^7.0.1"
},
"devDependencies": {
Expand Down
10 changes: 3 additions & 7 deletions server/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,14 @@ exports.criarUsuario = async (req, res) => {
}

exports.atualizar = async (req, res) => {
const dbi = req.app.get('datastore')
const transaction = dbi.transaction();
try {
const dbi = req.app.get('datastore')
const chave = dbi.key([tipo, Number(req.params.id)])
const transaction = dbi.transaction();
const [usuario] = await transaction.get(chave);
transaction.save({ key: chave, data: req.body });
await transaction.commit();
const usuario = await dbi.get(chave);
const mergedObj = { ...usuario, ...req.body }; //aqui a ordem é importante, estou priorizando as changes do body sobreescrevendo dados em comum que estão no datastore
dbi.save({ key: chave, data: [mergedObj] });
res.status(204).end()
} catch (e) {
transaction.rollback();
res.status(500).json({ errors: [{ location: req.path, msg: e.message, param: null }] })
}
}
Expand Down
Loading

0 comments on commit 271c9f8

Please sign in to comment.