Skip to content

Commit

Permalink
feat: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pbkompasz committed Sep 9, 2024
1 parent 1ffbbd6 commit 8f8d3e3
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
48 changes: 48 additions & 0 deletions functions/get-redeem-code.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { env, waitOnExecutionContext } from "cloudflare:test";
import { describe, it, expect } from "vitest";
import { onRequest, getRedeemCode } from "./get-redeem-code";
import { createContext, DEFAULT_BASE_URL } from "../vitest/helpers";
import { getAccessToken } from "./helpers";

describe("Get Order", () => {
it("throws 404 for non-existent order", async () => {
const transactionId = String(1);
const signedMessage = "";
const wallet = "";
const permitSig = "";

const { ctx, eventCtx } = createContext(DEFAULT_BASE_URL, { transactionId, signedMessage, wallet, permitSig });

const response = await onRequest(eventCtx);
await waitOnExecutionContext(ctx);

expect(response.status).toBe(404);
expect(await response.text()).toContain("Order not found");
});
});

describe("Redeem code helper", () => {
it("get redeem code for account and existing transaction", async () => {
const transactionId = 1;
const accessToken = await getAccessToken(env);
void expect(() => getRedeemCode(transactionId, accessToken)).rejects.toThrowError(
'Error from Reloadly API: {"status":401,"message":"Full authentication is required to access this resource"}'
);
});

it("get redeem code for other account", async () => {
const transactionId = 1;
const accessToken = await getAccessToken(env);
void expect(() => getRedeemCode(transactionId, accessToken)).rejects.toThrowError(
'Error from Reloadly API: {"status":401,"message":"Full authentication is required to access this resource"}'
);
});

it("get redeem code for account and inexistent transaction", async () => {
const transactionId = 1;
const accessToken = await getAccessToken(env);
void expect(() => getRedeemCode(transactionId, accessToken)).rejects.toThrowError(
'Error from Reloadly API: {"status":401,"mess":"Full authentication is required to access this resource"}'
);
});
});
54 changes: 54 additions & 0 deletions functions/post-order.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { waitOnExecutionContext, env } from "cloudflare:test";
import { describe, it, expect, beforeAll } from "vitest";
import { onRequest /*getGiftCardById, orderGiftCard, isDuplicateOrder, getExchangeRate, validateTransaction*/ } from "./post-order";
import { createContext, DEFAULT_BASE_URL } from "../vitest/helpers";
import { getGiftCards } from "./list-gift-cards";
import { getAccessToken } from "./helpers";

let giftCards = [];

describe("Post Order", () => {
beforeAll(async () => {
const accessToken = await getAccessToken(env);
const country = "US";
const productQuery = "visa";

// Fetch available cards
const cards = await getGiftCards(productQuery, country, accessToken);
expect(cards.length, `No cards for ${country} country`).toBeGreaterThan(0);
giftCards = cards;

// Create mock permits
const response = await fetch("http://localhost:3000/create-mock-app");
const resp = (await response.json()) as { success: boolean };
expect(resp.success).toBe(true);
});

it("simple order", async () => {
const { productId } = giftCards[0];
const chainId = "1";
let response;

response = await fetch("/create-mock-permit");
// const permit = await response.json();

response = await fetch("/create-mock-transfer");
const resp = (await response.json()) as { txHash: string };

const { ctx, eventCtx } = createContext(DEFAULT_BASE_URL, { productId, txHash: resp.txHash, chainId });

response = await onRequest(eventCtx);
await waitOnExecutionContext(ctx);

expect(response.status).toBe(404);
expect(await response.text()).toContain("Order not found");
});
});

describe("Post order helpers", () => {
it("wrong credentials", async () => {
// void expect(() => getTransactionFromOrderId("asd", { token: "token", isSandbox: true })).rejects.toThrowError(
// 'Error from Reloadly API: {"status":401,"message":"Full authentication is required to access this resource"}'
// );
});
});

0 comments on commit 8f8d3e3

Please sign in to comment.