test_forms.py 6.23 KB
Newer Older
1
2
from django.test import TestCase
from django.utils import timezone
3
from freezegun import freeze_time
4
5
6

from members.models import Member, Membership
from registrations import forms
7
from registrations.models import Entry, Renewal, Reference
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23


class MemberRegistrationFormTest(TestCase):

    def setUp(self):
        self.data = {
            'first_name': 'John',
            'last_name': 'Doe',
            'email': 'johndoe@example.com',
            'programme': 'computingscience',
            'student_number': 's1234567',
            'starting_year': 2014,
            'address_street': 'Heyendaalseweg 135',
            'address_street2': '',
            'address_postal_code': '6525AJ',
            'address_city': 'Nijmegen',
24
            'address_country': 'NL',
25
26
27
28
29
30
31
32
            'phone_number': '06123456789',
            'birthday': timezone.now().replace(year=1990, day=1),
            'language': 'en',
            'length': Entry.MEMBERSHIP_YEAR,
            'membership_type': Membership.MEMBER,
            'privacy_policy': 1,
        }

33
    def test_privacy_policy_checked(self):
34
35
36
37
38
39
40
        with self.subTest("Form is valid"):
            form = forms.MemberRegistrationForm(self.data)
            self.assertTrue(form.is_valid(), msg=dict(form.errors))
        with self.subTest("Form is not valid"):
            self.data['privacy_policy'] = 0
            form = forms.MemberRegistrationForm(self.data)
            self.assertFalse(form.is_valid(), msg=dict(form.errors))
41
42
43
44
45
46

    def test_has_privacy_policy_field(self):
        form = forms.MemberRegistrationForm(self.data)
        self.assertTrue(form.fields['privacy_policy'] is not None)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class BenefactorRegistrationFormTest(TestCase):

    def setUp(self):
        self.data = {
            'first_name': 'John',
            'last_name': 'Doe',
            'email': 'johndoe@example.com',
            'programme': 'computingscience',
            'student_number': 's1234567',
            'starting_year': 2014,
            'address_street': 'Heyendaalseweg 135',
            'address_street2': '',
            'address_postal_code': '6525AJ',
            'address_city': 'Nijmegen',
            'address_country': 'NL',
            'phone_number': '06123456789',
            'birthday': timezone.now().replace(year=1990, day=1),
            'language': 'en',
            'length': Entry.MEMBERSHIP_YEAR,
            'membership_type': Membership.BENEFACTOR,
            'privacy_policy': 1,
            'icis_employee': 1,
        }

    def test_privacy_policy_checked(self):
        with self.subTest("Form is valid"):
            form = forms.BenefactorRegistrationForm(self.data)
            self.assertTrue(form.is_valid(), msg=dict(form.errors))
        with self.subTest("Form is not valid"):
            self.data['privacy_policy'] = 0
            form = forms.BenefactorRegistrationForm(self.data)
            self.assertFalse(form.is_valid(), msg=dict(form.errors))

    def test_has_privacy_policy_field(self):
        form = forms.BenefactorRegistrationForm(self.data)
        self.assertTrue(form.fields['privacy_policy'] is not None)


class RenewalFormTest(TestCase):
86
87
88
89
    fixtures = ['members.json']

    def setUp(self):
        self.member = Member.objects.filter(last_name="Wiggers").first()
90
        self.member.membership_set.all().delete()
91
92
93
94
        self.data = {
            'member': self.member.pk,
            'length': Entry.MEMBERSHIP_STUDY,
            'membership_type': Membership.MEMBER,
95
            'privacy_policy': 1,
96
97
98
        }

    def test_is_valid(self):
99
        with self.subTest("Form is valid"):
100
            form = forms.RenewalForm(self.data)
101
102
103
            self.assertTrue(form.is_valid(), msg=dict(form.errors))
        with self.subTest("Form is not valid"):
            self.data['privacy_policy'] = 0
104
            form = forms.RenewalForm(self.data)
105
106
107
            self.assertFalse(form.is_valid(), msg=dict(form.errors))

    def test_has_privacy_policy_field(self):
108
        form = forms.RenewalForm(self.data)
109
        self.assertTrue(form.fields['privacy_policy'] is not None)
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172


class ReferenceFormTest(TestCase):
    fixtures = ['members.json']

    def setUp(self):
        self.member = Member.objects.filter(last_name="Wiggers").first()
        self.member.membership_set.all().delete()
        self.entry = Renewal.objects.create(member=self.member)
        self.member.membership_set.all().delete()
        self.data = {
            'member': self.member.pk,
            'entry': self.entry.pk
        }

    @freeze_time('2018-08-01')
    def test_clean(self):
        with self.subTest("Form is valid"):
            form = forms.ReferenceForm(self.data)
            self.assertTrue(form.is_valid())
            form.clean()
        with self.subTest("Form throws error about benefactor type"):
            m = Membership.objects.create(
                type=Membership.BENEFACTOR,
                user=self.member,
                since='2017-09-01',
                until='2018-08-31'
            )
            form = forms.ReferenceForm(self.data)
            self.assertFalse(form.is_valid())
            self.assertEqual(form.errors, {
                '__all__': ['Benefactors cannot give references.']
            })
            m.delete()
        with self.subTest("Form throws error about membership end"):
            m = Membership.objects.create(
                type=Membership.MEMBER,
                user=self.member,
                since='2017-09-01',
                until='2018-08-31'
            )
            form = forms.ReferenceForm(self.data)
            self.assertFalse(form.is_valid())
            self.assertEqual(form.errors, {
                '__all__': [
                    "It's not possible to give references for "
                    "memberships that start after your own "
                    "membership's end."
                ]
            })
            m.delete()
        with self.subTest('Form throws error about uniqueness'):
            Reference.objects.create(
                member=self.member,
                entry=self.entry
            )
            form = forms.ReferenceForm(self.data)
            self.assertFalse(form.is_valid())
            self.assertEqual(form.errors, {
                '__all__': [
                    "You've already given a reference for this person."
                ]
            })