-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add trust and wallet handler unit tests
- Loading branch information
Showing
8 changed files
with
411 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
const request = require('supertest'); | ||
const express = require('express'); | ||
const sinon = require('sinon'); | ||
const chai = require('chai'); | ||
const sinonChai = require('sinon-chai'); | ||
const uuid = require('uuid'); | ||
const trustRouter = require('../routes/trustRouter'); | ||
const { errorHandler } = require('../utils/utils'); | ||
|
||
chai.use(sinonChai); | ||
const { expect } = chai; | ||
const ApiKeyService = require('../services/ApiKeyService'); | ||
const TrustService = require('../services/TrustService'); | ||
const JWTService = require('../services/JWTService'); | ||
const TrustRelationshipEnums = require('../utils/trust-enums'); | ||
|
||
describe('trustRouter', () => { | ||
let app; | ||
const authenticatedWalletId = uuid.v4(); | ||
|
||
beforeEach(() => { | ||
sinon.stub(ApiKeyService.prototype, 'check'); | ||
sinon.stub(JWTService, 'verify').returns({ | ||
id: authenticatedWalletId, | ||
}); | ||
app = express(); | ||
app.use(express.urlencoded({ extended: false })); // parse application/x-www-form-urlencoded | ||
app.use(express.json()); // parse application/json | ||
app.use(trustRouter); | ||
app.use(errorHandler); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
describe('post /trust_relationships', () => { | ||
const walletId = uuid.v4(); | ||
|
||
it('missed parameters', async () => { | ||
const res = await request(app).post('/trust_relationships').send({}); | ||
expect(res).property('statusCode').eq(422); | ||
}); | ||
|
||
it('missed parameters -- requestee wallet is required', async () => { | ||
const res = await request(app).post('/trust_relationships').send({ | ||
trust_request_type: 'send', | ||
}); | ||
expect(res).property('statusCode').eq(422); | ||
expect(res.body.message).match(/requestee_wallet.*required/); | ||
}); | ||
|
||
it('wrong parameters', async () => { | ||
const res = await request(app).post('/trust_relationships').send({ | ||
trust_request_type: 'sendError', | ||
requestee_wallet: 'wallet', | ||
}); | ||
expect(res).property('statusCode').eq(422); | ||
expect(res.body.message).match(/trust_request_type.*one/); | ||
}); | ||
|
||
it('successfully', async () => { | ||
const trustRelationshipId = uuid.v4(); | ||
const createTrustRelationshipStub = sinon | ||
.stub(TrustService.prototype, 'createTrustRelationship') | ||
.resolves({ id: trustRelationshipId }); | ||
|
||
const res = await request(app).post('/trust_relationships').send({ | ||
trust_request_type: 'send', | ||
requestee_wallet: walletId, | ||
}); | ||
|
||
expect(res).property('statusCode').eq(200); | ||
expect( | ||
createTrustRelationshipStub.calledOnceWithExactly({ | ||
walletLoginId: authenticatedWalletId, | ||
trustRequestType: 'send', | ||
requesteeWallet: walletId, | ||
requesterWallet: undefined, | ||
}), | ||
).eql(true); | ||
}); | ||
}); | ||
|
||
describe('post /trust_relationships/:id/accept', () => { | ||
it('missed parameters -- relationshipId must be a guid', async () => { | ||
const res = await request(app).post( | ||
`/trust_relationships/trustRelationshipId/accept`, | ||
); | ||
expect(res).property('statusCode').eq(422); | ||
expect(res.body.message).match(/trustRelationshipId.*GUID/); | ||
}); | ||
|
||
it('successfully', async () => { | ||
const trustRelationshipId = uuid.v4(); | ||
const acceptTrustRequestStub = sinon | ||
.stub(TrustService.prototype, 'acceptTrustRequestSentToMe') | ||
.resolves({ id: trustRelationshipId }); | ||
|
||
const res = await request(app).post( | ||
`/trust_relationships/${trustRelationshipId}/accept`, | ||
); | ||
|
||
expect(res).property('statusCode').eq(200); | ||
expect( | ||
acceptTrustRequestStub.calledOnceWithExactly({ | ||
walletLoginId: authenticatedWalletId, | ||
trustRelationshipId, | ||
}), | ||
).eql(true); | ||
}); | ||
}); | ||
|
||
describe('post /trust_relationships/:id/decline', () => { | ||
it('missed parameters -- relationshipId must be a guid', async () => { | ||
const res = await request(app).post( | ||
`/trust_relationships/trustRelationshipId/decline`, | ||
); | ||
expect(res).property('statusCode').eq(422); | ||
expect(res.body.message).match(/trustRelationshipId.*GUID/); | ||
}); | ||
|
||
it('successfully', async () => { | ||
const trustRelationshipId = uuid.v4(); | ||
const declineTrustRequestStub = sinon | ||
.stub(TrustService.prototype, 'declineTrustRequestSentToMe') | ||
.resolves({ id: trustRelationshipId }); | ||
|
||
const res = await request(app).post( | ||
`/trust_relationships/${trustRelationshipId}/decline`, | ||
); | ||
|
||
expect(res).property('statusCode').eq(200); | ||
expect( | ||
declineTrustRequestStub.calledOnceWithExactly({ | ||
walletLoginId: authenticatedWalletId, | ||
trustRelationshipId, | ||
}), | ||
).eql(true); | ||
}); | ||
}); | ||
|
||
describe('delete /trust_relationships/:id', () => { | ||
it('missed parameters -- relationshipId must be a guid', async () => { | ||
const res = await request(app).delete( | ||
`/trust_relationships/trustRelationshipId`, | ||
); | ||
expect(res).property('statusCode').eq(422); | ||
expect(res.body.message).match(/trustRelationshipId.*GUID/); | ||
}); | ||
|
||
it('successfully', async () => { | ||
const trustRelationshipId = uuid.v4(); | ||
const declineTrustRequestStub = sinon | ||
.stub(TrustService.prototype, 'cancelTrustRequestSentToMe') | ||
.resolves({ id: trustRelationshipId }); | ||
|
||
const res = await request(app).delete( | ||
`/trust_relationships/${trustRelationshipId}`, | ||
); | ||
|
||
expect(res).property('statusCode').eq(200); | ||
expect( | ||
declineTrustRequestStub.calledOnceWithExactly({ | ||
walletLoginId: authenticatedWalletId, | ||
trustRelationshipId, | ||
}), | ||
).eql(true); | ||
}); | ||
}); | ||
|
||
describe('get /trust_relationships', () => { | ||
const trustId = uuid.v4(); | ||
|
||
it('wrong state string should throw 422', async () => { | ||
const res = await request(app).get(`/trust_relationships?state=state`); | ||
expect(res).property('statusCode').eq(422); | ||
expect(res.body.message).match(/state.*one.*of/); | ||
}); | ||
|
||
it('wrong type string should throw 422', async () => { | ||
const res = await request(app).get(`/trust_relationships?type=type`); | ||
expect(res).property('statusCode').eq(422); | ||
expect(res.body.message).match(/type.*one.*of/); | ||
}); | ||
|
||
it('wrong request_type string should throw 422', async () => { | ||
const res = await request(app).get( | ||
`/trust_relationships?request_type=request_type`, | ||
); | ||
expect(res).property('statusCode').eq(422); | ||
expect(res.body.message).match(/request_type.*one.*of/); | ||
}); | ||
|
||
it('successfully', async () => { | ||
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}`, | ||
); | ||
expect(res).property('statusCode').eq(200); | ||
expect(res.body.trust_relationships).lengthOf(1); | ||
expect(res.body.trust_relationships[0]).eql({ id: trustId }); | ||
expect(getAllTrustRelationshipsStub).calledWith({ | ||
state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted, | ||
type: TrustRelationshipEnums.ENTITY_TRUST_TYPE.send, | ||
request_type: TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send, | ||
walletId: authenticatedWalletId, | ||
}); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.