ForestAdmin / forest-express-sequelize

🌱 Express/Sequelize agent for Forest Admin to integrate directly to your existing Express/Sequelize backend application.
https://www.forestadmin.com
GNU General Public License v3.0
190 stars 47 forks source link

How to stub liana.ensureAuthenticated? #219

Open neokaiyuan opened 5 years ago

neokaiyuan commented 5 years ago

Expected behavior

sinon.stub(liana, 'ensureAuthenticated') should stub the ensureAuthenticated

Actual behavior

stub does not address liana 'ensureAuthenticated', and code in forest-express-sequelize and forest-express seem to obscure how ensureAuthenticated is exposed

Context

I use liana.ensureAuthenticated as a middleware as suggested by Forest docs https://doc.forestadmin.com/developer-guide/express-sequelize.html#uploading-images and am hoping to unit test my controller.

neokaiyuan commented 5 years ago

seems to be a broader issue with stubbing express app middleware before app gets loaded.

https://stackoverflow.com/questions/41995464/how-to-mock-middleware-in-express-to-skip-authentication-for-unit-test https://stackoverflow.com/questions/41392497/node-express-es6-sinon-stubbing-middleware-not-working https://github.com/sinonjs/sinon/issues/1711

ended up resolving this by not including ensureAuthenticated middleware in test env, as bad as it is to include test-specific logic in production code.

polaroi8d commented 5 years ago

seems to be a broader issue with stubbing express app middleware before app gets loaded.

https://stackoverflow.com/questions/41995464/how-to-mock-middleware-in-express-to-skip-authentication-for-unit-test https://stackoverflow.com/questions/41392497/node-express-es6-sinon-stubbing-middleware-not-working sinonjs/sinon#1711

ended up resolving this by not including ensureAuthenticated middleware in test env, as bad as it is to include test-specific logic in production code.

Could you provide your working example?