How can you test values passed to EJS file rendered by Express? - javascript

I am trying to test my express web application using mocha and chai, but I am unable to figure out how to test the HTTP requests being handled by express. I am using ejs as my view engine so most of my route handlers look like this:
const someRouteHandlerForGETRequest = async (request, response) => {
// usually some DB queries to get data to display in view
const someQuery = await pool.query('SELECT * FROM table');
var sampleData = someQuery.rows;
response.render('ejspage', {
user: request.session.username,
sampleData: sampleData
});
}
Because of this, I believe that my response.body is just an html file / html string (using postman I was trying to see what was in the response, it appeared to just be the html for the view)
How do I test the data being passed to the ejs file (in this example; sampleData) that is being returned from the node-pg database query?
My current attempt at testing is this:
const { assert } = require('chai');
const chai = require('chai');
const chaiHttp = require('chai-http');
const app = require('../index');
chai.should();
chai.use(chaiHttp);
describe('/GET activeorders', () => {
it('it should GET active orders', (done) => {
chai.request('http://localhost:3000')
.get('/activeorders')
.end((err, res) => {
res.should.have.status(200);
done();
});
})
})
Basically the only this I can test is the response status, beyond that I can't figure out to test any of the values that are being returned from database queries.
I have also tried separating the model from the controller by removing the queries to the database from the route handler and instead having the route handler call a function that returns the results. This doesn't solve my problem though because using chai-http to test a HTTP request still doesn't give me access to the data since the route handler is simply rendering the ejs page with the data.

Related

How I can mock an incoming http request in jest using node.js http?

I am making a handler library named handle_http.js:
module.exports.redirectHttpToHttps = (db,req,res)=>{
const sql = "SELECT * from redirect where use_in_http = 1 and exact_match = 1 and url_from = ? and exact_match=1 LIMIT 1";
// redirection logic
}
And I made a http server where consumes the library:
const http = require('node:http');
// A simple database connection generator
const db = require('./db.js');
const handler = require('./handle_http.js');
http.createServer((req,res){
handler.redirectHttpToHttps(db,req,res);
});
http.listen(80);
But before running into an actual code, I want to make some unit tests using jest (test_redirect.jest.js):
const db = require('../src/db.js');
const redirect = require('../src/handle_http.js');
test("redirect to https",()=>{
const dbHandler = db(':memory:');
database.exec(`
INSERT INTO redirect (url_from,url_to,method,http_status_code,use_in_http,exact_match) VALUES
('http://google.com/mytest','http://yahoo.com','GET',301,1,1),
('http://google.com/mytest2','http://yandex.com','GET',302,1,0),
('http://google.com?q=ikariam','http://yandex.com','GET',302,1,1),
('http://example.com/products','https://fakestoreapi.com/products','POST',308,1,1),
('http://example.net/products','https://fakestoreapi.com/products','POST',308,1,0),
('http://example.net','https://fakestoreapi.com/products','POST',308,1,0);
`,function(error){ err_callback(error); });
// need to make fake request so I can call the `redirectHttpToHttps`
redirect.redirectHttpToHttps(db,/*mocked_request*/,/*some way to assert the response*/)
});
As you can see, I am able to populate an in-memory database with fake data, but I do not know how:
How I can make a fake an incoming http request.
How I can assert that http response has appropriate status code and headers
The provided example does not cut in my case because I need to test the http handling logic in my own http server written in nodejs.
An approach is to use the supertest and create an http server on the fly:
const http = require('node:http');
const request = require('supertest');
const db = require('../src/db.js');
const redirect = require('../src/handle_http.js');
test("redirect to https",(done)=>{
const dbHandler = db(':memory:');
database.exec(`
INSERT INTO redirect (url_from,url_to,method,http_status_code,use_in_http,exact_match) VALUES
('http://google.com/mytest','http://yahoo.com','GET',301,1,1),
('http://google.com/mytest2','http://yandex.com','GET',302,1,0),
('http://google.com?q=ikariam','http://yandex.com','GET',302,1,1),
('http://example.com/products','https://fakestoreapi.com/products','POST',308,1,1),
('http://example.net/products','https://fakestoreapi.com/products','POST',308,1,0),
('http://example.net','https://fakestoreapi.com/products','POST',308,1,0);
`,function(error){ done(error); });
const server = http.createServer((req,res)=>{
redirect.redirectHttpToHttps(dbHandler,req,res)
});
request(server)
.get('/mytest')
.set('Host','google.com')
.expect(301,done);
});
Pay attention into the lines:
request(server)
.get('/mytest')
.set('Host','google.com')
.expect(301,done);
Using request function comming from supertest I provide a server instance that does not listen to any port:
const server = http.createServer((req,res)=>{
redirect.redirectHttpToHttps(dbHandler,req,res)
});
During testing, you can avoid the https at all and create pure non-ssl servers that call the http handling function you want to perform.
Miscelanous
Also, your code has an error at section:
database.exec(`
INSERT INTO redirect (url_from,url_to,method,http_status_code,use_in_http,exact_match) VALUES
('http://google.com/mytest','http://yahoo.com','GET',301,1,1),
('http://google.com/mytest2','http://yandex.com','GET',302,1,0),
('http://google.com?q=ikariam','http://yandex.com','GET',302,1,1),
('http://example.com/products','https://fakestoreapi.com/products','POST',308,1,1),
('http://example.net/products','https://fakestoreapi.com/products','POST',308,1,0),
('http://example.net','https://fakestoreapi.com/products','POST',308,1,0);
`,function(error){ err_callback(error); });
Function err_callback is not defined. Therfore I used the jest's done function as defined into documentation
So the refactored part of the test is:
database.exec(`
INSERT INTO redirect (url_from,url_to,method,http_status_code,use_in_http,exact_match) VALUES
('http://google.com/mytest','http://yahoo.com','GET',301,1,1),
('http://google.com/mytest2','http://yandex.com','GET',302,1,0),
('http://google.com?q=ikariam','http://yandex.com','GET',302,1,1),
('http://example.com/products','https://fakestoreapi.com/products','POST',308,1,1),
('http://example.net/products','https://fakestoreapi.com/products','POST',308,1,0),
('http://example.net','https://fakestoreapi.com/products','POST',308,1,0);
`,function(error){ done(error); });

Nodejs controller is being messy

I'm new to javascript, node.js (or backend at all). I am trying to create a controller for the login page requests and I am confused about getting data from the MYSQL table and User Authentication and working with JWT package !
In my Controller, I first check if the user input is available in the user table (with a simple stored procedure), then I compare the database password and the user input, after this I want to create a token and with limited time. (I have watched some tutorial videos about JWT and there is no problem with it), my main problem is to figure out how to write a proper controller with this functions?
I have 2 other questions:
1.Is it the right and secure way to get data from MySQL table inside the route? Or should I create a JS class for my controller? (I'm a bit confused and doubtful here)
2.Assuming that comparePassword() returns true, how can I continue coding outside of the db.query callback function scope? Because I have to execute comparePasssword() inside db.query callback
loginController.js :
const { validationResult } = require('express-validator');
const bcrypt = require('bcrypt');
const db = require('../../sqlConnection')
let comparePassword = (dbPass, inputPass) => {
bcrypt.compare(inputPass, dbPass, function(err, result) {
console.log(result)
});
}
// for get request
exports.getController = (req, res) => {
res.send('login')
}
// for post request
exports.postController = (req, res) => {
let errors = validationResult(req)
if(!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() })
}
// find data from MYSQL table
let sql = `CALL findUser(?)`
db.query(sql, [req.body.username], (err, res) => {
if(err) console.log(err)
//console.log(Object.values(JSON.parse(JSON.stringify(res[0]))))
var data = JSON.stringify(res[0])
data = JSON.parse(data).find(x => x)
data ? comparePassword(data.password, req.body.password) : res.status(400).send('cannot find
user')
})
res.send('post login')
}
login.js :
const express = require('express')
const router = express.Router()
const { check } = require('express-validator');
const loginCont = require('../api/controllers/loginController')
router.route('/')
.get(
loginCont.getController
)
.post(
[
check('username').isLength({min: 3}).notEmpty(),
check('password').isLength({min: 4}).notEmpty()
],
loginCont.postController
)
module.exports = router
In my point of view, looks like there is no easy answer for your question so I will try to give you some directions so you can figure out which are the gaps in your code.
First question: MySQL and business logic on controller
In a design pattern like MVC or ADR (please take a look in the links for the flow details) The Controllers(MVC) Or Actions(ADR) are the entry point for the call, and a good practice is to use these entry points to basically:
Instantiate a service/class/domain-class that supports the request;
Call the necessary method/function to resolve what you want;
Send out the response;
This sample project can help you on how to structure your project following a design pattern: https://riptutorial.com/node-js/example/30554/a-simple-nodejs-application-with-mvc-and-api
Second question: db and continue the process
For authentication, I strongly suggest you to take a look on the OAuth or OAuth2 authentication flow. The OAuth(2) has a process where you generate a token and with that token you can always check in your Controllers, making the service a lot easier.
Also consider that you may need to create some external resources/services to solve if the token is right and valid, but it would facilitate your job.
This sample project should give you an example about how to scope your functions in files: https://github.com/cbroberg/node-mvc-api
Summary
You may have to think in splitting your functions into scoped domains so you can work with them in separate instead of having all the logic inside the controllers, then you will get closer to classes/services like: authenticantion, user, product, etc, that could be used and reused amount your controllers.
I hope that this answer could guide you closer to your achievements.

CORS proxy error in react app and express server

I am running a React app with an Express server back-end on port:5000. I am making a request to one of my endpoint using the state of the element to get some data and then render it to the page. I initially setup the proxy in the package.json file of the React app as "proxy": "http://localhost:5000" (documentation. Now it was giving me a proxy error saying cannot proxy to localhost:5000. So I used CORS in my express server as a universal middleware for all of the routes using app.use(cors()).That removed the proxy error but the request is still not working (the code is not even reaching the endpoint because nothing is being logged to the console) and I am pretty sure it's because of the same error. Here are the code snippets
router.get("/", async (req, res) => {
var elements = [];
elements = await Element.find();
res.json(elements);
});
const getElements = async () => {
try {
console.log('getElements call')
const data = await axios.get("/api/elements");
console.log(data);
dispatch({ type: GET_ELEMENTS, payload: data });
} catch (e) {
console.error(e);
}
};
const { getElements, elements, loading } = elementContext;
useEffect(() => {
getElements();
}, [])
Expected behaviour: I want the endpoint to send an array name elements to the call which can then be set to the elements state using the reducer and then can be accessed by destructing in the end. I hope I have given adequate information. Any help will be appreciated. Thank you.

Running query from inside Cloud Function using request parameters

I am having troubles running queries from Cloud Functions using the request parameters to build the query form HTTP calls. In the past, I have ran queries from cloud functions fine with no error. My problem arises when I try to run the query using parameters gotten from the request.
When I hardcode the location of the document in the function, it works fine but when I try to build a query, it returns status code of 200. I have also logged the the built query and it is logging out the right thing but no data is being returned. It only returns data when the document path is hardcoded. See code below.
Query looks like this
https://us-central1-<project-id>.cloudfunctions.net/getData/CollectionName/DocumentName
export const getData = functions.https.onRequest((request, response) => {
const params = request.url.split("/");
console.log("the params 0 "+params[0]);
console.log("the params 1 "+params[1]);
console.log("the params 2 "+params[2]);
//Build up the document path
const theQuery = "\'"+params[1]+"\/"+params[2]+"\'";
console.log("the query "+theQuery); <-- logs out right result in the form 'Collection/Document'
//Fetch the document
const promise = admin.firestore().doc("\'"+params[1]+"\/"+params[2]+"\'").get() <---- This doesnt work, building the query
//const promise = admin.firestore().doc('collectionName/DocID').get() <---- This hard coded and it works
promise.then(snapshot => {
const data = snapshot.data()
response.send(data)
}).catch(error => {
console.log(error)
response.status(500).send(error);
})
});
I tried using a different approach and giving the datafields a names as seen below
Query looks like this
https://us-central1-<project-id>.cloudfunctions.net/getData?CollectionName=CName&DocumentID=Dname
export const getData = functions.https.onRequest((request, response) => {
const collectName = request.query.CollectionName;
const DocId = request.query.DocumentName;
//Build up the document path
const theQuery = "'"+collectName+"\/"+collectName+"'";
console.log("the query "+theQuery); <---Logs out correct result
//Fetch the document
const promise = admin.firestore().doc(theQuery).get() <-- Building the query does not work
//const promise = admin.firestore().doc('collectionName/DocID').get() <---- This hard coded and it works
promise.then(snapshot => {
const data = snapshot.data()
response.send(data)
}).catch(error => {
console.log(error)
response.status(500).send(error);
})
});
In both cases, when the request is build from the URL, it does not return any data and it does not return any errors. And I am sure the documents I am trying to fetch exsist in the database. Am I missing anything ?
Try request.path. Then you can obtain the path components, e.g. request.path.split("/")[1]
The syntax for request.query is valid when using Express. This is referenced in some of the docs, but not made explicit that Express is required. It's confusing.
To properly handle the dynamic inputs, you may have more luck working with Express and creating routes and handlers. This Firebase page has links to some projects using it.
Walkthough set-up using Express on Firebase.

Send body JSON with Postman in a POST method to execute procedure in ms sql server

I want to send a JSON (body) on Postman with POST method and receive a result.
Im a trainee and my boss asked me this. I've been looking in web for a week and didn't find. I said to him that method to receive a data is GET, but he said that GET has a limit on URL. If the stored procedure have a lot of parameters we will not receive the expected result, so a I have to use Post method.
This is my code to connect to ms sql server:
var express = require('express');
var app = express();
var sql = require('mssql');
var config = {
user: 'MY_USER',
password: 'MY_PASS',
server: 'MY_SERVER',
database: 'MY_DB'
};
Searching in google I found a way to execute a procedure with GET method. In the browser I put the value I want and I receive the result, but is not still what he wants. The code is:
app.get('/get/:pNU_EST001', function (req, res) {
//conexão com o DB
sql.connect(config, function(){
var request = new sql.Request();
request.input('pNU_EST001', req.params.pNU_EST001);
request.execute('dbo.ESTSP004_ConsultaLivrosEmprestadosAluno_XX', function(err, recordsets, returnValue, affected) {
if(err) console.log(err);
res.send(recordsets);
//res.end(JSON.stringify(recordsets)); /*--- result in JSON format ---*/
});
});
});
On Postman he showed me a example with DB2, but I couldn't see the code. On Post Method, on HEADERS it has two key:
KEY => Content-Type (value: application/json) // KEY => Accept (value: application/json)
And in his example, on BODY, he wrote a JSON like: { "pNU_EST001" : "3"} and received the related result below. That's what I need.
Express has a few other verbs to use you are using
app.get but there is also app.post if you change your code to use the latter instead it will handle a post request
app.post('/path', function (req, res) {
Then in express to get values from the POST body you get that from
req.body property
So in your case you would want to use req.body.pNU_EST001 instead of req.params.pNU_EST001

Categories

Resources