Passed
Pull Request — develop (#93)
by
unknown
01:30
created

backend.tracim_backend.tests.functional.test_account   B

Complexity

Total Complexity 33

Size/Duplication

Total Lines 1900
Duplicated Lines 84 %

Importance

Changes 0
Metric Value
wmc 33
eloc 1467
dl 1596
loc 1900
rs 8.56
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
B TestAccountDisableWorkspaceNotification.test_api_enable_account_workspace_notification__ok__200__nominal() 69 69 1
B TestAccountKnownMembersEndpoint.test_api__get_user__ok_200__normal_user_by_email() 97 97 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 TestSetPasswordEndpoint.test_api__set_account_password__err_403__admin_wrong_password() 65 65 1
B TestAccountRecentlyActiveContentEndpoint.test_api__get_recently_active_content__ok__200__limit_2_multiple() 111 111 4
B TestSetEmailEndpoint.test_api__set_account_email__ok_200__user_nominal() 69 69 1
B TestSetUserInfoEndpoint.test_api__set_account_info__ok_200__nominal() 68 68 1
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_email() 65 65 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 TestSetPasswordEndpoint.test_api__set_account_password__err_400__admin_passwords_do_not_match() 0 67 1
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 TestSetEmailEndpoint.test_api__set_account_email__err_403__admin_wrong_password() 0 65 1
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__deactivated_members() 62 62 1
B TestSetPasswordEndpoint.test_api__set_account_password__ok_200__nominal() 62 62 1
B TestAccountKnownMembersEndpoint.test_api__get_user__err_403__admin__too_small_acp() 58 58 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 View Code Duplication
class TestAccountKnownMembersEndpoint(FunctionalTest):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1050
    # -*- coding: utf-8 -*-
1051
    """
1052
    Tests for GET /api/v2/users/me
1053
    """
1054
    fixtures = [BaseFixture]
1055
1056
    def test_api__get_user__ok_200__admin__by_name(self):
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'.format(user_id=user_id),
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
    def test_api__get_user__ok_200__admin__by_name__deactivated_members(self):
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
        test_user2.is_active = False
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
        }
1174
        res = self.testapp.get(
1175
            '/api/v2/users/me/known_members'.format(user_id=user_id),
1176
            status=200,
1177
            params=params,
1178
        )
1179
        res = res.json_body
1180
        assert len(res) == 1
1181
        assert res[0]['user_id'] == test_user.user_id
1182
        assert res[0]['public_name'] == test_user.display_name
1183
        assert res[0]['avatar_url'] is None
1184
1185
    def test_api__get_user__ok_200__admin__by_email(self):
1186
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1187
        admin = dbsession.query(models.User) \
1188
            .filter(models.User.email == '[email protected]') \
1189
            .one()
1190
        uapi = UserApi(
1191
            current_user=admin,
1192
            session=dbsession,
1193
            config=self.app_config,
1194
        )
1195
        gapi = GroupApi(
1196
            current_user=admin,
1197
            session=dbsession,
1198
            config=self.app_config,
1199
        )
1200
        groups = [gapi.get_one_with_name('users')]
1201
        test_user = uapi.create_user(
1202
            email='[email protected]',
1203
            password='pass',
1204
            name='bob',
1205
            groups=groups,
1206
            timezone='Europe/Paris',
1207
            lang='fr',
1208
            do_save=True,
1209
            do_notify=False,
1210
        )
1211
        test_user2 = uapi.create_user(
1212
            email='[email protected]',
1213
            password='pass',
1214
            name='bob2',
1215
            groups=groups,
1216
            timezone='Europe/Paris',
1217
            lang='fr',
1218
            do_save=True,
1219
            do_notify=False,
1220
        )
1221
        uapi.save(test_user)
1222
        uapi.save(test_user2)
1223
        transaction.commit()
1224
        user_id = int(admin.user_id)
1225
1226
        self.testapp.authorization = (
1227
            'Basic',
1228
            (
1229
                '[email protected]',
1230
                '[email protected]'
1231
            )
1232
        )
1233
        params = {
1234
            'acp': 'test',
1235
        }
1236
        res = self.testapp.get(
1237
            '/api/v2/users/me/known_members'.format(user_id=user_id),
1238
            status=200,
1239
            params=params,
1240
        )
1241
        res = res.json_body
1242
        assert len(res) == 2
1243
        assert res[0]['user_id'] == test_user.user_id
1244
        assert res[0]['public_name'] == test_user.display_name
1245
        assert res[0]['avatar_url'] is None
1246
1247
        assert res[1]['user_id'] == test_user2.user_id
1248
        assert res[1]['public_name'] == test_user2.display_name
1249
        assert res[1]['avatar_url'] is None
1250
1251
    def test_api__get_user__err_403__admin__too_small_acp(self):
1252
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1253
        admin = dbsession.query(models.User) \
1254
            .filter(models.User.email == '[email protected]') \
1255
            .one()
1256
        uapi = UserApi(
1257
            current_user=admin,
1258
            session=dbsession,
1259
            config=self.app_config,
1260
        )
1261
        gapi = GroupApi(
1262
            current_user=admin,
1263
            session=dbsession,
1264
            config=self.app_config,
1265
        )
1266
        groups = [gapi.get_one_with_name('users')]
1267
        test_user = uapi.create_user(
1268
            email='[email protected]',
1269
            password='pass',
1270
            name='bob',
1271
            groups=groups,
1272
            timezone='Europe/Paris',
1273
            lang='fr',
1274
            do_save=True,
1275
            do_notify=False,
1276
        )
1277
        test_user2 = uapi.create_user(
1278
            email='[email protected]',
1279
            password='pass',
1280
            name='bob2',
1281
            groups=groups,
1282
            timezone='Europe/Paris',
1283
            lang='fr',
1284
            do_save=True,
1285
            do_notify=False,
1286
        )
1287
        uapi.save(test_user)
1288
        transaction.commit()
1289
        user_id = int(admin.user_id)
1290
1291
        self.testapp.authorization = (
1292
            'Basic',
1293
            (
1294
                '[email protected]',
1295
                '[email protected]'
1296
            )
1297
        )
1298
        params = {
1299
            'acp': 't',
1300
        }
1301
        res = self.testapp.get(
1302
            '/api/v2/users/me/known_members'.format(user_id=user_id),
1303
            status=400,
1304
            params=params
1305
        )
1306
        assert isinstance(res.json, dict)
1307
        assert 'code' in res.json.keys()
1308
        assert res.json_body['code'] == error.GENERIC_SCHEMA_VALIDATION_ERROR  # nopep8
1309
1310
    def test_api__get_user__ok_200__normal_user_by_email(self):
1311
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1312
        admin = dbsession.query(models.User) \
1313
            .filter(models.User.email == '[email protected]') \
1314
            .one()
1315
        uapi = UserApi(
1316
            current_user=admin,
1317
            session=dbsession,
1318
            config=self.app_config,
1319
        )
1320
        gapi = GroupApi(
1321
            current_user=admin,
1322
            session=dbsession,
1323
            config=self.app_config,
1324
        )
1325
        groups = [gapi.get_one_with_name('users')]
1326
        test_user = uapi.create_user(
1327
            email='[email protected]',
1328
            password='pass',
1329
            name='bob',
1330
            groups=groups,
1331
            timezone='Europe/Paris',
1332
            lang='fr',
1333
            do_save=True,
1334
            do_notify=False,
1335
        )
1336
        test_user2 = uapi.create_user(
1337
            email='[email protected]',
1338
            password='pass',
1339
            name='bob2',
1340
            groups=groups,
1341
            timezone='Europe/Paris',
1342
            lang='fr',
1343
            do_save=True,
1344
            do_notify=False,
1345
        )
1346
        test_user3 = uapi.create_user(
1347
            email='[email protected]',
1348
            password='pass',
1349
            name='bob3',
1350
            groups=groups,
1351
            timezone='Europe/Paris',
1352
            lang='fr',
1353
            do_save=True,
1354
            do_notify=False,
1355
        )
1356
        uapi.save(test_user)
1357
        uapi.save(test_user2)
1358
        uapi.save(test_user3)
1359
        workspace_api = WorkspaceApi(
1360
            current_user=admin,
1361
            session=dbsession,
1362
            config=self.app_config
1363
1364
        )
1365
        workspace = WorkspaceApi(
1366
            current_user=admin,
1367
            session=dbsession,
1368
            config=self.app_config,
1369
        ).create_workspace(
1370
            'test workspace',
1371
            save_now=True
1372
        )
1373
        role_api = RoleApi(
1374
            current_user=admin,
1375
            session=dbsession,
1376
            config=self.app_config,
1377
        )
1378
        role_api.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1379
        role_api.create_one(test_user2, workspace, UserRoleInWorkspace.READER, False)
1380
        transaction.commit()
1381
        user_id = int(test_user.user_id)
1382
1383
        self.testapp.authorization = (
1384
            'Basic',
1385
            (
1386
                '[email protected]',
1387
                'pass'
1388
            )
1389
        )
1390
        params = {
1391
            'acp': 'test',
1392
        }
1393
        res = self.testapp.get(
1394
            '/api/v2/users/me/known_members'.format(user_id=user_id),
1395
            status=200,
1396
            params=params
1397
        )
1398
        res = res.json_body
1399
        assert len(res) == 2
1400
        assert res[0]['user_id'] == test_user.user_id
1401
        assert res[0]['public_name'] == test_user.display_name
1402
        assert res[0]['avatar_url'] is None
1403
1404
        assert res[1]['user_id'] == test_user2.user_id
1405
        assert res[1]['public_name'] == test_user2.display_name
1406
        assert res[1]['avatar_url'] is None
1407
1408
1409
class TestSetEmailEndpoint(FunctionalTest):
1410
    # -*- coding: utf-8 -*-
1411
    """
1412
    Tests for PUT /api/v2/users/me/email
1413
    """
1414
    fixtures = [BaseFixture]
1415
1416
    def test_api__set_account_email__err_400__admin_same_email(self):
1417
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1418
        admin = dbsession.query(models.User) \
1419
            .filter(models.User.email == '[email protected]') \
1420
            .one()
1421
        uapi = UserApi(
1422
            current_user=admin,
1423
            session=dbsession,
1424
            config=self.app_config,
1425
        )
1426
        gapi = GroupApi(
1427
            current_user=admin,
1428
            session=dbsession,
1429
            config=self.app_config,
1430
        )
1431
        groups = [gapi.get_one_with_name('users')]
1432
        test_user = uapi.create_user(
1433
            email='[email protected]',
1434
            password='pass',
1435
            name='bob',
1436
            groups=groups,
1437
            timezone='Europe/Paris',
1438
            lang='fr',
1439
            do_save=True,
1440
            do_notify=False,
1441
        )
1442
        uapi.save(test_user)
1443
        transaction.commit()
1444
        user_id = int(test_user.user_id)
1445
1446
        self.testapp.authorization = (
1447
            'Basic',
1448
            (
1449
                '[email protected]',
1450
                'pass',
1451
            )
1452
        )
1453
        # check before
1454
        res = self.testapp.get(
1455
            '/api/v2/users/me',
1456
            status=200
1457
        )
1458
        res = res.json_body
1459
        assert res['email'] == '[email protected]'
1460
1461
        # Set password
1462
        params = {
1463
            'email': '[email protected]',
1464
            'loggedin_user_password': 'pass',
1465
        }
1466
        res = self.testapp.put_json(
1467
            '/api/v2/users/me/email',
1468
            params=params,
1469
            status=400,
1470
        )
1471
        assert res.json_body
1472
        assert 'code' in res.json_body
1473
        assert res.json_body['code'] == error.EMAIL_ALREADY_EXIST_IN_DB
1474
        # Check After
1475
        res = self.testapp.get(
1476
            '/api/v2/users/me',
1477
            status=200
1478
        )
1479
        res = res.json_body
1480
        assert res['email'] == '[email protected]'
1481
1482
    def test_api__set_account_email__err_403__admin_wrong_password(self):
1483
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1484
        admin = dbsession.query(models.User) \
1485
            .filter(models.User.email == '[email protected]') \
1486
            .one()
1487
        uapi = UserApi(
1488
            current_user=admin,
1489
            session=dbsession,
1490
            config=self.app_config,
1491
        )
1492
        gapi = GroupApi(
1493
            current_user=admin,
1494
            session=dbsession,
1495
            config=self.app_config,
1496
        )
1497
        groups = [gapi.get_one_with_name('users')]
1498
        test_user = uapi.create_user(
1499
            email='[email protected]',
1500
            password='pass',
1501
            name='bob',
1502
            groups=groups,
1503
            timezone='Europe/Paris',
1504
            lang='fr',
1505
            do_save=True,
1506
            do_notify=False,
1507
        )
1508
        uapi.save(test_user)
1509
        transaction.commit()
1510
        user_id = int(test_user.user_id)
1511
1512
        self.testapp.authorization = (
1513
            'Basic',
1514
            (
1515
                '[email protected]',
1516
                'pass'
1517
            )
1518
        )
1519
        # check before
1520
        res = self.testapp.get(
1521
            '/api/v2/users/me',
1522
            status=200
1523
        )
1524
        res = res.json_body
1525
        assert res['email'] == '[email protected]'
1526
1527
        # Set password
1528
        params = {
1529
            'email': '[email protected]',
1530
            'loggedin_user_password': 'badpassword',
1531
        }
1532
        res = self.testapp.put_json(
1533
            '/api/v2/users/me/email',
1534
            params=params,
1535
            status=403,
1536
        )
1537
        assert res.json_body
1538
        assert 'code' in res.json_body
1539
        assert res.json_body['code'] == error.WRONG_USER_PASSWORD  # nopep8
1540
        # Check After
1541
        res = self.testapp.get(
1542
            '/api/v2/users/me',
1543
            status=200
1544
        )
1545
        res = res.json_body
1546
        assert res['email'] == '[email protected]'
1547
1548 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...
1549
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1550
        admin = dbsession.query(models.User) \
1551
            .filter(models.User.email == '[email protected]') \
1552
            .one()
1553
        uapi = UserApi(
1554
            current_user=admin,
1555
            session=dbsession,
1556
            config=self.app_config,
1557
        )
1558
        gapi = GroupApi(
1559
            current_user=admin,
1560
            session=dbsession,
1561
            config=self.app_config,
1562
        )
1563
        groups = [gapi.get_one_with_name('users')]
1564
        test_user = uapi.create_user(
1565
            email='[email protected]',
1566
            password='pass',
1567
            name='bob',
1568
            groups=groups,
1569
            timezone='Europe/Paris',
1570
            lang='fr',
1571
            do_save=True,
1572
            do_notify=False,
1573
        )
1574
        uapi.save(test_user)
1575
        transaction.commit()
1576
        user_id = int(test_user.user_id)
1577
1578
        self.testapp.authorization = (
1579
            'Basic',
1580
            (
1581
                '[email protected]',
1582
                'pass'
1583
            )
1584
        )
1585
        # check before
1586
        res = self.testapp.get(
1587
            '/api/v2/users/me',
1588
            status=200
1589
        )
1590
        res = res.json_body
1591
        assert res['email'] == '[email protected]'
1592
1593
        # Set password
1594
        params = {
1595
            'email': '[email protected]',
1596
            'loggedin_user_password': 'pass',
1597
        }
1598
        self.testapp.put_json(
1599
            '/api/v2/users/me/email',
1600
            params=params,
1601
            status=200,
1602
        )
1603
        self.testapp.authorization = (
1604
            'Basic',
1605
            (
1606
                '[email protected]',
1607
                'pass'
1608
            )
1609
        )
1610
        # Check After
1611
        res = self.testapp.get(
1612
            '/api/v2/users/me',
1613
            status=200
1614
        )
1615
        res = res.json_body
1616
        assert res['email'] == '[email protected]'
1617
1618
1619
class TestSetPasswordEndpoint(FunctionalTest):
1620
    # -*- coding: utf-8 -*-
1621
    """
1622
    Tests for PUT /api/v2/users/me/password
1623
    """
1624
    fixtures = [BaseFixture]
1625
1626 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...
1627
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1628
        admin = dbsession.query(models.User) \
1629
            .filter(models.User.email == '[email protected]') \
1630
            .one()
1631
        uapi = UserApi(
1632
            current_user=admin,
1633
            session=dbsession,
1634
            config=self.app_config,
1635
        )
1636
        gapi = GroupApi(
1637
            current_user=admin,
1638
            session=dbsession,
1639
            config=self.app_config,
1640
        )
1641
        groups = [gapi.get_one_with_name('users')]
1642
        test_user = uapi.create_user(
1643
            email='[email protected]',
1644
            password='pass',
1645
            name='bob',
1646
            groups=groups,
1647
            timezone='Europe/Paris',
1648
            lang='fr',
1649
            do_save=True,
1650
            do_notify=False,
1651
        )
1652
        uapi.save(test_user)
1653
        transaction.commit()
1654
        user_id = int(test_user.user_id)
1655
1656
        self.testapp.authorization = (
1657
            'Basic',
1658
            (
1659
                '[email protected]',
1660
                '[email protected]'
1661
            )
1662
        )
1663
        # check before
1664
        user = uapi.get_one(user_id)
1665
        assert user.validate_password('pass')
1666
        assert not user.validate_password('mynewpassword')
1667
        # Set password
1668
        params = {
1669
            'new_password': 'mynewpassword',
1670
            'new_password2': 'mynewpassword',
1671
            'loggedin_user_password': 'wrongpassword',
1672
        }
1673
        res = self.testapp.put_json(
1674
            '/api/v2/users/me/password',
1675
            params=params,
1676
            status=403,
1677
        )
1678
        assert res.json_body
1679
        assert 'code' in res.json_body
1680
        assert res.json_body['code'] == error.WRONG_USER_PASSWORD
1681
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1682
        uapi = UserApi(
1683
            current_user=admin,
1684
            session=dbsession,
1685
            config=self.app_config,
1686
        )
1687
        # Check After
1688
        user = uapi.get_one(user_id)
1689
        assert user.validate_password('pass')
1690
        assert not user.validate_password('mynewpassword')
1691
1692
    def test_api__set_account_password__err_400__admin_passwords_do_not_match(self):  # nopep8
1693
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1694
        admin = dbsession.query(models.User) \
1695
            .filter(models.User.email == '[email protected]') \
1696
            .one()
1697
        uapi = UserApi(
1698
            current_user=admin,
1699
            session=dbsession,
1700
            config=self.app_config,
1701
        )
1702
        gapi = GroupApi(
1703
            current_user=admin,
1704
            session=dbsession,
1705
            config=self.app_config,
1706
        )
1707
        groups = [gapi.get_one_with_name('users')]
1708
        test_user = uapi.create_user(
1709
            email='[email protected]',
1710
            password='pass',
1711
            name='bob',
1712
            groups=groups,
1713
            timezone='Europe/Paris',
1714
            lang='fr',
1715
            do_save=True,
1716
            do_notify=False,
1717
        )
1718
        uapi.save(test_user)
1719
        transaction.commit()
1720
        user_id = int(test_user.user_id)
1721
1722
        self.testapp.authorization = (
1723
            'Basic',
1724
            (
1725
                '[email protected]',
1726
                '[email protected]'
1727
            )
1728
        )
1729
        # check before
1730
        user = uapi.get_one(user_id)
1731
        assert user.validate_password('pass')
1732
        assert not user.validate_password('mynewpassword')
1733
        assert not user.validate_password('mynewpassword2')
1734
        # Set password
1735
        params = {
1736
            'new_password': 'mynewpassword',
1737
            'new_password2': 'mynewpassword2',
1738
            'loggedin_user_password': '[email protected]',
1739
        }
1740
        res = self.testapp.put_json(
1741
            '/api/v2/users/me/password',
1742
            params=params,
1743
            status=400,
1744
        )
1745
        assert res.json_body
1746
        assert 'code' in res.json_body
1747
        assert res.json_body['code'] == error.PASSWORD_DO_NOT_MATCH
1748
        # Check After
1749
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1750
        uapi = UserApi(
1751
            current_user=admin,
1752
            session=dbsession,
1753
            config=self.app_config,
1754
        )
1755
        user = uapi.get_one(user_id)
1756
        assert user.validate_password('pass')
1757
        assert not user.validate_password('mynewpassword')
1758
        assert not user.validate_password('mynewpassword2')
1759
1760 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...
1761
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1762
        admin = dbsession.query(models.User) \
1763
            .filter(models.User.email == '[email protected]') \
1764
            .one()
1765
        uapi = UserApi(
1766
            current_user=admin,
1767
            session=dbsession,
1768
            config=self.app_config,
1769
        )
1770
        gapi = GroupApi(
1771
            current_user=admin,
1772
            session=dbsession,
1773
            config=self.app_config,
1774
        )
1775
        groups = [gapi.get_one_with_name('users')]
1776
        test_user = uapi.create_user(
1777
            email='[email protected]',
1778
            password='pass',
1779
            name='bob',
1780
            groups=groups,
1781
            timezone='Europe/Paris',
1782
            lang='fr',
1783
            do_save=True,
1784
            do_notify=False,
1785
        )
1786
        uapi.save(test_user)
1787
        transaction.commit()
1788
        user_id = int(test_user.user_id)
1789
1790
        self.testapp.authorization = (
1791
            'Basic',
1792
            (
1793
                '[email protected]',
1794
                'pass'
1795
            )
1796
        )
1797
        # check before
1798
        user = uapi.get_one(user_id)
1799
        assert user.validate_password('pass')
1800
        assert not user.validate_password('mynewpassword')
1801
        # Set password
1802
        params = {
1803
            'new_password': 'mynewpassword',
1804
            'new_password2': 'mynewpassword',
1805
            'loggedin_user_password': 'pass',
1806
        }
1807
        self.testapp.put_json(
1808
            '/api/v2/users/me/password',
1809
            params=params,
1810
            status=204,
1811
        )
1812
        # Check After
1813
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1814
        uapi = UserApi(
1815
            current_user=admin,
1816
            session=dbsession,
1817
            config=self.app_config,
1818
        )
1819
        user = uapi.get_one(user_id)
1820
        assert not user.validate_password('pass')
1821
        assert user.validate_password('mynewpassword')
1822
1823
1824
1825
class TestSetUserInfoEndpoint(FunctionalTest):
1826
    # -*- coding: utf-8 -*-
1827
    """
1828
    Tests for PUT /api/v2/users/me
1829
    """
1830
    fixtures = [BaseFixture]
1831
1832 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...
1833
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1834
        admin = dbsession.query(models.User) \
1835
            .filter(models.User.email == '[email protected]') \
1836
            .one()
1837
        uapi = UserApi(
1838
            current_user=admin,
1839
            session=dbsession,
1840
            config=self.app_config,
1841
        )
1842
        gapi = GroupApi(
1843
            current_user=admin,
1844
            session=dbsession,
1845
            config=self.app_config,
1846
        )
1847
        groups = [gapi.get_one_with_name('users')]
1848
        test_user = uapi.create_user(
1849
            email='[email protected]',
1850
            password='pass',
1851
            name='bob',
1852
            groups=groups,
1853
            timezone='Europe/Paris',
1854
            lang='fr',
1855
            do_save=True,
1856
            do_notify=False,
1857
        )
1858
        uapi.save(test_user)
1859
        transaction.commit()
1860
        user_id = int(test_user.user_id)
1861
1862
        self.testapp.authorization = (
1863
            'Basic',
1864
            (
1865
                '[email protected]',
1866
                'pass',
1867
            )
1868
        )
1869
        # check before
1870
        res = self.testapp.get(
1871
            '/api/v2/users/me',
1872
            status=200
1873
        )
1874
        res = res.json_body
1875
        assert res['user_id'] == user_id
1876
        assert res['public_name'] == 'bob'
1877
        assert res['timezone'] == 'Europe/Paris'
1878
        assert res['lang'] == 'fr'
1879
        # Set params
1880
        params = {
1881
            'public_name': 'updated',
1882
            'timezone': 'Europe/London',
1883
            'lang': 'en',
1884
        }
1885
        self.testapp.put_json(
1886
            '/api/v2/users/me',
1887
            params=params,
1888
            status=200,
1889
        )
1890
        # Check After
1891
        res = self.testapp.get(
1892
            '/api/v2/users/me',
1893
            status=200
1894
        )
1895
        res = res.json_body
1896
        assert res['user_id'] == user_id
1897
        assert res['public_name'] == 'updated'
1898
        assert res['timezone'] == 'Europe/London'
1899
        assert res['lang'] == 'en'
1900