-
Notifications
You must be signed in to change notification settings - Fork 5
/
integration_test_case.py
857 lines (729 loc) · 34.8 KB
/
integration_test_case.py
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
from AccessControl import getSecurityManager
from AccessControl.SecurityManagement import setSecurityManager
from contextlib import contextmanager
from ftw.builder import Builder
from ftw.builder import create
from ftw.flamegraph import flamegraph
from ftw.mail.mail import IMail
from ftw.solr.connection import SolrResponse
from ftw.solr.interfaces import ISolrSearch
from ftw.solr.schema import SolrSchema
from ftw.testing.mailing import Mailing
from functools import wraps
from mock import MagicMock
from opengever.activity.hooks import insert_notification_defaults
from opengever.activity.mailer import process_mail_queue
from opengever.base.model import create_session
from opengever.base.oguid import Oguid
from opengever.base.role_assignments import RoleAssignmentManager
from opengever.base.role_assignments import SharingRoleAssignment
from opengever.core.testing import OPENGEVER_INTEGRATION_TESTING
from opengever.core.testing import OPENGEVER_SOLR_INTEGRATION_TESTING
from opengever.document.archival_file import STATE_CONVERTED
from opengever.document.behaviors.metadata import IDocumentMetadata
from opengever.document.interfaces import ICheckinCheckoutManager
from opengever.journal.tests.utils import get_journal_entry
from opengever.meeting.model.agendaitem import AgendaItem
from opengever.meeting.wrapper import MeetingWrapper
from opengever.ogds.auth.plugin import install_ogds_auth_plugin
from opengever.ogds.base.utils import get_current_admin_unit
from opengever.ogds.base.utils import get_current_org_unit
from opengever.ogds.models.org_unit import OrgUnit
from opengever.ogds.models.service import ogds_service
from opengever.ogds.models.user import User
from opengever.private import enable_opengever_private
from opengever.task.interfaces import ISuccessorTaskController
from opengever.task.task import ITask
from opengever.testing import assets
from opengever.testing import solr_data_for
from opengever.testing.helpers import SolrTestMixin
from opengever.testing.test_case import TestCase
from opengever.trash.trash import ITrasher
from operator import methodcaller
from plone import api
from plone.api.validation import at_least_one_of
from plone.api.validation import mutually_exclusive_parameters
from plone.app.relationfield.event import update_behavior_relations
from plone.app.testing import applyProfile
from plone.app.testing import login
from plone.app.testing import logout
from plone.app.testing import SITE_OWNER_NAME
from plone.namedfile.file import NamedBlobFile
from plone.portlets.constants import CONTEXT_CATEGORY
from plone.portlets.interfaces import ILocalPortletAssignmentManager
from plone.portlets.interfaces import IPortletManager
from Products.CMFDiffTool.utils import safe_utf8
from Products.PluggableAuthService.interfaces.plugins import IUserEnumerationPlugin
from sqlalchemy.sql.expression import desc
from urllib import urlencode
from z3c.relationfield.relation import RelationValue
from zope.component import getMultiAdapter
from zope.component import getUtility
from zope.i18n import translate
from zope.intid.interfaces import IIntIds
import json
import timeit
FEATURE_FLAGS = {
'activity': 'opengever.activity.interfaces.IActivitySettings.is_feature_enabled',
'bumblebee': 'opengever.bumblebee.interfaces.IGeverBumblebeeSettings.is_feature_enabled',
'bumblebee-open-pdf-new-tab': 'opengever.bumblebee.interfaces.IGeverBumblebeeSettings.open_pdf_in_a_new_window',
'bumblebee-auto-refresh': 'opengever.bumblebee.interfaces.IGeverBumblebeeSettings.is_auto_refresh_enabled',
'changed_for_end_date': 'opengever.dossier.interfaces.IDossierResolveProperties.use_changed_for_end_date',
'disposition-disregard-retention-period': 'opengever.disposition.interfaces.IDispositionSettings.disregard_retention_period', # noqa
'doc-properties': 'opengever.dossier.interfaces.ITemplateFolderProperties.create_doc_properties',
'docugate': 'opengever.docugate.interfaces.IDocugateSettings.is_feature_enabled',
'dossier-checklist': 'opengever.dossier.interfaces.IDossierChecklistSettings.is_feature_enabled',
'dossiertemplate': 'opengever.dossier.dossiertemplate.interfaces.IDossierTemplateSettings.is_feature_enabled',
'dossier-transfers': 'opengever.dossiertransfer.interfaces.IDossierTransferSettings.is_feature_enabled',
'ech0147-export': 'opengever.ech0147.interfaces.IECH0147Settings.ech0147_export_enabled',
'ech0147-import': 'opengever.ech0147.interfaces.IECH0147Settings.ech0147_import_enabled',
'extjs': 'ftw.tabbedview.interfaces.ITabbedView.extjs_enabled',
'gever_ui': 'opengever.base.interfaces.IGeverUI.is_feature_enabled',
'grant_role_manager_to_responsible': 'opengever.dossier.interfaces.IDossierSettings.grant_role_manager_to_responsible', # noqa
'meeting': 'opengever.meeting.interfaces.IMeetingSettings.is_feature_enabled',
'officeconnector-attach': 'opengever.officeconnector.interfaces.'
'IOfficeConnectorSettings.attach_to_outlook_enabled',
'officeconnector-checkout': 'opengever.officeconnector.interfaces.'
'IOfficeConnectorSettings.direct_checkout_and_edit_enabled',
'oneoffixx': 'opengever.oneoffixx.interfaces.IOneoffixxSettings.is_feature_enabled',
'repositoryfolder-documents-tab': 'opengever.repository.interfaces.IRepositoryFolderRecords.show_documents_tab',
'repositoryfolder-proposals-tab': 'opengever.repository.interfaces.IRepositoryFolderRecords.show_proposals_tab',
'repositoryfolder-tasks-tab': 'opengever.repository.interfaces.IRepositoryFolderRecords.show_tasks_tab',
'tasktemplatefolder_nesting': 'opengever.tasktemplates.interfaces.ITaskTemplateSettings.is_tasktemplatefolder_nesting_enabled', # noqa
'workspace': 'opengever.workspace.interfaces.IWorkspaceSettings.is_feature_enabled',
'workspace_client': 'opengever.workspaceclient.interfaces.IWorkspaceClientSettings.is_feature_enabled',
'workspace-meeting': 'opengever.workspace.interfaces.IWorkspaceMeetingSettings.is_feature_enabled',
'workspace-todo': 'opengever.workspace.interfaces.IToDoSettings.is_feature_enabled',
'favorites': 'opengever.base.interfaces.IFavoritesSettings.is_feature_enabled',
'solr': 'opengever.base.interfaces.ISearchSettings.use_solr',
'error_log': 'opengever.base.interfaces.IBaseSettings.is_redis_error_log_feature_enabled',
'purge-trash': 'opengever.dossier.interfaces.IDossierResolveProperties.purge_trash_enabled',
'journal-pdf': 'opengever.dossier.interfaces.IDossierResolveProperties.journal_pdf_enabled',
'tasks-pdf': 'opengever.dossier.interfaces.IDossierResolveProperties.tasks_pdf_enabled',
'private-tasks': 'opengever.task.interfaces.ITaskSettings.private_task_feature_enabled',
'optional-task-permissions-revoking': 'opengever.task.interfaces.ITaskSettings.optional_task_permissions_revoking_enabled', # noqa
'oc-plugin-check-enabled': 'opengever.officeconnector.interfaces.'
'IOfficeConnectorSettings.oc_plugin_check_enabled', # noqa
'sign': 'opengever.sign.interfaces.ISignSettings.is_feature_enabled', # noqa
}
FEATURE_VALUES = {
'ris': ('opengever.ris.interfaces.IRisSettings.base_url', u"http://ris.example.com/"),
}
FEATURE_PROFILES = {
'filing_number': 'opengever.dossier:filing',
}
FEATURE_METHODS = {
'private': enable_opengever_private,
}
class IntegrationTestCase(TestCase):
layer = OPENGEVER_INTEGRATION_TESTING
features = ()
api_headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
maxDiff = None
def setUp(self):
super(IntegrationTestCase, self).setUp()
self.portal = self.layer['portal']
self.request = self.layer['request']
self.deactivate_extjs()
self.install_ogds_plugin()
map(self.parse_feature, self.features)
if 'activity' in self.features:
Mailing(self.portal).set_up()
insert_notification_defaults(self.portal)
def tearDown(self):
super(IntegrationTestCase, self).tearDown()
if 'activity' in self.features:
process_mail_queue()
Mailing(self.portal).tear_down()
def install_ogds_plugin(self):
install_ogds_auth_plugin()
plugin = self.portal.acl_users['ogds_auth']
# Disable RAMCache by default, tests will enable it when needed
plugin.ZCacheable_setManagerId(None)
# Move user enumeration plugin to top position
while not self.portal.acl_users.plugins.listPluginIds(IUserEnumerationPlugin)[0] == plugin.getId():
self.portal.acl_users.plugins.movePluginsUp(IUserEnumerationPlugin, [plugin.getId()])
# Disable user enumeration for source_users
plugin_registry = self.portal.acl_users._getOb('plugins')
ifaces_to_disable = [
IUserEnumerationPlugin,
]
for iface in ifaces_to_disable:
plugin_registry.deactivatePlugin(iface, 'source_users')
@staticmethod
def open_flamegraph(func):
"""Test decorator for opening a flamegraph for creating a flamegraph of
the decorated test and opening it in the default browser using OS X's
"open" command.
Example:
@IntegrationTestCase.open_flamegraph
def test_critical_feature(self):
pass
"""
return flamegraph(open_svg=True)(func)
@staticmethod
def clock(func):
"""Decorator for measuring the duration of a test and printing the result.
This function is meant to be used temporarily in development.
Example:
@IntegrationTestCase.clock
def test_something(self):
pass
"""
@wraps(func)
def wrapper(self, *args, **kwargs):
timer = timeit.default_timer
start = timer()
try:
return func(self, *args, **kwargs)
finally:
end = timer()
print ''
print '{}.{} took {:.3f} ms'.format(
type(self).__name__,
func.__name__,
(end - start) * 1000)
return wrapper
def login(self, user, browser=None):
"""Login a user by passing in the user object.
Common users are available through the USER_LOOKUP_TABLE.
Log in security manager and browser:
>>> self.login(self.dossier_responsible, browser)
Log in only security manager:
>>> self.login(self.dossier_responsible)
The method may also be used used as context manager, ensuring that
after leaving the same user is logged in as before.
"""
if hasattr(user, 'getUserName'):
username = user.getUserName()
else:
username = user
security_manager = getSecurityManager()
if username == SITE_OWNER_NAME:
login(self.layer['app'], username)
else:
login(self.portal, username)
if browser is not None:
browser_auth_headers = filter(
lambda item: item[0] == 'Authorization',
browser.session_headers)
browser.login(username)
@contextmanager
def login_context_manager():
try:
yield
finally:
setSecurityManager(security_manager)
if browser is not None:
browser.clear_request_header('Authorization')
[browser.append_request_header(name, value)
for (name, value) in browser_auth_headers]
return login_context_manager()
def logout(self, browser=None):
"""Logout the currently logged in user.
Log in security manager and browser:
>>> self.logout(browser)
Log in only security manager:
>>> self.logout()
The method may also be used used as context manager, ensuring that
after leaving the same user is logged in as before.
"""
security_manager = getSecurityManager()
logout()
if browser is not None:
browser_auth_headers = filter(
lambda item: item[0] == 'Authorization',
browser.session_headers)
browser.logout()
@contextmanager
def logout_context_manager():
try:
yield
finally:
setSecurityManager(security_manager)
if browser is not None:
browser.clear_request_header('Authorization')
[browser.append_request_header(name, value)
for (name, value) in browser_auth_headers]
return logout_context_manager()
def deactivate_extjs(self):
"""ExtJS is JavaScript and therefore currently untestable with
ftw.testbrowser.
In order to test listing tabs, we disable ExtJS in tests by default.
It can be reactivated by activating the fature "extjs":
>>> self.activate_feature('extjs')
"""
api.portal.set_registry_record(
'ftw.tabbedview.interfaces.ITabbedView.extjs_enabled', False)
def parse_feature(self, feature):
"""Activate or deactivate a feature flag."""
if feature.startswith('!'):
self.deactivate_feature(feature.split('!')[-1])
else:
self.activate_feature(feature)
def activate_feature(self, feature):
"""Activate a feature flag.
"""
if feature in FEATURE_FLAGS:
api.portal.set_registry_record(FEATURE_FLAGS[feature], True)
elif feature in FEATURE_VALUES:
key, value = FEATURE_VALUES[feature]
api.portal.set_registry_record(key, value)
elif feature in FEATURE_PROFILES:
applyProfile(self.portal, FEATURE_PROFILES[feature])
elif feature in FEATURE_METHODS:
FEATURE_METHODS[feature]()
else:
raise ValueError('Invalid {!r}'.format(feature))
def deactivate_feature(self, feature):
"""Deactivate a feature flag.
"""
if feature in FEATURE_FLAGS:
api.portal.set_registry_record(FEATURE_FLAGS[feature], False)
elif feature in FEATURE_VALUES:
key = FEATURE_VALUES[feature][0]
api.portal.set_registry_record(key, u'')
elif feature in FEATURE_PROFILES:
raise NotImplementedError('Feel free to implement.')
elif feature in FEATURE_METHODS:
raise NotImplementedError('Feel free to implement.')
else:
raise ValueError('Invalid {!r}'.format(feature))
def __getattr__(self, name):
"""Make it possible to access objects from the content lookup table
directly with attribute access on the test case.
"""
obj = self._lookup_from_table(name)
if obj is not None:
return obj
else:
return self.__getattribute__(name)
@property
def dossier_tasks(self):
"""All tasks within self.dossier.
"""
return map(self.brain_to_object,
api.content.find(self.dossier, object_provides=ITask))
def _lookup_from_table(self, name):
"""This method helps to look up persistent objects or user objects which
were created in the fixture and registered there with a name.
"""
try:
table = self.layer['fixture_lookup_table']
except KeyError:
# The layer has not yet set up the fixture.
return None
if name not in table:
return None
type_, value = table[name]
if type_ == 'object':
locals()['__traceback_info__'] = {
'path': value,
'current user': getSecurityManager().getUser()}
return self.portal.restrictedTraverse(value)
elif type_ == 'user':
return api.user.get(value)
elif type_ == 'raw':
return value
else:
raise ValueError('Unsupport lookup entry type {!r}'.format(type_))
def get_catalog_indexdata(self, obj):
"""Return the catalog index data for an object as dict.
"""
catalog = api.portal.get_tool('portal_catalog')
rid = catalog.getrid('/'.join(obj.getPhysicalPath()))
return catalog.getIndexDataForRID(rid)
@staticmethod
def dateindex_value_from_datetime(datetime_obj):
yr, mo, dy, hr, mn = datetime_obj.timetuple()[:5]
return ((((yr * 12 + mo) * 31 + dy) * 24 + hr) * 60 + mn)
def assert_index_value(self, expected_value, index_name, *objects):
"""Asserts that an index exists and has a specific value for a
given object.
"""
for obj in objects:
index_data = self.get_catalog_indexdata(obj)
self.assertIn(
index_name, index_data,
'Index {!r} does not exist.'.format(index_name))
self.assertEquals(
expected_value, index_data[index_name],
'Unexpected index value {!r} in index {!r} for {!r}'.format(
index_data[index_name], index_name, obj))
def assert_json_structure_equal(self, expected_value, got, msg=None):
got = json.dumps(got, sort_keys=True, indent=4)
expected_value = json.dumps(expected_value, sort_keys=True, indent=4)
self.maxDiff = None
self.assertMultiLineEqual(expected_value, got, msg)
def get_catalog_metadata(self, obj):
"""Return the catalog metadata for an object as dict.
"""
catalog = api.portal.get_tool('portal_catalog')
rid = catalog.getrid('/'.join(obj.getPhysicalPath()))
return catalog.getMetadataForRID(rid)
def assert_metadata_value(self, expected_value, metadata_name, *objects):
"""Asserts that an metadata exists and has a specific value for a
given object.
"""
for obj in objects:
metadata = self.get_catalog_metadata(obj)
self.assertIn(
metadata_name, metadata,
'Metadata {!r} does not exist.'.format(metadata_name))
self.assertEquals(
expected_value, metadata[metadata_name],
'Unexpected metadata value {!r} in metadata {!r} for {!r}'.format(
metadata[metadata_name], metadata_name, obj))
def assert_index_and_metadata(self, expected_value, name, *objects):
"""Assert that an index and a metadata with the same name both exist
and have the same value for a given object.
"""
self.assert_index_value(expected_value, name, *objects)
self.assert_metadata_value(expected_value, name, *objects)
def set_workflow_state(self, new_workflow_state_id, *objects):
"""Set the workflow state of one or many objects.
When the state is changed for multiple nested objects at once, the
method can optimize reindexing security so that it is not executed
multiple times for the same object.
"""
wftool = api.portal.get_tool('portal_workflow')
for obj in objects:
chain = wftool.getChainFor(obj)
self.assertEquals(
1, len(chain),
'set_workflow_state does only support objects with'
' exactly one workflow, but {!r} has {!r}.'.format(obj, chain))
workflow = wftool[chain[0]]
self.assertIn(new_workflow_state_id, workflow.states)
wftool.setStatusOf(chain[0], obj, {
'review_state': new_workflow_state_id,
'action': '',
'actor': ''})
workflow.updateRoleMappingsFor(obj)
obj.reindexObject(idxs=['review_state'])
if ITask.providedBy(obj):
obj.get_sql_object().review_state = new_workflow_state_id
# reindexObjectSecurity is recursive. We should avoid updating the same
# object twice when the parent is changed too.
security_reindexed = []
for obj in sorted(objects, key=methodcaller('getPhysicalPath')):
current_path = '/'.join(obj.getPhysicalPath())
if any(filter(lambda path: current_path.startswith(path),
security_reindexed)):
# We just have updated the security of a parent recursively,
# thus the security of ``obj`` must be up to date at this point.
break
obj.reindexObjectSecurity()
security_reindexed.append(current_path)
def assert_workflow_state(self, workflow_state_id, obj):
"""Assert the workflow state of an object and of its brain.
"""
expected = {
'object': workflow_state_id,
'catalog index': workflow_state_id,
'catalog metadata': workflow_state_id}
got = {
'object': api.content.get_state(obj),
'catalog index': self.get_catalog_indexdata(obj)['review_state'],
'catalog metadata': self.get_catalog_metadata(obj)['review_state']}
self.assertEqual(
expected, got,
'Object {!r} has an incorrect workflow state.'.format(obj))
def assert_has_permissions(self, permissions, obj, msg=None):
"""Assert that the current user has all given permissions on the context.
"""
missing_permissions = [
permission for permission in permissions
if not api.user.has_permission(permission, obj=obj)]
self.assertEquals(
[], missing_permissions,
'Missing permissions for user {!r} on {!r}. {}'.format(
api.user.get_current(),
obj,
msg or ''))
def assert_has_not_permissions(self, permissions, obj, msg=None):
"""Assert that the current user has none of the given permissions
on the context.
"""
present_permissions = [
permission for permission in permissions
if api.user.has_permission(permission, obj=obj)]
self.assertEquals(
[], present_permissions,
'Too many permissions for user {!r} on {!r}. {}'.format(
api.user.get_current(),
obj,
msg or ''))
def assert_journal_entry(self, obj, action_type, title, comment=None, entry=-1): # noqa
entry = get_journal_entry(obj, entry)
action = entry.get('action')
self.assertEquals(action_type, action.get('type'))
self.assertEquals(title, translate(action.get('title')))
if comment is not None:
self.assertEquals(comment, entry.get('comments'))
def assert_local_roles(self, expected_roles, user, context):
if hasattr(user, 'getId'):
userid = user.getId()
else:
userid = user
current_roles = dict(context.get_local_roles()).get(userid, [])
self.assertItemsEqual(
expected_roles, current_roles,
"The user '{}' should have the roles {!r} on context {!r}. "
"But he has {}".format(userid, expected_roles, context, current_roles))
def brain_to_object(self, brain):
"""Return the object of a brain.
"""
return brain.getObject()
def object_to_brain(self, obj):
"""Return the brain of an object.
Make sure that the current user is allowed to view the object.
"""
catalog = api.portal.get_tool('portal_catalog')
brains = catalog(path={'query': '/'.join(obj.getPhysicalPath()),
'depth': 0})
self.assertEquals(1, len(brains))
return brains[0]
def enable_languages(self):
"""Enable a multi-language configuration with German, French and English.
"""
lang_tool = api.portal.get_tool('portal_languages')
lang_tool.use_combined_language_codes = True
lang_tool.addSupportedLanguage('de-ch')
lang_tool.addSupportedLanguage('fr-ch')
lang_tool.addSupportedLanguage('en')
def set_related_items(self, obj, items, fieldname='relatedItems',
append=False):
"""Set the related items on an object and update the relation catalog.
"""
assert isinstance(items, (list, tuple)), 'items must be list or tuple'
if append:
relations = getattr(obj, fieldname, [])
else:
relations = []
intids = map(getUtility(IIntIds).getId, items)
relations += map(RelationValue, intids)
setattr(obj, fieldname, relations)
update_behavior_relations(obj, None)
def add_related_item(self, obj, related_obj, fieldname='relatedItems'):
"""Add a relation from obj to related_obj.
"""
self.set_related_items(obj, [related_obj], fieldname=fieldname,
append=True)
def checkout_document(self, document, collaborative=False):
"""Checkout the given document.
"""
return self.get_checkout_manager(document).checkout(collaborative=collaborative)
def checkin_document(self, document):
"""Checkin the given document.
"""
return self.get_checkout_manager(document).checkin()
def cancel_document_checkout(self, document):
"""Checkin the given document.
"""
return self.get_checkout_manager(document).cancel()
def get_checkout_manager(self, document):
"""Returns the checkin checkout manager for a document.
"""
return getMultiAdapter((document, document.REQUEST),
ICheckinCheckoutManager)
def schedule_proposal(self, meeting, submitted_proposal):
"""Meeting: schedule a proposal for a meeting and return the
agenda item.
"""
if isinstance(meeting, MeetingWrapper):
meeting = meeting.model
proposal_model = submitted_proposal.load_model()
meeting.schedule_proposal(proposal_model)
self.assertEquals(proposal_model.STATE_SCHEDULED,
proposal_model.get_state())
agenda_item = AgendaItem.query.order_by(desc('id')).first()
return agenda_item
def schedule_ad_hoc(self, meeting, title):
if isinstance(meeting, MeetingWrapper):
meeting = meeting.model
meeting.schedule_ad_hoc(title)
agenda_item = AgendaItem.query.order_by(desc('id')).first()
return agenda_item
def schedule_paragraph(self, meeting, title):
if isinstance(meeting, MeetingWrapper):
meeting = meeting.model
meeting.schedule_text(title, is_paragraph=True)
agenda_item = AgendaItem.query.order_by(desc('id')).first()
return agenda_item
def decide_agendaitem_generate_and_return_excerpt(self, agendaitem, excerpt_title=None):
"""Meeting: decide an agendaitem, then generate the excerpt and
return it to the dossier. This will set the proposal to decided.
"""
agendaitem.decide()
excerpt = agendaitem.generate_excerpt(excerpt_title or agendaitem.get_title())
agendaitem.return_excerpt(excerpt)
def generate_protocol_document(self, meeting):
if isinstance(meeting, MeetingWrapper):
meeting = meeting.model
meeting.update_protocol_document()
def generate_agenda_item_list(self, meeting):
if isinstance(meeting, MeetingWrapper):
meeting = meeting.model
from opengever.meeting.command import AgendaItemListOperations
from opengever.meeting.command import CreateGeneratedDocumentCommand
command = CreateGeneratedDocumentCommand(
meeting.get_dossier(), meeting, AgendaItemListOperations(),
)
return command.execute()
def as_relation_value(self, obj):
return RelationValue(getUtility(IIntIds).getId(obj))
def assert_portlet_inheritance_blocked(self, manager_name, obj):
manager = getUtility(
IPortletManager, name=manager_name, context=obj)
assignable = getMultiAdapter(
(obj, manager), ILocalPortletAssignmentManager)
self.assertTrue(assignable.getBlacklistStatus(CONTEXT_CATEGORY))
def change_mail_data(self, mail, data):
old_file = IMail(mail).message
IMail(mail).message = NamedBlobFile(data=data, filename=old_file.filename)
def set_archival_file(self, document, data,
filename=u'test.pdf',
conversion_state=STATE_CONVERTED):
archival_file = NamedBlobFile(data=data, filename=u'test.pdf')
IDocumentMetadata(document).archival_file_state = conversion_state
IDocumentMetadata(document).archival_file = archival_file
def agenda_item_url(self, agenda_item, endpoint):
return '{}/agenda_items/{}/{}'.format(
agenda_item.meeting.get_url(view=None),
agenda_item.agenda_item_id,
endpoint)
def query_source_url(self, context, field, add=None, **kwargs):
base_url = context.absolute_url()
if add:
components = [base_url, '@querysources', add, field]
else:
components = [base_url, '@querysources', field]
params = {key: safe_utf8(value) for key, value in kwargs.items()}
url = '/'.join(components)
if params:
return '{}?{}'.format(url, urlencode(params))
return url
def get_ogds_user(self, user):
return ogds_service().fetch_user(user.getId())
def get_allowed_roles_and_users_for(self, obj):
"""Returns the indexed value of 'allowedRolesAndUsers'
"""
catalog = api.portal.get_tool('portal_catalog')
rid = catalog.getrid('/'.join(obj.getPhysicalPath()))
return catalog.getIndexDataForRID(rid).get('allowedRolesAndUsers')
def make_path_param(self, *objects):
"""Build a paths:list request parameter, as expected by some views.
"""
return {
'paths:list': ['/'.join(obj.getPhysicalPath()) for obj in objects]}
def make_pseudorelative_path_param(self, *objects):
"""Build a paths:list request parameter with pseudo-relative paths.
Pseudo-relative in this context means that the paths don't start with
the Plone site (they are relative to it), but they still start with
a leading slash.
We get paths like this from the new gever-ui in some cases, that's
why some of our views need to also handle them.
"""
def pseudo_relpath(obj):
return '/' + '/'.join(obj.getPhysicalPath()[2:])
return {
'paths:list': [pseudo_relpath(obj) for obj in objects]}
@mutually_exclusive_parameters('response_file', 'response_json')
@at_least_one_of('response_file', 'response_json')
def mock_solr(self, response_file=None, response_json=None):
conn = MagicMock(name='SolrConnection')
schema_resp = assets.load('solr_schema.json')
conn.get = MagicMock(name='get', return_value=SolrResponse(
body=schema_resp, status=200))
manager = MagicMock(name='SolrConnectionManager')
manager.connection = conn
manager.schema = SolrSchema(manager)
solr = getUtility(ISolrSearch)
solr._manager = manager
if response_file:
search_resp = assets.load(response_file)
else:
# Make sure status is in header
if u'responseHeader' not in response_json:
response_json[u'responseHeader'] = {}
if 'status' not in response_json[u'responseHeader']:
response_json[u'responseHeader']['status'] = 0
search_resp = json.dumps(response_json)
solr.search = MagicMock(name='search', return_value=SolrResponse(
body=search_resp, status=200))
return solr
def add_additional_org_unit(self, id_=u'gdgs'):
return create(Builder('org_unit')
.id(id_)
.with_default_groups()
.having(admin_unit=get_current_admin_unit()))
def disable_additional_org_units(self, org_unit_ids=None):
if org_unit_ids is None:
org_unit_ids = ('rk',)
current_ou = get_current_org_unit()
if current_ou in org_unit_ids:
raise ValueError
for ou_id in org_unit_ids:
ou = OrgUnit.get(ou_id)
create_session().delete(ou)
current_ou._chosen_strategy = None
def add_additional_admin_and_org_unit(self, id_=u'gdgs', title=u'Generalsekretari\xe4t'):
admin_unit = create(Builder('admin_unit')
.having(title=title,
unit_id=id_,
public_url='http://nohost/plone'))
org_unit = create(Builder('org_unit')
.id(id_)
.with_default_groups()
.having(admin_unit=admin_unit))
return admin_unit, org_unit
def assert_solr_called(self, solr, text, **kwargs):
solr.search.assert_called_with(query=text, **kwargs)
def register_successor(self, predecessor, successor):
ISuccessorTaskController(successor).set_predecessor(
Oguid.for_object(predecessor).id)
predecessor.get_sql_object().sync_with(predecessor)
successor.get_sql_object().sync_with(successor)
def get_workflow_transitions_for(self, obj):
wftool = api.portal.get_tool('portal_workflow')
return [transition['id'] for transition in
wftool.listActionInfos(object=obj, check_condition=True)]
def create_inactive_user(self):
create(Builder('ogds_user')
.having(firstname='Without', lastname='Orgunit',
userid='user.without.orgunit'))
def trash_documents(self, *objects):
for obj in objects:
ITrasher(obj).trash()
def assert_provides(self, obj, interface=None):
self.assertTrue(interface.providedBy(obj), '{} should provide {}'.format(obj, interface))
def set_roles(self, obj, principal, roles):
RoleAssignmentManager(obj).add_or_update_assignment(
SharingRoleAssignment(principal, roles))
def change_loginname(self, userid, new_loginname):
acl_users = api.portal.get_tool('acl_users')
acl_users.source_users.updateUser(userid, new_loginname)
user = User.query.get(userid)
user.username = new_loginname
class SolrIntegrationTestCase(IntegrationTestCase, SolrTestMixin):
layer = OPENGEVER_SOLR_INTEGRATION_TESTING
features = ('solr', )
def set_roles(self, *args, **kwargs):
super(SolrIntegrationTestCase, self).set_roles(*args, **kwargs)
self.commit_solr()
def assert_solr_field_value(self, expected_value, field_name, *objects):
for obj in objects:
solr_data = solr_data_for(obj)
self.assertIn(
field_name, solr_data,
'Field {!r} does not exist.'.format(field_name))
if isinstance(expected_value, str):
expected_value = expected_value.decode('utf8')
self.assertEqual(
expected_value, solr_data[field_name],
'Unexpected value {!r} for field {!r} of {!r}'.format(
solr_data[field_name], field_name, obj))