backend.tracim_backend.tests.functional.test_account   B
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 2160
Duplicated Lines 85.28 %

Importance

Changes 0
Metric Value
wmc 36
eloc 1685
dl 1842
loc 2160
rs 8.32
c 0
b 0
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
B TestAccountDisableWorkspaceNotification.test_api_enable_account_workspace_notification__ok__200__nominal() 69 69 1
B TestAccountEndpoint.test_api__get_user__ok_200__nominal() 50 50 1
B TestUserSetWorkspaceAsRead.test_api_set_content_as_read__ok__200__nominal() 97 97 1
B TestAccountRecentlyActiveContentEndpoint.test_api__get_recently_active_content__ok__200__limit_2_multiple() 111 111 4
B TestAccountEnableWorkspaceNotification.test_api_enable_account_workspace_notification__ok__200__user_nominal() 70 70 1
B TestAccountKnownMembersEndpoint.test_api__get_user__ok_200__admin__by_name() 65 65 1
B TestUserSetContentAsUnread.test_api_set_content_as_unread__ok__200__nominal() 95 95 1
B TestAccountRecentlyActiveContentEndpoint.test_api__get_recently_active_content__err__400__bad_before_content_id() 78 78 2
B TestAccountRecentlyActiveContentEndpoint.test_api__get_recently_active_content__ok__200__nominal() 124 124 4
A TestAccountWorkspaceEndpoint.test_api__get_account_workspaces__ok_200__nominal_case() 42 42 2
B TestUserReadStatusEndpoint.test_api__get_read_status__ok__200__nominal() 122 122 3
B TestUserSetContentAsRead.test_api_set_content_as_read__ok__200__nominal() 99 99 1
B TestAccountKnownMembersEndpoint.test_api__get_user__ok_200__admin__by_name_exclude_workspace_and_user() 103 103 1
B TestAccountKnownMembersEndpoint.test_api__get_user__ok_200__normal_user_by_email() 97 97 1
B TestSetPasswordEndpoint.test_api__set_account_password__err_403__admin_wrong_password() 65 65 1
B TestAccountKnownMembersEndpoint.test_api__get_user__ok_200__admin__by_email() 65 65 1
B TestSetUserInfoEndpoint.test_api__set_account_info__ok_200__nominal() 68 68 1
B TestSetEmailEndpoint.test_api__set_account_email__ok_200__user_nominal() 69 69 1
B TestSetPasswordEndpoint.test_api__set_account_password__err_400__admin_passwords_do_not_match() 0 67 1
B TestAccountKnownMembersEndpoint.test_api__get_user__ok_200__admin__by_name_exclude_user() 63 63 1
B TestSetEmailEndpoint.test_api__set_account_email__err_403__admin_wrong_password() 0 65 1
B TestAccountKnownMembersEndpoint.test_api__get_user__ok_200__admin__by_name__deactivated_members() 62 62 1
B TestAccountKnownMembersEndpoint.test_api__get_user__err_403__admin__too_small_acp() 58 58 1
B TestSetPasswordEndpoint.test_api__set_account_password__ok_200__nominal() 62 62 1
B TestAccountKnownMembersEndpoint.test_api__get_user__ok_200__admin__by_name_exclude_workspace() 91 91 1
B TestSetEmailEndpoint.test_api__set_account_email__err_400__admin_same_email() 0 65 1

How to fix   Duplicated Code   

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:

1
# -*- coding: utf-8 -*-
2
"""
3
Tests for /api/v2/users subpath endpoints.
4
"""
5
from time import sleep
6
7
import pytest
8
import requests
9
import transaction
10
11
from tracim_backend import error
12
from tracim_backend import models
13
from tracim_backend.app_models.contents import content_type_list
14
from tracim_backend.extensions import app_list
15
from tracim_backend.fixtures.content import Content as ContentFixtures
16
from tracim_backend.fixtures.users_and_groups import Base as BaseFixture
17
from tracim_backend.lib.core.application import ApplicationApi
18
from tracim_backend.lib.core.content import ContentApi
19
from tracim_backend.lib.core.group import GroupApi
20
from tracim_backend.lib.core.user import UserApi
21
from tracim_backend.lib.core.userworkspace import RoleApi
22
from tracim_backend.lib.core.workspace import WorkspaceApi
23
from tracim_backend.models import get_tm_session
24
from tracim_backend.models.data import UserRoleInWorkspace
25
from tracim_backend.models.revision_protection import new_revision
26
from tracim_backend.tests import FunctionalTest
27
28
29
class TestAccountRecentlyActiveContentEndpoint(FunctionalTest):
30
    """
31
    Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/recently_active # nopep8
32
    """
33
    fixtures = [BaseFixture]
34
35 View Code Duplication
    def test_api__get_recently_active_content__ok__200__nominal(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
36
37
        # init DB
38
        dbsession = get_tm_session(self.session_factory, transaction.manager)
39
        admin = dbsession.query(models.User) \
40
            .filter(models.User.email == '[email protected]') \
41
            .one()
42
        workspace_api = WorkspaceApi(
43
            current_user=admin,
44
            session=dbsession,
45
            config=self.app_config
46
47
        )
48
        workspace = WorkspaceApi(
49
            current_user=admin,
50
            session=dbsession,
51
            config=self.app_config,
52
        ).create_workspace(
53
            'test workspace',
54
            save_now=True
55
        )
56
        workspace2 = WorkspaceApi(
57
            current_user=admin,
58
            session=dbsession,
59
            config=self.app_config,
60
        ).create_workspace(
61
            'test workspace2',
62
            save_now=True
63
        )
64
65
        uapi = UserApi(
66
            current_user=admin,
67
            session=dbsession,
68
            config=self.app_config,
69
        )
70
        gapi = GroupApi(
71
            current_user=admin,
72
            session=dbsession,
73
            config=self.app_config,
74
        )
75
        groups = [gapi.get_one_with_name('users')]
76
        test_user = uapi.create_user(
77
            email='[email protected]',
78
            password='pass',
79
            name='bob',
80
            groups=groups,
81
            timezone='Europe/Paris',
82
            lang='fr',
83
            do_save=True,
84
            do_notify=False,
85
        )
86
        rapi = RoleApi(
87
            current_user=admin,
88
            session=dbsession,
89
            config=self.app_config,
90
        )
91
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
92
        api = ContentApi(
93
            current_user=admin,
94
            session=dbsession,
95
            config=self.app_config,
96
        )
97
        main_folder_workspace2 = api.create(content_type_list.Folder.slug, workspace2, None, 'Hepla', '', True)  # nopep8
98
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
99
        # creation order test
100
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
101
        secondly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
102
        # update order test
103
        firstly_created_but_recently_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
104
        secondly_created_but_not_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
105
        with new_revision(
106
            session=dbsession,
107
            tm=transaction.manager,
108
            content=firstly_created_but_recently_updated,
109
        ):
110
            firstly_created_but_recently_updated.description = 'Just an update'
111
        api.save(firstly_created_but_recently_updated)
112
        # comment change order
113
        firstly_created_but_recently_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
114
        secondly_created_but_not_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
115
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
116
        content_workspace_2 = api.create(content_type_list.Page.slug, workspace2, main_folder_workspace2, 'content_workspace_2', '', True)  # nopep8
117
        dbsession.flush()
118
        transaction.commit()
119
120
        self.testapp.authorization = (
121
            'Basic',
122
            (
123
                '[email protected]',
124
                'pass'
125
            )
126
        )
127
        res = self.testapp.get('/api/v2/users/me/workspaces/{workspace_id}/contents/recently_active'.format(   # nopep8
128
            workspace_id=workspace.workspace_id
129
        ), status=200)
130
        res = res.json_body
131
        assert len(res) == 7
132
        for elem in res:
133
            assert isinstance(elem['content_id'], int)
134
            assert isinstance(elem['content_type'], str)
135
            assert elem['content_type'] != 'comments'
136
            assert isinstance(elem['is_archived'], bool)
137
            assert isinstance(elem['is_deleted'], bool)
138
            assert isinstance(elem['label'], str)
139
            assert isinstance(elem['parent_id'], int) or elem['parent_id'] is None
140
            assert isinstance(elem['show_in_ui'], bool)
141
            assert isinstance(elem['slug'], str)
142
            assert isinstance(elem['status'], str)
143
            assert isinstance(elem['sub_content_types'], list)
144
            for sub_content_type in elem['sub_content_types']:
145
                assert isinstance(sub_content_type, str)
146
            assert isinstance(elem['workspace_id'], int)
147
        # comment is newest than page2
148
        assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
149
        assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
150
        # last updated content is newer than other one despite creation
151
        # of the other is more recent
152
        assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
153
        assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
154
        # creation order is inverted here as last created is last active
155
        assert res[4]['content_id'] == secondly_created.content_id
156
        assert res[5]['content_id'] == firstly_created.content_id
157
        # folder subcontent modification does not change folder order
158
        assert res[6]['content_id'] == main_folder.content_id
159
160 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...
161
        # TODO - G.M - 2018-07-20 - Better fix for this test, do not use sleep()
162
        # anymore to fix datetime lack of precision.
163
164
        # init DB
165
        dbsession = get_tm_session(self.session_factory, transaction.manager)
166
        admin = dbsession.query(models.User) \
167
            .filter(models.User.email == '[email protected]') \
168
            .one()
169
        workspace_api = WorkspaceApi(
170
            current_user=admin,
171
            session=dbsession,
172
            config=self.app_config
173
174
        )
175
        workspace = WorkspaceApi(
176
            current_user=admin,
177
            session=dbsession,
178
            config=self.app_config,
179
        ).create_workspace(
180
            'test workspace',
181
            save_now=True
182
        )
183
        workspace2 = WorkspaceApi(
184
            current_user=admin,
185
            session=dbsession,
186
            config=self.app_config,
187
        ).create_workspace(
188
            'test workspace2',
189
            save_now=True
190
        )
191
192
        api = ContentApi(
193
            current_user=admin,
194
            session=dbsession,
195
            config=self.app_config,
196
        )
197
        main_folder_workspace2 = api.create(content_type_list.Folder.slug, workspace2, None, 'Hepla', '', True)  # nopep8
198
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
199
        # creation order test
200
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
201
        secondly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
202
        # update order test
203
        firstly_created_but_recently_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
204
        secondly_created_but_not_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
205
        with new_revision(
206
            session=dbsession,
207
            tm=transaction.manager,
208
            content=firstly_created_but_recently_updated,
209
        ):
210
            firstly_created_but_recently_updated.description = 'Just an update'
211
        api.save(firstly_created_but_recently_updated)
212
        # comment change order
213
        firstly_created_but_recently_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
214
        secondly_created_but_not_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
215
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
216
        content_workspace_2 = api.create(content_type_list.Page.slug, workspace2, main_folder_workspace2, 'content_workspace_2', '', True)  # nopep8
217
        dbsession.flush()
218
        transaction.commit()
219
220
        self.testapp.authorization = (
221
            'Basic',
222
            (
223
                '[email protected]',
224
                '[email protected]'
225
            )
226
        )
227
        params = {
228
            'limit': 2,
229
        }
230
        res = self.testapp.get(
231
            '/api/v2/users/me/workspaces/{}/contents/recently_active'.format(workspace.workspace_id),  # nopep8
232
            status=200,
233
            params=params
234
        ) # nopep8
235
        res = res.json_body
236
        assert len(res) == 2
237
        for elem in res:
238
            assert isinstance(elem['content_id'], int)
239
            assert isinstance(elem['content_type'], str)
240
            assert elem['content_type'] != 'comments'
241
            assert isinstance(elem['is_archived'], bool)
242
            assert isinstance(elem['is_deleted'], bool)
243
            assert isinstance(elem['label'], str)
244
            assert isinstance(elem['parent_id'], int) or elem['parent_id'] is None
245
            assert isinstance(elem['show_in_ui'], bool)
246
            assert isinstance(elem['slug'], str)
247
            assert isinstance(elem['status'], str)
248
            assert isinstance(elem['sub_content_types'], list)
249
            for sub_content_type in elem['sub_content_types']:
250
                assert isinstance(sub_content_type, str)
251
            assert isinstance(elem['workspace_id'], int)
252
        # comment is newest than page2
253
        assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
254
        assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
255
256
        params = {
257
            'limit': 2,
258
            'before_content_id': secondly_created_but_not_commented.content_id,  # nopep8
259
        }
260
        res = self.testapp.get(
261
            '/api/v2/users/me/workspaces/{}/contents/recently_active'.format(workspace.workspace_id),  # nopep8
262
            status=200,
263
            params=params
264
        )
265
        res = res.json_body
266
        assert len(res) == 2
267
        # last updated content is newer than other one despite creation
268
        # of the other is more recent
269
        assert res[0]['content_id'] == firstly_created_but_recently_updated.content_id
270
        assert res[1]['content_id'] == secondly_created_but_not_updated.content_id
271
272 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...
273
        # TODO - G.M - 2018-07-20 - Better fix for this test, do not use sleep()
274
        # anymore to fix datetime lack of precision.
275
276
        # init DB
277
        dbsession = get_tm_session(self.session_factory, transaction.manager)
278
        admin = dbsession.query(models.User) \
279
            .filter(models.User.email == '[email protected]') \
280
            .one()
281
        workspace_api = WorkspaceApi(
282
            current_user=admin,
283
            session=dbsession,
284
            config=self.app_config
285
286
        )
287
        workspace = WorkspaceApi(
288
            current_user=admin,
289
            session=dbsession,
290
            config=self.app_config,
291
        ).create_workspace(
292
            'test workspace',
293
            save_now=True
294
        )
295
        workspace2 = WorkspaceApi(
296
            current_user=admin,
297
            session=dbsession,
298
            config=self.app_config,
299
        ).create_workspace(
300
            'test workspace2',
301
            save_now=True
302
        )
303
304
        api = ContentApi(
305
            current_user=admin,
306
            session=dbsession,
307
            config=self.app_config,
308
        )
309
        main_folder_workspace2 = api.create(content_type_list.Folder.slug, workspace2, None, 'Hepla', '', True)  # nopep8
310
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
311
        # creation order test
312
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
313
        secondly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
314
        # update order test
315
        firstly_created_but_recently_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
316
        secondly_created_but_not_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
317
        with new_revision(
318
            session=dbsession,
319
            tm=transaction.manager,
320
            content=firstly_created_but_recently_updated,
321
        ):
322
            firstly_created_but_recently_updated.description = 'Just an update'
323
        api.save(firstly_created_but_recently_updated)
324
        # comment change order
325
        firstly_created_but_recently_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
326
        secondly_created_but_not_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
327
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
328
        content_workspace_2 = api.create(content_type_list.Page.slug, workspace2, main_folder_workspace2, 'content_workspace_2', '', True)  # nopep8
329
        dbsession.flush()
330
        transaction.commit()
331
332
        self.testapp.authorization = (
333
            'Basic',
334
            (
335
                '[email protected]',
336
                '[email protected]'
337
            )
338
        )
339
        params = {
340
            'before_content_id': 4000
341
        }
342
        res = self.testapp.get(
343
            '/api/v2/users/me/workspaces/{}/contents/recently_active'.format(workspace.workspace_id),  # nopep8
344
            status=400,
345
            params=params
346
        )
347
        assert isinstance(res.json, dict)
348
        assert 'code' in res.json.keys()
349
        assert res.json_body['code'] == error.CONTENT_NOT_FOUND
350
351
352
class TestUserReadStatusEndpoint(FunctionalTest):
353
    """
354
    Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status # nopep8
355
    """
356 View Code Duplication
    def test_api__get_read_status__ok__200__nominal(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
357
358
        # init DB
359
        dbsession = get_tm_session(self.session_factory, transaction.manager)
360
        admin = dbsession.query(models.User) \
361
            .filter(models.User.email == '[email protected]') \
362
            .one()
363
        workspace_api = WorkspaceApi(
364
            current_user=admin,
365
            session=dbsession,
366
            config=self.app_config
367
368
        )
369
        workspace = WorkspaceApi(
370
            current_user=admin,
371
            session=dbsession,
372
            config=self.app_config,
373
        ).create_workspace(
374
            'test workspace',
375
            save_now=True
376
        )
377
        workspace2 = WorkspaceApi(
378
            current_user=admin,
379
            session=dbsession,
380
            config=self.app_config,
381
        ).create_workspace(
382
            'test workspace2',
383
            save_now=True
384
        )
385
        uapi = UserApi(
386
            current_user=admin,
387
            session=dbsession,
388
            config=self.app_config,
389
        )
390
        gapi = GroupApi(
391
            current_user=admin,
392
            session=dbsession,
393
            config=self.app_config,
394
        )
395
        groups = [gapi.get_one_with_name('users')]
396
        test_user = uapi.create_user(
397
            email='[email protected]',
398
            password='pass',
399
            name='bob',
400
            groups=groups,
401
            timezone='Europe/Paris',
402
            lang='fr',
403
            do_save=True,
404
            do_notify=False,
405
        )
406
        rapi = RoleApi(
407
            current_user=admin,
408
            session=dbsession,
409
            config=self.app_config,
410
        )
411
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
412
        api = ContentApi(
413
            current_user=admin,
414
            session=dbsession,
415
            config=self.app_config,
416
        )
417
        main_folder_workspace2 = api.create(content_type_list.Folder.slug, workspace2, None, 'Hepla', '', True)  # nopep8
418
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
419
        # creation order test
420
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
421
        secondly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
422
        # update order test
423
        firstly_created_but_recently_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'update_order_test', '', True)  # nopep8
424
        secondly_created_but_not_updated = api.create(content_type_list.Page.slug, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
425
        with new_revision(
426
            session=dbsession,
427
            tm=transaction.manager,
428
            content=firstly_created_but_recently_updated,
429
        ):
430
            firstly_created_but_recently_updated.description = 'Just an update'
431
        api.save(firstly_created_but_recently_updated)
432
        # comment change order
433
        firstly_created_but_recently_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
434
        secondly_created_but_not_commented = api.create(content_type_list.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
435
        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
436
        content_workspace_2 = api.create(content_type_list.Page.slug, workspace2, main_folder_workspace2, 'content_workspace_2', '', True)  # nopep8
437
        dbsession.flush()
438
        transaction.commit()
439
440
        self.testapp.authorization = (
441
            'Basic',
442
            (
443
                '[email protected]',
444
                'pass'
445
            )
446
        )
447
        selected_contents_id = [
448
            firstly_created_but_recently_commented.content_id,
449
            firstly_created_but_recently_updated.content_id,
450
            firstly_created.content_id,
451
            main_folder.content_id,
452
        ]
453
        url = '/api/v2/users/me/workspaces/{workspace_id}/contents/read_status?contents_ids={cid1}&contents_ids={cid2}&contents_ids={cid3}&contents_ids={cid4}'.format(  # nopep8
454
              workspace_id=workspace.workspace_id,
455
              cid1=selected_contents_id[0],
456
              cid2=selected_contents_id[1],
457
              cid3=selected_contents_id[2],
458
              cid4=selected_contents_id[3],
459
        )
460
        res = self.testapp.get(
461
            url=url,
462
            status=200,
463
        )
464
        res = res.json_body
465
        assert len(res) == 4
466
        for elem in res:
467
            assert isinstance(elem['content_id'], int)
468
            assert isinstance(elem['read_by_user'], bool)
469
        # comment is newest than page2
470
        assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
471
        # last updated content is newer than other one despite creation
472
        # of the other is more recent
473
        assert res[1]['content_id'] == firstly_created_but_recently_updated.content_id
474
        # creation order is inverted here as last created is last active
475
        assert res[2]['content_id'] == firstly_created.content_id
476
        # folder subcontent modification does not change folder order
477
        assert res[3]['content_id'] == main_folder.content_id
478
479
480 View Code Duplication
class TestUserSetContentAsRead(FunctionalTest):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
481
    """
482
    Tests for /api/v2/users/me/workspaces/{workspace_id}/contents/{content_id}/read  # nopep8
483
    """
484
485
    def test_api_set_content_as_read__ok__200__nominal(self):
486
        # init DB
487
        dbsession = get_tm_session(self.session_factory, transaction.manager)
488
        admin = dbsession.query(models.User) \
489
            .filter(models.User.email == '[email protected]') \
490
            .one()
491
        workspace_api = WorkspaceApi(
492
            current_user=admin,
493
            session=dbsession,
494
            config=self.app_config
495
496
        )
497
        workspace = WorkspaceApi(
498
            current_user=admin,
499
            session=dbsession,
500
            config=self.app_config,
501
        ).create_workspace(
502
            'test workspace',
503
            save_now=True
504
        )
505
        uapi = UserApi(
506
            current_user=admin,
507
            session=dbsession,
508
            config=self.app_config,
509
        )
510
        gapi = GroupApi(
511
            current_user=admin,
512
            session=dbsession,
513
            config=self.app_config,
514
        )
515
        groups = [gapi.get_one_with_name('users')]
516
        test_user = uapi.create_user(
517
            email='[email protected]',
518
            password='pass',
519
            name='bob',
520
            groups=groups,
521
            timezone='Europe/Paris',
522
            lang='fr',
523
            do_save=True,
524
            do_notify=False,
525
        )
526
        rapi = RoleApi(
527
            current_user=admin,
528
            session=dbsession,
529
            config=self.app_config,
530
        )
531
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
532
        api = ContentApi(
533
            current_user=admin,
534
            session=dbsession,
535
            config=self.app_config,
536
        )
537
        api2 = ContentApi(
538
            current_user=test_user,
539
            session=dbsession,
540
            config=self.app_config,
541
        )
542
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
543
        # creation order test
544
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
545
        api.mark_unread(firstly_created)
546
        api2.mark_unread(firstly_created)
547
        dbsession.flush()
548
        transaction.commit()
549
550
        self.testapp.authorization = (
551
            'Basic',
552
            (
553
                '[email protected]',
554
                'pass'
555
            )
556
        )
557
        # before
558
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
559
            user_id=test_user.user_id,
560
            workspace_id=workspace.workspace_id
561
            ),
562
            status=200
563
        )
564
        assert res.json_body[0]['content_id'] == firstly_created.content_id
565
        assert res.json_body[0]['read_by_user'] is False
566
567
        # read
568
        self.testapp.put(
569
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format(  # nopep8
570
                workspace_id=workspace.workspace_id,
571
                content_id=firstly_created.content_id,
572
                user_id=test_user.user_id,
573
            )
574
        )
575
        # after
576
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
577
            user_id=test_user.user_id,
578
            workspace_id=workspace.workspace_id
579
            ),
580
            status=200
581
        )
582
        assert res.json_body[0]['content_id'] == firstly_created.content_id
583
        assert res.json_body[0]['read_by_user'] is True
584
585 View Code Duplication
class TestUserSetContentAsUnread(FunctionalTest):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
586
    """
587
    Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread  # nopep8
588
    """
589
    def test_api_set_content_as_unread__ok__200__nominal(self):
590
        # init DB
591
        dbsession = get_tm_session(self.session_factory, transaction.manager)
592
        admin = dbsession.query(models.User) \
593
            .filter(models.User.email == '[email protected]') \
594
            .one()
595
        workspace_api = WorkspaceApi(
596
            current_user=admin,
597
            session=dbsession,
598
            config=self.app_config
599
600
        )
601
        workspace = WorkspaceApi(
602
            current_user=admin,
603
            session=dbsession,
604
            config=self.app_config,
605
        ).create_workspace(
606
            'test workspace',
607
            save_now=True
608
        )
609
        uapi = UserApi(
610
            current_user=admin,
611
            session=dbsession,
612
            config=self.app_config,
613
        )
614
        gapi = GroupApi(
615
            current_user=admin,
616
            session=dbsession,
617
            config=self.app_config,
618
        )
619
        groups = [gapi.get_one_with_name('users')]
620
        test_user = uapi.create_user(
621
            email='[email protected]',
622
            password='pass',
623
            name='bob',
624
            groups=groups,
625
            timezone='Europe/Paris',
626
            lang='fr',
627
            do_save=True,
628
            do_notify=False,
629
        )
630
        rapi = RoleApi(
631
            current_user=admin,
632
            session=dbsession,
633
            config=self.app_config,
634
        )
635
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
636
        api = ContentApi(
637
            current_user=admin,
638
            session=dbsession,
639
            config=self.app_config,
640
        )
641
        api2 = ContentApi(
642
            current_user=test_user,
643
            session=dbsession,
644
            config=self.app_config,
645
        )
646
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
647
        # creation order test
648
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
649
        api.mark_read(firstly_created)
650
        api2.mark_read(firstly_created)
651
        dbsession.flush()
652
        transaction.commit()
653
654
        self.testapp.authorization = (
655
            'Basic',
656
            (
657
                '[email protected]',
658
                'pass'
659
            )
660
        )
661
        # before
662
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
663
            user_id=test_user.user_id,
664
            workspace_id=workspace.workspace_id
665
        ), status=200)
666
        assert res.json_body[0]['content_id'] == firstly_created.content_id
667
        assert res.json_body[0]['read_by_user'] is True
668
669
        # unread
670
        self.testapp.put(
671
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format(  # nopep8
672
                workspace_id=workspace.workspace_id,
673
                content_id=firstly_created.content_id,
674
                user_id=test_user.user_id,
675
            )
676
        )
677
        # after
678
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
679
            user_id=test_user.user_id,
680
            workspace_id=workspace.workspace_id
681
        ), status=200)
682
        assert res.json_body[0]['content_id'] == firstly_created.content_id
683
        assert res.json_body[0]['read_by_user'] is False
684
685
686
687
class TestUserSetWorkspaceAsRead(FunctionalTest):
688
    """
689
    Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/read
690
    """
691 View Code Duplication
    def test_api_set_content_as_read__ok__200__nominal(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
692
        # init DB
693
        dbsession = get_tm_session(self.session_factory, transaction.manager)
694
        admin = dbsession.query(models.User) \
695
            .filter(models.User.email == '[email protected]') \
696
            .one()
697
        workspace_api = WorkspaceApi(
698
            current_user=admin,
699
            session=dbsession,
700
            config=self.app_config
701
702
        )
703
        workspace = WorkspaceApi(
704
            current_user=admin,
705
            session=dbsession,
706
            config=self.app_config,
707
        ).create_workspace(
708
            'test workspace',
709
            save_now=True
710
        )
711
        uapi = UserApi(
712
            current_user=admin,
713
            session=dbsession,
714
            config=self.app_config,
715
        )
716
        gapi = GroupApi(
717
            current_user=admin,
718
            session=dbsession,
719
            config=self.app_config,
720
        )
721
        groups = [gapi.get_one_with_name('users')]
722
        test_user = uapi.create_user(
723
            email='[email protected]',
724
            password='pass',
725
            name='bob',
726
            groups=groups,
727
            timezone='Europe/Paris',
728
            lang='fr',
729
            do_save=True,
730
            do_notify=False,
731
        )
732
        rapi = RoleApi(
733
            current_user=admin,
734
            session=dbsession,
735
            config=self.app_config,
736
        )
737
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
738
        api = ContentApi(
739
            current_user=admin,
740
            session=dbsession,
741
            config=self.app_config,
742
        )
743
        api2 = ContentApi(
744
            current_user=test_user,
745
            session=dbsession,
746
            config=self.app_config,
747
        )
748
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
749
        # creation order test
750
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
751
        api.mark_unread(main_folder)
752
        api.mark_unread(firstly_created)
753
        api2.mark_unread(main_folder)
754
        api2.mark_unread(firstly_created)
755
        dbsession.flush()
756
        transaction.commit()
757
758
        self.testapp.authorization = (
759
            'Basic',
760
            (
761
                '[email protected]',
762
                'pass'
763
            )
764
        )
765
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
766
            user_id=test_user.user_id,
767
            workspace_id=workspace.workspace_id
768
        ), status=200)
769
        assert res.json_body[0]['content_id'] == firstly_created.content_id
770
        assert res.json_body[0]['read_by_user'] is False
771
        assert res.json_body[1]['content_id'] == main_folder.content_id
772
        assert res.json_body[1]['read_by_user'] is False
773
        self.testapp.put(
774
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/read'.format(  # nopep8
775
                workspace_id=workspace.workspace_id,
776
                content_id=firstly_created.content_id,
777
                user_id=test_user.user_id,
778
            )
779
        )
780
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
781
            user_id=test_user.user_id,
782
            workspace_id=workspace.workspace_id
783
        ), status=200)
784
        assert res.json_body[0]['content_id'] == firstly_created.content_id
785
        assert res.json_body[0]['read_by_user'] is True
786
        assert res.json_body[1]['content_id'] == main_folder.content_id
787
        assert res.json_body[1]['read_by_user'] is True
788
789 View Code Duplication
class TestAccountEnableWorkspaceNotification(FunctionalTest):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
790
    """
791
    Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/notifications/activate
792
    """
793
    def test_api_enable_account_workspace_notification__ok__200__user_nominal(self):
794
        # init DB
795
        dbsession = get_tm_session(self.session_factory, transaction.manager)
796
        admin = dbsession.query(models.User) \
797
            .filter(models.User.email == '[email protected]') \
798
            .one()
799
        workspace_api = WorkspaceApi(
800
            current_user=admin,
801
            session=dbsession,
802
            config=self.app_config
803
804
        )
805
        workspace = WorkspaceApi(
806
            current_user=admin,
807
            session=dbsession,
808
            config=self.app_config,
809
        ).create_workspace(
810
            'test workspace',
811
            save_now=True
812
        )
813
        uapi = UserApi(
814
            current_user=admin,
815
            session=dbsession,
816
            config=self.app_config,
817
        )
818
        gapi = GroupApi(
819
            current_user=admin,
820
            session=dbsession,
821
            config=self.app_config,
822
        )
823
        groups = [gapi.get_one_with_name('users')]
824
        test_user = uapi.create_user(
825
            email='[email protected]',
826
            password='pass',
827
            name='bob',
828
            groups=groups,
829
            timezone='Europe/Paris',
830
            lang='fr',
831
            do_save=True,
832
            do_notify=False,
833
        )
834
        rapi = RoleApi(
835
            current_user=admin,
836
            session=dbsession,
837
            config=self.app_config,
838
        )
839
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, with_notif=False)  # nopep8
840
        transaction.commit()
841
        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
842
        assert role.do_notify is False
843
        self.testapp.authorization = (
844
            'Basic',
845
            (
846
                '[email protected]',
847
                'pass',
848
            )
849
        )
850
        self.testapp.put_json('/api/v2/users/{user_id}/workspaces/{workspace_id}/notifications/activate'.format(  # nopep8
851
            user_id=test_user.user_id,
852
            workspace_id=workspace.workspace_id
853
        ), status=204)
854
855
        dbsession = get_tm_session(self.session_factory, transaction.manager)
856
        rapi = RoleApi(
857
            current_user=admin,
858
            session=dbsession,
859
            config=self.app_config,
860
        )
861
        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
862
        assert role.do_notify is True
863
864
865 View Code Duplication
class TestAccountDisableWorkspaceNotification(FunctionalTest):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
866
    """
867
    Tests for /api/v2/users/me/workspaces/{workspace_id}/notifications/deactivate  # nopep8
868
    """
869
    def test_api_enable_account_workspace_notification__ok__200__nominal(self):
870
        # init DB
871
        dbsession = get_tm_session(self.session_factory, transaction.manager)
872
        admin = dbsession.query(models.User) \
873
            .filter(models.User.email == '[email protected]') \
874
            .one()
875
        workspace_api = WorkspaceApi(
876
            current_user=admin,
877
            session=dbsession,
878
            config=self.app_config
879
880
        )
881
        workspace = WorkspaceApi(
882
            current_user=admin,
883
            session=dbsession,
884
            config=self.app_config,
885
        ).create_workspace(
886
            'test workspace',
887
            save_now=True
888
        )
889
        uapi = UserApi(
890
            current_user=admin,
891
            session=dbsession,
892
            config=self.app_config,
893
        )
894
        gapi = GroupApi(
895
            current_user=admin,
896
            session=dbsession,
897
            config=self.app_config,
898
        )
899
        groups = [gapi.get_one_with_name('users')]
900
        test_user = uapi.create_user(
901
            email='[email protected]',
902
            password='pass',
903
            name='bob',
904
            groups=groups,
905
            timezone='Europe/Paris',
906
            lang='fr',
907
            do_save=True,
908
            do_notify=False,
909
        )
910
        rapi = RoleApi(
911
            current_user=admin,
912
            session=dbsession,
913
            config=self.app_config,
914
        )
915
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, with_notif=True)  # nopep8
916
        transaction.commit()
917
        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
918
        assert role.do_notify is True
919
        self.testapp.authorization = (
920
            'Basic',
921
            (
922
                '[email protected]',
923
                'pass',
924
            )
925
        )
926
        self.testapp.put_json('/api/v2/users/me/workspaces/{workspace_id}/notifications/deactivate'.format(  # nopep8
927
            user_id=test_user.user_id,
928
            workspace_id=workspace.workspace_id
929
        ), status=204)
930
        dbsession = get_tm_session(self.session_factory, transaction.manager)
931
        rapi = RoleApi(
932
            current_user=admin,
933
            session=dbsession,
934
            config=self.app_config,
935
        )
936
        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
937
        assert role.do_notify is False
938
939
940
class TestAccountWorkspaceEndpoint(FunctionalTest):
941
    """
942
    Tests for /api/v2/users/me/workspaces
943
    """
944
    fixtures = [BaseFixture, ContentFixtures]
945
946 View Code Duplication
    def test_api__get_account_workspaces__ok_200__nominal_case(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
947
        """
948
        Check obtain all workspaces reachables for user with user auth.
949
        """
950
        dbsession = get_tm_session(self.session_factory, transaction.manager)
951
        admin = dbsession.query(models.User) \
952
            .filter(models.User.email == '[email protected]') \
953
            .one()
954
955
        workspace_api = WorkspaceApi(
956
            session=dbsession,
957
            current_user=admin,
958
            config=self.app_config,
959
        )
960
        workspace = workspace_api.get_one(1)
961
        app_api = ApplicationApi(
962
            app_list
963
        )
964
965
        default_sidebar_entry = app_api.get_default_workspace_menu_entry(workspace=workspace)  # nope8
966
        self.testapp.authorization = (
967
            'Basic',
968
            (
969
                '[email protected]',
970
                '[email protected]'
971
            )
972
        )
973
        res = self.testapp.get('/api/v2/users/1/workspaces', status=200)
974
        res = res.json_body
975
        workspace = res[0]
976
        assert workspace['workspace_id'] == 1
977
        assert workspace['label'] == 'Business'
978
        assert workspace['slug'] == 'business'
979
        assert workspace['is_deleted'] is False
980
981
        assert len(workspace['sidebar_entries']) == len(default_sidebar_entry)
982
        for counter, sidebar_entry in enumerate(default_sidebar_entry):
983
            workspace['sidebar_entries'][counter]['slug'] = sidebar_entry.slug
984
            workspace['sidebar_entries'][counter]['label'] = sidebar_entry.label
985
            workspace['sidebar_entries'][counter]['route'] = sidebar_entry.route
986
            workspace['sidebar_entries'][counter]['hexcolor'] = sidebar_entry.hexcolor  # nopep8
987
            workspace['sidebar_entries'][counter]['fa_icon'] = sidebar_entry.fa_icon  # nopep8
988
989
990
class TestAccountEndpoint(FunctionalTest):
991
    # -*- coding: utf-8 -*-
992
    """
993
    Tests for GET /api/v2/users/me
994
    """
995
    fixtures = [BaseFixture]
996
997 View Code Duplication
    def test_api__get_user__ok_200__nominal(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
998
        dbsession = get_tm_session(self.session_factory, transaction.manager)
999
        admin = dbsession.query(models.User) \
1000
            .filter(models.User.email == '[email protected]') \
1001
            .one()
1002
        uapi = UserApi(
1003
            current_user=admin,
1004
            session=dbsession,
1005
            config=self.app_config,
1006
        )
1007
        gapi = GroupApi(
1008
            current_user=admin,
1009
            session=dbsession,
1010
            config=self.app_config,
1011
        )
1012
        groups = [gapi.get_one_with_name('users')]
1013
        test_user = uapi.create_user(
1014
            email='[email protected]',
1015
            password='pass',
1016
            name='bob',
1017
            groups=groups,
1018
            timezone='Europe/Paris',
1019
            lang='fr',
1020
            do_save=True,
1021
            do_notify=False,
1022
        )
1023
        uapi.save(test_user)
1024
        transaction.commit()
1025
        user_id = int(test_user.user_id)
1026
1027
        self.testapp.authorization = (
1028
            'Basic',
1029
            (
1030
                '[email protected]',
1031
                'pass'
1032
            )
1033
        )
1034
        res = self.testapp.get(
1035
            '/api/v2/users/me',
1036
            status=200
1037
        )
1038
        res = res.json_body
1039
        assert res['user_id'] == user_id
1040
        assert res['created']
1041
        assert res['is_active'] is True
1042
        assert res['profile'] == 'users'
1043
        assert res['email'] == '[email protected]'
1044
        assert res['public_name'] == 'bob'
1045
        assert res['timezone'] == 'Europe/Paris'
1046
        assert res['is_deleted'] is False
1047
1048
1049
class TestAccountKnownMembersEndpoint(FunctionalTest):
1050
    # -*- coding: utf-8 -*-
1051
    """
1052
    Tests for GET /api/v2/users/me
1053
    """
1054
    fixtures = [BaseFixture]
1055
1056 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...
1057
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1058
        admin = dbsession.query(models.User) \
1059
            .filter(models.User.email == '[email protected]') \
1060
            .one()
1061
        uapi = UserApi(
1062
            current_user=admin,
1063
            session=dbsession,
1064
            config=self.app_config,
1065
        )
1066
        gapi = GroupApi(
1067
            current_user=admin,
1068
            session=dbsession,
1069
            config=self.app_config,
1070
        )
1071
        groups = [gapi.get_one_with_name('users')]
1072
        test_user = uapi.create_user(
1073
            email='[email protected]',
1074
            password='pass',
1075
            name='bob',
1076
            groups=groups,
1077
            timezone='Europe/Paris',
1078
            lang='fr',
1079
            do_save=True,
1080
            do_notify=False,
1081
        )
1082
        test_user2 = uapi.create_user(
1083
            email='[email protected]',
1084
            password='pass',
1085
            name='bob2',
1086
            groups=groups,
1087
            timezone='Europe/Paris',
1088
            lang='fr',
1089
            do_save=True,
1090
            do_notify=False,
1091
        )
1092
        uapi.save(test_user)
1093
        uapi.save(test_user2)
1094
        transaction.commit()
1095
        user_id = int(admin.user_id)
1096
1097
        self.testapp.authorization = (
1098
            'Basic',
1099
            (
1100
                '[email protected]',
1101
                '[email protected]'
1102
            )
1103
        )
1104
        params = {
1105
            'acp': 'bob',
1106
        }
1107
        res = self.testapp.get(
1108
            '/api/v2/users/me/known_members',
1109
            status=200,
1110
            params=params,
1111
        )
1112
        res = res.json_body
1113
        assert len(res) == 2
1114
        assert res[0]['user_id'] == test_user.user_id
1115
        assert res[0]['public_name'] == test_user.display_name
1116
        assert res[0]['avatar_url'] is None
1117
1118
        assert res[1]['user_id'] == test_user2.user_id
1119
        assert res[1]['public_name'] == test_user2.display_name
1120
        assert res[1]['avatar_url'] is None
1121
1122 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...
1123
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1124
        admin = dbsession.query(models.User) \
1125
            .filter(models.User.email == '[email protected]') \
1126
            .one()
1127
        uapi = UserApi(
1128
            current_user=admin,
1129
            session=dbsession,
1130
            config=self.app_config,
1131
        )
1132
        gapi = GroupApi(
1133
            current_user=admin,
1134
            session=dbsession,
1135
            config=self.app_config,
1136
        )
1137
        groups = [gapi.get_one_with_name('users')]
1138
        test_user = uapi.create_user(
1139
            email='[email protected]',
1140
            password='pass',
1141
            name='bob',
1142
            groups=groups,
1143
            timezone='Europe/Paris',
1144
            lang='fr',
1145
            do_save=True,
1146
            do_notify=False,
1147
        )
1148
        test_user2 = uapi.create_user(
1149
            email='[email protected]',
1150
            password='pass',
1151
            name='bob2',
1152
            groups=groups,
1153
            timezone='Europe/Paris',
1154
            lang='fr',
1155
            do_save=True,
1156
            do_notify=False,
1157
        )
1158
1159
        uapi.save(test_user)
1160
        uapi.save(test_user2)
1161
        transaction.commit()
1162
        user_id = int(admin.user_id)
1163
1164
        self.testapp.authorization = (
1165
            'Basic',
1166
            (
1167
                '[email protected]',
1168
                '[email protected]'
1169
            )
1170
        )
1171
        params = {
1172
            'acp': 'bob',
1173
            'exclude_user_ids': [test_user2.user_id]
1174
        }
1175
        res = self.testapp.get(
1176
            '/api/v2/users/me/known_members',
1177
            status=200,
1178
            params=params,
1179
        )
1180
        res = res.json_body
1181
        assert len(res) == 1
1182
        assert res[0]['user_id'] == test_user.user_id
1183
        assert res[0]['public_name'] == test_user.display_name
1184
        assert res[0]['avatar_url'] is None
1185
1186 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...
1187
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1188
        admin = dbsession.query(models.User) \
1189
            .filter(models.User.email == '[email protected]') \
1190
            .one()
1191
        uapi = UserApi(
1192
            current_user=admin,
1193
            session=dbsession,
1194
            config=self.app_config,
1195
        )
1196
        gapi = GroupApi(
1197
            current_user=admin,
1198
            session=dbsession,
1199
            config=self.app_config,
1200
        )
1201
        groups = [gapi.get_one_with_name('users')]
1202
        test_user = uapi.create_user(
1203
            email='[email protected]',
1204
            password='pass',
1205
            name='bob',
1206
            groups=groups,
1207
            timezone='Europe/Paris',
1208
            lang='fr',
1209
            do_save=True,
1210
            do_notify=False,
1211
        )
1212
        test_user2 = uapi.create_user(
1213
            email='[email protected]',
1214
            password='pass',
1215
            name='bob2',
1216
            groups=groups,
1217
            timezone='Europe/Paris',
1218
            lang='fr',
1219
            do_save=True,
1220
            do_notify=False,
1221
        )
1222
        workspace_api = WorkspaceApi(
1223
            current_user=admin,
1224
            session=dbsession,
1225
            config=self.app_config
1226
1227
        )
1228
        workspace = WorkspaceApi(
1229
            current_user=admin,
1230
            session=dbsession,
1231
            config=self.app_config,
1232
        ).create_workspace(
1233
            'test workspace',
1234
            save_now=True
1235
        )
1236
        workspace2 = WorkspaceApi(
1237
            current_user=admin,
1238
            session=dbsession,
1239
            config=self.app_config,
1240
        ).create_workspace(
1241
            'test workspace2',
1242
            save_now=True
1243
        )
1244
        role_api = RoleApi(
1245
            current_user=admin,
1246
            session=dbsession,
1247
            config=self.app_config,
1248
        )
1249
        role_api.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1250
        role_api.create_one(test_user2, workspace2, UserRoleInWorkspace.READER, False)
1251
        uapi.save(test_user)
1252
        uapi.save(test_user2)
1253
        transaction.commit()
1254
        user_id = int(admin.user_id)
1255
1256
        self.testapp.authorization = (
1257
            'Basic',
1258
            (
1259
                '[email protected]',
1260
                '[email protected]'
1261
            )
1262
        )
1263
        params = {
1264
            'acp': 'bob',
1265
            'exclude_workspace_ids': [workspace2.workspace_id]
1266
        }
1267
        res = self.testapp.get(
1268
            '/api/v2/users/me/known_members',
1269
            status=200,
1270
            params=params,
1271
        )
1272
        res = res.json_body
1273
        assert len(res) == 1
1274
        assert res[0]['user_id'] == test_user.user_id
1275
        assert res[0]['public_name'] == test_user.display_name
1276
        assert res[0]['avatar_url'] is None
1277
1278 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...
1279
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1280
        admin = dbsession.query(models.User) \
1281
            .filter(models.User.email == '[email protected]') \
1282
            .one()
1283
        uapi = UserApi(
1284
            current_user=admin,
1285
            session=dbsession,
1286
            config=self.app_config,
1287
        )
1288
        gapi = GroupApi(
1289
            current_user=admin,
1290
            session=dbsession,
1291
            config=self.app_config,
1292
        )
1293
        groups = [gapi.get_one_with_name('users')]
1294
        test_user = uapi.create_user(
1295
            email='[email protected]',
1296
            password='pass',
1297
            name='bob',
1298
            groups=groups,
1299
            timezone='Europe/Paris',
1300
            lang='fr',
1301
            do_save=True,
1302
            do_notify=False,
1303
        )
1304
        test_user2 = uapi.create_user(
1305
            email='[email protected]',
1306
            password='pass',
1307
            name='bob2',
1308
            groups=groups,
1309
            timezone='Europe/Paris',
1310
            lang='fr',
1311
            do_save=True,
1312
            do_notify=False,
1313
        )
1314
        test_user3 = uapi.create_user(
1315
            email='[email protected]',
1316
            password='pass',
1317
            name='bob3',
1318
            groups=groups,
1319
            timezone='Europe/Paris',
1320
            lang='fr',
1321
            do_save=True,
1322
            do_notify=False,
1323
        )
1324
        workspace_api = WorkspaceApi(
1325
            current_user=admin,
1326
            session=dbsession,
1327
            config=self.app_config
1328
1329
        )
1330
        workspace = WorkspaceApi(
1331
            current_user=admin,
1332
            session=dbsession,
1333
            config=self.app_config,
1334
        ).create_workspace(
1335
            'test workspace',
1336
            save_now=True
1337
        )
1338
        workspace2 = WorkspaceApi(
1339
            current_user=admin,
1340
            session=dbsession,
1341
            config=self.app_config,
1342
        ).create_workspace(
1343
            'test workspace2',
1344
            save_now=True
1345
        )
1346
        role_api = RoleApi(
1347
            current_user=admin,
1348
            session=dbsession,
1349
            config=self.app_config,
1350
        )
1351
        role_api.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1352
        role_api.create_one(test_user2, workspace2, UserRoleInWorkspace.READER, False)
1353
        role_api.create_one(test_user3, workspace, UserRoleInWorkspace.READER, False)
1354
        uapi.save(test_user)
1355
        uapi.save(test_user2)
1356
        transaction.commit()
1357
        user_id = int(admin.user_id)
1358
1359
        self.testapp.authorization = (
1360
            'Basic',
1361
            (
1362
                '[email protected]',
1363
                '[email protected]'
1364
            )
1365
        )
1366
        params = {
1367
            'acp': 'bob',
1368
            'exclude_workspace_ids': [workspace2.workspace_id],
1369
            'exclude_user_ids': [test_user3.user_id]
1370
        }
1371
        res = self.testapp.get(
1372
            '/api/v2/users/me/known_members',
1373
            status=200,
1374
            params=params,
1375
        )
1376
        res = res.json_body
1377
        assert len(res) == 1
1378
        assert res[0]['user_id'] == test_user.user_id
1379
        assert res[0]['public_name'] == test_user.display_name
1380
        assert res[0]['avatar_url'] is None
1381
1382 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...
1383
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1384
        admin = dbsession.query(models.User) \
1385
            .filter(models.User.email == '[email protected]') \
1386
            .one()
1387
        uapi = UserApi(
1388
            current_user=admin,
1389
            session=dbsession,
1390
            config=self.app_config,
1391
        )
1392
        gapi = GroupApi(
1393
            current_user=admin,
1394
            session=dbsession,
1395
            config=self.app_config,
1396
        )
1397
        groups = [gapi.get_one_with_name('users')]
1398
        test_user = uapi.create_user(
1399
            email='[email protected]',
1400
            password='pass',
1401
            name='bob',
1402
            groups=groups,
1403
            timezone='Europe/Paris',
1404
            lang='fr',
1405
            do_save=True,
1406
            do_notify=False,
1407
        )
1408
        test_user2 = uapi.create_user(
1409
            email='[email protected]',
1410
            password='pass',
1411
            name='bob2',
1412
            groups=groups,
1413
            timezone='Europe/Paris',
1414
            lang='fr',
1415
            do_save=True,
1416
            do_notify=False,
1417
        )
1418
        test_user2.is_active = False
1419
        uapi.save(test_user)
1420
        uapi.save(test_user2)
1421
        transaction.commit()
1422
        user_id = int(admin.user_id)
1423
1424
        self.testapp.authorization = (
1425
            'Basic',
1426
            (
1427
                '[email protected]',
1428
                '[email protected]'
1429
            )
1430
        )
1431
        params = {
1432
            'acp': 'bob',
1433
        }
1434
        res = self.testapp.get(
1435
            '/api/v2/users/me/known_members',
1436
            status=200,
1437
            params=params,
1438
        )
1439
        res = res.json_body
1440
        assert len(res) == 1
1441
        assert res[0]['user_id'] == test_user.user_id
1442
        assert res[0]['public_name'] == test_user.display_name
1443
        assert res[0]['avatar_url'] is None
1444
1445 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...
1446
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1447
        admin = dbsession.query(models.User) \
1448
            .filter(models.User.email == '[email protected]') \
1449
            .one()
1450
        uapi = UserApi(
1451
            current_user=admin,
1452
            session=dbsession,
1453
            config=self.app_config,
1454
        )
1455
        gapi = GroupApi(
1456
            current_user=admin,
1457
            session=dbsession,
1458
            config=self.app_config,
1459
        )
1460
        groups = [gapi.get_one_with_name('users')]
1461
        test_user = uapi.create_user(
1462
            email='[email protected]',
1463
            password='pass',
1464
            name='bob',
1465
            groups=groups,
1466
            timezone='Europe/Paris',
1467
            lang='fr',
1468
            do_save=True,
1469
            do_notify=False,
1470
        )
1471
        test_user2 = uapi.create_user(
1472
            email='[email protected]',
1473
            password='pass',
1474
            name='bob2',
1475
            groups=groups,
1476
            timezone='Europe/Paris',
1477
            lang='fr',
1478
            do_save=True,
1479
            do_notify=False,
1480
        )
1481
        uapi.save(test_user)
1482
        uapi.save(test_user2)
1483
        transaction.commit()
1484
        user_id = int(admin.user_id)
1485
1486
        self.testapp.authorization = (
1487
            'Basic',
1488
            (
1489
                '[email protected]',
1490
                '[email protected]'
1491
            )
1492
        )
1493
        params = {
1494
            'acp': 'test',
1495
        }
1496
        res = self.testapp.get(
1497
            '/api/v2/users/me/known_members',
1498
            status=200,
1499
            params=params,
1500
        )
1501
        res = res.json_body
1502
        assert len(res) == 2
1503
        assert res[0]['user_id'] == test_user.user_id
1504
        assert res[0]['public_name'] == test_user.display_name
1505
        assert res[0]['avatar_url'] is None
1506
1507
        assert res[1]['user_id'] == test_user2.user_id
1508
        assert res[1]['public_name'] == test_user2.display_name
1509
        assert res[1]['avatar_url'] is None
1510
1511 View Code Duplication
    def test_api__get_user__err_403__admin__too_small_acp(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1512
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1513
        admin = dbsession.query(models.User) \
1514
            .filter(models.User.email == '[email protected]') \
1515
            .one()
1516
        uapi = UserApi(
1517
            current_user=admin,
1518
            session=dbsession,
1519
            config=self.app_config,
1520
        )
1521
        gapi = GroupApi(
1522
            current_user=admin,
1523
            session=dbsession,
1524
            config=self.app_config,
1525
        )
1526
        groups = [gapi.get_one_with_name('users')]
1527
        test_user = uapi.create_user(
1528
            email='[email protected]',
1529
            password='pass',
1530
            name='bob',
1531
            groups=groups,
1532
            timezone='Europe/Paris',
1533
            lang='fr',
1534
            do_save=True,
1535
            do_notify=False,
1536
        )
1537
        test_user2 = uapi.create_user(
1538
            email='[email protected]',
1539
            password='pass',
1540
            name='bob2',
1541
            groups=groups,
1542
            timezone='Europe/Paris',
1543
            lang='fr',
1544
            do_save=True,
1545
            do_notify=False,
1546
        )
1547
        uapi.save(test_user)
1548
        transaction.commit()
1549
        user_id = int(admin.user_id)
1550
1551
        self.testapp.authorization = (
1552
            'Basic',
1553
            (
1554
                '[email protected]',
1555
                '[email protected]'
1556
            )
1557
        )
1558
        params = {
1559
            'acp': 't',
1560
        }
1561
        res = self.testapp.get(
1562
            '/api/v2/users/me/known_members',
1563
            status=400,
1564
            params=params
1565
        )
1566
        assert isinstance(res.json, dict)
1567
        assert 'code' in res.json.keys()
1568
        assert res.json_body['code'] == error.GENERIC_SCHEMA_VALIDATION_ERROR  # nopep8
1569
1570 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...
1571
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1572
        admin = dbsession.query(models.User) \
1573
            .filter(models.User.email == '[email protected]') \
1574
            .one()
1575
        uapi = UserApi(
1576
            current_user=admin,
1577
            session=dbsession,
1578
            config=self.app_config,
1579
        )
1580
        gapi = GroupApi(
1581
            current_user=admin,
1582
            session=dbsession,
1583
            config=self.app_config,
1584
        )
1585
        groups = [gapi.get_one_with_name('users')]
1586
        test_user = uapi.create_user(
1587
            email='[email protected]',
1588
            password='pass',
1589
            name='bob',
1590
            groups=groups,
1591
            timezone='Europe/Paris',
1592
            lang='fr',
1593
            do_save=True,
1594
            do_notify=False,
1595
        )
1596
        test_user2 = uapi.create_user(
1597
            email='[email protected]',
1598
            password='pass',
1599
            name='bob2',
1600
            groups=groups,
1601
            timezone='Europe/Paris',
1602
            lang='fr',
1603
            do_save=True,
1604
            do_notify=False,
1605
        )
1606
        test_user3 = uapi.create_user(
1607
            email='[email protected]',
1608
            password='pass',
1609
            name='bob3',
1610
            groups=groups,
1611
            timezone='Europe/Paris',
1612
            lang='fr',
1613
            do_save=True,
1614
            do_notify=False,
1615
        )
1616
        uapi.save(test_user)
1617
        uapi.save(test_user2)
1618
        uapi.save(test_user3)
1619
        workspace_api = WorkspaceApi(
1620
            current_user=admin,
1621
            session=dbsession,
1622
            config=self.app_config
1623
1624
        )
1625
        workspace = WorkspaceApi(
1626
            current_user=admin,
1627
            session=dbsession,
1628
            config=self.app_config,
1629
        ).create_workspace(
1630
            'test workspace',
1631
            save_now=True
1632
        )
1633
        role_api = RoleApi(
1634
            current_user=admin,
1635
            session=dbsession,
1636
            config=self.app_config,
1637
        )
1638
        role_api.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1639
        role_api.create_one(test_user2, workspace, UserRoleInWorkspace.READER, False)
1640
        transaction.commit()
1641
        user_id = int(test_user.user_id)
1642
1643
        self.testapp.authorization = (
1644
            'Basic',
1645
            (
1646
                '[email protected]',
1647
                'pass'
1648
            )
1649
        )
1650
        params = {
1651
            'acp': 'test',
1652
        }
1653
        res = self.testapp.get(
1654
            '/api/v2/users/me/known_members',
1655
            status=200,
1656
            params=params
1657
        )
1658
        res = res.json_body
1659
        assert len(res) == 2
1660
        assert res[0]['user_id'] == test_user.user_id
1661
        assert res[0]['public_name'] == test_user.display_name
1662
        assert res[0]['avatar_url'] is None
1663
1664
        assert res[1]['user_id'] == test_user2.user_id
1665
        assert res[1]['public_name'] == test_user2.display_name
1666
        assert res[1]['avatar_url'] is None
1667
1668
1669
class TestSetEmailEndpoint(FunctionalTest):
1670
    # -*- coding: utf-8 -*-
1671
    """
1672
    Tests for PUT /api/v2/users/me/email
1673
    """
1674
    fixtures = [BaseFixture]
1675
1676
    def test_api__set_account_email__err_400__admin_same_email(self):
1677
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1678
        admin = dbsession.query(models.User) \
1679
            .filter(models.User.email == '[email protected]') \
1680
            .one()
1681
        uapi = UserApi(
1682
            current_user=admin,
1683
            session=dbsession,
1684
            config=self.app_config,
1685
        )
1686
        gapi = GroupApi(
1687
            current_user=admin,
1688
            session=dbsession,
1689
            config=self.app_config,
1690
        )
1691
        groups = [gapi.get_one_with_name('users')]
1692
        test_user = uapi.create_user(
1693
            email='[email protected]',
1694
            password='pass',
1695
            name='bob',
1696
            groups=groups,
1697
            timezone='Europe/Paris',
1698
            lang='fr',
1699
            do_save=True,
1700
            do_notify=False,
1701
        )
1702
        uapi.save(test_user)
1703
        transaction.commit()
1704
        user_id = int(test_user.user_id)
1705
1706
        self.testapp.authorization = (
1707
            'Basic',
1708
            (
1709
                '[email protected]',
1710
                'pass',
1711
            )
1712
        )
1713
        # check before
1714
        res = self.testapp.get(
1715
            '/api/v2/users/me',
1716
            status=200
1717
        )
1718
        res = res.json_body
1719
        assert res['email'] == '[email protected]'
1720
1721
        # Set password
1722
        params = {
1723
            'email': '[email protected]',
1724
            'loggedin_user_password': 'pass',
1725
        }
1726
        res = self.testapp.put_json(
1727
            '/api/v2/users/me/email',
1728
            params=params,
1729
            status=400,
1730
        )
1731
        assert res.json_body
1732
        assert 'code' in res.json_body
1733
        assert res.json_body['code'] == error.EMAIL_ALREADY_EXIST_IN_DB
1734
        # Check After
1735
        res = self.testapp.get(
1736
            '/api/v2/users/me',
1737
            status=200
1738
        )
1739
        res = res.json_body
1740
        assert res['email'] == '[email protected]'
1741
1742
    def test_api__set_account_email__err_403__admin_wrong_password(self):
1743
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1744
        admin = dbsession.query(models.User) \
1745
            .filter(models.User.email == '[email protected]') \
1746
            .one()
1747
        uapi = UserApi(
1748
            current_user=admin,
1749
            session=dbsession,
1750
            config=self.app_config,
1751
        )
1752
        gapi = GroupApi(
1753
            current_user=admin,
1754
            session=dbsession,
1755
            config=self.app_config,
1756
        )
1757
        groups = [gapi.get_one_with_name('users')]
1758
        test_user = uapi.create_user(
1759
            email='[email protected]',
1760
            password='pass',
1761
            name='bob',
1762
            groups=groups,
1763
            timezone='Europe/Paris',
1764
            lang='fr',
1765
            do_save=True,
1766
            do_notify=False,
1767
        )
1768
        uapi.save(test_user)
1769
        transaction.commit()
1770
        user_id = int(test_user.user_id)
1771
1772
        self.testapp.authorization = (
1773
            'Basic',
1774
            (
1775
                '[email protected]',
1776
                'pass'
1777
            )
1778
        )
1779
        # check before
1780
        res = self.testapp.get(
1781
            '/api/v2/users/me',
1782
            status=200
1783
        )
1784
        res = res.json_body
1785
        assert res['email'] == '[email protected]'
1786
1787
        # Set password
1788
        params = {
1789
            'email': '[email protected]',
1790
            'loggedin_user_password': 'badpassword',
1791
        }
1792
        res = self.testapp.put_json(
1793
            '/api/v2/users/me/email',
1794
            params=params,
1795
            status=403,
1796
        )
1797
        assert res.json_body
1798
        assert 'code' in res.json_body
1799
        assert res.json_body['code'] == error.WRONG_USER_PASSWORD  # nopep8
1800
        # Check After
1801
        res = self.testapp.get(
1802
            '/api/v2/users/me',
1803
            status=200
1804
        )
1805
        res = res.json_body
1806
        assert res['email'] == '[email protected]'
1807
1808 View Code Duplication
    def test_api__set_account_email__ok_200__user_nominal(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1809
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1810
        admin = dbsession.query(models.User) \
1811
            .filter(models.User.email == '[email protected]') \
1812
            .one()
1813
        uapi = UserApi(
1814
            current_user=admin,
1815
            session=dbsession,
1816
            config=self.app_config,
1817
        )
1818
        gapi = GroupApi(
1819
            current_user=admin,
1820
            session=dbsession,
1821
            config=self.app_config,
1822
        )
1823
        groups = [gapi.get_one_with_name('users')]
1824
        test_user = uapi.create_user(
1825
            email='[email protected]',
1826
            password='pass',
1827
            name='bob',
1828
            groups=groups,
1829
            timezone='Europe/Paris',
1830
            lang='fr',
1831
            do_save=True,
1832
            do_notify=False,
1833
        )
1834
        uapi.save(test_user)
1835
        transaction.commit()
1836
        user_id = int(test_user.user_id)
1837
1838
        self.testapp.authorization = (
1839
            'Basic',
1840
            (
1841
                '[email protected]',
1842
                'pass'
1843
            )
1844
        )
1845
        # check before
1846
        res = self.testapp.get(
1847
            '/api/v2/users/me',
1848
            status=200
1849
        )
1850
        res = res.json_body
1851
        assert res['email'] == '[email protected]'
1852
1853
        # Set password
1854
        params = {
1855
            'email': '[email protected]',
1856
            'loggedin_user_password': 'pass',
1857
        }
1858
        self.testapp.put_json(
1859
            '/api/v2/users/me/email',
1860
            params=params,
1861
            status=200,
1862
        )
1863
        self.testapp.authorization = (
1864
            'Basic',
1865
            (
1866
                '[email protected]',
1867
                'pass'
1868
            )
1869
        )
1870
        # Check After
1871
        res = self.testapp.get(
1872
            '/api/v2/users/me',
1873
            status=200
1874
        )
1875
        res = res.json_body
1876
        assert res['email'] == '[email protected]'
1877
1878
1879
class TestSetPasswordEndpoint(FunctionalTest):
1880
    # -*- coding: utf-8 -*-
1881
    """
1882
    Tests for PUT /api/v2/users/me/password
1883
    """
1884
    fixtures = [BaseFixture]
1885
1886 View Code Duplication
    def test_api__set_account_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...
1887
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1888
        admin = dbsession.query(models.User) \
1889
            .filter(models.User.email == '[email protected]') \
1890
            .one()
1891
        uapi = UserApi(
1892
            current_user=admin,
1893
            session=dbsession,
1894
            config=self.app_config,
1895
        )
1896
        gapi = GroupApi(
1897
            current_user=admin,
1898
            session=dbsession,
1899
            config=self.app_config,
1900
        )
1901
        groups = [gapi.get_one_with_name('users')]
1902
        test_user = uapi.create_user(
1903
            email='[email protected]',
1904
            password='pass',
1905
            name='bob',
1906
            groups=groups,
1907
            timezone='Europe/Paris',
1908
            lang='fr',
1909
            do_save=True,
1910
            do_notify=False,
1911
        )
1912
        uapi.save(test_user)
1913
        transaction.commit()
1914
        user_id = int(test_user.user_id)
1915
1916
        self.testapp.authorization = (
1917
            'Basic',
1918
            (
1919
                '[email protected]',
1920
                '[email protected]'
1921
            )
1922
        )
1923
        # check before
1924
        user = uapi.get_one(user_id)
1925
        assert user.validate_password('pass')
1926
        assert not user.validate_password('mynewpassword')
1927
        # Set password
1928
        params = {
1929
            'new_password': 'mynewpassword',
1930
            'new_password2': 'mynewpassword',
1931
            'loggedin_user_password': 'wrongpassword',
1932
        }
1933
        res = self.testapp.put_json(
1934
            '/api/v2/users/me/password',
1935
            params=params,
1936
            status=403,
1937
        )
1938
        assert res.json_body
1939
        assert 'code' in res.json_body
1940
        assert res.json_body['code'] == error.WRONG_USER_PASSWORD
1941
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1942
        uapi = UserApi(
1943
            current_user=admin,
1944
            session=dbsession,
1945
            config=self.app_config,
1946
        )
1947
        # Check After
1948
        user = uapi.get_one(user_id)
1949
        assert user.validate_password('pass')
1950
        assert not user.validate_password('mynewpassword')
1951
1952
    def test_api__set_account_password__err_400__admin_passwords_do_not_match(self):  # nopep8
1953
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1954
        admin = dbsession.query(models.User) \
1955
            .filter(models.User.email == '[email protected]') \
1956
            .one()
1957
        uapi = UserApi(
1958
            current_user=admin,
1959
            session=dbsession,
1960
            config=self.app_config,
1961
        )
1962
        gapi = GroupApi(
1963
            current_user=admin,
1964
            session=dbsession,
1965
            config=self.app_config,
1966
        )
1967
        groups = [gapi.get_one_with_name('users')]
1968
        test_user = uapi.create_user(
1969
            email='[email protected]',
1970
            password='pass',
1971
            name='bob',
1972
            groups=groups,
1973
            timezone='Europe/Paris',
1974
            lang='fr',
1975
            do_save=True,
1976
            do_notify=False,
1977
        )
1978
        uapi.save(test_user)
1979
        transaction.commit()
1980
        user_id = int(test_user.user_id)
1981
1982
        self.testapp.authorization = (
1983
            'Basic',
1984
            (
1985
                '[email protected]',
1986
                '[email protected]'
1987
            )
1988
        )
1989
        # check before
1990
        user = uapi.get_one(user_id)
1991
        assert user.validate_password('pass')
1992
        assert not user.validate_password('mynewpassword')
1993
        assert not user.validate_password('mynewpassword2')
1994
        # Set password
1995
        params = {
1996
            'new_password': 'mynewpassword',
1997
            'new_password2': 'mynewpassword2',
1998
            'loggedin_user_password': '[email protected]',
1999
        }
2000
        res = self.testapp.put_json(
2001
            '/api/v2/users/me/password',
2002
            params=params,
2003
            status=400,
2004
        )
2005
        assert res.json_body
2006
        assert 'code' in res.json_body
2007
        assert res.json_body['code'] == error.PASSWORD_DO_NOT_MATCH
2008
        # Check After
2009
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2010
        uapi = UserApi(
2011
            current_user=admin,
2012
            session=dbsession,
2013
            config=self.app_config,
2014
        )
2015
        user = uapi.get_one(user_id)
2016
        assert user.validate_password('pass')
2017
        assert not user.validate_password('mynewpassword')
2018
        assert not user.validate_password('mynewpassword2')
2019
2020 View Code Duplication
    def test_api__set_account_password__ok_200__nominal(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2021
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2022
        admin = dbsession.query(models.User) \
2023
            .filter(models.User.email == '[email protected]') \
2024
            .one()
2025
        uapi = UserApi(
2026
            current_user=admin,
2027
            session=dbsession,
2028
            config=self.app_config,
2029
        )
2030
        gapi = GroupApi(
2031
            current_user=admin,
2032
            session=dbsession,
2033
            config=self.app_config,
2034
        )
2035
        groups = [gapi.get_one_with_name('users')]
2036
        test_user = uapi.create_user(
2037
            email='[email protected]',
2038
            password='pass',
2039
            name='bob',
2040
            groups=groups,
2041
            timezone='Europe/Paris',
2042
            lang='fr',
2043
            do_save=True,
2044
            do_notify=False,
2045
        )
2046
        uapi.save(test_user)
2047
        transaction.commit()
2048
        user_id = int(test_user.user_id)
2049
2050
        self.testapp.authorization = (
2051
            'Basic',
2052
            (
2053
                '[email protected]',
2054
                'pass'
2055
            )
2056
        )
2057
        # check before
2058
        user = uapi.get_one(user_id)
2059
        assert user.validate_password('pass')
2060
        assert not user.validate_password('mynewpassword')
2061
        # Set password
2062
        params = {
2063
            'new_password': 'mynewpassword',
2064
            'new_password2': 'mynewpassword',
2065
            'loggedin_user_password': 'pass',
2066
        }
2067
        self.testapp.put_json(
2068
            '/api/v2/users/me/password',
2069
            params=params,
2070
            status=204,
2071
        )
2072
        # Check After
2073
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2074
        uapi = UserApi(
2075
            current_user=admin,
2076
            session=dbsession,
2077
            config=self.app_config,
2078
        )
2079
        user = uapi.get_one(user_id)
2080
        assert not user.validate_password('pass')
2081
        assert user.validate_password('mynewpassword')
2082
2083
2084
2085
class TestSetUserInfoEndpoint(FunctionalTest):
2086
    # -*- coding: utf-8 -*-
2087
    """
2088
    Tests for PUT /api/v2/users/me
2089
    """
2090
    fixtures = [BaseFixture]
2091
2092 View Code Duplication
    def test_api__set_account_info__ok_200__nominal(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2093
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2094
        admin = dbsession.query(models.User) \
2095
            .filter(models.User.email == '[email protected]') \
2096
            .one()
2097
        uapi = UserApi(
2098
            current_user=admin,
2099
            session=dbsession,
2100
            config=self.app_config,
2101
        )
2102
        gapi = GroupApi(
2103
            current_user=admin,
2104
            session=dbsession,
2105
            config=self.app_config,
2106
        )
2107
        groups = [gapi.get_one_with_name('users')]
2108
        test_user = uapi.create_user(
2109
            email='[email protected]',
2110
            password='pass',
2111
            name='bob',
2112
            groups=groups,
2113
            timezone='Europe/Paris',
2114
            lang='fr',
2115
            do_save=True,
2116
            do_notify=False,
2117
        )
2118
        uapi.save(test_user)
2119
        transaction.commit()
2120
        user_id = int(test_user.user_id)
2121
2122
        self.testapp.authorization = (
2123
            'Basic',
2124
            (
2125
                '[email protected]',
2126
                'pass',
2127
            )
2128
        )
2129
        # check before
2130
        res = self.testapp.get(
2131
            '/api/v2/users/me',
2132
            status=200
2133
        )
2134
        res = res.json_body
2135
        assert res['user_id'] == user_id
2136
        assert res['public_name'] == 'bob'
2137
        assert res['timezone'] == 'Europe/Paris'
2138
        assert res['lang'] == 'fr'
2139
        # Set params
2140
        params = {
2141
            'public_name': 'updated',
2142
            'timezone': 'Europe/London',
2143
            'lang': 'en',
2144
        }
2145
        self.testapp.put_json(
2146
            '/api/v2/users/me',
2147
            params=params,
2148
            status=200,
2149
        )
2150
        # Check After
2151
        res = self.testapp.get(
2152
            '/api/v2/users/me',
2153
            status=200
2154
        )
2155
        res = res.json_body
2156
        assert res['user_id'] == user_id
2157
        assert res['public_name'] == 'updated'
2158
        assert res['timezone'] == 'Europe/London'
2159
        assert res['lang'] == 'en'
2160