Code Duplication    Length = 84-85 lines in 6 locations

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

@@ 1408-1491 (lines=84) @@
1405
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1406
        assert res.json_body[0]['read_by_user'] is True
1407
1408
    def test_api_set_content_as_read__ok__403__other_user(self):
1409
        # init DB
1410
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1411
        admin = dbsession.query(models.User) \
1412
            .filter(models.User.email == '[email protected]') \
1413
            .one()
1414
        workspace_api = WorkspaceApi(
1415
            current_user=admin,
1416
            session=dbsession,
1417
            config=self.app_config
1418
1419
        )
1420
        workspace = WorkspaceApi(
1421
            current_user=admin,
1422
            session=dbsession,
1423
            config=self.app_config,
1424
        ).create_workspace(
1425
            'test workspace',
1426
            save_now=True
1427
        )
1428
        uapi = UserApi(
1429
            current_user=admin,
1430
            session=dbsession,
1431
            config=self.app_config,
1432
        )
1433
        gapi = GroupApi(
1434
            current_user=admin,
1435
            session=dbsession,
1436
            config=self.app_config,
1437
        )
1438
        groups = [gapi.get_one_with_name('users')]
1439
        test_user = uapi.create_user(
1440
            email='[email protected]',
1441
            password='pass',
1442
            name='bob',
1443
            groups=groups,
1444
            timezone='Europe/Paris',
1445
            lang='fr',
1446
            do_save=True,
1447
            do_notify=False,
1448
        )
1449
        rapi = RoleApi(
1450
            current_user=admin,
1451
            session=dbsession,
1452
            config=self.app_config,
1453
        )
1454
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1455
        api = ContentApi(
1456
            current_user=admin,
1457
            session=dbsession,
1458
            config=self.app_config,
1459
        )
1460
        api2 = ContentApi(
1461
            current_user=test_user,
1462
            session=dbsession,
1463
            config=self.app_config,
1464
        )
1465
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
1466
        # creation order test
1467
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
1468
        api.mark_unread(firstly_created)
1469
        api2.mark_unread(firstly_created)
1470
        dbsession.flush()
1471
        transaction.commit()
1472
1473
        self.testapp.authorization = (
1474
            'Basic',
1475
            (
1476
                '[email protected]',
1477
                'pass'
1478
            )
1479
        )
1480
        # read
1481
        res = self.testapp.put(
1482
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format(  # nopep8
1483
                workspace_id=workspace.workspace_id,
1484
                content_id=firstly_created.content_id,
1485
                user_id=admin.user_id,
1486
            ),
1487
            status=403,
1488
        )
1489
        assert isinstance(res.json, dict)
1490
        assert 'code' in res.json.keys()
1491
        assert res.json_body['code'] == error.INSUFFICIENT_USER_PROFILE
1492
1493
    def test_api_set_content_as_read__ok__200__admin_with_comments_read_content(self):
1494
        # init DB
@@ 1223-1306 (lines=84) @@
1220
        assert 'code' in res.json.keys()
1221
        assert res.json_body['code'] == error.WORKSPACE_NOT_FOUND
1222
1223
    def test_api_set_content_as_read__ok__200__admin_content_do_not_exist(self):
1224
        # init DB
1225
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1226
        admin = dbsession.query(models.User) \
1227
            .filter(models.User.email == '[email protected]') \
1228
            .one()
1229
        workspace_api = WorkspaceApi(
1230
            current_user=admin,
1231
            session=dbsession,
1232
            config=self.app_config
1233
1234
        )
1235
        workspace = WorkspaceApi(
1236
            current_user=admin,
1237
            session=dbsession,
1238
            config=self.app_config,
1239
        ).create_workspace(
1240
            'test workspace',
1241
            save_now=True
1242
        )
1243
        uapi = UserApi(
1244
            current_user=admin,
1245
            session=dbsession,
1246
            config=self.app_config,
1247
        )
1248
        gapi = GroupApi(
1249
            current_user=admin,
1250
            session=dbsession,
1251
            config=self.app_config,
1252
        )
1253
        groups = [gapi.get_one_with_name('users')]
1254
        test_user = uapi.create_user(
1255
            email='[email protected]',
1256
            password='pass',
1257
            name='bob',
1258
            groups=groups,
1259
            timezone='Europe/Paris',
1260
            lang='fr',
1261
            do_save=True,
1262
            do_notify=False,
1263
        )
1264
        rapi = RoleApi(
1265
            current_user=admin,
1266
            session=dbsession,
1267
            config=self.app_config,
1268
        )
1269
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1270
        api = ContentApi(
1271
            current_user=admin,
1272
            session=dbsession,
1273
            config=self.app_config,
1274
        )
1275
        api2 = ContentApi(
1276
            current_user=test_user,
1277
            session=dbsession,
1278
            config=self.app_config,
1279
        )
1280
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
1281
        # creation order test
1282
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
1283
        api.mark_unread(firstly_created)
1284
        api2.mark_unread(firstly_created)
1285
        dbsession.flush()
1286
        transaction.commit()
1287
1288
        self.testapp.authorization = (
1289
            'Basic',
1290
            (
1291
                '[email protected]',
1292
                '[email protected]'
1293
            )
1294
        )
1295
        # read
1296
        res = self.testapp.put(
1297
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format(  # nopep8
1298
                workspace_id=workspace.workspace_id,
1299
                content_id=4000,
1300
                user_id=test_user.user_id,
1301
            ),
1302
            status=400,
1303
        )
1304
        assert isinstance(res.json, dict)
1305
        assert 'code' in res.json.keys()
1306
        assert res.json_body['code'] == error.CONTENT_NOT_FOUND
1307
1308
    def test_api_set_content_as_read__ok__200__user_itself(self):
1309
        # init DB
@@ 1138-1221 (lines=84) @@
1135
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1136
        assert res.json_body[0]['read_by_user'] is False
1137
1138
    def test_api_set_content_as_read__ok__200__admin_workspace_do_not_exist(self):
1139
        # init DB
1140
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1141
        admin = dbsession.query(models.User) \
1142
            .filter(models.User.email == '[email protected]') \
1143
            .one()
1144
        workspace_api = WorkspaceApi(
1145
            current_user=admin,
1146
            session=dbsession,
1147
            config=self.app_config
1148
1149
        )
1150
        workspace = WorkspaceApi(
1151
            current_user=admin,
1152
            session=dbsession,
1153
            config=self.app_config,
1154
        ).create_workspace(
1155
            'test workspace',
1156
            save_now=True
1157
        )
1158
        uapi = UserApi(
1159
            current_user=admin,
1160
            session=dbsession,
1161
            config=self.app_config,
1162
        )
1163
        gapi = GroupApi(
1164
            current_user=admin,
1165
            session=dbsession,
1166
            config=self.app_config,
1167
        )
1168
        groups = [gapi.get_one_with_name('users')]
1169
        test_user = uapi.create_user(
1170
            email='[email protected]',
1171
            password='pass',
1172
            name='bob',
1173
            groups=groups,
1174
            timezone='Europe/Paris',
1175
            lang='fr',
1176
            do_save=True,
1177
            do_notify=False,
1178
        )
1179
        rapi = RoleApi(
1180
            current_user=admin,
1181
            session=dbsession,
1182
            config=self.app_config,
1183
        )
1184
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1185
        api = ContentApi(
1186
            current_user=admin,
1187
            session=dbsession,
1188
            config=self.app_config,
1189
        )
1190
        api2 = ContentApi(
1191
            current_user=test_user,
1192
            session=dbsession,
1193
            config=self.app_config,
1194
        )
1195
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
1196
        # creation order test
1197
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
1198
        api.mark_unread(firstly_created)
1199
        api2.mark_unread(firstly_created)
1200
        dbsession.flush()
1201
        transaction.commit()
1202
1203
        self.testapp.authorization = (
1204
            'Basic',
1205
            (
1206
                '[email protected]',
1207
                '[email protected]'
1208
            )
1209
        )
1210
        # read
1211
        res = self.testapp.put(
1212
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format(  # nopep8
1213
                workspace_id=4000,
1214
                content_id=firstly_created.content_id,
1215
                user_id=test_user.user_id,
1216
            ),
1217
            status=400,
1218
        )
1219
        assert isinstance(res.json, dict)
1220
        assert 'code' in res.json.keys()
1221
        assert res.json_body['code'] == error.WORKSPACE_NOT_FOUND
1222
1223
    def test_api_set_content_as_read__ok__200__admin_content_do_not_exist(self):
1224
        # init DB
@@ 2093-2177 (lines=85) @@
2090
        assert res.json_body[0]['content_id'] == firstly_created.content_id
2091
        assert res.json_body[0]['read_by_user'] is False
2092
2093
    def test_api_set_content_as_unread__err__403__other_user(self):
2094
        # init DB
2095
        dbsession = get_tm_session(self.session_factory, transaction.manager)
2096
        admin = dbsession.query(models.User) \
2097
            .filter(models.User.email == '[email protected]') \
2098
            .one()
2099
        workspace_api = WorkspaceApi(
2100
            current_user=admin,
2101
            session=dbsession,
2102
            config=self.app_config
2103
2104
        )
2105
        workspace = WorkspaceApi(
2106
            current_user=admin,
2107
            session=dbsession,
2108
            config=self.app_config,
2109
        ).create_workspace(
2110
            'test workspace',
2111
            save_now=True
2112
        )
2113
        uapi = UserApi(
2114
            current_user=admin,
2115
            session=dbsession,
2116
            config=self.app_config,
2117
        )
2118
        gapi = GroupApi(
2119
            current_user=admin,
2120
            session=dbsession,
2121
            config=self.app_config,
2122
        )
2123
        groups = [gapi.get_one_with_name('users')]
2124
        test_user = uapi.create_user(
2125
            email='[email protected]',
2126
            password='pass',
2127
            name='bob',
2128
            groups=groups,
2129
            timezone='Europe/Paris',
2130
            lang='fr',
2131
            do_save=True,
2132
            do_notify=False,
2133
        )
2134
        rapi = RoleApi(
2135
            current_user=admin,
2136
            session=dbsession,
2137
            config=self.app_config,
2138
        )
2139
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
2140
        api = ContentApi(
2141
            current_user=admin,
2142
            session=dbsession,
2143
            config=self.app_config,
2144
        )
2145
        api2 = ContentApi(
2146
            current_user=test_user,
2147
            session=dbsession,
2148
            config=self.app_config,
2149
        )
2150
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
2151
        # creation order test
2152
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
2153
        api.mark_read(firstly_created)
2154
        api2.mark_read(firstly_created)
2155
        dbsession.flush()
2156
        transaction.commit()
2157
2158
        self.testapp.authorization = (
2159
            'Basic',
2160
            (
2161
                '[email protected]',
2162
                'pass'
2163
            )
2164
        )
2165
2166
        # unread
2167
        res = self.testapp.put(
2168
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format(  # nopep8
2169
                workspace_id=workspace.workspace_id,
2170
                content_id=firstly_created.content_id,
2171
                user_id=admin.user_id,
2172
            ),
2173
            status=403,
2174
        )
2175
        assert isinstance(res.json, dict)
2176
        assert 'code' in res.json.keys()
2177
        assert res.json_body['code'] == error.INSUFFICIENT_USER_PROFILE
2178
2179
    def test_api_set_content_as_unread__ok__200__with_comments_read_content(self):
2180
        # init DB
@@ 1911-1995 (lines=85) @@
1908
        assert 'code' in res.json.keys()
1909
        assert res.json_body['code'] == error.WORKSPACE_NOT_FOUND
1910
1911
    def test_api_set_content_as_unread__err__400__admin_content_do_not_exist(self):
1912
        # init DB
1913
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1914
        admin = dbsession.query(models.User) \
1915
            .filter(models.User.email == '[email protected]') \
1916
            .one()
1917
        workspace_api = WorkspaceApi(
1918
            current_user=admin,
1919
            session=dbsession,
1920
            config=self.app_config
1921
1922
        )
1923
        workspace = WorkspaceApi(
1924
            current_user=admin,
1925
            session=dbsession,
1926
            config=self.app_config,
1927
        ).create_workspace(
1928
            'test workspace',
1929
            save_now=True
1930
        )
1931
        uapi = UserApi(
1932
            current_user=admin,
1933
            session=dbsession,
1934
            config=self.app_config,
1935
        )
1936
        gapi = GroupApi(
1937
            current_user=admin,
1938
            session=dbsession,
1939
            config=self.app_config,
1940
        )
1941
        groups = [gapi.get_one_with_name('users')]
1942
        test_user = uapi.create_user(
1943
            email='[email protected]',
1944
            password='pass',
1945
            name='bob',
1946
            groups=groups,
1947
            timezone='Europe/Paris',
1948
            lang='fr',
1949
            do_save=True,
1950
            do_notify=False,
1951
        )
1952
        rapi = RoleApi(
1953
            current_user=admin,
1954
            session=dbsession,
1955
            config=self.app_config,
1956
        )
1957
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1958
        api = ContentApi(
1959
            current_user=admin,
1960
            session=dbsession,
1961
            config=self.app_config,
1962
        )
1963
        api2 = ContentApi(
1964
            current_user=test_user,
1965
            session=dbsession,
1966
            config=self.app_config,
1967
        )
1968
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
1969
        # creation order test
1970
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
1971
        api.mark_read(firstly_created)
1972
        api2.mark_read(firstly_created)
1973
        dbsession.flush()
1974
        transaction.commit()
1975
1976
        self.testapp.authorization = (
1977
            'Basic',
1978
            (
1979
                '[email protected]',
1980
                '[email protected]'
1981
            )
1982
        )
1983
1984
        # unread
1985
        res = self.testapp.put(
1986
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format(  # nopep8
1987
                workspace_id=workspace.workspace_id,
1988
                content_id=4000,
1989
                user_id=test_user.user_id,
1990
            ),
1991
            status=400,
1992
        )
1993
        assert isinstance(res.json, dict)
1994
        assert 'code' in res.json.keys()
1995
        assert res.json_body['code'] == error.CONTENT_NOT_FOUND
1996
1997
    def test_api_set_content_as_unread__ok__200__user_itself(self):
1998
        # init DB
@@ 1826-1909 (lines=84) @@
1823
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1824
        assert res.json_body[0]['read_by_user'] is True
1825
1826
    def test_api_set_content_as_unread__err__400__admin_workspace_do_not_exist(self):
1827
        # init DB
1828
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1829
        admin = dbsession.query(models.User) \
1830
            .filter(models.User.email == '[email protected]') \
1831
            .one()
1832
        workspace_api = WorkspaceApi(
1833
            current_user=admin,
1834
            session=dbsession,
1835
            config=self.app_config
1836
1837
        )
1838
        workspace = WorkspaceApi(
1839
            current_user=admin,
1840
            session=dbsession,
1841
            config=self.app_config,
1842
        ).create_workspace(
1843
            'test workspace',
1844
            save_now=True
1845
        )
1846
        uapi = UserApi(
1847
            current_user=admin,
1848
            session=dbsession,
1849
            config=self.app_config,
1850
        )
1851
        gapi = GroupApi(
1852
            current_user=admin,
1853
            session=dbsession,
1854
            config=self.app_config,
1855
        )
1856
        groups = [gapi.get_one_with_name('users')]
1857
        test_user = uapi.create_user(
1858
            email='[email protected]',
1859
            password='pass',
1860
            name='bob',
1861
            groups=groups,
1862
            timezone='Europe/Paris',
1863
            lang='fr',
1864
            do_save=True,
1865
            do_notify=False,
1866
        )
1867
        rapi = RoleApi(
1868
            current_user=admin,
1869
            session=dbsession,
1870
            config=self.app_config,
1871
        )
1872
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1873
        api = ContentApi(
1874
            current_user=admin,
1875
            session=dbsession,
1876
            config=self.app_config,
1877
        )
1878
        api2 = ContentApi(
1879
            current_user=test_user,
1880
            session=dbsession,
1881
            config=self.app_config,
1882
        )
1883
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
1884
        # creation order test
1885
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
1886
        api.mark_read(firstly_created)
1887
        api2.mark_read(firstly_created)
1888
        dbsession.flush()
1889
        transaction.commit()
1890
1891
        self.testapp.authorization = (
1892
            'Basic',
1893
            (
1894
                '[email protected]',
1895
                '[email protected]'
1896
            )
1897
        )
1898
        # unread
1899
        res = self.testapp.put(
1900
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format(  # nopep8
1901
                workspace_id=4000,
1902
                content_id=firstly_created.content_id,
1903
                user_id=test_user.user_id,
1904
            ),
1905
            status=400,
1906
        )
1907
        assert isinstance(res.json, dict)
1908
        assert 'code' in res.json.keys()
1909
        assert res.json_body['code'] == error.WORKSPACE_NOT_FOUND
1910
1911
    def test_api_set_content_as_unread__err__400__admin_content_do_not_exist(self):
1912
        # init DB