Browse Source

add few tests for get/set file + previews endpoints

Guénaël Muller 6 years ago
parent
commit
ccf2c882f6
3 changed files with 350 additions and 3 deletions
  1. 2 1
      setup.py
  2. 11 1
      tracim/tests/__init__.py
  3. 337 1
      tracim/tests/functional/test_contents.py

+ 2 - 1
setup.py View File

49
     'pytest-cov',
49
     'pytest-cov',
50
     'pep8',
50
     'pep8',
51
     'mypy',
51
     'mypy',
52
-    'requests'
52
+    'requests',
53
+    'Pillow'
53
 ]
54
 ]
54
 
55
 
55
 mysql_require = [
56
 mysql_require = [

+ 11 - 1
tracim/tests/__init__.py View File

25
 from tracim.extensions import hapic
25
 from tracim.extensions import hapic
26
 from tracim import web
26
 from tracim import web
27
 from webtest import TestApp
27
 from webtest import TestApp
28
-
28
+from io import BytesIO
29
+from PIL import Image
29
 
30
 
30
 def eq_(a, b, msg=None):
31
 def eq_(a, b, msg=None):
31
     # TODO - G.M - 05-04-2018 - Remove this when all old nose code is removed
32
     # TODO - G.M - 05-04-2018 - Remove this when all old nose code is removed
54
     assert content_query.count() > 0
55
     assert content_query.count() > 0
55
 
56
 
56
 
57
 
58
+def create_test_image():
59
+    file = BytesIO()
60
+    image = Image.new('RGBA', size=(1000, 1000), color=(0, 0, 0))
61
+    image.save(file, 'png')
62
+    file.name = 'test_image.png'
63
+    file.seek(0)
64
+    return file
65
+
66
+
57
 class FunctionalTest(unittest.TestCase):
67
 class FunctionalTest(unittest.TestCase):
58
 
68
 
59
     fixtures = [BaseFixture]
69
     fixtures = [BaseFixture]

+ 337 - 1
tracim/tests/functional/test_contents.py View File

1
 # -*- coding: utf-8 -*-
1
 # -*- coding: utf-8 -*-
2
+import io
3
+
4
+import pytest
2
 import transaction
5
 import transaction
6
+from PIL import Image
3
 from depot.io.utils import FileIntent
7
 from depot.io.utils import FileIntent
4
 
8
 
5
 from tracim import models
9
 from tracim import models
7
 from tracim.lib.core.workspace import WorkspaceApi
11
 from tracim.lib.core.workspace import WorkspaceApi
8
 from tracim.models.data import ContentType
12
 from tracim.models.data import ContentType
9
 from tracim.models import get_tm_session
13
 from tracim.models import get_tm_session
10
-from tracim.tests import FunctionalTest
14
+from tracim.models.revision_protection import new_revision
15
+from tracim.tests import FunctionalTest, create_test_image
11
 from tracim.tests import set_html_document_slug_to_legacy
16
 from tracim.tests import set_html_document_slug_to_legacy
12
 from tracim.fixtures.content import Content as ContentFixtures
17
 from tracim.fixtures.content import Content as ContentFixtures
13
 from tracim.fixtures.users_and_groups import Base as BaseFixture
18
 from tracim.fixtures.users_and_groups import Base as BaseFixture
943
             status=400
948
             status=400
944
         )
949
         )
945
 
950
 
951
+    def test_api__get_file_raw__ok_200__nominal_case(self) -> None:
952
+        """
953
+        Get one file of a content
954
+        """
955
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
956
+        admin = dbsession.query(models.User) \
957
+            .filter(models.User.email == 'admin@admin.admin') \
958
+            .one()
959
+        workspace_api = WorkspaceApi(
960
+            current_user=admin,
961
+            session=dbsession,
962
+            config=self.app_config
963
+        )
964
+        content_api = ContentApi(
965
+            current_user=admin,
966
+            session=dbsession,
967
+            config=self.app_config
968
+        )
969
+        business_workspace = workspace_api.get_one(1)
970
+        tool_folder = content_api.get_one(1, content_type=ContentType.Any)
971
+        test_file = content_api.create(
972
+            content_type=ContentType.File,
973
+            workspace=business_workspace,
974
+            parent=tool_folder,
975
+            label='Test file',
976
+            do_save=False,
977
+            do_notify=False,
978
+        )
979
+        test_file.file_extension = '.txt'
980
+        test_file.depot_file = FileIntent(
981
+            b'Test file',
982
+            'Test_file.txt',
983
+            'text/plain',
984
+        )
985
+        content_api.update_content(test_file, 'Test_file', '<p>description</p>')  # nopep8
986
+        dbsession.flush()
987
+        transaction.commit()
988
+        content_id = int(test_file.content_id)
989
+        self.testapp.authorization = (
990
+            'Basic',
991
+            (
992
+                'admin@admin.admin',
993
+                'admin@admin.admin'
994
+            )
995
+        )
996
+        res = self.testapp.get(
997
+            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
998
+            status=200
999
+        )
1000
+        assert res.body == b'Test file'
1001
+        assert res.content_type == 'text/plain'
1002
+        assert res.content_length == len(b'Test file')
1003
+
1004
+    def test_api__set_file_raw__ok_200__nominal_case(self) -> None:
1005
+        """
1006
+        Set one file of a content
1007
+        """
1008
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
1009
+        admin = dbsession.query(models.User) \
1010
+            .filter(models.User.email == 'admin@admin.admin') \
1011
+            .one()
1012
+        workspace_api = WorkspaceApi(
1013
+            current_user=admin,
1014
+            session=dbsession,
1015
+            config=self.app_config
1016
+        )
1017
+        content_api = ContentApi(
1018
+            current_user=admin,
1019
+            session=dbsession,
1020
+            config=self.app_config
1021
+        )
1022
+        business_workspace = workspace_api.get_one(1)
1023
+        tool_folder = content_api.get_one(1, content_type=ContentType.Any)
1024
+        test_file = content_api.create(
1025
+            content_type=ContentType.File,
1026
+            workspace=business_workspace,
1027
+            parent=tool_folder,
1028
+            label='Test file',
1029
+            do_save=True,
1030
+            do_notify=False,
1031
+        )
1032
+        dbsession.flush()
1033
+        transaction.commit()
1034
+        content_id = int(test_file.content_id)
1035
+        image = create_test_image()
1036
+        self.testapp.authorization = (
1037
+            'Basic',
1038
+            (
1039
+                'admin@admin.admin',
1040
+                'admin@admin.admin'
1041
+            )
1042
+        )
1043
+        res = self.testapp.put(
1044
+            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
1045
+            upload_files=[
1046
+                ('files',image.name, image.getvalue())
1047
+            ],
1048
+            status=204,
1049
+        )
1050
+        res = self.testapp.get(
1051
+            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
1052
+            status=200
1053
+        )
1054
+        assert res.body == image.getvalue()
1055
+        assert res.content_type == 'image/png'
1056
+        assert res.content_length == len(image.getvalue())
1057
+
1058
+    def test_api__get_jpeg_preview__ok__200__nominal_case(self) -> None:
1059
+        """
1060
+        Set one file of a content
1061
+        """
1062
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
1063
+        admin = dbsession.query(models.User) \
1064
+            .filter(models.User.email == 'admin@admin.admin') \
1065
+            .one()
1066
+        workspace_api = WorkspaceApi(
1067
+            current_user=admin,
1068
+            session=dbsession,
1069
+            config=self.app_config
1070
+        )
1071
+        content_api = ContentApi(
1072
+            current_user=admin,
1073
+            session=dbsession,
1074
+            config=self.app_config
1075
+        )
1076
+        business_workspace = workspace_api.get_one(1)
1077
+        tool_folder = content_api.get_one(1, content_type=ContentType.Any)
1078
+        test_file = content_api.create(
1079
+            content_type=ContentType.File,
1080
+            workspace=business_workspace,
1081
+            parent=tool_folder,
1082
+            label='Test file',
1083
+            do_save=True,
1084
+            do_notify=False,
1085
+        )
1086
+        dbsession.flush()
1087
+        transaction.commit()
1088
+        content_id = int(test_file.content_id)
1089
+        image = create_test_image()
1090
+        self.testapp.authorization = (
1091
+            'Basic',
1092
+            (
1093
+                'admin@admin.admin',
1094
+                'admin@admin.admin'
1095
+            )
1096
+        )
1097
+        res = self.testapp.put(
1098
+            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
1099
+            upload_files=[
1100
+                ('files',image.name, image.getvalue())
1101
+            ],
1102
+            status=204,
1103
+        )
1104
+        res = self.testapp.get(
1105
+            '/api/v2/workspaces/1/files/{}/preview/jpg'.format(content_id),
1106
+            status=200
1107
+        )
1108
+        assert res.body != image.getvalue()
1109
+        assert res.content_type == 'image/jpeg'
1110
+
1111
+    def test_api__get_sized_jpeg_preview__ok__200__nominal_case(self) -> None:
1112
+        """
1113
+        Set one file of a content
1114
+        """
1115
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
1116
+        admin = dbsession.query(models.User) \
1117
+            .filter(models.User.email == 'admin@admin.admin') \
1118
+            .one()
1119
+        workspace_api = WorkspaceApi(
1120
+            current_user=admin,
1121
+            session=dbsession,
1122
+            config=self.app_config
1123
+        )
1124
+        content_api = ContentApi(
1125
+            current_user=admin,
1126
+            session=dbsession,
1127
+            config=self.app_config
1128
+        )
1129
+        business_workspace = workspace_api.get_one(1)
1130
+        tool_folder = content_api.get_one(1, content_type=ContentType.Any)
1131
+        test_file = content_api.create(
1132
+            content_type=ContentType.File,
1133
+            workspace=business_workspace,
1134
+            parent=tool_folder,
1135
+            label='Test file',
1136
+            do_save=True,
1137
+            do_notify=False,
1138
+        )
1139
+        dbsession.flush()
1140
+        transaction.commit()
1141
+        content_id = int(test_file.content_id)
1142
+        image = create_test_image()
1143
+        self.testapp.authorization = (
1144
+            'Basic',
1145
+            (
1146
+                'admin@admin.admin',
1147
+                'admin@admin.admin'
1148
+            )
1149
+        )
1150
+        res = self.testapp.put(
1151
+            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
1152
+            upload_files=[
1153
+                ('files',image.name, image.getvalue())
1154
+            ],
1155
+            status=204,
1156
+        )
1157
+        res = self.testapp.get(
1158
+            '/api/v2/workspaces/1/files/{}/preview/jpg/256x256'.format(content_id), # nopep8
1159
+            status=200
1160
+        )
1161
+        assert res.body != image.getvalue()
1162
+        assert res.content_type == 'image/jpeg'
1163
+        new_image = Image.open(io.BytesIO(res.body))
1164
+        assert 256, 256 == new_image.size
1165
+
1166
+    def test_api__get_full_pdf_preview__ok__200__nominal_case(self) -> None:
1167
+        """
1168
+        Set one file of a content
1169
+        """
1170
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
1171
+        admin = dbsession.query(models.User) \
1172
+            .filter(models.User.email == 'admin@admin.admin') \
1173
+            .one()
1174
+        workspace_api = WorkspaceApi(
1175
+            current_user=admin,
1176
+            session=dbsession,
1177
+            config=self.app_config
1178
+        )
1179
+        content_api = ContentApi(
1180
+            current_user=admin,
1181
+            session=dbsession,
1182
+            config=self.app_config
1183
+        )
1184
+        business_workspace = workspace_api.get_one(1)
1185
+        tool_folder = content_api.get_one(1, content_type=ContentType.Any)
1186
+        test_file = content_api.create(
1187
+            content_type=ContentType.File,
1188
+            workspace=business_workspace,
1189
+            parent=tool_folder,
1190
+            label='Test file',
1191
+            do_save=True,
1192
+            do_notify=False,
1193
+        )
1194
+        with new_revision(
1195
+                session=dbsession,
1196
+                tm=transaction.manager,
1197
+                content=test_file,
1198
+        ):
1199
+            test_file.file_extension = '.txt'
1200
+            test_file.depot_file = FileIntent(
1201
+                b'Test file',
1202
+                'Test_file.txt',
1203
+                'text/plain',
1204
+            )
1205
+            content_api.update_content(test_file, 'Test_file', '<p>description</p>')
1206
+        dbsession.flush()
1207
+        transaction.commit()
1208
+        content_id = int(test_file.content_id)
1209
+        self.testapp.authorization = (
1210
+            'Basic',
1211
+            (
1212
+                'admin@admin.admin',
1213
+                'admin@admin.admin'
1214
+            )
1215
+        )
1216
+        res = self.testapp.put(
1217
+            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
1218
+            upload_files=[
1219
+                ('files', test_file.file_name, test_file.depot_file.file.read())
1220
+            ],
1221
+            status=204,
1222
+        )
1223
+        res = self.testapp.get(
1224
+            '/api/v2/workspaces/1/files/{}/preview/pdf/full'.format(content_id), # nopep8
1225
+            status=200
1226
+        )
1227
+        assert res.content_type == 'application/pdf'
1228
+
1229
+    @pytest.mark.xfail(reason='TODO')
1230
+    def test_api__get_full_pdf_preview__err__400__jpg_UnavailablePreviewType(self) -> None:
1231
+        """
1232
+        Set one file of a content
1233
+        """
1234
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
1235
+        admin = dbsession.query(models.User) \
1236
+            .filter(models.User.email == 'admin@admin.admin') \
1237
+            .one()
1238
+        workspace_api = WorkspaceApi(
1239
+            current_user=admin,
1240
+            session=dbsession,
1241
+            config=self.app_config
1242
+        )
1243
+        content_api = ContentApi(
1244
+            current_user=admin,
1245
+            session=dbsession,
1246
+            config=self.app_config
1247
+        )
1248
+        business_workspace = workspace_api.get_one(1)
1249
+        tool_folder = content_api.get_one(1, content_type=ContentType.Any)
1250
+        test_file = content_api.create(
1251
+            content_type=ContentType.File,
1252
+            workspace=business_workspace,
1253
+            parent=tool_folder,
1254
+            label='Test file',
1255
+            do_save=True,
1256
+            do_notify=False,
1257
+        )
1258
+        dbsession.flush()
1259
+        transaction.commit()
1260
+        content_id = int(test_file.content_id)
1261
+        image = create_test_image()
1262
+        self.testapp.authorization = (
1263
+            'Basic',
1264
+            (
1265
+                'admin@admin.admin',
1266
+                'admin@admin.admin'
1267
+            )
1268
+        )
1269
+        res = self.testapp.put(
1270
+            '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
1271
+            upload_files=[
1272
+                ('files',image.name, image.getvalue())
1273
+            ],
1274
+            status=204,
1275
+        )
1276
+        res = self.testapp.get(
1277
+            '/api/v2/workspaces/1/files/{}/preview/pdf/full'.format(content_id), # nopep8
1278
+            status=200
1279
+        )
1280
+        assert res.content_type == ''
1281
+
946
 
1282
 
947
 class TestThreads(FunctionalTest):
1283
 class TestThreads(FunctionalTest):
948
     """
1284
     """