1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.contrib.auth.models import User
6 from mock import Mock, patch, DEFAULT
7 from catalogue.test_utils import WLTestCase
8 from .models import BillingPlan
11 BillingAgreementMock = Mock(
17 amount={'value': '100'}
26 class PaypalTests(WLTestCase):
29 cls.user = User(username='test')
30 cls.user.set_password('test')
34 def tearDownClass(cls):
37 def test_paypal_form(self):
38 response = self.client.get('/paypal/form/')
39 self.assertEqual(response.status_code, 200)
41 def test_paypal_form_unauthorized(self):
42 """Legacy flow: only allow payment for logged-in users."""
43 response = self.client.post('/paypal/form/', {"amount": "0"})
44 self.assertEqual(response.status_code, 403)
46 def test_paypal_form_invalid(self):
47 """Paypal form: error on bad input."""
48 self.client.login(username='test', password='test')
50 response = self.client.post('/paypal/form/', {"amount": "0"})
51 self.assertEqual(response.status_code, 200)
53 len(response.context['form'].errors['amount']),
56 @patch.multiple('paypalrestsdk',
58 BillingAgreement=DEFAULT
60 def test_paypal_form_valid(self, BillingPlan, BillingAgreement):
61 self.client.login(username='test', password='test')
62 response = self.client.post('/paypal/form/', {"amount": "100"})
63 self.assertEqual(response.status_code, 302)
64 # Assert: BillingPlan created? BillingAgreement created?
67 @patch.multiple('paypalrestsdk',
69 BillingAgreement=DEFAULT,
71 def test_paypal_form_valid(self, BillingPlan, BillingAgreement):
72 self.client.login(username='test', password='test')
73 response = self.client.post('/paypal/app-form/', {"amount": "100"})
74 self.assertEqual(response.status_code, 302)
76 @patch.multiple('paypalrestsdk',
77 BillingAgreement=BillingAgreementMock
79 def test_paypal_return(self):
80 self.client.login(username='test', password='test')
81 BillingPlan.objects.create(amount=100)
82 response = self.client.get('/paypal/return/?token=secret-token')
84 def test_paypal_cancel(self):
85 response = self.client.get('/paypal/cancel/')
86 self.assertEqual(response.status_code, 200)