Passed
Pull Request — develop (#31)
by Bastien
01:43
created

backend.tracim_backend.tests.functional.test_contents   F

Complexity

Total Complexity 103

Size/Duplication

Total Lines 4081
Duplicated Lines 35.48 %

Importance

Changes 0
Metric Value
wmc 103
eloc 3080
dl 1448
loc 4081
rs 0.8
c 0
b 0
f 0

81 Methods

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