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

tracim_backend.tests.functional.test_workspaces   F

Complexity

Total Complexity 77

Size/Duplication

Total Lines 3164
Duplicated Lines 50.6 %

Importance

Changes 0
Metric Value
wmc 77
eloc 2335
dl 1601
loc 3164
rs 1.04
c 0
b 0
f 0

75 Methods

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