Skip to content

Commit

Permalink
fix: unit, integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkparti committed Oct 11, 2023
1 parent fef965a commit c59923c
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 61 deletions.
8 changes: 7 additions & 1 deletion __tests__/wallet-get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ describe('Wallet: Get wallets of an account', () => {
await walletService.createWallet(seed.wallet.id, `test${i}`);
}

const res = await walletService.getAllWallets(seed.wallet.id);
const res = await walletService.getAllWallets(
seed.wallet.id,
undefined,
undefined,
'created_at',
'desc',
);
expect(res.count).to.eq(11);
});

Expand Down
73 changes: 45 additions & 28 deletions server/handlers/trustHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const trustGet = async (req, res) => {
// limit, offset
} = validatedQuery;

const {wallet_id} = req
const { wallet_id } = req;
const trustService = new TrustService();
const trustRelationships = await trustService.getAllTrustRelationships({
walletId: wallet_id,
Expand All @@ -34,10 +34,16 @@ const trustGet = async (req, res) => {
};

const trustPost = async (req, res) => {
const validatedBody = await trustPostSchema.validateAsync(req.body, { abortEarly: false });
const validatedBody = await trustPostSchema.validateAsync(req.body, {
abortEarly: false,
});

const { requestee_wallet, requester_wallet, trust_request_type } = validatedBody
const {wallet_id} = req
const {
requestee_wallet,
requester_wallet,
trust_request_type,
} = validatedBody;
const { wallet_id } = req;
const trustService = new TrustService();
const trustRelationship = await trustService.createTrustRelationship({
walletLoginId: wallet_id,
Expand All @@ -50,30 +56,35 @@ const trustPost = async (req, res) => {
};

const trustRelationshipGetById = async (req, res) => {
const validatedParams = await trustRelationshipIdSchema.validateAsync(req.params, {
abortEarly: false,
});
const validatedParams = await trustRelationshipIdSchema.validateAsync(
req.params,
{
abortEarly: false,
},
);

const { trustRelationshipId } = validatedParams
const {wallet_id} = req
const { trustRelationshipId } = validatedParams;
const { wallet_id } = req;

const trustService = new TrustService()
const trustService = new TrustService();
const trustRelationship = await trustService.trustRelationshipGetById({
walletLoginId: wallet_id,
trustRelationshipId
})

res.status(200).json(trustRelationship)
}
trustRelationshipId,
});

res.status(200).json(trustRelationship);
};

const trustRelationshipAccept = async (req, res) => {
const validatedParams = await trustRelationshipIdSchema.validateAsync(req.params, {
abortEarly: false,
});
const validatedParams = await trustRelationshipIdSchema.validateAsync(
req.params,
{
abortEarly: false,
},
);

const { trustRelationshipId } = validatedParams;
const {wallet_id} = req
const { wallet_id } = req;
const trustService = new TrustService();
const json = await trustService.acceptTrustRequestSentToMe({
trustRelationshipId,
Expand All @@ -83,12 +94,15 @@ const trustRelationshipAccept = async (req, res) => {
};

const trustRelationshipDecline = async (req, res) => {
const validatedParams = await trustRelationshipIdSchema.validateAsync(req.params, {
abortEarly: false,
});
const validatedParams = await trustRelationshipIdSchema.validateAsync(
req.params,
{
abortEarly: false,
},
);

const { trustRelationshipId } = validatedParams;
const {wallet_id} = req
const { wallet_id } = req;
const trustService = new TrustService();
const json = await trustService.declineTrustRequestSentToMe({
walletLoginId: wallet_id,
Expand All @@ -98,12 +112,15 @@ const trustRelationshipDecline = async (req, res) => {
};

const trustRelationshipDelete = async (req, res) => {
const validatedParams = await trustRelationshipIdSchema.validateAsync(req.params, {
abortEarly: false,
});
const validatedParams = await trustRelationshipIdSchema.validateAsync(
req.params,
{
abortEarly: false,
},
);

const { trustRelationshipId } = validatedParams;
const {wallet_id} = req
const { wallet_id } = req;
const trustService = new TrustService();
const json = await trustService.cancelTrustRequest({
walletLoginId: wallet_id,
Expand All @@ -118,5 +135,5 @@ module.exports = {
trustRelationshipAccept,
trustRelationshipDecline,
trustRelationshipDelete,
trustRelationshipGetById
trustRelationshipGetById,
};
9 changes: 8 additions & 1 deletion server/handlers/walletHandler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,29 @@ describe('walletRouter', () => {

describe('post /wallets', () => {
const walletId = uuid.v4();
const mockWallet = { id: walletId, wallet: 'test-wallet-2' };
const mockWallet = {
id: walletId,
wallet: 'test-wallet-2',
about: 'test about',
};

it('successfully creates managed wallet', async () => {
const createWalletStub = sinon
.stub(WalletService.prototype, 'createWallet')
.resolves(mockWallet);
const res = await request(app).post('/wallets').send({
wallet: mockWallet.wallet,
about: mockWallet.about,
});
expect(res).property('statusCode').eq(201);
expect(res.body.wallet).eq(mockWallet.wallet);
expect(res.body.id).eq(mockWallet.id);
expect(res.body.about).eq(mockWallet.about);
expect(
createWalletStub.calledOnceWithExactly(
authenticatedWalletId,
mockWallet.wallet,
mockWallet.about,
),
).eql(true);
});
Expand Down
39 changes: 25 additions & 14 deletions server/models/Trust.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,13 @@ class Trust {
*/
async getTrustRelationshipsRequestedToMe(walletId) {
const walletModel = new Wallet(this._session);
const { wallets: allWallets } = await walletModel.getAllWallets(walletId);
const { wallets: allWallets } = await walletModel.getAllWallets(
walletId,
undefined,
undefined,
'created_at',
'desc',
);
const allTrustRelationships = [];
await Promise.all(
allWallets.map(async (wallet) => {
Expand Down Expand Up @@ -340,11 +346,11 @@ class Trust {
});
const [trustRelationship] = trustRelationships;

if(!trustRelationship){
if (!trustRelationship) {
throw new HttpError(
404,
'No such trust relationship exists or it is not associated with the current wallet.'
)
404,
'No such trust relationship exists or it is not associated with the current wallet.',
);
}

if (trustRelationship?.originator_wallet_id !== walletId) {
Expand All @@ -365,10 +371,12 @@ class Trust {
* target wallet
*/
async hasTrust(walletLoginId, trustType, senderWallet, receiveWallet) {

Joi.assert(trustType,
Joi.string()
.valid(...Object.values(TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE)));
Joi.assert(
trustType,
Joi.string().valid(
...Object.values(TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE),
),
);

const trustRelationships = await this.getTrustRelationshipsTrusted(
walletLoginId,
Expand Down Expand Up @@ -404,7 +412,7 @@ class Trust {
return false;
}

async getTrustRelationshipById({ walletId, trustRelationshipId}) {
async getTrustRelationshipById({ walletId, trustRelationshipId }) {
const filter = {
and: [
{
Expand All @@ -420,13 +428,16 @@ class Trust {
],
};

const [trustRelationship] = await this._trustRepository.getByFilter(filter)
const [trustRelationship] = await this._trustRepository.getByFilter(filter);

if(!trustRelationship){
throw new HttpError(404, 'No such trust relationship exists or it is not associated with the current wallet.')
if (!trustRelationship) {
throw new HttpError(
404,
'No such trust relationship exists or it is not associated with the current wallet.',
);
}

return trustRelationship
return trustRelationship;
}

// NOT YET IN USE
Expand Down
4 changes: 3 additions & 1 deletion server/models/Wallet.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ describe('Wallet Model', () => {
it('should create wallet', async () => {
walletRepositoryStub.getByName.rejects(new HttpError(404));
const newWalletId = uuid();
const about = 'test about';

walletRepositoryStub.create.resolves({ id: newWalletId });

const result = await walletModel.createWallet(walletId, wallet);
const result = await walletModel.createWallet(walletId, wallet, about);

expect(result).eql({ id: newWalletId });
expect(trustRepositoryStub.create).calledOnceWithExactly({
Expand All @@ -87,6 +88,7 @@ describe('Wallet Model', () => {
expect(walletRepositoryStub.getByName).calledOnceWithExactly('wallet');
expect(walletRepositoryStub.create).calledOnceWithExactly({
name: wallet,
about,
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions server/services/TokenService.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class TokenService {
walletLoginId,
undefined,
undefined,
false,
false,
'created_at',
'desc',
);

const walletIds = [...allWallets.map((e) => e.id)];
Expand Down
22 changes: 13 additions & 9 deletions server/services/TrustService.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class TrustService {
}) {
// check if wallet exists first
// throws error if no wallet matching walletId exists
const walletService = new WalletService()
await walletService.getWallet(walletId)
const walletService = new WalletService();
await walletService.getWallet(walletId);

return this._trust.getTrustRelationships({
walletId,
Expand All @@ -36,7 +36,13 @@ class TrustService {
// except if done manually or coming up with a single query
async getAllTrustRelationships({ walletId, state, type, request_type }) {
const walletModel = new Wallet(this._session);
const { wallets } = await walletModel.getAllWallets(walletId);
const { wallets } = await walletModel.getAllWallets(
walletId,
undefined,
undefined,
'created_at',
'desc',
);

const alltrustRelationships = [];

Expand Down Expand Up @@ -115,14 +121,12 @@ class TrustService {
});
}

async trustRelationshipGetById({ walletLoginId, trustRelationshipId}){
async trustRelationshipGetById({ walletLoginId, trustRelationshipId }) {
return this._trust.getTrustRelationshipById({
walletId: walletLoginId,
trustRelationshipId
})
}

trustRelationshipId,
});
}
}


module.exports = TrustService;
12 changes: 7 additions & 5 deletions server/services/WalletService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ describe('WalletService', () => {
const loggedInWalletId = uuid.v4();
const wallet = 'wallet';
try {
await walletService.createWallet(loggedInWalletId, wallet);
await walletService.createWallet(loggedInWalletId, wallet, null);
} catch (e) {}
expect(
createWalletStub.calledOnceWithExactly(loggedInWalletId, wallet),
createWalletStub.calledOnceWithExactly(loggedInWalletId, wallet, null),
).eql(true);
expect(sessionBeginTransactionStub.calledOnce).eql(true);
expect(sessionRollbackTransactionStub.calledOnce).eql(true);
Expand All @@ -169,16 +169,18 @@ describe('WalletService', () => {
it('should create wallet', async () => {
const loggedInWalletId = uuid.v4();
const wallet = 'wallet';
const about = 'test about';
const walletId = uuid.v4();
createWalletStub.resolves({ name: wallet, id: walletId });
createWalletStub.resolves({ name: wallet, id: walletId, about });
expect(walletService).instanceOf(WalletService);
const createdWallet = await walletService.createWallet(
loggedInWalletId,
wallet,
about,
);
expect(createdWallet).eql({ wallet, id: walletId });
expect(createdWallet).eql({ wallet, id: walletId, about });
expect(
createWalletStub.calledOnceWithExactly(loggedInWalletId, wallet),
createWalletStub.calledOnceWithExactly(loggedInWalletId, wallet, about),
).eql(true);
expect(sessionBeginTransactionStub.calledOnce).eql(true);
expect(sessionCommitTransactionStub.calledOnce).eql(true);
Expand Down

0 comments on commit c59923c

Please sign in to comment.