tests.py 13 KB
Newer Older
okhin's avatar
okhin committed
1
2
from django.contrib.admin.sites import AdminSite
from django.contrib.auth.models import User
Aymeric Barantal's avatar
Aymeric Barantal committed
3
from django.test import TestCase
4
5
6
from rest_framework.test import APIClient

from picampaign.organization.models import Organization, GroupType, Group, FeedbackCategory
okhin's avatar
okhin committed
7
from picampaign.organization.admin import OrganizationAdmin, GroupTypeAdmin, GroupAdmin, CategoryAdmin
8
from picampaign.campaign.models import Campaign
Aymeric Barantal's avatar
Aymeric Barantal committed
9
10

# Create your tests here.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class OrganizationTest(TestCase):
    def test_str(self):
        organization = Organization.objects.create(
                name='Majestic 12',
                sip_key='majestic-12'
        )
        self.assertEqual(str(organization), organization.name)

class GroupTypeTest(TestCase):
    def test_str(self):
        organization = Organization.objects.create(
                name='Majestic 12',
                sip_key='majestic-12'
        )
        grouptype = GroupType.objects.create(
                name='comittee',
                organization=organization
        )
        self.assertEqual(str(grouptype), grouptype.name)

class GroupTest(TestCase):
    def test_str(self):
        organization = Organization.objects.create(
                name='Majestic 12',
                sip_key='majestic-12'
        )
        grouptype = GroupType.objects.create(
                name='comittee',
                organization=organization
        )
        group = Group.objects.create(
            name='group',
            type=grouptype
        )
        self.assertEqual(str(group), group.name)

class FeedbackCategoryTest(TestCase):
    def test_str(self):
        organization = Organization.objects.create(
                name='Majestic 12',
                sip_key='majestic-12'
        )
        feedbackcategory =FeedbackCategory.objects.create(
                name='Feedback',
                organization=organization
        )
        self.assertEqual(str(feedbackcategory), feedbackcategory.name)

okhin's avatar
okhin committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class OrganizationViewSet(TestCase):
    def setUp(self):
        self.organization = Organization.objects.create(
                name='Majestic 12',
                sip_key='majestic-12'
        )
        self.campaign = Campaign.objects.create(
                title='Test Campaign',
                start_date='2000-01-01',
                end_date='2100-12-31',
                organization=self.organization
        )

    def test_organization_viewset(self):
        client = APIClient()
        response = client.get('/campaigns/%(cid)s/organization/' % {'cid': self.campaign.id}, format='json')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'{"id":1,"name":"Majestic 12","description":"","website":"","logo":null}')

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class CategoryViewSet(TestCase):
    def setUp(self):
        self.organization = Organization.objects.create(
                name='Majestic 12',
                sip_key='majestic-12'
        )
        self.campaign = Campaign.objects.create(
                title='Test Campaign',
                start_date='2000-01-01',
                end_date='2100-12-31',
                organization=self.organization
        )
        self.feedbackcategory = FeedbackCategory.objects.create(
                name='Feedback',
                organization=self.organization
        )

    def test_feedbackcategory_viewset(self):
        client = APIClient()
        response = client.get('/campaigns/%(cid)s/categories/' % {'cid': self.campaign.id}, format='json')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'[{"id":1,"name":"Feedback"}]')
okhin's avatar
okhin committed
100

okhin's avatar
okhin committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
class GroupViewSetTest(TestCase):
    def setUp(self):
        self.organization = Organization.objects.create(
                name='Majestic 12',
                sip_key='majestic-12'
        )
        self.campaign = Campaign.objects.create(
                title='Test Campaign',
                start_date='2000-01-01',
                end_date='2100-12-31',
                organization=self.organization
        )
        self.grouptype = GroupType.objects.create(
                name='comittee',
                organization = self.organization
        )
        self.group = Group.objects.create(
                name="INTA",
                type=self.grouptype
        )

    def test_grouptype_viewset(self):
        client = APIClient()
        response = client.get('/campaigns/%(cid)s/groups/' % {'cid': self.campaign.id}, format='json')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'[{"id":1,"type":"comittee","name":"INTA","media":""}]')

okhin's avatar
okhin committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class GroupTypeViewSetTest(TestCase):
    def setUp(self):
        self.organization = Organization.objects.create(
                name='Majestic 12',
                sip_key='majestic-12'
        )
        self.campaign = Campaign.objects.create(
                title='Test Campaign',
                start_date='2000-01-01',
                end_date='2100-12-31',
                organization=self.organization
        )
        self.grouptype = GroupType.objects.create(
                name='comittee',
                organization = self.organization
        )

    def test_grouptype_viewset(self):
        client = APIClient()
        response = client.get('/campaigns/%(cid)s/grouptypes/' % {'cid': self.campaign.id}, format='json')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'[{"id":1,"name":"comittee"}]')

okhin's avatar
okhin committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
class MockRequest(object):
    pass

class OrganizationAdminTest(TestCase):
    def setUp(self):
        self.organization = Organization.objects.create(
            name='Majestic 12',
            sip_key='majestic-12'
        )
        self.super_user = User.objects.create_superuser(
            username='john',
            email='johndoe@example.com',
            password='password',
        )
        self.user = User.objects.create_user(
            username='jane',
            email='janedoe@example.com',
            password='password'
        )
        self.site = AdminSite()

    def test_get_queryset(self):
        ma = OrganizationAdmin(Organization, self.site)
        request = MockRequest()
        request.user = self.super_user
        self.assertEqual(list(ma.get_queryset(request)), list(Organization.objects.all()))
        request.user = self.user
        self.assertEqual(list(ma.get_queryset(request)), [])
        request.user.organizations = Organization.objects.all()
        self.assertEqual(list(ma.get_queryset(request)), list(Organization.objects.all()))

class GroupTypeAdminTest(TestCase):
    def setUp(self):
        self.organization = Organization.objects.create(
            name='Majestic 12',
            sip_key='majestic-12'
        )
        self.grouptype = GroupType.objects.create(
                name='Group',
                organization=self.organization
        )
        self.super_user = User.objects.create_superuser(
            username='john',
            email='johndoe@example.com',
            password='password',
        )
        self.user = User.objects.create_user(
            username='jane',
            email='janedoe@example.com',
            password='password'
        )
        self.site = AdminSite()

    def test_get_queryset(self):
        ma = GroupTypeAdmin(GroupType, self.site)
        request = MockRequest()
        request.user = self.super_user
        self.assertEqual(list(ma.get_queryset(request)), list(GroupType.objects.all()))
        request.user = self.user
        self.assertEqual(list(ma.get_queryset(request)), [])
        request.user.organizations = Organization.objects.all()
        self.assertEqual(list(ma.get_queryset(request)), list(GroupType.objects.all()))

    def test_formfield_forforeignkey(self):
        ma = GroupTypeAdmin(GroupType, self.site)
        request = MockRequest()
        request.user = self.user
        form = ma.get_form(request)()
        self.assertHTMLEqual(str(form['organization']),
                '<div class="related-widget-wrapper">'
                '<select id="id_organization" name="organization" required>'
                '<option value="" selected="selected">---------</option>'
                '</select>'
                '</div>')
        request.user.organizations = Organization.objects.all()
        form = ma.get_form(request)()
        self.assertHTMLEqual(str(form['organization']),
                '<div class="related-widget-wrapper">'
                '<select id="id_organization" name="organization" required>'
                '<option value="" selected="selected">---------</option>'
                '<option value="%(id)d">%(org)s</option>'
                '</select>'
                '</div>' % {'id': self.organization.id, 'org': self.organization})

okhin's avatar
okhin committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
class GroupAdminTest(TestCase):
    def setUp(self):
        self.organization = Organization.objects.create(
            name='Majestic 12',
            sip_key='majestic-12'
        )
        self.grouptype = GroupType.objects.create(
            name='Group',
            organization=self.organization
        )
        self.group = Group.objects.create(
            name='Test Group',
            type=self.grouptype
        )
        self.super_user = User.objects.create_superuser(
            username='john',
            email='johndoe@example.com',
            password='password',
        )
        self.user = User.objects.create_user(
            username='jane',
            email='janedoe@example.com',
            password='password'
        )

        self.site = AdminSite()

    def test_get_queryset(self):
        ma = GroupAdmin(Group, self.site)
        request = MockRequest()
        request.user = self.super_user
        self.assertEqual(list(ma.get_queryset(request)), list(Group.objects.all()))
        request.user = self.user
        self.assertEqual(list(ma.get_queryset(request)), [])
        request.user.organizations = Organization.objects.all()
        self.assertEqual(list(ma.get_queryset(request)), list(Group.objects.all()))

    def test_formfield_forforeignkey(self):
        ma = GroupAdmin(Group, self.site)
        request = MockRequest()
        request.user = self.user
        form = ma.get_form(request)()
        self.assertHTMLEqual(str(form['type']),
                '<div class="related-widget-wrapper">'
                '<select id="id_type" name="type">'
                '<option value="" selected="selected">---------</option>'
                '</select>'
                '</div>')
        request.user.organizations = Organization.objects.all()
        form = ma.get_form(request)()
        self.assertHTMLEqual(str(form['type']),
                '<div class="related-widget-wrapper">'
                '<select id="id_type" name="type">'
                '<option value="" selected="selected">---------</option>'
                '<option value="%(id)d">%(typ)s</option>'
                '</select>'
                '</div>' % {'id': self.grouptype.id, 'typ': self.grouptype})


class CategoryAdminTest(TestCase):
    def setUp(self):
        self.organization = Organization.objects.create(
            name='Majestic 12',
            sip_key='majestic-12'
        )
        self.group = FeedbackCategory.objects.create(
            name='Test Feedback',
            organization=self.organization
        )
        self.super_user = User.objects.create_superuser(
            username='john',
            email='johndoe@example.com',
            password='password',
        )
        self.user = User.objects.create_user(
            username='jane',
            email='janedoe@example.com',
            password='password'
        )

        self.site = AdminSite()

    def test_get_queryset(self):
        ma = CategoryAdmin(FeedbackCategory, self.site)
        request = MockRequest()
        request.user = self.super_user
        self.assertEqual(list(ma.get_queryset(request)), list(FeedbackCategory.objects.all()))
        request.user = self.user
        self.assertEqual(list(ma.get_queryset(request)), [])
        request.user.organizations = Organization.objects.all()
        self.assertEqual(list(ma.get_queryset(request)), list(FeedbackCategory.objects.all()))

    def test_formfield_forforeignkey(self):
        ma = CategoryAdmin(FeedbackCategory, self.site)
        request = MockRequest()
        request.user = self.user
        form = ma.get_form(request)()
        self.assertHTMLEqual(str(form['organization']),
                '<div class="related-widget-wrapper">'
                '<select id="id_organization" name="organization" required>'
                '<option value="" selected="selected">---------</option>'
                '</select>'
                '</div>')
        request.user.organizations = Organization.objects.all()
        form = ma.get_form(request)()
        self.assertHTMLEqual(str(form['organization']),
                '<div class="related-widget-wrapper">'
                '<select id="id_organization" name="organization" required>'
                '<option value="" selected="selected">---------</option>'
                '<option value="%(id)d">%(org)s</option>'
                '</select>'
                '</div>' % {'id': self.organization.id, 'org': self.organization})