diff --git a/.github/workflows/test-apps.yml b/.github/workflows/test-apps.yml index 8ac9e536a..7d0ac1e18 100644 --- a/.github/workflows/test-apps.yml +++ b/.github/workflows/test-apps.yml @@ -124,7 +124,7 @@ jobs: ECOM_STORE_ID=$ECOM_STORE_ID " > functions/.env npx cloudcommerce prepare - firebase --project=ecom2-demo emulators:start --only functions & + firebase --project=ecom2-demo emulators:start --only functions,hosting & - name: Run tests shell: bash @@ -137,5 +137,15 @@ jobs: GALAXPAY_ID: ${{ secrets.GALAXPAY_ID }} GALAXPAY_HASH: ${{ secrets.GALAXPAY_HASH }} run: | + printf "ECOM_AUTHENTICATION_ID=$ECOM_AUTHENTICATION_ID + ECOM_API_KEY=$ECOM_API_KEY + ECOM_STORE_ID=$ECOM_STORE_ID + " > .env sleep 10 pnpm test:apps + - name: check debug + if: always() + shell: bash + working-directory: store + run: | + cat firebase-debug.log diff --git a/packages/modules/src/firebase/checkout.ts b/packages/modules/src/firebase/checkout.ts index c1ac5e89f..8ce9397bf 100644 --- a/packages/modules/src/firebase/checkout.ts +++ b/packages/modules/src/firebase/checkout.ts @@ -27,7 +27,8 @@ type Item = Exclude[number] export default async (req: Request, res: Response) => { const host = req.hostname !== 'localhost' && req.hostname !== '127.0.0.1' ? `https://${req.hostname}` - : 'http://127.0.0.1:5000'; + : 'http://127.0.0.1:5000/_api/modules'; + console.log('>> debug ', host); const modulesBaseURL = `${host}${req.url.replace(/\/@?checkout[^/]*$/i, '')}`; const validate = ajv.compile(checkoutSchema.params); diff --git a/packages/modules/tests/1-modules.test.ts b/packages/modules/tests/1-modules.test.ts index a043fc4bb..9a23b3a80 100644 --- a/packages/modules/tests/1-modules.test.ts +++ b/packages/modules/tests/1-modules.test.ts @@ -1,5 +1,26 @@ -import { describe, test, expect } from 'vitest'; -import { modulesUrl } from '@cloudcommerce/test-base'; +import type { Customers, Products } from '@cloudcommerce/api/types'; +import * as dotenv from 'dotenv'; +import { + describe, + test, + expect, + beforeAll, +} from 'vitest'; +import { + modulesUrl, + getCustomerApi, + getProductApi, +} from '@cloudcommerce/test-base'; + +const requestApiModule = (moduleName: string, body) => { + return fetch(`${modulesUrl}/${moduleName}`, { + method: 'POST', + body: JSON.stringify(body), + headers: { + 'Content-Type': 'application/json', + }, + }); +}; describe('Test GET Schemas', async () => { test('@checkout', async () => { @@ -23,3 +44,116 @@ describe('Test GET Schemas', async () => { expect(req.status).toBe(200); }); }); + +describe('Test POST', async () => { + dotenv.config({ path: '../../.env' }); + + let product: Products | null = null; + let customer: Customers | null = null; + let item; + let to; + let appShippingCustom; + let appPaymentCustom; + let shipping; + let transaction; + + beforeAll(async () => { + product = await getProductApi(); + customer = await getCustomerApi('test@test.com'); + item = { + ...product, + product_id: product?._id, + quantity: 1, + }; + to = customer?.addresses?.length && { ...customer?.addresses[0] }; + const bodyCalculateShipping = { + to, + items: [item], + }; + + let req = await requestApiModule('calculate_shipping', bodyCalculateShipping); + let data = await req.json(); + appShippingCustom = data?.result?.find((app) => app.app_id === 1253); + + const bodyListPayments = { + items: [item], + }; + + req = await requestApiModule('list_payments', bodyListPayments); + data = await req.json(); + + appPaymentCustom = data?.result.find((app) => app.app_id === 108091); + }); + + // test only if the application is configured + test('calculate_shipping (App Custon shipping)', async () => { + if (appShippingCustom) { + const appShipping = appShippingCustom; + expect(appShipping).toBeDefined(); + if (to) { + shipping = { + app_id: appShipping.app_id, + to, + service_code: appShipping.response.shipping_services[0].service_code, + }; + } + } + }); + + // test only if the application is configured + test('list_payments (App Custon payment)', async () => { + if (appPaymentCustom) { + const appPayment = appPaymentCustom; + + expect(appPayment).toBeDefined(); + const { + label, + payment_method: paymentMethod, + type, + } = appPayment.response.payment_gateways[0]; + transaction = { + app_id: appPayment.app_id, + label, + payment_method: paymentMethod, + type, + buyer: { + email: 'test@test.com', + fullname: 'Usuário Teste', + birth_date: { + day: 1, + month: 1, + year: 1990, + }, + phone: { + number: '999999999', + country_code: 55, + }, + registry_type: 'p', + doc_number: '12345678912', + }, + to, + }; + } + }); + + /// test only if transaction and delivery are defined + test('@checkout', async () => { + if (shipping && transaction) { + const bodyCheckout = { + items: [item], + customer: { + name: { + given_name: 'Teste', + }, + main_email: 'test@test.com', + }, + shipping, + transaction, + }; + const req = await requestApiModule('@checkout', bodyCheckout); + console.log('debug ', await req.json()); + + expect(req.status).toBe(200); + } + }, 10000); +}); diff --git a/packages/test-base/src/get-resource-to-tests.ts b/packages/test-base/src/get-resource-to-tests.ts new file mode 100644 index 000000000..f5318630f --- /dev/null +++ b/packages/test-base/src/get-resource-to-tests.ts @@ -0,0 +1,37 @@ +import logger from 'firebase-functions/logger'; +import api from '@cloudcommerce/api'; + +const getProductApi = async (productName?: string) => { + const product = await api.get(`products?limit=1${productName ? `&name=${productName}` : ''}`).then(async ({ data }) => { + if (data.result.length) { + const projectId = data.result[0]._id; + return api.get(`products/${projectId}`).then(({ data: productFound }) => productFound); + } + return null; + }).catch((e) => { + logger.error(e); + throw e; + }); + + return product; +}; + +const getCustomerApi = async (email: string) => { + const customer = await api.get(`customers?main_email=${email}`).then(async ({ data }) => { + if (data.result.length) { + const customerId = data.result[0]._id; + return api.get(`customers/${customerId}`).then(({ data: customerFound }) => customerFound); + } + return null; + }).catch((e) => { + logger.error(e); + throw e; + }); + + return customer; +}; + +export { + getProductApi, + getCustomerApi, +}; diff --git a/packages/test-base/src/index.ts b/packages/test-base/src/index.ts index 3b7cb04a6..34dff8bdd 100644 --- a/packages/test-base/src/index.ts +++ b/packages/test-base/src/index.ts @@ -1,3 +1,5 @@ export * from './endpoints'; export * from './playloads'; + +export * from './get-resource-to-tests';