backend.tracim_backend.tests.functional.test_contents   F
last analyzed

Complexity

Total Complexity 141

Size/Duplication

Total Lines 5678
Duplicated Lines 26.51 %

Importance

Changes 0
Metric Value
wmc 141
eloc 4376
dl 1505
loc 5678
rs 0.8
c 0
b 0
f 0

99 Methods

Rating   Name   Duplication   Size   Complexity  
A TestHtmlDocuments.test_api__set_html_document_status__err_400__wrong_status() 0 22 1
B TestFolder.test_api__get_folder__err_400__content_not_in_workspace() 0 58 1
A TestFolder.test_api__get_folder__err_400__wrong_content_type() 0 47 1
A TestFolder.test_api__get_folder__err_400__workspace_id_is_not_int() 44 44 1
A TestFolder.test_api__get_folder__err_400__workspace_does_not_exist() 44 44 1
A TestHtmlDocuments.test_api__get_html_document__err_400__content_does_not_exist() 0 18 1
A TestHtmlDocuments.test_api__get_html_document__ok_200__legacy_slug() 0 43 1
A TestHtmlDocuments.test_api__update_html_document__err_400__empty_label() 0 24 1
B TestFolder.test_api__update_folder__err_400__label_already_used() 0 60 1
A TestHtmlDocuments.test_api__update_html_document__err_400__not_editable() 0 32 1
A TestHtmlDocuments.test_api__get_html_document__err_400__workspace_does_not_exist() 0 18 1
B TestFolder.test_api__get_folder_revisions__ok_200__nominal_case() 0 174 4
B TestFolder.test_api__set_folder_status__ok_200__nominal_case() 0 77 1
A TestFolder.test_api__get_folder__err_400__content_id_is_not_int() 45 45 1
A TestHtmlDocuments.test_api__get_html_document__err_400__content_id_is_not_int() 0 18 1
A TestHtmlDocuments.test_api__get_html_document__ok_200__deleted_content() 0 23 1
A TestFolder.test_api__get_folder__err_400__content_does_not_exist() 0 37 1
A TestHtmlDocuments.test_api__get_html_document__err_400__workspace_id_is_not_int() 0 18 1
B TestFolder.test_api__update_folder__err_400__not_modified() 0 86 1
A TestHtmlDocuments.test_api__get_html_document__ok_200__nominal_case() 42 42 1
A TestHtmlDocuments.test_api__get_html_document__err_400__content_not_in_workspace() 0 18 1
B TestFolder.test_api__update_folder__err_400__empty_label() 54 54 1
B TestHtmlDocuments.test_api__get_html_document_revisions__ok_200__nominal_case() 0 89 1
B TestHtmlDocuments.test_api__update_html_document__err_400__not_modified() 80 80 1
A TestHtmlDocuments.test_api__get_html_document__ok_200__archived_content() 0 23 1
B TestHtmlDocuments.test_api__update_html_document__ok_200__nominal_case() 0 73 1
A TestHtmlDocuments.test_api__get_html_document__err_400__wrong_content_type() 0 18 1
B TestFolder.test_api__get_folder__ok_200__nominal_case() 0 70 1
B TestFolder.test_api__update_folder__ok_200__nominal_case() 0 74 1
A TestHtmlDocuments.test_api__set_html_document_status__ok_200__nominal_case() 0 41 1
B TestFolder.test_api__set_folder_status__err_400__wrong_status() 52 52 1
B TestFiles.test_api__get_sized_jpeg_preview__ok__200__force_download_case() 60 60 1
B TestFiles.test_api__get_jpeg_preview__ok__200__nominal_case() 58 58 1
A TestThreads.test_api__get_thread__err_400__workspace_does_not_exist() 0 18 1
B TestFiles.test_api__create_file__err_400__filename_already_used() 0 59 1
A TestThreads.test_api__get_thread__ok_200__nominal_case() 43 43 1
B TestFiles.test_api__set_file_raw__err_400__closed_status_file() 51 51 1
B TestFiles.test_api__get_file_raw__ok_200__force_download_case() 0 63 2
B TestFiles.test_api__update_file_info__err_400__content_archived() 0 66 3
B TestFiles.test_api__get_full_pdf_preview__ok__200__nominal_case() 63 63 2
A TestFiles.test_api__create_file__err__400__parent_not_found() 0 42 1
A TestFiles.test_api__get_file__err_400__content_id_is_not_int() 0 18 1
C TestThreads.test_api__get_thread_revisions__ok_200__most_revision_type() 0 107 7
B TestFiles.test_api__set_file_raw__err_400_not_modified() 0 69 1
B TestFiles.test_api__get_jpeg_preview__ok__200__force_download_case() 64 64 1
A TestThreads.test_api__set_thread_status__ok_200__nominal_case() 0 42 1
B TestFiles.test_api__get_full_pdf_preview__err__400__png_UnavailablePreview() 57 57 2
A TestFiles.test_api__get_file__err_400__workspace_id_is_not_int() 0 18 1
B TestFiles.test_api__update_file_info__err_400__content_deleted() 0 66 3
B TestFiles.test_api__get_file__ok_200__no_file_add() 0 72 1
B TestFiles.test_api__get_pdf_preview__ok__200__force_download_case() 0 67 2
B TestFiles.test_api__set_file_raw__ok_200__nominal_case() 53 53 1
B TestFiles.test_api__get_jpeg_preview__err_400__UnavailablePreview() 60 60 2
B TestFiles.test_api__get_sized_jpeg_preview__err_400__UnavailablePreview() 0 60 2
A TestThreads.test_api__get_thread__err_400__wrong_content_type() 0 18 1
B TestFiles.test_api__get_pdf_preview_err__400__UnavailablePreview() 58 58 2
A TestFiles.test_api__get_files__err_400__wrong_content_type() 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__err_400__wrong_status() 0 72 2
B TestFiles.test_api__set_file_status__ok_200__nominal_case() 0 80 2
B TestFiles.test_api__update_file_info_err_400__empty_label() 0 68 3
B TestFiles.test_api__get_allowed_size_dim__ok__nominal_case() 0 51 1
B TestThreads.test_api__update_thread__err_400__not_modified() 80 80 1
B TestFiles.test_api__get_file_revisions__ok_200__nominal_case() 0 89 3
B TestFiles.test_api__update_file_info__err_400__label_already_used() 0 94 3
B TestFiles.test_api__get_pdf_revision_preview__ok__200__force_download_case() 75 75 1
B TestThreads.test_api__get_thread_revisions__ok_200__nominal_case() 0 67 1
A TestThreads.test_api__update_thread__err_400__empty_label() 0 24 1
A TestThreads.test_api__get_thread__err_400__content_not_in_workspace() 0 18 1
B TestFiles.test_api__get_file__ok_200__nominal_case() 0 89 3
A TestFiles.test_api__get_file__err_400__workspace_does_not_exist() 0 18 1
B TestFiles.test_api__update_file_info__err_400__content_status_closed() 0 65 3
B TestFiles.test_api__get_pdf_revision_preview__ok__200__nominal_case() 0 75 1
A TestThreads.test_api__set_thread_status__ok_400__wrong_status() 0 24 1
B TestFiles.test_api__get_sized_jpeg_preview__err__400__SizeNotAllowed() 54 54 1
A TestThreads.test_api__get_thread__err_400_content_id_is_not_int() 0 18 1
B TestFiles.test_api__get_full_pdf_preview__err__400__png_UnavailablePreviewType() 53 53 1
B TestFiles.test_api__set_file_raw__ok_200__filename_already_used() 0 65 1
B TestFiles.test_api__get_sized_jpeg_preview__ok__200__force_download_case_filename_is_raw() 60 60 1
B TestFiles.test_api__update_file_info__ok_200__nominal_case() 0 121 3
B TestFiles.test_api__create_file__ok__200__nominal_case() 0 72 1
B TestFiles.test_api__get_sized_jpeg_revision_preview__ok__200__nominal_case() 0 76 1
B TestFiles.test_api__get_file_raw__ok_200__nominal_case() 0 58 2
A TestFiles.test_api__get_file__err_400__content_not_in_workspace() 0 18 1
B TestFiles.test_api__get_file__ok_200__binary_file() 0 86 2
B TestFiles.test_api__get_full_pdf_revision_preview__ok__200__force_download_case() 76 76 1
B TestFiles.test_api__update_file_info__err_400__not_modified() 0 134 3
B TestFiles.test_api__get_pdf_preview__ok__err__400_page_of_preview_not_found() 0 66 2
B TestFiles.test_api__create_file__err__400__unallow_subcontent() 0 56 2
B TestFiles.test_api__get_sized_jpeg_revision_preview__ok__200__force_download_case() 0 80 1
B TestFiles.test_api__get_full_pdf_preview__ok__200__force_download_case() 0 77 2
B TestFiles.test_api__get_sized_jpeg_preview__ok__200__force_download_case_no_filename() 60 60 1
B TestThreads.test_api__update_thread__ok_200__nominal_case() 0 75 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() 65 65 2
A TestFiles.test_api__get_file__err_400__content_does_not_exist() 0 18 1
B TestFiles.test_api__create_file__ok__200__in_folder() 0 83 1
A TestThreads.test_api__get_thread__err_400__content_does_not_exist() 0 18 1
A TestThreads.test_api__get_thread__err_400__workspace_id_is_not_int() 0 18 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
from urllib.parse import quote
3
4
import transaction
5
6
from tracim_backend import models
7
from tracim_backend.lib.core.content import ContentApi
8
from tracim_backend.lib.core.workspace import WorkspaceApi
9
from tracim_backend.models import get_tm_session
10
from tracim_backend.app_models.contents import content_type_list
11
from tracim_backend.models.revision_protection import new_revision
12
import io
13
14
import pytest
15
import transaction
16
from depot.io.utils import FileIntent
17
from PIL import Image
18
19
from tracim_backend import error
20
from tracim_backend import models
21
from tracim_backend.app_models.contents import content_type_list
22
from tracim_backend.fixtures.content import Content as ContentFixtures
23
from tracim_backend.fixtures.users_and_groups import Base as BaseFixture
24
from tracim_backend.lib.core.content import ContentApi
25
from tracim_backend.lib.core.workspace import WorkspaceApi
26
from tracim_backend.models import get_tm_session
27
from tracim_backend.models.revision_protection import new_revision
28
from tracim_backend.tests import FunctionalTest
29
from tracim_backend.tests import create_1000px_png_test_image
30
from tracim_backend.tests import set_html_document_slug_to_legacy
31
32
33
class TestFolder(FunctionalTest):
34
    """
35
    Tests for /api/v2/workspaces/{workspace_id}/folders/{content_id}
36
    endpoint
37
    """
38
39
    fixtures = [BaseFixture]
40
41
    def test_api__get_folder__ok_200__nominal_case(self) -> None:
42
        """
43
        Get one folder content
44
        """
45
        dbsession = get_tm_session(self.session_factory, transaction.manager)
46
        admin = dbsession.query(models.User) \
47
            .filter(models.User.email == '[email protected]') \
48
            .one()
49
        workspace_api = WorkspaceApi(
50
            current_user=admin,
51
            session=dbsession,
52
            config=self.app_config
53
        )
54
        content_api = ContentApi(
55
            current_user=admin,
56
            session=dbsession,
57
            config=self.app_config
58
        )
59
        test_workspace = workspace_api.create_workspace(
60
            label='test',
61
            save_now=True,
62
        )
63
        folder = content_api.create(
64
            label='test-folder',
65
            content_type_slug=content_type_list.Folder.slug,
66
            workspace=test_workspace,
67
            do_save=True,
68
            do_notify=False
69
        )
70
        transaction.commit()
71
72
        self.testapp.authorization = (
73
            'Basic',
74
            (
75
                '[email protected]',
76
                '[email protected]'
77
            )
78
        )
79
        res = self.testapp.get(
80
            '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
81
                workspace_id=test_workspace.workspace_id,
82
                content_id=folder.content_id,
83
            ),
84
            status=200
85
        )
86
        content = res.json_body
87
        assert content['content_type'] == 'folder'
88
        assert content['content_id'] == folder.content_id
89
        assert content['is_archived'] is False
90
        assert content['is_deleted'] is False
91
        assert content['is_editable'] is True
92
        assert content['label'] == 'test-folder'
93
        assert content['parent_id'] is None
94
        assert content['show_in_ui'] is True
95
        assert content['slug'] == 'test-folder'
96
        assert content['status'] == 'open'
97
        assert content['workspace_id'] == test_workspace.workspace_id
98
        assert content['current_revision_id'] == folder.revision_id
99
        # TODO - G.M - 2018-06-173 - check date format
100
        assert content['created']
101
        assert content['author']
102
        assert content['author']['user_id'] == 1
103
        assert content['author']['avatar_url'] is None
104
        assert content['author']['public_name'] == 'Global manager'
105
        # TODO - G.M - 2018-06-173 - check date format
106
        assert content['modified']
107
        assert content['last_modifier']['user_id'] == 1
108
        assert content['last_modifier']['public_name'] == 'Global manager'
109
        assert content['last_modifier']['avatar_url'] is None
110
        assert content['raw_content'] == ''
111
112
    def test_api__get_folder__err_400__wrong_content_type(self) -> None:
113
        """
114
        Get one folder of a content content 7 is not folder
115
        """
116
        self.testapp.authorization = (
117
            'Basic',
118
            (
119
                '[email protected]',
120
                '[email protected]'
121
            )
122
        )
123
        dbsession = get_tm_session(self.session_factory, transaction.manager)
124
        admin = dbsession.query(models.User) \
125
            .filter(models.User.email == '[email protected]') \
126
            .one()
127
        workspace_api = WorkspaceApi(
128
            current_user=admin,
129
            session=dbsession,
130
            config=self.app_config
131
        )
132
        content_api = ContentApi(
133
            current_user=admin,
134
            session=dbsession,
135
            config=self.app_config
136
        )
137
        test_workspace = workspace_api.create_workspace(
138
            label='test',
139
            save_now=True,
140
        )
141
        thread = content_api.create(
142
            label='thread',
143
            content_type_slug=content_type_list.Thread.slug,
144
            workspace=test_workspace,
145
            do_save=True,
146
            do_notify=False
147
        )
148
        transaction.commit()
149
        res = self.testapp.get(
150
            '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
151
                workspace_id=test_workspace.workspace_id,
152
                content_id=thread.content_id
153
            ),
154
            status=400
155
        )
156
        assert res.json_body
157
        assert 'code' in res.json_body
158
        assert res.json_body['code'] == error.CONTENT_TYPE_NOT_ALLOWED
159
160
    def test_api__get_folder__err_400__content_does_not_exist(self) -> None:  # nopep8
161
        """
162
        Get one folder content (content 170 does not exist in db)
163
        """
164
        self.testapp.authorization = (
165
            'Basic',
166
            (
167
                '[email protected]',
168
                '[email protected]'
169
            )
170
        )
171
        dbsession = get_tm_session(self.session_factory, transaction.manager)
172
        admin = dbsession.query(models.User) \
173
            .filter(models.User.email == '[email protected]') \
174
            .one()
175
        workspace_api = WorkspaceApi(
176
            current_user=admin,
177
            session=dbsession,
178
            config=self.app_config
179
        )
180
        content_api = ContentApi(
181
            current_user=admin,
182
            session=dbsession,
183
            config=self.app_config
184
        )
185
        test_workspace = workspace_api.create_workspace(
186
            label='test',
187
            save_now=True,
188
        )
189
        transaction.commit()
190
        res = self.testapp.get(
191
            '/api/v2/workspaces/{workspace_id}/folders/170'.format(workspace_id=test_workspace.workspace_id),  # nopep8
192
            status=400
193
        )
194
        assert res.json_body
195
        assert 'code' in res.json_body
196
        assert res.json_body['code'] == error.CONTENT_NOT_FOUND
197
198
    def test_api__get_folder__err_400__content_not_in_workspace(self) -> None:  # nopep8
199
        """
200
        Get one folders of a content (content is in another workspace)
201
        """
202
        self.testapp.authorization = (
203
            'Basic',
204
            (
205
                '[email protected]',
206
                '[email protected]'
207
            )
208
        )
209
        dbsession = get_tm_session(self.session_factory, transaction.manager)
210
        admin = dbsession.query(models.User) \
211
            .filter(models.User.email == '[email protected]') \
212
            .one()
213
        workspace_api = WorkspaceApi(
214
            current_user=admin,
215
            session=dbsession,
216
            config=self.app_config
217
        )
218
        content_api = ContentApi(
219
            current_user=admin,
220
            session=dbsession,
221
            config=self.app_config
222
        )
223
        test_workspace = workspace_api.create_workspace(
224
            label='test',
225
            save_now=True,
226
        )
227
        folder = content_api.create(
228
            label='test_folder',
229
            content_type_slug=content_type_list.Folder.slug,
230
            workspace=test_workspace,
231
            do_save=True,
232
            do_notify=False
233
        )
234
        test_workspace2 = workspace_api.create_workspace(
235
            label='test2',
236
            save_now=True,
237
        )
238
        transaction.commit()
239
        self.testapp.authorization = (
240
            'Basic',
241
            (
242
                '[email protected]',
243
                '[email protected]'
244
            )
245
        )
246
        res = self.testapp.get(
247
            '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
248
                workspace_id=test_workspace2.workspace_id,
249
                content_id=folder.content_id,
250
            ),
251
            status=400
252
        )
253
        assert res.json_body
254
        assert 'code' in res.json_body
255
        assert res.json_body['code'] == error.CONTENT_NOT_FOUND
256
257 View Code Duplication
    def test_api__get_folder__err_400__workspace_does_not_exist(self) -> None:  # nopep8
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
258
        """
259
        Get one folder content (Workspace 40 does not exist)
260
        """
261
        dbsession = get_tm_session(self.session_factory, transaction.manager)
262
        admin = dbsession.query(models.User) \
263
            .filter(models.User.email == '[email protected]') \
264
            .one()
265
        workspace_api = WorkspaceApi(
266
            current_user=admin,
267
            session=dbsession,
268
            config=self.app_config
269
        )
270
        content_api = ContentApi(
271
            current_user=admin,
272
            session=dbsession,
273
            config=self.app_config
274
        )
275
        test_workspace = workspace_api.create_workspace(
276
            label='test',
277
            save_now=True,
278
        )
279
        folder = content_api.create(
280
            label='test_folder',
281
            content_type_slug=content_type_list.Folder.slug,
282
            workspace=test_workspace,
283
            do_save=True,
284
            do_notify=False
285
        )
286
        transaction.commit()
287
        self.testapp.authorization = (
288
            'Basic',
289
            (
290
                '[email protected]',
291
                '[email protected]'
292
            )
293
        )
294
        res = self.testapp.get(
295
            '/api/v2/workspaces/40/folders/{content_id}'.format(content_id=folder.content_id),  # nopep8
296
            status=400
297
        )
298
        assert res.json_body
299
        assert 'code' in res.json_body
300
        assert res.json_body['code'] == error.WORKSPACE_NOT_FOUND
301
302 View Code Duplication
    def test_api__get_folder__err_400__workspace_id_is_not_int(self) -> None:  # nopep8
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
303
        """
304
        Get one folder content, workspace id is not int
305
        """
306
        dbsession = get_tm_session(self.session_factory, transaction.manager)
307
        admin = dbsession.query(models.User) \
308
            .filter(models.User.email == '[email protected]') \
309
            .one()
310
        workspace_api = WorkspaceApi(
311
            current_user=admin,
312
            session=dbsession,
313
            config=self.app_config
314
        )
315
        content_api = ContentApi(
316
            current_user=admin,
317
            session=dbsession,
318
            config=self.app_config
319
        )
320
        test_workspace = workspace_api.create_workspace(
321
            label='test',
322
            save_now=True,
323
        )
324
        folder = content_api.create(
325
            label='test_folder',
326
            content_type_slug=content_type_list.Folder.slug,
327
            workspace=test_workspace,
328
            do_save=True,
329
            do_notify=False
330
        )
331
        transaction.commit()
332
        self.testapp.authorization = (
333
            'Basic',
334
            (
335
                '[email protected]',
336
                '[email protected]'
337
            )
338
        )
339
        res = self.testapp.get(
340
            '/api/v2/workspaces/coucou/folders/{content_id}'.format(content_id=folder.content_id),  # nopep8
341
            status=400
342
        )
343
        assert res.json_body
344
        assert 'code' in res.json_body
345
        assert res.json_body['code'] == error.WORKSPACE_INVALID_ID
346
347 View Code Duplication
    def test_api__get_folder__err_400__content_id_is_not_int(self) -> None:  # nopep8
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
348
        """
349
        Get one folder content, content_id is not int
350
        """
351
        dbsession = get_tm_session(self.session_factory, transaction.manager)
352
        admin = dbsession.query(models.User) \
353
            .filter(models.User.email == '[email protected]') \
354
            .one()
355
        workspace_api = WorkspaceApi(
356
            current_user=admin,
357
            session=dbsession,
358
            config=self.app_config
359
        )
360
        content_api = ContentApi(
361
            current_user=admin,
362
            session=dbsession,
363
            config=self.app_config
364
        )
365
        test_workspace = workspace_api.create_workspace(
366
            label='test',
367
            save_now=True,
368
        )
369
        folder = content_api.create(
370
            label='test_folder',
371
            content_type_slug=content_type_list.Folder.slug,
372
            workspace=test_workspace,
373
            do_save=True,
374
            do_notify=False
375
        )
376
        transaction.commit()
377
378
        self.testapp.authorization = (
379
            'Basic',
380
            (
381
                '[email protected]',
382
                '[email protected]'
383
            )
384
        )
385
        res = self.testapp.get(
386
            '/api/v2/workspaces/{workspace_id}/folders/coucou'.format(workspace_id=test_workspace.workspace_id),  # nopep8
387
            status=400
388
        )
389
        assert res.json_body
390
        assert 'code' in res.json_body
391
        assert res.json_body['code'] == error.CONTENT_INVALID_ID
392
393 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...
394
        """
395
        Update(put) one folder content
396
        """
397
        dbsession = get_tm_session(self.session_factory, transaction.manager)
398
        admin = dbsession.query(models.User) \
399
            .filter(models.User.email == '[email protected]') \
400
            .one()
401
        workspace_api = WorkspaceApi(
402
            current_user=admin,
403
            session=dbsession,
404
            config=self.app_config
405
        )
406
        content_api = ContentApi(
407
            current_user=admin,
408
            session=dbsession,
409
            config=self.app_config
410
        )
411
        test_workspace = workspace_api.create_workspace(
412
            label='test',
413
            save_now=True,
414
        )
415
        folder = content_api.create(
416
            label='test_folder',
417
            content_type_slug=content_type_list.Folder.slug,
418
            workspace=test_workspace,
419
            do_save=True,
420
            do_notify=False
421
        )
422
        transaction.commit()
423
        self.testapp.authorization = (
424
            'Basic',
425
            (
426
                '[email protected]',
427
                '[email protected]'
428
            )
429
        )
430
        params = {
431
            'label': '',
432
            'raw_content': '<p> Le nouveau contenu </p>',
433
            'sub_content_types': [content_type_list.Folder.slug]
434
        }
435
        res = self.testapp.put_json(
436
            '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
437
                workspace_id=test_workspace.workspace_id,
438
                content_id=folder.content_id,
439
            ),
440
            params=params,
441
            status=400
442
        )
443
        # INFO - G.M - 2018-09-10 - Handled by marshmallow schema
444
        assert res.json_body
445
        assert 'code' in res.json_body
446
        assert res.json_body['code'] == error.GENERIC_SCHEMA_VALIDATION_ERROR  # nopep8
447
448
    def test_api__update_folder__ok_200__nominal_case(self) -> None:
449
        """
450
        Update(put) one html document of a content
451
        """
452
        dbsession = get_tm_session(self.session_factory, transaction.manager)
453
        admin = dbsession.query(models.User) \
454
            .filter(models.User.email == '[email protected]') \
455
            .one()
456
        workspace_api = WorkspaceApi(
457
            current_user=admin,
458
            session=dbsession,
459
            config=self.app_config
460
        )
461
        content_api = ContentApi(
462
            current_user=admin,
463
            session=dbsession,
464
            config=self.app_config
465
        )
466
        test_workspace = workspace_api.create_workspace(
467
            label='test',
468
            save_now=True,
469
        )
470
        folder = content_api.create(
471
            label='test_folder',
472
            content_type_slug=content_type_list.Folder.slug,
473
            workspace=test_workspace,
474
            do_save=True,
475
            do_notify=False
476
        )
477
        transaction.commit()
478
        self.testapp.authorization = (
479
            'Basic',
480
            (
481
                '[email protected]',
482
                '[email protected]'
483
            )
484
        )
485
        params = {
486
            'label': 'My New label',
487
            'raw_content': '<p> Le nouveau contenu </p>',
488
            'sub_content_types': [content_type_list.Folder.slug]
489
        }
490
        res = self.testapp.put_json(
491
            '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
492
                workspace_id=test_workspace.workspace_id,
493
                content_id=folder.content_id,
494
            ),
495
            params=params,
496
            status=200
497
        )
498
        content = res.json_body
499
        assert content['content_type'] == 'folder'
500
        assert content['content_id'] == folder.content_id
501
        assert content['is_archived'] is False
502
        assert content['is_deleted'] is False
503
        assert content['is_editable'] is True
504
        assert content['label'] == 'My New label'
505
        assert content['parent_id'] is None
506
        assert content['show_in_ui'] is True
507
        assert content['slug'] == 'my-new-label'
508
        assert content['status'] == 'open'
509
        assert content['workspace_id'] == test_workspace.workspace_id
510
        assert content['current_revision_id']
511
        # TODO - G.M - 2018-06-173 - check date format
512
        assert content['created']
513
        assert content['author']
514
        assert content['author']['user_id'] == 1
515
        assert content['author']['avatar_url'] is None
516
        assert content['author']['public_name'] == 'Global manager'
517
        # TODO - G.M - 2018-06-173 - check date format
518
        assert content['modified']
519
        assert content['last_modifier'] == content['author']
520
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
521
        assert content['sub_content_types'] == [content_type_list.Folder.slug]
522
523
    def test_api__update_folder__err_400__not_modified(self) -> None:
524
        """
525
        Update(put) one html document of a content
526
        """
527
        dbsession = get_tm_session(self.session_factory, transaction.manager)
528
        admin = dbsession.query(models.User) \
529
            .filter(models.User.email == '[email protected]') \
530
            .one()
531
        workspace_api = WorkspaceApi(
532
            current_user=admin,
533
            session=dbsession,
534
            config=self.app_config
535
        )
536
        content_api = ContentApi(
537
            current_user=admin,
538
            session=dbsession,
539
            config=self.app_config
540
        )
541
        test_workspace = workspace_api.create_workspace(
542
            label='test',
543
            save_now=True,
544
        )
545
        folder = content_api.create(
546
            label='test_folder',
547
            content_type_slug=content_type_list.Folder.slug,
548
            workspace=test_workspace,
549
            do_save=True,
550
            do_notify=False
551
        )
552
        transaction.commit()
553
        self.testapp.authorization = (
554
            'Basic',
555
            (
556
                '[email protected]',
557
                '[email protected]'
558
            )
559
        )
560
        params = {
561
            'label': 'My New label',
562
            'raw_content': '<p> Le nouveau contenu </p>',
563
            'sub_content_types': [content_type_list.Folder.slug]
564
        }
565
        res = self.testapp.put_json(
566
            '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
567
                workspace_id=test_workspace.workspace_id,
568
                content_id=folder.content_id,
569
            ),
570
            params=params,
571
            status=200
572
        )
573
        content = res.json_body
574
        assert content['content_type'] == 'folder'
575
        assert content['content_id'] == folder.content_id
576
        assert content['is_archived'] is False
577
        assert content['is_deleted'] is False
578
        assert content['is_editable'] is True
579
        assert content['label'] == 'My New label'
580
        assert content['parent_id'] is None
581
        assert content['show_in_ui'] is True
582
        assert content['slug'] == 'my-new-label'
583
        assert content['status'] == 'open'
584
        assert content['workspace_id'] == test_workspace.workspace_id
585
        assert content['current_revision_id']
586
        # TODO - G.M - 2018-06-173 - check date format
587
        assert content['created']
588
        assert content['author']
589
        assert content['author']['user_id'] == 1
590
        assert content['author']['avatar_url'] is None
591
        assert content['author']['public_name'] == 'Global manager'
592
        # TODO - G.M - 2018-06-173 - check date format
593
        assert content['modified']
594
        assert content['last_modifier'] == content['author']
595
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
596
        assert content['sub_content_types'] == [content_type_list.Folder.slug]
597
598
        res = self.testapp.put_json(
599
            '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
600
                workspace_id=test_workspace.workspace_id,
601
                content_id=folder.content_id,
602
            ),
603
            params=params,
604
            status=400
605
        )
606
        assert res.json_body
607
        assert 'code' in res.json_body
608
        assert res.json_body['code'] == error.SAME_VALUE_ERROR
609
610
    def test_api__update_folder__err_400__label_already_used(self) -> None:
611
        """
612
        Update(put) one html document of a content
613
        """
614
        dbsession = get_tm_session(self.session_factory, transaction.manager)
615
        admin = dbsession.query(models.User) \
616
            .filter(models.User.email == '[email protected]') \
617
            .one()
618
        workspace_api = WorkspaceApi(
619
            current_user=admin,
620
            session=dbsession,
621
            config=self.app_config
622
        )
623
        content_api = ContentApi(
624
            current_user=admin,
625
            session=dbsession,
626
            config=self.app_config
627
        )
628
        test_workspace = workspace_api.create_workspace(
629
            label='test',
630
            save_now=True,
631
        )
632
        content_api.create(
633
            label='already_used',
634
            content_type_slug=content_type_list.Folder.slug,
635
            workspace=test_workspace,
636
            do_save=True,
637
            do_notify=False
638
        )
639
        folder = content_api.create(
640
            label='test_folder',
641
            content_type_slug=content_type_list.Folder.slug,
642
            workspace=test_workspace,
643
            do_save=True,
644
            do_notify=False
645
        )
646
        transaction.commit()
647
        self.testapp.authorization = (
648
            'Basic',
649
            (
650
                '[email protected]',
651
                '[email protected]'
652
            )
653
        )
654
        params = {
655
            'label': 'already_used',
656
            'raw_content': '<p> Le nouveau contenu </p>',
657
            'sub_content_types': [content_type_list.Folder.slug]
658
        }
659
        res = self.testapp.put_json(
660
            '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
661
                workspace_id=test_workspace.workspace_id,
662
                content_id=folder.content_id,
663
            ),
664
            params=params,
665
            status=400
666
        )
667
        assert isinstance(res.json, dict)
668
        assert 'code' in res.json.keys()
669
        assert res.json_body['code'] == error.CONTENT_FILENAME_ALREADY_USED_IN_FOLDER  # nopep8
670
671
    def test_api__get_folder_revisions__ok_200__nominal_case(
672
            self
673
    ) -> None:
674
        """
675
        Get one html document of a content
676
        """
677
        dbsession = get_tm_session(self.session_factory, transaction.manager)
678
        admin = dbsession.query(models.User) \
679
            .filter(models.User.email == '[email protected]') \
680
            .one()
681
        workspace_api = WorkspaceApi(
682
            current_user=admin,
683
            session=dbsession,
684
            config=self.app_config
685
        )
686
        content_api = ContentApi(
687
            current_user=admin,
688
            session=dbsession,
689
            config=self.app_config
690
        )
691
        test_workspace = workspace_api.create_workspace(
692
            label='test',
693
            save_now=True,
694
        )
695
        folder = content_api.create(
696
            label='test-folder',
697
            content_type_slug=content_type_list.Folder.slug,
698
            workspace=test_workspace,
699
            do_save=True,
700
            do_notify=False
701
        )
702
        with new_revision(
703
           session=dbsession,
704
           tm=transaction.manager,
705
           content=folder,
706
        ):
707
            content_api.update_content(
708
                folder,
709
                new_label='test-folder-updated',
710
                new_content='Just a test'
711
            )
712
        content_api.save(folder)
713
        with new_revision(
714
           session=dbsession,
715
           tm=transaction.manager,
716
           content=folder,
717
        ):
718
            content_api.archive(
719
                folder,
720
            )
721
        content_api.save(folder)
722
        with new_revision(
723
           session=dbsession,
724
           tm=transaction.manager,
725
           content=folder,
726
        ):
727
            content_api.unarchive(
728
                folder,
729
            )
730
        content_api.save(folder)
731
        transaction.commit()
732
        self.testapp.authorization = (
733
            'Basic',
734
            (
735
                '[email protected]',
736
                '[email protected]'
737
            )
738
        )
739
        res = self.testapp.get(
740
            '/api/v2/workspaces/{workspace_id}/folders/{content_id}/revisions'.format(  # nopep8
741
                workspace_id=test_workspace.workspace_id,
742
                content_id=folder.content_id,
743
            ),
744
            status=200
745
        )
746
        revisions = res.json_body
747
        assert len(revisions) == 4
748
        revision = revisions[0]
749
        assert revision['content_type'] == 'folder'
750
        assert revision['content_id'] == folder.content_id
751
        assert revision['is_archived'] is False
752
        assert revision['is_deleted'] is False
753
        assert revision['is_editable'] is False
754
        assert revision['label'] == 'test-folder'
755
        assert revision['parent_id'] is None
756
        assert revision['show_in_ui'] is True
757
        assert revision['slug'] == 'test-folder'
758
        assert revision['status'] == 'open'
759
        assert revision['workspace_id'] == test_workspace.workspace_id
760
        assert revision['revision_id']
761
        assert revision['revision_type'] == 'creation'
762
        assert revision['sub_content_types']
763
        # TODO - G.M - 2018-06-173 - Test with real comments
764
        assert revision['comment_ids'] == []
765
        # TODO - G.M - 2018-06-173 - check date format
766
        assert revision['created']
767
        assert revision['author']
768
        assert revision['author']['user_id'] == 1
769
        assert revision['author']['avatar_url'] is None
770
        assert revision['author']['public_name'] == 'Global manager'
771
772
        revision = revisions[1]
773
        assert revision['content_type'] == 'folder'
774
        assert revision['content_id'] == folder.content_id
775
        assert revision['is_archived'] is False
776
        assert revision['is_deleted'] is False
777
        assert revision['is_editable'] is False
778
        assert revision['label'] == 'test-folder-updated'
779
        assert revision['parent_id'] is None
780
        assert revision['show_in_ui'] is True
781
        assert revision['slug'] == 'test-folder-updated'
782
        assert revision['status'] == 'open'
783
        assert revision['workspace_id'] == test_workspace.workspace_id
784
        assert revision['revision_id']
785
        assert revision['revision_type'] == 'edition'
786
        assert revision['sub_content_types']
787
        # TODO - G.M - 2018-06-173 - Test with real comments
788
        assert revision['comment_ids'] == []
789
        # TODO - G.M - 2018-06-173 - check date format
790
        assert revision['created']
791
        assert revision['author']
792
        assert revision['author']['user_id'] == 1
793
        assert revision['author']['avatar_url'] is None
794
        assert revision['author']['public_name'] == 'Global manager'
795
796
        revision = revisions[2]
797
        assert revision['content_type'] == 'folder'
798
        assert revision['content_id'] == folder.content_id
799
        assert revision['is_archived'] is True
800
        assert revision['is_deleted'] is False
801
        assert revision['is_editable'] is False
802
        assert revision['label'] != 'test-folder-updated'
803
        assert revision['label'].startswith('test-folder-updated')
804
        assert revision['parent_id'] is None
805
        assert revision['show_in_ui'] is True
806
        assert revision['slug'] != 'test-folder-updated'
807
        assert revision['slug'].startswith('test-folder-updated')
808
        assert revision['status'] == 'open'
809
        assert revision['workspace_id'] == test_workspace.workspace_id
810
        assert revision['revision_id']
811
        assert revision['revision_type'] == 'archiving'
812
        assert revision['sub_content_types']
813
        # TODO - G.M - 2018-06-173 - Test with real comments
814
        assert revision['comment_ids'] == []
815
        # TODO - G.M - 2018-06-173 - check date format
816
        assert revision['created']
817
        assert revision['author']
818
        assert revision['author']['user_id'] == 1
819
        assert revision['author']['avatar_url'] is None
820
        assert revision['author']['public_name'] == 'Global manager'
821
822
        revision = revisions[3]
823
        assert revision['content_type'] == 'folder'
824
        assert revision['content_id'] == folder.content_id
825
        assert revision['is_archived'] is False
826
        assert revision['is_deleted'] is False
827
        assert revision['is_editable'] is True
828
        assert revision['label'].startswith('test-folder-updated')
829
        assert revision['parent_id'] is None
830
        assert revision['show_in_ui'] is True
831
        assert revision['slug'].startswith('test-folder-updated')
832
        assert revision['status'] == 'open'
833
        assert revision['workspace_id'] == test_workspace.workspace_id
834
        assert revision['revision_id']
835
        assert revision['revision_type'] == 'unarchiving'
836
        assert revision['sub_content_types']
837
        # TODO - G.M - 2018-06-173 - Test with real comments
838
        assert revision['comment_ids'] == []
839
        # TODO - G.M - 2018-06-173 - check date format
840
        assert revision['created']
841
        assert revision['author']
842
        assert revision['author']['user_id'] == 1
843
        assert revision['author']['avatar_url'] is None
844
        assert revision['author']['public_name'] == 'Global manager'
845
846
    def test_api__set_folder_status__ok_200__nominal_case(self) -> None:
847
        """
848
        Get one folder content
849
        """
850
        dbsession = get_tm_session(self.session_factory, transaction.manager)
851
        admin = dbsession.query(models.User) \
852
            .filter(models.User.email == '[email protected]') \
853
            .one()
854
        workspace_api = WorkspaceApi(
855
            current_user=admin,
856
            session=dbsession,
857
            config=self.app_config
858
        )
859
        content_api = ContentApi(
860
            current_user=admin,
861
            session=dbsession,
862
            config=self.app_config
863
        )
864
        test_workspace = workspace_api.create_workspace(
865
            label='test',
866
            save_now=True,
867
        )
868
        folder = content_api.create(
869
            label='test_folder',
870
            content_type_slug=content_type_list.Folder.slug,
871
            workspace=test_workspace,
872
            do_save=True,
873
            do_notify=False
874
        )
875
        transaction.commit()
876
        self.testapp.authorization = (
877
            'Basic',
878
            (
879
                '[email protected]',
880
                '[email protected]'
881
            )
882
        )
883
        params = {
884
            'status': 'closed-deprecated',
885
        }
886
887
        # before
888
        res = self.testapp.get(
889
            '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(  # nopep8
890
                # nopep8
891
                workspace_id=test_workspace.workspace_id,
892
                content_id=folder.content_id,
893
            ),
894
            status=200
895
        )
896
        content = res.json_body
897
        assert content['content_type'] == 'folder'
898
        assert content['content_id'] == folder.content_id
899
        assert content['status'] == 'open'
900
901
        # set status
902
        self.testapp.put_json(
903
            '/api/v2/workspaces/{workspace_id}/folders/{content_id}/status'.format(  # nopep8
904
                workspace_id=test_workspace.workspace_id,
905
                content_id=folder.content_id,
906
            ),
907
            params=params,
908
            status=204
909
        )
910
911
        # after
912
        res = self.testapp.get(
913
            '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
914
                workspace_id=test_workspace.workspace_id,
915
                content_id=folder.content_id,
916
            ),
917
            status=200
918
        )
919
        content = res.json_body
920
        assert content['content_type'] == 'folder'
921
        assert content['content_id'] == folder.content_id
922
        assert content['status'] == 'closed-deprecated'
923
924 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...
925
        """
926
        Get one folder content
927
        """
928
        self.testapp.authorization = (
929
            'Basic',
930
            (
931
                '[email protected]',
932
                '[email protected]'
933
            )
934
        )
935
        params = {
936
            'status': 'unexistant-status',
937
        }
938
        dbsession = get_tm_session(self.session_factory, transaction.manager)
939
        admin = dbsession.query(models.User) \
940
            .filter(models.User.email == '[email protected]') \
941
            .one()
942
        workspace_api = WorkspaceApi(
943
            current_user=admin,
944
            session=dbsession,
945
            config=self.app_config
946
        )
947
        content_api = ContentApi(
948
            current_user=admin,
949
            session=dbsession,
950
            config=self.app_config
951
        )
952
        test_workspace = workspace_api.create_workspace(
953
            label='test',
954
            save_now=True,
955
        )
956
        folder = content_api.create(
957
            label='test_folder',
958
            content_type_slug=content_type_list.Folder.slug,
959
            workspace=test_workspace,
960
            do_save=True,
961
            do_notify=False
962
        )
963
        transaction.commit()
964
        res = self.testapp.put_json(
965
            '/api/v2/workspaces/{workspace_id}/folders/{content_id}/status'.format(  # nopep8
966
                workspace_id=test_workspace.workspace_id,
967
                content_id=folder.content_id,
968
            ),
969
            params=params,
970
            status=400
971
        )
972
        # TODO - G.M - 2018-09-10 - handle by marshmallow schema
973
        assert res.json_body
974
        assert 'code' in res.json_body
975
        assert res.json_body['code'] == error.GENERIC_SCHEMA_VALIDATION_ERROR  # nopep8
976
977
978
class TestHtmlDocuments(FunctionalTest):
979
    """
980
    Tests for /api/v2/workspaces/{workspace_id}/html-documents/{content_id}
981
    endpoint
982
    """
983
984
    fixtures = [BaseFixture, ContentFixtures]
985
986
    def test_api__get_html_document__ok_200__legacy_slug(self) -> None:
987
        """
988
        Get one html document of a content
989
        """
990
        self.testapp.authorization = (
991
            'Basic',
992
            (
993
                '[email protected]',
994
                '[email protected]'
995
            )
996
        )
997
        set_html_document_slug_to_legacy(self.session_factory)
998
        res = self.testapp.get(
999
            '/api/v2/workspaces/2/html-documents/6',
1000
            status=200
1001
        )
1002
        content = res.json_body
1003
        assert content['content_type'] == 'html-document'
1004
        assert content['content_id'] == 6
1005
        assert content['is_archived'] is False
1006
        assert content['is_deleted'] is False
1007
        assert content['is_editable'] is True
1008
        assert content['label'] == 'Tiramisu Recipe'
1009
        assert content['parent_id'] == 3
1010
        assert content['show_in_ui'] is True
1011
        assert content['slug'] == 'tiramisu-recipe'
1012
        assert content['status'] == 'open'
1013
        assert content['workspace_id'] == 2
1014
        assert content['current_revision_id'] == 27
1015
        # TODO - G.M - 2018-06-173 - check date format
1016
        assert content['created']
1017
        assert content['author']
1018
        assert content['author']['user_id'] == 1
1019
        assert content['author']['avatar_url'] is None
1020
        assert content['author']['public_name'] == 'Global manager'
1021
        # TODO - G.M - 2018-06-173 - check date format
1022
        assert content['modified']
1023
        assert content['last_modifier'] != content['author']
1024
        assert content['last_modifier']['user_id'] == 3
1025
        assert content['last_modifier']['public_name'] == 'Bob i.'
1026
        assert content['last_modifier']['avatar_url'] is None
1027
        assert content['raw_content'] == '<p>To cook a great Tiramisu, you need many ingredients.</p>'  # nopep8
1028
        assert content['file_extension'] == '.document.html'
1029
1030 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...
1031
        """
1032
        Get one html document of a content
1033
        """
1034
        self.testapp.authorization = (
1035
            'Basic',
1036
            (
1037
                '[email protected]',
1038
                '[email protected]'
1039
            )
1040
        )
1041
        res = self.testapp.get(
1042
            '/api/v2/workspaces/2/html-documents/6',
1043
            status=200
1044
        )
1045
        content = res.json_body
1046
        assert content['content_type'] == 'html-document'
1047
        assert content['content_id'] == 6
1048
        assert content['is_archived'] is False
1049
        assert content['is_deleted'] is False
1050
        assert content['is_editable'] is True
1051
        assert content['label'] == 'Tiramisu Recipe'
1052
        assert content['parent_id'] == 3
1053
        assert content['show_in_ui'] is True
1054
        assert content['slug'] == 'tiramisu-recipe'
1055
        assert content['status'] == 'open'
1056
        assert content['workspace_id'] == 2
1057
        assert content['current_revision_id'] == 27
1058
        # TODO - G.M - 2018-06-173 - check date format
1059
        assert content['created']
1060
        assert content['author']
1061
        assert content['author']['user_id'] == 1
1062
        assert content['author']['avatar_url'] is None
1063
        assert content['author']['public_name'] == 'Global manager'
1064
        # TODO - G.M - 2018-06-173 - check date format
1065
        assert content['modified']
1066
        assert content['last_modifier'] != content['author']
1067
        assert content['last_modifier']['user_id'] == 3
1068
        assert content['last_modifier']['public_name'] == 'Bob i.'
1069
        assert content['last_modifier']['avatar_url'] is None
1070
        assert content['raw_content'] == '<p>To cook a great Tiramisu, you need many ingredients.</p>'  # nopep8
1071
        assert content['file_extension'] == '.document.html'
1072
1073
    def test_api__get_html_document__ok_200__archived_content(self) -> None:
1074
        """
1075
        Get one html document of a content
1076
        """
1077
        self.testapp.authorization = (
1078
            'Basic',
1079
            (
1080
                '[email protected]',
1081
                '[email protected]'
1082
            )
1083
        )
1084
        res = self.testapp.put_json(
1085
            '/api/v2/workspaces/2/contents/6/archived',
1086
            status=204
1087
        )
1088
        res = self.testapp.get(
1089
            '/api/v2/workspaces/2/html-documents/6',
1090
            status=200
1091
        )
1092
        content = res.json_body
1093
        assert content['content_type'] == 'html-document'
1094
        assert content['content_id'] == 6
1095
        assert content['is_archived'] is True
1096
1097
    def test_api__get_html_document__ok_200__deleted_content(self) -> None:
1098
        """
1099
        Get one html document of a content
1100
        """
1101
        self.testapp.authorization = (
1102
            'Basic',
1103
            (
1104
                '[email protected]',
1105
                '[email protected]'
1106
            )
1107
        )
1108
        res = self.testapp.put_json(
1109
            '/api/v2/workspaces/2/contents/6/trashed',
1110
            status=204
1111
        )
1112
        res = self.testapp.get(
1113
            '/api/v2/workspaces/2/html-documents/6',
1114
            status=200
1115
        )
1116
        content = res.json_body
1117
        assert content['content_type'] == 'html-document'
1118
        assert content['content_id'] == 6
1119
        assert content['is_deleted'] is True
1120
1121
    def test_api__get_html_document__err_400__wrong_content_type(self) -> None:
1122
        """
1123
        Get one html document of a content content 7 is not html_document
1124
        """
1125
        self.testapp.authorization = (
1126
            'Basic',
1127
            (
1128
                '[email protected]',
1129
                '[email protected]'
1130
            )
1131
        )
1132
        res = self.testapp.get(
1133
            '/api/v2/workspaces/2/html-documents/7',
1134
            status=400
1135
        )
1136
        assert res.json_body
1137
        assert 'code' in res.json_body
1138
        assert res.json_body['code'] == error.CONTENT_TYPE_NOT_ALLOWED
1139
1140
    def test_api__get_html_document__err_400__content_does_not_exist(self) -> None:  # nopep8
1141
        """
1142
        Get one html document of a content (content 170 does not exist in db
1143
        """
1144
        self.testapp.authorization = (
1145
            'Basic',
1146
            (
1147
                '[email protected]',
1148
                '[email protected]'
1149
            )
1150
        )
1151
        res = self.testapp.get(
1152
            '/api/v2/workspaces/2/html-documents/170',
1153
            status=400
1154
        )
1155
        assert res.json_body
1156
        assert 'code' in res.json_body
1157
        assert res.json_body['code'] == error.CONTENT_NOT_FOUND
1158
1159
    def test_api__get_html_document__err_400__content_not_in_workspace(self) -> None:  # nopep8
1160
        """
1161
        Get one html document of a content (content 6 is in workspace 2)
1162
        """
1163
        self.testapp.authorization = (
1164
            'Basic',
1165
            (
1166
                '[email protected]',
1167
                '[email protected]'
1168
            )
1169
        )
1170
        res = self.testapp.get(
1171
            '/api/v2/workspaces/1/html-documents/6',
1172
            status=400
1173
        )
1174
        assert res.json_body
1175
        assert 'code' in res.json_body
1176
        assert res.json_body['code'] == error.CONTENT_NOT_FOUND
1177
1178
    def test_api__get_html_document__err_400__workspace_does_not_exist(self) -> None:  # nopep8
1179
        """
1180
        Get one html document of a content (Workspace 40 does not exist)
1181
        """
1182
        self.testapp.authorization = (
1183
            'Basic',
1184
            (
1185
                '[email protected]',
1186
                '[email protected]'
1187
            )
1188
        )
1189
        res = self.testapp.get(
1190
            '/api/v2/workspaces/40/html-documents/6',
1191
            status=400
1192
        )
1193
        assert res.json_body
1194
        assert 'code' in res.json_body
1195
        assert res.json_body['code'] == error.WORKSPACE_NOT_FOUND
1196
1197
    def test_api__get_html_document__err_400__workspace_id_is_not_int(self) -> None:  # nopep8
1198
        """
1199
        Get one html document of a content, workspace id is not int
1200
        """
1201
        self.testapp.authorization = (
1202
            'Basic',
1203
            (
1204
                '[email protected]',
1205
                '[email protected]'
1206
            )
1207
        )
1208
        res = self.testapp.get(
1209
            '/api/v2/workspaces/coucou/html-documents/6',
1210
            status=400
1211
        )
1212
        assert res.json_body
1213
        assert 'code' in res.json_body
1214
        assert res.json_body['code'] == error.WORKSPACE_INVALID_ID
1215
1216
    def test_api__get_html_document__err_400__content_id_is_not_int(self) -> None:  # nopep8
1217
        """
1218
        Get one html document of a content, content_id is not int
1219
        """
1220
        self.testapp.authorization = (
1221
            'Basic',
1222
            (
1223
                '[email protected]',
1224
                '[email protected]'
1225
            )
1226
        )
1227
        res = self.testapp.get(
1228
            '/api/v2/workspaces/2/html-documents/coucou',
1229
            status=400
1230
        )
1231
        assert res.json_body
1232
        assert 'code' in res.json_body
1233
        assert res.json_body['code'] == error.CONTENT_INVALID_ID
1234
1235
    def test_api__update_html_document__err_400__empty_label(self) -> None:  # nopep8
1236
        """
1237
        Update(put) one html document of a content
1238
        """
1239
        self.testapp.authorization = (
1240
            'Basic',
1241
            (
1242
                '[email protected]',
1243
                '[email protected]'
1244
            )
1245
        )
1246
        params = {
1247
            'label': '',
1248
            'raw_content': '<p> Le nouveau contenu </p>',
1249
        }
1250
        res = self.testapp.put_json(
1251
            '/api/v2/workspaces/2/html-documents/6',
1252
            params=params,
1253
            status=400
1254
        )
1255
        # INFO - G.M - 2018-09-10 -  Handled by marshmallow schema
1256
        assert res.json_body
1257
        assert 'code' in res.json_body
1258
        assert res.json_body['code'] == error.GENERIC_SCHEMA_VALIDATION_ERROR  # nopep8
1259
1260
    def test_api__update_html_document__ok_200__nominal_case(self) -> None:
1261
        """
1262
        Update(put) one html document of a content
1263
        """
1264
        self.testapp.authorization = (
1265
            'Basic',
1266
            (
1267
                '[email protected]',
1268
                '[email protected]'
1269
            )
1270
        )
1271
        params = {
1272
            'label': 'My New label',
1273
            'raw_content': '<p> Le nouveau contenu </p>',
1274
        }
1275
        res = self.testapp.put_json(
1276
            '/api/v2/workspaces/2/html-documents/6',
1277
            params=params,
1278
            status=200
1279
        )
1280
        content = res.json_body
1281
        assert content['content_type'] == 'html-document'
1282
        assert content['content_id'] == 6
1283
        assert content['is_archived'] is False
1284
        assert content['is_deleted'] is False
1285
        assert content['is_editable'] is True
1286
        assert content['label'] == 'My New label'
1287
        assert content['parent_id'] == 3
1288
        assert content['show_in_ui'] is True
1289
        assert content['slug'] == 'my-new-label'
1290
        assert content['status'] == 'open'
1291
        assert content['workspace_id'] == 2
1292
        assert content['current_revision_id'] == 28
1293
        # TODO - G.M - 2018-06-173 - check date format
1294
        assert content['created']
1295
        assert content['author']
1296
        assert content['author']['user_id'] == 1
1297
        assert content['author']['avatar_url'] is None
1298
        assert content['author']['public_name'] == 'Global manager'
1299
        # TODO - G.M - 2018-06-173 - check date format
1300
        assert content['modified']
1301
        assert content['last_modifier'] == content['author']
1302
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
1303
        assert content['file_extension'] == '.document.html'
1304
1305
        res = self.testapp.get(
1306
            '/api/v2/workspaces/2/html-documents/6',
1307
            status=200
1308
        )
1309
        content = res.json_body
1310
        assert content['content_type'] == 'html-document'
1311
        assert content['content_id'] == 6
1312
        assert content['is_archived'] is False
1313
        assert content['is_deleted'] is False
1314
        assert content['is_editable'] is True
1315
        assert content['label'] == 'My New label'
1316
        assert content['parent_id'] == 3
1317
        assert content['show_in_ui'] is True
1318
        assert content['slug'] == 'my-new-label'
1319
        assert content['status'] == 'open'
1320
        assert content['workspace_id'] == 2
1321
        assert content['current_revision_id'] == 28
1322
        # TODO - G.M - 2018-06-173 - check date format
1323
        assert content['created']
1324
        assert content['author']
1325
        assert content['author']['user_id'] == 1
1326
        assert content['author']['avatar_url'] is None
1327
        assert content['author']['public_name'] == 'Global manager'
1328
        # TODO - G.M - 2018-06-173 - check date format
1329
        assert content['modified']
1330
        assert content['last_modifier'] == content['author']
1331
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
1332
        assert content['file_extension'] == '.document.html'
1333
1334
    def test_api__update_html_document__err_400__not_editable(self) -> None:
1335
        """
1336
        Update(put) one html document of a content
1337
        """
1338
        self.testapp.authorization = (
1339
            'Basic',
1340
            (
1341
                '[email protected]',
1342
                '[email protected]'
1343
            )
1344
        )
1345
        params = {
1346
            'status': 'closed-deprecated',
1347
        }
1348
        self.testapp.put_json(
1349
            '/api/v2/workspaces/2/html-documents/6/status',
1350
            params=params,
1351
            status=204
1352
        )
1353
1354
        params = {
1355
            'label': 'My New label',
1356
            'raw_content': '<p> Le nouveau contenu ! </p>',
1357
        }
1358
        res = self.testapp.put_json(
1359
            '/api/v2/workspaces/2/html-documents/6',
1360
            params=params,
1361
            status=400
1362
        )
1363
        assert res.json_body
1364
        assert 'code' in res.json_body
1365
        assert res.json_body['code'] == error.CONTENT_IN_NOT_EDITABLE_STATE
1366
1367 View Code Duplication
    def test_api__update_html_document__err_400__not_modified(self) -> None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1368
        """
1369
        Update(put) one html document of a content
1370
        """
1371
        self.testapp.authorization = (
1372
            'Basic',
1373
            (
1374
                '[email protected]',
1375
                '[email protected]'
1376
            )
1377
        )
1378
        params = {
1379
            'label': 'My New label',
1380
            'raw_content': '<p> Le nouveau contenu </p>',
1381
        }
1382
        res = self.testapp.put_json(
1383
            '/api/v2/workspaces/2/html-documents/6',
1384
            params=params,
1385
            status=200
1386
        )
1387
        content = res.json_body
1388
        assert content['content_type'] == 'html-document'
1389
        assert content['content_id'] == 6
1390
        assert content['is_archived'] is False
1391
        assert content['is_deleted'] is False
1392
        assert content['is_editable'] is True
1393
        assert content['label'] == 'My New label'
1394
        assert content['parent_id'] == 3
1395
        assert content['show_in_ui'] is True
1396
        assert content['slug'] == 'my-new-label'
1397
        assert content['status'] == 'open'
1398
        assert content['workspace_id'] == 2
1399
        assert content['current_revision_id'] == 28
1400
        # TODO - G.M - 2018-06-173 - check date format
1401
        assert content['created']
1402
        assert content['author']
1403
        assert content['author']['user_id'] == 1
1404
        assert content['author']['avatar_url'] is None
1405
        assert content['author']['public_name'] == 'Global manager'
1406
        # TODO - G.M - 2018-06-173 - check date format
1407
        assert content['modified']
1408
        assert content['last_modifier'] == content['author']
1409
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
1410
1411
        res = self.testapp.get(
1412
            '/api/v2/workspaces/2/html-documents/6',
1413
            status=200
1414
        )
1415
        content = res.json_body
1416
        assert content['content_type'] == 'html-document'
1417
        assert content['content_id'] == 6
1418
        assert content['is_archived'] is False
1419
        assert content['is_deleted'] is False
1420
        assert content['is_editable'] is True
1421
        assert content['label'] == 'My New label'
1422
        assert content['parent_id'] == 3
1423
        assert content['show_in_ui'] is True
1424
        assert content['slug'] == 'my-new-label'
1425
        assert content['status'] == 'open'
1426
        assert content['workspace_id'] == 2
1427
        assert content['current_revision_id'] == 28
1428
        # TODO - G.M - 2018-06-173 - check date format
1429
        assert content['created']
1430
        assert content['author']
1431
        assert content['author']['user_id'] == 1
1432
        assert content['author']['avatar_url'] is None
1433
        assert content['author']['public_name'] == 'Global manager'
1434
        # TODO - G.M - 2018-06-173 - check date format
1435
        assert content['modified']
1436
        assert content['last_modifier'] == content['author']
1437
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
1438
1439
        res = self.testapp.put_json(
1440
            '/api/v2/workspaces/2/html-documents/6',
1441
            params=params,
1442
            status=400
1443
        )
1444
        assert res.json_body
1445
        assert 'code' in res.json_body
1446
        assert res.json_body['code'] == error.SAME_VALUE_ERROR
1447
1448
    def test_api__get_html_document_revisions__ok_200__nominal_case(
1449
            self
1450
    ) -> None:
1451
        """
1452
        Get one html document of a content
1453
        """
1454
        self.testapp.authorization = (
1455
            'Basic',
1456
            (
1457
                '[email protected]',
1458
                '[email protected]'
1459
            )
1460
        )
1461
        res = self.testapp.get(
1462
            '/api/v2/workspaces/2/html-documents/6/revisions',
1463
            status=200
1464
        )
1465
        revisions = res.json_body
1466
        assert len(revisions) == 3
1467
        revision = revisions[0]
1468
        assert revision['content_type'] == 'html-document'
1469
        assert revision['content_id'] == 6
1470
        assert revision['is_archived'] is False
1471
        assert revision['is_deleted'] is False
1472
        assert revision['is_editable'] is False
1473
        assert revision['label'] == 'Tiramisu Recipes!!!'
1474
        assert revision['parent_id'] == 3
1475
        assert revision['show_in_ui'] is True
1476
        assert revision['slug'] == 'tiramisu-recipes'
1477
        assert revision['status'] == 'open'
1478
        assert revision['workspace_id'] == 2
1479
        assert revision['revision_id'] == 6
1480
        assert revision['revision_type'] == 'creation'
1481
        assert revision['sub_content_types']
1482
        # TODO - G.M - 2018-06-173 - Test with real comments
1483
        assert revision['comment_ids'] == []
1484
        # TODO - G.M - 2018-06-173 - check date format
1485
        assert revision['created']
1486
        assert revision['author']
1487
        assert revision['author']['user_id'] == 1
1488
        assert revision['author']['avatar_url'] is None
1489
        assert revision['author']['public_name'] == 'Global manager'
1490
        revision = revisions[1]
1491
        assert revision['content_type'] == 'html-document'
1492
        assert revision['content_id'] == 6
1493
        assert revision['is_archived'] is False
1494
        assert revision['is_deleted'] is False
1495
        assert revision['is_editable'] is False
1496
        assert revision['label'] == 'Tiramisu Recipes!!!'
1497
        assert revision['parent_id'] == 3
1498
        assert revision['show_in_ui'] is True
1499
        assert revision['slug'] == 'tiramisu-recipes'
1500
        assert revision['status'] == 'open'
1501
        assert revision['workspace_id'] == 2
1502
        assert revision['revision_id'] == 7
1503
        assert revision['revision_type'] == 'edition'
1504
        assert revision['sub_content_types']
1505
        # TODO - G.M - 2018-06-173 - Test with real comments
1506
        assert revision['comment_ids'] == []
1507
        # TODO - G.M - 2018-06-173 - check date format
1508
        assert revision['created']
1509
        assert revision['author']
1510
        assert revision['author']['user_id'] == 1
1511
        assert revision['author']['avatar_url'] is None
1512
        assert revision['author']['public_name'] == 'Global manager'
1513
        revision = revisions[2]
1514
        assert revision['content_type'] == 'html-document'
1515
        assert revision['content_id'] == 6
1516
        assert revision['is_archived'] is False
1517
        assert revision['is_deleted'] is False
1518
        assert revision['is_editable'] is True
1519
        assert revision['label'] == 'Tiramisu Recipe'
1520
        assert revision['parent_id'] == 3
1521
        assert revision['show_in_ui'] is True
1522
        assert revision['slug'] == 'tiramisu-recipe'
1523
        assert revision['status'] == 'open'
1524
        assert revision['workspace_id'] == 2
1525
        assert revision['revision_id'] == 27
1526
        assert revision['revision_type'] == 'edition'
1527
        assert revision['sub_content_types']
1528
        # TODO - G.M - 2018-06-173 - Test with real comments
1529
        assert revision['comment_ids'] == []
1530
        # TODO - G.M - 2018-06-173 - check date format
1531
        assert revision['created']
1532
        assert revision['author']
1533
        assert revision['author']['user_id'] == 3
1534
        assert revision['author']['avatar_url'] is None
1535
        assert revision['author']['public_name'] == 'Bob i.'
1536
        assert revision['file_extension'] == '.document.html'
1537
1538
    def test_api__set_html_document_status__ok_200__nominal_case(self) -> None:
1539
        """
1540
        Get one html document of a content
1541
        """
1542
        self.testapp.authorization = (
1543
            'Basic',
1544
            (
1545
                '[email protected]',
1546
                '[email protected]'
1547
            )
1548
        )
1549
        params = {
1550
            'status': 'closed-deprecated',
1551
        }
1552
1553
        # before
1554
        res = self.testapp.get(
1555
            '/api/v2/workspaces/2/html-documents/6',
1556
            status=200
1557
        )
1558
        content = res.json_body
1559
        assert content['content_type'] == 'html-document'
1560
        assert content['content_id'] == 6
1561
        assert content['status'] == 'open'
1562
1563
        # set status
1564
        self.testapp.put_json(
1565
            '/api/v2/workspaces/2/html-documents/6/status',
1566
            params=params,
1567
            status=204
1568
        )
1569
1570
        # after
1571
        res = self.testapp.get(
1572
            '/api/v2/workspaces/2/html-documents/6',
1573
            status=200
1574
        )
1575
        content = res.json_body
1576
        assert content['content_type'] == 'html-document'
1577
        assert content['content_id'] == 6
1578
        assert content['status'] == 'closed-deprecated'
1579
1580
    def test_api__set_html_document_status__err_400__wrong_status(self) -> None:
1581
        """
1582
        Get one html document of a content
1583
        """
1584
        self.testapp.authorization = (
1585
            'Basic',
1586
            (
1587
                '[email protected]',
1588
                '[email protected]'
1589
            )
1590
        )
1591
        params = {
1592
            'status': 'unexistant-status',
1593
        }
1594
        res = self.testapp.put_json(
1595
            '/api/v2/workspaces/2/html-documents/6/status',
1596
            params=params,
1597
            status=400
1598
        )
1599
        assert res.json_body
1600
        assert 'code' in res.json_body
1601
        assert res.json_body['code'] == error.GENERIC_SCHEMA_VALIDATION_ERROR  # nopep8
1602
1603
1604
class TestFiles(FunctionalTest):
1605
    """
1606
    Tests for /api/v2/workspaces/{workspace_id}/files/{content_id}
1607
    endpoint
1608
    """
1609
1610
    fixtures = [BaseFixture, ContentFixtures]
1611
1612
    def test_api__get_file__ok_200__nominal_case(self) -> None:
1613
        """
1614
        Get one file of a content
1615
        """
1616
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1617
        admin = dbsession.query(models.User) \
1618
            .filter(models.User.email == '[email protected]') \
1619
            .one()
1620
        workspace_api = WorkspaceApi(
1621
            current_user=admin,
1622
            session=dbsession,
1623
            config=self.app_config
1624
        )
1625
        content_api = ContentApi(
1626
            current_user=admin,
1627
            session=dbsession,
1628
            config=self.app_config
1629
        )
1630
        business_workspace = workspace_api.get_one(1)
1631
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
1632
        test_file = content_api.create(
1633
            content_type_slug=content_type_list.File.slug,
1634
            workspace=business_workspace,
1635
            parent=tool_folder,
1636
            label='Test file',
1637
            do_save=False,
1638
            do_notify=False,
1639
        )
1640
        with new_revision(
1641
            session=dbsession,
1642
            tm=transaction.manager,
1643
            content=test_file,
1644
        ):
1645
            content_api.update_file_data(
1646
                test_file,
1647
                'Test_file.txt',
1648
                new_mimetype='plain/text',
1649
                new_content=b'Test file',
1650
            )
1651
        with new_revision(
1652
            session=dbsession,
1653
            tm=transaction.manager,
1654
            content=test_file,
1655
        ):
1656
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
1657
            dbsession.flush()
1658
        transaction.commit()
1659
1660
        self.testapp.authorization = (
1661
            'Basic',
1662
            (
1663
                '[email protected]',
1664
                '[email protected]'
1665
            )
1666
        )
1667
        res = self.testapp.get(
1668
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
1669
            status=200
1670
        )
1671
        content = res.json_body
1672
        assert content['content_type'] == 'file'
1673
        assert content['content_id'] == test_file.content_id
1674
        assert content['is_archived'] is False
1675
        assert content['is_deleted'] is False
1676
        assert content['is_editable'] is True
1677
        assert content['label'] == 'Test_file'
1678
        assert content['parent_id'] == 1
1679
        assert content['show_in_ui'] is True
1680
        assert content['slug'] == 'test-file'
1681
        assert content['status'] == 'open'
1682
        assert content['workspace_id'] == 1
1683
        assert content['current_revision_id']
1684
        # TODO - G.M - 2018-06-173 - check date format
1685
        assert content['created']
1686
        assert content['author']
1687
        assert content['author']['user_id'] == 1
1688
        assert content['author']['avatar_url'] is None
1689
        assert content['author']['public_name'] == 'Global manager'
1690
        # TODO - G.M - 2018-06-173 - check date format
1691
        assert content['modified']
1692
        assert content['last_modifier'] == content['author']
1693
        assert content['raw_content'] == '<p>description</p>'  # nopep8
1694
        assert content['mimetype'] == 'plain/text'
1695
        assert content['size'] == len(b'Test file')
1696
        assert content['file_extension'] == '.txt'
1697
        assert content['filename'] == 'Test_file.txt'
1698
        assert content['page_nb'] == 1
1699
        assert content['has_pdf_preview'] is True
1700
        assert content['has_jpeg_preview'] is True
1701
1702
    def test_api__get_file__ok_200__no_file_add(self) -> None:
1703
        """
1704
        Get one file of a content
1705
        """
1706
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1707
        admin = dbsession.query(models.User) \
1708
            .filter(models.User.email == '[email protected]') \
1709
            .one()
1710
        workspace_api = WorkspaceApi(
1711
            current_user=admin,
1712
            session=dbsession,
1713
            config=self.app_config
1714
        )
1715
        content_api = ContentApi(
1716
            current_user=admin,
1717
            session=dbsession,
1718
            config=self.app_config
1719
        )
1720
        business_workspace = workspace_api.get_one(1)
1721
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
1722
        test_file = content_api.create(
1723
            content_type_slug=content_type_list.File.slug,
1724
            workspace=business_workspace,
1725
            parent=tool_folder,
1726
            label='Test file',
1727
            do_save=True,
1728
            do_notify=False,
1729
        )
1730
        dbsession.flush()
1731
        transaction.commit()
1732
1733
        self.testapp.authorization = (
1734
            'Basic',
1735
            (
1736
                '[email protected]',
1737
                '[email protected]'
1738
            )
1739
        )
1740
        res = self.testapp.get(
1741
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
1742
            status=200
1743
        )
1744
        content = res.json_body
1745
        assert content['content_type'] == 'file'
1746
        assert content['content_id'] == test_file.content_id
1747
        assert content['is_archived'] is False
1748
        assert content['is_deleted'] is False
1749
        assert content['is_editable'] is True
1750
        assert content['label'] == 'Test file'
1751
        assert content['parent_id'] == 1
1752
        assert content['show_in_ui'] is True
1753
        assert content['slug'] == 'test-file'
1754
        assert content['status'] == 'open'
1755
        assert content['workspace_id'] == 1
1756
        assert content['current_revision_id']
1757
        # TODO - G.M - 2018-06-173 - check date format
1758
        assert content['created']
1759
        assert content['author']
1760
        assert content['author']['user_id'] == 1
1761
        assert content['author']['avatar_url'] is None
1762
        assert content['author']['public_name'] == 'Global manager'
1763
        # TODO - G.M - 2018-06-173 - check date format
1764
        assert content['modified']
1765
        assert content['last_modifier'] == content['author']
1766
        assert content['raw_content'] == ''
1767
        assert content['mimetype'] == ''
1768
        assert content['file_extension'] == ''
1769
        assert content['filename'] == 'Test file'
1770
        assert content['size'] is None
1771
        assert content['page_nb'] is None
1772
        assert content['has_pdf_preview'] is False
1773
        assert content['has_jpeg_preview'] is False
1774
1775
    def test_api__get_file__ok_200__binary_file(self) -> None:
1776
        """
1777
        Get one file of a content
1778
        """
1779
        dbsession = get_tm_session(self.session_factory,
1780
                                   transaction.manager)
1781
        admin = dbsession.query(models.User) \
1782
            .filter(models.User.email == '[email protected]') \
1783
            .one()
1784
        workspace_api = WorkspaceApi(
1785
            current_user=admin,
1786
            session=dbsession,
1787
            config=self.app_config
1788
        )
1789
        content_api = ContentApi(
1790
            current_user=admin,
1791
            session=dbsession,
1792
            config=self.app_config
1793
        )
1794
        business_workspace = workspace_api.get_one(1)
1795
        tool_folder = content_api.get_one(1,
1796
                                          content_type=content_type_list.Any_SLUG)
1797
        test_file = content_api.create(
1798
            content_type_slug=content_type_list.File.slug,
1799
            workspace=business_workspace,
1800
            parent=tool_folder,
1801
            label='Test file',
1802
            do_save=False,
1803
            do_notify=False,
1804
        )
1805
        with new_revision(
1806
            session=dbsession,
1807
            tm=transaction.manager,
1808
            content=test_file,
1809
        ):
1810
            content_api.update_file_data(
1811
                test_file,
1812
                'Test_file.bin',
1813
                new_mimetype='application/octet-stream',
1814
                new_content=bytes(100),
1815
            )
1816
        content_api.save(test_file)
1817
        dbsession.flush()
1818
        transaction.commit()
1819
1820
        self.testapp.authorization = (
1821
            'Basic',
1822
            (
1823
                '[email protected]',
1824
                '[email protected]'
1825
            )
1826
        )
1827
        res = self.testapp.get(
1828
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
1829
            status=200
1830
        )
1831
        content = res.json_body
1832
        assert content['content_type'] == 'file'
1833
        assert content['content_id'] == test_file.content_id
1834
        assert content['is_archived'] is False
1835
        assert content['is_deleted'] is False
1836
        assert content['is_editable'] is True
1837
        assert content['label'] == 'Test_file'
1838
        assert content['parent_id'] == 1
1839
        assert content['show_in_ui'] is True
1840
        assert content['slug'] == 'test-file'
1841
        assert content['status'] == 'open'
1842
        assert content['workspace_id'] == 1
1843
        assert content['current_revision_id']
1844
        # TODO - G.M - 2018-06-173 - check date format
1845
        assert content['created']
1846
        assert content['author']
1847
        assert content['author']['user_id'] == 1
1848
        assert content['author']['avatar_url'] is None
1849
        assert content['author']['public_name'] == 'Global manager'
1850
        # TODO - G.M - 2018-06-173 - check date format
1851
        assert content['modified']
1852
        assert content['last_modifier'] == content['author']
1853
        assert content['raw_content'] == ''
1854
        assert content['mimetype'] == 'application/octet-stream'
1855
        assert content['size'] == 100
1856
        assert content['file_extension'] == '.bin'
1857
        assert content['filename'] == 'Test_file.bin'
1858
        assert content['page_nb'] is None
1859
        assert content['has_pdf_preview'] is False
1860
        assert content['has_jpeg_preview'] is False
1861
1862
    def test_api__get_files__err_400__wrong_content_type(self) -> None:
1863
        """
1864
        Get one file of a content content
1865
        """
1866
        self.testapp.authorization = (
1867
            'Basic',
1868
            (
1869
                '[email protected]',
1870
                '[email protected]'
1871
            )
1872
        )
1873
        res = self.testapp.get(
1874
            '/api/v2/workspaces/2/files/6',
1875
            status=400
1876
        )
1877
        assert res.json_body
1878
        assert 'code' in res.json_body
1879
        assert res.json_body['code'] == error.CONTENT_TYPE_NOT_ALLOWED
1880
1881
    def test_api__get_file__err_400__content_does_not_exist(self) -> None:  # nopep8
1882
        """
1883
        Get one file (content 170 does not exist in db
1884
        """
1885
        self.testapp.authorization = (
1886
            'Basic',
1887
            (
1888
                '[email protected]',
1889
                '[email protected]'
1890
            )
1891
        )
1892
        res = self.testapp.get(
1893
            '/api/v2/workspaces/1/files/170',
1894
            status=400
1895
        )
1896
        assert res.json_body
1897
        assert 'code' in res.json_body
1898
        assert res.json_body['code'] == error.CONTENT_NOT_FOUND
1899
1900
    def test_api__get_file__err_400__content_not_in_workspace(self) -> None:  # nopep8
1901
        """
1902
        Get one file (content 9 is in workspace 2)
1903
        """
1904
        self.testapp.authorization = (
1905
            'Basic',
1906
            (
1907
                '[email protected]',
1908
                '[email protected]'
1909
            )
1910
        )
1911
        res = self.testapp.get(
1912
            '/api/v2/workspaces/1/files/9',
1913
            status=400
1914
        )
1915
        assert res.json_body
1916
        assert 'code' in res.json_body
1917
        assert res.json_body['code'] == error.CONTENT_NOT_FOUND
1918
1919
    def test_api__get_file__err_400__workspace_does_not_exist(self) -> None:  # nopep8
1920
        """
1921
        Get one file (Workspace 40 does not exist)
1922
        """
1923
        self.testapp.authorization = (
1924
            'Basic',
1925
            (
1926
                '[email protected]',
1927
                '[email protected]'
1928
            )
1929
        )
1930
        res = self.testapp.get(
1931
            '/api/v2/workspaces/40/files/9',
1932
            status=400
1933
        )
1934
        assert res.json_body
1935
        assert 'code' in res.json_body
1936
        assert res.json_body['code'] == error.WORKSPACE_NOT_FOUND
1937
1938
    def test_api__get_file__err_400__workspace_id_is_not_int(self) -> None:  # nopep8
1939
        """
1940
        Get one file, workspace id is not int
1941
        """
1942
        self.testapp.authorization = (
1943
            'Basic',
1944
            (
1945
                '[email protected]',
1946
                '[email protected]'
1947
            )
1948
        )
1949
        res = self.testapp.get(
1950
            '/api/v2/workspaces/coucou/files/9',
1951
            status=400
1952
        )
1953
        assert res.json_body
1954
        assert 'code' in res.json_body
1955
        assert res.json_body['code'] == error.WORKSPACE_INVALID_ID
1956
1957
    def test_api__get_file__err_400__content_id_is_not_int(self) -> None:  # nopep8
1958
        """
1959
        Get one file, content_id is not int
1960
        """
1961
        self.testapp.authorization = (
1962
            'Basic',
1963
            (
1964
                '[email protected]',
1965
                '[email protected]'
1966
            )
1967
        )
1968
        res = self.testapp.get(
1969
            '/api/v2/workspaces/2/files/coucou',
1970
            status=400
1971
        )
1972
        assert res.json_body
1973
        assert 'code' in res.json_body
1974
        assert res.json_body['code'] == error.CONTENT_INVALID_ID
1975
1976
    def test_api__update_file_info_err_400__empty_label(self) -> None:  # nopep8
1977
        """
1978
        Update(put) one file
1979
        """
1980
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1981
        admin = dbsession.query(models.User) \
1982
            .filter(models.User.email == '[email protected]') \
1983
            .one()
1984
        workspace_api = WorkspaceApi(
1985
            current_user=admin,
1986
            session=dbsession,
1987
            config=self.app_config
1988
        )
1989
        content_api = ContentApi(
1990
            current_user=admin,
1991
            session=dbsession,
1992
            config=self.app_config
1993
        )
1994
        business_workspace = workspace_api.get_one(1)
1995
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
1996
        test_file = content_api.create(
1997
            content_type_slug=content_type_list.File.slug,
1998
            workspace=business_workspace,
1999
            parent=tool_folder,
2000
            label='Test file',
2001
            do_save=False,
2002
            do_notify=False,
2003
        )
2004
        with new_revision(
2005
            session=dbsession,
2006
            tm=transaction.manager,
2007
            content=test_file,
2008
        ):
2009
            content_api.update_file_data(
2010
                test_file,
2011
                'Test_file.txt',
2012
                new_mimetype='plain/text',
2013
                new_content=b'Test file',
2014
            )
2015
        with new_revision(
2016
            session=dbsession,
2017
            tm=transaction.manager,
2018
            content=test_file,
2019
        ):
2020
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
2021
            dbsession.flush()
2022
        transaction.commit()
2023
2024
        self.testapp.authorization = (
2025
            'Basic',
2026
            (
2027
                '[email protected]',
2028
                '[email protected]'
2029
            )
2030
        )
2031
        params = {
2032
            'label': '',
2033
            'raw_content': '<p> Le nouveau contenu </p>',
2034
        }
2035
        res = self.testapp.put_json(
2036
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2037
            params=params,
2038
            status=400
2039
        )
2040
        # INFO - G.M - 2018-09-10 - Handle by marshmallow schema
2041
        assert res.json_body
2042
        assert 'code' in res.json_body
2043
        assert res.json_body['code'] == error.GENERIC_SCHEMA_VALIDATION_ERROR  # nopep8
2044
2045
    def test_api__update_file_info__ok_200__nominal_case(self) -> None:
2046
        """
2047
        Update(put) one file
2048
        """
2049
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2050
        admin = dbsession.query(models.User) \
2051
            .filter(models.User.email == '[email protected]') \
2052
            .one()
2053
        workspace_api = WorkspaceApi(
2054
            current_user=admin,
2055
            session=dbsession,
2056
            config=self.app_config
2057
        )
2058
        content_api = ContentApi(
2059
            current_user=admin,
2060
            session=dbsession,
2061
            config=self.app_config
2062
        )
2063
        business_workspace = workspace_api.get_one(1)
2064
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
2065
        with dbsession.no_autoflush:
2066
            test_file = content_api.create(
2067
                content_type_slug=content_type_list.File.slug,
2068
                workspace=business_workspace,
2069
                parent=tool_folder,
2070
                label='Test file',
2071
                do_save=False,
2072
                do_notify=False,
2073
            )
2074
            content_api.update_file_data(
2075
                test_file,
2076
                'Test_file.txt',
2077
                new_mimetype='plain/text',
2078
                new_content=b'Test file',
2079
            )
2080
        with new_revision(
2081
            session=dbsession,
2082
            tm=transaction.manager,
2083
            content=test_file,
2084
        ):
2085
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
2086
        dbsession.flush()
2087
        transaction.commit()
2088
2089
        self.testapp.authorization = (
2090
            'Basic',
2091
            (
2092
                '[email protected]',
2093
                '[email protected]'
2094
            )
2095
        )
2096
        params = {
2097
            'label': 'My New label',
2098
            'raw_content': '<p> Le nouveau contenu </p>',
2099
        }
2100
        res = self.testapp.put_json(
2101
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2102
            params=params,
2103
            status=200
2104
        )
2105
        content = res.json_body
2106
        assert content['content_type'] == 'file'
2107
        assert content['content_id'] == test_file.content_id
2108
        assert content['is_archived'] is False
2109
        assert content['is_deleted'] is False
2110
        assert content['is_editable'] is True
2111
        assert content['label'] == 'My New label'
2112
        assert content['parent_id'] == 1
2113
        assert content['show_in_ui'] is True
2114
        assert content['slug'] == 'my-new-label'
2115
        assert content['status'] == 'open'
2116
        assert content['workspace_id'] == 1
2117
        assert content['current_revision_id']
2118
        # TODO - G.M - 2018-06-173 - check date format
2119
        assert content['created']
2120
        assert content['author']
2121
        assert content['author']['user_id'] == 1
2122
        assert content['author']['avatar_url'] is None
2123
        assert content['author']['public_name'] == 'Global manager'
2124
        # TODO - G.M - 2018-06-173 - check date format
2125
        assert content['modified']
2126
        assert content['last_modifier'] == content['author']
2127
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
2128
        assert content['mimetype'] == 'plain/text'
2129
        assert content['size'] == len(b'Test file')
2130
        assert content['page_nb'] == 1
2131
        assert content['has_pdf_preview'] is True
2132
        assert content['has_jpeg_preview'] is True
2133
2134
        res = self.testapp.get(
2135
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2136
            status=200
2137
        )
2138
        content = res.json_body
2139
        assert content['content_type'] == 'file'
2140
        assert content['content_id'] == test_file.content_id
2141
        assert content['is_archived'] is False
2142
        assert content['is_deleted'] is False
2143
        assert content['is_editable'] is True
2144
        assert content['label'] == 'My New label'
2145
        assert content['parent_id'] == 1
2146
        assert content['show_in_ui'] is True
2147
        assert content['slug'] == 'my-new-label'
2148
        assert content['status'] == 'open'
2149
        assert content['workspace_id'] == 1
2150
        assert content['current_revision_id']
2151
        # TODO - G.M - 2018-06-173 - check date format
2152
        assert content['created']
2153
        assert content['author']
2154
        assert content['author']['user_id'] == 1
2155
        assert content['author']['avatar_url'] is None
2156
        assert content['author']['public_name'] == 'Global manager'
2157
        # TODO - G.M - 2018-06-173 - check date format
2158
        assert content['modified']
2159
        assert content['last_modifier'] == content['author']
2160
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
2161
        assert content['mimetype'] == 'plain/text'
2162
        assert content['size'] == len(b'Test file')
2163
        assert content['page_nb'] == 1
2164
        assert content['has_pdf_preview'] is True
2165
        assert content['has_jpeg_preview'] is True
2166
2167
    def test_api__update_file_info__err_400__content_status_closed(self) -> None:
2168
        """
2169
        Update(put) one file
2170
        """
2171
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2172
        admin = dbsession.query(models.User) \
2173
            .filter(models.User.email == '[email protected]') \
2174
            .one()
2175
        workspace_api = WorkspaceApi(
2176
            current_user=admin,
2177
            session=dbsession,
2178
            config=self.app_config
2179
        )
2180
        content_api = ContentApi(
2181
            current_user=admin,
2182
            session=dbsession,
2183
            config=self.app_config
2184
        )
2185
        business_workspace = workspace_api.get_one(1)
2186
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
2187
        with dbsession.no_autoflush:
2188
            test_file = content_api.create(
2189
                content_type_slug=content_type_list.File.slug,
2190
                workspace=business_workspace,
2191
                parent=tool_folder,
2192
                label='Test file',
2193
                do_save=False,
2194
                do_notify=False,
2195
            )
2196
            content_api.update_file_data(
2197
                test_file,
2198
                'Test_file.txt',
2199
                new_mimetype='plain/text',
2200
                new_content=b'Test file',
2201
            )
2202
        with new_revision(
2203
            session=dbsession,
2204
            tm=transaction.manager,
2205
            content=test_file,
2206
        ):
2207
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
2208
        test_file.status = 'closed-validated'
2209
        content_api.save(test_file)
2210
        dbsession.flush()
2211
        transaction.commit()
2212
2213
        self.testapp.authorization = (
2214
            'Basic',
2215
            (
2216
                '[email protected]',
2217
                '[email protected]'
2218
            )
2219
        )
2220
        params = {
2221
            'label': 'My New label',
2222
            'raw_content': '<p> Le nouveau contenu </p>',
2223
        }
2224
        res = self.testapp.put_json(
2225
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2226
            params=params,
2227
            status=400
2228
        )
2229
        assert isinstance(res.json, dict)
2230
        assert 'code' in res.json.keys()
2231
        assert res.json_body['code'] == error.CONTENT_IN_NOT_EDITABLE_STATE
2232
2233
    def test_api__update_file_info__err_400__content_deleted(self) -> None:
2234
        """
2235
        Update(put) one file
2236
        """
2237
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2238
        admin = dbsession.query(models.User) \
2239
            .filter(models.User.email == '[email protected]') \
2240
            .one()
2241
        workspace_api = WorkspaceApi(
2242
            current_user=admin,
2243
            session=dbsession,
2244
            config=self.app_config
2245
        )
2246
        content_api = ContentApi(
2247
            current_user=admin,
2248
            session=dbsession,
2249
            config=self.app_config,
2250
            show_deleted=True,
2251
        )
2252
        business_workspace = workspace_api.get_one(1)
2253
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
2254
        with dbsession.no_autoflush:
2255
            test_file = content_api.create(
2256
                content_type_slug=content_type_list.File.slug,
2257
                workspace=business_workspace,
2258
                parent=tool_folder,
2259
                label='Test file',
2260
                do_save=False,
2261
                do_notify=False,
2262
            )
2263
            content_api.update_file_data(
2264
                test_file,
2265
                'Test_file.txt',
2266
                new_mimetype='plain/text',
2267
                new_content=b'Test file',
2268
            )
2269
        with new_revision(
2270
            session=dbsession,
2271
            tm=transaction.manager,
2272
            content=test_file,
2273
        ):
2274
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
2275
            test_file.is_deleted = True
2276
        content_api.save(test_file)
2277
        dbsession.flush()
2278
        transaction.commit()
2279
2280
        self.testapp.authorization = (
2281
            'Basic',
2282
            (
2283
                '[email protected]',
2284
                '[email protected]'
2285
            )
2286
        )
2287
        params = {
2288
            'label': 'My New label',
2289
            'raw_content': '<p> Le nouveau contenu </p>',
2290
        }
2291
        res = self.testapp.put_json(
2292
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2293
            params=params,
2294
            status=400
2295
        )
2296
        assert isinstance(res.json, dict)
2297
        assert 'code' in res.json.keys()
2298
        assert res.json_body['code'] == error.CONTENT_IN_NOT_EDITABLE_STATE
2299
2300
    def test_api__update_file_info__err_400__content_archived(self) -> None:
2301
        """
2302
        Update(put) one file
2303
        """
2304
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2305
        admin = dbsession.query(models.User) \
2306
            .filter(models.User.email == '[email protected]') \
2307
            .one()
2308
        workspace_api = WorkspaceApi(
2309
            current_user=admin,
2310
            session=dbsession,
2311
            config=self.app_config
2312
        )
2313
        content_api = ContentApi(
2314
            current_user=admin,
2315
            session=dbsession,
2316
            config=self.app_config,
2317
            show_deleted=True,
2318
        )
2319
        business_workspace = workspace_api.get_one(1)
2320
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
2321
        with dbsession.no_autoflush:
2322
            test_file = content_api.create(
2323
                content_type_slug=content_type_list.File.slug,
2324
                workspace=business_workspace,
2325
                parent=tool_folder,
2326
                label='Test file',
2327
                do_save=False,
2328
                do_notify=False,
2329
            )
2330
            content_api.update_file_data(
2331
                test_file,
2332
                'Test_file.txt',
2333
                new_mimetype='plain/text',
2334
                new_content=b'Test file',
2335
            )
2336
        with new_revision(
2337
            session=dbsession,
2338
            tm=transaction.manager,
2339
            content=test_file,
2340
        ):
2341
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
2342
            test_file.is_archived = True
2343
        content_api.save(test_file)
2344
        dbsession.flush()
2345
        transaction.commit()
2346
2347
        self.testapp.authorization = (
2348
            'Basic',
2349
            (
2350
                '[email protected]',
2351
                '[email protected]'
2352
            )
2353
        )
2354
        params = {
2355
            'label': 'My New label',
2356
            'raw_content': '<p> Le nouveau contenu </p>',
2357
        }
2358
        res = self.testapp.put_json(
2359
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2360
            params=params,
2361
            status=400
2362
        )
2363
        assert isinstance(res.json, dict)
2364
        assert 'code' in res.json.keys()
2365
        assert res.json_body['code'] == error.CONTENT_IN_NOT_EDITABLE_STATE
2366
2367
    def test_api__update_file_info__err_400__not_modified(self) -> None:
2368
        """
2369
        Update(put) one file
2370
        """
2371
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2372
        admin = dbsession.query(models.User) \
2373
            .filter(models.User.email == '[email protected]') \
2374
            .one()
2375
        workspace_api = WorkspaceApi(
2376
            current_user=admin,
2377
            session=dbsession,
2378
            config=self.app_config
2379
        )
2380
        content_api = ContentApi(
2381
            current_user=admin,
2382
            session=dbsession,
2383
            config=self.app_config
2384
        )
2385
        business_workspace = workspace_api.get_one(1)
2386
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
2387
        test_file = content_api.create(
2388
            content_type_slug=content_type_list.File.slug,
2389
            workspace=business_workspace,
2390
            parent=tool_folder,
2391
            label='Test file',
2392
            do_save=False,
2393
            do_notify=False,
2394
        )
2395
        with new_revision(
2396
            session=dbsession,
2397
            tm=transaction.manager,
2398
            content=test_file,
2399
        ):
2400
            content_api.update_file_data(
2401
                test_file,
2402
                'Test_file.txt',
2403
                new_mimetype='plain/text',
2404
                new_content=b'Test file',
2405
            )
2406
        with new_revision(
2407
            session=dbsession,
2408
            tm=transaction.manager,
2409
            content=test_file,
2410
        ):
2411
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
2412
        dbsession.flush()
2413
        transaction.commit()
2414
2415
        self.testapp.authorization = (
2416
            'Basic',
2417
            (
2418
                '[email protected]',
2419
                '[email protected]'
2420
            )
2421
        )
2422
        params = {
2423
            'label': 'My New label',
2424
            'raw_content': '<p> Le nouveau contenu </p>',
2425
        }
2426
        res = self.testapp.put_json(
2427
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2428
            params=params,
2429
            status=200
2430
        )
2431
        content = res.json_body
2432
        assert content['content_type'] == 'file'
2433
        assert content['content_id'] == test_file.content_id
2434
        assert content['is_archived'] is False
2435
        assert content['is_deleted'] is False
2436
        assert content['is_editable'] is True
2437
        assert content['label'] == 'My New label'
2438
        assert content['parent_id'] == 1
2439
        assert content['show_in_ui'] is True
2440
        assert content['slug'] == 'my-new-label'
2441
        assert content['status'] == 'open'
2442
        assert content['workspace_id'] == 1
2443
        assert content['current_revision_id']
2444
        # TODO - G.M - 2018-06-173 - check date format
2445
        assert content['created']
2446
        assert content['author']
2447
        assert content['author']['user_id'] == 1
2448
        assert content['author']['avatar_url'] is None
2449
        assert content['author']['public_name'] == 'Global manager'
2450
        # TODO - G.M - 2018-06-173 - check date format
2451
        assert content['modified']
2452
        assert content['last_modifier'] == content['author']
2453
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
2454
        assert content['mimetype'] == 'plain/text'
2455
        assert content['size'] == len(b'Test file')
2456
        assert content['page_nb'] == 1
2457
        assert content['has_pdf_preview'] is True
2458
        assert content['has_jpeg_preview'] is True
2459
2460
        res = self.testapp.get(
2461
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2462
            status=200
2463
        )
2464
        content = res.json_body
2465
        assert content['content_type'] == 'file'
2466
        assert content['content_id'] == test_file.content_id
2467
        assert content['is_archived'] is False
2468
        assert content['is_deleted'] is False
2469
        assert content['is_editable'] is True
2470
        assert content['label'] == 'My New label'
2471
        assert content['parent_id'] == 1
2472
        assert content['show_in_ui'] is True
2473
        assert content['slug'] == 'my-new-label'
2474
        assert content['status'] == 'open'
2475
        assert content['workspace_id'] == 1
2476
        assert content['current_revision_id']
2477
        # TODO - G.M - 2018-06-173 - check date format
2478
        assert content['created']
2479
        assert content['author']
2480
        assert content['author']['user_id'] == 1
2481
        assert content['author']['avatar_url'] is None
2482
        assert content['author']['public_name'] == 'Global manager'
2483
        # TODO - G.M - 2018-06-173 - check date format
2484
        assert content['modified']
2485
        assert content['last_modifier'] == content['author']
2486
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
2487
        assert content['mimetype'] == 'plain/text'
2488
        assert content['size'] == len(b'Test file')
2489
        assert content['page_nb'] == 1
2490
        assert content['has_pdf_preview'] is True
2491
        assert content['has_jpeg_preview'] is True
2492
2493
        res = self.testapp.put_json(
2494
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2495
            params=params,
2496
            status=400
2497
        )
2498
        assert res.json_body
2499
        assert 'code' in res.json_body
2500
        assert res.json_body['code'] == error.SAME_VALUE_ERROR
2501
2502
    def test_api__update_file_info__err_400__label_already_used(self) -> None:
2503
        """
2504
        Update(put) one file, failed because label already used
2505
        """
2506
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2507
        admin = dbsession.query(models.User) \
2508
            .filter(models.User.email == '[email protected]') \
2509
            .one()
2510
        workspace_api = WorkspaceApi(
2511
            current_user=admin,
2512
            session=dbsession,
2513
            config=self.app_config
2514
        )
2515
        content_api = ContentApi(
2516
            current_user=admin,
2517
            session=dbsession,
2518
            config=self.app_config
2519
        )
2520
        business_workspace = workspace_api.get_one(1)
2521
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
2522
        content_api.create(
2523
            content_type_slug=content_type_list.File.slug,
2524
            workspace=business_workspace,
2525
            parent=tool_folder,
2526
            label='folder_used',
2527
            do_save=True,
2528
            do_notify=False,
2529
        )
2530
        with dbsession.no_autoflush:
2531
            test_file = content_api.create(
2532
                content_type_slug=content_type_list.File.slug,
2533
                workspace=business_workspace,
2534
                parent=tool_folder,
2535
                label='Test file',
2536
                do_save=False,
2537
                do_notify=False,
2538
            )
2539
            test_file.file_extension = '.txt'
2540
            test_file.depot_file = FileIntent(
2541
                b'Test file',
2542
                'Test_file.txt',
2543
                'text/plain',
2544
            )
2545
        test_file2 = content_api.create(
2546
            content_type_slug=content_type_list.File.slug,
2547
            workspace=business_workspace,
2548
            parent=tool_folder,
2549
            filename='already_used.txt',
2550
            do_save=False,
2551
            do_notify=False,
2552
        )
2553
        test_file2.file_extension = '.txt'
2554
        test_file2.depot_file = FileIntent(
2555
            b'Test file',
2556
            'already_used.txt',
2557
            'text/plain',
2558
        )
2559
        with new_revision(
2560
            session=dbsession,
2561
            tm=transaction.manager,
2562
            content=test_file,
2563
        ):
2564
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
2565
        dbsession.flush()
2566
        transaction.commit()
2567
2568
        self.testapp.authorization = (
2569
            'Basic',
2570
            (
2571
                '[email protected]',
2572
                '[email protected]'
2573
            )
2574
        )
2575
        params = {
2576
            'label': 'folder_used',
2577
            'raw_content': '<p> Le nouveau contenu </p>',
2578
        }
2579
        res = self.testapp.put_json(
2580
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2581
            params=params,
2582
            status=200
2583
        )
2584
        params = {
2585
            'label': 'already_used',
2586
            'raw_content': '<p> Le nouveau contenu </p>',
2587
        }
2588
        res = self.testapp.put_json(
2589
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2590
            params=params,
2591
            status=400
2592
        )
2593
        assert isinstance(res.json, dict)
2594
        assert 'code' in res.json.keys()
2595
        assert res.json_body['code'] == error.CONTENT_FILENAME_ALREADY_USED_IN_FOLDER  # nopep8
2596
2597
    def test_api__get_file_revisions__ok_200__nominal_case(
2598
            self
2599
    ) -> None:
2600
        """
2601
        Get file revisions
2602
        """
2603
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2604
        admin = dbsession.query(models.User) \
2605
            .filter(models.User.email == '[email protected]') \
2606
            .one()
2607
        workspace_api = WorkspaceApi(
2608
            current_user=admin,
2609
            session=dbsession,
2610
            config=self.app_config
2611
        )
2612
        content_api = ContentApi(
2613
            current_user=admin,
2614
            session=dbsession,
2615
            config=self.app_config
2616
        )
2617
        business_workspace = workspace_api.get_one(1)
2618
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
2619
        test_file = content_api.create(
2620
            content_type_slug=content_type_list.File.slug,
2621
            workspace=business_workspace,
2622
            parent=tool_folder,
2623
            label='Test file',
2624
            do_save=False,
2625
            do_notify=False,
2626
        )
2627
        with new_revision(
2628
            session=dbsession,
2629
            tm=transaction.manager,
2630
            content=test_file,
2631
        ):
2632
            content_api.update_file_data(
2633
                test_file,
2634
                'Test_file.txt',
2635
                new_mimetype='plain/text',
2636
                new_content=b'Test file',
2637
            )
2638
        with new_revision(
2639
            session=dbsession,
2640
            tm=transaction.manager,
2641
            content=test_file,
2642
        ):
2643
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
2644
            dbsession.flush()
2645
        transaction.commit()
2646
2647
        self.testapp.authorization = (
2648
            'Basic',
2649
            (
2650
                '[email protected]',
2651
                '[email protected]'
2652
            )
2653
        )
2654
        res = self.testapp.get(
2655
            '/api/v2/workspaces/1/files/{}/revisions'.format(test_file.content_id),  # nopep8
2656
            status=200
2657
        )
2658
        revisions = res.json_body
2659
        assert len(revisions) == 1
2660
        revision = revisions[0]
2661
        assert revision['content_type'] == 'file'
2662
        assert revision['content_id'] == test_file.content_id
2663
        assert revision['is_archived'] is False
2664
        assert revision['is_deleted'] is False
2665
        assert revision['is_editable'] is True
2666
        assert revision['label'] == 'Test_file'
2667
        assert revision['parent_id'] == 1
2668
        assert revision['show_in_ui'] is True
2669
        assert revision['slug'] == 'test-file'
2670
        assert revision['status'] == 'open'
2671
        assert revision['workspace_id'] == 1
2672
        assert revision['revision_id']
2673
        assert revision['sub_content_types']
2674
        # TODO - G.M - 2018-06-173 - Test with real comments
2675
        assert revision['comment_ids'] == []
2676
        # TODO - G.M - 2018-06-173 - check date format
2677
        assert revision['created']
2678
        assert revision['author']
2679
        assert revision['author']['user_id'] == 1
2680
        assert revision['author']['avatar_url'] is None
2681
        assert revision['author']['public_name'] == 'Global manager'
2682
        assert revision['mimetype'] == 'plain/text'
2683
        assert revision['size'] == len(b'Test file')
2684
        assert revision['page_nb'] == 1
2685
        assert revision['has_pdf_preview'] is True
2686
2687
    def test_api__set_file_status__ok_200__nominal_case(self) -> None:
2688
        """
2689
        set file status
2690
        """
2691
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2692
        admin = dbsession.query(models.User) \
2693
            .filter(models.User.email == '[email protected]') \
2694
            .one()
2695
        workspace_api = WorkspaceApi(
2696
            current_user=admin,
2697
            session=dbsession,
2698
            config=self.app_config
2699
        )
2700
        content_api = ContentApi(
2701
            current_user=admin,
2702
            session=dbsession,
2703
            config=self.app_config
2704
        )
2705
        business_workspace = workspace_api.get_one(1)
2706
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
2707
        test_file = content_api.create(
2708
            content_type_slug=content_type_list.File.slug,
2709
            workspace=business_workspace,
2710
            parent=tool_folder,
2711
            label='Test file',
2712
            do_save=False,
2713
            do_notify=False,
2714
        )
2715
        test_file.file_extension = '.txt'
2716
        test_file.depot_file = FileIntent(
2717
            b'Test file',
2718
            'Test_file.txt',
2719
            'text/plain',
2720
        )
2721
        with new_revision(
2722
            session=dbsession,
2723
            tm=transaction.manager,
2724
            content=test_file,
2725
        ):
2726
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
2727
        dbsession.flush()
2728
        transaction.commit()
2729
2730
        self.testapp.authorization = (
2731
            'Basic',
2732
            (
2733
                '[email protected]',
2734
                '[email protected]'
2735
            )
2736
        )
2737
        params = {
2738
            'status': 'closed-deprecated',
2739
        }
2740
2741
        # before
2742
        res = self.testapp.get(
2743
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2744
            status=200
2745
        )
2746
        content = res.json_body
2747
        assert content['content_type'] == 'file'
2748
        assert content['content_id'] == test_file.content_id
2749
        assert content['status'] == 'open'
2750
2751
        # set status
2752
        self.testapp.put_json(
2753
            '/api/v2/workspaces/1/files/{}/status'.format(test_file.content_id),
2754
            params=params,
2755
            status=204
2756
        )
2757
2758
        # after
2759
        res = self.testapp.get(
2760
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2761
            status=200
2762
        )
2763
        content = res.json_body
2764
        assert content['content_type'] == 'file'
2765
        assert content['content_id'] == test_file.content_id
2766
        assert content['status'] == 'closed-deprecated'
2767
2768
    def test_api__set_file_status__err_400__wrong_status(self) -> None:
2769
        """
2770
        set file status
2771
        """
2772
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2773
        admin = dbsession.query(models.User) \
2774
            .filter(models.User.email == '[email protected]') \
2775
            .one()
2776
        workspace_api = WorkspaceApi(
2777
            current_user=admin,
2778
            session=dbsession,
2779
            config=self.app_config
2780
        )
2781
        content_api = ContentApi(
2782
            current_user=admin,
2783
            session=dbsession,
2784
            config=self.app_config
2785
        )
2786
        business_workspace = workspace_api.get_one(1)
2787
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
2788
        test_file = content_api.create(
2789
            content_type_slug=content_type_list.File.slug,
2790
            workspace=business_workspace,
2791
            parent=tool_folder,
2792
            label='Test file',
2793
            do_save=False,
2794
            do_notify=False,
2795
        )
2796
        test_file.file_extension = '.txt'
2797
        test_file.depot_file = FileIntent(
2798
            b'Test file',
2799
            'Test_file.txt',
2800
            'text/plain',
2801
        )
2802
        with new_revision(
2803
            session=dbsession,
2804
            tm=transaction.manager,
2805
            content=test_file,
2806
        ):
2807
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
2808
        dbsession.flush()
2809
        transaction.commit()
2810
        self.testapp.authorization = (
2811
            'Basic',
2812
            (
2813
                '[email protected]',
2814
                '[email protected]'
2815
            )
2816
        )
2817
        params = {
2818
            'status': 'unexistant-status',
2819
        }
2820
2821
        # before
2822
        res = self.testapp.get(
2823
            '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
2824
            status=200
2825
        )
2826
        content = res.json_body
2827
        assert content['content_type'] == 'file'
2828
        assert content['content_id'] == test_file.content_id
2829
        assert content['status'] == 'open'
2830
2831
        # set status
2832
        res = self.testapp.put_json(
2833
            '/api/v2/workspaces/1/files/{}/status'.format(test_file.content_id),
2834
            params=params,
2835
            status=400
2836
        )
2837
        assert isinstance(res.json, dict)
2838
        assert 'code' in res.json.keys()
2839
        assert res.json_body['code'] == error.GENERIC_SCHEMA_VALIDATION_ERROR  # nopep8
2840
2841
    def test_api__get_file_raw__ok_200__nominal_case(self) -> None:
2842
        """
2843
        Get one file of a content
2844
        """
2845
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2846
        admin = dbsession.query(models.User) \
2847
            .filter(models.User.email == '[email protected]') \
2848
            .one()
2849
        workspace_api = WorkspaceApi(
2850
            current_user=admin,
2851
            session=dbsession,
2852
            config=self.app_config
2853
        )
2854
        content_api = ContentApi(
2855
            current_user=admin,
2856
            session=dbsession,
2857
            config=self.app_config
2858
        )
2859
        business_workspace = workspace_api.get_one(1)
2860
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
2861
        test_file = content_api.create(
2862
            content_type_slug=content_type_list.File.slug,
2863
            workspace=business_workspace,
2864
            parent=tool_folder,
2865
            label='Test file',
2866
            do_save=False,
2867
            do_notify=False,
2868
        )
2869
        test_file.file_extension = '.txt'
2870
        test_file.depot_file = FileIntent(
2871
            b'Test file',
2872
            'Test_file.txt',
2873
            'text/plain',
2874
        )
2875
        with new_revision(
2876
            session=dbsession,
2877
            tm=transaction.manager,
2878
            content=test_file,
2879
        ):
2880
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
2881
            dbsession.flush()
2882
        transaction.commit()
2883
        content_id = int(test_file.content_id)
2884
        self.testapp.authorization = (
2885
            'Basic',
2886
            (
2887
                '[email protected]',
2888
                '[email protected]'
2889
            )
2890
        )
2891
        filename = 'Test_file.txt'
2892
        res = self.testapp.get(
2893
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, filename),
2894
            status=200
2895
        )
2896
        assert res.body == b'Test file'
2897
        assert res.content_type == 'text/plain'
2898
        assert res.content_length == len(b'Test file')
2899
2900
    def test_api__get_file_raw__ok_200__force_download_case(self) -> None:
2901
        """
2902
        Get one file of a content
2903
        """
2904
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2905
        admin = dbsession.query(models.User) \
2906
            .filter(models.User.email == '[email protected]') \
2907
            .one()
2908
        workspace_api = WorkspaceApi(
2909
            current_user=admin,
2910
            session=dbsession,
2911
            config=self.app_config
2912
        )
2913
        content_api = ContentApi(
2914
            current_user=admin,
2915
            session=dbsession,
2916
            config=self.app_config
2917
        )
2918
        business_workspace = workspace_api.get_one(1)
2919
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
2920
        test_file = content_api.create(
2921
            content_type_slug=content_type_list.File.slug,
2922
            workspace=business_workspace,
2923
            parent=tool_folder,
2924
            label='Test file',
2925
            do_save=False,
2926
            do_notify=False,
2927
        )
2928
        with new_revision(
2929
            session=dbsession,
2930
            tm=transaction.manager,
2931
            content=test_file,
2932
        ):
2933
            content_api.update_file_data(
2934
                test_file,
2935
                new_content=b'Test file',
2936
                new_filename='Test_file.txt',
2937
                new_mimetype='text/plain',
2938
            )
2939
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
2940
        dbsession.flush()
2941
        transaction.commit()
2942
        content_id = int(test_file.content_id)
2943
        self.testapp.authorization = (
2944
            'Basic',
2945
            (
2946
                '[email protected]',
2947
                '[email protected]'
2948
            )
2949
        )
2950
        params = {
2951
            'force_download': 1,
2952
        }
2953
        filename = 'Test_file.txt'
2954
        res = self.testapp.get(
2955
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, filename),
2956
            status=200,
2957
            params=params
2958
        )
2959
        assert res.headers['Content-Disposition'] == 'attachment; filename="{}"; filename*=UTF-8\'\'{};'.format(filename, filename)  # nopep8
2960
        assert res.body == b'Test file'
2961
        assert res.content_type == 'text/plain'
2962
        assert res.content_length == len(b'Test file')
2963
2964
    def test_api__create_file__ok__200__nominal_case(self) -> None:
2965
        """
2966
        create one file of a content at workspace root
2967
        """
2968
2969
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2970
        admin = dbsession.query(models.User) \
2971
            .filter(models.User.email == '[email protected]') \
2972
            .one()
2973
        workspace_api = WorkspaceApi(
2974
            current_user=admin,
2975
            session=dbsession,
2976
            config=self.app_config
2977
        )
2978
        content_api = ContentApi(
2979
            current_user=admin,
2980
            session=dbsession,
2981
            config=self.app_config
2982
        )
2983
        business_workspace = workspace_api.get_one(1)
2984
2985
        self.testapp.authorization = (
2986
            'Basic',
2987
            (
2988
                '[email protected]',
2989
                '[email protected]'
2990
            )
2991
        )
2992
        image = create_1000px_png_test_image()
2993
        res = self.testapp.post(
2994
            '/api/v2/workspaces/{}/files'.format(business_workspace.workspace_id),
2995
            upload_files=[
2996
                ('files', image.name, image.getvalue())
2997
            ],
2998
            status=200,
2999
        )
3000
        res = res.json_body
3001
        assert res['parent_id'] is None
3002
        assert res['content_type'] == 'file'
3003
        assert res['is_archived'] is False
3004
        assert res['is_deleted'] is False
3005
        assert res['is_editable'] is True
3006
        assert res['workspace_id'] == business_workspace.workspace_id
3007
        assert isinstance(res['content_id'], int)
3008
        content_id = res['content_id']
3009
        assert res['status'] == 'open'
3010
        assert res['label'] == 'test_image'
3011
        assert res['slug'] == 'test-image'
3012
3013
        res = self.testapp.get(
3014
            '/api/v2/workspaces/{workspace_id}/files/{content_id}'.format(
3015
                workspace_id=business_workspace.workspace_id,
3016
                content_id=content_id
3017
            ),
3018
            status=200,
3019
        )
3020
3021
        res = res.json_body
3022
        assert res['parent_id'] is None
3023
        assert res['content_type'] == 'file'
3024
        assert res['is_archived'] is False
3025
        assert res['is_deleted'] is False
3026
        assert res['is_editable'] is True
3027
        assert res['workspace_id'] == business_workspace.workspace_id
3028
        assert isinstance(res['content_id'], int)
3029
        content_id = res['content_id']
3030
        assert res['status'] == 'open'
3031
        assert res['label'] == 'test_image'
3032
        assert res['slug'] == 'test-image'
3033
        assert res['author']['user_id'] == admin.user_id
3034
        assert res['page_nb'] == 1
3035
        assert res['mimetype'] == 'image/png'
3036
3037
    def test_api__create_file__err_400__filename_already_used(self) -> None:
3038
        """
3039
        create one file of a content but filename is already used here
3040
        """
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
3058
        self.testapp.authorization = (
3059
            'Basic',
3060
            (
3061
                '[email protected]',
3062
                '[email protected]'
3063
            )
3064
        )
3065
        image = create_1000px_png_test_image()
3066
        res = self.testapp.post(
3067
            '/api/v2/workspaces/{}/files'.format(business_workspace.workspace_id),
3068
            upload_files=[
3069
                ('files', image.name, image.getvalue())
3070
            ],
3071
            status=200,
3072
        )
3073
        res = res.json_body
3074
        assert res['parent_id'] is None
3075
        assert res['content_type'] == 'file'
3076
        assert res['is_archived'] is False
3077
        assert res['is_deleted'] is False
3078
        assert res['is_editable'] is True
3079
        assert res['workspace_id'] == business_workspace.workspace_id
3080
        assert isinstance(res['content_id'], int)
3081
        content_id = res['content_id']
3082
        assert res['status'] == 'open'
3083
        assert res['label'] == 'test_image'
3084
        assert res['slug'] == 'test-image'
3085
3086
        res = self.testapp.post(
3087
            '/api/v2/workspaces/{}/files'.format(business_workspace.workspace_id),
3088
            upload_files=[
3089
                ('files', image.name, image.getvalue())
3090
            ],
3091
            status=400,
3092
        )
3093
        assert isinstance(res.json, dict)
3094
        assert 'code' in res.json.keys()
3095
        assert res.json_body['code'] == error.CONTENT_FILENAME_ALREADY_USED_IN_FOLDER
3096
3097
    def test_api__create_file__ok__200__in_folder(self) -> None:
3098
        """
3099
        create one file of a content in a folder
3100
        """
3101
3102
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3103
        admin = dbsession.query(models.User) \
3104
            .filter(models.User.email == '[email protected]') \
3105
            .one()
3106
        workspace_api = WorkspaceApi(
3107
            current_user=admin,
3108
            session=dbsession,
3109
            config=self.app_config
3110
        )
3111
        content_api = ContentApi(
3112
            current_user=admin,
3113
            session=dbsession,
3114
            config=self.app_config
3115
        )
3116
        business_workspace = workspace_api.get_one(1)
3117
        folder = content_api.create(
3118
            label='test-folder',
3119
            content_type_slug=content_type_list.Folder.slug,
3120
            workspace=business_workspace,
3121
            do_save=True,
3122
            do_notify=False
3123
        )
3124
        transaction.commit()
3125
        self.testapp.authorization = (
3126
            'Basic',
3127
            (
3128
                '[email protected]',
3129
                '[email protected]'
3130
            )
3131
        )
3132
        params = {
3133
            'parent_id': folder.content_id,
3134
        }
3135
        image = create_1000px_png_test_image()
3136
        res = self.testapp.post(
3137
            '/api/v2/workspaces/{}/files'.format(business_workspace.workspace_id),
3138
            upload_files=[
3139
                ('files', image.name, image.getvalue())
3140
            ],
3141
            params=params,
3142
            status=200,
3143
        )
3144
        res = res.json_body
3145
        assert res['parent_id'] == folder.content_id
3146
        assert res['content_type'] == 'file'
3147
        assert res['is_archived'] is False
3148
        assert res['is_deleted'] is False
3149
        assert res['is_editable'] is True
3150
        assert res['workspace_id'] == business_workspace.workspace_id
3151
        assert isinstance(res['content_id'], int)
3152
        content_id = res['content_id']
3153
        assert res['status'] == 'open'
3154
        assert res['label'] == 'test_image'
3155
        assert res['slug'] == 'test-image'
3156
3157
        res = self.testapp.get(
3158
            '/api/v2/workspaces/{workspace_id}/files/{content_id}'.format(
3159
                workspace_id=business_workspace.workspace_id,
3160
                content_id=content_id
3161
            ),
3162
            status=200,
3163
        )
3164
3165
        res = res.json_body
3166
        assert res['parent_id'] == folder.content_id
3167
        assert res['content_type'] == 'file'
3168
        assert res['is_archived'] is False
3169
        assert res['is_deleted'] is False
3170
        assert res['is_editable'] is True
3171
        assert res['workspace_id'] == business_workspace.workspace_id
3172
        assert isinstance(res['content_id'], int)
3173
        content_id = res['content_id']
3174
        assert res['status'] == 'open'
3175
        assert res['label'] == 'test_image'
3176
        assert res['slug'] == 'test-image'
3177
        assert res['author']['user_id'] == admin.user_id
3178
        assert res['page_nb'] == 1
3179
        assert res['mimetype'] == 'image/png'
3180
3181
    def test_api__create_file__err__400__unallow_subcontent(self) -> None:
3182
        """
3183
        create one file of a content but subcontent of type file unallowed here
3184
        """
3185
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3186
        admin = dbsession.query(models.User) \
3187
            .filter(models.User.email == '[email protected]') \
3188
            .one()
3189
        workspace_api = WorkspaceApi(
3190
            current_user=admin,
3191
            session=dbsession,
3192
            config=self.app_config
3193
        )
3194
        content_api = ContentApi(
3195
            current_user=admin,
3196
            session=dbsession,
3197
            config=self.app_config
3198
        )
3199
        business_workspace = workspace_api.get_one(1)
3200
        folder = content_api.create(
3201
            label='test-folder',
3202
            content_type_slug=content_type_list.Folder.slug,
3203
            workspace=business_workspace,
3204
            do_save=True,
3205
            do_notify=False
3206
        )
3207
        with new_revision(
3208
                session=dbsession,
3209
                tm=transaction.manager,
3210
                content=folder,
3211
        ):
3212
            content_api.set_allowed_content(folder, [])
3213
        content_api.save(folder)
3214
        transaction.commit()
3215
        self.testapp.authorization = (
3216
            'Basic',
3217
            (
3218
                '[email protected]',
3219
                '[email protected]'
3220
            )
3221
        )
3222
        params = {
3223
            'parent_id': folder.content_id,
3224
        }
3225
        image = create_1000px_png_test_image()
3226
        res = self.testapp.post(
3227
            '/api/v2/workspaces/{}/files'.format(business_workspace.workspace_id),
3228
            upload_files=[
3229
                ('files', image.name, image.getvalue())
3230
            ],
3231
            params=params,
3232
            status=400,
3233
        )
3234
        assert isinstance(res.json, dict)
3235
        assert 'code' in res.json.keys()
3236
        assert res.json_body['code'] == error.UNALLOWED_SUBCONTENT
3237
3238
    def test_api__create_file__err__400__parent_not_found(self) -> None:
3239
        """
3240
        create one file of a content but parent_id is not valid
3241
        """
3242
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3243
        admin = dbsession.query(models.User) \
3244
            .filter(models.User.email == '[email protected]') \
3245
            .one()
3246
        workspace_api = WorkspaceApi(
3247
            current_user=admin,
3248
            session=dbsession,
3249
            config=self.app_config
3250
        )
3251
        content_api = ContentApi(
3252
            current_user=admin,
3253
            session=dbsession,
3254
            config=self.app_config
3255
        )
3256
        business_workspace = workspace_api.get_one(1)
3257
3258
        self.testapp.authorization = (
3259
            'Basic',
3260
            (
3261
                '[email protected]',
3262
                '[email protected]'
3263
            )
3264
        )
3265
        params = {
3266
            'parent_id': 3000
3267
        }
3268
        image = create_1000px_png_test_image()
3269
        res = self.testapp.post(
3270
            '/api/v2/workspaces/{}/files'.format(business_workspace.workspace_id),
3271
            upload_files=[
3272
                ('files', image.name, image.getvalue())
3273
            ],
3274
            params=params,
3275
            status=400,
3276
        )
3277
        assert isinstance(res.json, dict)
3278
        assert 'code' in res.json.keys()
3279
        assert res.json_body['code'] == error.PARENT_NOT_FOUND
3280
3281 View Code Duplication
    def test_api__set_file_raw__ok_200__nominal_case(self) -> None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3282
        """
3283
        Set one file of a content
3284
        """
3285
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3286
        admin = dbsession.query(models.User) \
3287
            .filter(models.User.email == '[email protected]') \
3288
            .one()
3289
        workspace_api = WorkspaceApi(
3290
            current_user=admin,
3291
            session=dbsession,
3292
            config=self.app_config
3293
        )
3294
        content_api = ContentApi(
3295
            current_user=admin,
3296
            session=dbsession,
3297
            config=self.app_config
3298
        )
3299
        business_workspace = workspace_api.get_one(1)
3300
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3301
        test_file = content_api.create(
3302
            content_type_slug=content_type_list.File.slug,
3303
            workspace=business_workspace,
3304
            parent=tool_folder,
3305
            label='Test file',
3306
            do_save=False,
3307
            do_notify=False,
3308
        )
3309
        dbsession.flush()
3310
        transaction.commit()
3311
        content_id = int(test_file.content_id)
3312
        image = create_1000px_png_test_image()
3313
        self.testapp.authorization = (
3314
            'Basic',
3315
            (
3316
                '[email protected]',
3317
                '[email protected]'
3318
            )
3319
        )
3320
        self.testapp.put(
3321
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3322
            upload_files=[
3323
                ('files', image.name, image.getvalue())
3324
            ],
3325
            status=204,
3326
        )
3327
        res = self.testapp.get(
3328
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3329
            status=200
3330
        )
3331
        assert res.body == image.getvalue()
3332
        assert res.content_type == 'image/png'
3333
        assert res.content_length == len(image.getvalue())
3334
3335
    def test_api__set_file_raw__ok_200__filename_already_used(self) -> None:
3336
        """
3337
        Set one file of a content
3338
        """
3339
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3340
        admin = dbsession.query(models.User) \
3341
            .filter(models.User.email == '[email protected]') \
3342
            .one()
3343
        workspace_api = WorkspaceApi(
3344
            current_user=admin,
3345
            session=dbsession,
3346
            config=self.app_config
3347
        )
3348
        content_api = ContentApi(
3349
            current_user=admin,
3350
            session=dbsession,
3351
            config=self.app_config
3352
        )
3353
        business_workspace = workspace_api.get_one(1)
3354
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3355
        test_file = content_api.create(
3356
            content_type_slug=content_type_list.File.slug,
3357
            workspace=business_workspace,
3358
            parent=tool_folder,
3359
            label='Test file',
3360
            do_save=False,
3361
            do_notify=False,
3362
        )
3363
        test_file_2 = content_api.create(
3364
            content_type_slug=content_type_list.File.slug,
3365
            workspace=business_workspace,
3366
            parent=tool_folder,
3367
            label='Test file2',
3368
            do_save=False,
3369
            do_notify=False,
3370
        )
3371
        dbsession.flush()
3372
        transaction.commit()
3373
        content_id = int(test_file.content_id)
3374
        content2_id = int(test_file_2.content_id)
3375
        image = create_1000px_png_test_image()
3376
        self.testapp.authorization = (
3377
            'Basic',
3378
            (
3379
                '[email protected]',
3380
                '[email protected]'
3381
            )
3382
        )
3383
        self.testapp.put(
3384
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3385
            upload_files=[
3386
                ('files', image.name, image.getvalue())
3387
            ],
3388
            status=204,
3389
        )
3390
        res = self.testapp.put(
3391
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content2_id, image.name),
3392
            upload_files=[
3393
                ('files', image.name, image.getvalue())
3394
            ],
3395
            status=400,
3396
        )
3397
        assert isinstance(res.json, dict)
3398
        assert 'code' in res.json.keys()
3399
        assert res.json_body['code'] == error.CONTENT_FILENAME_ALREADY_USED_IN_FOLDER  # nopep8
3400
3401
3402 View Code Duplication
    def test_api__set_file_raw__err_400__closed_status_file(self) -> None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3403
        """
3404
        Set one file of a content
3405
        """
3406
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3407
        admin = dbsession.query(models.User) \
3408
            .filter(models.User.email == '[email protected]') \
3409
            .one()
3410
        workspace_api = WorkspaceApi(
3411
            current_user=admin,
3412
            session=dbsession,
3413
            config=self.app_config
3414
        )
3415
        content_api = ContentApi(
3416
            current_user=admin,
3417
            session=dbsession,
3418
            config=self.app_config
3419
        )
3420
        business_workspace = workspace_api.get_one(1)
3421
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3422
        test_file = content_api.create(
3423
            content_type_slug=content_type_list.File.slug,
3424
            workspace=business_workspace,
3425
            parent=tool_folder,
3426
            label='Test file',
3427
            do_save=False,
3428
            do_notify=False,
3429
        )
3430
        test_file.status = 'closed-validated'
3431
        content_api.save(test_file)
3432
        dbsession.flush()
3433
        transaction.commit()
3434
        content_id = int(test_file.content_id)
3435
        image = create_1000px_png_test_image()
3436
        self.testapp.authorization = (
3437
            'Basic',
3438
            (
3439
                '[email protected]',
3440
                '[email protected]'
3441
            )
3442
        )
3443
        res = self.testapp.put(
3444
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3445
            upload_files=[
3446
                ('files', image.name, image.getvalue())
3447
            ],
3448
            status=400,
3449
        )
3450
        assert isinstance(res.json, dict)
3451
        assert 'code' in res.json.keys()
3452
        assert res.json_body['code'] == error.CONTENT_IN_NOT_EDITABLE_STATE
3453
3454
    @pytest.mark.xfail(
3455
        raises=AssertionError,
3456
        reason='Broken feature dues to pyramid behaviour'
3457
    )
3458
    def test_api__set_file_raw__err_400_not_modified(self) -> None:
3459
        """
3460
        Set one file of a content
3461
        """
3462
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3463
        admin = dbsession.query(models.User) \
3464
            .filter(models.User.email == '[email protected]') \
3465
            .one()
3466
        workspace_api = WorkspaceApi(
3467
            current_user=admin,
3468
            session=dbsession,
3469
            config=self.app_config
3470
        )
3471
        content_api = ContentApi(
3472
            current_user=admin,
3473
            session=dbsession,
3474
            config=self.app_config
3475
        )
3476
        business_workspace = workspace_api.get_one(1)
3477
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3478
        test_file = content_api.create(
3479
            content_type_slug=content_type_list.File.slug,
3480
            workspace=business_workspace,
3481
            parent=tool_folder,
3482
            label='Test file',
3483
            do_save=True,
3484
            do_notify=False,
3485
        )
3486
        dbsession.flush()
3487
        transaction.commit()
3488
        content_id = int(test_file.content_id)
3489
        image = create_1000px_png_test_image()
3490
        self.testapp.authorization = (
3491
            'Basic',
3492
            (
3493
                '[email protected]',
3494
                '[email protected]'
3495
            )
3496
        )
3497
        self.testapp.put(
3498
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3499
            upload_files=[
3500
                ('files', image.name, image.getvalue())
3501
            ],
3502
            status=204,
3503
        )
3504
        res = self.testapp.get(
3505
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3506
            status=200
3507
        )
3508
        assert res.body == image.getvalue()
3509
        assert res.content_type == 'image/png'
3510
        assert res.content_length == len(image.getvalue())
3511
3512
        res = self.testapp.put(
3513
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3514
            upload_files=[
3515
                ('files', image.name, image.getvalue())
3516
            ],
3517
            status='*',
3518
        )
3519
        assert res.status == 400
3520
        assert isinstance(res.json, dict)
3521
        assert 'code' in res.json.keys()
3522
        assert res.json_body['code'] == error.CONTENT_FILENAME_ALREADY_USED_IN_FOLDER  # nopep8
3523
3524
    def test_api__get_allowed_size_dim__ok__nominal_case(self) -> None:
3525
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3526
        admin = dbsession.query(models.User) \
3527
            .filter(models.User.email == '[email protected]') \
3528
            .one()
3529
        workspace_api = WorkspaceApi(
3530
            current_user=admin,
3531
            session=dbsession,
3532
            config=self.app_config
3533
        )
3534
        content_api = ContentApi(
3535
            current_user=admin,
3536
            session=dbsession,
3537
            config=self.app_config
3538
        )
3539
        business_workspace = workspace_api.get_one(1)
3540
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3541
        test_file = content_api.create(
3542
            content_type_slug=content_type_list.File.slug,
3543
            workspace=business_workspace,
3544
            parent=tool_folder,
3545
            label='Test file',
3546
            do_save=False,
3547
            do_notify=False,
3548
        )
3549
        test_file.file_extension = '.txt'
3550
        test_file.depot_file = FileIntent(
3551
            b'Test file',
3552
            'Test_file.txt',
3553
            'text/plain',
3554
        )
3555
        dbsession.flush()
3556
        transaction.commit()
3557
        self.testapp.authorization = (
3558
            'Basic',
3559
            (
3560
                '[email protected]',
3561
                '[email protected]'
3562
            )
3563
        )
3564
        content_id = int(test_file.content_id)
3565
        res = self.testapp.get(
3566
            '/api/v2/workspaces/1/files/{}/preview/jpg/allowed_dims'.format(content_id),  # nopep8
3567
            status=200,
3568
        )
3569
        res = res.json_body
3570
        assert res['restricted'] is True
3571
        assert len(res['dimensions']) == 1
3572
        dim = res['dimensions'][0]
3573
        assert dim['width'] == 256
3574
        assert dim['height'] == 256
3575
3576 View Code Duplication
    def test_api__get_jpeg_preview__ok__200__nominal_case(self) -> None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3577
        """
3578
        Set one file of a content
3579
        """
3580
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3581
        admin = dbsession.query(models.User) \
3582
            .filter(models.User.email == '[email protected]') \
3583
            .one()
3584
        workspace_api = WorkspaceApi(
3585
            current_user=admin,
3586
            session=dbsession,
3587
            config=self.app_config
3588
        )
3589
        content_api = ContentApi(
3590
            current_user=admin,
3591
            session=dbsession,
3592
            config=self.app_config
3593
        )
3594
        business_workspace = workspace_api.get_one(1)
3595
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3596
        test_file = content_api.create(
3597
            content_type_slug=content_type_list.File.slug,
3598
            workspace=business_workspace,
3599
            parent=tool_folder,
3600
            label='Test file',
3601
            do_save=False,
3602
            do_notify=False,
3603
        )
3604
        test_file.file_extension = '.txt'
3605
        test_file.depot_file = FileIntent(
3606
            b'Test file',
3607
            'Test_file.txt',
3608
            'text/plain',
3609
        )
3610
        dbsession.flush()
3611
        transaction.commit()
3612
        content_id = int(test_file.content_id)
3613
        image = create_1000px_png_test_image()
3614
        self.testapp.authorization = (
3615
            'Basic',
3616
            (
3617
                '[email protected]',
3618
                '[email protected]'
3619
            )
3620
        )
3621
        self.testapp.put(
3622
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3623
            upload_files=[
3624
                ('files', image.name, image.getvalue())
3625
            ],
3626
            status=204,
3627
        )
3628
        res = self.testapp.get(
3629
            '/api/v2/workspaces/1/files/{}/preview/jpg/'.format(content_id),
3630
            status=200
3631
        )
3632
        assert res.body != image.getvalue()
3633
        assert res.content_type == 'image/jpeg'
3634
3635 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...
3636
        """
3637
        Set one file of a content
3638
        """
3639
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3640
        admin = dbsession.query(models.User) \
3641
            .filter(models.User.email == '[email protected]') \
3642
            .one()
3643
        workspace_api = WorkspaceApi(
3644
            current_user=admin,
3645
            session=dbsession,
3646
            config=self.app_config
3647
        )
3648
        content_api = ContentApi(
3649
            current_user=admin,
3650
            session=dbsession,
3651
            config=self.app_config
3652
        )
3653
        business_workspace = workspace_api.get_one(1)
3654
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3655
        test_file = content_api.create(
3656
            content_type_slug=content_type_list.File.slug,
3657
            workspace=business_workspace,
3658
            parent=tool_folder,
3659
            label='Test file',
3660
            do_save=False,
3661
            do_notify=False,
3662
        )
3663
        test_file.file_extension = '.txt'
3664
        test_file.depot_file = FileIntent(
3665
            b'Test file',
3666
            'Test_file.txt',
3667
            'text/plain',
3668
        )
3669
        dbsession.flush()
3670
        transaction.commit()
3671
        content_id = int(test_file.content_id)
3672
        image = create_1000px_png_test_image()
3673
        self.testapp.authorization = (
3674
            'Basic',
3675
            (
3676
                '[email protected]',
3677
                '[email protected]'
3678
            )
3679
        )
3680
        self.testapp.put(
3681
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3682
            upload_files=[
3683
                ('files', image.name, image.getvalue())
3684
            ],
3685
            status=204,
3686
        )
3687
        params = {
3688
            'force_download': 1,
3689
        }
3690
        res = self.testapp.get(
3691
            '/api/v2/workspaces/1/files/{}/preview/jpg/raw'.format(content_id),
3692
            status=200,
3693
            params=params
3694
        )
3695
        filename = 'test_image_page_1.jpg'
3696
        assert res.headers['Content-Disposition'] == 'attachment; filename="{}"; filename*=UTF-8\'\'{};'.format(filename, filename)  # nopep8
3697
        assert res.body != image.getvalue()
3698
        assert res.content_type == 'image/jpeg'
3699
3700 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...
3701
        """
3702
        Set one file of a content
3703
        """
3704
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3705
        admin = dbsession.query(models.User) \
3706
            .filter(models.User.email == '[email protected]') \
3707
            .one()
3708
        workspace_api = WorkspaceApi(
3709
            current_user=admin,
3710
            session=dbsession,
3711
            config=self.app_config
3712
        )
3713
        content_api = ContentApi(
3714
            current_user=admin,
3715
            session=dbsession,
3716
            config=self.app_config
3717
        )
3718
        business_workspace = workspace_api.get_one(1)
3719
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3720
        test_file = content_api.create(
3721
            content_type_slug=content_type_list.File.slug,
3722
            workspace=business_workspace,
3723
            parent=tool_folder,
3724
            label='Test file',
3725
            do_save=False,
3726
            do_notify=False,
3727
        )
3728
        with new_revision(
3729
            session=dbsession,
3730
            tm=transaction.manager,
3731
            content=test_file,
3732
        ):
3733
            content_api.update_file_data(
3734
                test_file,
3735
                'Test_file.bin',
3736
                new_mimetype='application/octet-stream',
3737
                new_content=bytes(100),
3738
            )
3739
        dbsession.flush()
3740
        transaction.commit()
3741
        content_id = int(test_file.content_id)
3742
        self.testapp.authorization = (
3743
            'Basic',
3744
            (
3745
                '[email protected]',
3746
                '[email protected]'
3747
            )
3748
        )
3749
        params = {
3750
            'force_download': 0,
3751
        }
3752
        res = self.testapp.get(
3753
            '/api/v2/workspaces/1/files/{}/preview/jpg/'.format(content_id),
3754
            status=400,
3755
            params=params
3756
        )
3757
        assert isinstance(res.json, dict)
3758
        assert 'code' in res.json.keys()
3759
        assert res.json_body['code'] == error.UNAIVALABLE_PREVIEW
3760
3761 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...
3762
        """
3763
        get 256x256 preview of a txt file
3764
        """
3765
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3766
        admin = dbsession.query(models.User) \
3767
            .filter(models.User.email == '[email protected]') \
3768
            .one()
3769
        workspace_api = WorkspaceApi(
3770
            current_user=admin,
3771
            session=dbsession,
3772
            config=self.app_config
3773
        )
3774
        content_api = ContentApi(
3775
            current_user=admin,
3776
            session=dbsession,
3777
            config=self.app_config
3778
        )
3779
        business_workspace = workspace_api.get_one(1)
3780
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3781
        test_file = content_api.create(
3782
            content_type_slug=content_type_list.File.slug,
3783
            workspace=business_workspace,
3784
            parent=tool_folder,
3785
            label='Test file',
3786
            do_save=True,
3787
            do_notify=False,
3788
        )
3789
        dbsession.flush()
3790
        transaction.commit()
3791
        content_id = int(test_file.content_id)
3792
        image = create_1000px_png_test_image()
3793
        self.testapp.authorization = (
3794
            'Basic',
3795
            (
3796
                '[email protected]',
3797
                '[email protected]'
3798
            )
3799
        )
3800
        self.testapp.put(
3801
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3802
            upload_files=[
3803
                ('files', image.name, image.getvalue())
3804
            ],
3805
            status=204,
3806
        )
3807
        res = self.testapp.get(
3808
            '/api/v2/workspaces/1/files/{}/preview/jpg/256x256/{}'.format(content_id, image.name),  # nopep8
3809
            status=200
3810
        )
3811
        assert res.body != image.getvalue()
3812
        assert res.content_type == 'image/jpeg'
3813
        new_image = Image.open(io.BytesIO(res.body))
3814
        assert 256, 256 == new_image.size
3815
3816
    def test_api__get_sized_jpeg_preview__err_400__UnavailablePreview(self) -> None:
3817
        """
3818
        Set one file of a content
3819
        """
3820
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3821
        admin = dbsession.query(models.User) \
3822
            .filter(models.User.email == '[email protected]') \
3823
            .one()
3824
        workspace_api = WorkspaceApi(
3825
            current_user=admin,
3826
            session=dbsession,
3827
            config=self.app_config
3828
        )
3829
        content_api = ContentApi(
3830
            current_user=admin,
3831
            session=dbsession,
3832
            config=self.app_config
3833
        )
3834
        business_workspace = workspace_api.get_one(1)
3835
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3836
        test_file = content_api.create(
3837
            content_type_slug=content_type_list.File.slug,
3838
            workspace=business_workspace,
3839
            parent=tool_folder,
3840
            label='Test file',
3841
            do_save=False,
3842
            do_notify=False,
3843
        )
3844
        with new_revision(
3845
            session=dbsession,
3846
            tm=transaction.manager,
3847
            content=test_file,
3848
        ):
3849
            content_api.update_file_data(
3850
                test_file,
3851
                'Test_file.bin',
3852
                new_mimetype='application/octet-stream',
3853
                new_content=bytes(100),
3854
            )
3855
        dbsession.flush()
3856
        transaction.commit()
3857
        content_id = int(test_file.content_id)
3858
        self.testapp.authorization = (
3859
            'Basic',
3860
            (
3861
                '[email protected]',
3862
                '[email protected]'
3863
            )
3864
        )
3865
        params = {
3866
            'force_download': 0,
3867
        }
3868
        res = self.testapp.get(
3869
            '/api/v2/workspaces/1/files/{}/preview/jpg/256x256/{}'.format(content_id, 'Test_file.bin'),  # nopep8
3870
            status=400,
3871
            params=params,
3872
        )
3873
        assert isinstance(res.json, dict)
3874
        assert 'code' in res.json.keys()
3875
        assert res.json_body['code'] == error.UNAIVALABLE_PREVIEW
3876
3877 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...
3878
        """
3879
        get 256x256 preview of a txt file
3880
        """
3881
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3882
        admin = dbsession.query(models.User) \
3883
            .filter(models.User.email == '[email protected]') \
3884
            .one()
3885
        workspace_api = WorkspaceApi(
3886
            current_user=admin,
3887
            session=dbsession,
3888
            config=self.app_config
3889
        )
3890
        content_api = ContentApi(
3891
            current_user=admin,
3892
            session=dbsession,
3893
            config=self.app_config
3894
        )
3895
        business_workspace = workspace_api.get_one(1)
3896
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3897
        test_file = content_api.create(
3898
            content_type_slug=content_type_list.File.slug,
3899
            workspace=business_workspace,
3900
            parent=tool_folder,
3901
            label='Test file',
3902
            do_save=True,
3903
            do_notify=False,
3904
        )
3905
        dbsession.flush()
3906
        transaction.commit()
3907
        content_id = int(test_file.content_id)
3908
        image = create_1000px_png_test_image()
3909
        self.testapp.authorization = (
3910
            'Basic',
3911
            (
3912
                '[email protected]',
3913
                '[email protected]'
3914
            )
3915
        )
3916
        self.testapp.put(
3917
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3918
            upload_files=[
3919
                ('files', image.name, image.getvalue())
3920
            ],
3921
            status=204,
3922
        )
3923
        params = {
3924
            'force_download': 1,
3925
        }
3926
        dl_filename = 'test_image_page_1_256x256.jpg'
3927
        res = self.testapp.get(
3928
            '/api/v2/workspaces/1/files/{}/preview/jpg/256x256/{}'.format(content_id, dl_filename),  # nopep8
3929
            status=200,
3930
            params=params,
3931
        )
3932
        assert res.body != image.getvalue()
3933
        assert res.headers['Content-Disposition'] == 'attachment; filename="{}"; filename*=UTF-8\'\'{};'.format(dl_filename, dl_filename)  # nopep8
3934
        assert res.content_type == 'image/jpeg'
3935
        new_image = Image.open(io.BytesIO(res.body))
3936
        assert 256, 256 == new_image.size
3937
3938 View Code Duplication
    def test_api__get_sized_jpeg_preview__ok__200__force_download_case_no_filename(self) -> None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
3939
        """
3940
        get 256x256 preview of a txt file
3941
        """
3942
        dbsession = get_tm_session(self.session_factory, transaction.manager)
3943
        admin = dbsession.query(models.User) \
3944
            .filter(models.User.email == '[email protected]') \
3945
            .one()
3946
        workspace_api = WorkspaceApi(
3947
            current_user=admin,
3948
            session=dbsession,
3949
            config=self.app_config
3950
        )
3951
        content_api = ContentApi(
3952
            current_user=admin,
3953
            session=dbsession,
3954
            config=self.app_config
3955
        )
3956
        business_workspace = workspace_api.get_one(1)
3957
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
3958
        test_file = content_api.create(
3959
            content_type_slug=content_type_list.File.slug,
3960
            workspace=business_workspace,
3961
            parent=tool_folder,
3962
            label='Test file',
3963
            do_save=True,
3964
            do_notify=False,
3965
        )
3966
        dbsession.flush()
3967
        transaction.commit()
3968
        content_id = int(test_file.content_id)
3969
        image = create_1000px_png_test_image()
3970
        self.testapp.authorization = (
3971
            'Basic',
3972
            (
3973
                '[email protected]',
3974
                '[email protected]'
3975
            )
3976
        )
3977
        self.testapp.put(
3978
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
3979
            upload_files=[
3980
                ('files', image.name, image.getvalue())
3981
            ],
3982
            status=204,
3983
        )
3984
        params = {
3985
            'force_download': 1,
3986
        }
3987
        dl_filename = 'test_image_page_1_256x256.jpg'
3988
        res = self.testapp.get(
3989
            '/api/v2/workspaces/1/files/{}/preview/jpg/256x256/'.format(content_id),  # nopep8
3990
            status=200,
3991
            params=params,
3992
        )
3993
        assert res.body != image.getvalue()
3994
        assert res.headers['Content-Disposition'] == 'attachment; filename="{}"; filename*=UTF-8\'\'{};'.format(dl_filename, dl_filename)  # nopep8
3995
        assert res.content_type == 'image/jpeg'
3996
        new_image = Image.open(io.BytesIO(res.body))
3997
        assert 256, 256 == new_image.size
3998
3999 View Code Duplication
    def test_api__get_sized_jpeg_preview__ok__200__force_download_case_filename_is_raw(self) -> None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4000
        """
4001
        get 256x256 preview of a txt file
4002
        """
4003
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4004
        admin = dbsession.query(models.User) \
4005
            .filter(models.User.email == '[email protected]') \
4006
            .one()
4007
        workspace_api = WorkspaceApi(
4008
            current_user=admin,
4009
            session=dbsession,
4010
            config=self.app_config
4011
        )
4012
        content_api = ContentApi(
4013
            current_user=admin,
4014
            session=dbsession,
4015
            config=self.app_config
4016
        )
4017
        business_workspace = workspace_api.get_one(1)
4018
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4019
        test_file = content_api.create(
4020
            content_type_slug=content_type_list.File.slug,
4021
            workspace=business_workspace,
4022
            parent=tool_folder,
4023
            label='Test file',
4024
            do_save=True,
4025
            do_notify=False,
4026
        )
4027
        dbsession.flush()
4028
        transaction.commit()
4029
        content_id = int(test_file.content_id)
4030
        image = create_1000px_png_test_image()
4031
        self.testapp.authorization = (
4032
            'Basic',
4033
            (
4034
                '[email protected]',
4035
                '[email protected]'
4036
            )
4037
        )
4038
        self.testapp.put(
4039
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
4040
            upload_files=[
4041
                ('files', image.name, image.getvalue())
4042
            ],
4043
            status=204,
4044
        )
4045
        params = {
4046
            'force_download': 1,
4047
        }
4048
        dl_filename = 'test_image_page_1_256x256.jpg'
4049
        res = self.testapp.get(
4050
            '/api/v2/workspaces/1/files/{}/preview/jpg/256x256/raw'.format(content_id),  # nopep8
4051
            status=200,
4052
            params=params,
4053
        )
4054
        assert res.body != image.getvalue()
4055
        assert res.headers['Content-Disposition'] == 'attachment; filename="{}"; filename*=UTF-8\'\'{};'.format(dl_filename, dl_filename)  # nopep8
4056
        assert res.content_type == 'image/jpeg'
4057
        new_image = Image.open(io.BytesIO(res.body))
4058
        assert 256, 256 == new_image.size
4059
4060 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...
4061
        """
4062
        get 256x256 preview of a txt file
4063
        """
4064
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4065
        admin = dbsession.query(models.User) \
4066
            .filter(models.User.email == '[email protected]') \
4067
            .one()
4068
        workspace_api = WorkspaceApi(
4069
            current_user=admin,
4070
            session=dbsession,
4071
            config=self.app_config
4072
        )
4073
        content_api = ContentApi(
4074
            current_user=admin,
4075
            session=dbsession,
4076
            config=self.app_config
4077
        )
4078
        business_workspace = workspace_api.get_one(1)
4079
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4080
        test_file = content_api.create(
4081
            content_type_slug=content_type_list.File.slug,
4082
            workspace=business_workspace,
4083
            parent=tool_folder,
4084
            label='Test file',
4085
            do_save=True,
4086
            do_notify=False,
4087
        )
4088
        dbsession.flush()
4089
        transaction.commit()
4090
        content_id = int(test_file.content_id)
4091
        image = create_1000px_png_test_image()
4092
        self.testapp.authorization = (
4093
            'Basic',
4094
            (
4095
                '[email protected]',
4096
                '[email protected]'
4097
            )
4098
        )
4099
        self.testapp.put(
4100
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
4101
            upload_files=[
4102
                ('files', image.name, image.getvalue())
4103
            ],
4104
            status=204,
4105
        )
4106
        filename = 'test_image_512x512.jpg'
4107
        res = self.testapp.get(
4108
            '/api/v2/workspaces/1/files/{}/preview/jpg/512x512/{}'.format(content_id, filename),  # nopep8
4109
            status=400
4110
        )
4111
        assert res.json_body
4112
        assert 'code' in res.json_body
4113
        assert res.json_body['code'] == error.PREVIEW_DIM_NOT_ALLOWED
4114
4115
    def test_api__get_sized_jpeg_revision_preview__ok__200__nominal_case(self) -> None:  # nopep8
4116
        """
4117
        get 256x256 revision preview of a txt file
4118
        """
4119
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4120
        admin = dbsession.query(models.User) \
4121
            .filter(models.User.email == '[email protected]') \
4122
            .one()
4123
        workspace_api = WorkspaceApi(
4124
            current_user=admin,
4125
            session=dbsession,
4126
            config=self.app_config
4127
        )
4128
        content_api = ContentApi(
4129
            current_user=admin,
4130
            session=dbsession,
4131
            config=self.app_config
4132
        )
4133
        business_workspace = workspace_api.get_one(1)
4134
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4135
        test_file = content_api.create(
4136
            content_type_slug=content_type_list.File.slug,
4137
            workspace=business_workspace,
4138
            parent=tool_folder,
4139
            label='Test file',
4140
            do_save=False,
4141
            do_notify=False,
4142
        )
4143
        test_file.file_extension = '.txt'
4144
        test_file.depot_file = FileIntent(
4145
            b'Test file',
4146
            'Test_file.txt',
4147
            'text/plain',
4148
        )
4149
        dbsession.flush()
4150
        transaction.commit()
4151
        content_id = int(test_file.content_id)
4152
        revision_id = int(test_file.revision_id)
4153
        image = create_1000px_png_test_image()
4154
        self.testapp.authorization = (
4155
            'Basic',
4156
            (
4157
                '[email protected]',
4158
                '[email protected]'
4159
            )
4160
        )
4161
        self.testapp.put(
4162
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
4163
            upload_files=[
4164
                ('files', image.name, image.getvalue())
4165
            ],
4166
            status=204,
4167
        )
4168
        filename = "test_file.txt"
4169
        res = self.testapp.get(
4170
            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw/{filename}'.format(  # nopep8
4171
                content_id=content_id,
4172
                revision_id=revision_id,
4173
                filename=filename
4174
            ),
4175
            status=200
4176
        )
4177
        assert res.content_type == 'text/plain'
4178
        filename = "test_image_256x256.jpg"
4179
        res = self.testapp.get(
4180
            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/jpg/256x256/{filename}'.format(  # nopep8
4181
                content_id=content_id,
4182
                revision_id=revision_id,
4183
                filename=filename,
4184
            ),
4185
            status=200
4186
        )
4187
        assert res.body != image.getvalue()
4188
        assert res.content_type == 'image/jpeg'
4189
        new_image = Image.open(io.BytesIO(res.body))
4190
        assert 256, 256 == new_image.size
4191
4192
    def test_api__get_sized_jpeg_revision_preview__ok__200__force_download_case(self) -> None:  # nopep8
4193
        """
4194
        get 256x256 revision preview of a txt file
4195
        """
4196
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4197
        admin = dbsession.query(models.User) \
4198
            .filter(models.User.email == '[email protected]') \
4199
            .one()
4200
        workspace_api = WorkspaceApi(
4201
            current_user=admin,
4202
            session=dbsession,
4203
            config=self.app_config
4204
        )
4205
        content_api = ContentApi(
4206
            current_user=admin,
4207
            session=dbsession,
4208
            config=self.app_config
4209
        )
4210
        business_workspace = workspace_api.get_one(1)
4211
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4212
        test_file = content_api.create(
4213
            content_type_slug=content_type_list.File.slug,
4214
            workspace=business_workspace,
4215
            parent=tool_folder,
4216
            label='Test file',
4217
            do_save=False,
4218
            do_notify=False,
4219
        )
4220
        test_file.file_extension = '.txt'
4221
        test_file.depot_file = FileIntent(
4222
            b'Test file',
4223
            'Test_file.txt',
4224
            'text/plain',
4225
        )
4226
        dbsession.flush()
4227
        transaction.commit()
4228
        content_id = int(test_file.content_id)
4229
        revision_id = int(test_file.revision_id)
4230
        image = create_1000px_png_test_image()
4231
        self.testapp.authorization = (
4232
            'Basic',
4233
            (
4234
                '[email protected]',
4235
                '[email protected]'
4236
            )
4237
        )
4238
        self.testapp.put(
4239
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
4240
            upload_files=[
4241
                ('files', image.name, image.getvalue())
4242
            ],
4243
            status=204,
4244
        )
4245
        res = self.testapp.get(
4246
            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw/{filename}'.format(  # nopep8
4247
                content_id=content_id,
4248
                revision_id=revision_id,
4249
                filename=image.name
4250
            ),
4251
            status=200
4252
        )
4253
        assert res.content_type == 'text/plain'
4254
        params = {
4255
            'force_download': 1,
4256
        }
4257
        res = self.testapp.get(
4258
            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/jpg/256x256/'.format(  # nopep8
4259
                content_id=content_id,
4260
                revision_id=revision_id,
4261
            ),
4262
            status=200,
4263
            params=params,
4264
        )
4265
        filename = 'Test file_r{}_page_1_256x256.jpg'.format(revision_id)
4266
        urlencoded_filename = quote(filename)
4267
        assert res.headers['Content-Disposition'] == 'attachment; filename="{}"; filename*=UTF-8\'\'{};'.format(filename, urlencoded_filename)  # nopep8
4268
        assert res.body != image.getvalue()
4269
        assert res.content_type == 'image/jpeg'
4270
        new_image = Image.open(io.BytesIO(res.body))
4271
        assert 256, 256 == new_image.size
4272
4273 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...
4274
        """
4275
        get full pdf preview of a txt file
4276
        """
4277
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4278
        admin = dbsession.query(models.User) \
4279
            .filter(models.User.email == '[email protected]') \
4280
            .one()
4281
        workspace_api = WorkspaceApi(
4282
            current_user=admin,
4283
            session=dbsession,
4284
            config=self.app_config
4285
        )
4286
        content_api = ContentApi(
4287
            current_user=admin,
4288
            session=dbsession,
4289
            config=self.app_config
4290
        )
4291
        business_workspace = workspace_api.get_one(1)
4292
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4293
        test_file = content_api.create(
4294
            content_type_slug=content_type_list.File.slug,
4295
            workspace=business_workspace,
4296
            parent=tool_folder,
4297
            label='Test file',
4298
            do_save=True,
4299
            do_notify=False,
4300
        )
4301
        with new_revision(
4302
                session=dbsession,
4303
                tm=transaction.manager,
4304
                content=test_file,
4305
        ):
4306
            test_file.file_extension = '.txt'
4307
            test_file.depot_file = FileIntent(
4308
                b'Test file',
4309
                'Test_file.txt',
4310
                'text/plain',
4311
            )
4312
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
4313
        dbsession.flush()
4314
        transaction.commit()
4315
        content_id = int(test_file.content_id)
4316
        self.testapp.authorization = (
4317
            'Basic',
4318
            (
4319
                '[email protected]',
4320
                '[email protected]'
4321
            )
4322
        )
4323
        self.testapp.put(
4324
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, test_file.file_name),
4325
            upload_files=[
4326
                ('files', test_file.file_name, test_file.depot_file.file.read())
4327
            ],
4328
            status=204,
4329
        )
4330
        filename = 'test_image.pdf'
4331
        res = self.testapp.get(
4332
            '/api/v2/workspaces/1/files/{}/preview/pdf/full/{}'.format(content_id, filename),  # nopep8
4333
            status=200
4334
        )
4335
        assert res.content_type == 'application/pdf'
4336
4337
    def test_api__get_full_pdf_preview__ok__200__force_download_case(self) -> None:
4338
        """
4339
        get full pdf preview of a txt file
4340
        """
4341
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4342
        admin = dbsession.query(models.User) \
4343
            .filter(models.User.email == '[email protected]') \
4344
            .one()
4345
        workspace_api = WorkspaceApi(
4346
            current_user=admin,
4347
            session=dbsession,
4348
            config=self.app_config
4349
        )
4350
        content_api = ContentApi(
4351
            current_user=admin,
4352
            session=dbsession,
4353
            config=self.app_config
4354
        )
4355
        business_workspace = workspace_api.get_one(1)
4356
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4357
        test_file = content_api.create(
4358
            content_type_slug=content_type_list.File.slug,
4359
            workspace=business_workspace,
4360
            parent=tool_folder,
4361
            label='Test file',
4362
            do_save=True,
4363
            do_notify=False,
4364
        )
4365
        with new_revision(
4366
                session=dbsession,
4367
                tm=transaction.manager,
4368
                content=test_file,
4369
        ):
4370
            test_file.file_extension = '.txt'
4371
            test_file.depot_file = FileIntent(
4372
                b'Test file',
4373
                'Test_file.txt',
4374
                'text/plain',
4375
            )
4376
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
4377
        dbsession.flush()
4378
        transaction.commit()
4379
        content_id = int(test_file.content_id)
4380
        self.testapp.authorization = (
4381
            'Basic',
4382
            (
4383
                '[email protected]',
4384
                '[email protected]'
4385
            )
4386
        )
4387
        filename = 'Test_file.txt'
4388
        self.testapp.put(
4389
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, filename),
4390
            upload_files=[
4391
                ('files', test_file.file_name, test_file.depot_file.file.read())
4392
            ],
4393
            status=204,
4394
        )
4395
        params = {
4396
            'force_download': 1,
4397
        }
4398
        res = self.testapp.get(
4399
            '/api/v2/workspaces/1/files/{}/preview/pdf/full/{}'.format(content_id, filename),  # nopep8
4400
            status=200,
4401
            params=params
4402
        )
4403
        assert res.headers['Content-Disposition'] == 'attachment; filename="{}"; filename*=UTF-8\'\'{};'.format(filename, filename)  # nopep8
4404
        assert res.content_type == 'application/pdf'
4405
4406
        res = self.testapp.get(
4407
            '/api/v2/workspaces/1/files/{}/preview/pdf/full/{}'.format(content_id, 'Test_file.pdf'),  # nopep8
4408
            status=200,
4409
            params=params
4410
        )
4411
        filename = "Test_file.pdf"
4412
        assert res.headers['Content-Disposition'] == 'attachment; filename="{}"; filename*=UTF-8\'\'{};'.format(filename, filename)  # nopep8
4413
        assert res.content_type == 'application/pdf'
4414
4415 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...
4416
        """
4417
       get full pdf preview of a png image -> error UnavailablePreviewType
4418
        """
4419
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4420
        admin = dbsession.query(models.User) \
4421
            .filter(models.User.email == '[email protected]') \
4422
            .one()
4423
        workspace_api = WorkspaceApi(
4424
            current_user=admin,
4425
            session=dbsession,
4426
            config=self.app_config
4427
        )
4428
        content_api = ContentApi(
4429
            current_user=admin,
4430
            session=dbsession,
4431
            config=self.app_config
4432
        )
4433
        business_workspace = workspace_api.get_one(1)
4434
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4435
        test_file = content_api.create(
4436
            content_type_slug=content_type_list.File.slug,
4437
            workspace=business_workspace,
4438
            parent=tool_folder,
4439
            label='Test file',
4440
            do_save=True,
4441
            do_notify=False,
4442
        )
4443
        dbsession.flush()
4444
        transaction.commit()
4445
        content_id = int(test_file.content_id)
4446
        image = create_1000px_png_test_image()
4447
        self.testapp.authorization = (
4448
            'Basic',
4449
            (
4450
                '[email protected]',
4451
                '[email protected]'
4452
            )
4453
        )
4454
        self.testapp.put(
4455
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
4456
            upload_files=[
4457
                ('files', image.name, image.getvalue())
4458
            ],
4459
            status=204,
4460
        )
4461
        res = self.testapp.get(
4462
            '/api/v2/workspaces/1/files/{}/preview/pdf/full/{}'.format(content_id, image.name),  # nopep8
4463
            status=400
4464
        )
4465
        assert res.json_body
4466
        assert 'code' in res.json_body
4467
        assert res.json_body['code'] == error.UNAVAILABLE_PREVIEW_TYPE
4468
4469 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...
4470
        """
4471
       get full pdf preview of a png image -> error UnavailablePreviewType
4472
        """
4473
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4474
        admin = dbsession.query(models.User) \
4475
            .filter(models.User.email == '[email protected]') \
4476
            .one()
4477
        workspace_api = WorkspaceApi(
4478
            current_user=admin,
4479
            session=dbsession,
4480
            config=self.app_config
4481
        )
4482
        content_api = ContentApi(
4483
            current_user=admin,
4484
            session=dbsession,
4485
            config=self.app_config
4486
        )
4487
        business_workspace = workspace_api.get_one(1)
4488
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4489
        test_file = content_api.create(
4490
            content_type_slug=content_type_list.File.slug,
4491
            workspace=business_workspace,
4492
            parent=tool_folder,
4493
            label='Test file',
4494
            do_save=False,
4495
            do_notify=False,
4496
        )
4497
        with new_revision(
4498
            session=dbsession,
4499
            tm=transaction.manager,
4500
            content=test_file,
4501
        ):
4502
            content_api.update_file_data(
4503
                test_file,
4504
                'Test_file.bin',
4505
                new_mimetype='application/octet-stream',
4506
                new_content=bytes(100),
4507
            )
4508
        dbsession.flush()
4509
        content_id = test_file.content_id
4510
        transaction.commit()
4511
        self.testapp.authorization = (
4512
            'Basic',
4513
            (
4514
                '[email protected]',
4515
                '[email protected]'
4516
            )
4517
        )
4518
        filename = 'Test_file.bin'
4519
        res = self.testapp.get(
4520
            '/api/v2/workspaces/1/files/{}/preview/pdf/full/{}'.format(content_id, filename),  # nopep8
4521
            status=400
4522
        )
4523
        assert isinstance(res.json, dict)
4524
        assert 'code' in res.json.keys()
4525
        assert res.json_body['code'] == error.UNAIVALABLE_PREVIEW
4526
4527 View Code Duplication
    def test_api__get_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...
4528
        """
4529
        get full pdf preview of a txt file
4530
        """
4531
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4532
        admin = dbsession.query(models.User) \
4533
            .filter(models.User.email == '[email protected]') \
4534
            .one()
4535
        workspace_api = WorkspaceApi(
4536
            current_user=admin,
4537
            session=dbsession,
4538
            config=self.app_config
4539
        )
4540
        content_api = ContentApi(
4541
            current_user=admin,
4542
            session=dbsession,
4543
            config=self.app_config
4544
        )
4545
        business_workspace = workspace_api.get_one(1)
4546
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4547
        test_file = content_api.create(
4548
            content_type_slug=content_type_list.File.slug,
4549
            workspace=business_workspace,
4550
            parent=tool_folder,
4551
            label='Test file',
4552
            do_save=True,
4553
            do_notify=False,
4554
        )
4555
        with new_revision(
4556
                session=dbsession,
4557
                tm=transaction.manager,
4558
                content=test_file,
4559
        ):
4560
            test_file.file_extension = '.txt'
4561
            test_file.depot_file = FileIntent(
4562
                b'Test file',
4563
                'Test_file.txt',
4564
                'text/plain',
4565
            )
4566
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
4567
        dbsession.flush()
4568
        transaction.commit()
4569
        content_id = int(test_file.content_id)
4570
        self.testapp.authorization = (
4571
            'Basic',
4572
            (
4573
                '[email protected]',
4574
                '[email protected]'
4575
            )
4576
        )
4577
        self.testapp.put(
4578
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, test_file.file_name),
4579
            upload_files=[
4580
                ('files', test_file.file_name, test_file.depot_file.file.read())
4581
            ],
4582
            status=204,
4583
        )
4584
        params = {'page': 1}
4585
        filename = 'test_file.pdf'
4586
        res = self.testapp.get(
4587
            '/api/v2/workspaces/1/files/{}/preview/pdf/{}'.format(content_id, filename),
4588
            status=200,
4589
            params=params,
4590
        )
4591
        assert res.content_type == 'application/pdf'
4592
4593 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...
4594
        """
4595
        get full pdf preview of a txt file
4596
        """
4597
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4598
        admin = dbsession.query(models.User) \
4599
            .filter(models.User.email == '[email protected]') \
4600
            .one()
4601
        workspace_api = WorkspaceApi(
4602
            current_user=admin,
4603
            session=dbsession,
4604
            config=self.app_config
4605
        )
4606
        content_api = ContentApi(
4607
            current_user=admin,
4608
            session=dbsession,
4609
            config=self.app_config
4610
        )
4611
        business_workspace = workspace_api.get_one(1)
4612
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4613
        test_file = content_api.create(
4614
            content_type_slug=content_type_list.File.slug,
4615
            workspace=business_workspace,
4616
            parent=tool_folder,
4617
            label='Test file',
4618
            do_save=False,
4619
            do_notify=False,
4620
        )
4621
        with new_revision(
4622
            session=dbsession,
4623
            tm=transaction.manager,
4624
            content=test_file,
4625
        ):
4626
            content_api.update_file_data(
4627
                test_file,
4628
                'Test_file.bin',
4629
                new_mimetype='application/octet-stream',
4630
                new_content=bytes(100),
4631
            )
4632
        dbsession.flush()
4633
        transaction.commit()
4634
        content_id = int(test_file.content_id)
4635
        self.testapp.authorization = (
4636
            'Basic',
4637
            (
4638
                '[email protected]',
4639
                '[email protected]'
4640
            )
4641
        )
4642
        params = {'page': 1}
4643
        res = self.testapp.get(
4644
            '/api/v2/workspaces/1/files/{}/preview/pdf/'.format(content_id),
4645
            status=400,
4646
            params=params,
4647
        )
4648
        assert isinstance(res.json, dict)
4649
        assert 'code' in res.json.keys()
4650
        assert res.json_body['code'] == error.UNAIVALABLE_PREVIEW
4651
4652
    def test_api__get_pdf_preview__ok__200__force_download_case(self) -> None:
4653
        """
4654
        get full pdf preview of a txt file
4655
        """
4656
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4657
        admin = dbsession.query(models.User) \
4658
            .filter(models.User.email == '[email protected]') \
4659
            .one()
4660
        workspace_api = WorkspaceApi(
4661
            current_user=admin,
4662
            session=dbsession,
4663
            config=self.app_config
4664
        )
4665
        content_api = ContentApi(
4666
            current_user=admin,
4667
            session=dbsession,
4668
            config=self.app_config
4669
        )
4670
        business_workspace = workspace_api.get_one(1)
4671
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4672
        test_file = content_api.create(
4673
            content_type_slug=content_type_list.File.slug,
4674
            workspace=business_workspace,
4675
            parent=tool_folder,
4676
            label='Test file',
4677
            do_save=True,
4678
            do_notify=False,
4679
        )
4680
        with new_revision(
4681
                session=dbsession,
4682
                tm=transaction.manager,
4683
                content=test_file,
4684
        ):
4685
            test_file.file_extension = '.txt'
4686
            test_file.depot_file = FileIntent(
4687
                b'Test file',
4688
                'Test_file.txt',
4689
                'text/plain',
4690
            )
4691
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
4692
        dbsession.flush()
4693
        transaction.commit()
4694
        content_id = int(test_file.content_id)
4695
        self.testapp.authorization = (
4696
            'Basic',
4697
            (
4698
                '[email protected]',
4699
                '[email protected]'
4700
            )
4701
        )
4702
        filename = 'test_file.txt'
4703
        self.testapp.put(
4704
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, filename),
4705
            upload_files=[
4706
                ('files', test_file.file_name, test_file.depot_file.file.read())
4707
            ],
4708
            status=204,
4709
        )
4710
        filename = 'Test_file_page_1.pdf'
4711
        params = {'page': 1, 'force_download': 1}
4712
        res = self.testapp.get(
4713
            '/api/v2/workspaces/1/files/{}/preview/pdf/{}'.format(content_id, filename),
4714
            status=200,
4715
            params=params,
4716
        )
4717
        assert res.content_type == 'application/pdf'
4718
        assert res.headers['Content-Disposition'] == 'attachment; filename="{}"; filename*=UTF-8\'\'{};'.format(filename, filename)  # nopep8
4719
4720
    def test_api__get_pdf_preview__ok__err__400_page_of_preview_not_found(self) -> None:  # nopep8
4721
        """
4722
        get full pdf preview of a txt file
4723
        """
4724
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4725
        admin = dbsession.query(models.User) \
4726
            .filter(models.User.email == '[email protected]') \
4727
            .one()
4728
        workspace_api = WorkspaceApi(
4729
            current_user=admin,
4730
            session=dbsession,
4731
            config=self.app_config
4732
        )
4733
        content_api = ContentApi(
4734
            current_user=admin,
4735
            session=dbsession,
4736
            config=self.app_config
4737
        )
4738
        business_workspace = workspace_api.get_one(1)
4739
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4740
        test_file = content_api.create(
4741
            content_type_slug=content_type_list.File.slug,
4742
            workspace=business_workspace,
4743
            parent=tool_folder,
4744
            label='Test file',
4745
            do_save=True,
4746
            do_notify=False,
4747
        )
4748
        with new_revision(
4749
                session=dbsession,
4750
                tm=transaction.manager,
4751
                content=test_file,
4752
        ):
4753
            test_file.file_extension = '.txt'
4754
            test_file.depot_file = FileIntent(
4755
                b'Test file',
4756
                'Test_file.txt',
4757
                'text/plain',
4758
            )
4759
            content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
4760
        dbsession.flush()
4761
        transaction.commit()
4762
        content_id = int(test_file.content_id)
4763
        self.testapp.authorization = (
4764
            'Basic',
4765
            (
4766
                '[email protected]',
4767
                '[email protected]'
4768
            )
4769
        )
4770
        self.testapp.put(
4771
            '/api/v2/workspaces/1/files/{}/raw/'.format(content_id),
4772
            upload_files=[
4773
                ('files', test_file.file_name, test_file.depot_file.file.read())
4774
            ],
4775
            status=204,
4776
        )
4777
        params = {'page': 2}
4778
        res = self.testapp.get(
4779
            '/api/v2/workspaces/1/files/{}/preview/pdf/'.format(content_id),
4780
            status=400,
4781
            params=params,
4782
        )
4783
        assert res.json_body
4784
        assert 'code' in res.json_body
4785
        assert res.json_body['code'] == error.PAGE_OF_PREVIEW_NOT_FOUND
4786
4787
    def test_api__get_pdf_revision_preview__ok__200__nominal_case(self) -> None:
4788
        """
4789
        get pdf revision preview of content
4790
        """
4791
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4792
        admin = dbsession.query(models.User) \
4793
            .filter(models.User.email == '[email protected]') \
4794
            .one()
4795
        workspace_api = WorkspaceApi(
4796
            current_user=admin,
4797
            session=dbsession,
4798
            config=self.app_config
4799
        )
4800
        content_api = ContentApi(
4801
            current_user=admin,
4802
            session=dbsession,
4803
            config=self.app_config
4804
        )
4805
        business_workspace = workspace_api.get_one(1)
4806
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4807
        test_file = content_api.create(
4808
            content_type_slug=content_type_list.File.slug,
4809
            workspace=business_workspace,
4810
            parent=tool_folder,
4811
            label='Test file',
4812
            do_save=False,
4813
            do_notify=False,
4814
        )
4815
        test_file.file_extension = '.txt'
4816
        test_file.depot_file = FileIntent(
4817
            b'Test file',
4818
            'Test_file.txt',
4819
            'text/plain',
4820
        )
4821
        dbsession.flush()
4822
        transaction.commit()
4823
        content_id = int(test_file.content_id)
4824
        revision_id = int(test_file.revision_id)
4825
        image = create_1000px_png_test_image()
4826
        self.testapp.authorization = (
4827
            'Basic',
4828
            (
4829
                '[email protected]',
4830
                '[email protected]'
4831
            )
4832
        )
4833
        self.testapp.put(
4834
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
4835
            upload_files=[
4836
                ('files', image.name, image.getvalue())
4837
            ],
4838
            status=204,
4839
        )
4840
        filename = image.name
4841
        res = self.testapp.get(
4842
            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw/{filename}'.format(  # nopep8
4843
                content_id=content_id,
4844
                revision_id=revision_id,
4845
                filename=filename,
4846
            ),
4847
            status=200
4848
        )
4849
        assert res.content_type == 'text/plain'
4850
        params = {'page': 1}
4851
        filename = 'test_image__page_1.pdf'
4852
        res = self.testapp.get(
4853
            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/pdf/{filename}'.format(  # nopep8
4854
                content_id=content_id,
4855
                revision_id=revision_id,
4856
                params=params,
4857
                filename=filename,
4858
            ),
4859
            status=200
4860
        )
4861
        assert res.content_type == 'application/pdf'
4862
4863
    def test_api__get_full_pdf_revision_preview__ok__200__nominal_case(self) -> None:
4864
        """
4865
        get pdf revision preview of content
4866
        """
4867
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4868
        admin = dbsession.query(models.User) \
4869
            .filter(models.User.email == '[email protected]') \
4870
            .one()
4871
        workspace_api = WorkspaceApi(
4872
            current_user=admin,
4873
            session=dbsession,
4874
            config=self.app_config
4875
        )
4876
        content_api = ContentApi(
4877
            current_user=admin,
4878
            session=dbsession,
4879
            config=self.app_config
4880
        )
4881
        business_workspace = workspace_api.get_one(1)
4882
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4883
        test_file = content_api.create(
4884
            content_type_slug=content_type_list.File.slug,
4885
            workspace=business_workspace,
4886
            parent=tool_folder,
4887
            label='Test file',
4888
            do_save=False,
4889
            do_notify=False,
4890
        )
4891
        test_file.file_extension = '.txt'
4892
        test_file.depot_file = FileIntent(
4893
            b'Test file',
4894
            'Test_file.txt',
4895
            'text/plain',
4896
        )
4897
        dbsession.flush()
4898
        transaction.commit()
4899
        content_id = int(test_file.content_id)
4900
        revision_id = int(test_file.revision_id)
4901
        image = create_1000px_png_test_image()
4902
        self.testapp.authorization = (
4903
            'Basic',
4904
            (
4905
                '[email protected]',
4906
                '[email protected]'
4907
            )
4908
        )
4909
        self.testapp.put(
4910
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
4911
            upload_files=[
4912
                ('files', image.name, image.getvalue())
4913
            ],
4914
            status=204,
4915
        )
4916
        res = self.testapp.get(
4917
            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw/'.format(  # nopep8
4918
                content_id=content_id,
4919
                revision_id=revision_id,
4920
            ),
4921
            status=200
4922
        )
4923
        assert res.content_type == 'text/plain'
4924
        res = self.testapp.get(
4925
            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/pdf/full/'.format(  # nopep8
4926
                content_id=content_id,
4927
                revision_id=revision_id,
4928
            ),
4929
            status=200
4930
        )
4931
        assert res.content_type == 'application/pdf'
4932
4933 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...
4934
        """
4935
        get pdf revision preview of content
4936
        """
4937
        dbsession = get_tm_session(self.session_factory, transaction.manager)
4938
        admin = dbsession.query(models.User) \
4939
            .filter(models.User.email == '[email protected]') \
4940
            .one()
4941
        workspace_api = WorkspaceApi(
4942
            current_user=admin,
4943
            session=dbsession,
4944
            config=self.app_config
4945
        )
4946
        content_api = ContentApi(
4947
            current_user=admin,
4948
            session=dbsession,
4949
            config=self.app_config
4950
        )
4951
        business_workspace = workspace_api.get_one(1)
4952
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
4953
        test_file = content_api.create(
4954
            content_type_slug=content_type_list.File.slug,
4955
            workspace=business_workspace,
4956
            parent=tool_folder,
4957
            label='Test file',
4958
            do_save=False,
4959
            do_notify=False,
4960
        )
4961
        test_file.file_extension = '.txt'
4962
        test_file.depot_file = FileIntent(
4963
            b'Test file',
4964
            'Test_file.txt',
4965
            'text/plain',
4966
        )
4967
        dbsession.flush()
4968
        transaction.commit()
4969
        content_id = int(test_file.content_id)
4970
        revision_id = int(test_file.revision_id)
4971
        image = create_1000px_png_test_image()
4972
        self.testapp.authorization = (
4973
            'Basic',
4974
            (
4975
                '[email protected]',
4976
                '[email protected]'
4977
            )
4978
        )
4979
        self.testapp.put(
4980
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
4981
            upload_files=[
4982
                ('files', image.name, image.getvalue())
4983
            ],
4984
            status=204,
4985
        )
4986
        res = self.testapp.get(
4987
            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw/{filename}'.format(  # nopep8
4988
                content_id=content_id,
4989
                revision_id=revision_id,
4990
                filename=image.name
4991
            ),
4992
            status=200
4993
        )
4994
        assert res.content_type == 'text/plain'
4995
        params = {'force_download': 1}
4996
        filename = 'Test_file.pdf'
4997
        res = self.testapp.get(
4998
            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/pdf/full/{filename}'.format(  # nopep8
4999
                content_id=content_id,
5000
                revision_id=revision_id,
5001
                filename='Test_file.pdf'
5002
            ),
5003
            status=200,
5004
            params=params,
5005
        )
5006
5007
        assert res.headers['Content-Disposition'] == 'attachment; filename="{}"; filename*=UTF-8\'\'{};'.format(filename, filename)  # nopep8
5008
        assert res.content_type == 'application/pdf'
5009
5010 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...
5011
        """
5012
        get pdf revision preview of content
5013
        """
5014
        dbsession = get_tm_session(self.session_factory, transaction.manager)
5015
        admin = dbsession.query(models.User) \
5016
            .filter(models.User.email == '[email protected]') \
5017
            .one()
5018
        workspace_api = WorkspaceApi(
5019
            current_user=admin,
5020
            session=dbsession,
5021
            config=self.app_config
5022
        )
5023
        content_api = ContentApi(
5024
            current_user=admin,
5025
            session=dbsession,
5026
            config=self.app_config
5027
        )
5028
        business_workspace = workspace_api.get_one(1)
5029
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
5030
        test_file = content_api.create(
5031
            content_type_slug=content_type_list.File.slug,
5032
            workspace=business_workspace,
5033
            parent=tool_folder,
5034
            label='Test file',
5035
            do_save=False,
5036
            do_notify=False,
5037
        )
5038
        test_file.file_extension = '.txt'
5039
        test_file.depot_file = FileIntent(
5040
            b'Test file',
5041
            'Test_file.txt',
5042
            'text/plain',
5043
        )
5044
        dbsession.flush()
5045
        transaction.commit()
5046
        content_id = int(test_file.content_id)
5047
        revision_id = int(test_file.revision_id)
5048
        image = create_1000px_png_test_image()
5049
        self.testapp.authorization = (
5050
            'Basic',
5051
            (
5052
                '[email protected]',
5053
                '[email protected]'
5054
            )
5055
        )
5056
        self.testapp.put(
5057
            '/api/v2/workspaces/1/files/{}/raw/{}'.format(content_id, image.name),
5058
            upload_files=[
5059
                ('files', image.name, image.getvalue())
5060
            ],
5061
            status=204,
5062
        )
5063
        res = self.testapp.get(
5064
            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw/{filename}'.format(  # nopep8
5065
                content_id=content_id,
5066
                revision_id=revision_id,
5067
                filename=image.name
5068
            ),
5069
            status=200
5070
        )
5071
        assert res.content_type == 'text/plain'
5072
        params = {'page': 1, 'force_download': 1}
5073
        filename = 'test_image_page_1.pdf'
5074
        res = self.testapp.get(
5075
            '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/pdf/{filename}'.format(  # nopep8
5076
                content_id=content_id,
5077
                revision_id=revision_id,
5078
                filename=filename
5079
            ),
5080
            status=200,
5081
            params=params,
5082
        )
5083
        assert res.headers['Content-Disposition'] == 'attachment; filename="{}"; filename*=UTF-8\'\'{};'.format(filename, filename)  # nopep8
5084
        assert res.content_type == 'application/pdf'
5085
5086
5087
class TestThreads(FunctionalTest):
5088
    """
5089
    Tests for /api/v2/workspaces/{workspace_id}/threads/{content_id}
5090
    endpoint
5091
    """
5092
5093
    fixtures = [BaseFixture, ContentFixtures]
5094
5095
    def test_api__get_thread__err_400__wrong_content_type(self) -> None:
5096
        """
5097
        Get one html document of a content
5098
        """
5099
        self.testapp.authorization = (
5100
            'Basic',
5101
            (
5102
                '[email protected]',
5103
                '[email protected]'
5104
            )
5105
        )
5106
        res = self.testapp.get(
5107
            '/api/v2/workspaces/2/threads/6',
5108
            status=400
5109
        )
5110
        assert res.json_body
5111
        assert 'code' in res.json_body
5112
        assert res.json_body['code'] == error.CONTENT_TYPE_NOT_ALLOWED
5113
5114 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...
5115
        """
5116
        Get one html document of a content
5117
        """
5118
        self.testapp.authorization = (
5119
            'Basic',
5120
            (
5121
                '[email protected]',
5122
                '[email protected]'
5123
            )
5124
        )
5125
        res = self.testapp.get(
5126
            '/api/v2/workspaces/2/threads/7',
5127
            status=200
5128
        )   # nopep8
5129
        content = res.json_body
5130
        assert content['content_type'] == 'thread'
5131
        assert content['content_id'] == 7
5132
        assert content['is_archived'] is False
5133
        assert content['is_deleted'] is False
5134
        assert content['is_editable'] is True
5135
        assert content['label'] == 'Best Cakes?'
5136
        assert content['parent_id'] == 3
5137
        assert content['show_in_ui'] is True
5138
        assert content['slug'] == 'best-cakes'
5139
        assert content['status'] == 'open'
5140
        assert content['workspace_id'] == 2
5141
        assert content['current_revision_id'] == 26
5142
        # TODO - G.M - 2018-06-173 - check date format
5143
        assert content['created']
5144
        assert content['author']
5145
        assert content['author']['user_id'] == 1
5146
        assert content['author']['avatar_url'] is None
5147
        assert content['author']['public_name'] == 'Global manager'
5148
        # TODO - G.M - 2018-06-173 - check date format
5149
        assert content['modified']
5150
        assert content['last_modifier'] != content['author']
5151
        assert content['last_modifier']['user_id'] == 3
5152
        assert content['last_modifier']['public_name'] == 'Bob i.'
5153
        assert content['last_modifier']['avatar_url'] is None
5154
        assert content['raw_content'] == 'What is the best cake?'  # nopep8
5155
        assert content['file_extension'] == '.thread.html'
5156
        assert content['filename'] == 'Best Cakes?.thread.html'
5157
5158
    def test_api__get_thread__err_400__content_does_not_exist(self) -> None:
5159
        """
5160
        Get one thread (content 170 does not exist)
5161
        """
5162
        self.testapp.authorization = (
5163
            'Basic',
5164
            (
5165
                '[email protected]',
5166
                '[email protected]'
5167
            )
5168
        )
5169
        res = self.testapp.get(
5170
            '/api/v2/workspaces/2/threads/170',
5171
            status=400
5172
        )
5173
        assert res.json_body
5174
        assert 'code' in res.json_body
5175
        assert res.json_body['code'] == error.CONTENT_NOT_FOUND
5176
5177
    def test_api__get_thread__err_400__content_not_in_workspace(self) -> None:
5178
        """
5179
        Get one thread(content 7 is in workspace 2)
5180
        """
5181
        self.testapp.authorization = (
5182
            'Basic',
5183
            (
5184
                '[email protected]',
5185
                '[email protected]'
5186
            )
5187
        )
5188
        res = self.testapp.get(
5189
            '/api/v2/workspaces/1/threads/7',
5190
            status=400
5191
        )
5192
        assert res.json_body
5193
        assert 'code' in res.json_body
5194
        assert res.json_body['code'] == error.CONTENT_NOT_FOUND
5195
5196
    def test_api__get_thread__err_400__workspace_does_not_exist(self) -> None:  # nopep8
5197
        """
5198
        Get one thread (Workspace 40 does not exist)
5199
        """
5200
        self.testapp.authorization = (
5201
            'Basic',
5202
            (
5203
                '[email protected]',
5204
                '[email protected]'
5205
            )
5206
        )
5207
        res = self.testapp.get(
5208
            '/api/v2/workspaces/40/threads/7',
5209
            status=400
5210
        )
5211
        assert res.json_body
5212
        assert 'code' in res.json_body
5213
        assert res.json_body['code'] == error.WORKSPACE_NOT_FOUND
5214
5215
    def test_api__get_thread__err_400__workspace_id_is_not_int(self) -> None:  # nopep8
5216
        """
5217
        Get one thread, workspace id is not int
5218
        """
5219
        self.testapp.authorization = (
5220
            'Basic',
5221
            (
5222
                '[email protected]',
5223
                '[email protected]'
5224
            )
5225
        )
5226
        res = self.testapp.get(
5227
            '/api/v2/workspaces/coucou/threads/7',
5228
            status=400
5229
        )
5230
        assert res.json_body
5231
        assert 'code' in res.json_body
5232
        assert res.json_body['code'] == error.WORKSPACE_INVALID_ID
5233
5234
    def test_api__get_thread__err_400_content_id_is_not_int(self) -> None:  # nopep8
5235
        """
5236
        Get one thread, content id is not int
5237
        """
5238
        self.testapp.authorization = (
5239
            'Basic',
5240
            (
5241
                '[email protected]',
5242
                '[email protected]'
5243
            )
5244
        )
5245
        res = self.testapp.get(
5246
            '/api/v2/workspaces/2/threads/coucou',
5247
            status=400
5248
        )
5249
        assert res.json_body
5250
        assert 'code' in res.json_body
5251
        assert res.json_body['code'] == error.CONTENT_INVALID_ID
5252
5253
    def test_api__update_thread__ok_200__nominal_case(self) -> None:
5254
        """
5255
        Update(put) thread
5256
        """
5257
        self.testapp.authorization = (
5258
            'Basic',
5259
            (
5260
                '[email protected]',
5261
                '[email protected]'
5262
            )
5263
        )
5264
        params = {
5265
            'label': 'My New label',
5266
            'raw_content': '<p> Le nouveau contenu </p>',
5267
        }
5268
        res = self.testapp.put_json(
5269
            '/api/v2/workspaces/2/threads/7',
5270
            params=params,
5271
            status=200
5272
        )
5273
        content = res.json_body
5274
        assert content['content_type'] == 'thread'
5275
        assert content['content_id'] == 7
5276
        assert content['is_archived'] is False
5277
        assert content['is_deleted'] is False
5278
        assert content['is_editable'] is True
5279
        assert content['label'] == 'My New label'
5280
        assert content['parent_id'] == 3
5281
        assert content['show_in_ui'] is True
5282
        assert content['slug'] == 'my-new-label'
5283
        assert content['status'] == 'open'
5284
        assert content['workspace_id'] == 2
5285
        assert content['current_revision_id'] == 28
5286
        # TODO - G.M - 2018-06-173 - check date format
5287
        assert content['created']
5288
        assert content['author']
5289
        assert content['author']['user_id'] == 1
5290
        assert content['author']['avatar_url'] is None
5291
        assert content['author']['public_name'] == 'Global manager'
5292
        # TODO - G.M - 2018-06-173 - check date format
5293
        assert content['modified']
5294
        assert content['last_modifier'] == content['author']
5295
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
5296
        assert content['file_extension'] == '.thread.html'
5297
        assert content['filename'] == 'My New label.thread.html'
5298
5299
        res = self.testapp.get(
5300
            '/api/v2/workspaces/2/threads/7',
5301
            status=200
5302
        )   # nopep8
5303
        content = res.json_body
5304
        assert content['content_type'] == 'thread'
5305
        assert content['content_id'] == 7
5306
        assert content['is_archived'] is False
5307
        assert content['is_deleted'] is False
5308
        assert content['is_editable'] is True
5309
        assert content['label'] == 'My New label'
5310
        assert content['parent_id'] == 3
5311
        assert content['show_in_ui'] is True
5312
        assert content['slug'] == 'my-new-label'
5313
        assert content['status'] == 'open'
5314
        assert content['workspace_id'] == 2
5315
        assert content['current_revision_id'] == 28
5316
        # TODO - G.M - 2018-06-173 - check date format
5317
        assert content['created']
5318
        assert content['author']
5319
        assert content['author']['user_id'] == 1
5320
        assert content['author']['avatar_url'] is None
5321
        assert content['author']['public_name'] == 'Global manager'
5322
        # TODO - G.M - 2018-06-173 - check date format
5323
        assert content['modified']
5324
        assert content['last_modifier'] == content['author']
5325
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
5326
        assert content['file_extension'] == '.thread.html'
5327
        assert content['filename'] == 'My New label.thread.html'
5328
5329 View Code Duplication
    def test_api__update_thread__err_400__not_modified(self) -> None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5330
        """
5331
        Update(put) thread
5332
        """
5333
        self.testapp.authorization = (
5334
            'Basic',
5335
            (
5336
                '[email protected]',
5337
                '[email protected]'
5338
            )
5339
        )
5340
        params = {
5341
            'label': 'My New label',
5342
            'raw_content': '<p> Le nouveau contenu </p>',
5343
        }
5344
        res = self.testapp.put_json(
5345
            '/api/v2/workspaces/2/threads/7',
5346
            params=params,
5347
            status=200
5348
        )
5349
        content = res.json_body
5350
        assert content['content_type'] == 'thread'
5351
        assert content['content_id'] == 7
5352
        assert content['is_archived'] is False
5353
        assert content['is_deleted'] is False
5354
        assert content['is_editable'] is True
5355
        assert content['label'] == 'My New label'
5356
        assert content['parent_id'] == 3
5357
        assert content['show_in_ui'] is True
5358
        assert content['slug'] == 'my-new-label'
5359
        assert content['status'] == 'open'
5360
        assert content['workspace_id'] == 2
5361
        assert content['current_revision_id'] == 28
5362
        # TODO - G.M - 2018-06-173 - check date format
5363
        assert content['created']
5364
        assert content['author']
5365
        assert content['author']['user_id'] == 1
5366
        assert content['author']['avatar_url'] is None
5367
        assert content['author']['public_name'] == 'Global manager'
5368
        # TODO - G.M - 2018-06-173 - check date format
5369
        assert content['modified']
5370
        assert content['last_modifier'] == content['author']
5371
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
5372
5373
        res = self.testapp.get(
5374
            '/api/v2/workspaces/2/threads/7',
5375
            status=200
5376
        )   # nopep8
5377
        content = res.json_body
5378
        assert content['content_type'] == 'thread'
5379
        assert content['content_id'] == 7
5380
        assert content['is_archived'] is False
5381
        assert content['is_deleted'] is False
5382
        assert content['is_editable'] is True
5383
        assert content['label'] == 'My New label'
5384
        assert content['parent_id'] == 3
5385
        assert content['show_in_ui'] is True
5386
        assert content['slug'] == 'my-new-label'
5387
        assert content['status'] == 'open'
5388
        assert content['workspace_id'] == 2
5389
        assert content['current_revision_id'] == 28
5390
        # TODO - G.M - 2018-06-173 - check date format
5391
        assert content['created']
5392
        assert content['author']
5393
        assert content['author']['user_id'] == 1
5394
        assert content['author']['avatar_url'] is None
5395
        assert content['author']['public_name'] == 'Global manager'
5396
        # TODO - G.M - 2018-06-173 - check date format
5397
        assert content['modified']
5398
        assert content['last_modifier'] == content['author']
5399
        assert content['raw_content'] == '<p> Le nouveau contenu </p>'
5400
5401
        res = self.testapp.put_json(
5402
            '/api/v2/workspaces/2/threads/7',
5403
            params=params,
5404
            status=400
5405
        )
5406
        assert res.json_body
5407
        assert 'code' in res.json_body
5408
        assert res.json_body['code'] == error.SAME_VALUE_ERROR
5409
5410
    def test_api__update_thread__err_400__empty_label(self) -> None:
5411
        """
5412
        Update(put) thread
5413
        """
5414
        self.testapp.authorization = (
5415
            'Basic',
5416
            (
5417
                '[email protected]',
5418
                '[email protected]'
5419
            )
5420
        )
5421
        params = {
5422
            'label': '',
5423
            'raw_content': '<p> Le nouveau contenu </p>',
5424
        }
5425
        res = self.testapp.put_json(
5426
            '/api/v2/workspaces/2/threads/7',
5427
            params=params,
5428
            status=400
5429
        )
5430
        # TODO - G.M - 2018-09-10 - Handle by marshmallow schema
5431
        assert res.json_body
5432
        assert 'code' in res.json_body
5433
        assert res.json_body['code'] == error.GENERIC_SCHEMA_VALIDATION_ERROR   # nopep8
5434
5435
    def test_api__get_thread_revisions__ok_200__nominal_case(
5436
            self
5437
    ) -> None:
5438
        """
5439
        Get threads revisions
5440
        """
5441
        self.testapp.authorization = (
5442
            'Basic',
5443
            (
5444
                '[email protected]',
5445
                '[email protected]'
5446
            )
5447
        )
5448
        res = self.testapp.get(
5449
            '/api/v2/workspaces/2/threads/7/revisions',
5450
            status=200
5451
        )
5452
        revisions = res.json_body
5453
        assert len(revisions) == 2
5454
        revision = revisions[0]
5455
        assert revision['content_type'] == 'thread'
5456
        assert revision['content_id'] == 7
5457
        assert revision['is_archived'] is False
5458
        assert revision['is_deleted'] is False
5459
        assert revision['is_editable'] is False
5460
        assert revision['label'] == 'Best Cake'
5461
        assert revision['parent_id'] == 3
5462
        assert revision['show_in_ui'] is True
5463
        assert revision['slug'] == 'best-cake'
5464
        assert revision['status'] == 'open'
5465
        assert revision['workspace_id'] == 2
5466
        assert revision['revision_id'] == 8
5467
        assert revision['sub_content_types']
5468
        assert revision['revision_type'] == 'creation'
5469
        assert revision['comment_ids'] == [18, 19, 20]
5470
        # TODO - G.M - 2018-06-173 - check date format
5471
        assert revision['created']
5472
        assert revision['author']
5473
        assert revision['author']['user_id'] == 1
5474
        assert revision['author']['avatar_url'] is None
5475
        assert revision['author']['public_name'] == 'Global manager'
5476
        assert revision['file_extension'] == '.thread.html'
5477
        assert revision['filename'] == 'Best Cake.thread.html'
5478
        revision = revisions[1]
5479
        assert revision['content_type'] == 'thread'
5480
        assert revision['content_id'] == 7
5481
        assert revision['is_archived'] is False
5482
        assert revision['is_deleted'] is False
5483
        assert revision['is_editable'] is True
5484
        assert revision['label'] == 'Best Cakes?'
5485
        assert revision['parent_id'] == 3
5486
        assert revision['show_in_ui'] is True
5487
        assert revision['slug'] == 'best-cakes'
5488
        assert revision['status'] == 'open'
5489
        assert revision['workspace_id'] == 2
5490
        assert revision['revision_id'] == 26
5491
        assert revision['revision_type'] == 'edition'
5492
        assert revision['sub_content_types']
5493
        assert revision['comment_ids'] == []
5494
        # TODO - G.M - 2018-06-173 - check date format
5495
        assert revision['created']
5496
        assert revision['author']
5497
        assert revision['author']['user_id'] == 3
5498
        assert revision['author']['avatar_url'] is None
5499
        assert revision['author']['public_name'] == 'Bob i.'
5500
        assert revision['file_extension'] == '.thread.html'
5501
        assert revision['filename'] == 'Best Cakes?.thread.html'
5502
5503
    def test_api__get_thread_revisions__ok_200__most_revision_type(self) -> None:
5504
        """
5505
        get threads revisions
5506
        """
5507
        dbsession = get_tm_session(self.session_factory, transaction.manager)
5508
        admin = dbsession.query(models.User) \
5509
            .filter(models.User.email == '[email protected]') \
5510
            .one()
5511
        workspace_api = WorkspaceApi(
5512
            current_user=admin,
5513
            session=dbsession,
5514
            config=self.app_config
5515
        )
5516
        business_workspace = workspace_api.get_one(1)
5517
        content_api = ContentApi(
5518
            current_user=admin,
5519
            session=dbsession,
5520
            config=self.app_config
5521
        )
5522
        tool_folder = content_api.get_one(1, content_type=content_type_list.Any_SLUG)
5523
        test_thread = content_api.create(
5524
            content_type_slug=content_type_list.Thread.slug,
5525
            workspace=business_workspace,
5526
            parent=tool_folder,
5527
            label='Test Thread',
5528
            do_save=True,
5529
            do_notify=False,
5530
        )
5531
        with new_revision(
5532
           session=dbsession,
5533
           tm=transaction.manager,
5534
           content=test_thread,
5535
        ):
5536
            content_api.update_content(
5537
                test_thread,
5538
                new_label='test_thread_updated',
5539
                new_content='Just a test'
5540
            )
5541
        content_api.save(test_thread)
5542
        with new_revision(
5543
           session=dbsession,
5544
           tm=transaction.manager,
5545
           content=test_thread,
5546
        ):
5547
            content_api.archive(test_thread)
5548
        content_api.save(test_thread)
5549
5550
        with new_revision(
5551
           session=dbsession,
5552
           tm=transaction.manager,
5553
           content=test_thread,
5554
        ):
5555
            content_api.unarchive(test_thread)
5556
        content_api.save(test_thread)
5557
5558
        with new_revision(
5559
           session=dbsession,
5560
           tm=transaction.manager,
5561
           content=test_thread,
5562
        ):
5563
            content_api.delete(test_thread)
5564
        content_api.save(test_thread)
5565
5566
        with new_revision(
5567
           session=dbsession,
5568
           tm=transaction.manager,
5569
           content=test_thread,
5570
        ):
5571
            content_api.undelete(test_thread)
5572
        content_api.save(test_thread)
5573
        dbsession.flush()
5574
        transaction.commit()
5575
        self.testapp.authorization = (
5576
            'Basic',
5577
            (
5578
                '[email protected]',
5579
                '[email protected]'
5580
            )
5581
        )
5582
        res = self.testapp.get(
5583
            '/api/v2/workspaces/1/threads/{}/revisions'.format(test_thread.content_id),  # nopep8
5584
            status=200
5585
        )
5586
        revisions = res.json_body
5587
        assert len(revisions) == 6
5588
        for revision in revisions:
5589
            assert revision['content_type'] == 'thread'
5590
            assert revision['workspace_id'] == 1
5591
            assert revision['content_id'] == test_thread.content_id
5592
        revision = revisions[0]
5593
        assert revision['revision_type'] == 'creation'
5594
        assert revision['is_editable'] is False
5595
        revision = revisions[1]
5596
        assert revision['revision_type'] == 'edition'
5597
        assert revision['is_editable'] is False
5598
        revision = revisions[2]
5599
        assert revision['revision_type'] == 'archiving'
5600
        assert revision['is_editable'] is False
5601
        revision = revisions[3]
5602
        assert revision['revision_type'] == 'unarchiving'
5603
        assert revision['is_editable'] is False
5604
        revision = revisions[4]
5605
        assert revision['revision_type'] == 'deletion'
5606
        assert revision['is_editable'] is False
5607
        revision = revisions[5]
5608
        assert revision['revision_type'] == 'undeletion'
5609
        assert revision['is_editable'] is True
5610
5611
    def test_api__set_thread_status__ok_200__nominal_case(self) -> None:
5612
        """
5613
        Set thread status
5614
        """
5615
        self.testapp.authorization = (
5616
            'Basic',
5617
            (
5618
                '[email protected]',
5619
                '[email protected]'
5620
            )
5621
        )
5622
        params = {
5623
            'status': 'closed-deprecated',
5624
        }
5625
5626
        # before
5627
        res = self.testapp.get(
5628
            '/api/v2/workspaces/2/threads/7',
5629
            status=200
5630
        )   # nopep8
5631
        content = res.json_body
5632
        assert content['content_type'] == 'thread'
5633
        assert content['content_id'] == 7
5634
        assert content['status'] == 'open'
5635
        assert content['is_editable'] is True
5636
        # set status
5637
        self.testapp.put_json(
5638
            '/api/v2/workspaces/2/threads/7/status',
5639
            params=params,
5640
            status=204
5641
        )
5642
5643
        # after
5644
        res = self.testapp.get(
5645
            '/api/v2/workspaces/2/threads/7',
5646
            status=200
5647
        )   # nopep8
5648
        content = res.json_body
5649
        assert content['content_type'] == 'thread'
5650
        assert content['content_id'] == 7
5651
        assert content['status'] == 'closed-deprecated'
5652
        assert content['is_editable'] is False
5653
5654
    def test_api__set_thread_status__ok_400__wrong_status(self) -> None:
5655
        """
5656
        Set thread status
5657
        """
5658
        self.testapp.authorization = (
5659
            'Basic',
5660
            (
5661
                '[email protected]',
5662
                '[email protected]'
5663
            )
5664
        )
5665
        params = {
5666
            'status': 'unexistant-status',
5667
        }
5668
5669
        res = self.testapp.put_json(
5670
            '/api/v2/workspaces/2/threads/7/status',
5671
            params=params,
5672
            status=400
5673
        )
5674
        # INFO - G.M - 2018-09-10 - Handle by marshmallow schema
5675
        assert res.json_body
5676
        assert 'code' in res.json_body
5677
        assert res.json_body['code'] == error.GENERIC_SCHEMA_VALIDATION_ERROR  # nopep8
5678