Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] chore(test-base-modules): Get resource in API for real testing and more test in modules #417

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .github/workflows/test-apps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion packages/modules/src/firebase/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type Item = Exclude<OrderSet['items'], undefined>[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);
Expand Down
138 changes: 136 additions & 2 deletions packages/modules/tests/1-modules.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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);
});
37 changes: 37 additions & 0 deletions packages/test-base/src/get-resource-to-tests.ts
Original file line number Diff line number Diff line change
@@ -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,
};
2 changes: 2 additions & 0 deletions packages/test-base/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './endpoints';

export * from './playloads';

export * from './get-resource-to-tests';
Loading