Code Duplication    Length = 80-81 lines in 7 locations

backend/tracim_backend/tests/functional/test_user.py 7 locations

@@ 2301-2381 (lines=81) @@
2298
        assert res.json_body[1]['content_id'] == main_folder.content_id
2299
        assert res.json_body[1]['read_by_user'] is True
2300
2301
    def test_api_set_content_as_read__err__403__other_user(self):
2302
        # init DB
2303
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2304
        admin = dbsession.query(models.User) \
2305
            .filter(models.User.email == '[email protected]') \
2306
            .one()
2307
        workspace_api = WorkspaceApi(
2308
            current_user=admin,
2309
            session=dbsession,
2310
            config=self.app_config
2311
2312
        )
2313
        workspace = WorkspaceApi(
2314
            current_user=admin,
2315
            session=dbsession,
2316
            config=self.app_config,
2317
        ).create_workspace(
2318
            'test workspace',
2319
            save_now=True
2320
        )
2321
        uapi = UserApi(
2322
            current_user=admin,
2323
            session=dbsession,
2324
            config=self.app_config,
2325
        )
2326
        gapi = GroupApi(
2327
            current_user=admin,
2328
            session=dbsession,
2329
            config=self.app_config,
2330
        )
2331
        groups = [gapi.get_one_with_name('users')]
2332
        test_user = uapi.create_user(
2333
            email='[email protected]',
2334
            password='pass',
2335
            name='bob',
2336
            groups=groups,
2337
            timezone='Europe/Paris',
2338
            lang='fr',
2339
            do_save=True,
2340
            do_notify=False,
2341
        )
2342
        rapi = RoleApi(
2343
            current_user=admin,
2344
            session=dbsession,
2345
            config=self.app_config,
2346
        )
2347
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
2348
        api = ContentApi(
2349
            current_user=admin,
2350
            session=dbsession,
2351
            config=self.app_config,
2352
        )
2353
        api2 = ContentApi(
2354
            current_user=test_user,
2355
            session=dbsession,
2356
            config=self.app_config,
2357
        )
2358
        main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
2359
        # creation order test
2360
        firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
2361
        api.mark_unread(main_folder)
2362
        api.mark_unread(firstly_created)
2363
        api2.mark_unread(main_folder)
2364
        api2.mark_unread(firstly_created)
2365
        dbsession.flush()
2366
        transaction.commit()
2367
2368
        self.testapp.authorization = (
2369
            'Basic',
2370
            (
2371
                '[email protected]',
2372
                'pass'
2373
            )
2374
        )
2375
        self.testapp.put(
2376
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/read'.format(  # nopep8
2377
                workspace_id=workspace.workspace_id,
2378
                content_id=firstly_created.content_id,
2379
                user_id=admin.user_id,
2380
            ),
2381
            status=403,
2382
        )
2383
2384
@@ 1960-2040 (lines=81) @@
1957
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1958
        assert res.json_body[0]['read_by_user'] is False
1959
1960
    def test_api_set_content_as_unread__err__403__other_user(self):
1961
        # init DB
1962
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1963
        admin = dbsession.query(models.User) \
1964
            .filter(models.User.email == '[email protected]') \
1965
            .one()
1966
        workspace_api = WorkspaceApi(
1967
            current_user=admin,
1968
            session=dbsession,
1969
            config=self.app_config
1970
1971
        )
1972
        workspace = WorkspaceApi(
1973
            current_user=admin,
1974
            session=dbsession,
1975
            config=self.app_config,
1976
        ).create_workspace(
1977
            'test workspace',
1978
            save_now=True
1979
        )
1980
        uapi = UserApi(
1981
            current_user=admin,
1982
            session=dbsession,
1983
            config=self.app_config,
1984
        )
1985
        gapi = GroupApi(
1986
            current_user=admin,
1987
            session=dbsession,
1988
            config=self.app_config,
1989
        )
1990
        groups = [gapi.get_one_with_name('users')]
1991
        test_user = uapi.create_user(
1992
            email='[email protected]',
1993
            password='pass',
1994
            name='bob',
1995
            groups=groups,
1996
            timezone='Europe/Paris',
1997
            lang='fr',
1998
            do_save=True,
1999
            do_notify=False,
2000
        )
2001
        rapi = RoleApi(
2002
            current_user=admin,
2003
            session=dbsession,
2004
            config=self.app_config,
2005
        )
2006
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
2007
        api = ContentApi(
2008
            current_user=admin,
2009
            session=dbsession,
2010
            config=self.app_config,
2011
        )
2012
        api2 = ContentApi(
2013
            current_user=test_user,
2014
            session=dbsession,
2015
            config=self.app_config,
2016
        )
2017
        main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
2018
        # creation order test
2019
        firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
2020
        api.mark_read(firstly_created)
2021
        api2.mark_read(firstly_created)
2022
        dbsession.flush()
2023
        transaction.commit()
2024
2025
        self.testapp.authorization = (
2026
            'Basic',
2027
            (
2028
                '[email protected]',
2029
                'pass'
2030
            )
2031
        )
2032
2033
        # unread
2034
        self.testapp.put(
2035
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format(  # nopep8
2036
                workspace_id=workspace.workspace_id,
2037
                content_id=firstly_created.content_id,
2038
                user_id=admin.user_id,
2039
            ),
2040
            status=403,
2041
        )
2042
2043
    def test_api_set_content_as_unread__ok__200__with_comments(self):
@@ 1781-1861 (lines=81) @@
1778
            status=400,
1779
        )
1780
1781
    def test_api_set_content_as_unread__err__400__admin_content_do_not_exist(self):
1782
        # init DB
1783
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1784
        admin = dbsession.query(models.User) \
1785
            .filter(models.User.email == '[email protected]') \
1786
            .one()
1787
        workspace_api = WorkspaceApi(
1788
            current_user=admin,
1789
            session=dbsession,
1790
            config=self.app_config
1791
1792
        )
1793
        workspace = WorkspaceApi(
1794
            current_user=admin,
1795
            session=dbsession,
1796
            config=self.app_config,
1797
        ).create_workspace(
1798
            'test workspace',
1799
            save_now=True
1800
        )
1801
        uapi = UserApi(
1802
            current_user=admin,
1803
            session=dbsession,
1804
            config=self.app_config,
1805
        )
1806
        gapi = GroupApi(
1807
            current_user=admin,
1808
            session=dbsession,
1809
            config=self.app_config,
1810
        )
1811
        groups = [gapi.get_one_with_name('users')]
1812
        test_user = uapi.create_user(
1813
            email='[email protected]',
1814
            password='pass',
1815
            name='bob',
1816
            groups=groups,
1817
            timezone='Europe/Paris',
1818
            lang='fr',
1819
            do_save=True,
1820
            do_notify=False,
1821
        )
1822
        rapi = RoleApi(
1823
            current_user=admin,
1824
            session=dbsession,
1825
            config=self.app_config,
1826
        )
1827
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1828
        api = ContentApi(
1829
            current_user=admin,
1830
            session=dbsession,
1831
            config=self.app_config,
1832
        )
1833
        api2 = ContentApi(
1834
            current_user=test_user,
1835
            session=dbsession,
1836
            config=self.app_config,
1837
        )
1838
        main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
1839
        # creation order test
1840
        firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
1841
        api.mark_read(firstly_created)
1842
        api2.mark_read(firstly_created)
1843
        dbsession.flush()
1844
        transaction.commit()
1845
1846
        self.testapp.authorization = (
1847
            'Basic',
1848
            (
1849
                '[email protected]',
1850
                '[email protected]'
1851
            )
1852
        )
1853
1854
        # unread
1855
        self.testapp.put(
1856
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format(  # nopep8
1857
                workspace_id=workspace.workspace_id,
1858
                content_id=4000,
1859
                user_id=test_user.user_id,
1860
            ),
1861
            status=400,
1862
        )
1863
1864
    def test_api_set_content_as_unread__ok__200__user_itself(self):
@@ 1699-1778 (lines=80) @@
1696
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1697
        assert res.json_body[0]['read_by_user'] is True
1698
1699
    def test_api_set_content_as_unread__err__400__admin_workspace_do_not_exist(self):
1700
        # init DB
1701
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1702
        admin = dbsession.query(models.User) \
1703
            .filter(models.User.email == '[email protected]') \
1704
            .one()
1705
        workspace_api = WorkspaceApi(
1706
            current_user=admin,
1707
            session=dbsession,
1708
            config=self.app_config
1709
1710
        )
1711
        workspace = WorkspaceApi(
1712
            current_user=admin,
1713
            session=dbsession,
1714
            config=self.app_config,
1715
        ).create_workspace(
1716
            'test workspace',
1717
            save_now=True
1718
        )
1719
        uapi = UserApi(
1720
            current_user=admin,
1721
            session=dbsession,
1722
            config=self.app_config,
1723
        )
1724
        gapi = GroupApi(
1725
            current_user=admin,
1726
            session=dbsession,
1727
            config=self.app_config,
1728
        )
1729
        groups = [gapi.get_one_with_name('users')]
1730
        test_user = uapi.create_user(
1731
            email='[email protected]',
1732
            password='pass',
1733
            name='bob',
1734
            groups=groups,
1735
            timezone='Europe/Paris',
1736
            lang='fr',
1737
            do_save=True,
1738
            do_notify=False,
1739
        )
1740
        rapi = RoleApi(
1741
            current_user=admin,
1742
            session=dbsession,
1743
            config=self.app_config,
1744
        )
1745
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1746
        api = ContentApi(
1747
            current_user=admin,
1748
            session=dbsession,
1749
            config=self.app_config,
1750
        )
1751
        api2 = ContentApi(
1752
            current_user=test_user,
1753
            session=dbsession,
1754
            config=self.app_config,
1755
        )
1756
        main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
1757
        # creation order test
1758
        firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
1759
        api.mark_read(firstly_created)
1760
        api2.mark_read(firstly_created)
1761
        dbsession.flush()
1762
        transaction.commit()
1763
1764
        self.testapp.authorization = (
1765
            'Basic',
1766
            (
1767
                '[email protected]',
1768
                '[email protected]'
1769
            )
1770
        )
1771
        # unread
1772
        self.testapp.put(
1773
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format(  # nopep8
1774
                workspace_id=4000,
1775
                content_id=firstly_created.content_id,
1776
                user_id=test_user.user_id,
1777
            ),
1778
            status=400,
1779
        )
1780
1781
    def test_api_set_content_as_unread__err__400__admin_content_do_not_exist(self):
@@ 1389-1468 (lines=80) @@
1386
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1387
        assert res.json_body[0]['read_by_user'] is True
1388
1389
    def test_api_set_content_as_read__ok__403__other_user(self):
1390
        # init DB
1391
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1392
        admin = dbsession.query(models.User) \
1393
            .filter(models.User.email == '[email protected]') \
1394
            .one()
1395
        workspace_api = WorkspaceApi(
1396
            current_user=admin,
1397
            session=dbsession,
1398
            config=self.app_config
1399
1400
        )
1401
        workspace = WorkspaceApi(
1402
            current_user=admin,
1403
            session=dbsession,
1404
            config=self.app_config,
1405
        ).create_workspace(
1406
            'test workspace',
1407
            save_now=True
1408
        )
1409
        uapi = UserApi(
1410
            current_user=admin,
1411
            session=dbsession,
1412
            config=self.app_config,
1413
        )
1414
        gapi = GroupApi(
1415
            current_user=admin,
1416
            session=dbsession,
1417
            config=self.app_config,
1418
        )
1419
        groups = [gapi.get_one_with_name('users')]
1420
        test_user = uapi.create_user(
1421
            email='[email protected]',
1422
            password='pass',
1423
            name='bob',
1424
            groups=groups,
1425
            timezone='Europe/Paris',
1426
            lang='fr',
1427
            do_save=True,
1428
            do_notify=False,
1429
        )
1430
        rapi = RoleApi(
1431
            current_user=admin,
1432
            session=dbsession,
1433
            config=self.app_config,
1434
        )
1435
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1436
        api = ContentApi(
1437
            current_user=admin,
1438
            session=dbsession,
1439
            config=self.app_config,
1440
        )
1441
        api2 = ContentApi(
1442
            current_user=test_user,
1443
            session=dbsession,
1444
            config=self.app_config,
1445
        )
1446
        main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
1447
        # creation order test
1448
        firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
1449
        api.mark_unread(firstly_created)
1450
        api2.mark_unread(firstly_created)
1451
        dbsession.flush()
1452
        transaction.commit()
1453
1454
        self.testapp.authorization = (
1455
            'Basic',
1456
            (
1457
                '[email protected]',
1458
                'pass'
1459
            )
1460
        )
1461
        # read
1462
        self.testapp.put(
1463
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format(  # nopep8
1464
                workspace_id=workspace.workspace_id,
1465
                content_id=firstly_created.content_id,
1466
                user_id=admin.user_id,
1467
            ),
1468
            status=403,
1469
        )
1470
1471
    def test_api_set_content_as_read__ok__200__admin_with_comments(self):
@@ 1207-1286 (lines=80) @@
1204
            status=400,
1205
        )
1206
1207
    def test_api_set_content_as_read__ok__200__admin_content_do_not_exist(self):
1208
        # init DB
1209
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1210
        admin = dbsession.query(models.User) \
1211
            .filter(models.User.email == '[email protected]') \
1212
            .one()
1213
        workspace_api = WorkspaceApi(
1214
            current_user=admin,
1215
            session=dbsession,
1216
            config=self.app_config
1217
1218
        )
1219
        workspace = WorkspaceApi(
1220
            current_user=admin,
1221
            session=dbsession,
1222
            config=self.app_config,
1223
        ).create_workspace(
1224
            'test workspace',
1225
            save_now=True
1226
        )
1227
        uapi = UserApi(
1228
            current_user=admin,
1229
            session=dbsession,
1230
            config=self.app_config,
1231
        )
1232
        gapi = GroupApi(
1233
            current_user=admin,
1234
            session=dbsession,
1235
            config=self.app_config,
1236
        )
1237
        groups = [gapi.get_one_with_name('users')]
1238
        test_user = uapi.create_user(
1239
            email='[email protected]',
1240
            password='pass',
1241
            name='bob',
1242
            groups=groups,
1243
            timezone='Europe/Paris',
1244
            lang='fr',
1245
            do_save=True,
1246
            do_notify=False,
1247
        )
1248
        rapi = RoleApi(
1249
            current_user=admin,
1250
            session=dbsession,
1251
            config=self.app_config,
1252
        )
1253
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1254
        api = ContentApi(
1255
            current_user=admin,
1256
            session=dbsession,
1257
            config=self.app_config,
1258
        )
1259
        api2 = ContentApi(
1260
            current_user=test_user,
1261
            session=dbsession,
1262
            config=self.app_config,
1263
        )
1264
        main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
1265
        # creation order test
1266
        firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
1267
        api.mark_unread(firstly_created)
1268
        api2.mark_unread(firstly_created)
1269
        dbsession.flush()
1270
        transaction.commit()
1271
1272
        self.testapp.authorization = (
1273
            'Basic',
1274
            (
1275
                '[email protected]',
1276
                '[email protected]'
1277
            )
1278
        )
1279
        # read
1280
        self.testapp.put(
1281
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format(  # nopep8
1282
                workspace_id=workspace.workspace_id,
1283
                content_id=4000,
1284
                user_id=test_user.user_id,
1285
            ),
1286
            status=400,
1287
        )
1288
1289
    def test_api_set_content_as_read__ok__200__user_itself(self):
@@ 1125-1204 (lines=80) @@
1122
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1123
        assert res.json_body[0]['read_by_user'] is False
1124
1125
    def test_api_set_content_as_read__ok__200__admin_workspace_do_not_exist(self):
1126
        # init DB
1127
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1128
        admin = dbsession.query(models.User) \
1129
            .filter(models.User.email == '[email protected]') \
1130
            .one()
1131
        workspace_api = WorkspaceApi(
1132
            current_user=admin,
1133
            session=dbsession,
1134
            config=self.app_config
1135
1136
        )
1137
        workspace = WorkspaceApi(
1138
            current_user=admin,
1139
            session=dbsession,
1140
            config=self.app_config,
1141
        ).create_workspace(
1142
            'test workspace',
1143
            save_now=True
1144
        )
1145
        uapi = UserApi(
1146
            current_user=admin,
1147
            session=dbsession,
1148
            config=self.app_config,
1149
        )
1150
        gapi = GroupApi(
1151
            current_user=admin,
1152
            session=dbsession,
1153
            config=self.app_config,
1154
        )
1155
        groups = [gapi.get_one_with_name('users')]
1156
        test_user = uapi.create_user(
1157
            email='[email protected]',
1158
            password='pass',
1159
            name='bob',
1160
            groups=groups,
1161
            timezone='Europe/Paris',
1162
            lang='fr',
1163
            do_save=True,
1164
            do_notify=False,
1165
        )
1166
        rapi = RoleApi(
1167
            current_user=admin,
1168
            session=dbsession,
1169
            config=self.app_config,
1170
        )
1171
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1172
        api = ContentApi(
1173
            current_user=admin,
1174
            session=dbsession,
1175
            config=self.app_config,
1176
        )
1177
        api2 = ContentApi(
1178
            current_user=test_user,
1179
            session=dbsession,
1180
            config=self.app_config,
1181
        )
1182
        main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
1183
        # creation order test
1184
        firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
1185
        api.mark_unread(firstly_created)
1186
        api2.mark_unread(firstly_created)
1187
        dbsession.flush()
1188
        transaction.commit()
1189
1190
        self.testapp.authorization = (
1191
            'Basic',
1192
            (
1193
                '[email protected]',
1194
                '[email protected]'
1195
            )
1196
        )
1197
        # read
1198
        self.testapp.put(
1199
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format(  # nopep8
1200
                workspace_id=4000,
1201
                content_id=firstly_created.content_id,
1202
                user_id=test_user.user_id,
1203
            ),
1204
            status=400,
1205
        )
1206
1207
    def test_api_set_content_as_read__ok__200__admin_content_do_not_exist(self):