Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 579 fix unit testing #593

Merged
merged 10 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danfercf1 this looks like an unrelated change that should go in a separate PR?

Copy link
Contributor Author

@danfercf1 danfercf1 Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right the problem is that the test related to this implementation will not work. I've reported that problem in this issue #594

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
Loading