bp - test transfer 1st darft
MODIFY
test/api/BillingPlatformTest.ts
test/api/BillingPlatformTest.ts
@@ -52,308 +52,6 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
billingTestHelper.initUserContextAsAdmin();
});
- describe('Sub accounts', () => {
- it('should create a sub account', async () => {
- const response = await billingTestHelper.userService.billingApi.createSubAccount({
- businessOwnerID: billingTestHelper.userContext.id
- });
- const subAccount = response.data as BillingAccount;
- expect(response.status).to.be.eq(StatusCodes.CREATED);
- expect(subAccount.id).to.not.be.null;
- expect(subAccount.accountExternalID).to.not.be.null;
- expect(subAccount.businessOwnerID).to.eq(billingTestHelper.userContext.id);
- expect(subAccount.activationLink).to.not.be.null;
- expect(subAccount.status).to.be.eq(BillingAccountStatus.IDLE);
- });
-
- it('should create a sub account and activate it', async () => {
- // Create a sub account
- const response = await billingTestHelper.userService.billingApi.createSubAccount({
- businessOwnerID: billingTestHelper.userContext.id
- });
- expect(response.status).to.be.eq(StatusCodes.CREATED);
- // Send the activation link
- const subAccountOnboardResponse = await billingTestHelper.userService.billingApi.sendSubAccountOnboarding(response.data.id);
- expect(subAccountOnboardResponse.status).to.be.eq(StatusCodes.OK);
- expect(subAccountOnboardResponse.data.status).to.be.eq(BillingAccountStatus.PENDING);
- // Activate the sub account
- const activationResponse = await billingTestHelper.userService.billingApi.activateSubAccount({ accountID: response.data.id, TenantID: billingTestHelper.tenantContext.getTenant().id });
- expect(activationResponse.status).to.be.eq(StatusCodes.OK);
- expect(activationResponse.data.status).to.be.eq(BillingAccountStatus.ACTIVE);
- });
-
- it('should not activate an inexistent sub account', async () => {
- const activationResponse = await billingTestHelper.userService.billingApi.activateSubAccount({ accountID: '5ce249a1a39ae1c056c389bd', TenantID: billingTestHelper.tenantContext.getTenant().id });
- expect(activationResponse.status).to.be.eq(StatusCodes.NOT_FOUND);
- });
-
- it('should not activate a sub account twice', async () => {
- // Create a sub account
- const response = await billingTestHelper.userService.billingApi.createSubAccount({
- businessOwnerID: billingTestHelper.userContext.id
- });
- expect(response.status).to.be.eq(StatusCodes.CREATED);
- // Send the activation link
- const subAccountOnboardResponse = await billingTestHelper.userService.billingApi.sendSubAccountOnboarding(response.data.id);
- expect(subAccountOnboardResponse.status).to.be.eq(StatusCodes.OK);
- // Activate the sub account
- let activationResponse = await billingTestHelper.userService.billingApi.activateSubAccount({ accountID: response.data.id, TenantID: billingTestHelper.tenantContext.getTenant().id });
- expect(activationResponse.status).to.be.eq(StatusCodes.OK);
- activationResponse = await billingTestHelper.userService.billingApi.activateSubAccount({ accountID: response.data.id, TenantID: billingTestHelper.tenantContext.getTenant().id });
- expect(activationResponse.status).to.be.eq(StatusCodes.INTERNAL_SERVER_ERROR);
- });
-
- it('should create a company assigned to a sub-account', async () => {
- const subAccountResponse = await billingTestHelper.userService.billingApi.createSubAccount({
- businessOwnerID: billingTestHelper.userContext.id
- });
- expect(subAccountResponse.status).to.be.eq(StatusCodes.CREATED);
-
- let companyResponse = await billingTestHelper.userService.companyApi.create({
- ...CompanyFactory.build(),
- accountData: {
- accountID: subAccountResponse.data.id
- }
- });
- expect(companyResponse.status).to.be.eq(StatusCodes.OK);
- companyResponse = await billingTestHelper.userService.companyApi.readById(companyResponse.data.id);
- expect(companyResponse.data.accountData.accountID).to.eq(subAccountResponse.data.id);
- });
-
- it('should update a company to assign a sub-account', async () => {
- const subAccountResponse = await billingTestHelper.userService.billingApi.createSubAccount({
- businessOwnerID: billingTestHelper.userContext.id
- });
- expect(subAccountResponse.status).to.be.eq(StatusCodes.CREATED);
-
- let companyResponse = await billingTestHelper.userService.companyApi.create(CompanyFactory.build());
- expect(companyResponse.status).to.be.eq(StatusCodes.OK);
- const companyID = companyResponse.data.id;
- companyResponse = await billingTestHelper.userService.companyApi.update({
- id: companyID,
- ...CompanyFactory.build(),
- accountData: { accountID: subAccountResponse.data.id }
- });
- expect(companyResponse.status).to.be.eq(StatusCodes.OK);
-
- companyResponse = await billingTestHelper.userService.companyApi.readById(companyID);
- expect(companyResponse.data.accountData.accountID).to.eq(subAccountResponse.data.id);
- });
-
- it('should create a site assigned to a sub-account', async () => {
- const subAccountResponse = await billingTestHelper.userService.billingApi.createSubAccount({
- businessOwnerID: billingTestHelper.userContext.id
- });
- expect(subAccountResponse.status).to.be.eq(StatusCodes.CREATED);
-
- // Create a company
- const companyResponse = await billingTestHelper.userService.companyApi.create(CompanyFactory.build());
- expect(companyResponse.status).to.be.eq(StatusCodes.OK);
- // Create a site
- let siteResponse = await billingTestHelper.userService.siteApi.create({
- ...SiteFactory.build(),
- companyID: companyResponse.data.id,
- accountData: {
- accountID: subAccountResponse.data.id
- }
- });
- expect(siteResponse.status).to.be.eq(StatusCodes.OK);
- siteResponse = await billingTestHelper.userService.siteApi.readById(siteResponse.data.id);
- expect(siteResponse.data.accountData.accountID).to.eq(subAccountResponse.data.id);
- });
-
- it('should update a site to assign a sub-account', async () => {
- const subAccountResponse = await billingTestHelper.userService.billingApi.createSubAccount({
- businessOwnerID: billingTestHelper.userContext.id
- });
- expect(subAccountResponse.status).to.be.eq(StatusCodes.CREATED);
-
- // Create a company
- const companyResponse = await billingTestHelper.userService.companyApi.create(CompanyFactory.build());
- expect(companyResponse.status).to.be.eq(StatusCodes.OK);
- // Create a site
- let siteResponse = await billingTestHelper.userService.siteApi.create({
- ...SiteFactory.build(),
- companyID: companyResponse.data.id,
- });
- expect(siteResponse.status).to.be.eq(StatusCodes.OK);
- const siteID = siteResponse.data.id;
-
- siteResponse = await billingTestHelper.userService.siteApi.update({
- id: siteID,
- ...SiteFactory.build(),
- companyID: companyResponse.data.id,
- accountData: { accountID: subAccountResponse.data.id }
- });
- expect(siteResponse.status).to.be.eq(StatusCodes.OK);
-
- siteResponse = await billingTestHelper.userService.siteApi.readById(siteID);
- expect(siteResponse.data.accountData.accountID).to.eq(subAccountResponse.data.id);
- });
-
- it('should list sub-accounts', async () => {
- const subAccountResponse = await billingTestHelper.userService.billingApi.createSubAccount({
- businessOwnerID: billingTestHelper.userContext.id
- });
- expect(subAccountResponse.status).to.be.eq(StatusCodes.CREATED);
-
- // List sub-accounts
- const subAccountsResponse = await billingTestHelper.userService.billingApi.readSubAccounts({
- userID: billingTestHelper.userContext.id
- });
- expect(subAccountsResponse.status).to.be.eq(StatusCodes.OK);
- expect(subAccountsResponse.data.result.map((subAccount: BillingAccount) => subAccount.id)).to.include(subAccountResponse.data.id);
- });
-
- it('should read sub-account', async () => {
- const subAccountCreateResponse = await billingTestHelper.userService.billingApi.createSubAccount({
- businessOwnerID: billingTestHelper.userContext.id
- });
- expect(subAccountCreateResponse.status).to.be.eq(StatusCodes.CREATED);
-
- // Get the sub-account
- const subAccountResponse = await billingTestHelper.userService.billingApi.readSubAccount(subAccountCreateResponse.data.id);
- const subAccount = subAccountResponse.data as BillingAccount;
- expect(subAccountResponse.status).to.be.eq(StatusCodes.OK);
- expect(subAccount.id).to.be.eq(subAccountCreateResponse.data.id);
- expect(subAccount.businessOwnerID).to.be.eq(billingTestHelper.userContext.id);
- expect(subAccount.status).to.be.eq(BillingAccountStatus.IDLE);
- });
-
- it('should send sub-account onboarding', async () => {
- const subAccountCreateResponse = await billingTestHelper.userService.billingApi.createSubAccount({
- businessOwnerID: billingTestHelper.userContext.id
- });
- expect(subAccountCreateResponse.status).to.be.eq(StatusCodes.CREATED);
-
- const subAccountOnboardResponse = await billingTestHelper.userService.billingApi.sendSubAccountOnboarding(subAccountCreateResponse.data.id);
- expect(subAccountOnboardResponse.status).to.be.eq(StatusCodes.OK);
- expect(subAccountOnboardResponse.data.status).to.be.eq(BillingAccountStatus.PENDING);
- });
-
- it('should not able to send sub-account onboarding twice', async () => {
- const subAccountCreateResponse = await billingTestHelper.userService.billingApi.createSubAccount({
- businessOwnerID: billingTestHelper.userContext.id
- });
- expect(subAccountCreateResponse.status).to.be.eq(StatusCodes.CREATED);
-
- let subAccountOnboardResponse = await billingTestHelper.userService.billingApi.sendSubAccountOnboarding(subAccountCreateResponse.data.id);
- expect(subAccountOnboardResponse.status).to.be.eq(StatusCodes.OK);
- expect(subAccountOnboardResponse.data.status).to.be.eq(BillingAccountStatus.PENDING);
-
- subAccountOnboardResponse = await billingTestHelper.userService.billingApi.sendSubAccountOnboarding(subAccountCreateResponse.data.id);
- expect(subAccountOnboardResponse.status).to.be.eq(StatusCodes.INTERNAL_SERVER_ERROR);
- });
-
- it('should not able to send sub-account onboarding for an activated sub-account', async () => {
- // Create the sub account
- const subAccountCreateResponse = await billingTestHelper.userService.billingApi.createSubAccount({
- businessOwnerID: billingTestHelper.userContext.id
- });
- expect(subAccountCreateResponse.status).to.be.eq(StatusCodes.CREATED);
- // Send onboarding
- let subAccountOnboardResponse = await billingTestHelper.userService.billingApi.sendSubAccountOnboarding(subAccountCreateResponse.data.id);
- expect(subAccountOnboardResponse.status).to.be.eq(StatusCodes.OK);
- expect(subAccountOnboardResponse.data.status).to.be.eq(BillingAccountStatus.PENDING);
- // Activate it
- const activationResponse = await billingTestHelper.userService.billingApi.activateSubAccount({ accountID: subAccountCreateResponse.data.id, TenantID: billingTestHelper.tenantContext.getTenant().id });
- expect(activationResponse.status).to.be.eq(StatusCodes.OK);
- expect(activationResponse.data.status).to.be.eq(BillingAccountStatus.ACTIVE);
- // Try to re-send onboarding
- subAccountOnboardResponse = await billingTestHelper.userService.billingApi.sendSubAccountOnboarding(subAccountCreateResponse.data.id);
- expect(subAccountOnboardResponse.status).to.be.eq(StatusCodes.INTERNAL_SERVER_ERROR);
- });
- });
-
- describe('Transfers', () => {
- it('should list transfers', async () => {
- const transfer = BillingTransferFactory.build();
- const transferID = await BillingStorage.saveTransfer(billingTestHelper.tenantContext.getTenant(), transfer);
- transfer.id = transferID;
- // const transfersResponse = await billingTestHelper.userService.billingApi.readTransfers({},{ limit: 1, skip: 0 }, [{ field: '-createdOn' }]);
- const transfersResponse = await billingTestHelper.userService.billingApi.readTransfers({ ID: transferID });
- expect(transfersResponse.status).to.be.eq(StatusCodes.OK);
- const savedTransfer = transfersResponse.data.result?.[0];
- expect(savedTransfer).not.to.be.null;
- delete savedTransfer.createdOn;
- delete savedTransfer.createdBy;
- delete savedTransfer.lastChangedOn;
- delete savedTransfer.lastChangedBy;
- expect(savedTransfer).to.containSubset(transfer);
- });
- });
- });
-
- describe('Where basic user', () => {
-
- beforeAll(async () => {
- billingTestHelper.billingImpl = await billingTestHelper.setBillingSystemValidCredentials();
- billingTestHelper.userContext = billingTestHelper.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.BASIC_USER);
- billingTestHelper.userService = new CentralServerService(
- billingTestHelper.tenantContext.getTenant().subdomain,
- billingTestHelper.userContext
- );
- expect(billingTestHelper.userService).to.not.be.null;
- });
-
- it('should not be able to create a sub account', async () => {
- const response = await billingTestHelper.userService.billingApi.createSubAccount({
- businessOwnerID: billingTestHelper.userContext.id
- });
- expect(response.status).to.be.eq(StatusCodes.FORBIDDEN);
- });
-
- it('should not activate an inexistent sub account', async () => {
- const activationResponse = await billingTestHelper.userService.billingApi.activateSubAccount({ accountID: '5ce249a1a39ae1c056c389bd', TenantID: billingTestHelper.tenantContext.getTenant().id });
- expect(activationResponse.status).to.be.eq(StatusCodes.NOT_FOUND);
- });
-
- it('should not be able to list sub-accounts', async () => {
- const subAccountsResponse = await billingTestHelper.userService.billingApi.readSubAccounts({});
- expect(subAccountsResponse.status).to.be.eq(StatusCodes.FORBIDDEN);
- });
-
- it('should not be able to read sub-account', async () => {
- // List sub-accounts
- const subAccountResponse = await billingTestHelper.userService.billingApi.readSubAccount('62978713f146ea8cb3bf8a95');
- expect(subAccountResponse.status).to.be.eq(StatusCodes.FORBIDDEN);
- });
-
- it('should not be able to send sub-account onboarding', async () => {
- // List sub-accounts
- const subAccountResponse = await billingTestHelper.userService.billingApi.sendSubAccountOnboarding('62978713f146ea8cb3bf8a95');
- expect(subAccountResponse.status).to.be.eq(StatusCodes.FORBIDDEN);
- });
-
- it('should not be able to list transfers', async () => {
- const transfersResponse = await billingTestHelper.userService.billingApi.readTransfers({});
- expect(transfersResponse.status).to.be.eq(StatusCodes.FORBIDDEN);
- });
- });
-
- describe('Storage', () => {
- it('should save a billing transfer', async () => {
- const transfer = BillingTransferFactory.build();
- const transferID = await BillingStorage.saveTransfer(billingTestHelper.tenantContext.getTenant(), transfer);
- expect(transferID).to.not.be.null;
-
- const retrievedTransfer = await BillingStorage.getTransferByID(billingTestHelper.tenantContext.getTenant(), transferID);
- expect(retrievedTransfer).to.containSubset(transfer);
- });
-
- it('should list billing transfers', async () => {
- const transfers = [
- BillingTransferFactory.build(),
- BillingTransferFactory.build(),
- ];
- const ids = await Promise.all(transfers.map(async (transfer) => BillingStorage.saveTransfer(billingTestHelper.tenantContext.getTenant(), transfer)));
-
- const retrievedTransfers = await BillingStorage.getTransfers(billingTestHelper.tenantContext.getTenant(), {}, Constants.DB_PARAMS_MAX_LIMIT);
- expect(retrievedTransfers.result.map((transfer) => transfer.id)).to.include.members(ids);
- });
- });
-
- describe('Transfer of Funds', () => {
// eslint-disable-next-line @typescript-eslint/require-await
beforeAll(async () => {
billingTestHelper.initUserContextAsAdmin();

bp - test transfer 1st darft