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

tracim_backend.tests.functional.test_workspaces   F

Complexity

Total Complexity 89

Size/Duplication

Total Lines 3653
Duplicated Lines 51.41 %

Importance

Changes 0
Metric Value
wmc 89
eloc 2716
dl 1878
loc 3653
rs 0.8
c 0
b 0
f 0

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