Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed Jun 5, 2024
1 parent c3ac5fd commit dfd41c8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 61 deletions.
81 changes: 36 additions & 45 deletions server/handlers/trustHandler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const TrustService = require('../services/TrustService');
const JWTService = require('../services/JWTService');
const TrustRelationshipEnums = require('../utils/trust-enums');


describe('trustRouter', () => {
let app;
const authenticatedWalletId = uuid.v4();
Expand Down Expand Up @@ -193,48 +192,41 @@ describe('trustRouter', () => {
expect(res.body.message).match(/request_type.*one.*of/);
});

it('successfully', async () => {
const limit = 10;
const offset = 0;
const count = 1;
const orderBy = 'created_at';
const order = 'desc';

const getAllTrustRelationshipsStub = sinon
.stub(TrustService.prototype, 'getAllTrustRelationships')
.resolves({
result: [{ id: trustId }],
count
it('successfully', async () => {
const limit = 10;
const offset = 0;
const orderBy = 'created_at';
const order = 'desc';

const getAllTrustRelationshipsStub = sinon
.stub(TrustService.prototype, 'getAllTrustRelationships')
.resolves([{ id: trustId }]);

const res = await request(app).get(
`/trust_relationships?type=${TrustRelationshipEnums.ENTITY_TRUST_TYPE.send}&request_type=${TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send}&state=${TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted}&limit=${limit}&offset=${offset}&order=${order}&sort_by=${orderBy}`,
);

expect(res).property('statusCode').eq(200);
expect(res.body.trust_relationships).to.have.lengthOf(1);
expect(res.body.trust_relationships[0]).to.eql({ id: trustId });

expect(getAllTrustRelationshipsStub).to.have.been.calledWith({
state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted,
type: TrustRelationshipEnums.ENTITY_TRUST_TYPE.send,
request_type: TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send,
limit,
offset,
order,
sort_by: orderBy,
walletId: authenticatedWalletId,
});

const res = await request(app).get(
`/trust_relationships?type=${TrustRelationshipEnums.ENTITY_TRUST_TYPE.send}&request_type=${TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send}&state=${TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted}&limit=${limit}&offset=${offset}&order=${order}&sort_by=${orderBy}`,
);

expect(res).property('statusCode').eq(200);
expect(res.body.trust_relationships).to.have.lengthOf(1);
expect(res.body.trust_relationships[0]).to.eql({ id: trustId });

expect(getAllTrustRelationshipsStub).to.have.been.calledWith({
state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted,
type: TrustRelationshipEnums.ENTITY_TRUST_TYPE.send,
request_type: TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send,
limit,
offset,
order,
sort_by: orderBy,
walletId: authenticatedWalletId,
});
});
});




describe('get /trust_relationships/:id', () => {
it('missed parameters -- relationshipId must be a guid', async () => {
const res = await request(app).get(
`/trust_relationships/trustRelationshipId`,
`/trust_relationships/trustRelationshipId`,
);
expect(res).property('statusCode').eq(422);
expect(res.body.message).match(/trustRelationshipId.*GUID/);
Expand All @@ -244,21 +236,20 @@ describe('trustRouter', () => {
const trustRelationshipId = uuid.v4();

const trustRelationshipGetByIdStub = sinon
.stub(TrustService.prototype, 'trustRelationshipGetById')
.resolves({id: trustRelationshipId});
.stub(TrustService.prototype, 'trustRelationshipGetById')
.resolves({ id: trustRelationshipId });

const res = await request(app).get(
`/trust_relationships/${trustRelationshipId}`,
`/trust_relationships/${trustRelationshipId}`,
);

expect(res).property('statusCode').eq(200);
expect(
trustRelationshipGetByIdStub.calledOnceWithExactly({
walletLoginId: authenticatedWalletId,
trustRelationshipId,
}),
trustRelationshipGetByIdStub.calledOnceWithExactly({
walletLoginId: authenticatedWalletId,
trustRelationshipId,
}),
).eql(true);
});

})
});
});
9 changes: 7 additions & 2 deletions server/handlers/walletHandler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const TrustService = require('../services/TrustService');
const JWTService = require('../services/JWTService');
const TrustRelationshipEnums = require('../utils/trust-enums');

describe.only('walletRouter', () => {
describe('walletRouter', () => {
let app;
const authenticatedWalletId = uuid.v4();

Expand Down Expand Up @@ -105,19 +105,24 @@ describe.only('walletRouter', () => {
it('successfully', async () => {
const getTrustRelationshipsStub = sinon
.stub(TrustService.prototype, 'getTrustRelationships')
.resolves([{ id: trustRelationshipId }]);
.resolves({ result: [{ id: trustRelationshipId }], count: 1 });
const res = await request(app).get(
`/wallets/${walletId}/trust_relationships?state=${TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.requested}`,
);
expect(res).property('statusCode').eq(200);
expect(res.body.trust_relationships).lengthOf(1);
expect(res.body.total).eql(1);
expect(res.body.trust_relationships[0].id).eql(trustRelationshipId);
expect(
getTrustRelationshipsStub.calledOnceWithExactly(authenticatedWalletId, {
walletId,
state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.requested,
type: undefined,
request_type: undefined,
limit: 500,
offset: 0,
sort_by: 'created_at',
order: 'desc',
}),
).eql(true);
});
Expand Down
1 change: 0 additions & 1 deletion server/handlers/walletHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ const walletGetTrustRelationships = async (req, res) => {
order,
} = validatedQuery;


const trustService = new TrustService();
const trust_relationships = await trustService.getTrustRelationships(
loggedInWalletId,
Expand Down
2 changes: 1 addition & 1 deletion server/repositories/WalletRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class WalletRepository extends BaseRepository {
created_at_end_date,
getCount,
) {
let query = this._session
let query = this._session
.getDB()
.select('id', 'name', 'about', 'logo_url', 'created_at')
.table('wallet')
Expand Down
2 changes: 1 addition & 1 deletion server/services/TrustService.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class TrustService {

await Promise.all(
wallets.map(async (w) => {
const trustRelationships = await this.getTrustRelationships(walletId,{
const trustRelationships = await this.getTrustRelationships(walletId, {
walletId: w.id,
state,
type,
Expand Down
35 changes: 24 additions & 11 deletions server/services/TrustService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ describe('TrustService', () => {
.stub(Trust.prototype, 'getTrustRelationships')
.resolves(['trustRelationships']);

const getTrustRelationshipsCountStub = sinon
.stub(Trust.prototype, 'getTrustRelationshipsCount')
.resolves(1);

const getWalletStub = sinon
.stub(WalletService.prototype, 'getWallet')
.resolves({ id: 'walletId' });
Expand All @@ -48,7 +52,8 @@ describe('TrustService', () => {
},
);

expect(trustRelationship).eql(['trustRelationships']);
expect(trustRelationship.result).eql(['trustRelationships']);
expect(trustRelationship.count).eql(1);
expect(
getWalletStub.calledOnceWithExactly({
walletId: 'walletId',
Expand All @@ -66,6 +71,14 @@ describe('TrustService', () => {
order: 'order',
}),
).eql(true);
expect(
getTrustRelationshipsCountStub.calledOnceWithExactly({
walletId: 'walletId',
state: 'state',
type: 'type',
request_type: 'request_type',
}),
).eql(true);
expect(
hasControlOverStub.calledOnceWithExactly(
authenticatedWalletId,
Expand Down Expand Up @@ -207,22 +220,22 @@ describe('TrustService', () => {
TrustService.prototype,
'getTrustRelationships',
);
getTrustRelationshipsStub
.onFirstCall()
.resolves([
getTrustRelationshipsStub.onFirstCall().resolves({
result: [
{ id: 'trustId1' },
{ id: 'trustId2' },
{ id: 'trustId3' },
{ id: 'trustId4' },
]);
getTrustRelationshipsStub
.onSecondCall()
.resolves([
],
});
getTrustRelationshipsStub.onSecondCall().resolves({
result: [
{ id: 'trustId1' },
{ id: 'trustId2' },
{ id: 'trustId5' },
{ id: 'trustId6' },
]);
],
});

const trustRelationships = await trustService.getAllTrustRelationships({
walletId: 'walletId',
Expand All @@ -248,15 +261,15 @@ describe('TrustService', () => {
),
).eql(true);
expect(
getTrustRelationshipsStub.getCall(0).calledWithExactly({
getTrustRelationshipsStub.getCall(0).calledWithExactly('walletId', {
walletId: 'id1',
state: 'state',
type: 'type',
request_type: 'request_type',
}),
).eql(true);
expect(
getTrustRelationshipsStub.getCall(1).calledWithExactly({
getTrustRelationshipsStub.getCall(1).calledWithExactly('walletId', {
walletId: 'id2',
state: 'state',
type: 'type',
Expand Down

0 comments on commit dfd41c8

Please sign in to comment.