Completed
Pull Request — develop (#31)
by inkhey
20:37 queued 14:44
created

backend.tracim_backend.tests.functional.test_contents   F

Complexity

Total Complexity 76

Size/Duplication

Total Lines 2942
Duplicated Lines 29.74 %

Importance

Changes 0
Metric Value
wmc 76
eloc 2183
dl 875
loc 2942
rs 1.12
c 0
b 0
f 0

64 Methods

Rating   Name   Duplication   Size   Complexity  
A TestHtmlDocuments.test_api__set_html_document_status__err_400__wrong_status() 0 18 1
B TestFolder.test_api__get_folder__err_400__content_not_in_workspace() 0 54 1
B TestFiles.test_api__get_jpeg_preview__ok__200__nominal_case() 58 58 1
A TestThreads.test_api__get_thread__err_400__workspace_does_not_exist() 0 14 1
A TestFolder.test_api__get_folder__err_400__wrong_content_type() 0 40 1
A TestFolder.test_api__get_folder__err_400__workspace_id_is_not_int() 0 40 1
A TestThreads.test_api__get_thread__ok_200__nominal_case() 40 40 1
A TestFolder.test_api__get_folder__err_400__workspace_does_not_exist() 0 40 1
A TestHtmlDocuments.test_api__get_html_document__err_400__content_does_not_exist() 0 14 1
B TestFiles.test_api__get_full_pdf_preview__ok__200__nominal_case() 62 62 2
A TestFiles.test_api__get_file__err_400__content_id_is_not_int() 0 14 1
C TestThreads.test_api__get_thread_revisions__ok_200__most_revision_type() 0 99 7
A TestHtmlDocuments.test_api__get_html_document__ok_200__legacy_slug() 0 41 1
A TestHtmlDocuments.test_api__update_html_document__err_400__empty_label() 0 19 1
A TestThreads.test_api__set_thread_status__ok_200__nominal_case() 41 41 1
A TestFiles.test_api__get_file__err_400__workspace_id_is_not_int() 0 14 1
A TestHtmlDocuments.test_api__get_html_document__err_400__workspace_does_not_exist() 0 14 1
B TestFolder.test_api__get_folder_revisions__ok_200__nominal_case() 0 170 4
B TestFiles.test_api__set_file_raw__ok_200__nominal_case() 0 53 1
A TestThreads.test_api__get_thread__err_400__wrong_content_type() 0 14 1
A TestFiles.test_api__get_files__err_400__wrong_content_type() 0 14 1
A TestFiles.test_api__set_file_status__err_400__wrong_status() 0 18 1
B TestFiles.test_api__get_sized_jpeg_preview__ok__200__nominal_case() 54 54 1
B TestFiles.test_api__set_file_status__ok_200__nominal_case() 0 75 1
B TestFolder.test_api__set_folder_status__ok_200__nominal_case() 0 77 1
B TestFiles.test_api__update_file_info_err_400__empty_label() 0 53 1
B TestFiles.test_api__get_allowed_size_dim__ok__nominal_case() 0 51 1
B TestFiles.test_api__get_file_revisions__ok_200__nominal_case() 74 74 1
B TestThreads.test_api__get_thread_revisions__ok_200__nominal_case() 0 61 1
A TestThreads.test_api__update_thread__err_400__empty_label() 0 19 1
B TestFiles.test_api__get_file__ok_200__nominal_case() 71 71 1
A TestThreads.test_api__get_thread__err_400__content_not_in_workspace() 0 14 1
A TestFiles.test_api__get_file__err_400__workspace_does_not_exist() 0 14 1
B TestFiles.test_api__get_pdf_revision_preview__ok__200__nominal_case() 0 71 1
A TestFolder.test_api__get_folder__err_400__content_id_is_not_int() 0 41 1
A TestThreads.test_api__set_thread_status__ok_400__wrong_status() 0 19 1
B TestFiles.test_api__get_sized_jpeg_preview__err__400__SizeNotAllowed() 49 49 1
A TestThreads.test_api__get_thread__err_400_content_id_is_not_int() 0 14 1
A TestHtmlDocuments.test_api__get_html_document__err_400__content_id_is_not_int() 0 14 1
A TestHtmlDocuments.test_api__get_html_document__ok_200__deleted_content() 0 23 1
B TestFiles.test_api__get_full_pdf_preview__err__400__png_UnavailablePreviewType() 49 49 1
A TestFolder.test_api__get_folder__err_400__content_does_not_exist() 0 33 1
A TestHtmlDocuments.test_api__get_html_document__err_400__workspace_id_is_not_int() 0 14 1
B TestFiles.test_api__update_file_info__ok_200__nominal_case() 0 103 1
B TestFiles.test_api__get_sized_jpeg_revision_preview__ok__200__nominal_case() 0 72 1
A TestHtmlDocuments.test_api__get_html_document__ok_200__nominal_case() 40 40 1
B TestFiles.test_api__get_file_raw__ok_200__nominal_case() 0 52 1
A TestFiles.test_api__get_file__err_400__content_not_in_workspace() 0 14 1
A TestHtmlDocuments.test_api__get_html_document__err_400__content_not_in_workspace() 0 14 1
B TestFolder.test_api__update_folder__err_400__empty_label() 49 49 1
B TestHtmlDocuments.test_api__get_html_document_revisions__ok_200__nominal_case() 0 85 1
A TestHtmlDocuments.test_api__get_html_document__ok_200__archived_content() 0 23 1
B TestFiles.test_api__get_pdf_preview__ok__err__400_page_of_preview_not_found() 62 62 2
A TestHtmlDocuments.test_api__get_html_document__err_400__wrong_content_type() 0 14 1
B TestHtmlDocuments.test_api__update_html_document__ok_200__nominal_case() 69 69 1
B TestThreads.test_api__update_thread__ok_200__nominal_case() 69 69 1
B TestFiles.test_api__get_pdf_preview__ok__200__nominal_case() 0 64 2
A TestFiles.test_api__get_file__err_400__content_does_not_exist() 0 14 1
B TestFolder.test_api__get_folder__ok_200__nominal_case() 0 69 1
B TestFolder.test_api__update_folder__ok_200__nominal_case() 0 73 1
A TestThreads.test_api__get_thread__err_400__content_does_not_exist() 0 14 1
A TestHtmlDocuments.test_api__set_html_document_status__ok_200__nominal_case() 41 41 1
A TestFolder.test_api__set_folder_status__err_400__wrong_status() 47 47 1
A TestThreads.test_api__get_thread__err_400__workspace_id_is_not_int() 0 14 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

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

Common duplication problems, and corresponding solutions are:

Complexity

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

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

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

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