Code Duplication    Length = 107-109 lines in 2 locations

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

@@ 1030-1136 (lines=107) @@
1027
    """
1028
    Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read  # nopep8
1029
    """
1030
    def test_api_set_content_as_read__ok__200__admin(self):
1031
        # init DB
1032
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1033
        admin = dbsession.query(models.User) \
1034
            .filter(models.User.email == '[email protected]') \
1035
            .one()
1036
        workspace_api = WorkspaceApi(
1037
            current_user=admin,
1038
            session=dbsession,
1039
            config=self.app_config
1040
1041
        )
1042
        workspace = WorkspaceApi(
1043
            current_user=admin,
1044
            session=dbsession,
1045
            config=self.app_config,
1046
        ).create_workspace(
1047
            'test workspace',
1048
            save_now=True
1049
        )
1050
        uapi = UserApi(
1051
            current_user=admin,
1052
            session=dbsession,
1053
            config=self.app_config,
1054
        )
1055
        gapi = GroupApi(
1056
            current_user=admin,
1057
            session=dbsession,
1058
            config=self.app_config,
1059
        )
1060
        groups = [gapi.get_one_with_name('users')]
1061
        test_user = uapi.create_user(
1062
            email='[email protected]',
1063
            password='pass',
1064
            name='bob',
1065
            groups=groups,
1066
            timezone='Europe/Paris',
1067
            do_save=True,
1068
            do_notify=False,
1069
        )
1070
        rapi = RoleApi(
1071
            current_user=admin,
1072
            session=dbsession,
1073
            config=self.app_config,
1074
        )
1075
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1076
        api = ContentApi(
1077
            current_user=admin,
1078
            session=dbsession,
1079
            config=self.app_config,
1080
        )
1081
        api2 = ContentApi(
1082
            current_user=test_user,
1083
            session=dbsession,
1084
            config=self.app_config,
1085
        )
1086
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
1087
        # creation order test
1088
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
1089
        api.mark_unread(firstly_created)
1090
        api2.mark_unread(firstly_created)
1091
        dbsession.flush()
1092
        transaction.commit()
1093
1094
        self.testapp.authorization = (
1095
            'Basic',
1096
            (
1097
                '[email protected]',
1098
                '[email protected]'
1099
            )
1100
        )
1101
        # before
1102
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
1103
            user_id=test_user.user_id,
1104
            workspace_id=workspace.workspace_id
1105
        ), status=200)
1106
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1107
        assert res.json_body[0]['read_by_user'] is False
1108
1109
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
1110
            user_id=admin.user_id,
1111
            workspace_id=workspace.workspace_id
1112
        ), status=200)
1113
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1114
        assert res.json_body[0]['read_by_user'] is False
1115
        # read
1116
        self.testapp.put(
1117
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format(  # nopep8
1118
                workspace_id=workspace.workspace_id,
1119
                content_id=firstly_created.content_id,
1120
                user_id=test_user.user_id,
1121
            )
1122
        )
1123
        # after
1124
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
1125
            user_id=test_user.user_id,
1126
            workspace_id=workspace.workspace_id
1127
        ), status=200) # nopep8
1128
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1129
        assert res.json_body[0]['read_by_user'] is True
1130
1131
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
1132
            user_id=admin.user_id,
1133
            workspace_id=workspace.workspace_id
1134
        ), status=200) # nopep8
1135
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1136
        assert res.json_body[0]['read_by_user'] is False
1137
1138
    def test_api_set_content_as_read__ok__200__admin_workspace_do_not_exist(self):
1139
        # init DB
@@ 1716-1824 (lines=109) @@
1713
    """
1714
    Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread  # nopep8
1715
    """
1716
    def test_api_set_content_as_unread__ok__200__admin(self):
1717
        # init DB
1718
        dbsession = get_tm_session(self.session_factory, transaction.manager)
1719
        admin = dbsession.query(models.User) \
1720
            .filter(models.User.email == '[email protected]') \
1721
            .one()
1722
        workspace_api = WorkspaceApi(
1723
            current_user=admin,
1724
            session=dbsession,
1725
            config=self.app_config
1726
1727
        )
1728
        workspace = WorkspaceApi(
1729
            current_user=admin,
1730
            session=dbsession,
1731
            config=self.app_config,
1732
        ).create_workspace(
1733
            'test workspace',
1734
            save_now=True
1735
        )
1736
        uapi = UserApi(
1737
            current_user=admin,
1738
            session=dbsession,
1739
            config=self.app_config,
1740
        )
1741
        gapi = GroupApi(
1742
            current_user=admin,
1743
            session=dbsession,
1744
            config=self.app_config,
1745
        )
1746
        groups = [gapi.get_one_with_name('users')]
1747
        test_user = uapi.create_user(
1748
            email='[email protected]',
1749
            password='pass',
1750
            name='bob',
1751
            groups=groups,
1752
            timezone='Europe/Paris',
1753
            lang='fr',
1754
            do_save=True,
1755
            do_notify=False,
1756
        )
1757
        rapi = RoleApi(
1758
            current_user=admin,
1759
            session=dbsession,
1760
            config=self.app_config,
1761
        )
1762
        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
1763
        api = ContentApi(
1764
            current_user=admin,
1765
            session=dbsession,
1766
            config=self.app_config,
1767
        )
1768
        api2 = ContentApi(
1769
            current_user=test_user,
1770
            session=dbsession,
1771
            config=self.app_config,
1772
        )
1773
        main_folder = api.create(content_type_list.Folder.slug, workspace, None, 'this is randomized folder', '', True)  # nopep8
1774
        # creation order test
1775
        firstly_created = api.create(content_type_list.Page.slug, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
1776
        api.mark_read(firstly_created)
1777
        api2.mark_read(firstly_created)
1778
        dbsession.flush()
1779
        transaction.commit()
1780
1781
        self.testapp.authorization = (
1782
            'Basic',
1783
            (
1784
                '[email protected]',
1785
                '[email protected]'
1786
            )
1787
        )
1788
        # before
1789
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
1790
            user_id=test_user.user_id,
1791
            workspace_id=workspace.workspace_id
1792
        ), status=200)
1793
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1794
        assert res.json_body[0]['read_by_user'] is True
1795
1796
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
1797
            user_id=admin.user_id,
1798
            workspace_id=workspace.workspace_id
1799
        ), status=200)
1800
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1801
        assert res.json_body[0]['read_by_user'] is True
1802
1803
        # unread
1804
        self.testapp.put(
1805
            '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format(  # nopep8
1806
                workspace_id=workspace.workspace_id,
1807
                content_id=firstly_created.content_id,
1808
                user_id=test_user.user_id,
1809
            )
1810
        )
1811
        # after
1812
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
1813
            user_id=test_user.user_id,
1814
            workspace_id=workspace.workspace_id
1815
        ), status=200)
1816
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1817
        assert res.json_body[0]['read_by_user'] is False
1818
1819
        res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format(  # nopep8
1820
            user_id=admin.user_id,
1821
            workspace_id=workspace.workspace_id
1822
        ), status=200)
1823
        assert res.json_body[0]['content_id'] == firstly_created.content_id
1824
        assert res.json_body[0]['read_by_user'] is True
1825
1826
    def test_api_set_content_as_unread__err__400__admin_workspace_do_not_exist(self):
1827
        # init DB