diff --git a/e2e_tests/account_test.py b/e2e_tests/account_test.py index 202e6c46..f8991fc2 100644 --- a/e2e_tests/account_test.py +++ b/e2e_tests/account_test.py @@ -1,79 +1,54 @@ import os import unittest -from datetime import timedelta +import pytest -from e2e_tests.application_test import create_business_application from unit import Unit from unit.models.account import * -from unit.models.application import CreateIndividualApplicationRequest -from e2e_tests.helpers.helpers import create_relationship +from e2e_tests.helpers.helpers import create_deposit_account, close_credit_account, create_individual_customer,\ + create_deposit_account_for_business, create_credit_account_for_business token = os.environ.get('TOKEN') client = Unit("https://api.s.unit.sh", token) -def create_individual_customer(): - request = CreateIndividualApplicationRequest( - FullName("Jhon", "Doe"), date.today() - timedelta(days=20 * 365), - Address("1600 Pennsylvania Avenue Northwest", "Washington", "CA", "20500", "US"), - "jone.doe1@unit-finance.com", - Phone("1", "2025550108"), ssn="721074426", - ) - response = client.applications.create(request) - for key, value in response.data.relationships.items(): - if key == "customer": - return value.id +@pytest.fixture +def credit_account(): + return create_credit_account_for_business(client).data - return "" +@pytest.fixture +def deposit_account(): + return create_deposit_account(client).data -def create_business_customer(): - b_app = create_business_application().data - return b_app.relationships.get("customer").id - -def create_deposit_account(): - customer_id = create_individual_customer() - request = CreateDepositAccountRequest("checking", - {"customer": Relationship("customer", customer_id)}, - {"purpose": "checking"}) - return client.accounts.create(request) - - -def test_create_deposit_account(): - response = create_deposit_account() - assert response.data.type == "depositAccount" - - -def create_deposit_account_for_business(): - customer_id = create_business_customer() - - request = CreateDepositAccountRequest("checking", - {"customer": Relationship("customer", customer_id)}, - {"purpose": "checking"}) - return client.accounts.create(request) +@pytest.fixture() +def account_with_owners(deposit_account): + account_id = deposit_account.id + customer_ids = [create_individual_customer(client), create_individual_customer(client)] + return client.accounts.add_owners(AccountOwnersRequest(account_id, + RelationshipArray.from_ids_array("customer", + customer_ids))).data -def create_credit_account_for_business(): - customer_id = create_business_customer() - request = CreateCreditAccountRequest("credit_terms_test", 20000, create_relationship("customer", customer_id), - {"purpose": "some_purpose"}) - return client.accounts.create(request) +def test_create_deposit_account(deposit_account): + assert deposit_account.type == "depositAccount" -def test_create_credit_account_for_business(): - response = create_credit_account_for_business() - assert response.data.type == "creditAccount" +def test_create_credit_account_for_business(credit_account): + assert credit_account + assert credit_account.type == "creditAccount" + res = close_credit_account(client, credit_account.id) + assert res.data.attributes.get("status").__eq__("Closed") def test_create_deposit_account_for_business(): - response = create_deposit_account_for_business() - assert response.data.type == "depositAccount" + account = create_deposit_account_for_business(client).data + assert account.type == "depositAccount" def test_create_joint_deposit_account(): - customer_id1 = create_individual_customer() - customer_id2 = create_individual_customer() + customer_id1 = create_individual_customer(client) + customer_id2 = create_individual_customer(client) request = CreateDepositAccountRequest("checking", {"customers": RelationshipArray([ Relationship("customer", customer_id1), @@ -83,8 +58,8 @@ def test_create_joint_deposit_account(): assert response.data.type == "depositAccount" -def test_get_account(): - account_id = create_deposit_account().data.id +def test_get_deposit_account(deposit_account): + account_id = deposit_account.id response = client.accounts.get(account_id, "customer") assert response.data.type == "depositAccount" and isinstance(response.included, list) @@ -95,30 +70,44 @@ def test_list_accounts(): assert acc.type == "depositAccount" -def test_limits_account(): - account_id = create_deposit_account().data.id +def test_list_credit_accounts(credit_account): + assert credit_account + + response = client.accounts.list(ListAccountParams(_type="credit")) + assert response.data is not None + for acc in response.data: + assert acc.type == "creditAccount" + + accounts_to_close = list(filter(lambda x: x.attributes.get("status").__eq__("Open"), response.data)) + for acc in accounts_to_close: + res = close_credit_account(client, acc.id) + assert res.data.attributes.get("status").__eq__("Closed") + + +def test_limits_account(deposit_account): + account_id = deposit_account.id response = client.accounts.limits(account_id) assert response.data.type == "limits" -def test_close_account(): - account_id = create_deposit_account().data.id - requet = CloseAccountRequest(account_id, "Fraud") - response = client.accounts.close_account(requet) +def test_close_account(deposit_account): + account_id = deposit_account.id + request = CloseDepositAccountRequest(account_id, "Fraud") + response = client.accounts.close_account(request) assert response.data.type == "depositAccount" -def test_close_and_reopen_account(): - account_id = create_deposit_account().data.id - requet = CloseAccountRequest(account_id) - response = client.accounts.close_account(requet) +def test_close_and_reopen_account(deposit_account): + account_id = deposit_account.id + request = CloseDepositAccountRequest(account_id) + response = client.accounts.close_account(request) assert response.data.type == "depositAccount" response = client.accounts.reopen_account(account_id) assert response.data.type == "depositAccount" -def test_update_account(): - account_id = create_deposit_account().data.id +def test_update_account(deposit_account): + account_id = deposit_account.id request = PatchDepositAccountRequest(account_id, tags={ "purpose": "tax", "trackUserId": "userId_fe6885b5815463b26f65e71095832bdd916890f7"}) @@ -127,8 +116,8 @@ def test_update_account(): assert response.data.attributes.get("tags").get("purpose") == "tax" -def test_update_credit_account(): - account_id = create_credit_account_for_business().data.id +def test_update_credit_account(credit_account): + account_id = credit_account.id _credit_limit = 40000 request = PatchCreditAccountRequest(account_id, tags={ "purpose": "tax", @@ -140,9 +129,9 @@ def test_update_credit_account(): assert response.data.attributes.get("tags").get("purpose") == "tax" -def test_get_deposit_products(): - response = create_deposit_account() - assert response.data.type == "depositAccount" +def test_get_deposit_products(deposit_account): + assert deposit_account.type == "depositAccount" + response = client.accounts.list() assert len(response.data) > 0 @@ -152,26 +141,19 @@ def test_get_deposit_products(): assert dp.type == "accountDepositProduct" -def add_owners(): - account_id = create_deposit_account().data.id - customer_ids = [create_individual_customer(), create_individual_customer()] - return client.accounts.add_owners(AccountOwnersRequest(account_id, - RelationshipArray.from_ids_array("customer", - customer_ids))) - +def test_add_owners(account_with_owners): + assert account_with_owners.type == "depositAccount" + assert account_with_owners.relationships["customers"].data is not None + assert len(account_with_owners.relationships["customers"].data) == 3 -def test_add_owners(): - response = add_owners() - assert response.data.type == "depositAccount" - assert response.data.relationships["customers"].data is not None - assert len(response.data.relationships["customers"].data) == 3 +def test_remove_owners(account_with_owners): + assert account_with_owners.type == "depositAccount" + account_id = account_with_owners.id -def test_remove_owners(): - response = add_owners() - assert response.data.type == "depositAccount" - account_id = response.data.id - last_owner_id = response.data.relationships["customers"].data.pop().id # An account should have at least one owner - response = client.accounts.remove_owners(AccountOwnersRequest(account_id, response.data.relationships["customers"])) + # Account should have at least one owner + last_owner_id = account_with_owners.relationships["customers"].data.pop().id + response = client.accounts.remove_owners(AccountOwnersRequest(account_id, + account_with_owners.relationships["customers"])) assert response.data.type == "depositAccount" assert response.data.relationships.get("customer").id == last_owner_id diff --git a/e2e_tests/application_test.py b/e2e_tests/application_test.py index 123c73a7..fd6901fb 100644 --- a/e2e_tests/application_test.py +++ b/e2e_tests/application_test.py @@ -1,6 +1,8 @@ import os import uuid from datetime import timedelta + +from e2e_tests.helpers.helpers import create_business_application from unit import Unit from unit.models.application import * @@ -34,32 +36,11 @@ def test_create_individual_application(): assert app.data.type == "individualApplication" -def create_business_application(): - request = CreateBusinessApplicationRequest( - name="Acme Inc.", - address=Address("1600 Pennsylvania Avenue Northwest", "Washington", "CA", "20500", "US"), - phone=Phone("1", "9294723497"), state_of_incorporation="CA", entity_type="Corporation", ein="123456789", - officer=Officer(full_name=FullName("Jone", "Doe"), date_of_birth=date.today() - timedelta(days=20 * 365), - address=Address("950 Allerton Street", "Redwood City", "CA", "94063", "US"), - phone=Phone("1", "2025550108"), email="jone.doe@unit-finance.com", ssn="123456789"), - contact=BusinessContact(full_name=FullName("Jone", "Doe"), email="jone.doe@unit-finance.com", phone=Phone("1", "2025550108")), - beneficial_owners=[ - BeneficialOwner( - FullName("James", "Smith"), date.today() - timedelta(days=20*365), - Address("650 Allerton Street","Redwood City","CA","94063","US"), - Phone("1","2025550127"),"james@unit-finance.com",ssn="574567625"), - BeneficialOwner(FullName("Richard","Hendricks"), date.today() - timedelta(days=20 * 365), - Address("470 Allerton Street", "Redwood City", "CA", "94063", "US"), - Phone("1", "2025550158"), "richard@unit-finance.com", ssn="574572795") - ] - ) - - return client.applications.create(request) - def test_create_business_application(): - response = create_business_application() + response = create_business_application(client) assert response.data.type == "businessApplication" + def test_list_and_get_applications(): response = client.applications.list() for app in response.data: @@ -67,13 +48,15 @@ def test_list_and_get_applications(): res = client.applications.get(app.id) assert res.data.type in ApplicationTypes + def test_update_individual_application(): app = create_individual_application() updated = client.applications.update(PatchApplicationRequest(app.data.id, tags={"patch": "test-patch"})) assert updated.data.type == "individualApplication" + def test_update_business_application(): - app = create_business_application() + app = create_business_application(client) updated = client.applications.update(PatchApplicationRequest(app.data.id, "businessApplication", tags={"patch": "test-patch"})) assert updated.data.type == "businessApplication" diff --git a/e2e_tests/card_test.py b/e2e_tests/card_test.py index fcdac0ef..0677a0a2 100644 --- a/e2e_tests/card_test.py +++ b/e2e_tests/card_test.py @@ -1,17 +1,17 @@ import os import unittest + +import pytest import requests -from datetime import timedelta -from e2e_tests.account_test import create_deposit_account, create_deposit_account_for_business, \ - create_credit_account_for_business +from e2e_tests.account_test import create_credit_account_for_business from unit import Unit from unit.models.card import CreateIndividualDebitCard, PatchIndividualDebitCard, ListCardParams, \ CreateBusinessDebitCard, CreateBusinessVirtualDebitCard, CreateBusinessCreditCard, CreateBusinessVirtualCreditCard, \ PatchBusinessDebitCard, PatchBusinessCreditCard from unit.models.account import * -from unit.models.application import CreateIndividualApplicationRequest -from e2e_tests.helpers.helpers import create_relationship, generate_uuid, full_name, address, phone +from e2e_tests.helpers.helpers import create_relationship, generate_uuid, full_name, address, phone, \ + create_deposit_account, create_deposit_account_for_business token = os.environ.get('TOKEN') client = Unit("https://api.s.unit.sh", token) @@ -25,6 +25,30 @@ } +def create_individual_debit_card(): + account_id = create_deposit_account(client).data.id + request = CreateIndividualDebitCard(relationships={ + "account": { + "data": { + "type": "depositAccount", + "id": account_id + } + } + }) + response = client.cards.create(request) + res = requests.post(f"https://api.s.unit.sh/sandbox/cards/{response.data.id}/activate/", headers=headers) + + if res.status_code != 200: + print("Failed to activate card") + + return response + + +@pytest.fixture() +def individual_debit_card(): + return create_individual_debit_card().data + + def find_card_id(criteria: Dict[str, str]): def filter_func(card): for key, value in criteria.items(): @@ -45,71 +69,47 @@ def filter_func(card): return response.data.id -def create_individual_customer(): - request = CreateIndividualApplicationRequest( - FullName("Jhon", "Doe"), date.today() - timedelta(days=20 * 365), - Address("1600 Pennsylvania Avenue Northwest", "Washington", "CA", "20500", "US"), - "jone.doe1@unit-finance.com", - Phone("1", "2025550108"), ssn="721074426", - ) - response = client.applications.create(request) - for key, value in response.data.relationships.items(): - if key == "customer": - return value.id - - return "" - - -def create_business_debit_card(): - account_id = create_deposit_account_for_business().data.id +@pytest.fixture() +def business_debit_card(): + account_id = create_deposit_account_for_business(client).data.id request = CreateBusinessDebitCard(full_name, "2001-08-10", address, phone, "richard@piedpiper.com", shipping_address=address, idempotency_key=generate_uuid(), relationships=create_relationship("depositAccount", account_id, "account")) - response = client.cards.create(request) - return response.data + return client.cards.create(request).data -def create_business_virtual_debit_card(): - account_id = create_deposit_account_for_business().data.id +@pytest.fixture() +def business_virtual_debit_card(): + account_id = create_deposit_account_for_business(client).data.id request = CreateBusinessVirtualDebitCard(full_name, "2001-08-10", address, phone, "richard@piedpiper.com", relationships=create_relationship("depositAccount", account_id, "account")) - response = client.cards.create(request) - return response.data + return client.cards.create(request).data -def create_individual_debit_card(): - account_id = create_deposit_account().data.id - request = CreateIndividualDebitCard(relationships={ - "account": { - "data": { - "type": "depositAccount", - "id": account_id - } - } - }) - response = client.cards.create(request) - res = requests.post(f"https://api.s.unit.sh/sandbox/cards/{response.data.id}/activate/", headers=headers) +@pytest.fixture +def business_credit_card(): + account_id = create_credit_account_for_business(client).data.id - if res.status_code != 200: - print("Failed to activate card") + request = CreateBusinessCreditCard(full_name, "2001-08-10", address, phone, + "richard@piedpiper.com", shipping_address=address, + idempotency_key=generate_uuid(), + relationships=create_relationship("creditAccount", account_id, "account")) - return response + return client.cards.create(request).data -def test_create_individual_debit_card(): - response = create_individual_debit_card() - print(response.data.attributes["status"]) - assert response.data.type == "individualDebitCard" +def test_create_individual_debit_card(individual_debit_card): + assert individual_debit_card.type == "individualDebitCard" -def test_get_debit_card(): - card_id = create_individual_debit_card().data.id +def test_get_individual_debit_card(individual_debit_card): + card_id = individual_debit_card.id response = client.cards.get(card_id) - assert response.data.type in card_types + assert response.data.type == "individualDebitCard" def test_list_cards(): @@ -120,8 +120,8 @@ def test_list_cards(): res = requests.post(f"https://api.s.unit.sh/sandbox/cards/{card.id}/activate/", headers=headers) -def test_get_debit_card_include_customer(): - card_id = create_individual_debit_card().data.id +def test_get_debit_card_include_customer(individual_debit_card): + card_id = individual_debit_card.id response = client.cards.get(card_id, "customer") assert response.data.type in card_types and response.included is not None @@ -174,8 +174,8 @@ def test_update_individual_card(): assert response.data.attributes.get("tags").get("test") == "updated" -def test_update_business_card(): - card_id = create_business_debit_card().id +def test_update_business_card(business_debit_card): + card_id = business_debit_card.id _address = Address("1818 Pennsylvania Avenue Northwest", "Washington", "CA", "21500", "US") request = PatchBusinessDebitCard(card_id, address=_address, tags={"test": "updated"}) response = client.cards.update(request) @@ -197,45 +197,30 @@ def test_card_limits(): assert response.data.type == "limits" -def test_create_business_debit_card(): - response = create_business_debit_card() - assert response.type == "businessDebitCard" - - -def test_create_business_virtual_debit_card(): - response = create_business_virtual_debit_card() - assert response.type == "businessVirtualDebitCard" +def test_create_business_debit_card(business_debit_card): + assert business_debit_card.type == "businessDebitCard" -def create_business_credit_card(): - account_id = create_credit_account_for_business().data.id +def test_create_business_virtual_debit_card(business_virtual_debit_card): + assert business_virtual_debit_card.type == "businessVirtualDebitCard" - request = CreateBusinessCreditCard(full_name, "2001-08-10", address, phone, - "richard@piedpiper.com", shipping_address=address, - idempotency_key=generate_uuid(), - relationships=create_relationship("creditAccount", account_id, "account")) - return client.cards.create(request) - - -def test_create_business_credit_card(): - response = create_business_credit_card() - assert response.data.type == "businessCreditCard" +def test_create_business_credit_card(business_credit_card): + assert business_credit_card.type == "businessCreditCard" def test_create_business_virtual_credit_card(): - account_id = create_credit_account_for_business().data.id + account_id = create_credit_account_for_business(client).data.id request = CreateBusinessVirtualCreditCard(full_name, "2001-08-10", address, phone, "richard@piedpiper.com", - relationships=create_relationship("creditAccount", account_id, "account") - ) + relationships=create_relationship("creditAccount", account_id, "account")) response = client.cards.create(request) assert response.data.type == "businessVirtualCreditCard" -def test_update_business_credit_card(): - card_id = create_business_credit_card().data.id +def test_update_business_credit_card(business_credit_card): + card_id = business_credit_card.id _address = Address("1818 Pennsylvania Avenue Northwest", "Washington", "CA", "21500", "US") request = PatchBusinessCreditCard(card_id, address=_address, tags={"test": "updated"}) response = client.cards.update(request) diff --git a/e2e_tests/check_deposit_test.py b/e2e_tests/check_deposit_test.py index ddb778c2..d3cb04d4 100644 --- a/e2e_tests/check_deposit_test.py +++ b/e2e_tests/check_deposit_test.py @@ -2,21 +2,28 @@ import pytest from unit import Unit from unit.models.check_deposit import CreateCheckDepositRequest -from e2e_tests.account_test import create_deposit_account -from e2e_tests.helpers.helpers import create_relationship +from e2e_tests.helpers.helpers import create_relationship, create_deposit_account token = os.environ.get('TOKEN') client = Unit("https://api.s.unit.sh", token) -def test_create_check_deposit(): - account = create_deposit_account() - res = client.checkDeposits.create(CreateCheckDepositRequest(20000, - create_relationship("depositAccount", account.data.id, - "account"), - "Check deposit")) - assert res.data.type == "checkDeposit" -def test_list_and_get_check_deposits(): +@pytest.fixture() +def check_deposit(): + account = create_deposit_account(client) + return client.checkDeposits.create( + CreateCheckDepositRequest(20000, create_relationship("depositAccount", account.data.id, "account"), + "Check deposit") + ).data + + +def test_create_check_deposit(check_deposit): + assert check_deposit.type == "checkDeposit" + + +def test_list_and_get_check_deposits(check_deposit): + assert check_deposit.type == "checkDeposit" + response = client.checkDeposits.list() for c in response.data: assert c.type == "checkDeposit" diff --git a/e2e_tests/counterparty_test.py b/e2e_tests/counterparty_test.py index ce7318de..ad5591cd 100644 --- a/e2e_tests/counterparty_test.py +++ b/e2e_tests/counterparty_test.py @@ -1,40 +1,55 @@ import os import unittest +import pytest + +from e2e_tests.helpers.helpers import create_individual_customer from unit import Unit from unit.models.counterparty import * -from e2e_tests.account_test import create_individual_customer token = os.environ.get('TOKEN') client = Unit("https://api.s.unit.sh", token) -def create_counterparty(): - customer_id = create_individual_customer() + +@pytest.fixture +def counterparty(): + customer_id = create_individual_customer(client) request = CreateCounterpartyRequest("Joe Doe", "123456789", "123", "Checking", "Person", {"customer": Relationship("customer", customer_id)}) - return client.counterparty.create(request) + return client.counterparty.create(request).data -def test_create_counterparty(): - response = create_counterparty() - assert response.data.type == "achCounterparty" -def test_delete_counterparty(): - counterparty_id = create_counterparty().data.id - response = client.counterparty.delete(counterparty_id) +def delete_counterparty(counterparty_id): + return client.counterparty.delete(counterparty_id) + + +def test_create_counterparty(counterparty): + assert counterparty.type == "achCounterparty" + + +def test_delete_counterparty(counterparty): + response = delete_counterparty(counterparty.id) assert response.data == [] -def test_get_counterparty(): - counterparty_id = create_counterparty().data.id + +def test_get_counterparty(counterparty): + counterparty_id = counterparty.id response = client.counterparty.get(counterparty_id) assert response.data.type == "achCounterparty" + response = delete_counterparty(counterparty_id) + assert response.data == [] + -def test_counterparty_list(): +def test_counterparty_list(counterparty): response = client.counterparty.list() for c in response.data: assert c.type == "achCounterparty" + delete_response = delete_counterparty(counterparty.id) + assert delete_response.data == [] + -def test_create_couterparty(): +def test_create_counterparty_json(): create_counterparty_json = { "data": { "type": "achCounterparty", @@ -70,7 +85,7 @@ def test_create_couterparty(): assert payload["data"]["type"] == create_counterparty_json["data"]["type"] -def test_create_with_token_couterparty(): +def test_create_counterparty_json_with_token(): create_counterparty_json = { "data": { "type": "achCounterparty", @@ -107,6 +122,7 @@ def test_create_with_token_couterparty(): assert payload["data"]["attributes"]["permissions"] == create_counterparty_json["data"]["attributes"]["permissions"] assert payload["data"]["attributes"]["tags"] == create_counterparty_json["data"]["attributes"]["tags"] + def test_counterparty_dto(): counterparty_api_response = { "type": "achCounterparty", diff --git a/e2e_tests/customerToken_test.py b/e2e_tests/customerToken_test.py index c7e82a91..0bdae0a4 100644 --- a/e2e_tests/customerToken_test.py +++ b/e2e_tests/customerToken_test.py @@ -1,30 +1,35 @@ import os import unittest +import pytest + +from e2e_tests.helpers.helpers import create_individual_customer from unit import Unit -from e2e_tests.account_test import create_individual_customer from unit.models.customerToken import CreateCustomerToken, CreateCustomerTokenVerification token = os.environ.get('TOKEN') client = Unit("https://api.s.unit.sh", token) -def test_create_token(): - account_id = create_individual_customer() - request = CreateCustomerToken(account_id, "customers accounts") + +@pytest.fixture +def customer_id(): + return create_individual_customer(client) + + +def test_create_token(customer_id): + request = CreateCustomerToken(customer_id, "customers accounts") response = client.customerTokens.create_token(request) assert response.data.type == "customerBearerToken" -def test_create_jwt_token(): - account_id = create_individual_customer() - request = CreateCustomerToken(account_id, "customers accounts", +def test_create_jwt_token(customer_id): + request = CreateCustomerToken(customer_id, "customers accounts", jwt_token="eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlZPV3VLR1R4eFlwRUNWSlAwdDRTNiJ9.eyJpc3MiOiJodHRwczovL2Rldi04NTRvbjk3dC51cy5hdXRoMC5jb20vIiwic3ViIjoiYkpjUkQ1SHo2eWRDWXlaTDVnTjE4MXo0OGxKamFOQURAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vZGV2LTg1NG9uOTd0LnVzLmF1dGgwLmNvbS9hcGkvdjIvIiwiaWF0IjoxNjQyOTMwNDA5LCJleHAiOjE2NDMwMTY4MDksImF6cCI6ImJKY1JENUh6NnlkQ1l5Wkw1Z04xODF6NDhsSmphTkFEIiwiZ3R5IjoiY2xpZW50LWNyZWRlbnRpYWxzIn0.DCF_ODpiBPhn-fcr-FF3-Ayf6fq9g8UjSqTssfvdALNxZDQMGXaQeCIisy2NLrhF81PHAX3dZzcZbpeO93OlOAomXlaVzNomEd-pCLvjv1dQoQRc2BB3IMWUxbPBdGV7kztJzIfUwrOBTNV-DqSdB4SoGYROveFa3An4Mlj2FjArTXDXhnUytq15X5p_k4zLLNzznHVd-Tdcnd7hz9sA1vvtWXu") response = client.customerTokens.create_token(request) assert response.data.type == "customerBearerToken" -def test_create_token_verification(): - account_id = create_individual_customer() - request = CreateCustomerTokenVerification(account_id, "sms") +def test_create_token_verification(customer_id): + request = CreateCustomerTokenVerification(customer_id, "sms") response = client.customerTokens.create_token_verification(request) assert response.data.type == "customerTokenVerification" diff --git a/e2e_tests/fee_test.py b/e2e_tests/fee_test.py index e7885408..bf13f56f 100644 --- a/e2e_tests/fee_test.py +++ b/e2e_tests/fee_test.py @@ -1,14 +1,16 @@ import os import unittest + +from e2e_tests.helpers.helpers import create_deposit_account from unit import Unit from unit.models.fee import * -from e2e_tests.account_test import create_deposit_account token = os.environ.get('TOKEN') client = Unit("https://api.s.unit.sh", token) -def test_create_individual_application(): - deposit_account_id = create_deposit_account().data.id + +def test_create_fee(): + deposit_account_id = create_deposit_account(client).data.id request = CreateFeeRequest(150, "test fee", {"account": Relationship("depositAccount", deposit_account_id)}) response = client.fees.create(request) diff --git a/e2e_tests/helpers/helpers.py b/e2e_tests/helpers/helpers.py index 852bbdea..f0d92f12 100644 --- a/e2e_tests/helpers/helpers.py +++ b/e2e_tests/helpers/helpers.py @@ -1,9 +1,9 @@ import uuid from datetime import date, timedelta -from unit.models import Relationship, Address, Phone, FullName -from unit.models.account import CreateDepositAccountRequest -from unit.models.application import CreateIndividualApplicationRequest +from unit.models import Relationship, Address, Phone, FullName, BusinessContact, Officer, BeneficialOwner +from unit.models.account import CreateDepositAccountRequest, CloseCreditAccountRequest, CreateCreditAccountRequest +from unit.models.application import CreateIndividualApplicationRequest, CreateBusinessApplicationRequest def create_relationship(_type: str, _id: str, relation: str = None): @@ -35,6 +35,30 @@ def generate_uuid(): }) +def create_business_application(client): + request = CreateBusinessApplicationRequest( + name="Acme Inc.", + address=Address("1600 Pennsylvania Avenue Northwest", "Washington", "CA", "20500", "US"), + phone=Phone("1", "9294723497"), state_of_incorporation="CA", entity_type="Corporation", ein="123456789", + officer=Officer(full_name=FullName("Jone", "Doe"), date_of_birth=date.today() - timedelta(days=20 * 365), + address=Address("950 Allerton Street", "Redwood City", "CA", "94063", "US"), + phone=Phone("1", "2025550108"), email="jone.doe@unit-finance.com", ssn="123456789"), + contact=BusinessContact(full_name=FullName("Jone", "Doe"), email="jone.doe@unit-finance.com", + phone=Phone("1", "2025550108")), + beneficial_owners=[ + BeneficialOwner( + FullName("James", "Smith"), date.today() - timedelta(days=20*365), + Address("650 Allerton Street","Redwood City","CA","94063","US"), + Phone("1","2025550127"),"james@unit-finance.com",ssn="574567625"), + BeneficialOwner(FullName("Richard","Hendricks"), date.today() - timedelta(days=20 * 365), + Address("470 Allerton Street", "Redwood City", "CA", "94063", "US"), + Phone("1", "2025550158"), "richard@unit-finance.com", ssn="574572795") + ] + ) + + return client.applications.create(request) + + def create_individual_customer(client): request = CreateIndividualApplicationRequest( FullName("Jhon", "Doe"), date.today() - timedelta(days=20 * 365), @@ -56,3 +80,51 @@ def create_deposit_account(client): {"customer": Relationship("customer", customer_id)}, {"purpose": "credit_operating"}) return client.accounts.create(request) + + +def create_deposit_account_for_business(client): + customer_id = create_business_customer(client) + + request = CreateDepositAccountRequest("checking", + {"customer": Relationship("customer", customer_id)}, + {"purpose": "checking"}) + return client.accounts.create(request) + + +def create_individual_customer(client): + request = CreateIndividualApplicationRequest( + FullName("Jhon", "Doe"), date.today() - timedelta(days=20 * 365), + Address("1600 Pennsylvania Avenue Northwest", "Washington", "CA", "20500", "US"), + "jone.doe1@unit-finance.com", + Phone("1", "2025550108"), ssn="721074426", + ) + response = client.applications.create(request) + for key, value in response.data.relationships.items(): + if key == "customer": + return value.id + + return "" + + +def create_business_customer(client): + b_app = create_business_application(client).data + return b_app.relationships.get("customer").id + + +def create_deposit_account(client): + customer_id = create_individual_customer(client) + request = CreateDepositAccountRequest("checking", + {"customer": Relationship("customer", customer_id)}, + {"purpose": "checking"}) + return client.accounts.create(request) + + +def create_credit_account_for_business(client): + customer_id = create_business_customer(client) + request = CreateCreditAccountRequest("credit_terms_test", 20000, create_relationship("customer", customer_id), + {"purpose": "some_purpose"}) + return client.accounts.create(request) + + +def close_credit_account(client, account_id): + return client.accounts.close_account(CloseCreditAccountRequest(account_id)) diff --git a/e2e_tests/payment_test.py b/e2e_tests/payment_test.py index 483d5f10..61eafd50 100644 --- a/e2e_tests/payment_test.py +++ b/e2e_tests/payment_test.py @@ -1,23 +1,28 @@ import os import unittest +import pytest + +from e2e_tests.helpers.helpers import create_deposit_account from unit import Unit from unit.models.payment import * -from e2e_tests.account_test import create_deposit_account token = os.environ.get('TOKEN') client = Unit("https://api.s.unit.sh", token) -def create_book_payment(): - account_id1 = create_deposit_account().data.id - account_id2 = create_deposit_account().data.id + +@pytest.fixture +def book_payment(): + account_id1 = create_deposit_account(client).data.id + account_id2 = create_deposit_account(client).data.id request = CreateBookPaymentRequest(200, "Book payment", {"account": Relationship("depositAccount", account_id1), "counterpartyAccount": Relationship("depositAccount", account_id2)}, tags={"purpose": "checking"}, ) - return client.payments.create(request) + return client.payments.create(request).data + def test_list_and_get_payments(): payments_ids = [] @@ -32,9 +37,10 @@ def test_list_and_get_payments(): response = client.payments.get(id) assert "Payment" in response.data.type -def test_create_book_payment(): - response = create_book_payment() - assert response.data.type == "bookPayment" + +def test_create_book_payment(book_payment): + assert book_payment.type == "bookPayment" + def test_list_and_get_payments_filter_by_type(): payments_ids = [] @@ -64,12 +70,8 @@ def test_list_and_get_payments_filter_by_status(): assert response.data.attributes["status"] == "Pending" or response.data.attributes["status"] == "Sent" -def test_create_book_payment(): - response = create_book_payment() - assert response.data.type == "bookPayment" - -def test_update_book_payment(): - payment_id = create_book_payment().data.id +def test_update_book_payment(book_payment): + payment_id = book_payment.id tags = {"purpose": "test"} request = PatchBookPaymentRequest(payment_id, tags) response = client.payments.update(request) diff --git a/e2e_tests/reward_test.py b/e2e_tests/reward_test.py index 03436439..e59849b8 100644 --- a/e2e_tests/reward_test.py +++ b/e2e_tests/reward_test.py @@ -1,6 +1,6 @@ import os -from e2e_tests.account_test import create_deposit_account +from e2e_tests.helpers.helpers import create_deposit_account from unit import Unit from unit.models.reward import CreateRewardRequest @@ -9,7 +9,7 @@ def test_create_reward(): - account_id = create_deposit_account().data.id + account_id = create_deposit_account(client).data.id req = CreateRewardRequest(3000, "Reward for transaction #5678", account_id, tags={"test": "rewards"}) res = client.rewards.create(req) assert res.data.type == "reward" diff --git a/unit/models/__init__.py b/unit/models/__init__.py index e540cd7b..82117dc0 100644 --- a/unit/models/__init__.py +++ b/unit/models/__init__.py @@ -66,6 +66,30 @@ class UnitRequest(object): def to_json_api(self) -> Dict: pass + def vars_to_attributes_dict(self, ignore: List[str] = []) -> Dict: + attributes = {} + + for k in self.__dict__: + if k != "relationships" and k not in ignore: + v = getattr(self, k) + if v: + attributes[to_camel_case(k)] = v + + return attributes + + def to_payload(self, _type: str, relationships: Dict[str, Relationship] = None, ignore: List[str] = []) -> Dict: + payload = { + "data": { + "type": _type, + "attributes": self.vars_to_attributes_dict(ignore), + } + } + + if relationships: + payload["data"]["relationships"] = relationships + + return payload + class UnitParams(object): def to_dict(self) -> Dict: diff --git a/unit/models/account.py b/unit/models/account.py index ba160249..3c624b3c 100644 --- a/unit/models/account.py +++ b/unit/models/account.py @@ -292,7 +292,7 @@ def from_json_api(attributes): CheckDepositAccountLimits.from_json_api(attributes["checkDeposit"])) -class CloseAccountRequest(UnitRequest): +class CloseDepositAccountRequest(UnitRequest): def __init__(self, account_id: str, reason: Optional[Literal["ByCustomer", "Fraud"]] = "ByCustomer"): self.account_id = account_id self.reason = reason @@ -313,10 +313,25 @@ def __repr__(self): json.dumps(self.to_json_api()) +class CloseCreditAccountRequest(UnitRequest): + def __init__(self, account_id: str, reason: Optional[Literal["ByCustomer", "Fraud"]] = "ByCustomer"): + self.account_id = account_id + self.reason = reason + + def to_json_api(self) -> Dict: + return super().to_payload("creditAccountClose", ignore=["account_id"]) + + def __repr__(self): + json.dumps(self.to_json_api()) + + +CloseAccountRequest = Union[CloseDepositAccountRequest, CloseCreditAccountRequest] + + class ListAccountParams(UnitParams): def __init__(self, offset: int = 0, limit: int = 100, customer_id: Optional[str] = None, tags: Optional[object] = None, include: Optional[str] = None, status: Optional[AccountStatus] = None, - from_balance: Optional[int] = None, to_balance: Optional[int] = None): + from_balance: Optional[int] = None, to_balance: Optional[int] = None, _type: Optional[str] = None): self.offset = offset self.limit = limit self.customer_id = customer_id @@ -325,6 +340,7 @@ def __init__(self, offset: int = 0, limit: int = 100, customer_id: Optional[str] self.status = status self.from_balance = from_balance self.to_balance = to_balance + self._type = _type def to_dict(self) -> Dict: parameters = {"page[limit]": self.limit, "page[offset]": self.offset} @@ -340,6 +356,8 @@ def to_dict(self) -> Dict: parameters["filter[fromBalance]"] = self.from_balance if self.to_balance: parameters["filter[toBalance]"] = self.to_balance + if self._type: + parameters["filter[type]"] = self._type return parameters