In controller I have imported config file for getting path for userId and secret path location and auth file for getting the userId name and secret value based on that I have redirected to some endpoint. but when I write the testcases how to pass or get the all details using stub. can you please assist.
controller.ts
import { userConf } from './conf';
import {userSec} from './auth'
export function userInfo(req: Request, res: Response) {
const path='test:path';
const path1='test:path1'
const userID = userConf(path); //user/Id
const userPass=userConf(path1); // user/pass
const secId=userSec(userID); // raj
const secPass=userSec(userPass) // Otersg
const url=`https://mapuser.com?userId=${secId}&usersec=${secPass}`;
res.redirect(302,url);
}
details.json
{
"test":{
"path":"user/Id",
"path1":"user/pass"
}
}
conf.ts
export function userConf(path) {
return 'implementation for getting path';
}
auth.ts
export function usersec(path) {
return 'implementation for getting values';
}
test.spec.ts
import sinon from 'sinon';
import proxyquire from 'proxyquire';
describe('should redirect', () => {
it('should pass with all valid', () => {
const getpath = sinon.stub().returns('test:path');
const getsecId = sinon.stub().returns('ram');
const urlctl = proxyquire('./controller', {
'./conf': {
userConf: getpath,
},
'./auth': {
userSec: getsecId,
},
});
const req = {};
const res = { redirect: sinon.stub() };
urlctl.getId(req, res);
sinon.assert.calledWithExactly(res.redirect, 302, 'https://mapuser.com?
userId=raj&usersec=Otersg');
});
});