@riturajgc/upload-manager-service
Version:
Upload manager service for bitnudge
69 lines (64 loc) • 3.71 kB
JavaScript
const { expect } = require('chai');
const sinon = require('sinon');
const { errorObject } = require('./../../src/lib/utils/index.js');
const { Storage } = require('./../../index.js');
const App = new Storage(1);
App.setMongoose({ dbURL: 'mongodb://localhost/bitnudge-upload-document-test-string' });
describe.only('Adapter', () => {
describe('fetch', () => {
let uploadManagerData;
beforeEach(function() {
uploadManagerData = sinon.stub(App.adapter.type.model, 'find');
});
afterEach(function() {
uploadManagerData.restore();
});
it('case-1: should return data if all the required input parameter is correct', async () => {
expect(App.adapter.fetch).to.be.exist;
});
it('case-2: should return data if all the required input parameter is correct', async () => {
const userId = "5b4c424d669a91104ad6eb7e", fileId = "123133412", projection = {projection: 'projection'};
uploadManagerData.withArgs({"source.userId":userId, _id: fileId}, projection).returns({
skip : sinon.stub().returns({
limit: sinon.stub().resolves([{
_id:"5b4c424d669a91104ad6eb7f",
userId:"5b4c424d669a91104ad6eb7e"
},{
_id:"5b4c424d669a91104ad6eb7g",
userId:"5b4c424d669a91104ad6eb7e"
}])
})
});
const result = await App.adapter.fetch({query: {userId, fileId}, projection, skip:1, limit:10});
expect(result.status).to.be.true;
expect(uploadManagerData.calledOnce).to.be.true;
expect(result.data[0]._id.toString()).equal("5b4c424d669a91104ad6eb7f");
expect(result.data[0].userId.toString()).equal("5b4c424d669a91104ad6eb7e");
expect(result.data[1]._id.toString()).equal("5b4c424d669a91104ad6eb7g");
expect(result.data[1].userId.toString()).equal("5b4c424d669a91104ad6eb7e");
});
it('case-3: should return false if all the required input parameter is not correct', async () => {
const result = await App.adapter.fetch({userId:"5b4c424d669a91104ad6eb7e"});
expect(result.status).to.be.false;
expect(result.message.toString()).to.contain("Please provide all the fields");
});
it('case-4: should return false if all the required input parameter are not in expected format', async () => {
const userId = "5b4c424d669a91104ad6eb7e", fileId = "123133412", projection = {projection: 'projection'};
const result = await App.adapter.fetch({query: {userId, fileId}, projection: 1, skip:1, limit:10});
expect(result.status).to.be.false;
expect(result.message.toString()).to.contain("Please provide all the valid fields");
});
it('case-5:should return false if any error happens in server', async () => {
const userId = "5b4c424d669a91104ad6eb7e", fileId = "123133412", projection = {projection: 'projection'};
uploadManagerData.withArgs({"source.userId":userId, _id: fileId}, projection).returns({
skip : sinon.stub().returns({
limit: sinon.stub().rejects("check")
})
});
const result = await App.adapter.fetch({query: {userId, fileId}, projection, skip:1, limit:10});
expect(result.status).to.be.false;
expect(uploadManagerData.calledOnce).to.be.true;
expect(result.message.toString()).equal("There is some problem, Please try after some time");
});
})
});