This commit is contained in:
1 parent
8efd032cc6
commit
df38dccbf1
735 files changed
+128321
-120870
No files matched your search
@@ -1,86 +1,102 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { authorizeTopupCapture, claimTopupDelivery, recordCreatedTopup } from "@/lib/services/paypal-topup";
|
||||
import {
|
||||
authorizeTopupCapture,
|
||||
claimTopupDelivery,
|
||||
recordCreatedTopup,
|
||||
} from "@/lib/services/paypal-topup";
|
||||
|
||||
describe("authorizeTopupCapture", () => {
|
||||
it("rejects an order created by a different user", async () => {
|
||||
const findOrder = async () => ({
|
||||
userId: 41,
|
||||
status: "CREATED",
|
||||
});
|
||||
it("rejects an order created by a different user", async () => {
|
||||
const findOrder = async () => ({
|
||||
userId: 41,
|
||||
status: "CREATED",
|
||||
});
|
||||
|
||||
await expect(authorizeTopupCapture(99, "ORDER-123", findOrder)).rejects.toMatchObject({
|
||||
code: "ORDER_NOT_FOUND",
|
||||
});
|
||||
});
|
||||
await expect(
|
||||
authorizeTopupCapture(99, "ORDER-123", findOrder),
|
||||
).rejects.toMatchObject({
|
||||
code: "ORDER_NOT_FOUND",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects an order that has already left the created state", async () => {
|
||||
const findOrder = async () => ({
|
||||
userId: 99,
|
||||
status: "COMPLETED",
|
||||
});
|
||||
it("rejects an order that has already left the created state", async () => {
|
||||
const findOrder = async () => ({
|
||||
userId: 99,
|
||||
status: "COMPLETED",
|
||||
});
|
||||
|
||||
await expect(authorizeTopupCapture(99, "ORDER-123", findOrder)).rejects.toMatchObject({
|
||||
code: "ORDER_ALREADY_PROCESSED",
|
||||
});
|
||||
});
|
||||
await expect(
|
||||
authorizeTopupCapture(99, "ORDER-123", findOrder),
|
||||
).rejects.toMatchObject({
|
||||
code: "ORDER_ALREADY_PROCESSED",
|
||||
});
|
||||
});
|
||||
|
||||
it("resumes credit delivery for a captured order without capturing it again", async () => {
|
||||
const findOrder = async () => ({
|
||||
userId: 99,
|
||||
status: "CAPTURED_PENDING_CREDIT",
|
||||
amount: 12.5,
|
||||
});
|
||||
it("resumes credit delivery for a captured order without capturing it again", async () => {
|
||||
const findOrder = async () => ({
|
||||
userId: 99,
|
||||
status: "CAPTURED_PENDING_CREDIT",
|
||||
amount: 12.5,
|
||||
});
|
||||
|
||||
await expect(authorizeTopupCapture(99, "ORDER-123", findOrder)).resolves.toMatchObject({
|
||||
action: "DELIVER_CREDITS",
|
||||
amount: 12.5,
|
||||
});
|
||||
});
|
||||
await expect(
|
||||
authorizeTopupCapture(99, "ORDER-123", findOrder),
|
||||
).resolves.toMatchObject({
|
||||
action: "DELIVER_CREDITS",
|
||||
amount: 12.5,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("recordCreatedTopup", () => {
|
||||
it("stores the PayPal order id with its owner before approval", async () => {
|
||||
let saved: Parameters<typeof record>[0] | undefined;
|
||||
const record = async (data: {
|
||||
userId: number;
|
||||
transactionId: string;
|
||||
status: string;
|
||||
description: string;
|
||||
amount: number;
|
||||
currency: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}) => {
|
||||
saved = data;
|
||||
};
|
||||
it("stores the PayPal order id with its owner before approval", async () => {
|
||||
let saved: Parameters<typeof record>[0] | undefined;
|
||||
const record = async (data: {
|
||||
userId: number;
|
||||
transactionId: string;
|
||||
status: string;
|
||||
description: string;
|
||||
amount: number;
|
||||
currency: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}) => {
|
||||
saved = data;
|
||||
};
|
||||
|
||||
await recordCreatedTopup(
|
||||
{ userId: 99, orderId: "ORDER-123", amount: 12.5, currency: "EUR", credits: 1250 },
|
||||
record,
|
||||
);
|
||||
await recordCreatedTopup(
|
||||
{
|
||||
userId: 99,
|
||||
orderId: "ORDER-123",
|
||||
amount: 12.5,
|
||||
currency: "EUR",
|
||||
credits: 1250,
|
||||
},
|
||||
record,
|
||||
);
|
||||
|
||||
expect(saved).toMatchObject({
|
||||
userId: 99,
|
||||
transactionId: "ORDER-123",
|
||||
status: "CREATED",
|
||||
amount: 12.5,
|
||||
currency: "EUR",
|
||||
});
|
||||
});
|
||||
expect(saved).toMatchObject({
|
||||
userId: 99,
|
||||
transactionId: "ORDER-123",
|
||||
status: "CREATED",
|
||||
amount: 12.5,
|
||||
currency: "EUR",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("claimTopupDelivery", () => {
|
||||
it("rejects delivery when another request already claimed the order", async () => {
|
||||
const claim = async () => ({ count: 0 });
|
||||
it("rejects delivery when another request already claimed the order", async () => {
|
||||
const claim = async () => ({ count: 0 });
|
||||
|
||||
await expect(claimTopupDelivery(claim)).rejects.toMatchObject({
|
||||
code: "DELIVERY_ALREADY_CLAIMED",
|
||||
});
|
||||
});
|
||||
await expect(claimTopupDelivery(claim)).rejects.toMatchObject({
|
||||
code: "DELIVERY_ALREADY_CLAIMED",
|
||||
});
|
||||
});
|
||||
|
||||
it("allows delivery after atomically claiming the pending order", async () => {
|
||||
const claim = async () => ({ count: 1 });
|
||||
it("allows delivery after atomically claiming the pending order", async () => {
|
||||
const claim = async () => ({ count: 1 });
|
||||
|
||||
await expect(claimTopupDelivery(claim)).resolves.toBeUndefined();
|
||||
});
|
||||
await expect(claimTopupDelivery(claim)).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user