UNPKG

quickbooks-api

Version:

A modular TypeScript SDK for seamless integration with Intuit QuickBooks APIs. Provides robust authentication handling and future-ready foundation for accounting, payments, and commerce operations.

280 lines (221 loc) 9.29 kB
// Imports import { AuthProvider, Environment, ApiClient, AuthScopes, type PaymentOptions, QuickbooksError } from '../src/app'; import { describe, expect, test } from 'bun:test'; // Describe the Payment API describe('Live API: Payments', async () => { // Initialize the Auth Provider const authProvider = new AuthProvider( process.env.QB_CLIENT_ID!, process.env.QB_CLIENT_SECRET!, process.env.REDIRECT_URI!, [AuthScopes.Accounting], null, Environment.Sandbox, ); // Deserialize the Token await authProvider.deserializeToken(process.env.SERIALIZED_TOKEN!, process.env.SECRET_KEY!); // Setup the API Client const apiClient = new ApiClient(authProvider, Environment.Sandbox); // Test retrieving all payments test('should retrieve all Payments', async () => { // Get all payments const searchResponse = await apiClient.payments.getAllPayments(); // Test the Payments expect(searchResponse.results).toBeInstanceOf(Array); // Test the Payment length expect(searchResponse.results.length).toBeGreaterThan(0); // Test the Intuit TID expect(searchResponse.intuitTID).toBeDefined(); expect(typeof searchResponse.intuitTID).toBe('string'); }); // Test Checking for Next Page test('should check for next page', async () => { // Get all payments const searchResponse = await apiClient.payments.getAllPayments(); // Test the Payments expect(searchResponse.hasNextPage).toBe(true); // Test the Intuit TID expect(searchResponse.intuitTID).toBeDefined(); expect(typeof searchResponse.intuitTID).toBe('string'); }); // Test retrieving a single payment test('should retrieve a single payment', async () => { // Get all payments const searchResponse = await apiClient.payments.getAllPayments(); // Get the first payment const payment = searchResponse.results[0]; // Get the Payment by ID const paymentResponse = await apiClient.payments.getPaymentById(payment.Id); // Test the Payment Response Structure expect(paymentResponse).toBeDefined(); expect(paymentResponse).toHaveProperty('payment'); expect(paymentResponse).toHaveProperty('intuitTID'); expect(typeof paymentResponse.intuitTID).toBe('string'); // Test the Payment ID expect(paymentResponse.payment?.Id).toBe(payment.Id); }); // Test retrieving 10 payments test('should retrieve 10 payments', async () => { // Setup the Payment Options const paymentOptions: PaymentOptions = { searchOptions: { maxResults: 10 } }; // Get all payments const searchResponse = await apiClient.payments.getAllPayments(paymentOptions); // Test the Payments expect(searchResponse.results).toBeInstanceOf(Array); // Test the Payment length expect(searchResponse.results.length).toBeGreaterThan(0); // Test the Intuit TID expect(searchResponse.intuitTID).toBeDefined(); expect(typeof searchResponse.intuitTID).toBe('string'); }); // Test pagination test('should handle pagination', async () => { // Setup the Payment Options const paymentOptions1: PaymentOptions = { searchOptions: { maxResults: 10, page: 1 } }; const paymentOptions2: PaymentOptions = { searchOptions: { maxResults: 10, page: 2 } }; // Get all payments const searchResponse1 = await apiClient.payments.getAllPayments(paymentOptions1); const searchResponse2 = await apiClient.payments.getAllPayments(paymentOptions2); // Test the Payments expect(searchResponse1.results).toBeInstanceOf(Array); expect(searchResponse2.results).toBeInstanceOf(Array); // Test the Payment length expect(searchResponse1.results.length).toBeGreaterThan(0); expect(searchResponse2.results.length).toBeGreaterThan(0); // Test the Payments are different expect(searchResponse1.results).not.toEqual(searchResponse2.results); }); // Should handle all Search Options test('should handle all search options', async () => { // Setup the Payment Options const paymentOptions: PaymentOptions = { searchOptions: { maxResults: 10, page: 1, orderBy: { field: 'Id', direction: 'DESC' }, }, }; // Get all payments const searchResponse = await apiClient.payments.getAllPayments(paymentOptions); // Test the Payments expect(searchResponse.results).toBeInstanceOf(Array); // Test the Payment length expect(searchResponse.results.length).toBeGreaterThan(0); // loop through the payments and test each id is less than the previous one for (let i = 0; i < searchResponse.results.length - 1; i++) expect(Number(searchResponse.results[i].Id)).toBeGreaterThan(Number(searchResponse.results[i + 1].Id)); }); // Test retrieving updated payments test('should retrieve updated payments', async () => { // Get the End Date const lastUpdated = new Date(); // Set the Date to 30 days ago lastUpdated.setDate(lastUpdated.getDate() - 30); // Get the Updated Payments const searchResponse = await apiClient.payments.getUpdatedPayments(lastUpdated); // Assert the Payments expect(searchResponse.results).toBeInstanceOf(Array); }); // Test retrieving updated payments test('should retrieve updated payments', async () => { // Get the Last Updated Time const lastUpdatedTime = new Date('2012-01-08'); // Get the Payments const searchResponse = await apiClient.payments.getUpdatedPayments(lastUpdatedTime); // Test the Payments expect(searchResponse.results).toBeInstanceOf(Array); // Test the Payment length expect(searchResponse.results.length).toBeGreaterThan(0); // Test the Intuit TID expect(searchResponse.intuitTID).toBeDefined(); expect(typeof searchResponse.intuitTID).toBe('string'); }); // Test returning an empty array if no payments are updated test('should return an empty array if no payments are updated', async () => { // Setup the Future Date const futureDate = new Date(); // Set the New Full Year futureDate.setFullYear(futureDate.getFullYear() + 20); // Get the Payments const searchResponse = await apiClient.payments.getUpdatedPayments(futureDate); // Assert the Payments expect(searchResponse.results).toBeArray(); // Assert the Payments Length expect(searchResponse.results.length).toBe(0); // Test the Intuit TID expect(searchResponse.intuitTID).toBeDefined(); expect(typeof searchResponse.intuitTID).toBe('string'); }); // Test error handling for invalid ID test('should throw QuickbooksError for invalid payment ID', async () => { try { await apiClient.payments.getPaymentById('invalid'); expect(false).toBe(true); // Should not reach here } catch (error) { // Assert the Error is a QuickbooksError expect(error).toBeInstanceOf(QuickbooksError); expect(error).toBeInstanceOf(Error); // Assert the Error has the correct structure expect(error.message).toBeDefined(); expect(error.details).toBeDefined(); expect(error.details.statusCode).toBeDefined(); expect(typeof error.details.statusCode).toBe('number'); expect(error.details.intuitError).toBeDefined(); expect(Array.isArray(error.details.intuitError)).toBe(true); expect(error.details.intuitTID).toBeDefined(); expect(typeof error.details.intuitTID).toBe('string'); } }); // Test error handling for invalid raw query test('should throw QuickbooksError for invalid raw query', async () => { // Get the Query Builder const queryBuilder = await apiClient.payments.getQueryBuilder(); // Add an invalid ID filter that will cause an error queryBuilder.whereId('invalid-id-that-does-not-exist'); try { await apiClient.payments.rawPaymentQuery(queryBuilder); expect(false).toBe(true); // Should not reach here } catch (error) { // Assert the Error is a QuickbooksError expect(error).toBeInstanceOf(QuickbooksError); expect(error).toBeInstanceOf(Error); // Assert the Error has the correct structure expect(error.message).toBeDefined(); expect(error.details).toBeDefined(); expect(error.details.statusCode).toBeDefined(); expect(typeof error.details.statusCode).toBe('number'); expect(error.details.intuitError).toBeDefined(); expect(Array.isArray(error.details.intuitError)).toBe(true); expect(error.details.intuitTID).toBeDefined(); expect(typeof error.details.intuitTID).toBe('string'); } }); // Test Payment class methods describe('Payment Class Methods', () => { // Test downloading payment PDF test('should download payment PDF', async () => { // Get all payments const searchResponse = await apiClient.payments.getAllPayments({ searchOptions: { maxResults: 1 } }); // Check if we have at least one payment if (searchResponse.results.length === 0) { console.log('No payments found, skipping PDF download test'); return; } // Get the first payment const payment = searchResponse.results[0]; // Get the payment by ID to get class instance const paymentResponse = await apiClient.payments.getPaymentById(payment.Id); // Check if payment exists if (!paymentResponse.payment) { console.log('Payment not found, skipping PDF download test'); return; } // Download the PDF const pdf = await paymentResponse.payment.downloadPDF(); // Assert the PDF is a Blob expect(pdf).toBeInstanceOf(Blob); expect(pdf.type).toContain('application/pdf'); expect(pdf.size).toBeGreaterThan(0); }); }); });