Skip to content

Commit

Permalink
Updated test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lowdisk17 committed Aug 15, 2024
1 parent 2a8e45a commit a0fecc2
Show file tree
Hide file tree
Showing 14 changed files with 1,075 additions and 25 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
"ts-mocha": "~10.0.0",
"tsx": "~4.16.5",
"typescript": "~5.5.4",
"typescript-eslint": "~8.0.0"
"typescript-eslint": "~8.0.0",
"sinon": "~18.0.0",
"node-mocks-http": "~1.15.1"
}
}
2 changes: 1 addition & 1 deletion src/apiServices/ticketsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function postTicket(req, res) {
throw new Error('No description specified');
}
if (!processedBody.subject) {
throw new Error('No subjet specified');
throw new Error('No subject specified');
}
if (!processedBody.type) {
throw new Error('No type specified');
Expand Down
200 changes: 200 additions & 0 deletions tests/unit/actionApi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck test suite
import chai from 'chai';
import config from 'config';
import actionService from '../../src/services/actionService';
import serviceHelper from '../../src/services/serviceHelper';
import actionApi from '../../src/apiServices/actionApi';
import sinon from 'sinon';
import httpMocks from 'node-mocks-http';

const { expect, assert } = chai;

const reqValid = {
params: {
id: 141
},
query: {
id: 141
}
}

describe('Action API', () => {
describe('Get Action API: Correctly verifies action', () => {
afterEach(function() {
sinon.restore();
});

// Testing using stub data
it('should return successful result 141 if stub value is valid', async () => {
const request = httpMocks.createRequest({
method: 'GET',
url: 'test',
body: reqValid,
query: {id: 141}
});
const res = httpMocks.createResponse({
eventEmiiter: require('events').EventEmitter,
req : request
});
await sinon.stub(actionService, "getAction").returns({ wkIdentity: 141});
await actionApi.getAction(request, res);
expect(JSON.parse(res._getData())).to.have.property('wkIdentity');
expect(JSON.parse(res._getData())).to.deep.equal({ wkIdentity: 141 });
});

it('should return Bad Request result 141 if stub value is invalid', async () => {
const request = httpMocks.createRequest({
method: 'GET',
url: 'test',
body: reqValid
});
const res = httpMocks.createResponse({
eventEmiiter: require('events').EventEmitter,
req : request
});
await sinon.stub(actionService, "getAction").returns({ wkIdentity: 141});
await actionApi.getAction(request, res);
expect(res._getData()).to.deep.equal('Bad Request');
});

it('should return error result 141 if stub value is valid', async () => {
const request = httpMocks.createRequest({
method: 'GET',
url: 'test',
body: reqValid,
query: {id: 141}
});
const res = httpMocks.createResponse({
eventEmiiter: require('events').EventEmitter,
req : request
});
await sinon.stub(actionService, "getAction").returns(false);
await actionApi.getAction(request, res);
expect(res._getData()).to.deep.equal('Not Found');
});
});

describe('Post Action: Correctly verifies action', () => {
afterEach(function() {
sinon.restore();
});

// Testing using stub data
it('should return error result if stub value has no chain', async () => {
const request = httpMocks.createRequest({
method: 'POST',
url: 'test',
body: reqValid,
});
const res = httpMocks.createResponse({
eventEmiiter: require('events').EventEmitter,
req : request
});
await sinon.stub(serviceHelper, "ensureObject").returns({});
await sinon.stub(actionApi, "postAction").returns('Error: No Chain specified');
const data = await actionApi.postAction(request, res);
assert.equal(data, "Error: No Chain specified");
});

it('should return error result if stub value has no wallet key', async () => {
const request = httpMocks.createRequest({
method: 'POST',
url: 'test',
body: reqValid,
});
const res = httpMocks.createResponse({
eventEmiiter: require('events').EventEmitter,
req : request
});
await sinon.stub(serviceHelper, "ensureObject").returns({chain: 1});
await sinon.stub(actionApi, "postAction").returns('Error: No Wallet-Key Identity specified');
const data = await actionApi.postAction(request, res);
assert.equal(data, "Error: No Wallet-Key Identity specified");
});

it('should return error result if stub value has no action', async () => {
const request = httpMocks.createRequest({
method: 'POST',
url: 'test',
body: reqValid,
});
const res = httpMocks.createResponse({
eventEmiiter: require('events').EventEmitter,
req : request
});
await sinon.stub(serviceHelper, "ensureObject").returns({chain: 1, wkIdentity: 1});
await sinon.stub(actionApi, "postAction").returns('Error: No Action specified');
const data = await actionApi.postAction(request, res);
assert.equal(data, 'Error: No Action specified');
});

it('should return error result if stub value has no payload', async () => {
const request = httpMocks.createRequest({
method: 'POST',
url: 'test',
body: reqValid,
});
const res = httpMocks.createResponse({
eventEmiiter: require('events').EventEmitter,
req : request
});
await sinon.stub(serviceHelper, "ensureObject").returns({chain: 1, wkIdentity: 1, action: "", });
await sinon.stub(actionApi, "postAction").returns('Error: No Payload specified');
const data = await actionApi.postAction(request, res);
assert.equal(data, 'Error: No Payload specified');
});

it('should return error result if stub value has no derivation', async () => {
const request = httpMocks.createRequest({
method: 'POST',
url: 'test',
body: reqValid,
});
const res = httpMocks.createResponse({
eventEmiiter: require('events').EventEmitter,
req : request
});
await sinon.stub(serviceHelper, "ensureObject").returns({chain: 1, wkIdentity: 1, action: "", payload: ""});
await sinon.stub(actionApi, "postAction").returns('Error: No Derivation Path specified');
const data = await actionApi.postAction(request, res);
assert.equal(data, 'Error: No Derivation Path specified');
});

it('should return error result if stub value has no derivation', async () => {
const request = httpMocks.createRequest({
method: 'POST',
url: 'test',
body: reqValid,
});
const res = httpMocks.createResponse({
eventEmiiter: require('events').EventEmitter,
req : request
});
await sinon.stub(serviceHelper, "ensureObject").returns({chain: 1, wkIdentity: 1, action: "tx", payload: "", path: ""});
await sinon.stub(actionService, "postAction").returns(false);
await sinon.stub(actionApi, "postAction").returns('Error: Failed to post action data');
const data = await actionApi.postAction(request, res);
assert.equal(data, 'Error: Failed to post action data');
});

it('should return successful result if stub value are valid', async () => {
const request = httpMocks.createRequest({
method: 'POST',
url: 'test',
body: reqValid,
});
const res = httpMocks.createResponse({
eventEmiiter: require('events').EventEmitter,
req : request
});
await sinon.stub(serviceHelper, "ensureObject").returns({chain: 1, wkIdentity: 1, action: "publicnoncesrequest", payload: "", path: ""});
await sinon.stub(actionService, "postAction").returns({chain: 1, wkIdentity: 1, action: "publicnoncesrequest", payload: "", path: ""});
await sinon.stub(actionApi, "postAction").returns({chain: 1, wkIdentity: 1, action: "publicnoncesrequest", payload: "", path: ""});
const data = await actionApi.postAction(request, res);
assert.deepEqual(data, {chain: 1, wkIdentity: 1, action: "publicnoncesrequest", payload: "", path: ""});
});

});
});
37 changes: 37 additions & 0 deletions tests/unit/actionService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import chai from 'chai';
import config from 'config';
import actionService from '../../src/services/actionService';
import serviceHelper from '../../src/services/serviceHelper';
import sinon from 'sinon';

const { expect, assert } = chai;

Expand Down Expand Up @@ -32,6 +33,10 @@ describe('Action Service', () => {
await database.collection(actionCollection).drop();
});

afterEach(function() {
sinon.restore();
});

it('should return error when id is undefined', async () => {
await actionService.getAction().catch((e) => assert.equal(e, 'Error: Action undefined not found'));
});
Expand All @@ -51,6 +56,38 @@ describe('Action Service', () => {
it('should return error after database drop and id is invalid', async () => {
await actionService.getAction(141).catch((e) => assert.equal(e, 'Error: Action 141 not found'));
});

// Testing using stub data
it('should return successful result 141 if stub value is valid', async () => {
await sinon.stub(serviceHelper, "findOneInDatabase").returns({ wkIdentity: 141});
await actionService.getAction(141).then((r) => {
expect(r).to.have.property('wkIdentity');
expect(r).to.deep.equal({ wkIdentity: 141 });
});
});

it('should return successful result 121 if stub value is valid', async () => {
await sinon.stub(serviceHelper, "findOneInDatabase").returns({ wkIdentity: 121});
await actionService.getAction(121).then((r) => {
expect(r).to.have.property('wkIdentity');
expect(r).to.deep.equal({ wkIdentity: 121 });
});
});

it('should return error result if stub value is false', async () => {
await sinon.stub(serviceHelper, "findOneInDatabase").returns(false);
await actionService.getAction(141).catch((e) => assert.equal(e, 'Error: Action 141 not found'));
});

it('should return error result if stub value is undefined', async () => {
await sinon.stub(serviceHelper, "findOneInDatabase").returns(undefined);
await actionService.getAction(141).catch((e) => assert.equal(e, 'Error: Action 141 not found'));
});

it('should return error result if stub value is null', async () => {
await sinon.stub(serviceHelper, "findOneInDatabase").returns(undefined);
await actionService.getAction(141).catch((e) => assert.equal(e, 'Error: Action 141 not found'));
});
});

describe('Post Action: Correctly verifies action', () => {
Expand Down
27 changes: 25 additions & 2 deletions tests/unit/networkFeesService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
// @ts-nocheck test suite
import chai from 'chai';
import networkFeesService from '../../src/services/networkFeesService';
import sinon from 'sinon';

const { expect, assert } = chai;

describe('Network Fees Service', () => {
describe('Obtain Fess: Correctly verifies fees', () => {
afterEach(function() {
sinon.restore();
});

it('should return ltc fees when valid', async () => {
await networkFeesService.obtainLitecoinFees().then((r) => {
assert.equal(r.coin, 'ltc');
Expand Down Expand Up @@ -82,8 +87,26 @@ describe('Network Fees Service', () => {
});
});

// Currently network and fetch fees functions are not test ready since
// the nature of these functions is recursive in getting data
// Fetch fees using stub values
it('should return successful result if stub value is valid values', async () => {
await sinon.stub(networkFeesService, "obtainBitcoinFees").returns(1);
await sinon.stub(networkFeesService, "obtainLitecoinFees").returns(1);
await sinon.stub(networkFeesService, "obtainEthFees").returns(1);
await sinon.stub(networkFeesService, "obtainSepoliaFees").returns(1);
await sinon.stub(networkFeesService, "networkFees").returns({ btcFee: 1, ltcFee: 1, ethFee: 1, sepFee: 1});
const response = networkFeesService.networkFees();
expect(response).to.deep.equal({ btcFee: 1, ltcFee: 1, ethFee: 1, sepFee: 1});
});

it('should return successful result if stub value is valid', async () => {
await sinon.stub(networkFeesService, "obtainBitcoinFees").returns(2);
await sinon.stub(networkFeesService, "obtainLitecoinFees").returns(2);
await sinon.stub(networkFeesService, "obtainEthFees").returns(2);
await sinon.stub(networkFeesService, "obtainSepoliaFees").returns(2);
await sinon.stub(networkFeesService, "networkFees").returns({ btcFee: 2, ltcFee: 2, ethFee: 2, sepFee: 2});
const response = networkFeesService.networkFees();
expect(response).to.deep.equal({ btcFee: 2, ltcFee: 2, ethFee: 2, sepFee: 2});
});
});

});
21 changes: 21 additions & 0 deletions tests/unit/notificationService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import chai from 'chai';
import config from 'config';
import notificationService from '../../src/services/notificationService';
import serviceHelper from '../../src/services/serviceHelper';
import sinon from 'sinon';
import syncService from '../../src/services/syncService';

const { expect } = chai;

Expand Down Expand Up @@ -40,6 +42,10 @@ describe('Notification Service', () => {
tokenCollection = config.collections.v1token;
});

afterEach(function() {
sinon.restore();
});

// Checking exception if the notification is successful or not
it('should return successfully sends key', async () => {
await database.collection(tokenCollection).insertMany(testInsert);
Expand All @@ -56,5 +62,20 @@ describe('Notification Service', () => {
expect(e).to.not.be.undefined;
});
});

// Testing using stub data
it('should return successful result 141 if stub value is valid', async () => {
const stub = await sinon.stub(syncService, "getTokens").returns({ wkIdentity: 141});
await notificationService.sendNotificationKey(141, data).catch(e => {
expect(e).to.be.undefined;
});
});

it('should return successful result 121 if stub value is valid', async () => {
const stub = await sinon.stub(syncService, "getTokens").returns({ wkIdentity: 121});
await notificationService.sendNotificationKey(121, data).catch(e => {
expect(e).to.be.undefined;
});
});
});
});
Loading

0 comments on commit a0fecc2

Please sign in to comment.