<s> import datetime <EOL> from corehq . apps . domain . models import Domain <EOL> from corehq . apps . accounting import generator <EOL> from corehq . apps . accounting . exceptions import NewSubscriptionError <EOL> from corehq . apps . accounting . models import ( <EOL> Subscription , BillingAccount , DefaultProductPlan , SoftwarePlanEdition , <EOL> SubscriptionAdjustmentMethod , SubscriptionType , EntryPoint , ) <EOL> from corehq . apps . accounting . tests . base_tests import BaseAccountingTest <EOL> class TestNewDomainSubscription ( BaseAccountingTest ) : <EOL> def setUp ( self ) : <EOL> super ( TestNewDomainSubscription , self ) . setUp ( ) <EOL> self . domain = Domain ( <EOL> name = "<STR_LIT>" , <EOL> is_active = True , <EOL> ) <EOL> self . domain . save ( ) <EOL> self . domain2 = Domain ( <EOL> name = "<STR_LIT>" , <EOL> is_active = True , <EOL> ) <EOL> self . domain2 . save ( ) <EOL> self . admin_user = generator . arbitrary_web_user ( ) <EOL> self . admin_user . add_domain_membership ( self . domain . name , is_admin = True ) <EOL> self . admin_user . save ( ) <EOL> self . account = BillingAccount . get_or_create_account_by_domain ( <EOL> self . domain . name , created_by = self . admin_user . username ) [ <NUM_LIT:0> ] <EOL> self . account2 = BillingAccount . get_or_create_account_by_domain ( <EOL> self . domain2 . name , created_by = self . admin_user . username ) [ <NUM_LIT:0> ] <EOL> self . standard_plan = DefaultProductPlan . get_default_plan_by_domain ( <EOL> self . domain . name , edition = SoftwarePlanEdition . STANDARD ) <EOL> self . advanced_plan = DefaultProductPlan . get_default_plan_by_domain ( <EOL> self . domain . name , edition = SoftwarePlanEdition . ADVANCED ) <EOL> def test_new_susbscription_in_future ( self ) : <EOL> """<STR_LIT>""" <EOL> today = datetime . date . today ( ) <EOL> in_30_days = today + datetime . timedelta ( days = <NUM_LIT:30> ) <EOL> week_after_30 = in_30_days + datetime . timedelta ( days = <NUM_LIT:7> ) <EOL> next_year = week_after_30 + datetime . timedelta ( days = <NUM_LIT> ) <EOL> trial_subscription = Subscription . new_domain_subscription ( <EOL> self . account , self . domain . name , self . advanced_plan , <EOL> date_end = in_30_days , <EOL> adjustment_method = SubscriptionAdjustmentMethod . TRIAL , <EOL> is_trial = True , <EOL> ) <EOL> trial_subscription . is_active = True <EOL> trial_subscription . save ( ) <EOL> subscription = Subscription . new_domain_subscription ( <EOL> self . account2 , self . domain . name , self . standard_plan , <EOL> web_user = self . admin_user . username , <EOL> date_start = week_after_30 , date_end = next_year , <EOL> ) <EOL> final_sub = Subscription . objects . get ( pk = subscription . id ) <EOL> self . assertEqual ( final_sub . date_start , week_after_30 ) <EOL> self . assertEqual ( final_sub . date_end , next_year ) <EOL> def test_conflicting_dates ( self ) : <EOL> """<STR_LIT>""" <EOL> today = datetime . date . today ( ) <EOL> one_week = today + datetime . timedelta ( days = <NUM_LIT:7> ) <EOL> one_month = today + datetime . timedelta ( days = <NUM_LIT:30> ) <EOL> Subscription . new_domain_subscription ( <EOL> self . account , self . domain . name , self . advanced_plan , <EOL> date_start = one_week , <EOL> date_end = one_month , <EOL> ) <EOL> self . assertRaises ( NewSubscriptionError , lambda : Subscription . new_domain_subscription ( <EOL> self . account , self . domain . name , self . standard_plan , <EOL> ) ) <EOL> self . assertRaises ( NewSubscriptionError , lambda : Subscription . new_domain_subscription ( <EOL> self . account , self . domain . name , self . standard_plan , <EOL> date_end = one_week + datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> ) ) <EOL> self . assertRaises ( NewSubscriptionError , lambda : Subscription . new_domain_subscription ( <EOL> self . account , self . domain . name , self . standard_plan , <EOL> date_start = one_month - datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> ) ) <EOL> sub_before = Subscription . new_domain_subscription ( <EOL> self . account , self . domain . name , self . standard_plan , <EOL> date_end = one_week , <EOL> ) <EOL> sub_after = Subscription . new_domain_subscription ( <EOL> self . account , self . domain . name , self . standard_plan , <EOL> date_start = one_month , <EOL> ) <EOL> def test_update_billing_account_entry_point_self_serve ( self ) : <EOL> self_serve_subscription = Subscription . new_domain_subscription ( <EOL> self . account , self . domain . name , self . advanced_plan , <EOL> web_user = self . admin_user . username , service_type = SubscriptionType . PRODUCT <EOL> ) <EOL> self . assertEqual ( self_serve_subscription . account . entry_point , EntryPoint . SELF_STARTED ) <EOL> def test_update_billing_account_entry_point_contracted ( self ) : <EOL> contracted_subscription = Subscription . new_domain_subscription ( <EOL> self . account , self . domain . name , self . advanced_plan , <EOL> web_user = self . admin_user . username , service_type = SubscriptionType . IMPLEMENTATION <EOL> ) <EOL> self . assertNotEqual ( contracted_subscription . account . entry_point , EntryPoint . SELF_STARTED ) <EOL> def test_dont_update_billing_account_if_set ( self ) : <EOL> self . account . entry_point = EntryPoint . CONTRACTED <EOL> self . account . save ( ) <EOL> subscription = Subscription . new_domain_subscription ( <EOL> self . account , self . domain . name , self . advanced_plan , <EOL> web_user = self . admin_user . username , service_type = SubscriptionType . IMPLEMENTATION <EOL> ) <EOL> self . assertEqual ( subscription . account . entry_point , EntryPoint . CONTRACTED ) </s>
