<s> """<STR_LIT>""" <EOL> from decimal import Decimal <EOL> from django . contrib . auth import get_user_model <EOL> from django . core . urlresolvers import reverse <EOL> from django . test . client import RequestFactory <EOL> from django . test . testcases import TestCase <EOL> import stripe <EOL> from mock import patch , PropertyMock <EOL> from djstripe import settings as djstripe_settings <EOL> from djstripe . models import Customer , CurrentSubscription <EOL> from djstripe . views import ChangeCardView , HistoryView <EOL> class AccountViewTest ( TestCase ) : <EOL> fake_stripe_customer_id = "<STR_LIT>" <EOL> def setUp ( self ) : <EOL> self . url = reverse ( "<STR_LIT>" ) <EOL> self . user = get_user_model ( ) . objects . create_user ( username = "<STR_LIT>" , <EOL> email = "<STR_LIT>" , <EOL> password = "<STR_LIT>" ) <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = fake_stripe_customer_id ) ) <EOL> def test_autocreate_customer ( self , stripe_create_customer_mock ) : <EOL> self . assertEqual ( Customer . objects . count ( ) , <NUM_LIT:0> ) <EOL> response = self . client . get ( self . url ) <EOL> stripe_create_customer_mock . assert_called_once_with ( email = self . user . email ) <EOL> self . assertEqual ( self . fake_stripe_customer_id , response . context [ "<STR_LIT>" ] . stripe_id ) <EOL> self . assertEqual ( self . user , response . context [ "<STR_LIT>" ] . subscriber ) <EOL> self . assertEqual ( Customer . objects . count ( ) , <NUM_LIT:1> ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = fake_stripe_customer_id ) ) <EOL> def test_plan_list_context ( self , stripe_create_customer_mock ) : <EOL> response = self . client . get ( self . url ) <EOL> self . assertEqual ( djstripe_settings . PLAN_LIST , response . context [ "<STR_LIT>" ] ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = fake_stripe_customer_id ) ) <EOL> def test_subscription_context ( self , stripe_create_customer_mock ) : <EOL> response = self . client . get ( self . url ) <EOL> self . assertEqual ( None , response . context [ "<STR_LIT>" ] ) <EOL> @ patch ( "<STR_LIT>" , new_callable = PropertyMock , return_value = CurrentSubscription ( plan = "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = fake_stripe_customer_id ) ) <EOL> def test_subscription_context_with_plan ( self , djstripe_customer_customer_subscription_mock , stripe_create_customer_mock ) : <EOL> response = self . client . get ( self . url ) <EOL> self . assertEqual ( "<STR_LIT>" , response . context [ "<STR_LIT>" ] . plan ) <EOL> class ChangeCardViewTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . url = reverse ( "<STR_LIT>" ) <EOL> self . user = get_user_model ( ) . objects . create_user ( username = "<STR_LIT>" , <EOL> email = "<STR_LIT>" , <EOL> password = "<STR_LIT>" ) <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> def test_get ( self , stripe_create_customer_mock ) : <EOL> response = self . client . get ( self . url ) <EOL> self . assertEqual ( <NUM_LIT:200> , response . status_code ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> def test_post_new_card ( self , stripe_customer_mock , update_card_mock , send_invoice_mock , retry_unpaid_invoices_mock ) : <EOL> self . client . post ( self . url , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> update_card_mock . assert_called_once_with ( self . user . customer , "<STR_LIT>" ) <EOL> send_invoice_mock . assert_called_with ( self . user . customer ) <EOL> retry_unpaid_invoices_mock . assert_called_once_with ( self . user . customer ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> def test_post_change_card ( self , stripe_customer_mock , update_card_mock , send_invoice_mock , retry_unpaid_invoices_mock ) : <EOL> Customer . objects . get_or_create ( subscriber = self . user , card_fingerprint = "<STR_LIT>" ) <EOL> self . assertEqual ( <NUM_LIT:1> , Customer . objects . count ( ) ) <EOL> self . client . post ( self . url , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assertEqual ( <NUM_LIT:1> , Customer . objects . count ( ) ) <EOL> update_card_mock . assert_called_once_with ( self . user . customer , "<STR_LIT>" ) <EOL> self . assertFalse ( send_invoice_mock . called ) <EOL> retry_unpaid_invoices_mock . assert_called_once_with ( self . user . customer ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> def test_post_card_error ( self , stripe_create_customer_mock , update_card_mock ) : <EOL> update_card_mock . side_effect = stripe . StripeError ( "<STR_LIT>" ) <EOL> response = self . client . post ( self . url , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> update_card_mock . assert_called_once_with ( self . user . customer , "<STR_LIT>" ) <EOL> self . assertIn ( "<STR_LIT>" , response . context ) <EOL> self . assertIn ( "<STR_LIT>" , response . context [ "<STR_LIT>" ] ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> def test_post_no_card ( self , stripe_create_customer_mock , update_card_mock ) : <EOL> update_card_mock . side_effect = stripe . StripeError ( "<STR_LIT>" ) <EOL> response = self . client . post ( self . url ) <EOL> update_card_mock . assert_called_once_with ( self . user . customer , None ) <EOL> self . assertIn ( "<STR_LIT>" , response . context ) <EOL> self . assertIn ( "<STR_LIT>" , response . context [ "<STR_LIT>" ] ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> def test_get_object ( self , stripe_create_customer_mock ) : <EOL> view_instance = ChangeCardView ( ) <EOL> request = RequestFactory ( ) <EOL> request . user = self . user <EOL> view_instance . request = request <EOL> object_a = view_instance . get_object ( ) <EOL> object_b = view_instance . get_object ( ) <EOL> customer_instance = Customer . objects . get ( subscriber = self . user ) <EOL> self . assertEqual ( customer_instance , object_a ) <EOL> self . assertEqual ( object_a , object_b ) <EOL> def test_get_success_url ( self ) : <EOL> view_instance = ChangeCardView ( ) <EOL> url = view_instance . get_post_success_url ( ) <EOL> self . assertEqual ( reverse ( "<STR_LIT>" ) , url ) <EOL> class HistoryViewTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . url = reverse ( "<STR_LIT>" ) <EOL> self . user = get_user_model ( ) . objects . create_user ( username = "<STR_LIT>" , <EOL> email = "<STR_LIT>" , <EOL> password = "<STR_LIT>" ) <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> def test_get_object ( self , stripe_create_customer_mock ) : <EOL> view_instance = HistoryView ( ) <EOL> request = RequestFactory ( ) <EOL> request . user = self . user <EOL> view_instance . request = request <EOL> object_a = view_instance . get_object ( ) <EOL> stripe_create_customer_mock . assert_called_once_with ( email = self . user . email ) <EOL> customer_instance = Customer . objects . get ( subscriber = self . user ) <EOL> self . assertEqual ( customer_instance , object_a ) <EOL> class SyncHistoryViewTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . url = reverse ( "<STR_LIT>" ) <EOL> self . user = get_user_model ( ) . objects . create_user ( username = "<STR_LIT>" , <EOL> email = "<STR_LIT>" , <EOL> password = "<STR_LIT>" ) <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , new_callable = PropertyMock , return_value = PropertyMock ( subscriber = "<STR_LIT>" ) ) <EOL> def test_post ( self , sync_subscriber_mock ) : <EOL> response = self . client . post ( self . url ) <EOL> sync_subscriber_mock . assert_called_once_with ( self . user ) <EOL> self . assertEqual ( "<STR_LIT>" , response . context [ "<STR_LIT>" ] . subscriber ) <EOL> class ConfirmFormViewTest ( TestCase ) : <EOL> fake_stripe_customer_id = "<STR_LIT>" <EOL> def setUp ( self ) : <EOL> self . plan = "<STR_LIT>" <EOL> self . url = reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . plan } ) <EOL> self . user = get_user_model ( ) . objects . create_user ( username = "<STR_LIT>" , <EOL> email = "<STR_LIT>" , <EOL> password = "<STR_LIT>" ) <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , new_callable = PropertyMock , return_value = CurrentSubscription ( plan = "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = fake_stripe_customer_id ) ) <EOL> def test_get_form_valid ( self , djstripe_customer_customer_subscription_mock , stripe_create_customer_mock ) : <EOL> response = self . client . get ( self . url ) <EOL> self . assertEqual ( <NUM_LIT:200> , response . status_code ) <EOL> @ patch ( "<STR_LIT>" , new_callable = PropertyMock , return_value = CurrentSubscription ( plan = "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = fake_stripe_customer_id ) ) <EOL> def test_get_form_unknown ( self , djstripe_customer_customer_subscription_mock , stripe_create_customer_mock ) : <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> self . assertRedirects ( response , reverse ( "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , new_callable = PropertyMock , return_value = CurrentSubscription ( plan = "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = fake_stripe_customer_id ) ) <EOL> def test_get_form_invalid ( self , djstripe_customer_customer_subscription_mock , stripe_create_customer_mock ) : <EOL> response = self . client . get ( self . url ) <EOL> self . assertRedirects ( response , reverse ( "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> def test_post_valid ( self , stripe_customer_mock , update_card_mock , subscribe_mock ) : <EOL> self . assertEqual ( <NUM_LIT:0> , Customer . objects . count ( ) ) <EOL> response = self . client . post ( self . url , { "<STR_LIT>" : self . plan , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assertEqual ( <NUM_LIT:1> , Customer . objects . count ( ) ) <EOL> update_card_mock . assert_called_once_with ( self . user . customer , "<STR_LIT>" ) <EOL> subscribe_mock . assert_called_once_with ( self . user . customer , self . plan ) <EOL> self . assertRedirects ( response , reverse ( "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> def test_post_no_card ( self , stripe_customer_mock , update_card_mock , subscribe_mock ) : <EOL> update_card_mock . side_effect = stripe . StripeError ( "<STR_LIT>" ) <EOL> response = self . client . post ( self . url , { "<STR_LIT>" : self . plan } ) <EOL> self . assertEqual ( <NUM_LIT:200> , response . status_code ) <EOL> self . assertIn ( "<STR_LIT>" , response . context ) <EOL> self . assertIn ( "<STR_LIT>" , response . context [ "<STR_LIT>" ] . errors [ "<STR_LIT>" ] ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> def test_post_form_invalid ( self , stripe_customer_mock ) : <EOL> response = self . client . post ( self . url ) <EOL> self . assertEqual ( <NUM_LIT:200> , response . status_code ) <EOL> self . assertIn ( "<STR_LIT>" , response . context [ "<STR_LIT>" ] . errors ) <EOL> self . assertIn ( "<STR_LIT>" , response . context [ "<STR_LIT>" ] . errors [ "<STR_LIT>" ] ) <EOL> class ChangePlanViewTest ( TestCase ) : <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> def setUp ( self , stripe_customer_mock ) : <EOL> self . url = reverse ( "<STR_LIT>" ) <EOL> self . user1 = get_user_model ( ) . objects . create_user ( username = "<STR_LIT>" , <EOL> email = "<STR_LIT>" , <EOL> password = "<STR_LIT>" ) <EOL> self . user2 = get_user_model ( ) . objects . create_user ( username = "<STR_LIT>" , <EOL> email = "<STR_LIT>" , <EOL> password = "<STR_LIT>" ) <EOL> Customer . get_or_create ( subscriber = self . user1 ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> def test_post_form_invalid ( self , stripe_customer_mock ) : <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> response = self . client . post ( self . url ) <EOL> self . assertEqual ( <NUM_LIT:200> , response . status_code ) <EOL> self . assertIn ( "<STR_LIT>" , response . context [ "<STR_LIT>" ] . errors ) <EOL> self . assertIn ( "<STR_LIT>" , response . context [ "<STR_LIT>" ] . errors [ "<STR_LIT>" ] ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> def test_post_new_sub_no_proration ( self , stripe_customer_mock ) : <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> response = self . client . post ( self . url , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assertEqual ( <NUM_LIT:200> , response . status_code ) <EOL> self . assertIn ( "<STR_LIT>" , response . context ) <EOL> self . assertIn ( "<STR_LIT>" , response . context [ "<STR_LIT>" ] . errors [ "<STR_LIT>" ] ) <EOL> @ patch ( "<STR_LIT>" , new_callable = PropertyMock , return_value = CurrentSubscription ( plan = "<STR_LIT:test>" , amount = Decimal ( <NUM_LIT> ) ) ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> def test_change_sub_no_proration ( self , subscribe_mock , current_subscription_mock ) : <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> response = self . client . post ( self . url , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assertRedirects ( response , reverse ( "<STR_LIT>" ) ) <EOL> subscribe_mock . assert_called_once_with ( self . user1 . customer , "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" , return_value = True ) <EOL> @ patch ( "<STR_LIT>" , new_callable = PropertyMock , return_value = CurrentSubscription ( plan = "<STR_LIT:test>" , amount = Decimal ( <NUM_LIT> ) ) ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> def test_change_sub_with_proration_downgrade ( self , subscribe_mock , current_subscription_mock , proration_policy_mock ) : <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> response = self . client . post ( self . url , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assertRedirects ( response , reverse ( "<STR_LIT>" ) ) <EOL> subscribe_mock . assert_called_once_with ( self . user1 . customer , "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" , return_value = True ) <EOL> @ patch ( "<STR_LIT>" , new_callable = PropertyMock , return_value = CurrentSubscription ( plan = "<STR_LIT:test>" , amount = Decimal ( <NUM_LIT> ) ) ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> def test_change_sub_with_proration_upgrade ( self , subscribe_mock , current_subscription_mock , proration_policy_mock ) : <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> response = self . client . post ( self . url , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assertRedirects ( response , reverse ( "<STR_LIT>" ) ) <EOL> subscribe_mock . assert_called_once_with ( self . user1 . customer , "<STR_LIT>" , prorate = True ) <EOL> @ patch ( "<STR_LIT>" , return_value = True ) <EOL> @ patch ( "<STR_LIT>" , new_callable = PropertyMock , return_value = CurrentSubscription ( plan = "<STR_LIT:test>" , amount = Decimal ( <NUM_LIT> ) ) ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> def test_change_sub_with_proration_same_plan ( self , subscribe_mock , current_subscription_mock , proration_policy_mock ) : <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> response = self . client . post ( self . url , { "<STR_LIT>" : "<STR_LIT:test>" } ) <EOL> self . assertRedirects ( response , reverse ( "<STR_LIT>" ) ) <EOL> subscribe_mock . assert_called_once_with ( self . user1 . customer , "<STR_LIT:test>" ) <EOL> @ patch ( "<STR_LIT>" , new_callable = PropertyMock , return_value = CurrentSubscription ( plan = "<STR_LIT:test>" , amount = Decimal ( <NUM_LIT> ) ) ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> def test_change_sub_same_plan ( self , subscribe_mock , current_subscription_mock ) : <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> response = self . client . post ( self . url , { "<STR_LIT>" : "<STR_LIT:test>" } ) <EOL> self . assertRedirects ( response , reverse ( "<STR_LIT>" ) ) <EOL> subscribe_mock . assert_called_once_with ( self . user1 . customer , "<STR_LIT:test>" ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> def test_change_sub_stripe_error ( self , subscribe_mock ) : <EOL> subscribe_mock . side_effect = stripe . StripeError ( "<STR_LIT>" ) <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> response = self . client . post ( self . url , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assertEqual ( <NUM_LIT:200> , response . status_code ) <EOL> self . assertIn ( "<STR_LIT>" , response . context ) <EOL> self . assertIn ( "<STR_LIT>" , response . context [ "<STR_LIT>" ] . errors [ "<STR_LIT>" ] ) <EOL> class CancelSubscriptionViewTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . url = reverse ( "<STR_LIT>" ) <EOL> self . user = get_user_model ( ) . objects . create_user ( username = "<STR_LIT>" , <EOL> email = "<STR_LIT>" , <EOL> password = "<STR_LIT>" ) <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , return_value = CurrentSubscription ( status = CurrentSubscription . STATUS_ACTIVE ) ) <EOL> def test_cancel_proration ( self , cancel_subscription_mock , stripe_create_customer_mock ) : <EOL> response = self . client . post ( self . url ) <EOL> cancel_subscription_mock . assert_called_once_with ( at_period_end = djstripe_settings . CANCELLATION_AT_PERIOD_END ) <EOL> self . assertRedirects ( response , reverse ( "<STR_LIT>" ) ) <EOL> self . assertTrue ( self . user . is_authenticated ( ) ) <EOL> @ patch ( "<STR_LIT>" , autospec = True ) <EOL> @ patch ( "<STR_LIT>" , return_value = PropertyMock ( id = "<STR_LIT>" ) ) <EOL> @ patch ( "<STR_LIT>" , return_value = CurrentSubscription ( status = CurrentSubscription . STATUS_CANCELLED ) ) <EOL> def test_cancel_no_proration ( self , cancel_subscription_mock , stripe_create_customer_mock , logout_mock ) : <EOL> response = self . client . post ( self . url ) <EOL> cancel_subscription_mock . assert_called_once_with ( at_period_end = djstripe_settings . CANCELLATION_AT_PERIOD_END ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertTrue ( logout_mock . called ) </s>
