"""Test suite for terms_and_conditions logic."""
from datetime import datetime
import pytest
from collaborator.logic import terms_and_conditions
from collaborator.models.ows import ows_abacus_account, ows_abacus_state, ows_royalties
from collaborator.models.rds import (
terms_and_conditions_agreement_persister,
terms_and_conditions_persister,
)
from collaborator.utils.error import OwsError
MOCK_ACCOUNT_PAYEE_ID = "account_payee-123"
def test_get_terms_and_conditions(mocker, mock_account, mock_user):
"""Test getting a vendor agreement."""
mock_ts_and_cs = {"id": 1, "template": "
1
"}
mock_agreement = {
"terms_and_conditions_id": 1,
"agreed_date": datetime(2012, 3, 4, 5, 6, 7).isoformat(),
}
mock_get_latest_ts_and_cs = mocker.patch.object(
terms_and_conditions_persister.TermsAndConditionsPersister,
"get_latest",
)
mock_get_latest_ts_and_cs_agreement = mocker.patch.object(
terms_and_conditions_agreement_persister.TermsAndConditionsAgreementPersister,
"get_latest_for_account",
)
mock_get_latest_ts_and_cs.return_value = mock_ts_and_cs
mock_get_latest_ts_and_cs_agreement.return_value = mock_agreement
result = terms_and_conditions.get_terms_and_conditions(mock_account)
mock_get_latest_ts_and_cs.assert_called_with()
mock_get_latest_ts_and_cs_agreement.assert_called_with(mock_account)
assert result == {
"latest_version": mock_ts_and_cs["id"],
"latest_template": mock_ts_and_cs["template"],
"agreed_version": mock_agreement["terms_and_conditions_id"],
"agreed_date": mock_agreement["agreed_date"],
}
def test_agree_terms_and_conditions(mocker, mock_account, mock_user):
"""Test agreeing terms and conditions."""
mock_get_current_abacus_statement_period = mocker.patch.object(
ows_royalties,
"get_current_abacus_statement_period",
)
mock_get_current_abacus_statement_period.return_value = {"statement_period_id": 1}
mock_ts_and_cs = {"id": 1, "template": "1
"}
mock_account_payee_response = {
"items": [{"data": {"account_payee_id": MOCK_ACCOUNT_PAYEE_ID}}]
}
mock_abacus_states_response = [
{"action_name": "payment_eligibility", "action_status": "approved"},
{"action_name": "tax_eligibility", "action_status": "approved"},
{"action_name": "banking_details_review", "action_status": "complete"},
]
mock_get_latest_ts_and_cs = mocker.patch.object(
terms_and_conditions_persister.TermsAndConditionsPersister,
"get_latest",
)
mock_agree_version = mocker.patch.object(
terms_and_conditions_agreement_persister.TermsAndConditionsAgreementPersister,
"agree_version",
)
mock_get_account_payee = mocker.patch.object(
ows_abacus_account, "get_account_payee"
)
mock_get_abacus_states = mocker.patch.object(ows_abacus_state, "get_abacus_states")
mock_get_latest_ts_and_cs.return_value = mock_ts_and_cs
mock_get_account_payee.return_value = mock_account_payee_response
mock_get_abacus_states.return_value = mock_abacus_states_response
terms_and_conditions.agree_terms_and_conditions({}, mock_account.id, 1)
mock_get_latest_ts_and_cs.assert_called_with()
mock_get_account_payee.assert_called_with(mock_account.id)
mock_get_abacus_states.assert_called_with(MOCK_ACCOUNT_PAYEE_ID)
mock_agree_version.assert_called_with(
{}, mock_account.id, 1, abacus_statement_period={"statement_period_id": 1}
)
def test_agree_terms_and_conditions_failure_because_of_wrong_version(
mocker, mock_account, mock_user
):
"""Test agreeing terms and conditions."""
mock_get_current_abacus_statement_period = mocker.patch.object(
ows_royalties,
"get_current_abacus_statement_period",
)
mock_get_current_abacus_statement_period.return_value = {"statement_period_id": 1}
mock_ts_and_cs = {"id": 2, "template": "2
"}
mock_get_latest_ts_and_cs = mocker.patch.object(
terms_and_conditions_persister.TermsAndConditionsPersister,
"get_latest",
)
mock_agree_version = mocker.patch.object(
terms_and_conditions_agreement_persister.TermsAndConditionsAgreementPersister,
"agree_version",
)
mock_get_account_payee = mocker.patch.object(
ows_abacus_account, "get_account_payee"
)
mock_get_abacus_states = mocker.patch.object(ows_abacus_state, "get_abacus_states")
mock_get_latest_ts_and_cs.return_value = mock_ts_and_cs
with pytest.raises(OwsError) as exc_info:
terms_and_conditions.agree_terms_and_conditions({}, mock_account.id, 1)
mock_get_latest_ts_and_cs.assert_called_with()
mock_get_account_payee.assert_not_called()
mock_get_abacus_states.assert_not_called()
mock_agree_version.assert_not_called()
assert exc_info.value.status == 404
assert exc_info.value.code == "not_found"
assert exc_info.value.message == "Trying to sign outdated terms and conditions."
def test_agree_terms_and_conditions_failure_because_of_payment_eligibility(
mocker, mock_account, mock_user
):
"""Test agreeing terms and conditions."""
mock_get_current_abacus_statement_period = mocker.patch.object(
ows_royalties,
"get_current_abacus_statement_period",
)
mock_get_current_abacus_statement_period.return_value = {"statement_period_id": 1}
mock_ts_and_cs = {"id": 1, "template": "1
"}
mock_account_payee_response = {
"items": [{"data": {"account_payee_id": MOCK_ACCOUNT_PAYEE_ID}}]
}
mock_abacus_states_response = [
{"action_name": "payment_eligibility", "action_status": "approved"},
{"action_name": "tax_eligibility", "action_status": "approved"},
{"action_name": "banking_details_review", "action_status": "rejected"},
]
mock_get_latest_ts_and_cs = mocker.patch.object(
terms_and_conditions_persister.TermsAndConditionsPersister,
"get_latest",
)
mock_agree_version = mocker.patch.object(
terms_and_conditions_agreement_persister.TermsAndConditionsAgreementPersister,
"agree_version",
)
mock_get_account_payee = mocker.patch.object(
ows_abacus_account, "get_account_payee"
)
mock_get_abacus_states = mocker.patch.object(ows_abacus_state, "get_abacus_states")
mock_get_latest_ts_and_cs.return_value = mock_ts_and_cs
mock_get_account_payee.return_value = mock_account_payee_response
mock_get_abacus_states.return_value = mock_abacus_states_response
with pytest.raises(OwsError) as exc_info:
terms_and_conditions.agree_terms_and_conditions({}, mock_account.id, 1)
mock_get_latest_ts_and_cs.assert_called_with()
mock_get_account_payee.assert_called_with(mock_account.id)
mock_get_abacus_states.assert_called_with(MOCK_ACCOUNT_PAYEE_ID)
mock_agree_version.assert_not_called()
assert exc_info.value.status == 404
assert exc_info.value.code == "not_found"
assert exc_info.value.message == "Client is not payment eligible."