Skip to content

Commit

Permalink
add test for create payment document + fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed Feb 8, 2024
1 parent cb60788 commit 671e777
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 49 deletions.
6 changes: 6 additions & 0 deletions adapters/payment/PaymentAdapter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ export interface InitPaymentAdapter {
export default abstract class PaymentAdapter {
readonly InitPaymentAdapter: InitPaymentAdapter;
config: Config;
private initializationPromise;
protected constructor(InitPaymentAdapter: InitPaymentAdapter);
/**
* Waiting for initialization
*/
wait(): Promise<void>;
private initialize;
/**
* Make new payment
* @param Payment - payment document
Expand Down
11 changes: 10 additions & 1 deletion adapters/payment/PaymentAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ class PaymentAdapter {
constructor(InitPaymentAdapter) {
this.InitPaymentAdapter = InitPaymentAdapter;
this.config = InitPaymentAdapter.config;
PaymentMethod.alive(this);
this.initializationPromise = this.initialize();
}
/**
* Waiting for initialization
*/
async wait() {
await this.initializationPromise;
}
async initialize() {
await PaymentMethod.alive(this);
}
/**
* Method for creating and obtaining an existing Payment Adapter
Expand Down
15 changes: 14 additions & 1 deletion adapters/payment/PaymentAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,23 @@ export interface InitPaymentAdapter {
export default abstract class PaymentAdapter {
public readonly InitPaymentAdapter: InitPaymentAdapter;
public config: Config
private initializationPromise: Promise<void>;

protected constructor(InitPaymentAdapter: InitPaymentAdapter) {
this.InitPaymentAdapter = InitPaymentAdapter;
this.config = InitPaymentAdapter.config
PaymentMethod.alive(this);
this.initializationPromise = this.initialize();
}

/**
* Waiting for initialization
*/
public async wait(): Promise<void> {
await this.initializationPromise;
}

private async initialize(): Promise<void> {
await PaymentMethod.alive(this);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion models/PaymentDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let attributes = {
externalId: {
type: "string",
unique: true,
required: true,
required: false,
// allowNull: true // Only for NEW state
},
/** Model from which payment is made*/
Expand Down
4 changes: 2 additions & 2 deletions models/PaymentDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ let attributes = {
externalId: {
type: "string",
unique: true,
required: true,
required: false,
// allowNull: true // Only for NEW state
} as unknown as string,

Expand Down Expand Up @@ -220,7 +220,7 @@ let Model = {
},

/** Payment check cycle*/
processor: async function (timeout: number) {
processor: async function (timeout: number): Promise<ReturnType<typeof setInterval>> {
sails.log.silly("PaymentDocument.processor > started with timeout: " + timeout ?? 45000);
return (payment_processor_interval = setInterval(async () => {
let actualTime = new Date();
Expand Down
48 changes: 27 additions & 21 deletions test/integration/models/paymentDocument.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const ExternalTestPaymentSystem_1 = __importDefault(require("../../unit/external_payments/ExternalTestPaymentSystem"));
//import Order from '../../../models/Order';
describe("PaymentDocument", function () {
this.timeout(31000);
let paymentMethod;
/**
*
*/
it("register TODO", async function () {
ExternalTestPaymentSystem_1.default.getInstance();
//static
before(async function () {
let testPaymentSystem = ExternalTestPaymentSystem_1.default.getInstance();
await testPaymentSystem.wait();
paymentMethod = await PaymentMethod.findOne({
adapter: "test-payment-system",
});
await PaymentMethod.update({ id: paymentMethod.id }, { enable: true }).fetch();
// TODO:
// Проверка суммы. Проверка originModel. Проверка платежного метода. Проверить paymentResponse, сравнить.
});
it("doPaid TODO", async function () {
Expand All @@ -27,28 +34,27 @@ describe("PaymentDocument", function () {
it("afterUpdate(sails) TODO", async function () {
// Создать корзину. В корзине выбрать платежный метод (тестовая система). Поставить оплату и проверить что в корзине status === 'PAID'
});
// it("register payment document", async function () {
// });
it("check payment processor", async function () {
/**
* Для того чтобы протестировать платежный документ мы создаем два платежа. И подписываемся на
* событие doCheck(); если мы получаем эти два платежа через евент, то процессор платежей их вызывает.
* In order to test the payment document, we create two payments. And subscribe to
* doCheck() event; if we receive these two payments through an event, then the payment processor calls them.
*/
// let count = [];
// emitter.on("core-payment-document-check", function (paymentDocument) {
// count.push(paymentDocument);
// });
// await PaymentDocument.destroy({});
// let order = await Order.create({}).fetch();
// let paymentMethod = await PaymentMethod.findOne({
// adapter: "test-payment-system",
// });
//
// if (!paymentMethod) throw "paymentMethod (test-payment-system) was not found "
// PaymentDocument.processor(3000);
// await PaymentDocument.register(order.id, "order", 100, paymentMethod.id, "http://", "http://", "test-payment-processor", { test: true });
// await sleep(5000);
let count = [];
emitter.on("core-payment-document-check", function (paymentDocument) {
count.push(paymentDocument);
});
await PaymentDocument.destroy({});
let order = await Order.create({}).fetch();
if (!paymentMethod)
throw "paymentMethod (test-payment-system) was not found ";
PaymentDocument.processor(3000);
await PaymentDocument.register(order.id, "order", 100, paymentMethod.id, "http://", "http://", "test-payment-processor", { test: true });
await sleep(5000);
// // back to 120 sec payment processor
// PaymentDocument.processor(120000);
// // // expect(count.length).to.equal(1);
PaymentDocument.processor(120000);
(0, chai_1.expect)(count.length).to.equal(1);
});
});
function sleep(ms) {
Expand Down
54 changes: 31 additions & 23 deletions test/integration/models/paymentDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ import PaymentDocument from "../../../models/PaymentDocument";

describe("PaymentDocument", function () {
this.timeout(31000);

let paymentMethod;
/**
*
*/
it("register TODO", async function () {
TestPaymentSystem.getInstance();
//static
before(async function () {
let testPaymentSystem = TestPaymentSystem.getInstance();
await testPaymentSystem.wait();

paymentMethod = await PaymentMethod.findOne({
adapter: "test-payment-system",
});

await PaymentMethod.update({id: paymentMethod.id}, {enable: true}).fetch()

// TODO:
// Проверка суммы. Проверка originModel. Проверка платежного метода. Проверить paymentResponse, сравнить.
});

Expand All @@ -32,33 +40,33 @@ describe("PaymentDocument", function () {
// Создать корзину. В корзине выбрать платежный метод (тестовая система). Поставить оплату и проверить что в корзине status === 'PAID'
});

// it("register payment document", async function () {

// });

it("check payment processor", async function () {
/**
* Для того чтобы протестировать платежный документ мы создаем два платежа. И подписываемся на
* событие doCheck(); если мы получаем эти два платежа через евент, то процессор платежей их вызывает.
* In order to test the payment document, we create two payments. And subscribe to
* doCheck() event; if we receive these two payments through an event, then the payment processor calls them.
*/

// let count = [];
// emitter.on("core-payment-document-check", function (paymentDocument) {
// count.push(paymentDocument);
// });
let count = [];
emitter.on("core-payment-document-check", function (paymentDocument) {
count.push(paymentDocument);
});

// await PaymentDocument.destroy({});
// let order = await Order.create({}).fetch();
// let paymentMethod = await PaymentMethod.findOne({
// adapter: "test-payment-system",
// });

//
// if (!paymentMethod) throw "paymentMethod (test-payment-system) was not found "
// PaymentDocument.processor(3000);
// await PaymentDocument.register(order.id, "order", 100, paymentMethod.id, "http://", "http://", "test-payment-processor", { test: true });
await PaymentDocument.destroy({});
let order = await Order.create({}).fetch();

if (!paymentMethod) throw "paymentMethod (test-payment-system) was not found "
PaymentDocument.processor(3000);
await PaymentDocument.register(order.id, "order", 100, paymentMethod.id, "http://", "http://", "test-payment-processor", { test: true });

// await sleep(5000);
await sleep(5000);

// // back to 120 sec payment processor
// PaymentDocument.processor(120000);
// // // expect(count.length).to.equal(1);
PaymentDocument.processor(120000);
expect(count.length).to.equal(1);
});
});

Expand Down

0 comments on commit 671e777

Please sign in to comment.