I am trying to write test cases for an Node.js backend project
The database is using pg-promise. I run into issue when trying to stub the repository and it tries to call other repositories.
Here is the repository file user.js
async findByRefToken(refToken) {
return await this.db.oneOrNone(`SELECT id FROM useraccount WHERE username = $1`, refToken);
}
async createNormal(data) {
// generate a verify email token
const verifyToken = await this.generateToken();
// create unique ref code
const tokenid = nanoid(Number(env.REF_TOKEN_LENGTH));
const refToken = await this.generateUniqueRefToken(tokenid);
// find the referral's id from the ref token
let referredBy = null;
if(data.referred) {
referredBy = await this.findByRefToken(data.referred);
}
const meta = {
dob: data.dob,
gender: data.gender,
country: data.country,
phone: data.phone,
email_verified: false,
verify_token: verifyToken,
ref_token: refToken,
referred: referredBy ? referredBy.id : null
};
const verificationMeta = {
kyc_verified: false
};
const normalUser = {
id: data.userId,
first_name: data.firstName.trim(),
last_name: data.lastName.trim(),
active: true,
email: data.email.trim().toLowerCase(),
username: data.username.trim(),
role: 'Normal',
password: bcrypt.hashSync(data.password, Number(env.SALT_ROUNDS)),
meta: meta,
verification_meta: verificationMeta
};
const newUser = await this.db.one(CREATE_NORMAL_USER, normalUser);
return {
id: newUser.id,
firstName: data.firstName,
email: data.email,
verifyToken: verifyToken,
refToken: refToken,
referredBy: referredBy
};
}
I am trying to test the createNormal function, but it would also call other repo function like findByRefToken and await this.db.one(CREATE_NORMAL_USER, normalUser); Is there anyway to stub them away?
And here is the test written user.test.js
const chai = require("chai");
const sinon = require("sinon");
const expect = chai.expect;
const {faker} = require("#faker-js/faker");
const UserRepository = require("../../repos/user");
describe("UserRepository", function() {
const stubValue = {
dob: faker.date.birthdate(),
gender: faker.name.gender(),
country: faker.address.country(),
phone: faker.phone.number(),
email_verified: false,
id: faker.datatype.uuid(),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
username: faker.name.fullName(),
password: faker.random.alphaNumeric(5),
email: faker.internet.email(),
verifyToken: faker.random.alphaNumeric(12),
refToken: faker.random.alphaNumeric(12),
referredBy: faker.random.alphaNumeric(12)
};
describe("create", function () {
it("should add a new user to the db", async function () {
// const stub = sinon.stub(UserRepository, "createNormal").resolves(stubValue.refToken);
const userRepository = new UserRepository();
const user = await userRepository.createNormal(stubValue);
expect(user.id).to.equal(stubValue.id);
expect(user.name).to.equal(stubValue.name);
expect(user.phone).to.equal(stubValue.phone);
expect(user.id).to.equal(stubValue.id);
expect(user.verifyToken).to.equal(stubValue.verifyToken);
});
});
});
Thank you for responding
The method creates and saves a new user in the database. What is left to test if you stub the database calls?
My recommendation would be to not mock; set up the appropriate structures in the database and use them. You can use factory-bot to make this simpler.
Note: it's strange that it doesn't return a user.
I'd extract making the user from saving the user. I'd also put all the token and referral stuff into their own methods.
async newNormal(data) {
const verifyToken = await this.generateToken();
// create unique ref code
const tokenid = this.generateTokenId();
const refToken = await this.generateUniqueRefToken(tokenid);
const referredBy = await this.findByRefToken(data.referred);
const meta = {
dob: data.dob,
gender: data.gender,
country: data.country,
phone: data.phone,
email_verified: false,
verify_token: verifyToken,
ref_token: refToken,
referred: referredBy ? referredBy.id : null
};
return {
id: data.userId,
first_name: data.firstName.trim(),
last_name: data.lastName.trim(),
active: true,
email: data.email.trim().toLowerCase(),
username: data.username.trim(),
role: 'Normal',
password: bcrypt.hashSync(data.password, Number(env.SALT_ROUNDS)),
meta: meta,
verification_meta: {
kyc_verified: false
};
};
}
async createNormal(data) {
const userData = await this.newNormal(data);
return await this.db.one(CREATE_NORMAL_USER, userData);
}
Now createNormal is an integration method. All it does is call newNormal and pass the result through to a SQL query. You can test it by mocking newNormal and db.one.
The important work is happening in newNormal, focus testing on that. You can mock generateToken, generateTokenId, generateUniqueRefToken and findByRefToken. But, again, this test would be easier and more realistic by just setting up the necessary data.
Related
So, I am making an e-shop app which uses Mongo DB and Express JS as the backend. I have already created the productSchema, userSchema and the categorySchema and have coded for the appropriate GET requests.
I have made a jwt.js file which handles whether the the GET request should be allowed or not based on the token.
The code for jwt.js is given below
const { expressjwt } = require("express-jwt");
function authJwt() {
const secret = process.env.secret;
const api = process.env.API_URL;
return expressjwt({
secret,
algorithms: ["HS256"],
isRevoked: isRevoked,
}).unless({
path: [
{ url: /\/api\/v1\/products(.*)/, methods: ["GET", "OPTIONS"] },
{ url: /\/api\/v1\/categories(.*)/, methods: ["GET", "OPTIONS"] },
`${api}/users/login`,
`${api}/users/register`,
],
});
}
async function isRevoked(req, payload, done) {
if (!payload.isAdmin) {
done(null, true);
}
done();
}
module.exports = authJwt;
The code for products.js which handles the GET, POST, PUT and DELETE requests for the products database is given below.
const { Product } = require("../models/product");
const express = require("express");
const { Category } = require("../models/category");
const router = express.Router();
const mongoose = require("mongoose");
router.get(`/`, async (req, res) => {
// localhost:3000/api/v1/products?categories=2342342,234234
let filter = {};
if (req.query.categories) {
filter = { category: req.query.categories.split(",") };
}
const productList = await Product.find(filter).populate("category");
if (!productList) {
res.status(500).json({ success: false });
}
res.send(productList);
});
router.get(`/:id`, async (req, res) => {
const product = await Product.findById(req.params.id).populate("category");
if (!product) {
res.status(500).json({ success: false });
}
res.send(product);
});
router.post(`/`, async (req, res) => {
const category = await Category.findById(req.body.category);
if (!category) return res.status(400).send("Invalid Category");
let product = new Product({
name: req.body.name,
description: req.body.description,
richDescription: req.body.richDescription,
image: req.body.image,
brand: req.body.brand,
price: req.body.price,
category: req.body.category,
countInStock: req.body.countInStock,
rating: req.body.rating,
numReviews: req.body.numReviews,
isFeatured: req.body.isFeatured,
});
product = await product.save();
if (!product) return res.status(500).send("The product cannot be created");
res.send(product);
});
router.put("/:id", async (req, res) => {
if (!mongoose.isValidObjectId(req.params.id)) {
return res.status(400).send("Invalid Product Id");
}
const category = await Category.findById(req.body.category);
if (!category) return res.status(400).send("Invalid Category");
const product = await Product.findByIdAndUpdate(
req.params.id,
{
name: req.body.name,
description: req.body.description,
richDescription: req.body.richDescription,
image: req.body.image,
brand: req.body.brand,
price: req.body.price,
category: req.body.category,
countInStock: req.body.countInStock,
rating: req.body.rating,
numReviews: req.body.numReviews,
isFeatured: req.body.isFeatured,
},
{ new: true }
);
if (!product) return res.status(500).send("the product cannot be updated!");
res.send(product);
});
router.delete("/:id", (req, res) => {
Product.findByIdAndRemove(req.params.id)
.then((product) => {
if (product) {
return res
.status(200)
.json({ success: true, message: "the product is deleted!" });
} else {
return res
.status(404)
.json({ success: false, message: "product not found!" });
}
})
.catch((err) => {
return res.status(500).json({ success: false, error: err });
});
});
router.get(`/get/count`, async (req, res) => {
const productCount = await Product.countDocuments((count) => count);
if (!productCount) {
res.status(500).json({ success: false });
}
res.send({
productCount: productCount,
});
});
router.get(`/get/featured/:count`, async (req, res) => {
const count = req.params.count ? req.params.count : 0;
const products = await Product.find({ isFeatured: true }).limit(+count);
if (!products) {
res.status(500).json({ success: false });
}
res.send(products);
});
module.exports = router;
Now, the codes for the users.js and categories.js are similar and thus I am not sharing it.
I am getting the problem when doing GET request for products using POSTMAN API. Even though I am passing the correct token using BEARER TOKEN field in the POSTMAN API, it is getting stuck at sending request. When I delete the isRevoked part, everything works fine, but then again I can't control the get request based on the isAdmin part. So, the problem is in the isRevoked part. But, what exactly is the issue. It seems fine to me logically.
the problem could arise from so many things, could not say without a deeper look at your code but, here are some suggestions:
should isRevoked be async?
does your payload contains isAdmin?
and if so, inside the if statement should be done(null, false) after the if statement you should get a userid or any sort of unique fields such as userEmail, ..., then use your userModel to query the user document so that your last done() be done(null, user)
I have below javascript schema
const mongoose = require('mongoose');
const pointSchema = new mongoose.Schema({
timestamp: Number,
coords: {
latitude: Number,
longitude: Number,
altitude: Number,
accuracy: Number,
heading: Number,
speed: Number
}
});
const trackSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
name: {
type: String,
default: ''
},
locations: [pointSchema]
});
mongoose.model('Track', trackSchema);
I'm trying to convert above file to Django models. wondering how to write that locations: [pointSchema] and const pointSchema in my models.py.
Is it possible to convert that ?
this is how express server saves the data. I want to achieve same
router.post('/tracks', async (req, res) => {
const { name, locations } = req.body;
if (!name || !locations) {
return res
.status(422)
.send({ error: 'You must provide a name and locations' });
}
try {
const track = new Track({ name, locations, userId: req.user._id });
await track.save();
res.send(track);
} catch (err) {
res.status(422).send({ error: err.message });
}
});
You will be using a Many-to-one relationship depicted as a ForeignKey for the first part. Taking the docs as a basis here, it would look like this:
from django.db import models
class Coords(models.Model):
latitude = models.FloatField(...)
longitude = models.FloatField(...)
...
class PointSchema(models.Model):
timestamp = models.DateTimeField(...)
coords = models.ForeignKey(Coords, on_delete=models.CASCADE)
And for trackSchema, we use a ManyToManyField. Short excerpt:
class TrackSchema(models.Model):
...
locations = models.ManyToManyField(PointSchema)
...
In my controller i have a function that creates a user but also checks to make sure that the user does not already exist and then a dashboard function which gets the user from the request and returns any petitions that have been created by that user.
I've looked at mocha, chai and sinon to carry out the tests along with various online resources but have no idea how to begin testing these two functions since they rely on models. Can anyone point me in the right direction to testing the controller or know of any resources which maybe able to help me?
Controller:
const bcrypt = require('bcryptjs');
const passport = require('passport');
const Users = require('../models/Users');
const Petitions = require('../models/Petitions');
const UserController = {
async register(req, res) {
const {name, email, password, passwordCon} = req.body;
let errors = []
// check required fields
if (!name || !email || !password || !passwordCon) {
errors.push({ msg: 'Please enter all fields' });
}
// check passwords match
if (password !== passwordCon) {
errors.push({ msg: 'Passwords do not match' });
}
// check password length
if (password.length < 6) {
errors.push({ msg: 'Password must be at least 6 characters' });
}
// if validation fails, render messages
if (errors.length > 0) {
res.render('user/register', {
errors,
name,
email,
password,
passwordCon
})
} else {
// validation passed
Users.findOne({email: email})
.then(user => {
if (user) {
// user exists
errors.push({msg: 'Email already in use'});
res.render('user/register', {
errors,
name,
email,
password,
passwordCon
});
} else {
const newUser = new Users({
name: name,
email: email,
password: password
});
// hash password
bcrypt.genSalt(10, (error, salt) =>
bcrypt.hash(newUser.password, salt, (error, hash) => {
if (error) throw error;
// set password to hashed
newUser.password = hash;
// save user
newUser.save()
.then(user => {
req.flash('success_msg', 'Registration Success');
res.redirect('/user/login');
})
.catch(error => console.log(error));
}))
}
});
}
},
async dashboard(req, res) {
const user = req.user;
const petitions = await Petitions.find({createdBy: user._id});
console.log('here');
res.render('user/dashboard', {
user: req.user,
petitions: petitions
})
}
};
module.exports = UserController;
Models:
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
createdOn: {
type: Date,
default: Date.now
},
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
petitions: [
{ type: mongoose.Schema.Types.ObjectId, ref: 'Petitions' }
]
})
const Users = mongoose.model('Users', UserSchema);
module.exports = Users;
const mongoose = require('mongoose');
const PetitionSchema = new mongoose.Schema({
createdOn: {
type: Date,
default: Date.now
},
title: {
type: String,
required: true
},
signaturesNeeded: {
type: String,
required: true
},
description: {
type: String,
required: true
},
createdBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Users'
},
signatures: [
{ type: mongoose.Schema.Types.ObjectId, ref: 'Users' }
]
})
const Petitions = mongoose.model('Petitions', PetitionSchema);
module.exports = Petitions;
I am trying to use sinon to test a piece of code that is using an DynamoDB SDK method batchGet. Below the code:
const fetchSingleUser = async (userId) => {
try {
let queryParams = {RequestItems: {}};
queryParams.RequestItems['users'] = {
Keys: [{'UserId': userId}],
ProjectionExpression: 'UserId,Age,#UserName',
ExpressionAttributeNames: {'#UserName': 'Name'}
};
const res = await docClient.batchGet(queryParams).promise();
return res.Responses.users[0];
} catch (e) {
console.log('users::fetch::error - ', e);
}
};
Below the test using sinon:
'use strict';
const sinon = require('sinon');
const proxyquire = require('proxyquire').noCallThru();
let assert = require('assert');
describe('DynamoDB Mock Test', function () {
let AWS;
let scriptToTest;
let batchGetFunc;
before(function () {
batchGetFunc = sinon.stub();
AWS = {
DynamoDB: {
DocumentClient: sinon.stub().returns({
batchGet: batchGetFunc
})
}
};
scriptToTest = proxyquire('../index', {
'aws-sdk': AWS
});
});
it('Should scan using async/await and promise', async function () {
let result = { UserId: 'segf876seg876', Age: 33, Name: 'Paul' }
batchGetFunc.withArgs(sinon.match.any).returns({
promise: () => result
});
const data = await scriptToTest.fetchSingleUser('segf876seg876');
console.log('--data: ', data)
assert.equal(data.UserId, 'segf876seg876');
});
});
The Problem:
const data = await scriptToTest.fetchSingleUser('segf876seg876') always returns 'undefined'
Function fetchSingleUser always returns 'undefined' because you do not return anything after catch (after error happens). You only define return value on success.
But why errors happens, because const res does not contain Responses.users[0].
Simple solution:
change let result = { UserId: 'segf876seg876', Age: 33, Name: 'Paul' } to satisfy code Responses.users[0] to
const result = {
Responses: {
users: [{ UserId: 'segf876seg876', Age: 33, Name: 'Paul' }],
},
};
Note: use const if you not change variable value.
I have the following code:
async save(id: string) {
const person = await PersonModel.findOne({
where: { id: id },
});
if (!person) {
await PersonModel.create({
id: '2345',
name: 'John Doe',
age: 25
});
return;
}
await person.increment({ age: 15 });
}
Now, I wanted to test person.increment() in which the age will be added with 15. I have the following code to escape the condition that will create a new record for the model.
const findOneFake = sinon.spy(() => {
return {}; //returns empty object or true
});
const proxy = (proxyquire('./path/to/file.ts', {
'./path/to/PersonModel.ts': {
default: {
findOne: findOneFake
}
}
})).default;
beforeEach(async () => {
await save();
});
it('should increment age with 15');
How am I going to do that? What do I do to test it? I can use sinon.fake() to PersonModel.create or PersonModel.update but I am troubled testing the instance of a Sequelize Model.