Skip to content

Commit

Permalink
refactor as suggested in code review
Browse files Browse the repository at this point in the history
  • Loading branch information
tsaleksandrova committed Mar 4, 2021
1 parent 3619230 commit 6fbdf64
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 296 deletions.
2 changes: 1 addition & 1 deletion e2e/scenario/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('API tests', function() {
appMock = app;
})
});

afterAll(() => {
appMock.server.close();
});
Expand Down
18 changes: 8 additions & 10 deletions e2e/scenario/fixture/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,14 @@ describe('api', function() {
expect(err.status).toBe(403);
});

request.csrf({
url: restServiceMockUrl + '/form'
}).then(function () {
request.post(restServiceMockUrl + '/form').send({
field: 'value'
}).do().then(function (res) {
expect(res.status).toBe(200);
}).catch(function (err) {
expect(true).toBeFalsy();
});
request.authenticate(new CsrfAuthenticator({
csrfFetchUrl: restServiceMockUrl + '/form'
}));

request.post(restServiceMockUrl + '/form').send({
field: 'value'
}).do().then(function (res) {
expect(res.status).toBe(200);
});
});

Expand Down
6 changes: 4 additions & 2 deletions e2e/scenario/fixture/mock/apiServiceMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var csrf = require('csurf');

module.exports = function() {
var app = express();
// will also use a _csrf cookie (secret) and validate against it
var csrfProtection = csrf({
cookie: true
});
Expand Down Expand Up @@ -86,9 +87,10 @@ module.exports = function() {

app.get('/form', csrfProtection, function (req, res) {
if (req.headers['x-csrf-token'].toLowerCase() === 'fetch') {
res.set('x-csrf-token', req.csrfToken());
var csrfToken = req.csrfToken();
res.set('x-csrf-token', csrfToken);
res.send({
csrfToken: req.csrfToken()
csrfToken: csrfToken
});
} else {
res.sendStatus(200);
Expand Down
250 changes: 2 additions & 248 deletions package-lock.json

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

Loading

0 comments on commit 6fbdf64

Please sign in to comment.