Completed
Pull Request — develop (#31)
by inkhey
03:11
created

backend.tracim_backend.tests.library.test_content_api   F

Complexity

Total Complexity 99

Size/Duplication

Total Lines 3068
Duplicated Lines 27.71 %

Importance

Changes 0
Metric Value
wmc 99
eloc 2406
dl 850
loc 3068
rs 0.8
c 0
b 0
f 0

40 Methods

Rating   Name   Duplication   Size   Complexity  
B TestContentApi.test_search_in_label_or_description() 0 87 3
B TestContentApi.test_update_no_change() 0 79 4
B TestContentApi.test_unit__get_last_active__ok__nominal_case() 0 79 2
B TestContentApi.test_unit_copy_file__same_label_different_parent_ok() 118 118 2
B TestContentApi.test_unit_copy_file_different_label_different_parent_ok() 120 120 2
F TestContentApi.test_mark_read__workspace() 0 106 13
A TestContentApi.test_compare_content_for_sorting_by_label() 0 14 1
A TestContentApi.test_unit__set_allowed_content__ok__private_method() 41 41 1
B TestContentApi.test_unit__get_last_active__ok__workspace_filter_workspace_full() 0 64 2
B TestContentApi.test_archive_unarchive() 160 160 3
A TestContentApi.test_sort_by_content_type() 0 18 1
B TestContentApi.test_unit_copy_file_different_label_same_parent_ok() 0 105 2
B TestContentApi.test_get_all_with_parent_id() 0 74 1
B TestContentApi.test_delete_undelete() 158 158 3
A TestContentApi.test_unit__create_content__OK_nominal_case() 35 35 1
B TestContentApi.test_unit__restore_content_default_allowed_content__ok__nominal_case() 0 44 1
A TestContentApi.test_unit__create_content__err_content_type_not_allowed_in_this_workspace() 34 34 2
B TestContentApi.test_unit__get_last_active__ok__do_no_show_deleted_archived() 0 94 3
B TestContentApi.test_search_in_label() 54 54 2
C TestContentApi.test_mark_read__all() 0 98 7
B TestContentApi.test_update_file_data() 0 128 2
A TestContentApi.test_compare_content_for_sorting_by_type() 0 14 1
B TestContentApi.test_unit__get_last_active__ok__workspace_filter_workspace_empty() 0 59 2
A TestContentApi.test_set_status_unknown_status() 0 39 3
A TestContentApi.test_create_comment_ok() 0 43 1
B TestContentApi.test_unit__create_content__err_content_type_not_allowed_in_this_folder() 0 65 3
B TestContentApi.test_unit__get_last_active__ok__workspace_filter_workspace_limit_2_multiples_times() 0 73 2
B TestContentApi.test_get_all_with_filter() 0 76 1
B TestContentApi.test_archive() 0 117 2
A TestContentApi.test_sort_by_label_or_filename() 0 20 1
B TestContentApiSecurity.test_unit__cant_get_non_access_content__ok__nominal_case() 0 56 1
B TestContentApi.test_update() 0 132 2
A TestContentApi.test_unit__create_content__err_empty_label() 34 34 2
B TestContentApi.test_mark_read() 0 72 3
A TestContentApi.test_set_status_ok() 0 43 3
A TestContentApi.test_unit__set_allowed_content__ok__nominal_case() 41 41 1
B TestContentApi.test_search_in_description() 55 55 2
B TestContentApi.test_unit__get_last_active__ok__workspace_filter_workspace_content_ids() 0 74 2
B TestContentApi.test_unit__search_exclude_content_under_deleted_or_archived_parents__ok() 0 73 3
B TestContentApi.test_delete() 0 103 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like backend.tracim_backend.tests.library.test_content_api often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
import datetime
3
import transaction
4
import pytest
5
6
from tracim_backend.config import CFG
7
from tracim_backend.lib.core.content import compare_content_for_sorting_by_type_and_name
8
from tracim_backend.lib.core.content import ContentApi
9
# TODO - G.M - 28-03-2018 - [GroupApi] Re-enable GroupApi
10
from tracim_backend.lib.core.group import GroupApi
11
from tracim_backend.lib.core.user import UserApi
12
from tracim_backend.exceptions import SameValueError
13
from tracim_backend.exceptions import EmptyLabelNotAllowed
14
from tracim_backend.exceptions import UnallowedSubContent
15
# TODO - G.M - 28-03-2018 - [RoleApi] Re-enable RoleApi
16
from tracim_backend.lib.core.workspace import RoleApi
17
# TODO - G.M - 28-03-2018 - [WorkspaceApi] Re-enable WorkspaceApi
18
from tracim_backend.lib.core.workspace import WorkspaceApi
19
from tracim_backend.app_models.contents import CONTENT_TYPES
20
from tracim_backend.models.revision_protection import new_revision
21
from tracim_backend.models.auth import User
22
from tracim_backend.models.auth import Group
23
24
from tracim_backend.models.data import ActionDescription
25
from tracim_backend.models.data import ContentRevisionRO
26
from tracim_backend.models.data import Workspace
27
from tracim_backend.models.data import Content
28
from tracim_backend.models.data import UserRoleInWorkspace
29
from tracim_backend.fixtures.users_and_groups import Test as FixtureTest
30
from tracim_backend.tests import DefaultTest
31
from tracim_backend.tests import eq_
32
33
34
class TestContentApi(DefaultTest):
35
36
    def test_compare_content_for_sorting_by_type(self):
37
        c1 = Content()
38
        c1.label = ''
39
        c1.type = 'file'
40
41
        c2 = Content()
42
        c2.label = ''
43
        c2.type = 'folder'
44
45
        c11 = c1
46
47
        eq_(1, compare_content_for_sorting_by_type_and_name(c1, c2))
48
        eq_(-1, compare_content_for_sorting_by_type_and_name(c2, c1))
49
        eq_(0, compare_content_for_sorting_by_type_and_name(c1, c11))
50
51
    def test_compare_content_for_sorting_by_label(self):
52
        c1 = Content()
53
        c1.label = 'bbb'
54
        c1.type = 'file'
55
56
        c2 = Content()
57
        c2.label = 'aaa'
58
        c2.type = 'file'
59
60
        c11 = c1
61
62
        eq_(1, compare_content_for_sorting_by_type_and_name(c1, c2))
63
        eq_(-1, compare_content_for_sorting_by_type_and_name(c2, c1))
64
        eq_(0, compare_content_for_sorting_by_type_and_name(c1, c11))
65
66
    def test_sort_by_label_or_filename(self):
67
        c1 = Content()
68
        c1.label = 'ABCD'
69
        c1.type = 'file'
70
71
        c2 = Content()
72
        c2.label = ''
73
        c2.type = 'file'
74
        c2.file_name = 'AABC'
75
76
        c3 = Content()
77
        c3.label = 'BCDE'
78
        c3.type = 'file'
79
80
        items = [c1, c2, c3]
81
        sorteds = ContentApi.sort_content(items)
82
83
        eq_(sorteds[0], c2)
84
        eq_(sorteds[1], c1)
85
        eq_(sorteds[2], c3)
86
87
    def test_sort_by_content_type(self):
88
        c1 = Content()
89
        c1.label = 'AAAA'
90
        c1.type = 'file'
91
92
        c2 = Content()
93
        c2.label = 'BBBB'
94
        c2.type = 'folder'
95
96
        items = [c1, c2]
97
        sorteds = ContentApi.sort_content(items)
98
99
        eq_(sorteds[0], c2,
100
            'value is {} instead of {}'.format(sorteds[0].content_id,
101
                                               c2.content_id))
102
        eq_(sorteds[1], c1,
103
            'value is {} instead of {}'.format(sorteds[1].content_id,
104
                                               c1.content_id))
105
106 View Code Duplication
    def test_unit__create_content__OK_nominal_case(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
107
        uapi = UserApi(
108
            session=self.session,
109
            config=self.app_config,
110
            current_user=None,
111
        )
112
        group_api = GroupApi(
113
            current_user=None,
114
            session=self.session,
115
            config=self.app_config,
116
        )
117
        groups = [group_api.get_one(Group.TIM_USER),
118
                  group_api.get_one(Group.TIM_MANAGER),
119
                  group_api.get_one(Group.TIM_ADMIN)]
120
121
        user = uapi.create_minimal_user(email='this.is@user',
122
                                        groups=groups, save_now=True)
123
        workspace = WorkspaceApi(
124
            current_user=user,
125
            session=self.session,
126
            config=self.app_config,
127
        ).create_workspace('test workspace', save_now=True)
128
        api = ContentApi(
129
            current_user=user,
130
            session=self.session,
131
            config=self.app_config,
132
        )
133
        item = api.create(
134
            content_type_slug=CONTENT_TYPES.Folder.slug,
135
            workspace=workspace,
136
            parent=None,
137
            label='not_deleted',
138
            do_save=True
139
        )
140
        assert isinstance(item, Content)
141
142 View Code Duplication
    def test_unit__create_content__err_empty_label(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
143
        uapi = UserApi(
144
            session=self.session,
145
            config=self.app_config,
146
            current_user=None,
147
        )
148
        group_api = GroupApi(
149
            current_user=None,
150
            session=self.session,
151
            config=self.app_config,
152
        )
153
        groups = [group_api.get_one(Group.TIM_USER),
154
                  group_api.get_one(Group.TIM_MANAGER),
155
                  group_api.get_one(Group.TIM_ADMIN)]
156
157
        user = uapi.create_minimal_user(email='this.is@user',
158
                                        groups=groups, save_now=True)
159
        workspace = WorkspaceApi(
160
            current_user=user,
161
            session=self.session,
162
            config=self.app_config,
163
        ).create_workspace('test workspace', save_now=True)
164
        api = ContentApi(
165
            current_user=user,
166
            session=self.session,
167
            config=self.app_config,
168
        )
169
        with pytest.raises(EmptyLabelNotAllowed):
170
            api.create(
171
                content_type_slug=CONTENT_TYPES.Thread.slug,
172
                workspace=workspace,
173
                parent=None,
174
                label='',
175
                do_save=True
176
            )
177
178
    def test_unit__create_content__err_content_type_not_allowed_in_this_folder(self):
179
        uapi = UserApi(
180
            session=self.session,
181
            config=self.app_config,
182
            current_user=None,
183
        )
184
        group_api = GroupApi(
185
            current_user=None,
186
            session=self.session,
187
            config=self.app_config,
188
        )
189
        groups = [group_api.get_one(Group.TIM_USER),
190
                  group_api.get_one(Group.TIM_MANAGER),
191
                  group_api.get_one(Group.TIM_ADMIN)]
192
193
        user = uapi.create_minimal_user(email='this.is@user',
194
                                        groups=groups, save_now=True)
195
        workspace = WorkspaceApi(
196
            current_user=user,
197
            session=self.session,
198
            config=self.app_config,
199
        ).create_workspace('test workspace', save_now=True)
200
        api = ContentApi(
201
            current_user=user,
202
            session=self.session,
203
            config=self.app_config,
204
        )
205
        folder = api.create(
206
            content_type_slug=CONTENT_TYPES.Folder.slug,
207
            workspace=workspace,
208
            parent=None,
209
            label='plop',
210
            do_save=False
211
        )
212
        allowed_content_dict = {CONTENT_TYPES.Folder.slug: True, CONTENT_TYPES.File.slug: False} # nopep8
213
        api._set_allowed_content(
214
            folder,
215
            allowed_content_dict=allowed_content_dict
216
        )
217
        api.save(content=folder)
218
        # not in list -> do not allow
219
        with pytest.raises(UnallowedSubContent):
220
            api.create(
221
                content_type_slug=CONTENT_TYPES.Event.slug,
222
                workspace=workspace,
223
                parent=folder,
224
                label='lapin',
225
                do_save=True
226
            )
227
        # in list but false -> do not allow
228
        with pytest.raises(UnallowedSubContent):
229
            api.create(
230
                content_type_slug=CONTENT_TYPES.File.slug,
231
                workspace=workspace,
232
                parent=folder,
233
                label='lapin',
234
                do_save=True
235
            )
236
        # in list and true -> allow
237
        api.create(
238
            content_type_slug=CONTENT_TYPES.Folder.slug,
239
            workspace=workspace,
240
            parent=folder,
241
            label='lapin',
242
            do_save=True
243
        )
244
245 View Code Duplication
    def test_unit__create_content__err_content_type_not_allowed_in_this_workspace(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
246
        uapi = UserApi(
247
            session=self.session,
248
            config=self.app_config,
249
            current_user=None,
250
        )
251
        group_api = GroupApi(
252
            current_user=None,
253
            session=self.session,
254
            config=self.app_config,
255
        )
256
        groups = [group_api.get_one(Group.TIM_USER),
257
                  group_api.get_one(Group.TIM_MANAGER),
258
                  group_api.get_one(Group.TIM_ADMIN)]
259
260
        user = uapi.create_minimal_user(email='this.is@user',
261
                                        groups=groups, save_now=True)
262
        workspace = WorkspaceApi(
263
            current_user=user,
264
            session=self.session,
265
            config=self.app_config,
266
        ).create_workspace('test workspace', save_now=True)
267
        api = ContentApi(
268
            current_user=user,
269
            session=self.session,
270
            config=self.app_config,
271
        )
272
        with pytest.raises(UnallowedSubContent):
273
            api.create(
274
                content_type_slug=CONTENT_TYPES.Event.slug,
275
                workspace=workspace,
276
                parent=None,
277
                label='lapin',
278
                do_save=True
279
           )
280
281 View Code Duplication
    def test_unit__set_allowed_content__ok__private_method(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
282
        uapi = UserApi(
283
            session=self.session,
284
            config=self.app_config,
285
            current_user=None,
286
        )
287
        group_api = GroupApi(
288
            current_user=None,
289
            session=self.session,
290
            config=self.app_config,
291
        )
292
        groups = [group_api.get_one(Group.TIM_USER),
293
                  group_api.get_one(Group.TIM_MANAGER),
294
                  group_api.get_one(Group.TIM_ADMIN)]
295
296
        user = uapi.create_minimal_user(email='this.is@user',
297
                                        groups=groups, save_now=True)
298
        workspace = WorkspaceApi(
299
            current_user=user,
300
            session=self.session,
301
            config=self.app_config,
302
        ).create_workspace('test workspace', save_now=True)
303
        api = ContentApi(
304
            current_user=user,
305
            session=self.session,
306
            config=self.app_config,
307
        )
308
        folder = api.create(
309
            content_type_slug=CONTENT_TYPES.Folder.slug,
310
            workspace=workspace,
311
            parent=None,
312
            label='plop',
313
            do_save=False
314
        )
315
        allowed_content_dict = {CONTENT_TYPES.Folder.slug: True, CONTENT_TYPES.File.slug: False}  # nopep8
316
        api._set_allowed_content(
317
            folder,
318
            allowed_content_dict=allowed_content_dict
319
        )
320
        assert 'allowed_content' in folder.properties
321
        assert folder.properties['allowed_content'] == {CONTENT_TYPES.Folder.slug: True, CONTENT_TYPES.File.slug: False}
322
323 View Code Duplication
    def test_unit__set_allowed_content__ok__nominal_case(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
324
        uapi = UserApi(
325
            session=self.session,
326
            config=self.app_config,
327
            current_user=None,
328
        )
329
        group_api = GroupApi(
330
            current_user=None,
331
            session=self.session,
332
            config=self.app_config,
333
        )
334
        groups = [group_api.get_one(Group.TIM_USER),
335
                  group_api.get_one(Group.TIM_MANAGER),
336
                  group_api.get_one(Group.TIM_ADMIN)]
337
338
        user = uapi.create_minimal_user(email='this.is@user',
339
                                        groups=groups, save_now=True)
340
        workspace = WorkspaceApi(
341
            current_user=user,
342
            session=self.session,
343
            config=self.app_config,
344
        ).create_workspace('test workspace', save_now=True)
345
        api = ContentApi(
346
            current_user=user,
347
            session=self.session,
348
            config=self.app_config,
349
        )
350
        folder = api.create(
351
            content_type_slug=CONTENT_TYPES.Folder.slug,
352
            workspace=workspace,
353
            parent=None,
354
            label='plop',
355
            do_save=False
356
        )
357
        allowed_content_type_slug_list = [CONTENT_TYPES.Folder.slug, CONTENT_TYPES.File.slug]  # nopep8
358
        api.set_allowed_content(
359
            folder,
360
            allowed_content_type_slug_list=allowed_content_type_slug_list
361
        )
362
        assert 'allowed_content' in folder.properties
363
        assert folder.properties['allowed_content'] == {CONTENT_TYPES.Folder.slug: True, CONTENT_TYPES.File.slug: True}
364
365
    def test_unit__restore_content_default_allowed_content__ok__nominal_case(self):
366
        uapi = UserApi(
367
            session=self.session,
368
            config=self.app_config,
369
            current_user=None,
370
        )
371
        group_api = GroupApi(
372
            current_user=None,
373
            session=self.session,
374
            config=self.app_config,
375
        )
376
        groups = [group_api.get_one(Group.TIM_USER),
377
                  group_api.get_one(Group.TIM_MANAGER),
378
                  group_api.get_one(Group.TIM_ADMIN)]
379
380
        user = uapi.create_minimal_user(email='this.is@user',
381
                                        groups=groups, save_now=True)
382
        workspace = WorkspaceApi(
383
            current_user=user,
384
            session=self.session,
385
            config=self.app_config,
386
        ).create_workspace('test workspace', save_now=True)
387
        api = ContentApi(
388
            current_user=user,
389
            session=self.session,
390
            config=self.app_config,
391
        )
392
        folder = api.create(
393
            content_type_slug=CONTENT_TYPES.Folder.slug,
394
            workspace=workspace,
395
            parent=None,
396
            label='plop',
397
            do_save=False
398
        )
399
        allowed_content_type_slug_list = [CONTENT_TYPES.Folder.slug, CONTENT_TYPES.File.slug]  # nopep8
400
        api.set_allowed_content(
401
            folder,
402
            allowed_content_type_slug_list=allowed_content_type_slug_list
403
        )
404
        assert 'allowed_content' in folder.properties
405
        assert folder.properties['allowed_content'] == {CONTENT_TYPES.Folder.slug: True, CONTENT_TYPES.File.slug: True} # nopep8
406
        api.restore_content_default_allowed_content(folder)
407
        assert 'allowed_content' in folder.properties
408
        assert folder.properties['allowed_content'] == CONTENT_TYPES.default_allowed_content_properties(folder.type)  # nopep8
409
410
    def test_delete(self):
411
        uapi = UserApi(
412
            session=self.session,
413
            config=self.app_config,
414
            current_user=None,
415
        )
416
        group_api = GroupApi(
417
            current_user=None,
418
            session=self.session,
419
            config=self.app_config,
420
        )
421
        groups = [group_api.get_one(Group.TIM_USER),
422
                  group_api.get_one(Group.TIM_MANAGER),
423
                  group_api.get_one(Group.TIM_ADMIN)]
424
425
        user = uapi.create_minimal_user(email='this.is@user',
426
                                        groups=groups, save_now=True)
427
        workspace = WorkspaceApi(
428
            current_user=user,
429
            session=self.session,
430
            config=self.app_config,
431
        ).create_workspace('test workspace', save_now=True)
432
        api = ContentApi(
433
            current_user=user,
434
            session=self.session,
435
            config=self.app_config,
436
        )
437
        item = api.create(
438
            content_type_slug=CONTENT_TYPES.Folder.slug,
439
            workspace=workspace,
440
            parent=None,
441
            label='not_deleted',
442
            do_save=True
443
        )
444
        item2 = api.create(
445
            content_type_slug=CONTENT_TYPES.Folder.slug,
446
            workspace=workspace,
447
            parent=None,
448
            label='to_delete',
449
            do_save=True
450
        )
451
        uid = user.user_id
452
        wid = workspace.workspace_id
453
        transaction.commit()
454
455
        # Refresh instances after commit
456
        user = uapi.get_one(uid)
457
        workspace_api = WorkspaceApi(
458
            current_user=user,
459
            session=self.session,
460
            config=self.app_config
461
        )
462
        workspace = workspace_api.get_one(wid)
463
        api = ContentApi(
464
            current_user=user,
465
            session=self.session,
466
            config=self.app_config,
467
        )
468
        items = api.get_all(None, CONTENT_TYPES.Any_SLUG, workspace)
469
        eq_(2, len(items))
470
471
        items = api.get_all(None, CONTENT_TYPES.Any_SLUG, workspace)
472
        with new_revision(
473
                session=self.session,
474
                tm=transaction.manager,
475
                content=items[0]
476
        ):
477
            api.delete(items[0])
478
        transaction.commit()
479
480
        # Refresh instances after commit
481
        user = uapi.get_one(uid)
482
        workspace_api = WorkspaceApi(
483
            current_user=user,
484
            session=self.session,
485
            config=self.app_config
486
        )
487
        workspace = workspace_api.get_one(wid)
488
        api = ContentApi(
489
            current_user=user, 
490
            session=self.session,
491
            config=self.app_config,
492
        )
493
        items = api.get_all(None, CONTENT_TYPES.Any_SLUG, workspace)
494
        eq_(1, len(items))
495
        transaction.commit()
496
497
        # Test that the item is still available if "show deleted" is activated
498
        # Refresh instances after commit
499
        user = uapi.get_one(uid)
500
        workspace_api = WorkspaceApi(
501
            current_user=user,
502
            session=self.session,
503
            config=self.app_config,
504
        )
505
        api = ContentApi(
506
            current_user=user,
507
            session=self.session,
508
            config=self.app_config,
509
            show_deleted=True,
510
        )
511
        items = api.get_all(None, CONTENT_TYPES.Any_SLUG, workspace)
512
        eq_(2, len(items))
513
514
    def test_archive(self):
515
        uapi = UserApi(
516
            session=self.session,
517
            config=self.app_config,
518
            current_user=None,
519
        )
520
        group_api = GroupApi(
521
            current_user=None,
522
            session=self.session,
523
            config=self.app_config,
524
        )
525
        groups = [group_api.get_one(Group.TIM_USER),
526
                  group_api.get_one(Group.TIM_MANAGER),
527
                  group_api.get_one(Group.TIM_ADMIN)]
528
529
        user = uapi.create_minimal_user(
530
            email='this.is@user',
531
            groups=groups,
532
            save_now=True,
533
        )
534
        workspace_api = WorkspaceApi(
535
            current_user=user,
536
            session=self.session,
537
            config=self.app_config,
538
        )
539
        workspace = workspace_api.create_workspace(
540
            'test workspace',
541
            save_now=True
542
        )
543
        api = ContentApi(
544
            current_user=user,
545
            session=self.session,
546
            config=self.app_config,
547
        )
548
        item = api.create(
549
            content_type_slug=CONTENT_TYPES.Folder.slug,
550
            workspace=workspace,
551
            parent=None,
552
            label='not_archived',
553
            do_save=True
554
        )
555
        item2 = api.create(
556
            content_type_slug=CONTENT_TYPES.Folder.slug,
557
            workspace=workspace,
558
            parent=None,
559
            label='to_archive',
560
            do_save=True
561
        )
562
        uid = user.user_id
563
        wid = workspace.workspace_id
564
        transaction.commit()
565
        # Refresh instances after commit
566
        user = uapi.get_one(uid)
567
        workspace_api = WorkspaceApi(
568
            current_user=user,
569
            session=self.session,
570
            config=self.app_config,
571
        )
572
        api = ContentApi(
573
            session=self.session,
574
            current_user=user,
575
            config=self.app_config,
576
        )
577
578
        items = api.get_all(None, CONTENT_TYPES.Any_SLUG, workspace)
579
        eq_(2, len(items))
580
581
        items = api.get_all(None, CONTENT_TYPES.Any_SLUG, workspace)
582
        with new_revision(
583
                session=self.session,
584
                tm=transaction.manager,
585
                content=items[0],
586
        ):
587
            api.archive(items[0])
588
        transaction.commit()
589
590
        # Refresh instances after commit
591
        user = uapi.get_one(uid)
592
        workspace_api = WorkspaceApi(
593
            current_user=user,
594
            session=self.session,
595
            config=self.app_config,
596
        )
597
        workspace = workspace_api.get_one(wid)
598
        api = ContentApi(
599
            current_user=user, 
600
            session=self.session,
601
            config=self.app_config,
602
        )
603
604
        items = api.get_all(None, CONTENT_TYPES.Any_SLUG, workspace)
605
        eq_(1, len(items))
606
        transaction.commit()
607
608
        # Refresh instances after commit
609
        user = uapi.get_one(uid)
610
        workspace_api = WorkspaceApi(
611
            current_user=user,
612
            session=self.session,
613
            config=self.app_config,
614
        )
615
        workspace = workspace_api.get_one(wid)
616
        api = ContentApi(
617
            current_user=user,
618
            session=self.session,
619
            config=self.app_config,
620
        )
621
622
        # Test that the item is still available if "show deleted" is activated
623
        api = ContentApi(
624
            current_user=None,
625
            session=self.session,
626
            config=self.app_config,
627
            show_archived=True,
628
        )
629
        items = api.get_all(None, CONTENT_TYPES.Any_SLUG, workspace)
630
        eq_(2, len(items))
631
632
    def test_get_all_with_filter(self):
633
        uapi = UserApi(
634
            session=self.session,
635
            config=self.app_config,
636
            current_user=None,
637
        )
638
        group_api = GroupApi(
639
            current_user=None,
640
            session=self.session,
641
            config=self.app_config,
642
        )
643
        groups = [group_api.get_one(Group.TIM_USER),
644
                  group_api.get_one(Group.TIM_MANAGER),
645
                  group_api.get_one(Group.TIM_ADMIN)]
646
647
        user = uapi.create_minimal_user(
648
            email='this.is@user',
649
            groups=groups,
650
            save_now=True
651
        )
652
        workspace = WorkspaceApi(
653
            current_user=user,
654
            session=self.session,
655
            config=self.app_config,
656
        ).create_workspace(
657
            'test workspace',
658
            save_now=True
659
        )
660
661
        api = ContentApi(
662
            current_user=user,
663
            session=self.session,
664
            config=self.app_config,
665
        )
666
        item = api.create(
667
            content_type_slug=CONTENT_TYPES.Folder.slug,
668
            workspace=workspace,
669
            parent=None,
670
            label='thefolder',
671
            do_save=True
672
        )
673
        item2 = api.create(
674
            content_type_slug=CONTENT_TYPES.File.slug,
675
            workspace=workspace,
676
            parent=None,
677
            label='thefile',
678
            do_save=True
679
        )
680
        uid = user.user_id
681
        wid = workspace.workspace_id
682
        transaction.commit()
683
684
        # Refresh instances after commit
685
        user = uapi.get_one(uid)
686
        workspace_api = WorkspaceApi(
687
            current_user=user,
688
            session=self.session,
689
            config=self.app_config,
690
        )
691
        workspace = workspace_api.get_one(wid)
692
        api = ContentApi(
693
            current_user=user, 
694
            session=self.session,
695
            config=self.app_config,
696
        )
697
698
        items = api.get_all(None, CONTENT_TYPES.Any_SLUG, workspace)
699
        eq_(2, len(items))
700
701
        items2 = api.get_all(None, CONTENT_TYPES.File.slug, workspace)
702
        eq_(1, len(items2))
703
        eq_('thefile', items2[0].label)
704
705
        items3 = api.get_all(None, CONTENT_TYPES.Folder.slug, workspace)
706
        eq_(1, len(items3))
707
        eq_('thefolder', items3[0].label)
708
709
    def test_get_all_with_parent_id(self):
710
        uapi = UserApi(
711
            session=self.session,
712
            config=self.app_config,
713
            current_user=None,
714
        )
715
        group_api = GroupApi(
716
            current_user=None,
717
            session=self.session,
718
            config=self.app_config
719
        )
720
        groups = [group_api.get_one(Group.TIM_USER),
721
                  group_api.get_one(Group.TIM_MANAGER),
722
                  group_api.get_one(Group.TIM_ADMIN)]
723
724
        user = uapi.create_minimal_user(email='this.is@user',
725
                                        groups=groups, save_now=True)
726
        workspace = WorkspaceApi(
727
            current_user=user,
728
            session=self.session,
729
            config=self.app_config
730
        ).create_workspace('test workspace', save_now=True)
731
        api = ContentApi(
732
            current_user=user, 
733
            session=self.session,
734
            config=self.app_config,
735
        )
736
        item = api.create(
737
            CONTENT_TYPES.Folder.slug,
738
            workspace,
739
            None,
740
            'parent',
741
            do_save=True,
742
        )
743
        item2 = api.create(
744
            CONTENT_TYPES.File.slug,
745
            workspace,
746
            item,
747
            'file1',
748
            do_save=True,
749
        )
750
        item3 = api.create(
751
            CONTENT_TYPES.File.slug,
752
            workspace,
753
            None,
754
            'file2',
755
            do_save=True,
756
        )
757
        parent_id = item.content_id
758
        child_id = item2.content_id
759
        uid = user.user_id
760
        wid = workspace.workspace_id
761
        transaction.commit()
762
763
        # Refresh instances after commit
764
        user = uapi.get_one(uid)
765
        workspace_api = WorkspaceApi(
766
            current_user=user,
767
            session=self.session,
768
            config=self.app_config,
769
        )
770
        workspace = workspace_api.get_one(wid)
771
        api = ContentApi(
772
            current_user=user,
773
            session=self.session,
774
            config=self.app_config,
775
        )
776
777
        items = api.get_all(None, CONTENT_TYPES.Any_SLUG, workspace)
778
        eq_(3, len(items))
779
780
        items2 = api.get_all(parent_id, CONTENT_TYPES.File.slug, workspace)
781
        eq_(1, len(items2))
782
        eq_(child_id, items2[0].content_id)
783
784
    def test_set_status_unknown_status(self):
785
        uapi = UserApi(
786
            session=self.session,
787
            config=self.app_config,
788
            current_user=None,
789
        )
790
        group_api = GroupApi(
791
            current_user=None,
792
            session=self.session,
793
            config=self.app_config,
794
        )
795
        groups = [group_api.get_one(Group.TIM_USER),
796
                  group_api.get_one(Group.TIM_MANAGER),
797
                  group_api.get_one(Group.TIM_ADMIN)]
798
799
        user = uapi.create_minimal_user(email='this.is@user',
800
                                        groups=groups, save_now=True)
801
802
        workspace = WorkspaceApi(
803
            current_user=user,
804
            session=self.session,
805
            config=self.app_config,
806
        ).create_workspace(
807
            'test workspace',
808
            save_now=True
809
        )
810
        api = ContentApi(
811
            current_user=user, 
812
            session=self.session,
813
            config=self.app_config,
814
        )
815
        c = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'parent', '', True)
816
        with new_revision(
817
            session=self.session,
818
            tm=transaction.manager,
819
            content=c,
820
        ):
821
            with pytest.raises(ValueError):
822
                api.set_status(c, 'unknown-status')
823
824
    def test_set_status_ok(self):
825
        uapi = UserApi(
826
            session=self.session,
827
            config=self.app_config,
828
            current_user=None,
829
        )
830
        group_api = GroupApi(
831
            current_user=None,
832
            session=self.session,
833
            config=self.app_config,
834
        )
835
        groups = [group_api.get_one(Group.TIM_USER),
836
                  group_api.get_one(Group.TIM_MANAGER),
837
                  group_api.get_one(Group.TIM_ADMIN)]
838
839
        user = uapi.create_minimal_user(email='this.is@user',
840
                                        groups=groups, save_now=True)
841
842
        workspace = WorkspaceApi(
843
            current_user=user,
844
            session=self.session,
845
            config=self.app_config,
846
        ).create_workspace(
847
            'test workspace',
848
            save_now=True
849
        )
850
        api = ContentApi(
851
            current_user=user,
852
            session=self.session,
853
            config=self.app_config,
854
        )
855
        c = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'parent', '', True)
856
        with new_revision(
857
            session=self.session,
858
            tm=transaction.manager,
859
            content=c,
860
        ):
861
            for new_status in ['open', 'closed-validated', 'closed-unvalidated',
862
                               'closed-deprecated']:
863
                api.set_status(c, new_status)
864
865
                eq_(new_status, c.status)
866
                eq_(ActionDescription.STATUS_UPDATE, c.revision_type)
867
868
    def test_create_comment_ok(self):
869
        uapi = UserApi(
870
            session=self.session,
871
            config=self.app_config,
872
            current_user=None,
873
        )
874
        group_api = GroupApi(
875
            current_user=None,
876
            session=self.session,
877
            config=self.config,
878
        )
879
        groups = [group_api.get_one(Group.TIM_USER),
880
                  group_api.get_one(Group.TIM_MANAGER),
881
                  group_api.get_one(Group.TIM_ADMIN)]
882
883
        user = uapi.create_minimal_user(email='this.is@user',
884
                                        groups=groups, save_now=True)
885
886
        workspace = WorkspaceApi(
887
            current_user=user,
888
            session=self.session,
889
            config=self.app_config,
890
        ).create_workspace(
891
            'test workspace',
892
            save_now=True
893
        )
894
895
        api = ContentApi(
896
            current_user=user,
897
            session=self.session,
898
            config=self.app_config,
899
        )
900
        p = api.create(CONTENT_TYPES.Page.slug, workspace, None, 'this_is_a_page')
901
        c = api.create_comment(workspace, p, 'this is the comment', True)
902
903
        eq_(Content, c.__class__)
904
        eq_(p.content_id, c.parent_id)
905
        eq_(user, c.owner)
906
        eq_(workspace, c.workspace)
907
        eq_(CONTENT_TYPES.Comment.slug, c.type)
908
        eq_('this is the comment', c.description)
909
        eq_('', c.label)
910
        eq_(ActionDescription.COMMENT, c.revision_type)
911
912 View Code Duplication
    def test_unit_copy_file_different_label_different_parent_ok(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
913
        uapi = UserApi(
914
            session=self.session,
915
            config=self.app_config,
916
            current_user=None,
917
        )
918
        group_api = GroupApi(
919
            current_user=None,
920
            session=self.session,
921
            config=self.app_config
922
        )
923
        groups = [group_api.get_one(Group.TIM_USER),
924
                  group_api.get_one(Group.TIM_MANAGER),
925
                  group_api.get_one(Group.TIM_ADMIN)]
926
927
        user = uapi.create_minimal_user(
928
            email='user1@user',
929
            groups=groups,
930
            save_now=True
931
        )
932
        user2 = uapi.create_minimal_user(
933
            email='user2@user',
934
            groups=groups,
935
            save_now=True
936
        )
937
        workspace = WorkspaceApi(
938
            current_user=user,
939
            session=self.session,
940
            config=self.app_config,
941
        ).create_workspace(
942
            'test workspace',
943
            save_now=True
944
        )
945
        RoleApi(
946
            current_user=user,
947
            session=self.session,
948
            config=self.app_config,
949
        ).create_one(
950
            user2,
951
            workspace,
952
            UserRoleInWorkspace.WORKSPACE_MANAGER,
953
            with_notif=False
954
        )
955
        api = ContentApi(
956
            current_user=user,
957
            session=self.session,
958
            config=self.app_config,
959
        )
960
        foldera = api.create(
961
            CONTENT_TYPES.Folder.slug,
962
            workspace,
963
            None,
964
            'folder a',
965
            '',
966
            True
967
        )
968
        with self.session.no_autoflush:
969
            text_file = api.create(
970
                content_type_slug=CONTENT_TYPES.File.slug,
971
                workspace=workspace,
972
                parent=foldera,
973
                label='test_file',
974
                do_save=False,
975
            )
976
            api.update_file_data(
977
                text_file,
978
                'test_file',
979
                'text/plain',
980
                b'test_content'
981
            )
982
983
        api.save(text_file, ActionDescription.CREATION)
984
        api2 = ContentApi(
985
            current_user=user2,
986
            session=self.session,
987
            config=self.app_config,
988
        )
989
        workspace2 = WorkspaceApi(
990
            current_user=user2,
991
            session=self.session,
992
            config=self.app_config,
993
        ).create_workspace(
994
            'test workspace2',
995
            save_now=True
996
        )
997
        folderb = api2.create(
998
            CONTENT_TYPES.Folder.slug,
999
            workspace2,
1000
            None,
1001
            'folder b',
1002
            '',
1003
            True
1004
        )
1005
1006
        api2.copy(
1007
            item=text_file,
1008
            new_parent=folderb,
1009
            new_label='test_file_copy'
1010
        )
1011
1012
        transaction.commit()
1013
        text_file_copy = api2.get_one_by_label_and_parent(
1014
            'test_file_copy',
1015
            folderb,
1016
        )
1017
1018
        assert text_file != text_file_copy
1019
        assert text_file_copy.content_id != text_file.content_id
1020
        assert text_file_copy.workspace_id == workspace2.workspace_id
1021
        assert text_file_copy.depot_file.file.read() == text_file.depot_file.file.read()   # nopep8
1022
        assert text_file_copy.depot_file.path != text_file.depot_file.path
1023
        assert text_file_copy.label == 'test_file_copy'
1024
        assert text_file_copy.type == text_file.type
1025
        assert text_file_copy.parent.content_id == folderb.content_id
1026
        assert text_file_copy.owner.user_id == user.user_id
1027
        assert text_file_copy.description == text_file.description
1028
        assert text_file_copy.file_extension == text_file.file_extension
1029
        assert text_file_copy.file_mimetype == text_file.file_mimetype
1030
        assert text_file_copy.revision_type == ActionDescription.COPY
1031
        assert len(text_file_copy.revisions) == len(text_file.revisions) + 1
1032
1033 View Code Duplication
    def test_unit_copy_file__same_label_different_parent_ok(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1034
        uapi = UserApi(
1035
            session=self.session,
1036
            config=self.app_config,
1037
            current_user=None,
1038
        )
1039
        group_api = GroupApi(
1040
            current_user=None,
1041
            session=self.session,
1042
            config=self.app_config,
1043
        )
1044
        groups = [group_api.get_one(Group.TIM_USER),
1045
                  group_api.get_one(Group.TIM_MANAGER),
1046
                  group_api.get_one(Group.TIM_ADMIN)]
1047
1048
        user = uapi.create_minimal_user(
1049
            email='user1@user',
1050
            groups=groups,
1051
            save_now=True
1052
        )
1053
        user2 = uapi.create_minimal_user(
1054
            email='user2@user',
1055
            groups=groups,
1056
            save_now=True
1057
        )
1058
        workspace = WorkspaceApi(
1059
            current_user=user,
1060
            session=self.session,
1061
            config=self.app_config,
1062
        ).create_workspace(
1063
            'test workspace',
1064
            save_now=True
1065
        )
1066
        RoleApi(
1067
            current_user=user,
1068
            session=self.session,
1069
            config=self.app_config,
1070
        ).create_one(
1071
            user2,
1072
            workspace,
1073
            UserRoleInWorkspace.WORKSPACE_MANAGER,
1074
            with_notif=False
1075
        )
1076
        api = ContentApi(
1077
            current_user=user,
1078
            session=self.session,
1079
            config=self.app_config,
1080
        )
1081
        foldera = api.create(
1082
            CONTENT_TYPES.Folder.slug,
1083
            workspace,
1084
            None,
1085
            'folder a',
1086
            '',
1087
            True
1088
        )
1089
        with self.session.no_autoflush:
1090
            text_file = api.create(
1091
                content_type_slug=CONTENT_TYPES.File.slug,
1092
                workspace=workspace,
1093
                parent=foldera,
1094
                label='test_file',
1095
                do_save=False,
1096
            )
1097
            api.update_file_data(
1098
                text_file,
1099
                'test_file',
1100
                'text/plain',
1101
                b'test_content'
1102
            )
1103
1104
        api.save(text_file, ActionDescription.CREATION)
1105
        api2 = ContentApi(
1106
            current_user=user2,
1107
            session=self.session,
1108
            config=self.app_config,
1109
        )
1110
        workspace2 = WorkspaceApi(
1111
            current_user=user2,
1112
            session=self.session,
1113
            config=self.app_config,
1114
        ).create_workspace(
1115
            'test workspace2',
1116
            save_now=True
1117
        )
1118
        folderb = api2.create(
1119
            CONTENT_TYPES.Folder.slug,
1120
            workspace2,
1121
            None,
1122
            'folder b',
1123
            '',
1124
            True
1125
        )
1126
        api2.copy(
1127
            item=text_file,
1128
            new_parent=folderb,
1129
        )
1130
1131
        transaction.commit()
1132
        text_file_copy = api2.get_one_by_label_and_parent(
1133
            'test_file',
1134
            folderb,
1135
        )
1136
1137
        assert text_file != text_file_copy
1138
        assert text_file_copy.content_id != text_file.content_id
1139
        assert text_file_copy.workspace_id == workspace2.workspace_id
1140
        assert text_file_copy.depot_file.file.read() == text_file.depot_file.file.read()  # nopep8
1141
        assert text_file_copy.depot_file.path != text_file.depot_file.path
1142
        assert text_file_copy.label == text_file.label
1143
        assert text_file_copy.type == text_file.type
1144
        assert text_file_copy.parent.content_id == folderb.content_id
1145
        assert text_file_copy.owner.user_id == user.user_id
1146
        assert text_file_copy.description == text_file.description
1147
        assert text_file_copy.file_extension == text_file.file_extension
1148
        assert text_file_copy.file_mimetype == text_file.file_mimetype
1149
        assert text_file_copy.revision_type == ActionDescription.COPY
1150
        assert len(text_file_copy.revisions) == len(text_file.revisions) + 1
1151
1152
    def test_unit_copy_file_different_label_same_parent_ok(self):
1153
        uapi = UserApi(
1154
            session=self.session,
1155
            config=self.app_config,
1156
            current_user=None,
1157
        )
1158
        group_api = GroupApi(
1159
            current_user=None,
1160
            session=self.session,
1161
            config=self.app_config,
1162
        )
1163
        groups = [group_api.get_one(Group.TIM_USER),
1164
                  group_api.get_one(Group.TIM_MANAGER),
1165
                  group_api.get_one(Group.TIM_ADMIN)]
1166
1167
        user = uapi.create_minimal_user(
1168
            email='user1@user',
1169
            groups=groups,
1170
            save_now=True,
1171
        )
1172
        user2 = uapi.create_minimal_user(
1173
            email='user2@user',
1174
            groups=groups,
1175
            save_now=True
1176
        )
1177
        workspace = WorkspaceApi(
1178
            current_user=user,
1179
            session=self.session,
1180
            config=self.app_config,
1181
        ).create_workspace(
1182
            'test workspace',
1183
            save_now=True
1184
        )
1185
        RoleApi(
1186
            current_user=user,
1187
            session=self.session,
1188
            config=self.app_config,
1189
        ).create_one(
1190
            user2, workspace,
1191
            UserRoleInWorkspace.WORKSPACE_MANAGER,
1192
            with_notif=False
1193
        )
1194
        api = ContentApi(
1195
            current_user=user,
1196
            session=self.session,
1197
            config=self.app_config,
1198
        )
1199
        foldera = api.create(
1200
            CONTENT_TYPES.Folder.slug,
1201
            workspace,
1202
            None,
1203
            'folder a',
1204
            '',
1205
            True
1206
        )
1207
        with self.session.no_autoflush:
1208
            text_file = api.create(
1209
                content_type_slug=CONTENT_TYPES.File.slug,
1210
                workspace=workspace,
1211
                parent=foldera,
1212
                label='test_file',
1213
                do_save=False,
1214
            )
1215
            api.update_file_data(
1216
                text_file,
1217
                'test_file',
1218
                'text/plain',
1219
                b'test_content'
1220
            )
1221
1222
        api.save(
1223
            text_file,
1224
            ActionDescription.CREATION
1225
        )
1226
        api2 = ContentApi(
1227
            current_user=user2,
1228
            session=self.session,
1229
            config=self.app_config,
1230
        )
1231
1232
        api2.copy(
1233
            item=text_file,
1234
            new_label='test_file_copy'
1235
        )
1236
1237
        transaction.commit()
1238
        text_file_copy = api2.get_one_by_label_and_parent(
1239
            'test_file_copy',
1240
            foldera,
1241
        )
1242
1243
        assert text_file != text_file_copy
1244
        assert text_file_copy.content_id != text_file.content_id
1245
        assert text_file_copy.workspace_id == workspace.workspace_id
1246
        assert text_file_copy.depot_file.file.read() == text_file.depot_file.file.read()  # nopep8
1247
        assert text_file_copy.depot_file.path != text_file.depot_file.path
1248
        assert text_file_copy.label == 'test_file_copy'
1249
        assert text_file_copy.type == text_file.type
1250
        assert text_file_copy.parent.content_id == foldera.content_id
1251
        assert text_file_copy.owner.user_id == user.user_id
1252
        assert text_file_copy.description == text_file.description
1253
        assert text_file_copy.file_extension == text_file.file_extension
1254
        assert text_file_copy.file_mimetype == text_file.file_mimetype
1255
        assert text_file_copy.revision_type == ActionDescription.COPY
1256
        assert len(text_file_copy.revisions) == len(text_file.revisions) + 1
1257
1258
    def test_mark_read__workspace(self):
1259
        uapi = UserApi(
1260
            session=self.session,
1261
            config=self.app_config,
1262
            current_user=None,
1263
        )
1264
        group_api = GroupApi(
1265
            current_user=None,
1266
            session=self.session,
1267
            config=self.app_config,
1268
        )
1269
        groups = [group_api.get_one(Group.TIM_USER),
1270
                  group_api.get_one(Group.TIM_MANAGER),
1271
                  group_api.get_one(Group.TIM_ADMIN)]
1272
1273
        user_a = uapi.create_minimal_user(email='this.is@user',
1274
                                          groups=groups, save_now=True)
1275
        user_b = uapi.create_minimal_user(email='[email protected]',
1276
                                          groups=groups, save_now=True)
1277
1278
        wapi = WorkspaceApi(
1279
            current_user=user_a,
1280
            session=self.session,
1281
            config=self.app_config,
1282
        )
1283
        workspace1 = wapi.create_workspace(
1284
            'test workspace n°1',
1285
            save_now=True)
1286
        workspace2 = wapi.create_workspace(
1287
            'test workspace n°2',
1288
            save_now=True)
1289
1290
        role_api1 = RoleApi(
1291
            current_user=user_a,
1292
            session=self.session,
1293
            config=self.app_config,
1294
        )
1295
        role_api1.create_one(
1296
            user_b,
1297
            workspace1,
1298
            UserRoleInWorkspace.READER,
1299
            False
1300
        )
1301
1302
        role_api2 = RoleApi(
1303
            current_user=user_b,
1304
            session=self.session,
1305
            config=self.app_config,
1306
        )
1307
        role_api2.create_one(user_b, workspace2, UserRoleInWorkspace.READER,
1308
                             False)
1309
1310
        cont_api_a = ContentApi(
1311
            current_user=user_a,
1312
            session=self.session,
1313
            config=self.app_config,
1314
        )
1315
        cont_api_b = ContentApi(
1316
            current_user=user_b,
1317
            session=self.session,
1318
            config=self.app_config,
1319
        )
1320
1321
        # Creates page_1 & page_2 in workspace 1
1322
        #     and page_3 & page_4 in workspace 2
1323
        page_1 = cont_api_a.create(CONTENT_TYPES.Page.slug, workspace1, None,
1324
                                   'this is a page', do_save=True)
1325
        page_2 = cont_api_a.create(CONTENT_TYPES.Page.slug, workspace1, None,
1326
                                   'this is page1', do_save=True)
1327
        page_3 = cont_api_a.create(CONTENT_TYPES.Thread.slug, workspace2, None,
1328
                                   'this is page2', do_save=True)
1329
        page_4 = cont_api_a.create(CONTENT_TYPES.File.slug, workspace2, None,
1330
                                   'this is page3', do_save=True)
1331
1332
        for rev in page_1.revisions:
1333
            eq_(user_b not in rev.read_by.keys(), True)
1334
        for rev in page_2.revisions:
1335
            eq_(user_b not in rev.read_by.keys(), True)
1336
        for rev in page_3.revisions:
1337
            eq_(user_b not in rev.read_by.keys(), True)
1338
        for rev in page_4.revisions:
1339
            eq_(user_b not in rev.read_by.keys(), True)
1340
1341
        # Set as read the workspace n°1
1342
        cont_api_b.mark_read__workspace(workspace=workspace1)
1343
1344
        for rev in page_1.revisions:
1345
            eq_(user_b in rev.read_by.keys(), True)
1346
        for rev in page_2.revisions:
1347
            eq_(user_b in rev.read_by.keys(), True)
1348
        for rev in page_3.revisions:
1349
            eq_(user_b not in rev.read_by.keys(), True)
1350
        for rev in page_4.revisions:
1351
            eq_(user_b not in rev.read_by.keys(), True)
1352
1353
        # Set as read the workspace n°2
1354
        cont_api_b.mark_read__workspace(workspace=workspace2)
1355
1356
        for rev in page_1.revisions:
1357
            eq_(user_b in rev.read_by.keys(), True)
1358
        for rev in page_2.revisions:
1359
            eq_(user_b in rev.read_by.keys(), True)
1360
        for rev in page_3.revisions:
1361
            eq_(user_b in rev.read_by.keys(), True)
1362
        for rev in page_4.revisions:
1363
            eq_(user_b in rev.read_by.keys(), True)
1364
1365
    def test_mark_read(self):
1366
        uapi = UserApi(
1367
            session=self.session,
1368
            config=self.app_config,
1369
            current_user=None,
1370
        )
1371
        group_api = GroupApi(
1372
            current_user=None,
1373
            session=self.session,
1374
            config = self.app_config,
1375
        )
1376
        groups = [group_api.get_one(Group.TIM_USER),
1377
                  group_api.get_one(Group.TIM_MANAGER),
1378
                  group_api.get_one(Group.TIM_ADMIN)]
1379
1380
        user_a = uapi.create_minimal_user(
1381
            email='this.is@user',
1382
            groups=groups,
1383
            save_now=True
1384
        )
1385
        user_b = uapi.create_minimal_user(
1386
            email='[email protected]',
1387
            groups=groups,
1388
            save_now=True
1389
        )
1390
1391
        wapi = WorkspaceApi(
1392
            current_user=user_a,
1393
            session=self.session,
1394
            config=self.app_config,
1395
        )
1396
        workspace_api = WorkspaceApi(
1397
            current_user=user_a,
1398
            session=self.session,
1399
            config=self.app_config,
1400
        )
1401
        workspace = wapi.create_workspace(
1402
            'test workspace',
1403
            save_now=True)
1404
1405
        role_api = RoleApi(
1406
            current_user=user_a,
1407
            session=self.session,
1408
            config=self.app_config,
1409
        )
1410
        role_api.create_one(
1411
            user_b,
1412
            workspace,
1413
            UserRoleInWorkspace.READER,
1414
            False
1415
        )
1416
        cont_api_a = ContentApi(
1417
            current_user=user_a,
1418
            session=self.session,
1419
            config=self.app_config,
1420
        )
1421
        cont_api_b = ContentApi(
1422
            current_user=user_b,
1423
            session=self.session,
1424
            config=self.app_config,
1425
        )
1426
1427
        page_1 = cont_api_a.create(CONTENT_TYPES.Page.slug, workspace, None,
1428
                                   'this is a page', do_save=True)
1429
1430
        for rev in page_1.revisions:
1431
            eq_(user_b not in rev.read_by.keys(), True)
1432
1433
        cont_api_b.mark_read(page_1)
1434
1435
        for rev in page_1.revisions:
1436
            eq_(user_b in rev.read_by.keys(), True)
1437
1438
    def test_mark_read__all(self):
1439
        uapi = UserApi(
1440
            session=self.session,
1441
            config=self.app_config,
1442
            current_user=None,
1443
        )
1444
        group_api = GroupApi(
1445
            current_user=None,
1446
            session=self.session,
1447
            config=self.app_config,
1448
        )
1449
        groups = [group_api.get_one(Group.TIM_USER),
1450
                  group_api.get_one(Group.TIM_MANAGER),
1451
                  group_api.get_one(Group.TIM_ADMIN)]
1452
1453
        user_a = uapi.create_minimal_user(
1454
            email='this.is@user',
1455
            groups=groups,
1456
            save_now=True
1457
        )
1458
        user_b = uapi.create_minimal_user(
1459
            email='[email protected]',
1460
            groups=groups,
1461
            save_now=True
1462
        )
1463
1464
        wapi = WorkspaceApi(
1465
            current_user=user_a,
1466
            session=self.session,
1467
            config=self.app_config,
1468
        )
1469
        workspace = wapi.create_workspace(
1470
            'test workspace',
1471
            save_now=True)
1472
1473
        role_api = RoleApi(
1474
            current_user=user_a,
1475
            session=self.session,
1476
            config=self.app_config,
1477
        )
1478
        role_api.create_one(
1479
            user_b,
1480
            workspace,
1481
            UserRoleInWorkspace.READER,
1482
            False
1483
        )
1484
        cont_api_a = ContentApi(
1485
            current_user=user_a,
1486
            session=self.session,
1487
            config=self.app_config,
1488
        )
1489
        cont_api_b = ContentApi(
1490
            current_user=user_b,
1491
            session=self.session,
1492
            config=self.app_config,
1493
        )
1494
1495
        page_2 = cont_api_a.create(
1496
            CONTENT_TYPES.Page.slug,
1497
            workspace,
1498
            None,
1499
            'this is page1',
1500
            do_save=True
1501
        )
1502
        page_3 = cont_api_a.create(
1503
            CONTENT_TYPES.Thread.slug,
1504
            workspace,
1505
            None,
1506
            'this is page2',
1507
            do_save=True
1508
        )
1509
        page_4 = cont_api_a.create(
1510
            CONTENT_TYPES.File.slug,
1511
            workspace,
1512
            None,
1513
            'this is page3',
1514
            do_save=True
1515
        )
1516
1517
        for rev in page_2.revisions:
1518
            eq_(user_b not in rev.read_by.keys(), True)
1519
        for rev in page_3.revisions:
1520
            eq_(user_b not in rev.read_by.keys(), True)
1521
        for rev in page_4.revisions:
1522
            eq_(user_b not in rev.read_by.keys(), True)
1523
1524
        self.session.refresh(page_2)
1525
        self.session.refresh(page_3)
1526
        self.session.refresh(page_4)
1527
1528
        cont_api_b.mark_read__all()
1529
1530
        for rev in page_2.revisions:
1531
            eq_(user_b in rev.read_by.keys(), True)
1532
        for rev in page_3.revisions:
1533
            eq_(user_b in rev.read_by.keys(), True)
1534
        for rev in page_4.revisions:
1535
            eq_(user_b in rev.read_by.keys(), True)
1536
1537
    def test_update(self):
1538
        uapi = UserApi(
1539
            session=self.session,
1540
            config=self.app_config,
1541
            current_user=None,
1542
        )
1543
        group_api = GroupApi(
1544
            current_user=None,
1545
            session=self.session,
1546
            config=self.app_config,
1547
        )
1548
        groups = [group_api.get_one(Group.TIM_USER),
1549
                  group_api.get_one(Group.TIM_MANAGER),
1550
                  group_api.get_one(Group.TIM_ADMIN)]
1551
1552
        user1 = uapi.create_minimal_user(
1553
            email='this.is@user',
1554
            groups=groups,
1555
            save_now=True
1556
        )
1557
1558
        workspace_api = WorkspaceApi(
1559
            current_user=user1,
1560
            session=self.session,
1561
            config=self.app_config,
1562
        )
1563
        workspace = workspace_api.create_workspace(
1564
            'test workspace',
1565
            save_now=True
1566
        )
1567
        
1568
        wid = workspace.workspace_id
1569
1570
        user2 = uapi.create_minimal_user('[email protected]')
1571
        uapi.save(user2)
1572
1573
        RoleApi(
1574
            current_user=user1,
1575
            session=self.session,
1576
            config=self.app_config,
1577
        ).create_one(
1578
            user2,
1579
            workspace,
1580
            UserRoleInWorkspace.CONTENT_MANAGER,
1581
            with_notif=False,
1582
            flush=True
1583
        )
1584
1585
        # Test starts here
1586
1587
        api = ContentApi(
1588
            current_user=user1,
1589
            session=self.session,
1590
            config=self.app_config,
1591
        )
1592
1593
        p = api.create(
1594
            content_type_slug=CONTENT_TYPES.Page.slug,
1595
            workspace=workspace,
1596
            parent=None,
1597
            label='this_is_a_page',
1598
            do_save=True
1599
        )
1600
1601
        u1id = user1.user_id
1602
        u2id = user2.user_id
1603
        pcid = p.content_id
1604
        poid = p.owner_id
1605
1606
        transaction.commit()
1607
1608
        # Refresh instances after commit
1609
        user1 = uapi.get_one(u1id)
1610
        workspace = WorkspaceApi(
1611
            current_user=user1,
1612
            session=self.session,
1613
            config=self.app_config,
1614
        ).get_one(wid)
1615
        api = ContentApi(
1616
            current_user=user1,
1617
            session=self.session,
1618
            config=self.app_config,
1619
        )
1620
1621
        content = api.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
1622
        eq_(u1id, content.owner_id)
1623
        eq_(poid, content.owner_id)
1624
1625
        u2 = UserApi(
1626
            session=self.session,
1627
            config=self.app_config,
1628
            current_user=None,
1629
        ).get_one(u2id)
1630
        api2 = ContentApi(
1631
            current_user=u2,
1632
            session=self.session,
1633
            config=self.app_config,
1634
        )
1635
        content2 = api2.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
1636
        with new_revision(
1637
           session=self.session,
1638
           tm=transaction.manager,
1639
           content=content2,
1640
        ):
1641
            api2.update_content(
1642
                content2,
1643
                'this is an updated page',
1644
                'new content'
1645
            )
1646
        api2.save(content2)
1647
        transaction.commit()
1648
1649
        # Refresh instances after commit
1650
        user1 = uapi.get_one(u1id)
1651
        workspace = WorkspaceApi(
1652
            current_user=user1,
1653
            session=self.session,
1654
            config=self.app_config,
1655
        ).get_one(wid)
1656
        api = ContentApi(
1657
            current_user=user1,
1658
            session=self.session,
1659
            config=self.app_config,
1660
        )
1661
1662
        updated = api.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
1663
        eq_(u2id, updated.owner_id,
1664
            'the owner id should be {} (found {})'.format(u2id,
1665
                                                          updated.owner_id))
1666
        eq_('this is an updated page', updated.label)
1667
        eq_('new content', updated.description)
1668
        eq_(ActionDescription.EDITION, updated.revision_type)
1669
1670
    def test_update_no_change(self):
1671
        uapi = UserApi(
1672
            session=self.session,
1673
            config=self.app_config,
1674
            current_user=None,
1675
        )
1676
        group_api = GroupApi(
1677
            current_user=None,
1678
            session=self.session,
1679
            config = self.app_config,
1680
        )
1681
        groups = [group_api.get_one(Group.TIM_USER),
1682
                  group_api.get_one(Group.TIM_MANAGER),
1683
                  group_api.get_one(Group.TIM_ADMIN)]
1684
1685
        user1 = uapi.create_minimal_user(
1686
            email='this.is@user',
1687
            groups=groups,
1688
            save_now=True,
1689
        )
1690
1691
        workspace = WorkspaceApi(
1692
            current_user=user1,
1693
            session=self.session,
1694
            config=self.app_config,
1695
        ).create_workspace(
1696
            'test workspace',
1697
            save_now=True
1698
        )
1699
1700
        user2 = uapi.create_minimal_user('[email protected]')
1701
        uapi.save(user2)
1702
1703
        RoleApi(
1704
            current_user=user1,
1705
            session=self.session,
1706
            config=self.app_config,
1707
        ).create_one(
1708
            user2,
1709
            workspace,
1710
            UserRoleInWorkspace.CONTENT_MANAGER,
1711
            with_notif=False,
1712
            flush=True
1713
        )
1714
        api = ContentApi(
1715
            current_user=user1,
1716
            session=self.session,
1717
            config=self.app_config,
1718
        )
1719
        with self.session.no_autoflush:
1720
            page = api.create(
1721
                content_type_slug=CONTENT_TYPES.Page.slug,
1722
                workspace=workspace,
1723
                label="same_content",
1724
                do_save=False
1725
            )
1726
            page.description = "Same_content_here"
1727
        api.save(page, ActionDescription.CREATION, do_notify=True)
1728
        transaction.commit()
1729
1730
        api2 = ContentApi(
1731
            current_user=user2,
1732
            session=self.session,
1733
            config=self.app_config,
1734
        )
1735
        content2 = api2.get_one(page.content_id, CONTENT_TYPES.Any_SLUG, workspace)
1736
        with new_revision(
1737
           session=self.session,
1738
           tm=transaction.manager,
1739
           content=content2,
1740
        ):
1741
            with pytest.raises(SameValueError):
1742
                api2.update_content(
1743
                    item=content2,
1744
                    new_label='same_content',
1745
                    new_content='Same_content_here'
1746
                )
1747
        api2.save(content2)
1748
        transaction.commit()
1749
1750
    def test_update_file_data(self):
1751
        uapi = UserApi(
1752
            session=self.session,
1753
            config=self.app_config,
1754
            current_user=None,
1755
        )
1756
        group_api = GroupApi(
1757
            current_user=None,
1758
            session=self.session,
1759
            config=self.app_config,
1760
        )
1761
        groups = [group_api.get_one(Group.TIM_USER),
1762
                  group_api.get_one(Group.TIM_MANAGER),
1763
                  group_api.get_one(Group.TIM_ADMIN)]
1764
1765
        user1 = uapi.create_minimal_user(
1766
            email='this.is@user',
1767
            groups=groups,
1768
            save_now=True
1769
        )
1770
1771
        workspace_api = WorkspaceApi(
1772
            current_user=user1,
1773
            session=self.session,
1774
            config=self.app_config,
1775
        )
1776
        workspace = workspace_api.create_workspace(
1777
            'test workspace',
1778
            save_now=True
1779
        )
1780
        wid = workspace.workspace_id
1781
1782
        user2 = uapi.create_minimal_user('[email protected]')
1783
        uapi.save(user2)
1784
1785
        RoleApi(
1786
            current_user=user1,
1787
            session=self.session,
1788
            config=self.app_config,
1789
        ).create_one(
1790
            user2,
1791
            workspace,
1792
            UserRoleInWorkspace.CONTENT_MANAGER,
1793
            with_notif=True,
1794
            flush=True
1795
        )
1796
1797
        # Test starts here
1798
        api = ContentApi(
1799
            current_user=user1,
1800
            session=self.session,
1801
            config=self.app_config,
1802
        )
1803
        p = api.create(
1804
            content_type_slug=CONTENT_TYPES.File.slug,
1805
            workspace=workspace,
1806
            parent=None,
1807
            label='this_is_a_page',
1808
            do_save=True
1809
        )
1810
1811
        u1id = user1.user_id
1812
        u2id = user2.user_id
1813
        pcid = p.content_id
1814
        poid = p.owner_id
1815
1816
        api.save(p)
1817
        transaction.commit()
1818
1819
        # Refresh instances after commit
1820
        user1 = uapi.get_one(u1id)
1821
        workspace_api2 = WorkspaceApi(
1822
            current_user=user1,
1823
            session=self.session,
1824
            config=self.app_config,
1825
        )
1826
        workspace = workspace_api2.get_one(wid)
1827
        api = ContentApi(
1828
            current_user=user1,
1829
            session=self.session,
1830
            config=self.app_config,
1831
        )
1832
1833
        content = api.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
1834
        eq_(u1id, content.owner_id)
1835
        eq_(poid, content.owner_id)
1836
1837
        u2 = UserApi(
1838
            current_user=None,
1839
            session=self.session,
1840
            config=self.app_config,
1841
        ).get_one(u2id)
1842
        api2 = ContentApi(
1843
            current_user=u2,
1844
            session=self.session,
1845
            config=self.app_config,
1846
        )
1847
        content2 = api2.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
1848
        with new_revision(
1849
            session=self.session,
1850
            tm=transaction.manager,
1851
            content=content2,
1852
        ):
1853
            api2.update_file_data(
1854
                content2,
1855
                'index.html',
1856
                'text/html',
1857
                b'<html>hello world</html>'
1858
            )
1859
        api2.save(content2)
1860
        transaction.commit()
1861
1862
        # Refresh instances after commit
1863
        user1 = uapi.get_one(u1id)
1864
        workspace = WorkspaceApi(
1865
            current_user=user1,
1866
            session=self.session,
1867
            config=self.app_config,
1868
        ).get_one(wid)
1869
1870
        updated = api.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
1871
        eq_(u2id, updated.owner_id,
1872
            'the owner id should be {} (found {})'.format(u2id,
1873
                                                          updated.owner_id))
1874
        eq_('this_is_a_page.html', updated.file_name)
1875
        eq_('text/html', updated.file_mimetype)
1876
        eq_(b'<html>hello world</html>', updated.depot_file.file.read())
1877
        eq_(ActionDescription.REVISION, updated.revision_type)
1878
1879
    def test_update_no_change(self):
1880
        uapi = UserApi(
1881
            session=self.session,
1882
            config=self.app_config,
1883
            current_user=None,
1884
        )
1885
        group_api = GroupApi(
1886
            current_user=None,
1887
            session=self.session,
1888
            config=self.app_config,
1889
        )
1890
        groups = [group_api.get_one(Group.TIM_USER),
1891
                  group_api.get_one(Group.TIM_MANAGER),
1892
                  group_api.get_one(Group.TIM_ADMIN)]
1893
1894
        user1 = uapi.create_minimal_user(
1895
            email='this.is@user',
1896
            groups=groups,
1897
            save_now=True,
1898
        )
1899
1900
        workspace_api = WorkspaceApi(
1901
            current_user=user1,
1902
            session=self.session,
1903
            config=self.app_config,
1904
        )
1905
        workspace = workspace_api.create_workspace(
1906
            'test workspace',
1907
            save_now=True
1908
        )
1909
1910
        user2 = uapi.create_minimal_user('[email protected]')
1911
        uapi.save(user2)
1912
1913
        RoleApi(
1914
            current_user=user1,
1915
            session=self.session,
1916
            config=self.app_config,
1917
        ).create_one(
1918
            user2,
1919
            workspace,
1920
            UserRoleInWorkspace.CONTENT_MANAGER,
1921
            with_notif=False,
1922
            flush=True
1923
        )
1924
        api = ContentApi(
1925
            current_user=user1,
1926
            session=self.session,
1927
            config=self.app_config,
1928
        )
1929
        with self.session.no_autoflush:
1930
            page = api.create(
1931
                content_type_slug=CONTENT_TYPES.Page.slug,
1932
                workspace=workspace,
1933
                label="same_content",
1934
                do_save=False
1935
            )
1936
            api.update_file_data(
1937
                page,
1938
                'index.html',
1939
                'text/html',
1940
                b'<html>Same Content Here</html>'
1941
            )
1942
        api.save(page, ActionDescription.CREATION, do_notify=True)
1943
        transaction.commit()
1944
1945
        api2 = ContentApi(
1946
            current_user=user2,
1947
            session=self.session,
1948
            config=self.app_config,
1949
        )
1950
        content2 = api2.get_one(page.content_id, CONTENT_TYPES.Any_SLUG, workspace)
1951
        with new_revision(
1952
            session=self.session,
1953
            tm=transaction.manager,
1954
            content=content2,
1955
        ):
1956
            with pytest.raises(SameValueError):
1957
                api2.update_file_data(
1958
                    page,
1959
                    'index.html',
1960
                    'text/html',
1961
                    b'<html>Same Content Here</html>'
1962
                )
1963
        api2.save(content2)
1964
        transaction.commit()
1965
1966 View Code Duplication
    def test_archive_unarchive(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1967
        uapi = UserApi(
1968
            session=self.session,
1969
            config=self.app_config,
1970
            current_user=None,
1971
        )
1972
        group_api = GroupApi(
1973
            current_user=None,
1974
            session=self.session,
1975
            config=self.app_config,
1976
        )
1977
        groups = [group_api.get_one(Group.TIM_USER),
1978
                  group_api.get_one(Group.TIM_MANAGER),
1979
                  group_api.get_one(Group.TIM_ADMIN)]
1980
1981
        user1 = uapi.create_minimal_user(
1982
            email='this.is@user',
1983
            groups=groups,
1984
            save_now=True
1985
        )
1986
        u1id = user1.user_id
1987
1988
        workspace_api = WorkspaceApi(
1989
            current_user=user1,
1990
            session=self.session,
1991
            config=self.app_config,
1992
        )
1993
        workspace = workspace_api.create_workspace(
1994
            'test workspace',
1995
            save_now=True
1996
        )
1997
        wid = workspace.workspace_id
1998
1999
        user2 = uapi.create_minimal_user('[email protected]')
2000
        uapi.save(user2)
2001
2002
        RoleApi(
2003
            current_user=user1,
2004
            session=self.session,
2005
            config=self.app_config,
2006
        ).create_one(
2007
            user2,
2008
            workspace,
2009
            UserRoleInWorkspace.CONTENT_MANAGER,
2010
            with_notif=True,
2011
            flush=True
2012
        )
2013
2014
        # show archived is used at the top end of the test
2015
        api = ContentApi(
2016
            current_user=user1,
2017
            session=self.session,
2018
            show_archived=True,
2019
            config=self.app_config,
2020
        )
2021
        p = api.create(
2022
            content_type_slug=CONTENT_TYPES.File.slug,
2023
            workspace=workspace,
2024
            parent=None,
2025
            label='this_is_a_page',
2026
            do_save=True
2027
        )
2028
2029
        u1id = user1.user_id
2030
        u2id = user2.user_id
2031
        pcid = p.content_id
2032
        poid = p.owner_id
2033
2034
        transaction.commit()
2035
2036
        ####
2037
2038
        # refresh after commit
2039
        user1 = UserApi(
2040
            current_user=None,
2041
            config=self.app_config,
2042
            session=self.session
2043
        ).get_one(u1id)
2044
        workspace = WorkspaceApi(
2045
            current_user=user1,
2046
            session=self.session,
2047
            config=self.app_config,
2048
        ).get_one(wid)
2049
2050
        content = api.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
2051
        eq_(u1id, content.owner_id)
2052
        eq_(poid, content.owner_id)
2053
2054
        u2api = UserApi(
2055
            session=self.session,
2056
            config=self.app_config,
2057
            current_user=None,
2058
        )
2059
        u2 = u2api.get_one(u2id)
2060
        api2 = ContentApi(
2061
            current_user=u2,
2062
            session=self.session,
2063
            config=self.app_config,
2064
            show_archived=True,
2065
        )
2066
        content2 = api2.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
2067
        with new_revision(
2068
                session=self.session,
2069
                tm=transaction.manager,
2070
                content=content2,
2071
        ):
2072
            api2.archive(content2)
2073
        api2.save(content2)
2074
        transaction.commit()
2075
2076
        # refresh after commit
2077
        user1 = UserApi(
2078
            current_user=None,
2079
            session=self.session,
2080
            config=self.app_config,
2081
        ).get_one(u1id)
2082
        workspace = WorkspaceApi(
2083
            current_user=user1,
2084
            session=self.session,
2085
            config=self.app_config,
2086
        ).get_one(wid)
2087
        u2 = UserApi(
2088
            current_user=None,
2089
            session=self.session,
2090
            config=self.app_config,
2091
        ).get_one(u2id)
2092
        api = ContentApi(
2093
            current_user=user1,
2094
            session=self.session,
2095
            config=self.app_config,
2096
            show_archived=True,
2097
        )
2098
        api2 = ContentApi(
2099
            current_user=u2,
2100
            session=self.session,
2101
            config=self.app_config,
2102
            show_archived=True,
2103
        )
2104
2105
        updated = api2.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
2106
        eq_(u2id, updated.owner_id,
2107
            'the owner id should be {} (found {})'.format(u2id,
2108
                                                          updated.owner_id))
2109
        eq_(True, updated.is_archived)
2110
        eq_(ActionDescription.ARCHIVING, updated.revision_type)
2111
2112
        ####
2113
2114
        updated2 = api.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
2115
        with new_revision(
2116
            session=self.session,
2117
            tm=transaction.manager,
2118
            content=updated,
2119
2120
        ):
2121
            api.unarchive(updated)
2122
        api.save(updated2)
2123
        eq_(False, updated2.is_archived)
2124
        eq_(ActionDescription.UNARCHIVING, updated2.revision_type)
2125
        eq_(u1id, updated2.owner_id)
2126
2127 View Code Duplication
    def test_delete_undelete(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2128
        uapi = UserApi(
2129
            session=self.session,
2130
            config=self.app_config,
2131
            current_user=None,
2132
        )
2133
        group_api = GroupApi(
2134
            current_user=None,
2135
            session=self.session,
2136
            config=self.app_config,
2137
        )
2138
        groups = [group_api.get_one(Group.TIM_USER),
2139
                  group_api.get_one(Group.TIM_MANAGER),
2140
                  group_api.get_one(Group.TIM_ADMIN)]
2141
2142
        user1 = uapi.create_minimal_user(
2143
            email='this.is@user',
2144
            groups=groups,
2145
            save_now=True
2146
        )
2147
        u1id = user1.user_id
2148
2149
        workspace_api = WorkspaceApi(
2150
            current_user=user1,
2151
            session=self.session,
2152
            config=self.app_config,
2153
        )
2154
        workspace = workspace_api.create_workspace(
2155
            'test workspace',
2156
            save_now=True
2157
        )
2158
        wid = workspace.workspace_id
2159
2160
        user2 = uapi.create_minimal_user('[email protected]')
2161
        uapi.save(user2)
2162
2163
        RoleApi(
2164
            current_user=user1,
2165
            session=self.session,
2166
            config=self.app_config,
2167
        ).create_one(
2168
            user2,
2169
            workspace,
2170
            UserRoleInWorkspace.CONTENT_MANAGER,
2171
            with_notif=True,
2172
            flush=True
2173
        )
2174
2175
        # show archived is used at the top end of the test
2176
        api = ContentApi(
2177
            current_user=user1,
2178
            session=self.session,
2179
            config=self.app_config,
2180
            show_deleted=True,
2181
        )
2182
        p = api.create(
2183
            content_type_slug=CONTENT_TYPES.File.slug,
2184
            workspace=workspace,
2185
            parent=None,
2186
            label='this_is_a_page',
2187
            do_save=True
2188
        )
2189
2190
        u1id = user1.user_id
2191
        u2id = user2.user_id
2192
        pcid = p.content_id
2193
        poid = p.owner_id
2194
2195
        transaction.commit()
2196
2197
        ####
2198
        user1 = UserApi(
2199
            current_user=None,
2200
            session=self.session,
2201
            config=self.app_config,
2202
        ).get_one(u1id)
2203
        workspace = WorkspaceApi(
2204
            current_user=user1,
2205
            session=self.session,
2206
            config=self.app_config,
2207
        ).get_one(wid)
2208
2209
        content = api.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
2210
        eq_(u1id, content.owner_id)
2211
        eq_(poid, content.owner_id)
2212
2213
        u2 = UserApi(
2214
            current_user=None,
2215
            session=self.session,
2216
            config=self.app_config,
2217
        ).get_one(u2id)
2218
        api2 = ContentApi(
2219
            current_user=u2,
2220
            session=self.session,
2221
            config=self.app_config,
2222
            show_deleted=True,
2223
        )
2224
        content2 = api2.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
2225
        with new_revision(
2226
                session=self.session,
2227
                tm=transaction.manager,
2228
                content=content2,
2229
        ):
2230
            api2.delete(content2)
2231
        api2.save(content2)
2232
        transaction.commit()
2233
2234
        ####
2235
2236
        user1 = UserApi(
2237
            current_user=None,
2238
            session=self.session,
2239
            config=self.app_config,
2240
        ).get_one(u1id)
2241
        workspace = WorkspaceApi(
2242
            current_user=user1,
2243
            session=self.session,
2244
            config=self.app_config,
2245
        ).get_one(wid)
2246
        # show archived is used at the top end of the test
2247
        api = ContentApi(
2248
            current_user=user1,
2249
            session=self.session,
2250
            config=self.app_config,
2251
            show_deleted=True,
2252
        )
2253
        u2 = UserApi(
2254
            current_user=None,
2255
            session=self.session,
2256
            config=self.app_config,
2257
        ).get_one(u2id)
2258
        api2 = ContentApi(
2259
            current_user=u2,
2260
            session=self.session,
2261
            config=self.app_config,
2262
            show_deleted=True
2263
        )
2264
2265
        updated = api2.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
2266
        eq_(u2id, updated.owner_id,
2267
            'the owner id should be {} (found {})'.format(u2id,
2268
                                                          updated.owner_id))
2269
        eq_(True, updated.is_deleted)
2270
        eq_(ActionDescription.DELETION, updated.revision_type)
2271
2272
        ####
2273
2274
        updated2 = api.get_one(pcid, CONTENT_TYPES.Any_SLUG, workspace)
2275
        with new_revision(
2276
            tm=transaction.manager,
2277
            session=self.session,
2278
            content=updated2,
2279
        ):
2280
            api.undelete(updated2)
2281
        api.save(updated2)
2282
        eq_(False, updated2.is_deleted)
2283
        eq_(ActionDescription.UNDELETION, updated2.revision_type)
2284
        eq_(u1id, updated2.owner_id)
2285
2286
    def test_unit__get_last_active__ok__nominal_case(self):
2287
        uapi = UserApi(
2288
            session=self.session,
2289
            config=self.app_config,
2290
            current_user=None,
2291
        )
2292
        group_api = GroupApi(
2293
            current_user=None,
2294
            session=self.session,
2295
            config=self.app_config,
2296
        )
2297
        groups = [group_api.get_one(Group.TIM_USER),
2298
                  group_api.get_one(Group.TIM_MANAGER),
2299
                  group_api.get_one(Group.TIM_ADMIN)]
2300
2301
        user = uapi.create_minimal_user(email='this.is@user',
2302
                                        groups=groups, save_now=True)
2303
        workspace = WorkspaceApi(
2304
            current_user=user,
2305
            session=self.session,
2306
            config=self.app_config,
2307
        ).create_workspace(
2308
            'test workspace',
2309
            save_now=True
2310
        )
2311
        workspace2 = WorkspaceApi(
2312
            current_user=user,
2313
            session=self.session,
2314
            config=self.app_config,
2315
        ).create_workspace(
2316
            'test workspace2',
2317
            save_now=True
2318
        )
2319
2320
        api = ContentApi(
2321
            current_user=user,
2322
            session=self.session,
2323
            config=self.app_config,
2324
        )
2325
        main_folder_workspace2 = api.create(CONTENT_TYPES.Folder.slug, workspace2, None, 'Hepla', '', True)  # nopep8
2326
        main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
2327
        # creation order test
2328
        firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
2329
        secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
2330
        # update order test
2331
        firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
2332
        secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
2333
        with new_revision(
2334
            session=self.session,
2335
            tm=transaction.manager,
2336
            content=firstly_created_but_recently_updated,
2337
        ):
2338
            firstly_created_but_recently_updated.description = 'Just an update'
2339
        api.save(firstly_created_but_recently_updated)
2340
        # comment change order
2341
        firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
2342
        secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
2343
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
2344
2345
        content_workspace_2 = api.create(CONTENT_TYPES.Page.slug, workspace2 ,main_folder_workspace2, 'content_workspace_2', '',True)  # nopep8
2346
        last_actives = api.get_last_active()
2347
        assert len(last_actives) == 9
2348
        # workspace_2 content
2349
        assert last_actives[0] == content_workspace_2
2350
        # comment is newest than page2
2351
        assert last_actives[1] == firstly_created_but_recently_commented
2352
        assert last_actives[2] == secondly_created_but_not_commented
2353
        # last updated content is newer than other one despite creation
2354
        # of the other is more recent
2355
        assert last_actives[3] == firstly_created_but_recently_updated
2356
        assert last_actives[4] == secondly_created_but_not_updated
2357
        # creation order is inverted here as last created is last active
2358
        assert last_actives[5] == secondly_created
2359
        assert last_actives[6] == firstly_created
2360
        # folder subcontent modification does not change folder order
2361
        assert last_actives[7] == main_folder
2362
        # folder subcontent modification does not change folder order
2363
        # (workspace2)
2364
        assert last_actives[8] == main_folder_workspace2
2365
2366
    def test_unit__get_last_active__ok__do_no_show_deleted_archived(self):
2367
        uapi = UserApi(
2368
            session=self.session,
2369
            config=self.app_config,
2370
            current_user=None,
2371
        )
2372
        group_api = GroupApi(
2373
            current_user=None,
2374
            session=self.session,
2375
            config=self.app_config,
2376
        )
2377
        groups = [group_api.get_one(Group.TIM_USER),
2378
                  group_api.get_one(Group.TIM_MANAGER),
2379
                  group_api.get_one(Group.TIM_ADMIN)]
2380
2381
        user = uapi.create_minimal_user(email='this.is@user',
2382
                                        groups=groups, save_now=True)
2383
        workspace = WorkspaceApi(
2384
            current_user=user,
2385
            session=self.session,
2386
            config=self.app_config,
2387
        ).create_workspace(
2388
            'test workspace',
2389
            save_now=True
2390
        )
2391
        workspace2 = WorkspaceApi(
2392
            current_user=user,
2393
            session=self.session,
2394
            config=self.app_config,
2395
        ).create_workspace(
2396
            'test workspace2',
2397
            save_now=True
2398
        )
2399
2400
        api = ContentApi(
2401
            current_user=user,
2402
            session=self.session,
2403
            config=self.app_config,
2404
            show_deleted=False,
2405
            show_archived=False,
2406
        )
2407
        main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
2408
        archived = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'archived', '', True)  # nopep8
2409
        deleted = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'deleted', '', True)  # nopep8
2410
        comment_archived = api.create_comment(workspace, parent=archived, content='just a comment', do_save=True)  # nopep8
2411
        comment_deleted = api.create_comment(workspace, parent=deleted, content='just a comment', do_save=True)  # nopep8
2412
        with new_revision(
2413
            session=self.session,
2414
            tm=transaction.manager,
2415
            content=archived,
2416
        ):
2417
            api.archive(archived)
2418
            api.save(archived)
2419
2420
        with new_revision(
2421
            session=self.session,
2422
            tm=transaction.manager,
2423
            content=deleted,
2424
        ):
2425
            api.delete(deleted)
2426
            api.save(deleted)
2427
        normal = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'normal', '', True)  # nopep8
2428
        comment_normal = api.create_comment(workspace, parent=normal, content='just a comment', do_save=True)  # nopep8
2429
2430
        last_actives = api.get_last_active()
2431
        assert len(last_actives) == 2
2432
        assert last_actives[0].content_id == normal.content_id
2433
        assert last_actives[1].content_id == main_folder.content_id
2434
2435
2436
        api._show_deleted = True
2437
        api._show_archived = False
2438
        last_actives = api.get_last_active()
2439
        assert len(last_actives) == 3
2440
        assert last_actives[0] == normal
2441
        assert last_actives[1] == deleted
2442
        assert last_actives[2] == main_folder
2443
2444
        api._show_deleted = False
2445
        api._show_archived = True
2446
        last_actives = api.get_last_active()
2447
        assert len(last_actives) == 3
2448
        assert last_actives[0]== normal
2449
        assert last_actives[1] == archived
2450
        assert last_actives[2] == main_folder
2451
2452
        api._show_deleted = True
2453
        api._show_archived = True
2454
        last_actives = api.get_last_active()
2455
        assert len(last_actives) == 4
2456
        assert last_actives[0] == normal
2457
        assert last_actives[1] == deleted
2458
        assert last_actives[2] == archived
2459
        assert last_actives[3] == main_folder
2460
2461
    def test_unit__get_last_active__ok__workspace_filter_workspace_full(self):
2462
        uapi = UserApi(
2463
            session=self.session,
2464
            config=self.app_config,
2465
            current_user=None,
2466
        )
2467
        group_api = GroupApi(
2468
            current_user=None,
2469
            session=self.session,
2470
            config=self.app_config,
2471
        )
2472
        groups = [group_api.get_one(Group.TIM_USER),
2473
                  group_api.get_one(Group.TIM_MANAGER),
2474
                  group_api.get_one(Group.TIM_ADMIN)]
2475
2476
        user = uapi.create_minimal_user(email='this.is@user',
2477
                                        groups=groups, save_now=True)
2478
        workspace = WorkspaceApi(
2479
            current_user=user,
2480
            session=self.session,
2481
            config=self.app_config,
2482
        ).create_workspace(
2483
            'test workspace',
2484
            save_now=True
2485
        )
2486
2487
        api = ContentApi(
2488
            current_user=user,
2489
            session=self.session,
2490
            config=self.app_config,
2491
        )
2492
        main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
2493
        # creation order test
2494
        firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
2495
        secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
2496
        # update order test
2497
        firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
2498
        secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
2499
        with new_revision(
2500
            session=self.session,
2501
            tm=transaction.manager,
2502
            content=firstly_created_but_recently_updated,
2503
        ):
2504
            firstly_created_but_recently_updated.description = 'Just an update'
2505
        api.save(firstly_created_but_recently_updated)
2506
        # comment change order
2507
        firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
2508
        secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
2509
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
2510
2511
        last_actives = api.get_last_active(workspace=workspace)
2512
        assert len(last_actives) == 7
2513
        # comment is newest than page2
2514
        assert last_actives[0] == firstly_created_but_recently_commented
2515
        assert last_actives[1] == secondly_created_but_not_commented
2516
        # last updated content is newer than other one despite creation
2517
        # of the other is more recent
2518
        assert last_actives[2] == firstly_created_but_recently_updated
2519
        assert last_actives[3] == secondly_created_but_not_updated
2520
        # creation order is inverted here as last created is last active
2521
        assert last_actives[4] == secondly_created
2522
        assert last_actives[5] == firstly_created
2523
        # folder subcontent modification does not change folder order
2524
        assert last_actives[6] == main_folder
2525
2526
    def test_unit__get_last_active__ok__workspace_filter_workspace_content_ids(self):
2527
        uapi = UserApi(
2528
            session=self.session,
2529
            config=self.app_config,
2530
            current_user=None,
2531
        )
2532
        group_api = GroupApi(
2533
            current_user=None,
2534
            session=self.session,
2535
            config=self.app_config,
2536
        )
2537
        groups = [group_api.get_one(Group.TIM_USER),
2538
                  group_api.get_one(Group.TIM_MANAGER),
2539
                  group_api.get_one(Group.TIM_ADMIN)]
2540
2541
        user = uapi.create_minimal_user(email='this.is@user',
2542
                                        groups=groups, save_now=True)
2543
        workspace = WorkspaceApi(
2544
            current_user=user,
2545
            session=self.session,
2546
            config=self.app_config,
2547
        ).create_workspace(
2548
            'test workspace',
2549
            save_now=True
2550
        )
2551
2552
        api = ContentApi(
2553
            current_user=user,
2554
            session=self.session,
2555
            config=self.app_config,
2556
        )
2557
        main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
2558
        # creation order test
2559
        firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
2560
        secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
2561
        # update order test
2562
        firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
2563
        secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
2564
        with new_revision(
2565
            session=self.session,
2566
            tm=transaction.manager,
2567
            content=firstly_created_but_recently_updated,
2568
        ):
2569
            firstly_created_but_recently_updated.description = 'Just an update'
2570
        api.save(firstly_created_but_recently_updated)
2571
        # comment change order
2572
        firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
2573
        secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
2574
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
2575
2576
        selected_contents = [
2577
            firstly_created_but_recently_commented,
2578
            firstly_created_but_recently_updated,
2579
            firstly_created,
2580
            main_folder,
2581
        ]
2582
        content_ids = [content.content_id for content in selected_contents]
2583
        last_actives = api.get_last_active(
2584
            workspace=workspace,
2585
            content_ids=content_ids,
2586
        )
2587
        assert len(last_actives) == 4
2588
        # comment is newest than page2
2589
        assert last_actives[0] == firstly_created_but_recently_commented
2590
        assert secondly_created_but_not_commented not in last_actives
2591
        # last updated content is newer than other one despite creation
2592
        # of the other is more recent
2593
        assert last_actives[1] == firstly_created_but_recently_updated
2594
        assert secondly_created_but_not_updated not in last_actives
2595
        # creation order is inverted here as last created is last active
2596
        assert secondly_created not in last_actives
2597
        assert last_actives[2] == firstly_created
2598
        # folder subcontent modification does not change folder order
2599
        assert last_actives[3] == main_folder
2600
2601
    def test_unit__get_last_active__ok__workspace_filter_workspace_limit_2_multiples_times(self):  # nopep8
2602
        uapi = UserApi(
2603
            session=self.session,
2604
            config=self.app_config,
2605
            current_user=None,
2606
        )
2607
        group_api = GroupApi(
2608
            current_user=None,
2609
            session=self.session,
2610
            config=self.app_config,
2611
        )
2612
        groups = [group_api.get_one(Group.TIM_USER),
2613
                  group_api.get_one(Group.TIM_MANAGER),
2614
                  group_api.get_one(Group.TIM_ADMIN)]
2615
2616
        user = uapi.create_minimal_user(email='this.is@user',
2617
                                        groups=groups, save_now=True)
2618
        workspace = WorkspaceApi(
2619
            current_user=user,
2620
            session=self.session,
2621
            config=self.app_config,
2622
        ).create_workspace(
2623
            'test workspace',
2624
            save_now=True
2625
        )
2626
2627
        api = ContentApi(
2628
            current_user=user,
2629
            session=self.session,
2630
            config=self.app_config,
2631
        )
2632
        main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
2633
        # creation order test
2634
        firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
2635
        secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
2636
        # update order test
2637
        firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
2638
        secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
2639
        with new_revision(
2640
            session=self.session,
2641
            tm=transaction.manager,
2642
            content=firstly_created_but_recently_updated,
2643
        ):
2644
            firstly_created_but_recently_updated.description = 'Just an update'
2645
        api.save(firstly_created_but_recently_updated)
2646
        # comment change order
2647
        firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
2648
        secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
2649
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
2650
2651
        last_actives = api.get_last_active(workspace=workspace, limit=2)  # nopep8
2652
        assert len(last_actives) == 2
2653
        # comment is newest than page2
2654
        assert last_actives[0] == firstly_created_but_recently_commented
2655
        assert last_actives[1] == secondly_created_but_not_commented
2656
2657
        last_actives = api.get_last_active(workspace=workspace, limit=2, before_content=last_actives[1])  # nopep8
2658
        assert len(last_actives) == 2
2659
        # last updated content is newer than other one despite creation
2660
        # of the other is more recent
2661
        assert last_actives[0] == firstly_created_but_recently_updated
2662
        assert last_actives[1] == secondly_created_but_not_updated
2663
2664
        last_actives = api.get_last_active(workspace=workspace, limit=2, before_content=last_actives[1])  # nopep8
2665
        assert len(last_actives) == 2
2666
        # creation order is inverted here as last created is last active
2667
        assert last_actives[0] == secondly_created
2668
        assert last_actives[1] == firstly_created
2669
2670
        last_actives = api.get_last_active(workspace=workspace, limit=2, before_content=last_actives[1])  # nopep8
2671
        assert len(last_actives) == 1
2672
        # folder subcontent modification does not change folder order
2673
        assert last_actives[0] == main_folder
2674
2675
    def test_unit__get_last_active__ok__workspace_filter_workspace_empty(self):
2676
        uapi = UserApi(
2677
            session=self.session,
2678
            config=self.app_config,
2679
            current_user=None,
2680
        )
2681
        group_api = GroupApi(
2682
            current_user=None,
2683
            session=self.session,
2684
            config=self.app_config,
2685
        )
2686
        groups = [group_api.get_one(Group.TIM_USER),
2687
                  group_api.get_one(Group.TIM_MANAGER),
2688
                  group_api.get_one(Group.TIM_ADMIN)]
2689
2690
        user = uapi.create_minimal_user(email='this.is@user',
2691
                                        groups=groups, save_now=True)
2692
        workspace = WorkspaceApi(
2693
            current_user=user,
2694
            session=self.session,
2695
            config=self.app_config,
2696
        ).create_workspace(
2697
            'test workspace',
2698
            save_now=True
2699
        )
2700
        workspace2 = WorkspaceApi(
2701
            current_user=user,
2702
            session=self.session,
2703
            config=self.app_config,
2704
        ).create_workspace(
2705
            'test workspace2',
2706
            save_now=True
2707
        )
2708
        api = ContentApi(
2709
            current_user=user,
2710
            session=self.session,
2711
            config=self.app_config,
2712
        )
2713
        main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
2714
        # creation order test
2715
        firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
2716
        secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
2717
        # update order test
2718
        firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
2719
        secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
2720
        with new_revision(
2721
            session=self.session,
2722
            tm=transaction.manager,
2723
            content=firstly_created_but_recently_updated,
2724
        ):
2725
            firstly_created_but_recently_updated.description = 'Just an update'
2726
        api.save(firstly_created_but_recently_updated)
2727
        # comment change order
2728
        firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
2729
        secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
2730
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
2731
2732
        last_actives = api.get_last_active(workspace=workspace2)
2733
        assert len(last_actives) == 0
2734
2735 View Code Duplication
    def test_search_in_label(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2736
        # HACK - D.A. - 2015-03-09
2737
        # This test is based on a bug which does NOT return results found
2738
        # at root of a workspace (eg a folder)
2739
        uapi = UserApi(
2740
            session=self.session,
2741
            config=self.app_config,
2742
            current_user=None,
2743
        )
2744
        group_api = GroupApi(
2745
            current_user=None,
2746
            session=self.session,
2747
            config=self.app_config,
2748
        )
2749
        groups = [group_api.get_one(Group.TIM_USER),
2750
                  group_api.get_one(Group.TIM_MANAGER),
2751
                  group_api.get_one(Group.TIM_ADMIN)]
2752
2753
        user = uapi.create_minimal_user(email='this.is@user',
2754
                                        groups=groups, save_now=True)
2755
2756
        workspace = WorkspaceApi(
2757
            current_user=user,
2758
            session=self.session,
2759
            config=self.app_config,
2760
        ).create_workspace(
2761
            'test workspace',
2762
            save_now=True
2763
        )
2764
2765
        api = ContentApi(
2766
            current_user=user, 
2767
            session=self.session,
2768
            config=self.app_config,
2769
        )
2770
        a = api.create(CONTENT_TYPES.Folder.slug, workspace, None,
2771
                       'this is randomized folder', '', True)
2772
        p = api.create(CONTENT_TYPES.Page.slug, workspace, a,
2773
                       'this is randomized label content', '', True)
2774
2775
        with new_revision(
2776
            session=self.session,
2777
            tm=transaction.manager,
2778
            content=p,
2779
        ):
2780
            p.description = 'This is some amazing test'
2781
2782
        api.save(p)
2783
        original_id = p.content_id
2784
2785
        res = api.search(['randomized'])
2786
        eq_(1, len(res.all()))
2787
        item = res.all()[0]
2788
        eq_(original_id, item.content_id)
2789
2790 View Code Duplication
    def test_search_in_description(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2791
        # HACK - D.A. - 2015-03-09
2792
        # This test is based on a bug which does NOT return results found
2793
        # at root of a workspace (eg a folder)
2794
2795
        uapi = UserApi(
2796
            session=self.session,
2797
            config=self.app_config,
2798
            current_user=None,
2799
        )
2800
        group_api = GroupApi(
2801
            current_user=None,
2802
            session=self.session,
2803
            config=self.app_config,
2804
        )
2805
        groups = [group_api.get_one(Group.TIM_USER),
2806
                  group_api.get_one(Group.TIM_MANAGER),
2807
                  group_api.get_one(Group.TIM_ADMIN)]
2808
2809
        user = uapi.create_minimal_user(email='this.is@user',
2810
                                        groups=groups, save_now=True)
2811
2812
        workspace = WorkspaceApi(
2813
            current_user=user,
2814
            session=self.session,
2815
            config=self.app_config,
2816
        ).create_workspace(
2817
            'test workspace',
2818
            save_now=True,
2819
        )
2820
2821
        api = ContentApi(
2822
            current_user=user, 
2823
            session=self.session,
2824
            config=self.app_config,
2825
        )
2826
        a = api.create(CONTENT_TYPES.Folder.slug, workspace, None,
2827
                       'this is randomized folder', '', True)
2828
        p = api.create(CONTENT_TYPES.Page.slug, workspace, a,
2829
                       'this is dummy label content', '', True)
2830
2831
        with new_revision(
2832
            tm=transaction.manager,
2833
            session=self.session,
2834
            content=p,
2835
        ):
2836
            p.description = 'This is some amazing test'
2837
2838
        api.save(p)
2839
        original_id = p.content_id
2840
2841
        res = api.search(['dummy'])
2842
        eq_(1, len(res.all()))
2843
        item = res.all()[0]
2844
        eq_(original_id, item.content_id)
2845
2846
    def test_search_in_label_or_description(self):
2847
        # HACK - D.A. - 2015-03-09
2848
        # This test is based on a bug which does NOT return results found
2849
        # at root of a workspace (eg a folder)
2850
2851
        uapi = UserApi(
2852
            session=self.session,
2853
            config=self.app_config,
2854
            current_user=None,
2855
        )
2856
        group_api = GroupApi(
2857
            current_user=None,
2858
            session=self.session,
2859
            config=self.app_config,
2860
        )
2861
        groups = [group_api.get_one(Group.TIM_USER),
2862
                  group_api.get_one(Group.TIM_MANAGER),
2863
                  group_api.get_one(Group.TIM_ADMIN)]
2864
2865
        user = uapi.create_minimal_user(email='this.is@user',
2866
                                        groups=groups, save_now=True)
2867
2868
        workspace = WorkspaceApi(
2869
            current_user=user,
2870
            session=self.session,
2871
            config=self.app_config,
2872
        ).create_workspace('test workspace', save_now=True)
2873
2874
        api = ContentApi(
2875
            current_user=user, 
2876
            session=self.session,
2877
            config=self.app_config,
2878
        )
2879
        a = api.create(
2880
            content_type_slug=CONTENT_TYPES.Folder.slug,
2881
            workspace=workspace,
2882
            parent=None,
2883
            label='this is randomized folder',
2884
            do_save=True
2885
        )
2886
        p1 = api.create(
2887
            content_type_slug=CONTENT_TYPES.Page.slug,
2888
            workspace=workspace,
2889
            parent=a,
2890
            label='this is dummy label content',
2891
            do_save=True
2892
        )
2893
        p2 = api.create(
2894
            content_type_slug=CONTENT_TYPES.Page.slug,
2895
            workspace=workspace,
2896
            parent=a,
2897
            label='Hey ! Jon !',
2898
            do_save=True
2899
        )
2900
2901
        with new_revision(
2902
            session=self.session,
2903
            tm=transaction.manager,
2904
            content=p1,
2905
        ):
2906
            p1.description = 'This is some amazing test'
2907
2908
        with new_revision(
2909
            session=self.session,
2910
            tm=transaction.manager,
2911
            content=p2,
2912
        ):
2913
            p2.description = 'What\'s up?'
2914
2915
        api.save(p1)
2916
        api.save(p2)
2917
2918
        id1 = p1.content_id
2919
        id2 = p2.content_id
2920
2921
        eq_(1, self.session.query(Workspace).filter(Workspace.label == 'test workspace').count())
2922
        eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'this is randomized folder').count())
2923
        eq_(2, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'this is dummy label content').count())
2924
        eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.description == 'This is some amazing test').count())
2925
        eq_(2, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'Hey ! Jon !').count())
2926
        eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.description == 'What\'s up?').count())
2927
2928
        res = api.search(['dummy', 'jon'])
2929
        eq_(2, len(res.all()))
2930
2931
        eq_(True, id1 in [o.content_id for o in res.all()])
2932
        eq_(True, id2 in [o.content_id for o in res.all()])
2933
2934
    def test_unit__search_exclude_content_under_deleted_or_archived_parents__ok(self):  # nopep8
2935
        admin = self.session.query(User)\
2936
            .filter(User.email == '[email protected]').one()
2937
        workspace = self._create_workspace_and_test(
2938
            'workspace_1',
2939
            admin
2940
        )
2941
        folder_1 = self._create_content_and_test(
2942
            'folder_1',
2943
            workspace=workspace,
2944
            type=CONTENT_TYPES.Folder.slug
2945
        )
2946
        folder_2 = self._create_content_and_test(
2947
            'folder_2',
2948
            workspace=workspace,
2949
            type=CONTENT_TYPES.Folder.slug
2950
        )
2951
        page_1 = self._create_content_and_test(
2952
            'foo', workspace=workspace,
2953
            type=CONTENT_TYPES.Page.slug,
2954
            parent=folder_1
2955
        )
2956
        page_2 = self._create_content_and_test(
2957
            'bar',
2958
            workspace=workspace,
2959
            type=CONTENT_TYPES.Page.slug,
2960
            parent=folder_2
2961
        )
2962
2963
        api = ContentApi(
2964
            current_user=admin,
2965
            session=self.session,
2966
            config=self.app_config,
2967
        )
2968
2969
        foo_result = api.search(['foo']).all()
2970
        eq_(1, len(foo_result))
2971
        assert page_1 in foo_result
2972
2973
        bar_result = api.search(['bar']).all()
2974
        eq_(1, len(bar_result))
2975
        assert page_2 in bar_result
2976
2977
        with new_revision(
2978
            session=self.session,
2979
            tm=transaction.manager,
2980
            content=folder_1,
2981
        ):
2982
            api.delete(folder_1)
2983
        with new_revision(
2984
            session=self.session,
2985
            tm=transaction.manager,
2986
            content=folder_2,
2987
        ):
2988
            api.archive(folder_2)
2989
2990
        # Actually ContentApi.search don't filter it
2991
        foo_result = api.search(['foo']).all()
2992
        eq_(1, len(foo_result))
2993
        assert page_1 in foo_result
2994
2995
        bar_result = api.search(['bar']).all()
2996
        eq_(1, len(bar_result))
2997
        assert page_2 in bar_result
2998
2999
        # ContentApi offer exclude_unavailable method to do it
3000
        foo_result = api.search(['foo']).all()
3001
        api.exclude_unavailable(foo_result)
3002
        eq_(0, len(foo_result))
3003
3004
        bar_result = api.search(['bar']).all()
3005
        api.exclude_unavailable(bar_result)
3006
        eq_(0, len(bar_result))
3007
3008
3009
class TestContentApiSecurity(DefaultTest):
3010
    fixtures = [FixtureTest, ]
3011
3012
    def test_unit__cant_get_non_access_content__ok__nominal_case(self):
3013
        admin = self.session.query(User)\
3014
            .filter(User.email == '[email protected]').one()
3015
        bob = self.session.query(User)\
3016
            .filter(User.email == '[email protected]').one()
3017
3018
        bob_workspace = WorkspaceApi(
3019
            current_user=bob,
3020
            session=self.session,
3021
            config=self.app_config,
3022
        ).create_workspace(
3023
            'bob_workspace',
3024
            save_now=True,
3025
        )
3026
        admin_workspace = WorkspaceApi(
3027
            current_user=admin,
3028
            session=self.session,
3029
            config=self.app_config,
3030
        ).create_workspace(
3031
            'admin_workspace',
3032
            save_now=True,
3033
        )
3034
3035
        bob_page = ContentApi(
3036
            current_user=bob,
3037
            session=self.session,
3038
            config=self.app_config,
3039
        ).create(
3040
            content_type_slug=CONTENT_TYPES.Page.slug,
3041
            workspace=bob_workspace,
3042
            label='bob_page',
3043
            do_save=True,
3044
        )
3045
3046
        admin_page = ContentApi(
3047
            current_user=admin,
3048
            session=self.session,
3049
            config=self.app_config,
3050
        ).create(
3051
            content_type_slug=CONTENT_TYPES.Page.slug,
3052
            workspace=admin_workspace,
3053
            label='admin_page',
3054
            do_save=True,
3055
        )
3056
3057
        bob_viewable = ContentApi(
3058
            current_user=bob,
3059
            session=self.session,
3060
            config=self.app_config,
3061
        ).get_all()
3062
        eq_(1, len(bob_viewable), 'Bob should view only one content')
3063
        eq_(
3064
            'bob_page',
3065
            bob_viewable[0].label,
3066
            'Bob should not view "{0}" content'.format(
3067
                bob_viewable[0].label,
3068
            )
3069
        )
3070