<s> import datetime <EOL> from decimal import Decimal <EOL> from django . db import models <EOL> import mock <EOL> from django . core import mail <EOL> from corehq . apps . domain . models import Domain <EOL> from dimagi . utils . dates import add_months_to_date <EOL> from corehq . apps . accounting import generator , tasks <EOL> from corehq . apps . accounting . generator import FakeStripeCard , FakeStripeCustomer <EOL> from corehq . apps . accounting . models import ( <EOL> BillingAccount , <EOL> Currency , <EOL> Subscription , <EOL> SubscriptionType , <EOL> BillingRecord , <EOL> Invoice , <EOL> SMALL_INVOICE_THRESHOLD , <EOL> StripePaymentMethod , <EOL> ) <EOL> from corehq . apps . smsbillables . models import ( <EOL> SmsGatewayFee , SmsGatewayFeeCriteria , SmsUsageFee , SmsUsageFeeCriteria , <EOL> SmsBillable , <EOL> ) <EOL> from corehq . apps . accounting . tests . base_tests import BaseAccountingTest <EOL> from corehq . util . dates import get_previous_month_date_range <EOL> class TestBillingAccount ( BaseAccountingTest ) : <EOL> def setUp ( self ) : <EOL> super ( TestBillingAccount , self ) . setUp ( ) <EOL> self . billing_contact = generator . arbitrary_web_user ( ) <EOL> self . dimagi_user = generator . arbitrary_web_user ( is_dimagi = True ) <EOL> self . currency = generator . init_default_currency ( ) <EOL> self . billing_account = generator . billing_account ( self . dimagi_user , self . billing_contact ) <EOL> def test_creation ( self ) : <EOL> self . assertIsNotNone ( self . billing_account ) <EOL> def test_deletions ( self ) : <EOL> self . assertRaises ( models . ProtectedError , self . currency . delete ) <EOL> def test_autopay_user ( self ) : <EOL> self . assertFalse ( self . billing_account . auto_pay_enabled ) <EOL> mail . outbox = [ ] <EOL> autopay_user = generator . arbitrary_web_user ( ) <EOL> self . billing_account . update_autopay_user ( autopay_user . username , None ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( self . billing_account . auto_pay_enabled ) <EOL> self . assertEqual ( self . billing_account . auto_pay_user , autopay_user . username ) <EOL> mail . outbox = [ ] <EOL> other_autopay_user = generator . arbitrary_web_user ( ) <EOL> self . billing_account . update_autopay_user ( other_autopay_user . username , None ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( self . billing_account . auto_pay_user , other_autopay_user . username ) <EOL> def tearDown ( self ) : <EOL> self . billing_contact . delete ( ) <EOL> self . dimagi_user . delete ( ) <EOL> SmsBillable . objects . all ( ) . delete ( ) <EOL> SmsGatewayFee . objects . all ( ) . delete ( ) <EOL> SmsGatewayFeeCriteria . objects . all ( ) . delete ( ) <EOL> SmsUsageFee . objects . all ( ) . delete ( ) <EOL> SmsUsageFeeCriteria . objects . all ( ) . delete ( ) <EOL> BillingAccount . objects . all ( ) . delete ( ) <EOL> Currency . objects . all ( ) . delete ( ) <EOL> super ( TestBillingAccount , self ) . tearDown ( ) <EOL> class TestSubscription ( BaseAccountingTest ) : <EOL> def setUp ( self ) : <EOL> super ( TestSubscription , self ) . setUp ( ) <EOL> self . billing_contact = generator . arbitrary_web_user ( ) <EOL> self . dimagi_user = generator . arbitrary_web_user ( is_dimagi = True ) <EOL> self . domain = Domain ( name = '<STR_LIT:test>' ) <EOL> self . domain . save ( ) <EOL> self . currency = generator . init_default_currency ( ) <EOL> self . account = generator . billing_account ( self . dimagi_user , self . billing_contact ) <EOL> self . subscription_length = <NUM_LIT:15> <EOL> subscription_start_date = datetime . date ( <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT> ) <EOL> subscription_end_date = add_months_to_date ( subscription_start_date , self . subscription_length ) <EOL> self . subscription = generator . generate_domain_subscription ( <EOL> self . account , <EOL> self . domain , <EOL> date_start = subscription_start_date , <EOL> date_end = subscription_end_date , <EOL> ) <EOL> def test_creation ( self ) : <EOL> self . assertIsNotNone ( self . subscription ) <EOL> def test_no_activation ( self ) : <EOL> tasks . activate_subscriptions ( based_on_date = self . subscription . date_start - datetime . timedelta ( <NUM_LIT:30> ) ) <EOL> subscription = Subscription . objects . get ( id = self . subscription . id ) <EOL> self . assertFalse ( subscription . is_active ) <EOL> def test_activation ( self ) : <EOL> tasks . activate_subscriptions ( based_on_date = self . subscription . date_start ) <EOL> subscription = Subscription . objects . get ( id = self . subscription . id ) <EOL> self . assertTrue ( subscription . is_active ) <EOL> def test_no_deactivation ( self ) : <EOL> tasks . activate_subscriptions ( based_on_date = self . subscription . date_start ) <EOL> tasks . deactivate_subscriptions ( based_on_date = self . subscription . date_end - datetime . timedelta ( <NUM_LIT:30> ) ) <EOL> subscription = Subscription . objects . get ( id = self . subscription . id ) <EOL> self . assertTrue ( subscription . is_active ) <EOL> def test_deactivation ( self ) : <EOL> tasks . deactivate_subscriptions ( based_on_date = self . subscription . date_end ) <EOL> subscription = Subscription . objects . get ( id = self . subscription . id ) <EOL> self . assertFalse ( subscription . is_active ) <EOL> def test_deletions ( self ) : <EOL> self . assertRaises ( models . ProtectedError , self . account . delete ) <EOL> self . assertRaises ( models . ProtectedError , self . subscription . plan_version . delete ) <EOL> self . assertRaises ( models . ProtectedError , self . subscription . subscriber . delete ) <EOL> def test_is_hidden_to_ops ( self ) : <EOL> self . subscription . is_hidden_to_ops = True <EOL> self . subscription . save ( ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( Subscription . objects . filter ( id = self . subscription . id ) ) ) <EOL> self . subscription . is_hidden_to_ops = False <EOL> self . subscription . save ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( Subscription . objects . filter ( id = self . subscription . id ) ) ) <EOL> def test_next_subscription ( self ) : <EOL> this_subscription_date_end = self . subscription . date_end <EOL> already_canceled_future_subscription = generator . generate_domain_subscription ( <EOL> self . account , <EOL> self . domain , <EOL> date_start = this_subscription_date_end , <EOL> date_end = this_subscription_date_end <EOL> ) <EOL> next_future_subscription = generator . generate_domain_subscription ( <EOL> self . account , <EOL> self . domain , <EOL> date_start = this_subscription_date_end , <EOL> date_end = add_months_to_date ( this_subscription_date_end , <NUM_LIT:1> ) , <EOL> ) <EOL> self . assertEqual ( self . subscription . next_subscription , next_future_subscription ) <EOL> def tearDown ( self ) : <EOL> self . billing_contact . delete ( ) <EOL> self . dimagi_user . delete ( ) <EOL> self . domain . delete ( ) <EOL> generator . delete_all_subscriptions ( ) <EOL> generator . delete_all_accounts ( ) <EOL> super ( TestSubscription , self ) . tearDown ( ) <EOL> class TestBillingRecord ( BaseAccountingTest ) : <EOL> def setUp ( self ) : <EOL> super ( TestBillingRecord , self ) . setUp ( ) <EOL> self . billing_contact = generator . arbitrary_web_user ( ) <EOL> self . dimagi_user = generator . arbitrary_web_user ( is_dimagi = True ) <EOL> self . domain = Domain ( name = '<STR_LIT:test>' ) <EOL> self . domain . save ( ) <EOL> self . invoice_start , self . invoice_end = get_previous_month_date_range ( ) <EOL> self . currency = generator . init_default_currency ( ) <EOL> self . account = generator . billing_account ( self . dimagi_user , self . billing_contact ) <EOL> self . subscription_length = <NUM_LIT:4> <EOL> subscription_start_date = datetime . date ( <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT> ) <EOL> subscription_end_date = add_months_to_date ( subscription_start_date , self . subscription_length ) <EOL> self . subscription = generator . generate_domain_subscription ( <EOL> self . account , <EOL> self . domain , <EOL> date_start = subscription_start_date , <EOL> date_end = subscription_end_date , <EOL> ) <EOL> self . invoice = Invoice ( <EOL> subscription = self . subscription , <EOL> date_start = self . invoice_start , <EOL> date_end = self . invoice_end , <EOL> is_hidden = False , <EOL> ) <EOL> self . billing_record = BillingRecord ( invoice = self . invoice ) <EOL> def test_should_send_email ( self ) : <EOL> self . assertTrue ( self . billing_record . should_send_email ) <EOL> def test_should_send_email_contracted ( self ) : <EOL> self . subscription . service_type = SubscriptionType . IMPLEMENTATION <EOL> self . assertFalse ( self . billing_record . should_send_email ) <EOL> self . invoice . balance = Decimal ( SMALL_INVOICE_THRESHOLD - <NUM_LIT:1> ) <EOL> self . assertFalse ( self . billing_record . should_send_email ) <EOL> self . invoice . balance = Decimal ( SMALL_INVOICE_THRESHOLD + <NUM_LIT:1> ) <EOL> self . assertTrue ( self . billing_record . should_send_email ) <EOL> def test_should_send_email_autogenerate_credits ( self ) : <EOL> self . subscription . auto_generate_credits = True <EOL> self . assertFalse ( self . billing_record . should_send_email ) <EOL> self . invoice . balance = Decimal ( SMALL_INVOICE_THRESHOLD + <NUM_LIT:1> ) <EOL> self . assertTrue ( self . billing_record . should_send_email ) <EOL> def test_should_send_email_hidden ( self ) : <EOL> self . assertTrue ( self . billing_record . should_send_email ) <EOL> self . invoice . is_hidden = True <EOL> self . assertFalse ( self . billing_record . should_send_email ) <EOL> @ mock . patch . object ( StripePaymentMethod , '<STR_LIT>' ) <EOL> class TestStripePaymentMethod ( BaseAccountingTest ) : <EOL> def setUp ( self ) : <EOL> super ( TestStripePaymentMethod , self ) . setUp ( ) <EOL> self . web_user = generator . arbitrary_web_user ( ) <EOL> self . dimagi_user = generator . arbitrary_web_user ( is_dimagi = True ) <EOL> self . fake_card = FakeStripeCard ( ) <EOL> self . fake_stripe_customer = FakeStripeCustomer ( cards = [ self . fake_card ] ) <EOL> self . currency = generator . init_default_currency ( ) <EOL> self . billing_account = generator . billing_account ( self . dimagi_user , self . web_user ) <EOL> self . billing_account_2 = generator . billing_account ( self . dimagi_user , self . web_user ) <EOL> self . payment_method = StripePaymentMethod ( web_user = self . web_user . username , <EOL> customer_id = self . fake_stripe_customer . id ) <EOL> self . payment_method . save ( ) <EOL> def test_set_autopay ( self , fake_customer ) : <EOL> fake_customer . __get__ = mock . Mock ( return_value = self . fake_stripe_customer ) <EOL> self . assertEqual ( self . billing_account . auto_pay_user , None ) <EOL> self . assertFalse ( self . billing_account . auto_pay_enabled ) <EOL> self . payment_method . set_autopay ( self . fake_card , self . billing_account , None ) <EOL> self . assertEqual ( self . fake_card . metadata , { "<STR_LIT>" . format ( self . billing_account . id ) : '<STR_LIT:True>' } ) <EOL> self . assertEqual ( self . billing_account . auto_pay_user , self . web_user . username ) <EOL> self . assertTrue ( self . billing_account . auto_pay_enabled ) <EOL> self . payment_method . set_autopay ( self . fake_card , self . billing_account_2 , None ) <EOL> self . assertEqual ( self . fake_card . metadata , { "<STR_LIT>" . format ( self . billing_account . id ) : '<STR_LIT:True>' , <EOL> "<STR_LIT>" . format ( self . billing_account_2 . id ) : '<STR_LIT:True>' } ) <EOL> other_web_user = generator . arbitrary_web_user ( ) <EOL> other_payment_method = StripePaymentMethod ( web_user = other_web_user . username ) <EOL> different_fake_card = FakeStripeCard ( ) <EOL> other_payment_method . set_autopay ( different_fake_card , self . billing_account , None ) <EOL> self . assertEqual ( self . billing_account . auto_pay_user , other_web_user . username ) <EOL> self . assertTrue ( different_fake_card . metadata [ "<STR_LIT>" . format ( self . billing_account . id ) ] ) <EOL> self . assertFalse ( self . fake_card . metadata [ "<STR_LIT>" . format ( self . billing_account . id ) ] == '<STR_LIT:True>' ) <EOL> def test_unset_autopay ( self , fake_customer ) : <EOL> fake_customer . __get__ = mock . Mock ( return_value = self . fake_stripe_customer ) <EOL> self . payment_method . set_autopay ( self . fake_card , self . billing_account , None ) <EOL> self . assertEqual ( self . fake_card . metadata , { "<STR_LIT>" . format ( self . billing_account . id ) : '<STR_LIT:True>' } ) <EOL> self . payment_method . unset_autopay ( self . fake_card , self . billing_account ) <EOL> self . assertEqual ( self . fake_card . metadata , { "<STR_LIT>" . format ( self . billing_account . id ) : '<STR_LIT:False>' } ) <EOL> self . assertIsNone ( self . billing_account . auto_pay_user ) <EOL> self . assertFalse ( self . billing_account . auto_pay_enabled ) </s>
