Skip to content

Commit

Permalink
test: post payment card order on production
Browse files Browse the repository at this point in the history
  • Loading branch information
EresDev committed Nov 3, 2024
1 parent 460ffae commit 3f21a7e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
11 changes: 6 additions & 5 deletions tests/fixtures/http-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import transaction from "./get-order/transaction.json";
import noTransaction from "./get-order/no-transaction.json";
import transaction0x33f4 from "./get-redeem-code/transaction-0x33f4.json";
import card from "./get-redeem-code/card.json";
import order from "./post-order/order.json";
import orderCard13959 from "./post-order/order-card-13959.json";
import orderCard18597 from "./post-order/order-card-18597.json";
import { RELOADLY_AUTH_URL, RELOADLY_PRODUCTION_API_URL, RELOADLY_SANDBOX_API_URL } from "../../functions/utils/helpers";

/**
Expand Down Expand Up @@ -42,10 +43,10 @@ export const httpMocks = [
const productName = url.searchParams.get("productName");

if (productName == "mastercard") {
return HttpResponse.json([bestMastercardProd], { status: 200 });
return HttpResponse.json(bestMastercardProd, { status: 200 });
}
if (productName == "visa") {
return HttpResponse.json([bestVisaProd], { status: 200 });
return HttpResponse.json(bestVisaProd, { status: 200 });
}
return HttpResponse.json([], { status: 200 });
}),
Expand All @@ -72,10 +73,10 @@ export const httpMocks = [
}),

http.post(`${RELOADLY_PRODUCTION_API_URL}/orders`, () => {
return HttpResponse.json(order, { status: 200 });
return HttpResponse.json(orderCard18597, { status: 200 });
}),
http.post(`${RELOADLY_SANDBOX_API_URL}/orders`, () => {
return HttpResponse.json(order, { status: 200 });
return HttpResponse.json(orderCard13959, { status: 200 });
}),

http.get(`${RELOADLY_PRODUCTION_API_URL}/products/13959`, () => {
Expand Down
File renamed without changes.
36 changes: 36 additions & 0 deletions tests/fixtures/post-order/order-card-18597.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"transactionId": 39018,
"amount": 50,
"discount": 0,
"currencyCode": "USD",
"fee": 6,
"smsFee": 0,
"totalFee": 6,
"preOrdered": false,
"recipientEmail": null,
"recipientPhone": null,
"customIdentifier": "0xd670e1f4f47a0104217f30a8804f7e13803da1dbfd9270dcb6d8d3a5ba72a52e",
"status": "SUCCESSFUL",
"transactionCreatedTime": "2024-11-03 03:25:42",
"product": {
"productId": 18597,
"productName": "Virtual MasterCard International USD US",
"countryCode": "US",
"quantity": 1,
"unitPrice": 44,
"totalPrice": 44,
"currencyCode": "USD",
"brand": {
"brandId": 378,
"brandName": "Mastercard"
}
},
"balanceInfo": {
"oldBalance": 212439823.44865,
"newBalance": 212439773.44865,
"cost": 50,
"currencyCode": "USD",
"currencyName": "US Dollar",
"updatedAt": "2024-11-02 13:46:17"
}
}
30 changes: 24 additions & 6 deletions tests/unit/post-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest
import { onRequest as pagesFunction } from "../../functions/post-order";
import { httpMocks } from "../fixtures/http-mocks";
import minedTx from "../fixtures/post-order/mined-tx.json";
import order from "../fixtures/post-order/order.json";
import orderCard13959 from "../fixtures/post-order/order-card-13959.json";
import orderCard18597 from "../fixtures/post-order/order-card-18597.json";
import receipt from "../fixtures/post-order/receipt.json";
import { getEventContext as createEventContext, TESTS_BASE_URL } from "./helpers";

Expand All @@ -31,7 +32,26 @@ describe("Post order for a payment card", () => {
server.close();
});

it.only("should post order on sandbox", async () => {
it("should post order on production", async () => {
const request = new Request(`${TESTS_BASE_URL}/post-order`, {
method: "POST",
body: JSON.stringify({
type: "permit",
chainId: 31337,
txHash: "0xac3485ce523faa13970412a89ef42d10939b44abd33cbcff1ed84cb566a3a3d5",
productId: 18597,
country: "US",
}),
}) as Request<unknown, IncomingRequestCfProperties<unknown>>;

const eventCtx = createEventContext(request, execContext);
const response = await pagesFunction(eventCtx);
await waitOnExecutionContext(execContext);
expect(response.status).toBe(200);
expect(await response.json()).toEqual(orderCard18597);
});

it("should post order on sandbox", async () => {
const request = new Request(`${TESTS_BASE_URL}/post-order`, {
method: "POST",
body: JSON.stringify({
Expand All @@ -47,18 +67,16 @@ describe("Post order for a payment card", () => {
const response = await pagesFunction(eventCtx);
await waitOnExecutionContext(execContext);
expect(response.status).toBe(200);
expect(await response.json()).toEqual(order);
expect(await response.json()).toEqual(orderCard13959);
});
});

async function initMocks() {
vi.mock("../../shared/helpers");
const helpers = await import("../../shared/helpers");
helpers.getFastestRpcUrl = vi.fn().mockImplementation(() => {
vi.spyOn(helpers, "getFastestRpcUrl").mockImplementation(async () => {
return "http://127.0.0.1:8545";
});

vi.mock("@ethersproject/providers");
const providers = await import("@ethersproject/providers");
providers.JsonRpcProvider.prototype.getTransactionReceipt = vi.fn().mockImplementation(async () => {
return receipt;
Expand Down

0 comments on commit 3f21a7e

Please sign in to comment.