forked from openlibhums/typesetting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
540 lines (469 loc) · 17.8 KB
/
tests.py
File metadata and controls
540 lines (469 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
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
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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
__copyright__ = "Copyright 2017 Birkbeck, University of London"
__author__ = "Martin Paul Eve, Andy Byers & Mauro Sanchez"
__license__ = "AGPL v3"
__maintainer__ = "Birkbeck Centre for Technology and Publishing"
from mock import Mock
import os
from django.test import TestCase
from django.utils import timezone
from django.http import HttpRequest
from django.core.exceptions import PermissionDenied
from django.core.files.base import ContentFile
from django.shortcuts import reverse
from django.conf import settings
from plugins.typesetting import plugin_settings, models, security
from submission import models as submission_models
from utils import setting_handler
from utils.shared import clear_cache
from utils.testing import helpers
from core import models as core_models, urls
from core import files as core_files
from bs4 import BeautifulSoup
class TestTypesetting(TestCase):
@staticmethod
def mock_messages_add(level, message, extra_tags):
pass
@staticmethod
def get_method(field):
return None
@staticmethod
def prepare_request_with_user(user, journal, press=None):
"""
Build a basic request dummy object with the journal set to journal
and the user having editor permissions.
:param user: the user to use
:param journal: the journal to use
:return: an object with user and journal properties
"""
request = Mock(HttpRequest)
request.user = user
request.GET = Mock()
request.GET.get = TestTypesetting.get_method
request.journal = journal
request._messages = Mock()
request._messages.add = TestTypesetting.mock_messages_add
request.path = '/a/fake/path/'
request.path_info = '/a/fake/path/'
request.press = press
return request
def test_proofreader_for_article_required(self):
"""
Tests that an assigned user can pas this check
"""
func = Mock()
kwargs = {'assignment_id': self.galley_proofing.pk}
decorated_func = security.proofreader_for_article_required(func)
request = self.prepare_request_with_user(
self.proofreader,
self.journal_one,
)
decorated_func(request, **kwargs)
self.assertTrue(
func.called,
"Security Error: Proofreader cannot access proofing task.",
)
def test_proofreader_for_article_required_bad_user(self):
"""
Tests a bad user cannot pass this check
"""
func = Mock()
kwargs = {'assignment_id': self.galley_proofing.pk}
decorated_func = security.proofreader_for_article_required(func)
request = self.prepare_request_with_user(
self.article_owner,
self.journal_one,
)
with self.assertRaises(PermissionDenied):
# test that editor_user_required raises a PermissionDenied exception
decorated_func(request, **kwargs)
self.assertFalse(
func.called,
"Security Error: Priviledged user able to access prooding task."
)
def test_require_not_notified(self):
"""
Tests that an assigned user can pas this check
"""
func = Mock()
kwargs = {'assignment_id': self.galley_proofing.pk}
decorator = security.require_not_notified(models.GalleyProofing)
request = self.prepare_request_with_user(
self.editor,
self.journal_one,
)
decorated_func = decorator(func)
decorated_func(request, **kwargs)
self.assertTrue(
func.called,
"Security Error: Editor can't bypass require_not_notified.",
)
def test_require_not_notified_with_notifed_true(self):
"""
Tests that an assigned user can pas this check
"""
func = Mock()
kwargs = {'assignment_id': self.galley_proofing_notified.pk}
decorator = security.require_not_notified(models.GalleyProofing)
request = self.prepare_request_with_user(
self.editor,
self.journal_one,
)
decorated_func = decorator(func)
with self.assertRaises(PermissionDenied):
decorated_func(request, **kwargs)
self.assertFalse(
func.called,
"Security Error: Editor can bypass require_not_notified.",
)
def test_can_manage_file(self):
func = Mock()
kwargs = {'file_id': self.private_file.pk}
decorated_func = security.user_can_manage_file(func)
users = [self.production_manager, self.editor]
for user in users:
request = self.prepare_request_with_user(
self.production_manager,
self.journal_one,
)
decorated_func(request, **kwargs)
self.assertTrue(
func.called,
"Security Error: Priviledged user cannot manage file."
)
def test_can_manage_file_bad_user(self):
func = Mock()
kwargs = {'file_id': self.private_file.pk}
decorated_func = security.user_can_manage_file(func)
users = [self.typesetter, self.bad_user, self.proofreader]
for user in users:
request = self.prepare_request_with_user(
self.typesetter,
self.journal_one,
)
with self.assertRaises(PermissionDenied):
# test that editor_user_required raises a PermissionDenied
# exception
decorated_func(request, **kwargs)
self.assertFalse(
func.called,
"Security Error: Non priviledged user can manage file."
)
def test_good_user_can_preview_typesetting_article(self):
func = Mock()
kwargs = {'assignment_id': self.typesetting_assignment.pk}
decorated_func = security.can_preview_typesetting_article(func)
self.typesetter.is_active = True
request = self.prepare_request_with_user(
self.typesetter,
self.journal_one,
)
decorated_func(request, **kwargs)
self.assertTrue(
func.called,
)
def test_bad_user_cant_preview_typesetting_article(self):
func = Mock()
kwargs = {'assignment_id': self.typesetting_assignment.pk}
decorated_func = security.can_preview_typesetting_article(func)
request = self.prepare_request_with_user(
self.article_owner,
self.journal_one,
)
with self.assertRaises(PermissionDenied):
decorated_func(request, **kwargs)
def test_archive_stage_hides_task(self):
self.client.force_login(self.typesetter)
response = self.client.get(
reverse('typesetting_assignments')
)
self.assertContains(
response,
'Active Article',
)
self.assertNotContains(
response,
'Archived Article'
)
def test_archived_article_task_404s(self):
self.client.force_login(self.typesetter)
response = self.client.get(
reverse(
'typesetting_assignment',
kwargs={
'assignment_id': self.archived_typesetting_task.pk
}
)
)
self.assertTrue(
response.status_code,
404,
)
def test_active_article_task_200s(self):
self.client.force_login(self.typesetter)
response = self.client.get(
reverse(
'typesetting_assignment',
kwargs={
'assignment_id': self.active_typesetting_task.pk
}
)
)
self.assertTrue(
response.status_code,
200,
)
def build_proofing_comparison_dict(self, published, theme):
setting_handler.save_setting(
'general',
'journal_theme',
self.journal_one,
theme,
)
clear_cache()
self.client.force_login(self.typesetter)
if published:
url = reverse(
'article_view',
kwargs={
'identifier_type': 'id',
'identifier': self.article_in_typesetting.pk,
}
)
else:
url = reverse(
'typesetting_preview_galley',
kwargs={
'article_id': self.article_in_typesetting.pk,
'galley_id': self.galley.pk,
'assignment_id': self.active_typesetting_task.pk,
}
)
response = self.client.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
data = []
include_ids = [
'article_opener',
'article_metadata',
'content', # This quite generic id in the OLH theme
# predates this proofing test
'author_biographies',
]
exclude_ids = [
'article_how_to_cite',
'article_date_published',
'note_to_proofreader_1',
'note_to_proofreader_2',
'article_footer_block',
]
for include_id in include_ids:
included_element = soup.find(id=include_id)
if not included_element:
continue
for exclude_id in exclude_ids:
excluded_element = included_element.find(id=exclude_id)
if not excluded_element:
continue
excluded_element.decompose()
data.append(included_element.prettify())
return data
def test_proof_matches_published_article_olh(self):
"""
Tests whether the metadata and article text offered
in proofing matches that of the published article.
"""
self.maxDiff = None
proofing = self.build_proofing_comparison_dict(False, 'OLH')
stage_published = submission_models.STAGE_PUBLISHED
self.article_in_typesetting.stage = stage_published
self.article_in_typesetting.date_published = timezone.now()
self.article_in_typesetting.save()
published = self.build_proofing_comparison_dict(True, 'OLH')
self.article_in_typesetting.stage = plugin_settings.STAGE
self.article_in_typesetting.date_published = None
self.article_in_typesetting.save()
self.assertEqual(proofing, published)
def test_proof_matches_published_article_material(self):
"""
Tests whether the metadata and article text offered
in proofing matches that of the published article.
"""
self.maxDiff = None
proofing = self.build_proofing_comparison_dict(False, 'material')
stage_published = submission_models.STAGE_PUBLISHED
self.article_in_typesetting.stage = stage_published
self.article_in_typesetting.date_published = timezone.now()
self.article_in_typesetting.save()
published = self.build_proofing_comparison_dict(True, 'material')
self.article_in_typesetting.stage = plugin_settings.STAGE
self.article_in_typesetting.date_published = None
self.article_in_typesetting.save()
self.assertEqual(proofing, published)
def test_proof_matches_published_article_clean(self):
"""
Tests whether the metadata and article text offered
in proofing matches that of the published article.
"""
self.maxDiff = None
proofing = self.build_proofing_comparison_dict(False, 'clean')
stage_published = submission_models.STAGE_PUBLISHED
self.article_in_typesetting.stage = stage_published
self.article_in_typesetting.date_published = timezone.now()
self.article_in_typesetting.save()
published = self.build_proofing_comparison_dict(True, 'clean')
self.article_in_typesetting.stage = plugin_settings.STAGE
self.article_in_typesetting.date_published = None
self.article_in_typesetting.save()
self.assertEqual(proofing, published)
@classmethod
def setUpTestData(self):
"""
Setup the test environment.
:return: None
"""
roles_to_setup = [
"reviewer",
"editor",
"production",
"typesetter",
"proofreader",
]
helpers.create_press()
self.journal_one, self.journal_two = helpers.create_journals()
helpers.create_roles(roles_to_setup)
self.editor = helpers.create_editor(self.journal_one)
self.article_owner = helpers.create_regular_user()
self.bad_user = helpers.create_second_user(self.journal_one)
self.proofreader = helpers.create_user(
username='proofer@janeway.systems',
roles=['proofreader'],
journal=self.journal_one,
)
self.proofreader.is_active = True
self.proofreader.save()
self.production_manager = helpers.create_user(
username='production_manager@janeway.systems',
roles=['production'],
journal=self.journal_one,
)
self.typesetter = helpers.create_user(
username='typesetter@janeway.systems',
roles=['typesetter'],
journal=self.journal_one,
**{'first_name': 'Kat', 'last_name': 'Janeway', 'is_active': True}
)
self.typesetter.is_active = True
self.typesetter.save()
self.article_in_typesetting = submission_models.Article.objects.create(
owner=self.article_owner,
title="A Test Article",
abstract="An abstract",
stage=plugin_settings.STAGE,
journal_id=self.journal_one.id
)
self.private_file = core_models.File.objects.create(
mime_type="A/FILE",
original_filename="blah.txt",
uuid_filename="UUID.txt",
label="A file that is private",
description="Oh yes, it's a file",
owner=self.article_owner,
is_galley=False,
privacy="owner",
article_id=self.article_in_typesetting.pk
)
self.workflow_element = core_models.WorkflowElement.objects.create(
journal=self.journal_one,
element_name='Typesetting Plugin',
handshake_url='dummy',
jump_url='dummy',
stage=plugin_settings.STAGE,
article_url=True,
)
self.workflow_log_entry = core_models.WorkflowLog.objects.create(
article=self.article_in_typesetting,
element=self.workflow_element,
)
self.typesetting_round = models.TypesettingRound.objects.create(
article=self.article_in_typesetting,
)
self.typesetting_assignment = models.TypesettingAssignment.objects.create(
round=self.typesetting_round,
manager=self.editor,
typesetter=self.typesetter,
due=timezone.now(),
)
self.test_file_name = 'test_galley.xml'
self.test_file_path = os.path.join(
settings.BASE_DIR,
'plugins',
'typesetting',
self.test_file_name,
)
with open(self.test_file_path, 'rb') as test_file:
content_file = ContentFile(test_file.read())
content_file.name = self.test_file_name
self.galley_file = core_files.save_file_to_article(
content_file,
self.article_in_typesetting,
self.typesetter
)
self.galley, created = core_models.Galley.objects.get_or_create(
article=self.article_in_typesetting,
label='XML',
type='xml',
defaults={'file': self.galley_file},
)
self.galley_proofing = models.GalleyProofing.objects.create(
round=self.typesetting_round,
manager=self.editor,
proofreader=self.proofreader,
due=timezone.now(),
)
self.galley_proofing_notified = models.GalleyProofing.objects.create(
round=self.typesetting_round,
manager=self.editor,
proofreader=self.proofreader,
due=timezone.now(),
notified=True,
)
self.author = helpers.create_user(
'author@janeway.systems',
['author'],
journal=self.journal_one,
)
self.author.is_active = True
self.author.save()
self.active_article = helpers.create_article(
journal=self.journal_one,
)
self.active_article.title = 'Active Article'
self.active_article.save()
self.active_article.authors.add(self.author)
self.archived_article = helpers.create_article(
journal=self.journal_one,
)
self.archived_article.stage = submission_models.STAGE_ARCHIVED
self.archived_article.title = 'Archived Article'
self.archived_article.save()
self.archived_article.authors.add(self.author)
self.active_typesetting_round = models.TypesettingRound.objects.create(
article=self.active_article,
)
self.active_typesetting_task = models.TypesettingAssignment.objects.create(
round=self.active_typesetting_round,
manager=self.editor,
typesetter=self.typesetter,
task='Active Task',
)
self.archived_typesetting_round = models.TypesettingRound.objects.create(
article=self.archived_article,
)
self.archived_typesetting_task = models.TypesettingAssignment.objects.create(
round=self.archived_typesetting_round,
manager=self.editor,
typesetter=self.typesetter,
task='Archived Task',
)
@classmethod
def tearDownClass(cls):
cls.galley.unlink_files()