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

Complexity

Total Complexity 98

Size/Duplication

Total Lines 5880
Duplicated Lines 66.26 %

Importance

Changes 0
Metric Value
wmc 98
eloc 4642
dl 3896
loc 5880
rs 0.8
c 0
b 0
f 0

80 Methods

Rating   Name   Duplication   Size   Complexity  
B TestUserSetContentAsRead.test_api_set_content_as_read__ok__200__admin_content_do_not_exist() 84 84 1
B TestUserRecentlyActiveContentEndpoint.test_api__get_recently_active_content__err__403__other_user() 0 99 2
B TestUserRecentlyActiveContentEndpoint.test_api__get_recently_active_content__ok__200__user_itself() 125 125 4
B TestUserReadStatusEndpoint.test_api__get_read_status__err__403__other_user() 0 112 2
B TestUserSetContentAsRead.test_api_set_content_as_read__ok__403__other_user() 84 84 1
B TestUserSetContentAsRead.test_api_set_content_as_read__ok__200__admin_workspace_do_not_exist() 84 84 1
B TestUserRecentlyActiveContentEndpoint.test_api__get_recently_active_content__ok__200__admin() 124 124 4
B TestUserRecentlyActiveContentEndpoint.test_api__get_recently_active_content__err__400__no_access_to_workspace() 0 93 2
B TestUserSetContentAsRead.test_api_set_content_as_read__ok__200__admin() 107 107 1
B TestUserReadStatusEndpoint.test_api__get_read_status__ok__200__admin() 0 112 3
B TestUserRecentlyActiveContentEndpoint.test_api__get_recently_active_content__ok__200__limit_2_multiple() 111 111 4
B TestUserReadStatusEndpoint.test_api__get_read_status__ok__200__user_itself() 123 123 3
B TestUserRecentlyActiveContentEndpoint.test_api__get_recently_active_content__err__400__bad_before_content_id() 78 78 2
B TestUserSetContentAsRead.test_api_set_content_as_read__ok__200__user_itself() 0 99 1
B TestUserDisableWorkspaceNotification.test_api_enable_user_workspace_notification__ok__200__user_itself() 69 69 1
B TestUserEnableWorkspaceNotification.test_api_enable_user_workspace_notification__ok__200__admin() 69 69 1
A TestUserWithNotificationEndpoint.test_api_delete_user__err_400__admin_itself() 0 23 1
B TestUserEndpoint.test_api__get_user__ok_200__user_itself() 50 50 1
B TestUserWithNotificationEndpoint.test_api__create_user__ok_200__full_admin_with_notif() 0 58 1
A TestUserWorkspaceEndpoint.test_api__get_user_workspaces__err_400__user_does_not_exist() 0 19 1
A TestUserWorkspaceEndpoint.test_api__get_user_workspaces__err_403__unallowed_user() 0 18 1
B TestUserEndpoint.test_api__get_user__err_403__other_normal_user() 0 54 1
B TestUserSetContentAsUnread.test_api_set_content_as_unread__ok__200__with_comments_read_content() 54 54 1
B TestUserSetContentAsUnread.test_api_set_content_as_unread__err__403__other_user() 85 85 1
B TestUserEnableWorkspaceNotification.test_api_enable_user_workspace_notification__err__403__other_user() 75 75 1
B TestUserDisableWorkspaceNotification.test_api_disable_user_workspace_notification__ok__200__admin() 70 70 1
B TestUserEndpoint.test_api__create_user__ok_200__full_admin() 0 44 1
B TestKnownMembersEndpoint.test_api__get_user__ok_200__admin__by_name() 65 65 1
B TestUserSetContentAsRead.test_api_set_content_as_read__ok__200__admin_with_comments_read_content() 108 108 1
B TestUserSetContentAsUnread.test_api_set_content_as_unread__err__400__admin_workspace_do_not_exist() 84 84 1
B TestUserEnableWorkspaceNotification.test_api_enable_user_workspace_notification__ok__200__user_itself() 70 70 1
B TestUserSetContentAsUnread.test_api_set_content_as_unread__err__400__admin_content_do_not_exist() 85 85 1
A TestUserWorkspaceEndpoint.test_api__get_user_workspaces__ok_200__nominal_case() 42 42 2
B TestUserSetContentAsUnread.test_api_set_content_as_unread__ok__200__with_comments_read_comment_only() 54 54 1
B TestUserWithNotificationEndpoint.test_api_delete_user__ok_200__admin() 46 46 1
B TestUserEndpoint.test_api__get_user__ok_200__admin() 51 51 1
B TestUserWithNotificationEndpoint.test_api__create_user__ok_200__limited_admin_with_notif() 0 53 1
B TestUserSetContentAsUnread.test_api_set_content_as_unread__ok__200__admin() 109 109 1
B TestUserSetWorkspaceAsRead.test_api_set_content_as_read__ok__200__admin() 97 97 1
B TestUserEndpoint.test_api__create_user__err_403__other_user() 52 52 1
B TestUserSetWorkspaceAsRead.test_api_set_content_as_read__ok__200__user_itself() 97 97 1
B TestUsersEndpoint.test_api__get_user__err_403__normal_user() 44 44 1
A TestUserWorkspaceEndpoint.test_api__get_user_workspaces__err_401__unregistered_user() 0 18 1
B TestUserEndpoint.test_api__create_user__err_400__email_already_in_db() 52 52 1
B TestUserDisableWorkspaceNotification.test_api_disable_user_workspace_notification__err__403__other_user() 75 75 1
B TestUsersEndpoint.test_api__get_user__ok_200__admin() 0 50 1
B TestUserSetContentAsRead.test_api_set_content_as_read__ok__200__admin_with_comments_read_comment() 108 108 1
A TestUserEndpoint.test_api__create_user__ok_200__limited_admin() 0 40 1
B TestUserSetContentAsUnread.test_api_set_content_as_unread__ok__200__user_itself() 0 95 1
B TestUserSetWorkspaceAsRead.test_api_set_content_as_read__err__403__other_user() 0 85 1
B TestKnownMembersEndpoint.test_api__get_user__ok_200__admin__by_email() 65 65 1
B TestKnownMembersEndpoint.test_api__get_user__ok_200__admin__by_name_exclude_user() 63 63 1
B TestSetUserInfoEndpoint.test_api__set_user_info__ok_200__user_itself() 68 68 1
A TestSetUserEnableDisableEndpoints.test_api_disable_user__err_400__cant_disable_myself_admin() 0 47 1
B TestSetEmailEndpoint.test_api__set_user_email__err_400__admin_same_email() 65 65 1
B TestSetPasswordEndpoint.test_api__set_user_password__err_403__admin_wrong_password() 65 65 1
B TestSetUserEnableDisableEndpoints.test_api_enable_user__err_403__other_account() 56 56 1
B TestSetUserInfoEndpoint.test_api__set_user_info__err_403__other_normal_user() 0 62 1
B TestSetPasswordEndpoint.test_api__set_user_password__ok_200__user_itself() 62 62 1
B TestSetPasswordEndpoint.test_api__set_user_password__ok_200__admin() 62 62 1
B TestSetUserEnableDisableEndpoints.test_api_disable_user__err_403__other_account() 0 56 1
B TestKnownMembersEndpoint.test_api__get_user__ok_200__admin__by_name_exclude_workspace() 91 91 1
B TestKnownMembersEndpoint.test_api__get_user__ok_200__admin__by_name_exclude_workspace_and_user() 103 103 1
B TestSetUserEnableDisableEndpoints.test_api_disable_user__ok_200__admin() 0 58 1
A TestSetUserProfileEndpoint.test_api__set_user_profile__err_400__admin_itself() 0 44 1
B TestSetUserProfileEndpoint.test_api__set_user_profile__ok_200__admin() 62 62 1
B TestSetPasswordEndpoint.test_api__set_user_password__err_400__admin_passwords_do_not_match() 0 67 1
B TestSetEmailEndpoint.test_api__set_user_email__ok_200__admin() 0 62 1
B TestSetUserEnableDisableEndpoints.test_api_disable_user__err_403__cant_disable_myself_user() 0 66 1
B TestSetUserProfileEndpoint.test_api__set_user_profile__err_403__other_normal_user() 64 64 1
B TestKnownMembersEndpoint.test_api__get_user__ok_200__admin__by_name__deactivated_members() 62 62 1
B TestKnownMembersEndpoint.test_api__get_user__err_403__admin__too_small_acp() 0 58 1
B TestSetEmailEndpoint.test_api__set_user_email__err_403__admin_wrong_password() 65 65 1
B TestSetEmailEndpoint.test_api__set_user_email__ok_200__user_itself() 69 69 1
B TestKnownMembersEndpoint.test_api__get_user__ok_200__normal_user_by_email() 97 97 1
B TestSetUserEnableDisableEndpoints.test_api_enable_user__ok_200__admin() 0 58 1
B TestSetUserInfoEndpoint.test_api__set_user_info__ok_200__admin() 68 68 1
B TestSetEmailEndpoint.test_api__set_user_email__err_403__other_normal_user() 0 61 1
B TestSetEmailEndpoint.test_api__set_user_email__err_400__admin_string_is_not_email() 66 66 1
B TestSetPasswordEndpoint.test_api__set_user_email__err_403__other_normal_user() 0 61 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

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

Common duplication problems, and corresponding solutions are:

Complexity

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

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