TestContentApi.test_archive()   B
last analyzed

Complexity

Conditions 2

Size

Total Lines 117
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 90
dl 0
loc 117
rs 7.3272
c 0
b 0
f 0
cc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# -*- coding: utf-8 -*-
2
import pytest
3
import transaction
4
5
from tracim_backend.app_models.contents import content_status_list
6
from tracim_backend.app_models.contents import content_type_list
7
from tracim_backend.exceptions import ContentInNotEditableState
8
from tracim_backend.exceptions import ContentFilenameAlreadyUsedInFolder
9
from tracim_backend.exceptions import EmptyLabelNotAllowed
10
from tracim_backend.exceptions import SameValueError
11
from tracim_backend.exceptions import UnallowedSubContent
12
from tracim_backend.fixtures.users_and_groups import Test as FixtureTest
13
from tracim_backend.lib.core.content import ContentApi
14
from tracim_backend.lib.core.content import \
15
    compare_content_for_sorting_by_type_and_name  # nopep8
16
# TODO - G.M - 28-03-2018 - [GroupApi] Re-enable GroupApi
17
from tracim_backend.lib.core.group import GroupApi
18
from tracim_backend.lib.core.user import UserApi
19
# TODO - G.M - 28-03-2018 - [WorkspaceApi] Re-enable WorkspaceApi
20
# TODO - G.M - 28-03-2018 - [RoleApi] Re-enable RoleApi
21
from tracim_backend.lib.core.workspace import RoleApi
22
from tracim_backend.lib.core.workspace import WorkspaceApi
23
from tracim_backend.models.auth import Group
24
from tracim_backend.models.auth import User
25
from tracim_backend.models.data import ActionDescription
26
from tracim_backend.models.data import Content
27
from tracim_backend.models.data import ContentRevisionRO
28
from tracim_backend.models.data import UserRoleInWorkspace
29
from tracim_backend.models.data import Workspace
30
from tracim_backend.models.revision_protection import new_revision
31
from tracim_backend.tests import DefaultTest
32
from tracim_backend.tests import eq_
33
34
35
class TestContentApi(DefaultTest):
36
37
    def test_compare_content_for_sorting_by_type(self):
38
        c1 = Content()
39
        c1.label = ''
40
        c1.type = 'file'
41
42
        c2 = Content()
43
        c2.label = ''
44
        c2.type = 'folder'
45
46
        c11 = c1
47
48
        eq_(1, compare_content_for_sorting_by_type_and_name(c1, c2))
49
        eq_(-1, compare_content_for_sorting_by_type_and_name(c2, c1))
50
        eq_(0, compare_content_for_sorting_by_type_and_name(c1, c11))
51
52
    def test_compare_content_for_sorting_by_label(self):
53
        c1 = Content()
54
        c1.label = 'bbb'
55
        c1.type = 'file'
56
57
        c2 = Content()
58
        c2.label = 'aaa'
59
        c2.type = 'file'
60
61
        c11 = c1
62
63
        eq_(1, compare_content_for_sorting_by_type_and_name(c1, c2))
64
        eq_(-1, compare_content_for_sorting_by_type_and_name(c2, c1))
65
        eq_(0, compare_content_for_sorting_by_type_and_name(c1, c11))
66
67
    def test_sort_by_label_or_filename(self):
68
        c1 = Content()
69
        c1.label = 'ABCD'
70
        c1.type = 'file'
71
72
        c2 = Content()
73
        c2.label = ''
74
        c2.type = 'file'
75
        c2.file_name = 'AABC'
76
77
        c3 = Content()
78
        c3.label = 'BCDE'
79
        c3.type = 'file'
80
81
        items = [c1, c2, c3]
82
        sorteds = ContentApi.sort_content(items)
83
84
        eq_(sorteds[0], c2)
85
        eq_(sorteds[1], c1)
86
        eq_(sorteds[2], c3)
87
88
    def test_sort_by_content_type(self):
89
        c1 = Content()
90
        c1.label = 'AAAA'
91
        c1.type = 'file'
92
93
        c2 = Content()
94
        c2.label = 'BBBB'
95
        c2.type = 'folder'
96
97
        items = [c1, c2]
98
        sorteds = ContentApi.sort_content(items)
99
100
        eq_(sorteds[0], c2,
101
            'value is {} instead of {}'.format(sorteds[0].content_id,
102
                                               c2.content_id))
103
        eq_(sorteds[1], c1,
104
            'value is {} instead of {}'.format(sorteds[1].content_id,
105
                                               c1.content_id))
106
107 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...
108
        uapi = UserApi(
109
            session=self.session,
110
            config=self.app_config,
111
            current_user=None,
112
        )
113
        group_api = GroupApi(
114
            current_user=None,
115
            session=self.session,
116
            config=self.app_config,
117
        )
118
        groups = [group_api.get_one(Group.TIM_USER),
119
                  group_api.get_one(Group.TIM_MANAGER),
120
                  group_api.get_one(Group.TIM_ADMIN)]
121
122
        user = uapi.create_minimal_user(email='this.is@user',
123
                                        groups=groups, save_now=True)
124
        workspace = WorkspaceApi(
125
            current_user=user,
126
            session=self.session,
127
            config=self.app_config,
128
        ).create_workspace('test workspace', save_now=True)
129
        api = ContentApi(
130
            current_user=user,
131
            session=self.session,
132
            config=self.app_config,
133
        )
134
        item = api.create(
135
            content_type_slug=content_type_list.Folder.slug,
136
            workspace=workspace,
137
            parent=None,
138
            label='not_deleted',
139
            do_save=True
140
        )
141
        assert isinstance(item, Content)
142
143 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...
144
        uapi = UserApi(
145
            session=self.session,
146
            config=self.app_config,
147
            current_user=None,
148
        )
149
        group_api = GroupApi(
150
            current_user=None,
151
            session=self.session,
152
            config=self.app_config,
153
        )
154
        groups = [group_api.get_one(Group.TIM_USER),
155
                  group_api.get_one(Group.TIM_MANAGER),
156
                  group_api.get_one(Group.TIM_ADMIN)]
157
158
        user = uapi.create_minimal_user(email='this.is@user',
159
                                        groups=groups, save_now=True)
160
        workspace = WorkspaceApi(
161
            current_user=user,
162
            session=self.session,
163
            config=self.app_config,
164
        ).create_workspace('test workspace', save_now=True)
165
        api = ContentApi(
166
            current_user=user,
167
            session=self.session,
168
            config=self.app_config,
169
        )
170
        with pytest.raises(EmptyLabelNotAllowed):
171
            api.create(
172
                content_type_slug=content_type_list.Thread.slug,
173
                workspace=workspace,
174
                parent=None,
175
                label='',
176
                do_save=True
177
            )
178
179
    def test_unit__create_content__err_content_type_not_allowed_in_this_folder(self):
180
        uapi = UserApi(
181
            session=self.session,
182
            config=self.app_config,
183
            current_user=None,
184
        )
185
        group_api = GroupApi(
186
            current_user=None,
187
            session=self.session,
188
            config=self.app_config,
189
        )
190
        groups = [group_api.get_one(Group.TIM_USER),
191
                  group_api.get_one(Group.TIM_MANAGER),
192
                  group_api.get_one(Group.TIM_ADMIN)]
193
194
        user = uapi.create_minimal_user(email='this.is@user',
195
                                        groups=groups, save_now=True)
196
        workspace = WorkspaceApi(
197
            current_user=user,
198
            session=self.session,
199
            config=self.app_config,
200
        ).create_workspace('test workspace', save_now=True)
201
        api = ContentApi(
202
            current_user=user,
203
            session=self.session,
204
            config=self.app_config,
205
        )
206
        folder = api.create(
207
            content_type_slug=content_type_list.Folder.slug,
208
            workspace=workspace,
209
            parent=None,
210
            label='plop',
211
            do_save=False
212
        )
213
        allowed_content_dict = {content_type_list.Folder.slug: True, content_type_list.File.slug: False} # nopep8
214
        api._set_allowed_content(
215
            folder,
216
            allowed_content_dict=allowed_content_dict
217
        )
218
        api.save(content=folder)
219
        # not in list -> do not allow
220
        with pytest.raises(UnallowedSubContent):
221
            api.create(
222
                content_type_slug=content_type_list.Event.slug,
223
                workspace=workspace,
224
                parent=folder,
225
                label='lapin',
226
                do_save=True
227
            )
228
        # in list but false -> do not allow
229
        with pytest.raises(UnallowedSubContent):
230
            api.create(
231
                content_type_slug=content_type_list.File.slug,
232
                workspace=workspace,
233
                parent=folder,
234
                label='lapin',
235
                do_save=True
236
            )
237
        # in list and true -> allow
238
        api.create(
239
            content_type_slug=content_type_list.Folder.slug,
240
            workspace=workspace,
241
            parent=folder,
242
            label='lapin',
243
            do_save=True
244
        )
245
246 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...
247
        uapi = UserApi(
248
            session=self.session,
249
            config=self.app_config,
250
            current_user=None,
251
        )
252
        group_api = GroupApi(
253
            current_user=None,
254
            session=self.session,
255
            config=self.app_config,
256
        )
257
        groups = [group_api.get_one(Group.TIM_USER),
258
                  group_api.get_one(Group.TIM_MANAGER),
259
                  group_api.get_one(Group.TIM_ADMIN)]
260
261
        user = uapi.create_minimal_user(email='this.is@user',
262
                                        groups=groups, save_now=True)
263
        workspace = WorkspaceApi(
264
            current_user=user,
265
            session=self.session,
266
            config=self.app_config,
267
        ).create_workspace('test workspace', save_now=True)
268
        api = ContentApi(
269
            current_user=user,
270
            session=self.session,
271
            config=self.app_config,
272
        )
273
        with pytest.raises(UnallowedSubContent):
274
            api.create(
275
                content_type_slug=content_type_list.Event.slug,
276
                workspace=workspace,
277
                parent=None,
278
                label='lapin',
279
                do_save=True
280
           )
281
282
    def test_unit__create_content__err_same_label_as_another_content(self):
283
        uapi = UserApi(
284
            session=self.session,
285
            config=self.app_config,
286
            current_user=None,
287
        )
288
        group_api = GroupApi(
289
            current_user=None,
290
            session=self.session,
291
            config=self.app_config,
292
        )
293
        groups = [group_api.get_one(Group.TIM_USER),
294
                  group_api.get_one(Group.TIM_MANAGER),
295
                  group_api.get_one(Group.TIM_ADMIN)]
296
297
        user = uapi.create_minimal_user(email='this.is@user',
298
                                        groups=groups, save_now=True)
299
        workspace = WorkspaceApi(
300
            current_user=user,
301
            session=self.session,
302
            config=self.app_config,
303
        ).create_workspace('test workspace', save_now=True)
304
        api = ContentApi(
305
            current_user=user,
306
            session=self.session,
307
            config=self.app_config,
308
        )
309
        api.create(
310
            content_type_slug=content_type_list.Page.slug,
311
            workspace=workspace,
312
            parent=None,
313
            label='file',
314
            do_save=True
315
        )
316
        with pytest.raises(ContentFilenameAlreadyUsedInFolder):
317
            api.create(
318
                content_type_slug=content_type_list.Page.slug,
319
                workspace=workspace,
320
                parent=None,
321
                label='file',
322
                do_save=True
323
            )
324
325
    def test_unit__is_filename_available__ok__nominal_case(self):
326
        uapi = UserApi(
327
            session=self.session,
328
            config=self.app_config,
329
            current_user=None,
330
        )
331
        group_api = GroupApi(
332
            current_user=None,
333
            session=self.session,
334
            config=self.app_config,
335
        )
336
        groups = [group_api.get_one(Group.TIM_USER),
337
                  group_api.get_one(Group.TIM_MANAGER),
338
                  group_api.get_one(Group.TIM_ADMIN)]
339
340
        user = uapi.create_minimal_user(email='this.is@user',
341
                                        groups=groups, save_now=True)
342
        workspace = WorkspaceApi(
343
            current_user=user,
344
            session=self.session,
345
            config=self.app_config,
346
        ).create_workspace('test workspace', save_now=True)
347
        api = ContentApi(
348
            current_user=user,
349
            session=self.session,
350
            config=self.app_config,
351
        )
352
        assert api._is_filename_available('test', workspace, parent=None) == True  # nopep8
353
        content = Content()
354
        content.label = 'test'
355
        content.owner = user
356
        content.parent = None
357
        content.workspace = workspace
358
        content.type = content_type_list.Page.slug
359
        content.revision_type = ActionDescription.CREATION
360
        self.session.add(content)
361
        api.save(content, ActionDescription.CREATION, do_notify=False)
362
        assert api._is_filename_available('test', workspace, parent=None) is False  # nopep8
363
        content = Content()
364
        content.label = 'test'
365
        content.owner = user
366
        content.parent = None
367
        content.workspace = workspace
368
        content.type = content_type_list.Page.slug
369
        content.revision_type = ActionDescription.CREATION
370
        self.session.add(content)
371
        api.save(content, ActionDescription.CREATION, do_notify=False)
372
        assert api._is_filename_available('test', workspace, parent=None) is False  # nopep8
373
374
    def test_unit__is_filename_available__ok__different_workspace(self):
375
        uapi = UserApi(
376
            session=self.session,
377
            config=self.app_config,
378
            current_user=None,
379
        )
380
        group_api = GroupApi(
381
            current_user=None,
382
            session=self.session,
383
            config=self.app_config,
384
        )
385
        groups = [group_api.get_one(Group.TIM_USER),
386
                  group_api.get_one(Group.TIM_MANAGER),
387
                  group_api.get_one(Group.TIM_ADMIN)]
388
389
        user = uapi.create_minimal_user(email='this.is@user',
390
                                        groups=groups, save_now=True)
391
        workspace = WorkspaceApi(
392
            current_user=user,
393
            session=self.session,
394
            config=self.app_config,
395
        ).create_workspace('test workspace', save_now=True)
396
        workspace2 = WorkspaceApi(
397
            current_user=user,
398
            session=self.session,
399
            config=self.app_config,
400
        ).create_workspace('test workspace2', save_now=True)
401
        api = ContentApi(
402
            current_user=user,
403
            session=self.session,
404
            config=self.app_config,
405
        )
406
        assert api._is_filename_available('test', workspace, parent=None) == True  # nopep8
407
        content = Content()
408
        content.label = 'test'
409
        content.owner = user
410
        content.parent = None
411
        content.workspace = workspace2
412
        content.type = content_type_list.Page.slug
413
        content.revision_type = ActionDescription.CREATION
414
        self.session.add(content)
415
        api.save(content, ActionDescription.CREATION, do_notify=False)
416
        assert api._is_filename_available('test', workspace, parent=None) == True  # nopep8
417
418
    def test_unit__is_filename_available__ok__different_parent(self):
419
        uapi = UserApi(
420
            session=self.session,
421
            config=self.app_config,
422
            current_user=None,
423
        )
424
        group_api = GroupApi(
425
            current_user=None,
426
            session=self.session,
427
            config=self.app_config,
428
        )
429
        groups = [group_api.get_one(Group.TIM_USER),
430
                  group_api.get_one(Group.TIM_MANAGER),
431
                  group_api.get_one(Group.TIM_ADMIN)]
432
433
        user = uapi.create_minimal_user(email='this.is@user',
434
                                        groups=groups, save_now=True)
435
        workspace = WorkspaceApi(
436
            current_user=user,
437
            session=self.session,
438
            config=self.app_config,
439
        ).create_workspace('test workspace', save_now=True)
440
        workspace = WorkspaceApi(
441
            current_user=user,
442
            session=self.session,
443
            config=self.app_config,
444
        ).create_workspace('test workspace2', save_now=True)
445
        api = ContentApi(
446
            current_user=user,
447
            session=self.session,
448
            config=self.app_config,
449
        )
450
        folder = Content()
451
        folder.label = 'folder'
452
        folder.owner = user
453
        folder.parent = None
454
        folder.workspace = workspace
455
        folder.type = content_type_list.Folder.slug
456
        folder.revision_type = ActionDescription.CREATION
457
        self.session.add(folder)
458
        folder2 = Content()
459
        folder2.label = 'folder2'
460
        folder2.owner = user
461
        folder2.parent = None
462
        folder2.workspace = workspace
463
        folder2.type = content_type_list.Folder.slug
464
        folder2.revision_type = ActionDescription.CREATION
465
        self.session.add(folder)
466
        assert api._is_filename_available('test', workspace, parent=None) == True  # nopep8
467
        content = Content()
468
        content.label = 'test'
469
        content.owner = user
470
        content.parent = folder
471
        content.workspace = workspace
472
        content.type = content_type_list.Page.slug
473
        content.revision_type = ActionDescription.CREATION
474
        self.session.add(content)
475
        api.save(content, ActionDescription.CREATION, do_notify=False)
476
        assert api._is_filename_available('test', workspace, parent=None) == True  # nopep8
477
        content = Content()
478
        content.label = 'test'
479
        content.owner = user
480
        content.parent = folder2
481
        content.workspace = workspace
482
        content.type = content_type_list.Page.slug
483
        content.revision_type = ActionDescription.CREATION
484
        self.session.add(content)
485
        api.save(content, ActionDescription.CREATION, do_notify=False)
486
        assert api._is_filename_available('test', workspace, parent=None) == True  # nopep8
487
        content = Content()
488
        content.label = 'test'
489
        content.owner = user
490
        content.parent = None
491
        content.workspace = workspace
492
        content.type = content_type_list.Page.slug
493
        content.revision_type = ActionDescription.CREATION
494
        self.session.add(content)
495
        api.save(content, ActionDescription.CREATION, do_notify=False)
496
        assert api._is_filename_available('test', workspace, parent=None) == False  # nopep8
497
498 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...
499
        uapi = UserApi(
500
            session=self.session,
501
            config=self.app_config,
502
            current_user=None,
503
        )
504
        group_api = GroupApi(
505
            current_user=None,
506
            session=self.session,
507
            config=self.app_config,
508
        )
509
        groups = [group_api.get_one(Group.TIM_USER),
510
                  group_api.get_one(Group.TIM_MANAGER),
511
                  group_api.get_one(Group.TIM_ADMIN)]
512
513
        user = uapi.create_minimal_user(email='this.is@user',
514
                                        groups=groups, save_now=True)
515
        workspace = WorkspaceApi(
516
            current_user=user,
517
            session=self.session,
518
            config=self.app_config,
519
        ).create_workspace('test workspace', save_now=True)
520
        api = ContentApi(
521
            current_user=user,
522
            session=self.session,
523
            config=self.app_config,
524
        )
525
        folder = api.create(
526
            content_type_slug=content_type_list.Folder.slug,
527
            workspace=workspace,
528
            parent=None,
529
            label='plop',
530
            do_save=False
531
        )
532
        allowed_content_dict = {content_type_list.Folder.slug: True, content_type_list.File.slug: False}  # nopep8
533
        api._set_allowed_content(
534
            folder,
535
            allowed_content_dict=allowed_content_dict
536
        )
537
        assert 'allowed_content' in folder.properties
538
        assert folder.properties['allowed_content'] == {content_type_list.Folder.slug: True, content_type_list.File.slug: False}
539
540 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...
541
        uapi = UserApi(
542
            session=self.session,
543
            config=self.app_config,
544
            current_user=None,
545
        )
546
        group_api = GroupApi(
547
            current_user=None,
548
            session=self.session,
549
            config=self.app_config,
550
        )
551
        groups = [group_api.get_one(Group.TIM_USER),
552
                  group_api.get_one(Group.TIM_MANAGER),
553
                  group_api.get_one(Group.TIM_ADMIN)]
554
555
        user = uapi.create_minimal_user(email='this.is@user',
556
                                        groups=groups, save_now=True)
557
        workspace = WorkspaceApi(
558
            current_user=user,
559
            session=self.session,
560
            config=self.app_config,
561
        ).create_workspace('test workspace', save_now=True)
562
        api = ContentApi(
563
            current_user=user,
564
            session=self.session,
565
            config=self.app_config,
566
        )
567
        folder = api.create(
568
            content_type_slug=content_type_list.Folder.slug,
569
            workspace=workspace,
570
            parent=None,
571
            label='plop',
572
            do_save=False
573
        )
574
        allowed_content_type_slug_list = [content_type_list.Folder.slug, content_type_list.File.slug]  # nopep8
575
        api.set_allowed_content(
576
            folder,
577
            allowed_content_type_slug_list=allowed_content_type_slug_list
578
        )
579
        assert 'allowed_content' in folder.properties
580
        assert folder.properties['allowed_content'] == {content_type_list.Folder.slug: True, content_type_list.File.slug: True}
581
582
    def test_unit__restore_content_default_allowed_content__ok__nominal_case(self):
583
        uapi = UserApi(
584
            session=self.session,
585
            config=self.app_config,
586
            current_user=None,
587
        )
588
        group_api = GroupApi(
589
            current_user=None,
590
            session=self.session,
591
            config=self.app_config,
592
        )
593
        groups = [group_api.get_one(Group.TIM_USER),
594
                  group_api.get_one(Group.TIM_MANAGER),
595
                  group_api.get_one(Group.TIM_ADMIN)]
596
597
        user = uapi.create_minimal_user(email='this.is@user',
598
                                        groups=groups, save_now=True)
599
        workspace = WorkspaceApi(
600
            current_user=user,
601
            session=self.session,
602
            config=self.app_config,
603
        ).create_workspace('test workspace', save_now=True)
604
        api = ContentApi(
605
            current_user=user,
606
            session=self.session,
607
            config=self.app_config,
608
        )
609
        folder = api.create(
610
            content_type_slug=content_type_list.Folder.slug,
611
            workspace=workspace,
612
            parent=None,
613
            label='plop',
614
            do_save=False
615
        )
616
        allowed_content_type_slug_list = [content_type_list.Folder.slug, content_type_list.File.slug]  # nopep8
617
        api.set_allowed_content(
618
            folder,
619
            allowed_content_type_slug_list=allowed_content_type_slug_list
620
        )
621
        assert 'allowed_content' in folder.properties
622
        assert folder.properties['allowed_content'] == {content_type_list.Folder.slug: True, content_type_list.File.slug: True} # nopep8
623
        api.restore_content_default_allowed_content(folder)
624
        assert 'allowed_content' in folder.properties
625
        assert folder.properties['allowed_content'] == content_type_list.default_allowed_content_properties(folder.type)  # nopep8
626
627
    def test_delete(self):
628
        uapi = UserApi(
629
            session=self.session,
630
            config=self.app_config,
631
            current_user=None,
632
        )
633
        group_api = GroupApi(
634
            current_user=None,
635
            session=self.session,
636
            config=self.app_config,
637
        )
638
        groups = [group_api.get_one(Group.TIM_USER),
639
                  group_api.get_one(Group.TIM_MANAGER),
640
                  group_api.get_one(Group.TIM_ADMIN)]
641
642
        user = uapi.create_minimal_user(email='this.is@user',
643
                                        groups=groups, save_now=True)
644
        workspace = WorkspaceApi(
645
            current_user=user,
646
            session=self.session,
647
            config=self.app_config,
648
        ).create_workspace('test workspace', save_now=True)
649
        api = ContentApi(
650
            current_user=user,
651
            session=self.session,
652
            config=self.app_config,
653
        )
654
        item = api.create(
655
            content_type_slug=content_type_list.Folder.slug,
656
            workspace=workspace,
657
            parent=None,
658
            label='not_deleted',
659
            do_save=True
660
        )
661
        item2 = api.create(
662
            content_type_slug=content_type_list.Folder.slug,
663
            workspace=workspace,
664
            parent=None,
665
            label='to_delete',
666
            do_save=True
667
        )
668
        uid = user.user_id
669
        wid = workspace.workspace_id
670
        transaction.commit()
671
672
        # Refresh instances after commit
673
        user = uapi.get_one(uid)
674
        workspace_api = WorkspaceApi(
675
            current_user=user,
676
            session=self.session,
677
            config=self.app_config
678
        )
679
        workspace = workspace_api.get_one(wid)
680
        api = ContentApi(
681
            current_user=user,
682
            session=self.session,
683
            config=self.app_config,
684
        )
685
        items = api.get_all(None, content_type_list.Any_SLUG, workspace)
686
        eq_(2, len(items))
687
688
        items = api.get_all(None, content_type_list.Any_SLUG, workspace)
689
        with new_revision(
690
                session=self.session,
691
                tm=transaction.manager,
692
                content=items[0]
693
        ):
694
            api.delete(items[0])
695
        transaction.commit()
696
697
        # Refresh instances after commit
698
        user = uapi.get_one(uid)
699
        workspace_api = WorkspaceApi(
700
            current_user=user,
701
            session=self.session,
702
            config=self.app_config
703
        )
704
        workspace = workspace_api.get_one(wid)
705
        api = ContentApi(
706
            current_user=user, 
707
            session=self.session,
708
            config=self.app_config,
709
        )
710
        items = api.get_all(None, content_type_list.Any_SLUG, workspace)
711
        eq_(1, len(items))
712
        transaction.commit()
713
714
        # Test that the item is still available if "show deleted" is activated
715
        # Refresh instances after commit
716
        user = uapi.get_one(uid)
717
        workspace_api = WorkspaceApi(
718
            current_user=user,
719
            session=self.session,
720
            config=self.app_config,
721
        )
722
        api = ContentApi(
723
            current_user=user,
724
            session=self.session,
725
            config=self.app_config,
726
            show_deleted=True,
727
        )
728
        items = api.get_all(None, content_type_list.Any_SLUG, workspace)
729
        eq_(2, len(items))
730
731
    def test_archive(self):
732
        uapi = UserApi(
733
            session=self.session,
734
            config=self.app_config,
735
            current_user=None,
736
        )
737
        group_api = GroupApi(
738
            current_user=None,
739
            session=self.session,
740
            config=self.app_config,
741
        )
742
        groups = [group_api.get_one(Group.TIM_USER),
743
                  group_api.get_one(Group.TIM_MANAGER),
744
                  group_api.get_one(Group.TIM_ADMIN)]
745
746
        user = uapi.create_minimal_user(
747
            email='this.is@user',
748
            groups=groups,
749
            save_now=True,
750
        )
751
        workspace_api = WorkspaceApi(
752
            current_user=user,
753
            session=self.session,
754
            config=self.app_config,
755
        )
756
        workspace = workspace_api.create_workspace(
757
            'test workspace',
758
            save_now=True
759
        )
760
        api = ContentApi(
761
            current_user=user,
762
            session=self.session,
763
            config=self.app_config,
764
        )
765
        item = api.create(
766
            content_type_slug=content_type_list.Folder.slug,
767
            workspace=workspace,
768
            parent=None,
769
            label='not_archived',
770
            do_save=True
771
        )
772
        item2 = api.create(
773
            content_type_slug=content_type_list.Folder.slug,
774
            workspace=workspace,
775
            parent=None,
776
            label='to_archive',
777
            do_save=True
778
        )
779
        uid = user.user_id
780
        wid = workspace.workspace_id
781
        transaction.commit()
782
        # Refresh instances after commit
783
        user = uapi.get_one(uid)
784
        workspace_api = WorkspaceApi(
785
            current_user=user,
786
            session=self.session,
787
            config=self.app_config,
788
        )
789
        api = ContentApi(
790
            session=self.session,
791
            current_user=user,
792
            config=self.app_config,
793
        )
794
795
        items = api.get_all(None, content_type_list.Any_SLUG, workspace)
796
        eq_(2, len(items))
797
798
        items = api.get_all(None, content_type_list.Any_SLUG, workspace)
799
        with new_revision(
800
                session=self.session,
801
                tm=transaction.manager,
802
                content=items[0],
803
        ):
804
            api.archive(items[0])
805
        transaction.commit()
806
807
        # Refresh instances after commit
808
        user = uapi.get_one(uid)
809
        workspace_api = WorkspaceApi(
810
            current_user=user,
811
            session=self.session,
812
            config=self.app_config,
813
        )
814
        workspace = workspace_api.get_one(wid)
815
        api = ContentApi(
816
            current_user=user, 
817
            session=self.session,
818
            config=self.app_config,
819
        )
820
821
        items = api.get_all(None, content_type_list.Any_SLUG, workspace)
822
        eq_(1, len(items))
823
        transaction.commit()
824
825
        # Refresh instances after commit
826
        user = uapi.get_one(uid)
827
        workspace_api = WorkspaceApi(
828
            current_user=user,
829
            session=self.session,
830
            config=self.app_config,
831
        )
832
        workspace = workspace_api.get_one(wid)
833
        api = ContentApi(
834
            current_user=user,
835
            session=self.session,
836
            config=self.app_config,
837
        )
838
839
        # Test that the item is still available if "show deleted" is activated
840
        api = ContentApi(
841
            current_user=None,
842
            session=self.session,
843
            config=self.app_config,
844
            show_archived=True,
845
        )
846
        items = api.get_all(None, content_type_list.Any_SLUG, workspace)
847
        eq_(2, len(items))
848
849
    def test_get_all_with_filter(self):
850
        uapi = UserApi(
851
            session=self.session,
852
            config=self.app_config,
853
            current_user=None,
854
        )
855
        group_api = GroupApi(
856
            current_user=None,
857
            session=self.session,
858
            config=self.app_config,
859
        )
860
        groups = [group_api.get_one(Group.TIM_USER),
861
                  group_api.get_one(Group.TIM_MANAGER),
862
                  group_api.get_one(Group.TIM_ADMIN)]
863
864
        user = uapi.create_minimal_user(
865
            email='this.is@user',
866
            groups=groups,
867
            save_now=True
868
        )
869
        workspace = WorkspaceApi(
870
            current_user=user,
871
            session=self.session,
872
            config=self.app_config,
873
        ).create_workspace(
874
            'test workspace',
875
            save_now=True
876
        )
877
878
        api = ContentApi(
879
            current_user=user,
880
            session=self.session,
881
            config=self.app_config,
882
        )
883
        item = api.create(
884
            content_type_slug=content_type_list.Folder.slug,
885
            workspace=workspace,
886
            parent=None,
887
            label='thefolder',
888
            do_save=True
889
        )
890
        item2 = api.create(
891
            content_type_slug=content_type_list.File.slug,
892
            workspace=workspace,
893
            parent=None,
894
            label='thefile',
895
            do_save=True
896
        )
897
        uid = user.user_id
898
        wid = workspace.workspace_id
899
        transaction.commit()
900
901
        # Refresh instances after commit
902
        user = uapi.get_one(uid)
903
        workspace_api = WorkspaceApi(
904
            current_user=user,
905
            session=self.session,
906
            config=self.app_config,
907
        )
908
        workspace = workspace_api.get_one(wid)
909
        api = ContentApi(
910
            current_user=user, 
911
            session=self.session,
912
            config=self.app_config,
913
        )
914
915
        items = api.get_all(None, content_type_list.Any_SLUG, workspace)
916
        eq_(2, len(items))
917
918
        items2 = api.get_all(None, content_type_list.File.slug, workspace)
919
        eq_(1, len(items2))
920
        eq_('thefile', items2[0].label)
921
922
        items3 = api.get_all(None, content_type_list.Folder.slug, workspace)
923
        eq_(1, len(items3))
924
        eq_('thefolder', items3[0].label)
925
926
    def test_get_all_with_parent_id(self):
927
        uapi = UserApi(
928
            session=self.session,
929
            config=self.app_config,
930
            current_user=None,
931
        )
932
        group_api = GroupApi(
933
            current_user=None,
934
            session=self.session,
935
            config=self.app_config
936
        )
937
        groups = [group_api.get_one(Group.TIM_USER),
938
                  group_api.get_one(Group.TIM_MANAGER),
939
                  group_api.get_one(Group.TIM_ADMIN)]
940
941
        user = uapi.create_minimal_user(email='this.is@user',
942
                                        groups=groups, save_now=True)
943
        workspace = WorkspaceApi(
944
            current_user=user,
945
            session=self.session,
946
            config=self.app_config
947
        ).create_workspace('test workspace', save_now=True)
948
        api = ContentApi(
949
            current_user=user, 
950
            session=self.session,
951
            config=self.app_config,
952
        )
953
        item = api.create(
954
            content_type_list.Folder.slug,
955
            workspace,
956
            None,
957
            'parent',
958
            do_save=True,
959
        )
960
        item2 = api.create(
961
            content_type_list.File.slug,
962
            workspace,
963
            item,
964
            'file1',
965
            do_save=True,
966
        )
967
        item3 = api.create(
968
            content_type_list.File.slug,
969
            workspace,
970
            None,
971
            'file2',
972
            do_save=True,
973
        )
974
        parent_id = item.content_id
975
        child_id = item2.content_id
976
        uid = user.user_id
977
        wid = workspace.workspace_id
978
        transaction.commit()
979
980
        # Refresh instances after commit
981
        user = uapi.get_one(uid)
982
        workspace_api = WorkspaceApi(
983
            current_user=user,
984
            session=self.session,
985
            config=self.app_config,
986
        )
987
        workspace = workspace_api.get_one(wid)
988
        api = ContentApi(
989
            current_user=user,
990
            session=self.session,
991
            config=self.app_config,
992
        )
993
994
        items = api.get_all(None, content_type_list.Any_SLUG, workspace)
995
        eq_(3, len(items))
996
997
        items2 = api.get_all(parent_id, content_type_list.File.slug, workspace)
998
        eq_(1, len(items2))
999
        eq_(child_id, items2[0].content_id)
1000
1001
    def test_set_status_unknown_status(self):
1002
        uapi = UserApi(
1003
            session=self.session,
1004
            config=self.app_config,
1005
            current_user=None,
1006
        )
1007
        group_api = GroupApi(
1008
            current_user=None,
1009
            session=self.session,
1010
            config=self.app_config,
1011
        )
1012
        groups = [group_api.get_one(Group.TIM_USER),
1013
                  group_api.get_one(Group.TIM_MANAGER),
1014
                  group_api.get_one(Group.TIM_ADMIN)]
1015
1016
        user = uapi.create_minimal_user(email='this.is@user',
1017
                                        groups=groups, save_now=True)
1018
1019
        workspace = WorkspaceApi(
1020
            current_user=user,
1021
            session=self.session,
1022
            config=self.app_config,
1023
        ).create_workspace(
1024
            'test workspace',
1025
            save_now=True
1026
        )
1027
        api = ContentApi(
1028
            current_user=user, 
1029
            session=self.session,
1030
            config=self.app_config,
1031
        )
1032
        c = api.create(content_type_list.Folder.slug, workspace, None, 'parent', '', True)
1033
        with new_revision(
1034
            session=self.session,
1035
            tm=transaction.manager,
1036
            content=c,
1037
        ):
1038
            with pytest.raises(ValueError):
1039
                api.set_status(c, 'unknown-status')
1040
1041
    def test_set_status_ok(self):
1042
        uapi = UserApi(
1043
            session=self.session,
1044
            config=self.app_config,
1045
            current_user=None,
1046
        )
1047
        group_api = GroupApi(
1048
            current_user=None,
1049
            session=self.session,
1050
            config=self.app_config,
1051
        )
1052
        groups = [group_api.get_one(Group.TIM_USER),
1053
                  group_api.get_one(Group.TIM_MANAGER),
1054
                  group_api.get_one(Group.TIM_ADMIN)]
1055
1056
        user = uapi.create_minimal_user(email='this.is@user',
1057
                                        groups=groups, save_now=True)
1058
1059
        workspace = WorkspaceApi(
1060
            current_user=user,
1061
            session=self.session,
1062
            config=self.app_config,
1063
        ).create_workspace(
1064
            'test workspace',
1065
            save_now=True
1066
        )
1067
        api = ContentApi(
1068
            current_user=user,
1069
            session=self.session,
1070
            config=self.app_config,
1071
        )
1072
        c = api.create(content_type_list.Folder.slug, workspace, None, 'parent', '', True)
1073
        with new_revision(
1074
            session=self.session,
1075
            tm=transaction.manager,
1076
            content=c,
1077
        ):
1078
            for new_status in ['open', 'closed-validated', 'closed-unvalidated',
1079
                               'closed-deprecated']:
1080
                api.set_status(c, new_status)
1081
1082
                eq_(new_status, c.status)
1083
                eq_(ActionDescription.STATUS_UPDATE, c.revision_type)
1084
1085
    def test_create_comment_ok(self):
1086
        uapi = UserApi(
1087
            session=self.session,
1088
            config=self.app_config,
1089
            current_user=None,
1090
        )
1091
        group_api = GroupApi(
1092
            current_user=None,
1093
            session=self.session,
1094
            config=self.config,
1095
        )
1096
        groups = [group_api.get_one(Group.TIM_USER),
1097
                  group_api.get_one(Group.TIM_MANAGER),
1098
                  group_api.get_one(Group.TIM_ADMIN)]
1099
1100
        user = uapi.create_minimal_user(email='this.is@user',
1101
                                        groups=groups, save_now=True)
1102
1103
        workspace = WorkspaceApi(
1104
            current_user=user,
1105
            session=self.session,
1106
            config=self.app_config,
1107
        ).create_workspace(
1108
            'test workspace',
1109
            save_now=True
1110
        )
1111
1112
        api = ContentApi(
1113
            current_user=user,
1114
            session=self.session,
1115
            config=self.app_config,
1116
        )
1117
        p = api.create(content_type_list.Page.slug, workspace, None, 'this_is_a_page')
1118
        c = api.create_comment(workspace, p, 'this is the comment', True)
1119
1120
        eq_(Content, c.__class__)
1121
        eq_(p.content_id, c.parent_id)
1122
        eq_(user, c.owner)
1123
        eq_(workspace, c.workspace)
1124
        eq_(content_type_list.Comment.slug, c.type)
1125
        eq_('this is the comment', c.description)
1126
        eq_('', c.label)
1127
        eq_(ActionDescription.COMMENT, c.revision_type)
1128
1129 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...
1130
        uapi = UserApi(
1131
            session=self.session,
1132
            config=self.app_config,
1133
            current_user=None,
1134
        )
1135
        group_api = GroupApi(
1136
            current_user=None,
1137
            session=self.session,
1138
            config=self.app_config
1139
        )
1140
        groups = [group_api.get_one(Group.TIM_USER),
1141
                  group_api.get_one(Group.TIM_MANAGER),
1142
                  group_api.get_one(Group.TIM_ADMIN)]
1143
1144
        user = uapi.create_minimal_user(
1145
            email='user1@user',
1146
            groups=groups,
1147
            save_now=True
1148
        )
1149
        user2 = uapi.create_minimal_user(
1150
            email='user2@user',
1151
            groups=groups,
1152
            save_now=True
1153
        )
1154
        workspace = WorkspaceApi(
1155
            current_user=user,
1156
            session=self.session,
1157
            config=self.app_config,
1158
        ).create_workspace(
1159
            'test workspace',
1160
            save_now=True
1161
        )
1162
        RoleApi(
1163
            current_user=user,
1164
            session=self.session,
1165
            config=self.app_config,
1166
        ).create_one(
1167
            user2,
1168
            workspace,
1169
            UserRoleInWorkspace.WORKSPACE_MANAGER,
1170
            with_notif=False
1171
        )
1172
        api = ContentApi(
1173
            current_user=user,
1174
            session=self.session,
1175
            config=self.app_config,
1176
        )
1177
        foldera = api.create(
1178
            content_type_list.Folder.slug,
1179
            workspace,
1180
            None,
1181
            'folder a',
1182
            '',
1183
            True
1184
        )
1185
        with self.session.no_autoflush:
1186
            text_file = api.create(
1187
                content_type_slug=content_type_list.File.slug,
1188
                workspace=workspace,
1189
                parent=foldera,
1190
                label='test_file',
1191
                do_save=False,
1192
            )
1193
            api.update_file_data(
1194
                text_file,
1195
                'test_file',
1196
                'text/plain',
1197
                b'test_content'
1198
            )
1199
1200
        api.save(text_file, ActionDescription.CREATION)
1201
        api2 = ContentApi(
1202
            current_user=user2,
1203
            session=self.session,
1204
            config=self.app_config,
1205
        )
1206
        workspace2 = WorkspaceApi(
1207
            current_user=user2,
1208
            session=self.session,
1209
            config=self.app_config,
1210
        ).create_workspace(
1211
            'test workspace2',
1212
            save_now=True
1213
        )
1214
        folderb = api2.create(
1215
            content_type_list.Folder.slug,
1216
            workspace2,
1217
            None,
1218
            'folder b',
1219
            '',
1220
            True
1221
        )
1222
1223
        api2.copy(
1224
            item=text_file,
1225
            new_parent=folderb,
1226
            new_label='test_file_copy'
1227
        )
1228
1229
        transaction.commit()
1230
        text_file_copy = api2.get_one_by_label_and_parent(
1231
            'test_file_copy',
1232
            folderb,
1233
        )
1234
1235
        assert text_file != text_file_copy
1236
        assert text_file_copy.content_id != text_file.content_id
1237
        assert text_file_copy.workspace_id == workspace2.workspace_id
1238
        assert text_file_copy.depot_file.file.read() == text_file.depot_file.file.read()   # nopep8
1239
        assert text_file_copy.depot_file.path != text_file.depot_file.path
1240
        assert text_file_copy.label == 'test_file_copy'
1241
        assert text_file_copy.type == text_file.type
1242
        assert text_file_copy.parent.content_id == folderb.content_id
1243
        assert text_file_copy.owner.user_id == user.user_id
1244
        assert text_file_copy.description == text_file.description
1245
        assert text_file_copy.file_extension == text_file.file_extension
1246
        assert text_file_copy.file_mimetype == text_file.file_mimetype
1247
        assert text_file_copy.revision_type == ActionDescription.COPY
1248
        assert len(text_file_copy.revisions) == len(text_file.revisions) + 1
1249
1250 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...
1251
        uapi = UserApi(
1252
            session=self.session,
1253
            config=self.app_config,
1254
            current_user=None,
1255
        )
1256
        group_api = GroupApi(
1257
            current_user=None,
1258
            session=self.session,
1259
            config=self.app_config,
1260
        )
1261
        groups = [group_api.get_one(Group.TIM_USER),
1262
                  group_api.get_one(Group.TIM_MANAGER),
1263
                  group_api.get_one(Group.TIM_ADMIN)]
1264
1265
        user = uapi.create_minimal_user(
1266
            email='user1@user',
1267
            groups=groups,
1268
            save_now=True
1269
        )
1270
        user2 = uapi.create_minimal_user(
1271
            email='user2@user',
1272
            groups=groups,
1273
            save_now=True
1274
        )
1275
        workspace = WorkspaceApi(
1276
            current_user=user,
1277
            session=self.session,
1278
            config=self.app_config,
1279
        ).create_workspace(
1280
            'test workspace',
1281
            save_now=True
1282
        )
1283
        RoleApi(
1284
            current_user=user,
1285
            session=self.session,
1286
            config=self.app_config,
1287
        ).create_one(
1288
            user2,
1289
            workspace,
1290
            UserRoleInWorkspace.WORKSPACE_MANAGER,
1291
            with_notif=False
1292
        )
1293
        api = ContentApi(
1294
            current_user=user,
1295
            session=self.session,
1296
            config=self.app_config,
1297
        )
1298
        foldera = api.create(
1299
            content_type_list.Folder.slug,
1300
            workspace,
1301
            None,
1302
            'folder a',
1303
            '',
1304
            True
1305
        )
1306
        with self.session.no_autoflush:
1307
            text_file = api.create(
1308
                content_type_slug=content_type_list.File.slug,
1309
                workspace=workspace,
1310
                parent=foldera,
1311
                label='test_file',
1312
                do_save=False,
1313
            )
1314
            api.update_file_data(
1315
                text_file,
1316
                'test_file',
1317
                'text/plain',
1318
                b'test_content'
1319
            )
1320
1321
        api.save(text_file, ActionDescription.CREATION)
1322
        api2 = ContentApi(
1323
            current_user=user2,
1324
            session=self.session,
1325
            config=self.app_config,
1326
        )
1327
        workspace2 = WorkspaceApi(
1328
            current_user=user2,
1329
            session=self.session,
1330
            config=self.app_config,
1331
        ).create_workspace(
1332
            'test workspace2',
1333
            save_now=True
1334
        )
1335
        folderb = api2.create(
1336
            content_type_list.Folder.slug,
1337
            workspace2,
1338
            None,
1339
            'folder b',
1340
            '',
1341
            True
1342
        )
1343
        api2.copy(
1344
            item=text_file,
1345
            new_parent=folderb,
1346
        )
1347
1348
        transaction.commit()
1349
        text_file_copy = api2.get_one_by_label_and_parent(
1350
            'test_file',
1351
            folderb,
1352
        )
1353
1354
        assert text_file != text_file_copy
1355
        assert text_file_copy.content_id != text_file.content_id
1356
        assert text_file_copy.workspace_id == workspace2.workspace_id
1357
        assert text_file_copy.depot_file.file.read() == text_file.depot_file.file.read()  # nopep8
1358
        assert text_file_copy.depot_file.path != text_file.depot_file.path
1359
        assert text_file_copy.label == text_file.label
1360
        assert text_file_copy.type == text_file.type
1361
        assert text_file_copy.parent.content_id == folderb.content_id
1362
        assert text_file_copy.owner.user_id == user.user_id
1363
        assert text_file_copy.description == text_file.description
1364
        assert text_file_copy.file_extension == text_file.file_extension
1365
        assert text_file_copy.file_mimetype == text_file.file_mimetype
1366
        assert text_file_copy.revision_type == ActionDescription.COPY
1367
        assert len(text_file_copy.revisions) == len(text_file.revisions) + 1
1368
1369
    def test_unit_copy_file_different_label_same_parent_ok(self):
1370
        uapi = UserApi(
1371
            session=self.session,
1372
            config=self.app_config,
1373
            current_user=None,
1374
        )
1375
        group_api = GroupApi(
1376
            current_user=None,
1377
            session=self.session,
1378
            config=self.app_config,
1379
        )
1380
        groups = [group_api.get_one(Group.TIM_USER),
1381
                  group_api.get_one(Group.TIM_MANAGER),
1382
                  group_api.get_one(Group.TIM_ADMIN)]
1383
1384
        user = uapi.create_minimal_user(
1385
            email='user1@user',
1386
            groups=groups,
1387
            save_now=True,
1388
        )
1389
        user2 = uapi.create_minimal_user(
1390
            email='user2@user',
1391
            groups=groups,
1392
            save_now=True
1393
        )
1394
        workspace = WorkspaceApi(
1395
            current_user=user,
1396
            session=self.session,
1397
            config=self.app_config,
1398
        ).create_workspace(
1399
            'test workspace',
1400
            save_now=True
1401
        )
1402
        RoleApi(
1403
            current_user=user,
1404
            session=self.session,
1405
            config=self.app_config,
1406
        ).create_one(
1407
            user2, workspace,
1408
            UserRoleInWorkspace.WORKSPACE_MANAGER,
1409
            with_notif=False
1410
        )
1411
        api = ContentApi(
1412
            current_user=user,
1413
            session=self.session,
1414
            config=self.app_config,
1415
        )
1416
        foldera = api.create(
1417
            content_type_list.Folder.slug,
1418
            workspace,
1419
            None,
1420
            'folder a',
1421
            '',
1422
            True
1423
        )
1424
        with self.session.no_autoflush:
1425
            text_file = api.create(
1426
                content_type_slug=content_type_list.File.slug,
1427
                workspace=workspace,
1428
                parent=foldera,
1429
                label='test_file',
1430
                do_save=False,
1431
            )
1432
            api.update_file_data(
1433
                text_file,
1434
                'test_file',
1435
                'text/plain',
1436
                b'test_content'
1437
            )
1438
1439
        api.save(
1440
            text_file,
1441
            ActionDescription.CREATION
1442
        )
1443
        api2 = ContentApi(
1444
            current_user=user2,
1445
            session=self.session,
1446
            config=self.app_config,
1447
        )
1448
1449
        api2.copy(
1450
            item=text_file,
1451
            new_label='test_file_copy'
1452
        )
1453
1454
        transaction.commit()
1455
        text_file_copy = api2.get_one_by_label_and_parent(
1456
            'test_file_copy',
1457
            foldera,
1458
        )
1459
1460
        assert text_file != text_file_copy
1461
        assert text_file_copy.content_id != text_file.content_id
1462
        assert text_file_copy.workspace_id == workspace.workspace_id
1463
        assert text_file_copy.depot_file.file.read() == text_file.depot_file.file.read()  # nopep8
1464
        assert text_file_copy.depot_file.path != text_file.depot_file.path
1465
        assert text_file_copy.label == 'test_file_copy'
1466
        assert text_file_copy.type == text_file.type
1467
        assert text_file_copy.parent.content_id == foldera.content_id
1468
        assert text_file_copy.owner.user_id == user.user_id
1469
        assert text_file_copy.description == text_file.description
1470
        assert text_file_copy.file_extension == text_file.file_extension
1471
        assert text_file_copy.file_mimetype == text_file.file_mimetype
1472
        assert text_file_copy.revision_type == ActionDescription.COPY
1473
        assert len(text_file_copy.revisions) == len(text_file.revisions) + 1
1474
1475
    def test_unit_copy_file_different_label_same_parent__err__label_already_used(self):
1476
        uapi = UserApi(
1477
            session=self.session,
1478
            config=self.app_config,
1479
            current_user=None,
1480
        )
1481
        group_api = GroupApi(
1482
            current_user=None,
1483
            session=self.session,
1484
            config=self.app_config,
1485
        )
1486
        groups = [group_api.get_one(Group.TIM_USER),
1487
                  group_api.get_one(Group.TIM_MANAGER),
1488
                  group_api.get_one(Group.TIM_ADMIN)]
1489
1490
        user = uapi.create_minimal_user(
1491
            email='user1@user',
1492
            groups=groups,
1493
            save_now=True,
1494
        )
1495
        user2 = uapi.create_minimal_user(
1496
            email='user2@user',
1497
            groups=groups,
1498
            save_now=True
1499
        )
1500
        workspace = WorkspaceApi(
1501
            current_user=user,
1502
            session=self.session,
1503
            config=self.app_config,
1504
        ).create_workspace(
1505
            'test workspace',
1506
            save_now=True
1507
        )
1508
        RoleApi(
1509
            current_user=user,
1510
            session=self.session,
1511
            config=self.app_config,
1512
        ).create_one(
1513
            user2, workspace,
1514
            UserRoleInWorkspace.WORKSPACE_MANAGER,
1515
            with_notif=False
1516
        )
1517
        api = ContentApi(
1518
            current_user=user,
1519
            session=self.session,
1520
            config=self.app_config,
1521
        )
1522
        foldera = api.create(
1523
            content_type_list.Folder.slug,
1524
            workspace,
1525
            None,
1526
            'folder a',
1527
            '',
1528
            True
1529
        )
1530
        already_exist = api.create(
1531
            content_type_list.Folder.slug,
1532
            workspace,
1533
            foldera,
1534
            'already_exist',
1535
            '',
1536
            True
1537
        )
1538
        with self.session.no_autoflush:
1539
            text_file = api.create(
1540
                content_type_slug=content_type_list.File.slug,
1541
                workspace=workspace,
1542
                parent=foldera,
1543
                label='test_file',
1544
                do_save=False,
1545
            )
1546
            api.update_file_data(
1547
                text_file,
1548
                'test_file',
1549
                'text/plain',
1550
                b'test_content'
1551
            )
1552
1553
        api.save(
1554
            text_file,
1555
            ActionDescription.CREATION
1556
        )
1557
        api2 = ContentApi(
1558
            current_user=user2,
1559
            session=self.session,
1560
            config=self.app_config,
1561
        )
1562
        with pytest.raises(ContentFilenameAlreadyUsedInFolder):
1563
            api2.copy(
1564
                item=text_file,
1565
                new_label='already_exist'
1566
            )
1567
1568
        transaction.commit()
1569
        new_already_exist = api2.get_one_by_label_and_parent(
1570
            'already_exist',
1571
            foldera,
1572
        )
1573
1574
        # file has no changed
1575
        assert new_already_exist.content_id == already_exist.content_id
1576
1577
    def test_mark_read__workspace(self):
1578
        uapi = UserApi(
1579
            session=self.session,
1580
            config=self.app_config,
1581
            current_user=None,
1582
        )
1583
        group_api = GroupApi(
1584
            current_user=None,
1585
            session=self.session,
1586
            config=self.app_config,
1587
        )
1588
        groups = [group_api.get_one(Group.TIM_USER),
1589
                  group_api.get_one(Group.TIM_MANAGER),
1590
                  group_api.get_one(Group.TIM_ADMIN)]
1591
1592
        user_a = uapi.create_minimal_user(email='this.is@user',
1593
                                          groups=groups, save_now=True)
1594
        user_b = uapi.create_minimal_user(email='[email protected]',
1595
                                          groups=groups, save_now=True)
1596
1597
        wapi = WorkspaceApi(
1598
            current_user=user_a,
1599
            session=self.session,
1600
            config=self.app_config,
1601
        )
1602
        workspace1 = wapi.create_workspace(
1603
            'test workspace n°1',
1604
            save_now=True)
1605
        workspace2 = wapi.create_workspace(
1606
            'test workspace n°2',
1607
            save_now=True)
1608
1609
        role_api1 = RoleApi(
1610
            current_user=user_a,
1611
            session=self.session,
1612
            config=self.app_config,
1613
        )
1614
        role_api1.create_one(
1615
            user_b,
1616
            workspace1,
1617
            UserRoleInWorkspace.READER,
1618
            False
1619
        )
1620
1621
        role_api2 = RoleApi(
1622
            current_user=user_b,
1623
            session=self.session,
1624
            config=self.app_config,
1625
        )
1626
        role_api2.create_one(user_b, workspace2, UserRoleInWorkspace.READER,
1627
                             False)
1628
1629
        cont_api_a = ContentApi(
1630
            current_user=user_a,
1631
            session=self.session,
1632
            config=self.app_config,
1633
        )
1634
        cont_api_b = ContentApi(
1635
            current_user=user_b,
1636
            session=self.session,
1637
            config=self.app_config,
1638
        )
1639
1640
        # Creates page_1 & page_2 in workspace 1
1641
        #     and page_3 & page_4 in workspace 2
1642
        page_1 = cont_api_a.create(content_type_list.Page.slug, workspace1, None,
1643
                                   'this is a page', do_save=True)
1644
        page_2 = cont_api_a.create(content_type_list.Page.slug, workspace1, None,
1645
                                   'this is page1', do_save=True)
1646
        page_3 = cont_api_a.create(content_type_list.Thread.slug, workspace2, None,
1647
                                   'this is page2', do_save=True)
1648
        page_4 = cont_api_a.create(content_type_list.File.slug, workspace2, None,
1649
                                   'this is page3', do_save=True)
1650
1651
        for rev in page_1.revisions:
1652
            eq_(user_b not in rev.read_by.keys(), True)
1653
        for rev in page_2.revisions:
1654
            eq_(user_b not in rev.read_by.keys(), True)
1655
        for rev in page_3.revisions:
1656
            eq_(user_b not in rev.read_by.keys(), True)
1657
        for rev in page_4.revisions:
1658
            eq_(user_b not in rev.read_by.keys(), True)
1659
1660
        # Set as read the workspace n°1
1661
        cont_api_b.mark_read__workspace(workspace=workspace1)
1662
1663
        for rev in page_1.revisions:
1664
            eq_(user_b in rev.read_by.keys(), True)
1665
        for rev in page_2.revisions:
1666
            eq_(user_b in rev.read_by.keys(), True)
1667
        for rev in page_3.revisions:
1668
            eq_(user_b not in rev.read_by.keys(), True)
1669
        for rev in page_4.revisions:
1670
            eq_(user_b not in rev.read_by.keys(), True)
1671
1672
        # Set as read the workspace n°2
1673
        cont_api_b.mark_read__workspace(workspace=workspace2)
1674
1675
        for rev in page_1.revisions:
1676
            eq_(user_b in rev.read_by.keys(), True)
1677
        for rev in page_2.revisions:
1678
            eq_(user_b in rev.read_by.keys(), True)
1679
        for rev in page_3.revisions:
1680
            eq_(user_b in rev.read_by.keys(), True)
1681
        for rev in page_4.revisions:
1682
            eq_(user_b in rev.read_by.keys(), True)
1683
1684
    def test_mark_read(self):
1685
        uapi = UserApi(
1686
            session=self.session,
1687
            config=self.app_config,
1688
            current_user=None,
1689
        )
1690
        group_api = GroupApi(
1691
            current_user=None,
1692
            session=self.session,
1693
            config = self.app_config,
1694
        )
1695
        groups = [group_api.get_one(Group.TIM_USER),
1696
                  group_api.get_one(Group.TIM_MANAGER),
1697
                  group_api.get_one(Group.TIM_ADMIN)]
1698
1699
        user_a = uapi.create_minimal_user(
1700
            email='this.is@user',
1701
            groups=groups,
1702
            save_now=True
1703
        )
1704
        user_b = uapi.create_minimal_user(
1705
            email='[email protected]',
1706
            groups=groups,
1707
            save_now=True
1708
        )
1709
1710
        wapi = WorkspaceApi(
1711
            current_user=user_a,
1712
            session=self.session,
1713
            config=self.app_config,
1714
        )
1715
        workspace_api = WorkspaceApi(
1716
            current_user=user_a,
1717
            session=self.session,
1718
            config=self.app_config,
1719
        )
1720
        workspace = wapi.create_workspace(
1721
            'test workspace',
1722
            save_now=True)
1723
1724
        role_api = RoleApi(
1725
            current_user=user_a,
1726
            session=self.session,
1727
            config=self.app_config,
1728
        )
1729
        role_api.create_one(
1730
            user_b,
1731
            workspace,
1732
            UserRoleInWorkspace.READER,
1733
            False
1734
        )
1735
        cont_api_a = ContentApi(
1736
            current_user=user_a,
1737
            session=self.session,
1738
            config=self.app_config,
1739
        )
1740
        cont_api_b = ContentApi(
1741
            current_user=user_b,
1742
            session=self.session,
1743
            config=self.app_config,
1744
        )
1745
1746
        page_1 = cont_api_a.create(content_type_list.Page.slug, workspace, None,
1747
                                   'this is a page', do_save=True)
1748
1749
        for rev in page_1.revisions:
1750
            eq_(user_b not in rev.read_by.keys(), True)
1751
1752
        cont_api_b.mark_read(page_1)
1753
1754
        for rev in page_1.revisions:
1755
            eq_(user_b in rev.read_by.keys(), True)
1756
1757
    def test_mark_read__all(self):
1758
        uapi = UserApi(
1759
            session=self.session,
1760
            config=self.app_config,
1761
            current_user=None,
1762
        )
1763
        group_api = GroupApi(
1764
            current_user=None,
1765
            session=self.session,
1766
            config=self.app_config,
1767
        )
1768
        groups = [group_api.get_one(Group.TIM_USER),
1769
                  group_api.get_one(Group.TIM_MANAGER),
1770
                  group_api.get_one(Group.TIM_ADMIN)]
1771
1772
        user_a = uapi.create_minimal_user(
1773
            email='this.is@user',
1774
            groups=groups,
1775
            save_now=True
1776
        )
1777
        user_b = uapi.create_minimal_user(
1778
            email='[email protected]',
1779
            groups=groups,
1780
            save_now=True
1781
        )
1782
1783
        wapi = WorkspaceApi(
1784
            current_user=user_a,
1785
            session=self.session,
1786
            config=self.app_config,
1787
        )
1788
        workspace = wapi.create_workspace(
1789
            'test workspace',
1790
            save_now=True)
1791
1792
        role_api = RoleApi(
1793
            current_user=user_a,
1794
            session=self.session,
1795
            config=self.app_config,
1796
        )
1797
        role_api.create_one(
1798
            user_b,
1799
            workspace,
1800
            UserRoleInWorkspace.READER,
1801
            False
1802
        )
1803
        cont_api_a = ContentApi(
1804
            current_user=user_a,
1805
            session=self.session,
1806
            config=self.app_config,
1807
        )
1808
        cont_api_b = ContentApi(
1809
            current_user=user_b,
1810
            session=self.session,
1811
            config=self.app_config,
1812
        )
1813
1814
        page_2 = cont_api_a.create(
1815
            content_type_list.Page.slug,
1816
            workspace,
1817
            None,
1818
            'this is page1',
1819
            do_save=True
1820
        )
1821
        page_3 = cont_api_a.create(
1822
            content_type_list.Thread.slug,
1823
            workspace,
1824
            None,
1825
            'this is page2',
1826
            do_save=True
1827
        )
1828
        page_4 = cont_api_a.create(
1829
            content_type_list.File.slug,
1830
            workspace,
1831
            None,
1832
            'this is page3',
1833
            do_save=True
1834
        )
1835
1836
        for rev in page_2.revisions:
1837
            eq_(user_b not in rev.read_by.keys(), True)
1838
        for rev in page_3.revisions:
1839
            eq_(user_b not in rev.read_by.keys(), True)
1840
        for rev in page_4.revisions:
1841
            eq_(user_b not in rev.read_by.keys(), True)
1842
1843
        self.session.refresh(page_2)
1844
        self.session.refresh(page_3)
1845
        self.session.refresh(page_4)
1846
1847
        cont_api_b.mark_read__all()
1848
1849
        for rev in page_2.revisions:
1850
            eq_(user_b in rev.read_by.keys(), True)
1851
        for rev in page_3.revisions:
1852
            eq_(user_b in rev.read_by.keys(), True)
1853
        for rev in page_4.revisions:
1854
            eq_(user_b in rev.read_by.keys(), True)
1855
1856
    def test_unit__update__ok__nominal_case(self):
1857
        uapi = UserApi(
1858
            session=self.session,
1859
            config=self.app_config,
1860
            current_user=None,
1861
        )
1862
        group_api = GroupApi(
1863
            current_user=None,
1864
            session=self.session,
1865
            config=self.app_config,
1866
        )
1867
        groups = [group_api.get_one(Group.TIM_USER),
1868
                  group_api.get_one(Group.TIM_MANAGER),
1869
                  group_api.get_one(Group.TIM_ADMIN)]
1870
1871
        user1 = uapi.create_minimal_user(
1872
            email='this.is@user',
1873
            groups=groups,
1874
            save_now=True
1875
        )
1876
1877
        workspace_api = WorkspaceApi(
1878
            current_user=user1,
1879
            session=self.session,
1880
            config=self.app_config,
1881
        )
1882
        workspace = workspace_api.create_workspace(
1883
            'test workspace',
1884
            save_now=True
1885
        )
1886
        
1887
        wid = workspace.workspace_id
1888
1889
        user2 = uapi.create_minimal_user('[email protected]')
1890
        uapi.save(user2)
1891
1892
        RoleApi(
1893
            current_user=user1,
1894
            session=self.session,
1895
            config=self.app_config,
1896
        ).create_one(
1897
            user2,
1898
            workspace,
1899
            UserRoleInWorkspace.CONTENT_MANAGER,
1900
            with_notif=False,
1901
            flush=True
1902
        )
1903
1904
        # Test starts here
1905
1906
        api = ContentApi(
1907
            current_user=user1,
1908
            session=self.session,
1909
            config=self.app_config,
1910
        )
1911
1912
        p = api.create(
1913
            content_type_slug=content_type_list.Page.slug,
1914
            workspace=workspace,
1915
            parent=None,
1916
            label='this_is_a_page',
1917
            do_save=True
1918
        )
1919
1920
        u1id = user1.user_id
1921
        u2id = user2.user_id
1922
        pcid = p.content_id
1923
        poid = p.owner_id
1924
1925
        transaction.commit()
1926
1927
        # Refresh instances after commit
1928
        user1 = uapi.get_one(u1id)
1929
        workspace = WorkspaceApi(
1930
            current_user=user1,
1931
            session=self.session,
1932
            config=self.app_config,
1933
        ).get_one(wid)
1934
        api = ContentApi(
1935
            current_user=user1,
1936
            session=self.session,
1937
            config=self.app_config,
1938
        )
1939
1940
        content = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
1941
        eq_(u1id, content.owner_id)
1942
        eq_(poid, content.owner_id)
1943
1944
        u2 = UserApi(
1945
            session=self.session,
1946
            config=self.app_config,
1947
            current_user=None,
1948
        ).get_one(u2id)
1949
        api2 = ContentApi(
1950
            current_user=u2,
1951
            session=self.session,
1952
            config=self.app_config,
1953
        )
1954
        content2 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
1955
        with new_revision(
1956
           session=self.session,
1957
           tm=transaction.manager,
1958
           content=content2,
1959
        ):
1960
            api2.update_content(
1961
                content2,
1962
                'this is an updated page',
1963
                'new content'
1964
            )
1965
        api2.save(content2)
1966
        transaction.commit()
1967
1968
        # Refresh instances after commit
1969
        user1 = uapi.get_one(u1id)
1970
        workspace = WorkspaceApi(
1971
            current_user=user1,
1972
            session=self.session,
1973
            config=self.app_config,
1974
        ).get_one(wid)
1975
        api = ContentApi(
1976
            current_user=user1,
1977
            session=self.session,
1978
            config=self.app_config,
1979
        )
1980
1981
        updated = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
1982
        eq_(u2id, updated.owner_id,
1983
            'the owner id should be {} (found {})'.format(u2id,
1984
                                                          updated.owner_id))
1985
        eq_('this is an updated page', updated.label)
1986
        eq_('new content', updated.description)
1987
        eq_(ActionDescription.EDITION, updated.revision_type)
1988
1989 View Code Duplication
    def test_unit__update__err__status_closed(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1990
        uapi = UserApi(
1991
            session=self.session,
1992
            config=self.app_config,
1993
            current_user=None,
1994
        )
1995
        group_api = GroupApi(
1996
            current_user=None,
1997
            session=self.session,
1998
            config=self.app_config,
1999
        )
2000
        groups = [group_api.get_one(Group.TIM_USER),
2001
                  group_api.get_one(Group.TIM_MANAGER),
2002
                  group_api.get_one(Group.TIM_ADMIN)]
2003
2004
        user1 = uapi.create_minimal_user(
2005
            email='this.is@user',
2006
            groups=groups,
2007
            save_now=True
2008
        )
2009
2010
        workspace_api = WorkspaceApi(
2011
            current_user=user1,
2012
            session=self.session,
2013
            config=self.app_config,
2014
        )
2015
        workspace = workspace_api.create_workspace(
2016
            'test workspace',
2017
            save_now=True
2018
        )
2019
2020
        wid = workspace.workspace_id
2021
2022
        user2 = uapi.create_minimal_user('[email protected]')
2023
        uapi.save(user2)
2024
2025
        RoleApi(
2026
            current_user=user1,
2027
            session=self.session,
2028
            config=self.app_config,
2029
        ).create_one(
2030
            user2,
2031
            workspace,
2032
            UserRoleInWorkspace.CONTENT_MANAGER,
2033
            with_notif=False,
2034
            flush=True
2035
        )
2036
2037
        # Test starts here
2038
2039
        api = ContentApi(
2040
            current_user=user1,
2041
            session=self.session,
2042
            config=self.app_config,
2043
        )
2044
2045
        p = api.create(
2046
            content_type_slug=content_type_list.Page.slug,
2047
            workspace=workspace,
2048
            parent=None,
2049
            label='this_is_a_page',
2050
            do_save=False
2051
        )
2052
        p.status = 'closed-validated'
2053
        api.save(p)
2054
        u1id = user1.user_id
2055
        u2id = user2.user_id
2056
        pcid = p.content_id
2057
        poid = p.owner_id
2058
2059
        transaction.commit()
2060
2061
        # Refresh instances after commit
2062
        user1 = uapi.get_one(u1id)
2063
        workspace = WorkspaceApi(
2064
            current_user=user1,
2065
            session=self.session,
2066
            config=self.app_config,
2067
        ).get_one(wid)
2068
        api = ContentApi(
2069
            current_user=user1,
2070
            session=self.session,
2071
            config=self.app_config,
2072
        )
2073
2074
        content = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
2075
        eq_(u1id, content.owner_id)
2076
        eq_(poid, content.owner_id)
2077
2078
        u2 = UserApi(
2079
            session=self.session,
2080
            config=self.app_config,
2081
            current_user=None,
2082
        ).get_one(u2id)
2083
        api2 = ContentApi(
2084
            current_user=u2,
2085
            session=self.session,
2086
            config=self.app_config,
2087
        )
2088
        content2 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
2089
        content2_nb_rev = len(content2.revisions)
2090
        with pytest.raises(ContentInNotEditableState):
2091
            with new_revision(
2092
                    session=self.session,
2093
                    tm=transaction.manager,
2094
                    content=content2,
2095
            ):
2096
                    api2.update_content(
2097
                        content2,
2098
                        'this is an updated page',
2099
                        'new content'
2100
                    )
2101
        content3 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
2102
        assert content3.label == 'this_is_a_page'
2103
        assert content2_nb_rev == len(content3.revisions)
2104
2105 View Code Duplication
    def test_unit__update__err__label_already_used(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2106
        uapi = UserApi(
2107
            session=self.session,
2108
            config=self.app_config,
2109
            current_user=None,
2110
        )
2111
        group_api = GroupApi(
2112
            current_user=None,
2113
            session=self.session,
2114
            config=self.app_config,
2115
        )
2116
        groups = [group_api.get_one(Group.TIM_USER),
2117
                  group_api.get_one(Group.TIM_MANAGER),
2118
                  group_api.get_one(Group.TIM_ADMIN)]
2119
2120
        user1 = uapi.create_minimal_user(
2121
            email='this.is@user',
2122
            groups=groups,
2123
            save_now=True
2124
        )
2125
2126
        workspace_api = WorkspaceApi(
2127
            current_user=user1,
2128
            session=self.session,
2129
            config=self.app_config,
2130
        )
2131
        workspace = workspace_api.create_workspace(
2132
            'test workspace',
2133
            save_now=True
2134
        )
2135
2136
        wid = workspace.workspace_id
2137
2138
        user2 = uapi.create_minimal_user('[email protected]')
2139
        uapi.save(user2)
2140
2141
        RoleApi(
2142
            current_user=user1,
2143
            session=self.session,
2144
            config=self.app_config,
2145
        ).create_one(
2146
            user2,
2147
            workspace,
2148
            UserRoleInWorkspace.CONTENT_MANAGER,
2149
            with_notif=False,
2150
            flush=True
2151
        )
2152
2153
        # Test starts here
2154
2155
        api = ContentApi(
2156
            current_user=user1,
2157
            session=self.session,
2158
            config=self.app_config,
2159
        )
2160
2161
        p = api.create(
2162
            content_type_slug=content_type_list.Page.slug,
2163
            workspace=workspace,
2164
            parent=None,
2165
            label='this_is_a_page',
2166
            do_save=True
2167
        )
2168
        p2 = api.create(
2169
            content_type_slug=content_type_list.Page.slug,
2170
            workspace=workspace,
2171
            parent=None,
2172
            label='this_is_a_page2',
2173
            do_save=True
2174
        )
2175
        u1id = user1.user_id
2176
        u2id = user2.user_id
2177
        pcid = p.content_id
2178
        poid = p.owner_id
2179
2180
        transaction.commit()
2181
2182
        # Refresh instances after commit
2183
        user1 = uapi.get_one(u1id)
2184
        workspace = WorkspaceApi(
2185
            current_user=user1,
2186
            session=self.session,
2187
            config=self.app_config,
2188
        ).get_one(wid)
2189
        api = ContentApi(
2190
            current_user=user1,
2191
            session=self.session,
2192
            config=self.app_config,
2193
        )
2194
2195
        content = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
2196
        eq_(u1id, content.owner_id)
2197
        eq_(poid, content.owner_id)
2198
2199
        u2 = UserApi(
2200
            session=self.session,
2201
            config=self.app_config,
2202
            current_user=None,
2203
        ).get_one(u2id)
2204
        api2 = ContentApi(
2205
            current_user=u2,
2206
            session=self.session,
2207
            config=self.app_config,
2208
        )
2209
        content2 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
2210
        content2_nb_rev = len(content2.revisions)
2211
        with pytest.raises(ContentFilenameAlreadyUsedInFolder):
2212
            with new_revision(
2213
                    session=self.session,
2214
                    tm=transaction.manager,
2215
                    content=content2,
2216
            ):
2217
                api2.update_content(
2218
                    content2,
2219
                    'this_is_a_page2',
2220
                    'new content'
2221
                )
2222
            api2.save(content2)
2223
        content3 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
2224
        assert content3.label == 'this_is_a_page'
2225
        assert content2_nb_rev == len(content3.revisions)
2226
2227 View Code Duplication
    def test_unit__update__err__label_dont_change(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2228
        uapi = UserApi(
2229
            session=self.session,
2230
            config=self.app_config,
2231
            current_user=None,
2232
        )
2233
        group_api = GroupApi(
2234
            current_user=None,
2235
            session=self.session,
2236
            config=self.app_config,
2237
        )
2238
        groups = [group_api.get_one(Group.TIM_USER),
2239
                  group_api.get_one(Group.TIM_MANAGER),
2240
                  group_api.get_one(Group.TIM_ADMIN)]
2241
2242
        user1 = uapi.create_minimal_user(
2243
            email='this.is@user',
2244
            groups=groups,
2245
            save_now=True
2246
        )
2247
2248
        workspace_api = WorkspaceApi(
2249
            current_user=user1,
2250
            session=self.session,
2251
            config=self.app_config,
2252
        )
2253
        workspace = workspace_api.create_workspace(
2254
            'test workspace',
2255
            save_now=True
2256
        )
2257
2258
        wid = workspace.workspace_id
2259
2260
        user2 = uapi.create_minimal_user('[email protected]')
2261
        uapi.save(user2)
2262
2263
        RoleApi(
2264
            current_user=user1,
2265
            session=self.session,
2266
            config=self.app_config,
2267
        ).create_one(
2268
            user2,
2269
            workspace,
2270
            UserRoleInWorkspace.CONTENT_MANAGER,
2271
            with_notif=False,
2272
            flush=True
2273
        )
2274
2275
        # Test starts here
2276
2277
        api = ContentApi(
2278
            current_user=user1,
2279
            session=self.session,
2280
            config=self.app_config,
2281
        )
2282
2283
        p = api.create(
2284
            content_type_slug=content_type_list.Page.slug,
2285
            workspace=workspace,
2286
            parent=None,
2287
            label='this_is_a_page',
2288
            do_save=True
2289
        )
2290
        p2 = api.create(
2291
            content_type_slug=content_type_list.Page.slug,
2292
            workspace=workspace,
2293
            parent=None,
2294
            label='this_is_a_page2',
2295
            do_save=True
2296
        )
2297
        u1id = user1.user_id
2298
        u2id = user2.user_id
2299
        pcid = p.content_id
2300
        poid = p.owner_id
2301
2302
        transaction.commit()
2303
2304
        # Refresh instances after commit
2305
        user1 = uapi.get_one(u1id)
2306
        workspace = WorkspaceApi(
2307
            current_user=user1,
2308
            session=self.session,
2309
            config=self.app_config,
2310
        ).get_one(wid)
2311
        api = ContentApi(
2312
            current_user=user1,
2313
            session=self.session,
2314
            config=self.app_config,
2315
        )
2316
2317
        content = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
2318
        eq_(u1id, content.owner_id)
2319
        eq_(poid, content.owner_id)
2320
2321
        u2 = UserApi(
2322
            session=self.session,
2323
            config=self.app_config,
2324
            current_user=None,
2325
        ).get_one(u2id)
2326
        api2 = ContentApi(
2327
            current_user=u2,
2328
            session=self.session,
2329
            config=self.app_config,
2330
        )
2331
        content2 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
2332
        content2_nb_rev = len(content2.revisions)
2333
        with pytest.raises(SameValueError):
2334
            with new_revision(
2335
                    session=self.session,
2336
                    tm=transaction.manager,
2337
                    content=content2,
2338
            ):
2339
                api2.update_content(
2340
                    content2,
2341
                    'this_is_a_page',
2342
                    ''
2343
                )
2344
        api2.save(content2)
2345
        content3 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
2346
        assert content3.label == 'this_is_a_page'
2347
        assert content2_nb_rev == len(content3.revisions)
2348
2349
2350
    def test_update_no_change(self):
2351
        uapi = UserApi(
2352
            session=self.session,
2353
            config=self.app_config,
2354
            current_user=None,
2355
        )
2356
        group_api = GroupApi(
2357
            current_user=None,
2358
            session=self.session,
2359
            config = self.app_config,
2360
        )
2361
        groups = [group_api.get_one(Group.TIM_USER),
2362
                  group_api.get_one(Group.TIM_MANAGER),
2363
                  group_api.get_one(Group.TIM_ADMIN)]
2364
2365
        user1 = uapi.create_minimal_user(
2366
            email='this.is@user',
2367
            groups=groups,
2368
            save_now=True,
2369
        )
2370
2371
        workspace = WorkspaceApi(
2372
            current_user=user1,
2373
            session=self.session,
2374
            config=self.app_config,
2375
        ).create_workspace(
2376
            'test workspace',
2377
            save_now=True
2378
        )
2379
2380
        user2 = uapi.create_minimal_user('[email protected]')
2381
        uapi.save(user2)
2382
2383
        RoleApi(
2384
            current_user=user1,
2385
            session=self.session,
2386
            config=self.app_config,
2387
        ).create_one(
2388
            user2,
2389
            workspace,
2390
            UserRoleInWorkspace.CONTENT_MANAGER,
2391
            with_notif=False,
2392
            flush=True
2393
        )
2394
        api = ContentApi(
2395
            current_user=user1,
2396
            session=self.session,
2397
            config=self.app_config,
2398
        )
2399
        with self.session.no_autoflush:
2400
            page = api.create(
2401
                content_type_slug=content_type_list.Page.slug,
2402
                workspace=workspace,
2403
                label="same_content",
2404
                do_save=False
2405
            )
2406
            page.description = "Same_content_here"
2407
        api.save(page, ActionDescription.CREATION, do_notify=True)
2408
        transaction.commit()
2409
2410
        api2 = ContentApi(
2411
            current_user=user2,
2412
            session=self.session,
2413
            config=self.app_config,
2414
        )
2415
        content2 = api2.get_one(page.content_id, content_type_list.Any_SLUG, workspace)
2416
        content2_nb_rev = len(content2.revisions)
2417
        with new_revision(
2418
           session=self.session,
2419
           tm=transaction.manager,
2420
           content=content2,
2421
        ):
2422
            with pytest.raises(SameValueError):
2423
                api2.update_content(
2424
                    item=content2,
2425
                    new_label='same_content',
2426
                    new_content='Same_content_here'
2427
                )
2428
        api2.save(content2)
2429
        transaction.commit()
2430
        content3 = api2.get_one(page.content_id, content_type_list.Any_SLUG, workspace)
2431
        assert content3.label == 'same_content'
2432
        assert content2_nb_rev == len(content3.revisions)
2433
2434
    def test_update_file_data__ok_nominal(self):
2435
        uapi = UserApi(
2436
            session=self.session,
2437
            config=self.app_config,
2438
            current_user=None,
2439
        )
2440
        group_api = GroupApi(
2441
            current_user=None,
2442
            session=self.session,
2443
            config=self.app_config,
2444
        )
2445
        groups = [group_api.get_one(Group.TIM_USER),
2446
                  group_api.get_one(Group.TIM_MANAGER),
2447
                  group_api.get_one(Group.TIM_ADMIN)]
2448
2449
        user1 = uapi.create_minimal_user(
2450
            email='this.is@user',
2451
            groups=groups,
2452
            save_now=True
2453
        )
2454
2455
        workspace_api = WorkspaceApi(
2456
            current_user=user1,
2457
            session=self.session,
2458
            config=self.app_config,
2459
        )
2460
        workspace = workspace_api.create_workspace(
2461
            'test workspace',
2462
            save_now=True
2463
        )
2464
        wid = workspace.workspace_id
2465
2466
        user2 = uapi.create_minimal_user('[email protected]')
2467
        uapi.save(user2)
2468
2469
        RoleApi(
2470
            current_user=user1,
2471
            session=self.session,
2472
            config=self.app_config,
2473
        ).create_one(
2474
            user2,
2475
            workspace,
2476
            UserRoleInWorkspace.CONTENT_MANAGER,
2477
            with_notif=True,
2478
            flush=True
2479
        )
2480
2481
        # Test starts here
2482
        api = ContentApi(
2483
            current_user=user1,
2484
            session=self.session,
2485
            config=self.app_config,
2486
        )
2487
        p = api.create(
2488
            content_type_slug=content_type_list.File.slug,
2489
            workspace=workspace,
2490
            parent=None,
2491
            label='this_is_a_page',
2492
            do_save=True
2493
        )
2494
2495
        u1id = user1.user_id
2496
        u2id = user2.user_id
2497
        pcid = p.content_id
2498
        poid = p.owner_id
2499
2500
        api.save(p)
2501
        transaction.commit()
2502
2503
        # Refresh instances after commit
2504
        user1 = uapi.get_one(u1id)
2505
        workspace_api2 = WorkspaceApi(
2506
            current_user=user1,
2507
            session=self.session,
2508
            config=self.app_config,
2509
        )
2510
        workspace = workspace_api2.get_one(wid)
2511
        api = ContentApi(
2512
            current_user=user1,
2513
            session=self.session,
2514
            config=self.app_config,
2515
        )
2516
2517
        content = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
2518
        eq_(u1id, content.owner_id)
2519
        eq_(poid, content.owner_id)
2520
2521
        u2 = UserApi(
2522
            current_user=None,
2523
            session=self.session,
2524
            config=self.app_config,
2525
        ).get_one(u2id)
2526
        api2 = ContentApi(
2527
            current_user=u2,
2528
            session=self.session,
2529
            config=self.app_config,
2530
        )
2531
        content2 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
2532
        with new_revision(
2533
            session=self.session,
2534
            tm=transaction.manager,
2535
            content=content2,
2536
        ):
2537
            api2.update_file_data(
2538
                content2,
2539
                'index.html',
2540
                'text/html',
2541
                b'<html>hello world</html>'
2542
            )
2543
        api2.save(content2)
2544
        transaction.commit()
2545
2546
        # Refresh instances after commit
2547
        user1 = uapi.get_one(u1id)
2548
        workspace = WorkspaceApi(
2549
            current_user=user1,
2550
            session=self.session,
2551
            config=self.app_config,
2552
        ).get_one(wid)
2553
2554
        updated = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
2555
        eq_(u2id, updated.owner_id,
2556
            'the owner id should be {} (found {})'.format(u2id,
2557
                                                          updated.owner_id))
2558
        eq_('index.html', updated.file_name)
2559
        eq_('text/html', updated.file_mimetype)
2560
        eq_(b'<html>hello world</html>', updated.depot_file.file.read())
2561
        eq_(ActionDescription.REVISION, updated.revision_type)
2562
2563 View Code Duplication
    def test_update_file_data__err__content_status_closed(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2564
        uapi = UserApi(
2565
            session=self.session,
2566
            config=self.app_config,
2567
            current_user=None,
2568
        )
2569
        group_api = GroupApi(
2570
            current_user=None,
2571
            session=self.session,
2572
            config=self.app_config,
2573
        )
2574
        groups = [group_api.get_one(Group.TIM_USER),
2575
                  group_api.get_one(Group.TIM_MANAGER),
2576
                  group_api.get_one(Group.TIM_ADMIN)]
2577
2578
        user1 = uapi.create_minimal_user(
2579
            email='this.is@user',
2580
            groups=groups,
2581
            save_now=True
2582
        )
2583
2584
        workspace_api = WorkspaceApi(
2585
            current_user=user1,
2586
            session=self.session,
2587
            config=self.app_config,
2588
        )
2589
        workspace = workspace_api.create_workspace(
2590
            'test workspace',
2591
            save_now=True
2592
        )
2593
        wid = workspace.workspace_id
2594
2595
        user2 = uapi.create_minimal_user('[email protected]')
2596
        uapi.save(user2)
2597
2598
        RoleApi(
2599
            current_user=user1,
2600
            session=self.session,
2601
            config=self.app_config,
2602
        ).create_one(
2603
            user2,
2604
            workspace,
2605
            UserRoleInWorkspace.CONTENT_MANAGER,
2606
            with_notif=True,
2607
            flush=True
2608
        )
2609
2610
        # Test starts here
2611
        api = ContentApi(
2612
            current_user=user1,
2613
            session=self.session,
2614
            config=self.app_config,
2615
        )
2616
        p = api.create(
2617
            content_type_slug=content_type_list.File.slug,
2618
            workspace=workspace,
2619
            parent=None,
2620
            label='this_is_a_page',
2621
            do_save=False
2622
        )
2623
        p.status = 'closed-validated'
2624
        api.save(p)
2625
2626
        u1id = user1.user_id
2627
        u2id = user2.user_id
2628
        pcid = p.content_id
2629
        poid = p.owner_id
2630
2631
        api.save(p)
2632
        transaction.commit()
2633
2634
        # Refresh instances after commit
2635
        user1 = uapi.get_one(u1id)
2636
        workspace_api2 = WorkspaceApi(
2637
            current_user=user1,
2638
            session=self.session,
2639
            config=self.app_config,
2640
        )
2641
        workspace = workspace_api2.get_one(wid)
2642
        api = ContentApi(
2643
            current_user=user1,
2644
            session=self.session,
2645
            config=self.app_config,
2646
        )
2647
2648
        content = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
2649
        eq_(u1id, content.owner_id)
2650
        eq_(poid, content.owner_id)
2651
2652
        u2 = UserApi(
2653
            current_user=None,
2654
            session=self.session,
2655
            config=self.app_config,
2656
        ).get_one(u2id)
2657
        api2 = ContentApi(
2658
            current_user=u2,
2659
            session=self.session,
2660
            config=self.app_config,
2661
        )
2662
        content2 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
2663
        content2_nb_rev = len(content2.revisions)
2664
        with pytest.raises(ContentInNotEditableState):
2665
            with new_revision(
2666
                session=self.session,
2667
                tm=transaction.manager,
2668
                content=content2,
2669
            ):
2670
                    api2.update_file_data(
2671
                        content2,
2672
                        'index.html',
2673
                        'text/html',
2674
                        b'<html>hello world</html>'
2675
                    )
2676
        content3 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
2677
        assert content3.label == 'this_is_a_page'
2678
        assert content2_nb_rev == len(content3.revisions)
2679
2680 View Code Duplication
    def test_update_file_data__err__content_archived(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2681
        uapi = UserApi(
2682
            session=self.session,
2683
            config=self.app_config,
2684
            current_user=None,
2685
        )
2686
        group_api = GroupApi(
2687
            current_user=None,
2688
            session=self.session,
2689
            config=self.app_config,
2690
        )
2691
        groups = [group_api.get_one(Group.TIM_USER),
2692
                  group_api.get_one(Group.TIM_MANAGER),
2693
                  group_api.get_one(Group.TIM_ADMIN)]
2694
2695
        user1 = uapi.create_minimal_user(
2696
            email='this.is@user',
2697
            groups=groups,
2698
            save_now=True
2699
        )
2700
2701
        workspace_api = WorkspaceApi(
2702
            current_user=user1,
2703
            session=self.session,
2704
            config=self.app_config,
2705
        )
2706
        workspace = workspace_api.create_workspace(
2707
            'test workspace',
2708
            save_now=True
2709
        )
2710
        wid = workspace.workspace_id
2711
2712
        user2 = uapi.create_minimal_user('[email protected]')
2713
        uapi.save(user2)
2714
2715
        RoleApi(
2716
            current_user=user1,
2717
            session=self.session,
2718
            config=self.app_config,
2719
        ).create_one(
2720
            user2,
2721
            workspace,
2722
            UserRoleInWorkspace.CONTENT_MANAGER,
2723
            with_notif=True,
2724
            flush=True
2725
        )
2726
2727
        # Test starts here
2728
        api = ContentApi(
2729
            current_user=user1,
2730
            session=self.session,
2731
            config=self.app_config,
2732
            show_archived=True,
2733
        )
2734
        p = api.create(
2735
            content_type_slug=content_type_list.File.slug,
2736
            workspace=workspace,
2737
            parent=None,
2738
            label='this_is_a_page',
2739
            do_save=False
2740
        )
2741
        p.is_archived = True
2742
        api.save(p)
2743
2744
        u1id = user1.user_id
2745
        u2id = user2.user_id
2746
        pcid = p.content_id
2747
        poid = p.owner_id
2748
2749
        api.save(p)
2750
        transaction.commit()
2751
2752
        # Refresh instances after commit
2753
        user1 = uapi.get_one(u1id)
2754
        workspace_api2 = WorkspaceApi(
2755
            current_user=user1,
2756
            session=self.session,
2757
            config=self.app_config,
2758
        )
2759
        workspace = workspace_api2.get_one(wid)
2760
        api = ContentApi(
2761
            current_user=user1,
2762
            session=self.session,
2763
            config=self.app_config,
2764
            show_archived=True,
2765
        )
2766
2767
        content = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
2768
        eq_(u1id, content.owner_id)
2769
        eq_(poid, content.owner_id)
2770
2771
        u2 = UserApi(
2772
            current_user=None,
2773
            session=self.session,
2774
            config=self.app_config,
2775
        ).get_one(u2id)
2776
        api2 = ContentApi(
2777
            current_user=u2,
2778
            session=self.session,
2779
            config=self.app_config,
2780
            show_archived=True,
2781
        )
2782
        content2 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
2783
        content2_nb_rev = len(content2.revisions)
2784
        with pytest.raises(ContentInNotEditableState):
2785
            with new_revision(
2786
                session=self.session,
2787
                tm=transaction.manager,
2788
                content=content2,
2789
            ):
2790
                    api2.update_file_data(
2791
                        content2,
2792
                        'index.html',
2793
                        'text/html',
2794
                        b'<html>hello world</html>'
2795
                    )
2796
        content3 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
2797
        assert content3.label == 'this_is_a_page'
2798
        assert content2_nb_rev == len(content3.revisions)
2799
2800 View Code Duplication
    def test_update_file_data__err__content_deleted(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2801
        uapi = UserApi(
2802
            session=self.session,
2803
            config=self.app_config,
2804
            current_user=None,
2805
        )
2806
        group_api = GroupApi(
2807
            current_user=None,
2808
            session=self.session,
2809
            config=self.app_config,
2810
        )
2811
        groups = [group_api.get_one(Group.TIM_USER),
2812
                  group_api.get_one(Group.TIM_MANAGER),
2813
                  group_api.get_one(Group.TIM_ADMIN)]
2814
2815
        user1 = uapi.create_minimal_user(
2816
            email='this.is@user',
2817
            groups=groups,
2818
            save_now=True
2819
        )
2820
2821
        workspace_api = WorkspaceApi(
2822
            current_user=user1,
2823
            session=self.session,
2824
            config=self.app_config,
2825
        )
2826
        workspace = workspace_api.create_workspace(
2827
            'test workspace',
2828
            save_now=True
2829
        )
2830
        wid = workspace.workspace_id
2831
2832
        user2 = uapi.create_minimal_user('[email protected]')
2833
        uapi.save(user2)
2834
2835
        RoleApi(
2836
            current_user=user1,
2837
            session=self.session,
2838
            config=self.app_config,
2839
        ).create_one(
2840
            user2,
2841
            workspace,
2842
            UserRoleInWorkspace.CONTENT_MANAGER,
2843
            with_notif=True,
2844
            flush=True
2845
        )
2846
2847
        # Test starts here
2848
        api = ContentApi(
2849
            current_user=user1,
2850
            session=self.session,
2851
            config=self.app_config,
2852
            show_deleted=True,
2853
        )
2854
        p = api.create(
2855
            content_type_slug=content_type_list.File.slug,
2856
            workspace=workspace,
2857
            parent=None,
2858
            label='this_is_a_page',
2859
            do_save=False
2860
        )
2861
        p.is_deleted = True
2862
        api.save(p)
2863
2864
        u1id = user1.user_id
2865
        u2id = user2.user_id
2866
        pcid = p.content_id
2867
        poid = p.owner_id
2868
2869
        api.save(p)
2870
        transaction.commit()
2871
2872
        # Refresh instances after commit
2873
        user1 = uapi.get_one(u1id)
2874
        workspace_api2 = WorkspaceApi(
2875
            current_user=user1,
2876
            session=self.session,
2877
            config=self.app_config,
2878
        )
2879
        workspace = workspace_api2.get_one(wid)
2880
        api = ContentApi(
2881
            current_user=user1,
2882
            session=self.session,
2883
            config=self.app_config,
2884
            show_deleted=True,
2885
        )
2886
2887
        content = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
2888
        eq_(u1id, content.owner_id)
2889
        eq_(poid, content.owner_id)
2890
2891
        u2 = UserApi(
2892
            current_user=None,
2893
            session=self.session,
2894
            config=self.app_config,
2895
        ).get_one(u2id)
2896
        api2 = ContentApi(
2897
            current_user=u2,
2898
            session=self.session,
2899
            config=self.app_config,
2900
            show_deleted=True,
2901
        )
2902
        content2 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
2903
        content2_nb_rev = len(content2.revisions)
2904
        with pytest.raises(ContentInNotEditableState):
2905
            with new_revision(
2906
                session=self.session,
2907
                tm=transaction.manager,
2908
                content=content2,
2909
            ):
2910
                    api2.update_file_data(
2911
                        content2,
2912
                        'index.html',
2913
                        'text/html',
2914
                        b'<html>hello world</html>'
2915
                    )
2916
        content3 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
2917
        assert content3.label == 'this_is_a_page'
2918
        assert content2_nb_rev == len(content3.revisions)
2919
2920
    @pytest.mark.xfail(reason='Broken feature dues to pyramid behaviour')
2921
    def test_update_no_change(self):
2922
        uapi = UserApi(
2923
            session=self.session,
2924
            config=self.app_config,
2925
            current_user=None,
2926
        )
2927
        group_api = GroupApi(
2928
            current_user=None,
2929
            session=self.session,
2930
            config=self.app_config,
2931
        )
2932
        groups = [group_api.get_one(Group.TIM_USER),
2933
                  group_api.get_one(Group.TIM_MANAGER),
2934
                  group_api.get_one(Group.TIM_ADMIN)]
2935
2936
        user1 = uapi.create_minimal_user(
2937
            email='this.is@user',
2938
            groups=groups,
2939
            save_now=True,
2940
        )
2941
2942
        workspace_api = WorkspaceApi(
2943
            current_user=user1,
2944
            session=self.session,
2945
            config=self.app_config,
2946
        )
2947
        workspace = workspace_api.create_workspace(
2948
            'test workspace',
2949
            save_now=True
2950
        )
2951
2952
        user2 = uapi.create_minimal_user('[email protected]')
2953
        uapi.save(user2)
2954
2955
        RoleApi(
2956
            current_user=user1,
2957
            session=self.session,
2958
            config=self.app_config,
2959
        ).create_one(
2960
            user2,
2961
            workspace,
2962
            UserRoleInWorkspace.CONTENT_MANAGER,
2963
            with_notif=False,
2964
            flush=True
2965
        )
2966
        api = ContentApi(
2967
            current_user=user1,
2968
            session=self.session,
2969
            config=self.app_config,
2970
        )
2971
        with self.session.no_autoflush:
2972
            page = api.create(
2973
                content_type_slug=content_type_list.Page.slug,
2974
                workspace=workspace,
2975
                label="same_content",
2976
                do_save=False
2977
            )
2978
            api.update_file_data(
2979
                page,
2980
                'index.html',
2981
                'text/html',
2982
                b'<html>Same Content Here</html>'
2983
            )
2984
        api.save(page, ActionDescription.CREATION, do_notify=True)
2985
        transaction.commit()
2986
2987
        api2 = ContentApi(
2988
            current_user=user2,
2989
            session=self.session,
2990
            config=self.app_config,
2991
        )
2992
        content2 = api2.get_one(page.content_id, content_type_list.Any_SLUG, workspace)
2993
        content2_nb_rev = len(content2.revisions)
2994
        with new_revision(
2995
            session=self.session,
2996
            tm=transaction.manager,
2997
            content=content2,
2998
        ):
2999
            with pytest.raises(SameValueError):
3000
                api2.update_file_data(
3001
                    page,
3002
                    'index.html',
3003
                    'text/html',
3004
                    b'<html>Same Content Here</html>'
3005
                )
3006
        api2.save(content2)
3007
        transaction.commit()
3008
        content3 = api2.get_one(page.content_id, content_type_list.Any_SLUG, workspace)
3009
        assert content3.label == 'index'
3010
        assert content2_nb_rev == len(content3.revisions)
3011
3012 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...
3013
        uapi = UserApi(
3014
            session=self.session,
3015
            config=self.app_config,
3016
            current_user=None,
3017
        )
3018
        group_api = GroupApi(
3019
            current_user=None,
3020
            session=self.session,
3021
            config=self.app_config,
3022
        )
3023
        groups = [group_api.get_one(Group.TIM_USER),
3024
                  group_api.get_one(Group.TIM_MANAGER),
3025
                  group_api.get_one(Group.TIM_ADMIN)]
3026
3027
        user1 = uapi.create_minimal_user(
3028
            email='this.is@user',
3029
            groups=groups,
3030
            save_now=True
3031
        )
3032
        u1id = user1.user_id
3033
3034
        workspace_api = WorkspaceApi(
3035
            current_user=user1,
3036
            session=self.session,
3037
            config=self.app_config,
3038
        )
3039
        workspace = workspace_api.create_workspace(
3040
            'test workspace',
3041
            save_now=True
3042
        )
3043
        wid = workspace.workspace_id
3044
3045
        user2 = uapi.create_minimal_user('[email protected]')
3046
        uapi.save(user2)
3047
3048
        RoleApi(
3049
            current_user=user1,
3050
            session=self.session,
3051
            config=self.app_config,
3052
        ).create_one(
3053
            user2,
3054
            workspace,
3055
            UserRoleInWorkspace.CONTENT_MANAGER,
3056
            with_notif=True,
3057
            flush=True
3058
        )
3059
3060
        # show archived is used at the top end of the test
3061
        api = ContentApi(
3062
            current_user=user1,
3063
            session=self.session,
3064
            show_archived=True,
3065
            config=self.app_config,
3066
        )
3067
        p = api.create(
3068
            content_type_slug=content_type_list.File.slug,
3069
            workspace=workspace,
3070
            parent=None,
3071
            label='this_is_a_page',
3072
            do_save=True
3073
        )
3074
3075
        u1id = user1.user_id
3076
        u2id = user2.user_id
3077
        pcid = p.content_id
3078
        poid = p.owner_id
3079
3080
        transaction.commit()
3081
3082
        ####
3083
3084
        # refresh after commit
3085
        user1 = UserApi(
3086
            current_user=None,
3087
            config=self.app_config,
3088
            session=self.session
3089
        ).get_one(u1id)
3090
        workspace = WorkspaceApi(
3091
            current_user=user1,
3092
            session=self.session,
3093
            config=self.app_config,
3094
        ).get_one(wid)
3095
3096
        content = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
3097
        eq_(u1id, content.owner_id)
3098
        eq_(poid, content.owner_id)
3099
3100
        u2api = UserApi(
3101
            session=self.session,
3102
            config=self.app_config,
3103
            current_user=None,
3104
        )
3105
        u2 = u2api.get_one(u2id)
3106
        api2 = ContentApi(
3107
            current_user=u2,
3108
            session=self.session,
3109
            config=self.app_config,
3110
            show_archived=True,
3111
        )
3112
        content2 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
3113
        with new_revision(
3114
                session=self.session,
3115
                tm=transaction.manager,
3116
                content=content2,
3117
        ):
3118
            api2.archive(content2)
3119
        api2.save(content2)
3120
        transaction.commit()
3121
3122
        # refresh after commit
3123
        user1 = UserApi(
3124
            current_user=None,
3125
            session=self.session,
3126
            config=self.app_config,
3127
        ).get_one(u1id)
3128
        workspace = WorkspaceApi(
3129
            current_user=user1,
3130
            session=self.session,
3131
            config=self.app_config,
3132
        ).get_one(wid)
3133
        u2 = UserApi(
3134
            current_user=None,
3135
            session=self.session,
3136
            config=self.app_config,
3137
        ).get_one(u2id)
3138
        api = ContentApi(
3139
            current_user=user1,
3140
            session=self.session,
3141
            config=self.app_config,
3142
            show_archived=True,
3143
        )
3144
        api2 = ContentApi(
3145
            current_user=u2,
3146
            session=self.session,
3147
            config=self.app_config,
3148
            show_archived=True,
3149
        )
3150
3151
        updated = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
3152
        eq_(u2id, updated.owner_id,
3153
            'the owner id should be {} (found {})'.format(u2id,
3154
                                                          updated.owner_id))
3155
        eq_(True, updated.is_archived)
3156
        eq_(ActionDescription.ARCHIVING, updated.revision_type)
3157
3158
        ####
3159
3160
        updated2 = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
3161
        with new_revision(
3162
            session=self.session,
3163
            tm=transaction.manager,
3164
            content=updated,
3165
3166
        ):
3167
            api.unarchive(updated)
3168
        api.save(updated2)
3169
        eq_(False, updated2.is_archived)
3170
        eq_(ActionDescription.UNARCHIVING, updated2.revision_type)
3171
        eq_(u1id, updated2.owner_id)
3172
3173 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...
3174
        uapi = UserApi(
3175
            session=self.session,
3176
            config=self.app_config,
3177
            current_user=None,
3178
        )
3179
        group_api = GroupApi(
3180
            current_user=None,
3181
            session=self.session,
3182
            config=self.app_config,
3183
        )
3184
        groups = [group_api.get_one(Group.TIM_USER),
3185
                  group_api.get_one(Group.TIM_MANAGER),
3186
                  group_api.get_one(Group.TIM_ADMIN)]
3187
3188
        user1 = uapi.create_minimal_user(
3189
            email='this.is@user',
3190
            groups=groups,
3191
            save_now=True
3192
        )
3193
        u1id = user1.user_id
3194
3195
        workspace_api = WorkspaceApi(
3196
            current_user=user1,
3197
            session=self.session,
3198
            config=self.app_config,
3199
        )
3200
        workspace = workspace_api.create_workspace(
3201
            'test workspace',
3202
            save_now=True
3203
        )
3204
        wid = workspace.workspace_id
3205
3206
        user2 = uapi.create_minimal_user('[email protected]')
3207
        uapi.save(user2)
3208
3209
        RoleApi(
3210
            current_user=user1,
3211
            session=self.session,
3212
            config=self.app_config,
3213
        ).create_one(
3214
            user2,
3215
            workspace,
3216
            UserRoleInWorkspace.CONTENT_MANAGER,
3217
            with_notif=True,
3218
            flush=True
3219
        )
3220
3221
        # show archived is used at the top end of the test
3222
        api = ContentApi(
3223
            current_user=user1,
3224
            session=self.session,
3225
            config=self.app_config,
3226
            show_deleted=True,
3227
        )
3228
        p = api.create(
3229
            content_type_slug=content_type_list.File.slug,
3230
            workspace=workspace,
3231
            parent=None,
3232
            label='this_is_a_page',
3233
            do_save=True
3234
        )
3235
3236
        u1id = user1.user_id
3237
        u2id = user2.user_id
3238
        pcid = p.content_id
3239
        poid = p.owner_id
3240
3241
        transaction.commit()
3242
3243
        ####
3244
        user1 = UserApi(
3245
            current_user=None,
3246
            session=self.session,
3247
            config=self.app_config,
3248
        ).get_one(u1id)
3249
        workspace = WorkspaceApi(
3250
            current_user=user1,
3251
            session=self.session,
3252
            config=self.app_config,
3253
        ).get_one(wid)
3254
3255
        content = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
3256
        eq_(u1id, content.owner_id)
3257
        eq_(poid, content.owner_id)
3258
3259
        u2 = UserApi(
3260
            current_user=None,
3261
            session=self.session,
3262
            config=self.app_config,
3263
        ).get_one(u2id)
3264
        api2 = ContentApi(
3265
            current_user=u2,
3266
            session=self.session,
3267
            config=self.app_config,
3268
            show_deleted=True,
3269
        )
3270
        content2 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
3271
        with new_revision(
3272
                session=self.session,
3273
                tm=transaction.manager,
3274
                content=content2,
3275
        ):
3276
            api2.delete(content2)
3277
        api2.save(content2)
3278
        transaction.commit()
3279
3280
        ####
3281
3282
        user1 = UserApi(
3283
            current_user=None,
3284
            session=self.session,
3285
            config=self.app_config,
3286
        ).get_one(u1id)
3287
        workspace = WorkspaceApi(
3288
            current_user=user1,
3289
            session=self.session,
3290
            config=self.app_config,
3291
        ).get_one(wid)
3292
        # show archived is used at the top end of the test
3293
        api = ContentApi(
3294
            current_user=user1,
3295
            session=self.session,
3296
            config=self.app_config,
3297
            show_deleted=True,
3298
        )
3299
        u2 = UserApi(
3300
            current_user=None,
3301
            session=self.session,
3302
            config=self.app_config,
3303
        ).get_one(u2id)
3304
        api2 = ContentApi(
3305
            current_user=u2,
3306
            session=self.session,
3307
            config=self.app_config,
3308
            show_deleted=True
3309
        )
3310
3311
        updated = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
3312
        eq_(u2id, updated.owner_id,
3313
            'the owner id should be {} (found {})'.format(u2id,
3314
                                                          updated.owner_id))
3315
        eq_(True, updated.is_deleted)
3316
        eq_(ActionDescription.DELETION, updated.revision_type)
3317
3318
        ####
3319
3320
        updated2 = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
3321
        with new_revision(
3322
            tm=transaction.manager,
3323
            session=self.session,
3324
            content=updated2,
3325
        ):
3326
            api.undelete(updated2)
3327
        api.save(updated2)
3328
        eq_(False, updated2.is_deleted)
3329
        eq_(ActionDescription.UNDELETION, updated2.revision_type)
3330
        eq_(u1id, updated2.owner_id)
3331
3332
    def test_unit__get_last_active__ok__nominal_case(self):
3333
        uapi = UserApi(
3334
            session=self.session,
3335
            config=self.app_config,
3336
            current_user=None,
3337
        )
3338
        group_api = GroupApi(
3339
            current_user=None,
3340
            session=self.session,
3341
            config=self.app_config,
3342
        )
3343
        groups = [group_api.get_one(Group.TIM_USER),
3344
                  group_api.get_one(Group.TIM_MANAGER),
3345
                  group_api.get_one(Group.TIM_ADMIN)]
3346
3347
        user = uapi.create_minimal_user(email='this.is@user',
3348
                                        groups=groups, save_now=True)
3349
        workspace = WorkspaceApi(
3350
            current_user=user,
3351
            session=self.session,
3352
            config=self.app_config,
3353
        ).create_workspace(
3354
            'test workspace',
3355
            save_now=True
3356
        )
3357
        workspace2 = WorkspaceApi(
3358
            current_user=user,
3359
            session=self.session,
3360
            config=self.app_config,
3361
        ).create_workspace(
3362
            'test workspace2',
3363
            save_now=True
3364
        )
3365
3366
        api = ContentApi(
3367
            current_user=user,
3368
            session=self.session,
3369
            config=self.app_config,
3370
        )
3371
        main_folder_workspace2 = api.create(content_type_list.Folder.slug, workspace2, None, 'Hepla', '', True)  # nopep8
3372
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
3373
        # creation order test
3374
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
3375
        secondly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
3376
        # update order test
3377
        firstly_created_but_recently_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
3378
        secondly_created_but_not_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
3379
        with new_revision(
3380
            session=self.session,
3381
            tm=transaction.manager,
3382
            content=firstly_created_but_recently_updated,
3383
        ):
3384
            firstly_created_but_recently_updated.description = 'Just an update'
3385
        api.save(firstly_created_but_recently_updated)
3386
        # comment change order
3387
        firstly_created_but_recently_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
3388
        secondly_created_but_not_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
3389
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
3390
3391
        content_workspace_2 = api.create(content_type_list.Page.slug, workspace2, main_folder_workspace2, 'content_workspace_2', '', True)  # nopep8
3392
        last_actives = api.get_last_active()
3393
        assert len(last_actives) == 9
3394
        # workspace_2 content
3395
        assert last_actives[0] == content_workspace_2
3396
        # comment is newest than page2
3397
        assert last_actives[1] == firstly_created_but_recently_commented
3398
        assert last_actives[2] == secondly_created_but_not_commented
3399
        # last updated content is newer than other one despite creation
3400
        # of the other is more recent
3401
        assert last_actives[3] == firstly_created_but_recently_updated
3402
        assert last_actives[4] == secondly_created_but_not_updated
3403
        # creation order is inverted here as last created is last active
3404
        assert last_actives[5] == secondly_created
3405
        assert last_actives[6] == firstly_created
3406
        # folder subcontent modification does not change folder order
3407
        assert last_actives[7] == main_folder
3408
        # folder subcontent modification does not change folder order
3409
        # (workspace2)
3410
        assert last_actives[8] == main_folder_workspace2
3411
3412
    def test_unit__get_last_active__ok__do_no_show_deleted_archived(self):
3413
        uapi = UserApi(
3414
            session=self.session,
3415
            config=self.app_config,
3416
            current_user=None,
3417
        )
3418
        group_api = GroupApi(
3419
            current_user=None,
3420
            session=self.session,
3421
            config=self.app_config,
3422
        )
3423
        groups = [group_api.get_one(Group.TIM_USER),
3424
                  group_api.get_one(Group.TIM_MANAGER),
3425
                  group_api.get_one(Group.TIM_ADMIN)]
3426
3427
        user = uapi.create_minimal_user(email='this.is@user',
3428
                                        groups=groups, save_now=True)
3429
        workspace = WorkspaceApi(
3430
            current_user=user,
3431
            session=self.session,
3432
            config=self.app_config,
3433
        ).create_workspace(
3434
            'test workspace',
3435
            save_now=True
3436
        )
3437
        workspace2 = WorkspaceApi(
3438
            current_user=user,
3439
            session=self.session,
3440
            config=self.app_config,
3441
        ).create_workspace(
3442
            'test workspace2',
3443
            save_now=True
3444
        )
3445
3446
        api = ContentApi(
3447
            current_user=user,
3448
            session=self.session,
3449
            config=self.app_config,
3450
            show_deleted=False,
3451
            show_archived=False,
3452
        )
3453
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
3454
        archived = api.create(content_type_list.Page.slug, workspace, main_folder, 'archived', '', True)  # nopep8
3455
        deleted = api.create(content_type_list.Page.slug, workspace, main_folder, 'deleted', '', True)  # nopep8
3456
        comment_archived = api.create_comment(workspace, parent=archived, content='just a comment', do_save=True)  # nopep8
3457
        comment_deleted = api.create_comment(workspace, parent=deleted, content='just a comment', do_save=True)  # nopep8
3458
        with new_revision(
3459
            session=self.session,
3460
            tm=transaction.manager,
3461
            content=archived,
3462
        ):
3463
            api.archive(archived)
3464
            api.save(archived)
3465
3466
        with new_revision(
3467
            session=self.session,
3468
            tm=transaction.manager,
3469
            content=deleted,
3470
        ):
3471
            api.delete(deleted)
3472
            api.save(deleted)
3473
        normal = api.create(content_type_list.Page.slug, workspace, main_folder, 'normal', '', True)  # nopep8
3474
        comment_normal = api.create_comment(workspace, parent=normal, content='just a comment', do_save=True)  # nopep8
3475
3476
        last_actives = api.get_last_active()
3477
        assert len(last_actives) == 2
3478
        assert last_actives[0].content_id == normal.content_id
3479
        assert last_actives[1].content_id == main_folder.content_id
3480
3481
3482
        api._show_deleted = True
3483
        api._show_archived = False
3484
        last_actives = api.get_last_active()
3485
        assert len(last_actives) == 3
3486
        assert last_actives[0] == normal
3487
        assert last_actives[1] == deleted
3488
        assert last_actives[2] == main_folder
3489
3490
        api._show_deleted = False
3491
        api._show_archived = True
3492
        last_actives = api.get_last_active()
3493
        assert len(last_actives) == 3
3494
        assert last_actives[0]== normal
3495
        assert last_actives[1] == archived
3496
        assert last_actives[2] == main_folder
3497
3498
        api._show_deleted = True
3499
        api._show_archived = True
3500
        last_actives = api.get_last_active()
3501
        assert len(last_actives) == 4
3502
        assert last_actives[0] == normal
3503
        assert last_actives[1] == deleted
3504
        assert last_actives[2] == archived
3505
        assert last_actives[3] == main_folder
3506
3507
    def test_unit__get_last_active__ok__workspace_filter_workspace_full(self):
3508
        uapi = UserApi(
3509
            session=self.session,
3510
            config=self.app_config,
3511
            current_user=None,
3512
        )
3513
        group_api = GroupApi(
3514
            current_user=None,
3515
            session=self.session,
3516
            config=self.app_config,
3517
        )
3518
        groups = [group_api.get_one(Group.TIM_USER),
3519
                  group_api.get_one(Group.TIM_MANAGER),
3520
                  group_api.get_one(Group.TIM_ADMIN)]
3521
3522
        user = uapi.create_minimal_user(email='this.is@user',
3523
                                        groups=groups, save_now=True)
3524
        workspace = WorkspaceApi(
3525
            current_user=user,
3526
            session=self.session,
3527
            config=self.app_config,
3528
        ).create_workspace(
3529
            'test workspace',
3530
            save_now=True
3531
        )
3532
3533
        api = ContentApi(
3534
            current_user=user,
3535
            session=self.session,
3536
            config=self.app_config,
3537
        )
3538
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
3539
        # creation order test
3540
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
3541
        secondly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
3542
        # update order test
3543
        firstly_created_but_recently_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
3544
        secondly_created_but_not_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
3545
        with new_revision(
3546
            session=self.session,
3547
            tm=transaction.manager,
3548
            content=firstly_created_but_recently_updated,
3549
        ):
3550
            firstly_created_but_recently_updated.description = 'Just an update'
3551
        api.save(firstly_created_but_recently_updated)
3552
        # comment change order
3553
        firstly_created_but_recently_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
3554
        secondly_created_but_not_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
3555
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
3556
3557
        last_actives = api.get_last_active(workspace=workspace)
3558
        assert len(last_actives) == 7
3559
        # comment is newest than page2
3560
        assert last_actives[0] == firstly_created_but_recently_commented
3561
        assert last_actives[1] == secondly_created_but_not_commented
3562
        # last updated content is newer than other one despite creation
3563
        # of the other is more recent
3564
        assert last_actives[2] == firstly_created_but_recently_updated
3565
        assert last_actives[3] == secondly_created_but_not_updated
3566
        # creation order is inverted here as last created is last active
3567
        assert last_actives[4] == secondly_created
3568
        assert last_actives[5] == firstly_created
3569
        # folder subcontent modification does not change folder order
3570
        assert last_actives[6] == main_folder
3571
3572
    def test_unit__get_last_active__ok__workspace_filter_workspace_content_ids(self):
3573
        uapi = UserApi(
3574
            session=self.session,
3575
            config=self.app_config,
3576
            current_user=None,
3577
        )
3578
        group_api = GroupApi(
3579
            current_user=None,
3580
            session=self.session,
3581
            config=self.app_config,
3582
        )
3583
        groups = [group_api.get_one(Group.TIM_USER),
3584
                  group_api.get_one(Group.TIM_MANAGER),
3585
                  group_api.get_one(Group.TIM_ADMIN)]
3586
3587
        user = uapi.create_minimal_user(email='this.is@user',
3588
                                        groups=groups, save_now=True)
3589
        workspace = WorkspaceApi(
3590
            current_user=user,
3591
            session=self.session,
3592
            config=self.app_config,
3593
        ).create_workspace(
3594
            'test workspace',
3595
            save_now=True
3596
        )
3597
3598
        api = ContentApi(
3599
            current_user=user,
3600
            session=self.session,
3601
            config=self.app_config,
3602
        )
3603
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
3604
        # creation order test
3605
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
3606
        secondly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
3607
        # update order test
3608
        firstly_created_but_recently_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
3609
        secondly_created_but_not_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
3610
        with new_revision(
3611
            session=self.session,
3612
            tm=transaction.manager,
3613
            content=firstly_created_but_recently_updated,
3614
        ):
3615
            firstly_created_but_recently_updated.description = 'Just an update'
3616
        api.save(firstly_created_but_recently_updated)
3617
        # comment change order
3618
        firstly_created_but_recently_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
3619
        secondly_created_but_not_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
3620
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
3621
3622
        selected_contents = [
3623
            firstly_created_but_recently_commented,
3624
            firstly_created_but_recently_updated,
3625
            firstly_created,
3626
            main_folder,
3627
        ]
3628
        content_ids = [content.content_id for content in selected_contents]
3629
        last_actives = api.get_last_active(
3630
            workspace=workspace,
3631
            content_ids=content_ids,
3632
        )
3633
        assert len(last_actives) == 4
3634
        # comment is newest than page2
3635
        assert last_actives[0] == firstly_created_but_recently_commented
3636
        assert secondly_created_but_not_commented not in last_actives
3637
        # last updated content is newer than other one despite creation
3638
        # of the other is more recent
3639
        assert last_actives[1] == firstly_created_but_recently_updated
3640
        assert secondly_created_but_not_updated not in last_actives
3641
        # creation order is inverted here as last created is last active
3642
        assert secondly_created not in last_actives
3643
        assert last_actives[2] == firstly_created
3644
        # folder subcontent modification does not change folder order
3645
        assert last_actives[3] == main_folder
3646
3647
    def test_unit__get_last_active__ok__workspace_filter_workspace_limit_2_multiples_times(self):  # nopep8
3648
        uapi = UserApi(
3649
            session=self.session,
3650
            config=self.app_config,
3651
            current_user=None,
3652
        )
3653
        group_api = GroupApi(
3654
            current_user=None,
3655
            session=self.session,
3656
            config=self.app_config,
3657
        )
3658
        groups = [group_api.get_one(Group.TIM_USER),
3659
                  group_api.get_one(Group.TIM_MANAGER),
3660
                  group_api.get_one(Group.TIM_ADMIN)]
3661
3662
        user = uapi.create_minimal_user(email='this.is@user',
3663
                                        groups=groups, save_now=True)
3664
        workspace = WorkspaceApi(
3665
            current_user=user,
3666
            session=self.session,
3667
            config=self.app_config,
3668
        ).create_workspace(
3669
            'test workspace',
3670
            save_now=True
3671
        )
3672
3673
        api = ContentApi(
3674
            current_user=user,
3675
            session=self.session,
3676
            config=self.app_config,
3677
        )
3678
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
3679
        # creation order test
3680
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
3681
        secondly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
3682
        # update order test
3683
        firstly_created_but_recently_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
3684
        secondly_created_but_not_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
3685
        with new_revision(
3686
            session=self.session,
3687
            tm=transaction.manager,
3688
            content=firstly_created_but_recently_updated,
3689
        ):
3690
            firstly_created_but_recently_updated.description = 'Just an update'
3691
        api.save(firstly_created_but_recently_updated)
3692
        # comment change order
3693
        firstly_created_but_recently_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
3694
        secondly_created_but_not_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
3695
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
3696
3697
        last_actives = api.get_last_active(workspace=workspace, limit=2)  # nopep8
3698
        assert len(last_actives) == 2
3699
        # comment is newest than page2
3700
        assert last_actives[0] == firstly_created_but_recently_commented
3701
        assert last_actives[1] == secondly_created_but_not_commented
3702
3703
        last_actives = api.get_last_active(workspace=workspace, limit=2, before_content=last_actives[1])  # nopep8
3704
        assert len(last_actives) == 2
3705
        # last updated content is newer than other one despite creation
3706
        # of the other is more recent
3707
        assert last_actives[0] == firstly_created_but_recently_updated
3708
        assert last_actives[1] == secondly_created_but_not_updated
3709
3710
        last_actives = api.get_last_active(workspace=workspace, limit=2, before_content=last_actives[1])  # nopep8
3711
        assert len(last_actives) == 2
3712
        # creation order is inverted here as last created is last active
3713
        assert last_actives[0] == secondly_created
3714
        assert last_actives[1] == firstly_created
3715
3716
        last_actives = api.get_last_active(workspace=workspace, limit=2, before_content=last_actives[1])  # nopep8
3717
        assert len(last_actives) == 1
3718
        # folder subcontent modification does not change folder order
3719
        assert last_actives[0] == main_folder
3720
3721
    def test_unit__get_last_active__ok__workspace_filter_workspace_empty(self):
3722
        uapi = UserApi(
3723
            session=self.session,
3724
            config=self.app_config,
3725
            current_user=None,
3726
        )
3727
        group_api = GroupApi(
3728
            current_user=None,
3729
            session=self.session,
3730
            config=self.app_config,
3731
        )
3732
        groups = [group_api.get_one(Group.TIM_USER),
3733
                  group_api.get_one(Group.TIM_MANAGER),
3734
                  group_api.get_one(Group.TIM_ADMIN)]
3735
3736
        user = uapi.create_minimal_user(email='this.is@user',
3737
                                        groups=groups, save_now=True)
3738
        workspace = WorkspaceApi(
3739
            current_user=user,
3740
            session=self.session,
3741
            config=self.app_config,
3742
        ).create_workspace(
3743
            'test workspace',
3744
            save_now=True
3745
        )
3746
        workspace2 = WorkspaceApi(
3747
            current_user=user,
3748
            session=self.session,
3749
            config=self.app_config,
3750
        ).create_workspace(
3751
            'test workspace2',
3752
            save_now=True
3753
        )
3754
        api = ContentApi(
3755
            current_user=user,
3756
            session=self.session,
3757
            config=self.app_config,
3758
        )
3759
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
3760
        # creation order test
3761
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
3762
        secondly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
3763
        # update order test
3764
        firstly_created_but_recently_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
3765
        secondly_created_but_not_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
3766
        with new_revision(
3767
            session=self.session,
3768
            tm=transaction.manager,
3769
            content=firstly_created_but_recently_updated,
3770
        ):
3771
            firstly_created_but_recently_updated.description = 'Just an update'
3772
        api.save(firstly_created_but_recently_updated)
3773
        # comment change order
3774
        firstly_created_but_recently_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
3775
        secondly_created_but_not_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
3776
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
3777
3778
        last_actives = api.get_last_active(workspace=workspace2)
3779
        assert len(last_actives) == 0
3780
3781 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...
3782
        # HACK - D.A. - 2015-03-09
3783
        # This test is based on a bug which does NOT return results found
3784
        # at root of a workspace (eg a folder)
3785
        uapi = UserApi(
3786
            session=self.session,
3787
            config=self.app_config,
3788
            current_user=None,
3789
        )
3790
        group_api = GroupApi(
3791
            current_user=None,
3792
            session=self.session,
3793
            config=self.app_config,
3794
        )
3795
        groups = [group_api.get_one(Group.TIM_USER),
3796
                  group_api.get_one(Group.TIM_MANAGER),
3797
                  group_api.get_one(Group.TIM_ADMIN)]
3798
3799
        user = uapi.create_minimal_user(email='this.is@user',
3800
                                        groups=groups, save_now=True)
3801
3802
        workspace = WorkspaceApi(
3803
            current_user=user,
3804
            session=self.session,
3805
            config=self.app_config,
3806
        ).create_workspace(
3807
            'test workspace',
3808
            save_now=True
3809
        )
3810
3811
        api = ContentApi(
3812
            current_user=user, 
3813
            session=self.session,
3814
            config=self.app_config,
3815
        )
3816
        a = api.create(content_type_list.Folder.slug, workspace, None,
3817
                       'this is randomized folder', '', True)
3818
        p = api.create(content_type_list.Page.slug, workspace, a,
3819
                       'this is randomized label content', '', True)
3820
3821
        with new_revision(
3822
            session=self.session,
3823
            tm=transaction.manager,
3824
            content=p,
3825
        ):
3826
            p.description = 'This is some amazing test'
3827
3828
        api.save(p)
3829
        original_id = p.content_id
3830
3831
        res = api.search(['randomized'])
3832
        eq_(1, len(res.all()))
3833
        item = res.all()[0]
3834
        eq_(original_id, item.content_id)
3835
3836 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...
3837
        # HACK - D.A. - 2015-03-09
3838
        # This test is based on a bug which does NOT return results found
3839
        # at root of a workspace (eg a folder)
3840
3841
        uapi = UserApi(
3842
            session=self.session,
3843
            config=self.app_config,
3844
            current_user=None,
3845
        )
3846
        group_api = GroupApi(
3847
            current_user=None,
3848
            session=self.session,
3849
            config=self.app_config,
3850
        )
3851
        groups = [group_api.get_one(Group.TIM_USER),
3852
                  group_api.get_one(Group.TIM_MANAGER),
3853
                  group_api.get_one(Group.TIM_ADMIN)]
3854
3855
        user = uapi.create_minimal_user(email='this.is@user',
3856
                                        groups=groups, save_now=True)
3857
3858
        workspace = WorkspaceApi(
3859
            current_user=user,
3860
            session=self.session,
3861
            config=self.app_config,
3862
        ).create_workspace(
3863
            'test workspace',
3864
            save_now=True,
3865
        )
3866
3867
        api = ContentApi(
3868
            current_user=user, 
3869
            session=self.session,
3870
            config=self.app_config,
3871
        )
3872
        a = api.create(content_type_list.Folder.slug, workspace, None,
3873
                       'this is randomized folder', '', True)
3874
        p = api.create(content_type_list.Page.slug, workspace, a,
3875
                       'this is dummy label content', '', True)
3876
3877
        with new_revision(
3878
            tm=transaction.manager,
3879
            session=self.session,
3880
            content=p,
3881
        ):
3882
            p.description = 'This is some amazing test'
3883
3884
        api.save(p)
3885
        original_id = p.content_id
3886
3887
        res = api.search(['dummy'])
3888
        eq_(1, len(res.all()))
3889
        item = res.all()[0]
3890
        eq_(original_id, item.content_id)
3891
3892
    def test_search_in_label_or_description(self):
3893
        # HACK - D.A. - 2015-03-09
3894
        # This test is based on a bug which does NOT return results found
3895
        # at root of a workspace (eg a folder)
3896
3897
        uapi = UserApi(
3898
            session=self.session,
3899
            config=self.app_config,
3900
            current_user=None,
3901
        )
3902
        group_api = GroupApi(
3903
            current_user=None,
3904
            session=self.session,
3905
            config=self.app_config,
3906
        )
3907
        groups = [group_api.get_one(Group.TIM_USER),
3908
                  group_api.get_one(Group.TIM_MANAGER),
3909
                  group_api.get_one(Group.TIM_ADMIN)]
3910
3911
        user = uapi.create_minimal_user(email='this.is@user',
3912
                                        groups=groups, save_now=True)
3913
3914
        workspace = WorkspaceApi(
3915
            current_user=user,
3916
            session=self.session,
3917
            config=self.app_config,
3918
        ).create_workspace('test workspace', save_now=True)
3919
3920
        api = ContentApi(
3921
            current_user=user, 
3922
            session=self.session,
3923
            config=self.app_config,
3924
        )
3925
        a = api.create(
3926
            content_type_slug=content_type_list.Folder.slug,
3927
            workspace=workspace,
3928
            parent=None,
3929
            label='this is randomized folder',
3930
            do_save=True
3931
        )
3932
        p1 = api.create(
3933
            content_type_slug=content_type_list.Page.slug,
3934
            workspace=workspace,
3935
            parent=a,
3936
            label='this is dummy label content',
3937
            do_save=True
3938
        )
3939
        p2 = api.create(
3940
            content_type_slug=content_type_list.Page.slug,
3941
            workspace=workspace,
3942
            parent=a,
3943
            label='Hey ! Jon !',
3944
            do_save=True
3945
        )
3946
3947
        with new_revision(
3948
            session=self.session,
3949
            tm=transaction.manager,
3950
            content=p1,
3951
        ):
3952
            p1.description = 'This is some amazing test'
3953
3954
        with new_revision(
3955
            session=self.session,
3956
            tm=transaction.manager,
3957
            content=p2,
3958
        ):
3959
            p2.description = 'What\'s up?'
3960
3961
        api.save(p1)
3962
        api.save(p2)
3963
3964
        id1 = p1.content_id
3965
        id2 = p2.content_id
3966
3967
        eq_(1, self.session.query(Workspace).filter(Workspace.label == 'test workspace').count())
3968
        eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'this is randomized folder').count())
3969
        eq_(2, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'this is dummy label content').count())
3970
        eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.description == 'This is some amazing test').count())
3971
        eq_(2, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'Hey ! Jon !').count())
3972
        eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.description == 'What\'s up?').count())
3973
3974
        res = api.search(['dummy', 'jon'])
3975
        eq_(2, len(res.all()))
3976
3977
        eq_(True, id1 in [o.content_id for o in res.all()])
3978
        eq_(True, id2 in [o.content_id for o in res.all()])
3979
3980
    def test_unit__search_exclude_content_under_deleted_or_archived_parents__ok(self):  # nopep8
3981
        admin = self.session.query(User)\
3982
            .filter(User.email == '[email protected]').one()
3983
        workspace = self._create_workspace_and_test(
3984
            'workspace_1',
3985
            admin
3986
        )
3987
        folder_1 = self._create_content_and_test(
3988
            'folder_1',
3989
            workspace=workspace,
3990
            type=content_type_list.Folder.slug
3991
        )
3992
        folder_2 = self._create_content_and_test(
3993
            'folder_2',
3994
            workspace=workspace,
3995
            type=content_type_list.Folder.slug
3996
        )
3997
        page_1 = self._create_content_and_test(
3998
            'foo', workspace=workspace,
3999
            type=content_type_list.Page.slug,
4000
            parent=folder_1
4001
        )
4002
        page_2 = self._create_content_and_test(
4003
            'bar',
4004
            workspace=workspace,
4005
            type=content_type_list.Page.slug,
4006
            parent=folder_2
4007
        )
4008
4009
        api = ContentApi(
4010
            current_user=admin,
4011
            session=self.session,
4012
            config=self.app_config,
4013
        )
4014
4015
        foo_result = api.search(['foo']).all()
4016
        eq_(1, len(foo_result))
4017
        assert page_1 in foo_result
4018
4019
        bar_result = api.search(['bar']).all()
4020
        eq_(1, len(bar_result))
4021
        assert page_2 in bar_result
4022
4023
        with new_revision(
4024
            session=self.session,
4025
            tm=transaction.manager,
4026
            content=folder_1,
4027
        ):
4028
            api.delete(folder_1)
4029
        with new_revision(
4030
            session=self.session,
4031
            tm=transaction.manager,
4032
            content=folder_2,
4033
        ):
4034
            api.archive(folder_2)
4035
4036
        # Actually ContentApi.search don't filter it
4037
        foo_result = api.search(['foo']).all()
4038
        eq_(1, len(foo_result))
4039
        assert page_1 in foo_result
4040
4041
        bar_result = api.search(['bar']).all()
4042
        eq_(1, len(bar_result))
4043
        assert page_2 in bar_result
4044
4045
        # ContentApi offer exclude_unavailable method to do it
4046
        foo_result = api.search(['foo']).all()
4047
        api.exclude_unavailable(foo_result)
4048
        eq_(0, len(foo_result))
4049
4050
        bar_result = api.search(['bar']).all()
4051
        api.exclude_unavailable(bar_result)
4052
        eq_(0, len(bar_result))
4053
4054
4055
class TestContentApiSecurity(DefaultTest):
4056
    fixtures = [FixtureTest, ]
4057
4058
    def test_unit__cant_get_non_access_content__ok__nominal_case(self):
4059
        admin = self.session.query(User)\
4060
            .filter(User.email == '[email protected]').one()
4061
        bob = self.session.query(User)\
4062
            .filter(User.email == '[email protected]').one()
4063
4064
        bob_workspace = WorkspaceApi(
4065
            current_user=bob,
4066
            session=self.session,
4067
            config=self.app_config,
4068
        ).create_workspace(
4069
            'bob_workspace',
4070
            save_now=True,
4071
        )
4072
        admin_workspace = WorkspaceApi(
4073
            current_user=admin,
4074
            session=self.session,
4075
            config=self.app_config,
4076
        ).create_workspace(
4077
            'admin_workspace',
4078
            save_now=True,
4079
        )
4080
4081
        bob_page = ContentApi(
4082
            current_user=bob,
4083
            session=self.session,
4084
            config=self.app_config,
4085
        ).create(
4086
            content_type_slug=content_type_list.Page.slug,
4087
            workspace=bob_workspace,
4088
            label='bob_page',
4089
            do_save=True,
4090
        )
4091
4092
        admin_page = ContentApi(
4093
            current_user=admin,
4094
            session=self.session,
4095
            config=self.app_config,
4096
        ).create(
4097
            content_type_slug=content_type_list.Page.slug,
4098
            workspace=admin_workspace,
4099
            label='admin_page',
4100
            do_save=True,
4101
        )
4102
4103
        bob_viewable = ContentApi(
4104
            current_user=bob,
4105
            session=self.session,
4106
            config=self.app_config,
4107
        ).get_all()
4108
        eq_(1, len(bob_viewable), 'Bob should view only one content')
4109
        eq_(
4110
            'bob_page',
4111
            bob_viewable[0].label,
4112
            'Bob should not view "{0}" content'.format(
4113
                bob_viewable[0].label,
4114
            )
4115
        )
4116