Skip to content

Commit

Permalink
Issue 579 fix unit testing (#593)
Browse files Browse the repository at this point in the history
* Add tests for bot/validations.js

* Remove only

* Update bot tests and lightning tests

* Update describe name

* Update files to pass the linter check

* Adde stubs for dependencies and added some new tests

* Fix lint issues

---------

Co-authored-by: Daniel Candia Flores <[email protected]>
  • Loading branch information
danfercf1 and Daniel Candia Flores authored Oct 18, 2024
1 parent 3332ac3 commit c02f051
Show file tree
Hide file tree
Showing 13 changed files with 2,398 additions and 748 deletions.
20 changes: 7 additions & 13 deletions bot/validations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MainContext, OrderQuery, ctxUpdateAssertMsg } from "./start";
import { ICommunity } from "../models/community";
import { ICommunity, IUsernameId } from "../models/community";
import { FilterQuery } from "mongoose";
import { UserDocument } from "../models/user";
import { IOrder } from "../models/order";
Expand All @@ -9,7 +9,7 @@ const { parsePaymentRequest } = require('invoices');
const { ObjectId } = require('mongoose').Types;
import * as messages from './messages';
import { Order, User, Community } from '../models';
import { isIso4217, isDisputeSolver, removeLightningPrefix } from '../util';
import { isIso4217, isDisputeSolver, removeLightningPrefix, isOrderCreator } from '../util';
const { existLightningAddress } = require('../lnurl/lnurl-pay');
import { logger } from '../logger';

Expand Down Expand Up @@ -106,6 +106,7 @@ const validateAdmin = async (ctx: MainContext, id?: string) => {

const isSolver = isDisputeSolver(community, user);

// TODO this validation does not return anything
if (!user.admin && !isSolver)
return await messages.notAuthorized(ctx, tgUserId);

Expand Down Expand Up @@ -192,7 +193,8 @@ const validateSellOrder = async (ctx: MainContext) => {
await messages.mustBeANumberOrRange(ctx);
return false;
}


// TODO, this validation could be amount > 0?
if (amount !== 0 && amount < Number(process.env.MIN_PAYMENT_AMT)) {
await messages.mustBeGreatherEqThan(
ctx,
Expand Down Expand Up @@ -338,7 +340,7 @@ const validateInvoice = async (ctx: MainContext, lnInvoice: string) => {
const latestDate = new Date(
Date.now() + Number(process.env.INVOICE_EXPIRATION_WINDOW)
);
if (!("MAIN_PAYMENT_AMT" in process.env)) throw Error("MIN_PAYMENT_AMT not found, please check .env file");
if (!("MIN_PAYMENT_AMT" in process.env)) throw Error("MIN_PAYMENT_AMT not found, please check .env file");
if (!!invoice.tokens && invoice.tokens < Number(process.env.MIN_PAYMENT_AMT)) {
await messages.minimunAmountInvoiceMessage(ctx);
return false;
Expand Down Expand Up @@ -429,14 +431,6 @@ const isValidInvoice = async (ctx: MainContext, lnInvoice: string) => {
}
};

const isOrderCreator = (user: UserDocument, order: IOrder) => {
try {
return user._id == order.creator_id;
} catch (error) {
logger.error(error);
return false;
}
};

const validateTakeSellOrder = async (ctx: MainContext, bot: Telegraf<MainContext>, user: UserDocument, order: IOrder) => {
try {
Expand Down Expand Up @@ -688,7 +682,7 @@ const isBannedFromCommunity = async (user: UserDocument, communityId: string) =>
if (!communityId) return false;
const community = await Community.findOne({ _id: communityId });
if (!community) return false;
return community.banned_users.toObject().some((buser: ICommunity) => buser.id == user._id);
return community.banned_users.some((buser: IUsernameId) => buser.id == user._id);
} catch (error) {
logger.error(error);
return false;
Expand Down
1,050 changes: 514 additions & 536 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"lint": "eslint .",
"format": "prettier --write '**/*.js'",
"pretest": "npx tsc",
"test": "export NODE_ENV=test && mocha --exit 'tests/**/*.js'"
"test": "export NODE_ENV=test && mocha --exit tests/**/*.spec.js"
},
"license": "MIT",
"dependencies": {
Expand Down
Loading

0 comments on commit c02f051

Please sign in to comment.