From 4233789bd46b52321867d68a78769b4884485f97 Mon Sep 17 00:00:00 2001 From: Arvin <17693119+vindard@users.noreply.github.com> Date: Fri, 12 Jan 2024 09:52:43 -0400 Subject: [PATCH] test(core): move mongoose integration tests (#3822) * test(core): move wallet-invoices integration tests * test(core): move users-repository integration tests * test(core): move wallet-onchain-pending-receive-repo integration tests --- .../services/users-repository.spec.ts} | 2 +- .../wallet-invoices-repository.spec.ts | 81 ++++++++++++++++--- .../wallet-onchain-pending-receive.spec.ts | 0 .../services/mongoose/wallet-invoices.spec.ts | 78 ------------------ 4 files changed, 71 insertions(+), 90 deletions(-) rename core/api/test/{legacy-integration/services/mongoose/user-repository.spec.ts => integration/services/users-repository.spec.ts} (97%) rename core/api/test/{legacy-integration/services/mongoose => integration/services}/wallet-onchain-pending-receive.spec.ts (100%) delete mode 100644 core/api/test/legacy-integration/services/mongoose/wallet-invoices.spec.ts diff --git a/core/api/test/legacy-integration/services/mongoose/user-repository.spec.ts b/core/api/test/integration/services/users-repository.spec.ts similarity index 97% rename from core/api/test/legacy-integration/services/mongoose/user-repository.spec.ts rename to core/api/test/integration/services/users-repository.spec.ts index a54b8331af..2cb3fcfd1a 100644 --- a/core/api/test/legacy-integration/services/mongoose/user-repository.spec.ts +++ b/core/api/test/integration/services/users-repository.spec.ts @@ -5,7 +5,7 @@ import { randomUserId, randomPhone } from "test/helpers" const users = UsersRepository() -describe("Testing Users Repository", () => { +describe("Users Repository", () => { it("return default value if userId doesn't exist", async () => { const userId = randomUserId() diff --git a/core/api/test/integration/services/wallet-invoices-repository.spec.ts b/core/api/test/integration/services/wallet-invoices-repository.spec.ts index 94ee1ae7b8..a159c24b80 100644 --- a/core/api/test/integration/services/wallet-invoices-repository.spec.ts +++ b/core/api/test/integration/services/wallet-invoices-repository.spec.ts @@ -3,6 +3,8 @@ import { WalletCurrency } from "@/domain/shared" import { WalletInvoicesRepository } from "@/services/mongoose" import { createMockWalletInvoice } from "test/helpers/wallet-invoices" +const walletInvoices = WalletInvoicesRepository() + const recipientWalletDescriptor = { currency: WalletCurrency.Btc, id: crypto.randomUUID() as WalletId, @@ -11,11 +13,9 @@ const recipientWalletDescriptor = { let createdInvoices: WalletInvoice[] = [] beforeAll(async () => { - const walletInvoicesRepository = WalletInvoicesRepository() - // Create 5 invoices for the same wallet for (let i = 0; i < 5; i++) { - const invoice = await walletInvoicesRepository.persistNew( + const invoice = await walletInvoices.persistNew( createMockWalletInvoice(recipientWalletDescriptor), ) if (invoice instanceof Error) throw invoice @@ -28,7 +28,7 @@ beforeAll(async () => { }) // Create an invoice for a different wallet - await walletInvoicesRepository.persistNew( + await walletInvoices.persistNew( createMockWalletInvoice({ currency: WalletCurrency.Btc, id: crypto.randomUUID() as WalletId, @@ -37,11 +37,11 @@ beforeAll(async () => { }) describe("WalletInvoicesRepository", () => { - describe("getInvoicesForWallet", () => { - const walletInvoicesRepository = WalletInvoicesRepository() + describe("findInvoicesForWallet", () => { + const walletInvoices = WalletInvoicesRepository() it("gets first page of invoices", async () => { - const result = await walletInvoicesRepository.findInvoicesForWallets({ + const result = await walletInvoices.findInvoicesForWallets({ walletIds: [recipientWalletDescriptor.id], paginationArgs: { first: 100, @@ -69,7 +69,7 @@ describe("WalletInvoicesRepository", () => { }) it("gets page after cursor", async () => { - const result = await walletInvoicesRepository.findInvoicesForWallets({ + const result = await walletInvoices.findInvoicesForWallets({ walletIds: [recipientWalletDescriptor.id], paginationArgs: { first: 2, @@ -101,7 +101,7 @@ describe("WalletInvoicesRepository", () => { }) it("get last page of invoices", async () => { - const result = await walletInvoicesRepository.findInvoicesForWallets({ + const result = await walletInvoices.findInvoicesForWallets({ walletIds: [recipientWalletDescriptor.id], paginationArgs: { last: 100, @@ -120,7 +120,7 @@ describe("WalletInvoicesRepository", () => { }) it("get page before cursor", async () => { - const result = await walletInvoicesRepository.findInvoicesForWallets({ + const result = await walletInvoices.findInvoicesForWallets({ walletIds: [recipientWalletDescriptor.id], paginationArgs: { last: 2, @@ -154,7 +154,7 @@ describe("WalletInvoicesRepository", () => { }) it("returns empty edges for wallet without invoices", async () => { - const result = await walletInvoicesRepository.findInvoicesForWallets({ + const result = await walletInvoices.findInvoicesForWallets({ walletIds: [crypto.randomUUID() as WalletId], paginationArgs: { first: 100, @@ -173,4 +173,63 @@ describe("WalletInvoicesRepository", () => { ) }) }) + + describe("findByPaymentHash", () => { + it("finds an invoice", async () => { + const walletInvoice = createdInvoices[0] + const { paymentHash } = walletInvoice + const lookedUpInvoice = await walletInvoices.findByPaymentHash(paymentHash) + if (lookedUpInvoice instanceof Error) throw lookedUpInvoice + + const dateDifference = Math.abs( + lookedUpInvoice.createdAt.getTime() - walletInvoice.createdAt.getTime(), + ) + expect(dateDifference).toBeLessThanOrEqual(10) // 10ms + + lookedUpInvoice.createdAt = walletInvoice.createdAt = new Date() + expect(lookedUpInvoice).toEqual(walletInvoice) + }) + }) + + describe("markAsPaid", () => { + it("marks an invoice as paid", async () => { + const { paymentHash } = createdInvoices[0] + + const lookedUpInvoice = await walletInvoices.findByPaymentHash(paymentHash) + expect(lookedUpInvoice).toHaveProperty("paid", false) + expect(lookedUpInvoice).toHaveProperty("processingCompleted", false) + + const updatedResult = await walletInvoices.markAsPaid(paymentHash) + expect(updatedResult).not.toBeInstanceOf(Error) + expect(updatedResult).toHaveProperty("paid", true) + expect(updatedResult).toHaveProperty("processingCompleted", true) + + const lookedUpInvoiceAfter = await walletInvoices.findByPaymentHash(paymentHash) + expect(lookedUpInvoiceAfter).not.toBeInstanceOf(Error) + expect(lookedUpInvoiceAfter).toEqual(updatedResult) + expect(lookedUpInvoiceAfter).toHaveProperty("paid", true) + expect(updatedResult).toHaveProperty("processingCompleted", true) + }) + }) + + describe("markAsProcessingCompleted", () => { + it("marks an invoice as processing completed", async () => { + const { paymentHash } = createdInvoices[1] + + const lookedUpInvoice = await walletInvoices.findByPaymentHash(paymentHash) + expect(lookedUpInvoice).toHaveProperty("paid", false) + expect(lookedUpInvoice).toHaveProperty("processingCompleted", false) + + const updatedResult = await walletInvoices.markAsProcessingCompleted(paymentHash) + expect(updatedResult).not.toBeInstanceOf(Error) + expect(updatedResult).toHaveProperty("processingCompleted", true) + expect(updatedResult).toHaveProperty("paid", false) + + const lookedUpInvoiceAfter = await walletInvoices.findByPaymentHash(paymentHash) + expect(lookedUpInvoiceAfter).not.toBeInstanceOf(Error) + expect(lookedUpInvoiceAfter).toEqual(updatedResult) + expect(updatedResult).toHaveProperty("processingCompleted", true) + expect(lookedUpInvoiceAfter).toHaveProperty("paid", false) + }) + }) }) diff --git a/core/api/test/legacy-integration/services/mongoose/wallet-onchain-pending-receive.spec.ts b/core/api/test/integration/services/wallet-onchain-pending-receive.spec.ts similarity index 100% rename from core/api/test/legacy-integration/services/mongoose/wallet-onchain-pending-receive.spec.ts rename to core/api/test/integration/services/wallet-onchain-pending-receive.spec.ts diff --git a/core/api/test/legacy-integration/services/mongoose/wallet-invoices.spec.ts b/core/api/test/legacy-integration/services/mongoose/wallet-invoices.spec.ts deleted file mode 100644 index b9253713e0..0000000000 --- a/core/api/test/legacy-integration/services/mongoose/wallet-invoices.spec.ts +++ /dev/null @@ -1,78 +0,0 @@ -import crypto from "crypto" - -import { WalletCurrency } from "@/domain/shared" -import { WalletInvoicesRepository } from "@/services/mongoose" - -import { createUserAndWalletFromPhone, randomPhone } from "test/helpers" -import { createMockWalletInvoice } from "test/helpers/wallet-invoices" - -const phoneB = randomPhone() - -beforeAll(async () => { - await createUserAndWalletFromPhone(phoneB) -}) - -describe("WalletInvoices", () => { - const walletDescriptor = { - currency: WalletCurrency.Btc, - id: crypto.randomUUID() as WalletId, - } - it("persists and finds an invoice", async () => { - const repo = WalletInvoicesRepository() - const invoiceToPersist = createMockWalletInvoice(walletDescriptor) - const persistResult = await repo.persistNew(invoiceToPersist) - expect(persistResult).not.toBeInstanceOf(Error) - - const { paymentHash } = persistResult as WalletInvoice - const lookedUpInvoice = await repo.findByPaymentHash(paymentHash) - if (lookedUpInvoice instanceof Error) throw lookedUpInvoice - - const dateDifference = Math.abs( - lookedUpInvoice.createdAt.getTime() - invoiceToPersist.createdAt.getTime(), - ) - expect(dateDifference).toBeLessThanOrEqual(10) // 10ms - - lookedUpInvoice.createdAt = invoiceToPersist.createdAt = new Date() - expect(lookedUpInvoice).toEqual(invoiceToPersist) - }) - - it("marks an invoice as paid", async () => { - const repo = WalletInvoicesRepository() - const invoiceToPersist = createMockWalletInvoice(walletDescriptor) - const persistResult = await repo.persistNew(invoiceToPersist) - expect(persistResult).not.toBeInstanceOf(Error) - - const invoiceToUpdate = persistResult as WalletInvoice - const updatedResult = await repo.markAsPaid(invoiceToUpdate.paymentHash) - expect(updatedResult).not.toBeInstanceOf(Error) - expect(updatedResult).toHaveProperty("paid", true) - expect(updatedResult).toHaveProperty("processingCompleted", true) - - const { paymentHash } = updatedResult as WalletInvoice - const lookedUpInvoice = await repo.findByPaymentHash(paymentHash) - expect(lookedUpInvoice).not.toBeInstanceOf(Error) - expect(lookedUpInvoice).toEqual(updatedResult) - expect(lookedUpInvoice).toHaveProperty("paid", true) - expect(updatedResult).toHaveProperty("processingCompleted", true) - }) - - it("marks an invoice as processing completed", async () => { - const repo = WalletInvoicesRepository() - const invoiceToPersist = createMockWalletInvoice(walletDescriptor) - const persistResult = await repo.persistNew(invoiceToPersist) - expect(persistResult).not.toBeInstanceOf(Error) - - const invoiceToUpdate = persistResult as WalletInvoice - const updatedResult = await repo.markAsProcessingCompleted( - invoiceToUpdate.paymentHash, - ) - expect(updatedResult).not.toBeInstanceOf(Error) - expect(updatedResult).toHaveProperty("processingCompleted", true) - - const { paymentHash } = updatedResult as WalletInvoice - const lookedUpInvoice = await repo.findByPaymentHash(paymentHash) - expect(lookedUpInvoice).not.toBeInstanceOf(Error) - expect(lookedUpInvoice).toEqual(updatedResult) - expect(lookedUpInvoice).toHaveProperty("processingCompleted", true) - }) -})