Skip to content

Commit

Permalink
test: get-orders
Browse files Browse the repository at this point in the history
  • Loading branch information
pbkompasz committed Aug 24, 2024
1 parent 7d71a02 commit d64c4d2
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions functions/get-order.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { env, createExecutionContext, waitOnExecutionContext, Parameters } from "cloudflare:test";
import { describe, it, expect } from "vitest";
import { onRequest, getTransactionFromOrderId } from "./get-order";
import { getAccessToken } from "./helpers";

function createContext(baseUrl: string, params: Record<string, string>) {
const url = new URL(baseUrl);
url.search = new URLSearchParams(params).toString();
const request = new Request(url);
const ctx = createExecutionContext();
const eventCtx: Parameters<typeof onRequest>[0] = {
request,
functionPath: "",
waitUntil: ctx.waitUntil.bind(ctx),
passThroughOnException: ctx.passThroughOnException.bind(ctx),
env,
};
return { request, ctx, eventCtx };
}

const DEFAULT_BASE_URL = "http:/placeholder";

describe("Get Orders", () => {
it("throws 404 for non-existent order", async () => {
const { ctx, eventCtx } = createContext(DEFAULT_BASE_URL, { orderId: "1" });

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

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

it("throws 500 for missing order id", async () => {
const { ctx, eventCtx } = createContext(DEFAULT_BASE_URL, {});

const response = await onRequest(eventCtx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(500);
expect(await response.text()).toContain("There was an error while processing your request");
});

it("throws 404 for non-existent order", async () => {
const { ctx, eventCtx } = createContext(DEFAULT_BASE_URL, { orderId: "1" });

const response = await onRequest(eventCtx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(404);
});
it.skip("found existing but unsuccessfull transaction", async () => {
const { ctx, eventCtx } = createContext(DEFAULT_BASE_URL, { orderId: "1" });

const response = await onRequest(eventCtx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(404);
});

it.skip("found existing and successful transaction", async () => {
// TODO
const existingSuccessfulTransaction = { id: "asd" };
const { ctx, eventCtx } = createContext(DEFAULT_BASE_URL, { orderId: existingSuccessfulTransaction.id });

const response = await onRequest(eventCtx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(404);
});
});

describe("Get 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"}'
);
});

it("wrong mode (not sandbox)", async () => {
const accessToken = await getAccessToken(env);
void expect(() => getTransactionFromOrderId("asd", { token: accessToken.token, isSandbox: !accessToken.isSandbox })).rejects.toThrowError(
'Error from Reloadly API: {"status":401,"message":"Invalid token, are you in production & using a sandbox token or vice-versa?"}'
);
});
});

0 comments on commit d64c4d2

Please sign in to comment.