Add basic paypal unit tests.
[wolnelektury.git] / src / paypal / tests.py
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.
4 #
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
9
10
11 BillingAgreementMock = Mock(
12     execute=Mock(
13         return_value=Mock(
14             plan=Mock(
15                 payment_definitions=[
16                     Mock(
17                         amount={'value': '100'}
18                     )
19                 ]
20             )
21         )
22     )
23 )
24
25
26 class PaypalTests(WLTestCase):
27     @classmethod
28     def setUpClass(cls):
29         cls.user = User(username='test')
30         cls.user.set_password('test')
31         cls.user.save()
32
33     @classmethod
34     def tearDownClass(cls):
35         cls.user.delete()
36
37     def test_paypal_form(self):
38         response = self.client.get('/paypal/form/')
39         self.assertEqual(response.status_code, 200)
40
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)
45
46     def test_paypal_form_invalid(self):
47         """Paypal form: error on bad input."""
48         self.client.login(username='test', password='test')
49
50         response = self.client.post('/paypal/form/', {"amount": "0"})
51         self.assertEqual(response.status_code, 200)
52         self.assertEqual(
53             len(response.context['form'].errors['amount']),
54             1)
55
56     @patch.multiple('paypalrestsdk',
57         BillingPlan=DEFAULT,
58         BillingAgreement=DEFAULT
59     )
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?
65         # Models created?
66
67     @patch.multiple('paypalrestsdk',
68         BillingPlan=DEFAULT,
69         BillingAgreement=DEFAULT,
70     )
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)
75
76     @patch.multiple('paypalrestsdk',
77         BillingAgreement=BillingAgreementMock
78     )
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')
83
84     def test_paypal_cancel(self):
85         response = self.client.get('/paypal/cancel/')
86         self.assertEqual(response.status_code, 200)