forked from ubiquity/pay.ubq.fi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?"}' | ||
); | ||
}); | ||
}); |