def test_registering_user_when_stripe_is_down(self): self.request.session = {} self.request.method = 'POST' self.request.POST = { 'email': '*****@*****.**', 'name': 'pyrocks', 'stripe_token': '....', 'last_4_digits': '4242', 'password': '******', 'ver_password': '******', } with mock.patch('stripe.Customer.create', side_effect=socket.error( "Can't connect to Stripe")) as stripe_mock: register(self.request) users = User.objects.filter(email="*****@*****.**") self.assertEquals(len(users), 1) self.assertEquals(users[0].stripe_id, '') unpaid = UnpaidUsers.objects.filter(email="*****@*****.**") self.assertEquals(len(unpaid), 1) self.assertIsNotNone(unpaid[0].last_notification)
def test_registering_user_when_stripe_is_down(self): # Create the request used to test the view self.request.session = {} self.request.method = 'POST' self.request.POST = { 'email': '*****@*****.**', 'name': 'pyRock', 'stripe_token': '...', 'last_4_digits': '4242', 'password': '******', 'ver_password': '******', } # Mock out Stripe and ask it to throw a connection error with mock.patch('stripe.Customer.create', side_effect=socket.error( "Can't connect to Stripe")) as stripe_mock: # run the test register(self.request) # assert there is a record in the database without Stripe id users = User.objects.filter(email="*****@*****.**") self.assertEquals(len(users), 1) self.assertEquals(users[0].stripe_id, '') # Checks for unpaid users unpaid = UnpaidUsers.objects.filter(email="*****@*****.**") self.assertEquals(len(unpaid), 1) self.assertIsNotNone(unpaid[0].last_notification)
def test_registering_user_twice_cause_error_msg(self, save_mock): #create the request used to test the view self.request.session = {} self.request.method = 'POST' self.request.POST = {} #create the expected html html = render_to_response( 'register.html', { 'form': self.get_MockUserForm(), 'months': list(range(1, 12)), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': None, 'years': list(range(2011, 2036)), }) #mock out stripe so we don't hit their server with mock.patch('payments.views.Customer') as stripe_mock: config = {'create.return_value': mock.Mock()} stripe_mock.configure_mock(**config) #run the test resp = register(self.request) #verify that we did things correctly self.assertEqual(resp.content, html.content) self.assertEqual(resp.status_code, 200) self.assertEqual(self.request.session, {}) #assert there is no records in the database. users = User.objects.filter(email="*****@*****.**") self.assertEqual(len(users), 0)
def test_registering_new_user_returns_succesfully(self, create_mock, stripe_mock): self.request.session = {} self.request.method = 'POST' self.request.POST = { 'email': '*****@*****.**', 'name': 'pyRock', 'stripe_token': '...', 'last_4_digits': '4242', 'password': '******', 'ver_password': '******', } #get the return values of the mocks, for our checks later new_user = create_mock.return_value new_cust = stripe_mock.return_value resp = register(self.request) self.assertEquals(resp.content, "") self.assertEquals(resp.status_code, 302) self.assertEquals(self.request.session['user'], new_user.pk) #verify the user was actually stored in the database. create_mock.assert_called_with('pyRock', '*****@*****.**', 'bad_password', '4242', new_cust.id)
def test_registering_user_when_stripe_is_down_all_or_nothing( self, save_mock): # Create the request used to test the view self.request.session = {} self.request.method = 'POST' self.request.POST = { 'email': '*****@*****.**', 'name': 'pyRock', 'stripe_token': '...', 'last_4_digits': '4242', 'password': '******', 'ver_password': '******', } # Mock out stripe and ask it to throw a connection error with mock.patch('stripe.Customer.create', side_effect=socket.error( "Can't connect to Stripe.")) as stripe_mock: # Run the test resp = register(self.request) # Assert there is no new record in the database users = User.objects.filter(email="*****@*****.**") self.assertEquals(len(users), 0) # Check to make sure the associated table has no updated data unpaid = UnpaidUsers.objects.filter(email="*****@*****.**") self.assertEquals(len(unpaid), 0)
def test_registering_user_when_strip_is_down_all_or_nothing( self, save_mock): self.request.session = {} self.request.method = 'POST' self.request.POST = { 'email': '*****@*****.**', 'name': 'python', 'stripe_token': '...', 'last_4_digits': '4242', 'password': '******', 'ver_password': '******', } with mock.patch('stripe.Customer.create', side_effect=socket.error( "can't connect to stripe")) as stripe_mock: resp = register(self.request) users = User.objects.filter(email="*****@*****.**") self.assertEquals(len(users), 0) unpaid = UnpaidUsers.objects.filter(email="*****@*****.**") self.assertEquals(len(unpaid), 0)
def test_registering_user_twice_cause_error_msg(self, save_mock): #create the request used to test the view self.request.session = {} self.request.method = 'POST' self.request.POST = {} #create the expected html html = render_to_response( 'register.html', { 'form': self.get_MockUserForm(), 'months': list(range(1, 12)), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': None, 'years': list(range(2011, 2036)), } ) #mock out stripe so we don't hit their server with mock.patch('payments.views.Customer') as stripe_mock: config = {'create.return_value': mock.Mock()} stripe_mock.configure_mock(**config) #run the test resp = register(self.request) #verify that we did things correctly self.assertEqual(resp.content, html.content) self.assertEqual(resp.status_code, 200) self.assertEqual(self.request.session, {}) #assert there is no records in the database. users = User.objects.filter(email="*****@*****.**") self.assertEqual(len(users), 0) # class CustomerTests(TestCase): # def test_create_subscription(self): # with mock.patch('stripe.Customer.create') as create_mock: # cust_data = {'description': 'test user', 'email': '*****@*****.**', # 'card': '4242', 'plan': 'gold'} # Customer.create("subscription", **cust_data) # create_mock.assert_called_with(**cust_data) # def test_create_one_time_bill(self): # with mock.patch('stripe.Charge.create') as charge_mock: # cust_data = {'description': 'email', # 'card': '1234', # 'amount': '5000', # 'currency': 'usd'} # Customer.create("one_time", **cust_data) # charge_mock.assert_called_with(**cust_data)
def test_registering_user_when_stripe_is_down(self): #mock out Stripe and ask it to throw a connection error with mock.patch('stripe.Customer.create', side_effect=socket.error( "Can't connect to Stripe")) as stripe_mock: #run the test register(self.post_request) #assert there is a record in the database without Stripe id. users = User.objects.filter(email="*****@*****.**") self.assertEquals(len(users), 1) self.assertEquals(users[0].stripe_id, '') # check the associated table got updated. unpaid = UnpaidUsers.objects.filter(email="*****@*****.**") self.assertEquals(len(unpaid), 1) self.assertIsNotNone(unpaid[0].last_notification)
def test_invalid_form_returns_registration_page(self): with mock.patch('payments.forms.UserForm.is_valid') as user_mock: user_mock.return_value = False self.request.method = 'POST' self.request.POST = None resp = register(self.request) self.assertEqual(resp.content, self.expected_html) # make sure that we did indeed call our is_valid function self.assertEqual(user_mock.call_count, 1)
def test_registering_new_user_returns_succesfully(self, stripe_mock): resp = register(self.post_request) self.assertContains(resp, b'"status": "ok"') users = User.objects.filter(email="*****@*****.**") self.assertEqual(len(users), 1) self.assertEqual(users[0].stripe_id, '1234') #clean up users[0].delete()
def test_invalid_form_returns_registration_page(self): with mock.patch('payments.forms.UserForm.is_valid') as user_mock: user_mock.return_value = False self.post_request._data = b'{}' resp = register(self.post_request) # should return a list of errors self.assertContains(resp, '"status": "form-invalid"') # make sure that we did indeed call our is_valid function self.assertEquals(user_mock.call_count, 1)
def test_registering_user_twice_cause_error_msg(self, save_mock): # mock out stripe so we don't hit their server with mock.patch('payments.views.Customer') as stripe_mock: config = {'create.return_value': mock.Mock()} stripe_mock.configure_mock(**config) # run the test resp = register(self.post_request) # verify that we did things correctly self.assertContains(resp, '[email protected] is already a member') # assert there is no records in the database. users = User.objects.filter(email="*****@*****.**") self.assertEqual(len(users), 0)
def test_registering_user_when_strip_is_down_all_or_nothing( self, save_mock): #mock out stripe and ask it to throw a connection error with mock.patch('stripe.Customer.create', side_effect=socket.error( "can't connect to stripe")) as stripe_mock: #run the test resp = register(self.post_request) #assert there is no new record in the database users = User.objects.filter(email="*****@*****.**") self.assertEquals(len(users), 0) #check the associated table has no updated data unpaid = UnpaidUsers.objects.filter(email="*****@*****.**") self.assertEquals(len(unpaid), 0)
def test_registering_new_user_returns_successfully(self, stripe_mock): self.request.session = {} self.request.method = 'POST' self.request.POST = { 'email': '*****@*****.**', 'name': 'pyRock', 'stripe_token': '...', 'last_4_digits': '4242', 'password': '******', 'ver_password': '******', } resp = register(self.request) self.assertEqual(resp.content, b"") self.assertEqual(resp.status_code, 302) users = User.objects.filter(email="*****@*****.**") self.assertEqual(len(users), 1) self.assertEqual(users[0].stripe_id, '1234')