@@ -1065,7 +1065,7 @@ def _make_file_upload(filename):
1065
1065
return random (1024 ), {"Content-Disposition" : ('attachment' , {'filename' : filename })}
1066
1066
1067
1067
1068
- def test_owned_files (client , room , user , admin ):
1068
+ def test_owned_files (client , room , room2 , user , admin ):
1069
1069
# - upload a file via new endpoints
1070
1070
filedata , headers = _make_file_upload ('fug-1.jpeg' )
1071
1071
r = sogs_post_raw (client , f'/room/{ room .token } /file' , filedata , user , extra_headers = headers )
@@ -1125,8 +1125,14 @@ def test_owned_files(client, room, user, admin):
1125
1125
filedata , headers = _make_file_upload ('another.png' )
1126
1126
r = sogs_post_raw (client , f'/room/{ room .token } /file' , filedata , user , extra_headers = headers )
1127
1127
assert r .status_code == 201
1128
+ f3 = File (id = r .json ['id' ])
1129
+ assert f3 .expiry == from_now .hours (1 )
1128
1130
d , s = (utils .encode_base64 (x ) for x in (b"more post data" , pad64 ("fsdf" )))
1129
- post_info = {'data' : d , 'signature' : s , 'files' : [f1 .id , r .json ['id' ]]}
1131
+ post_info = {'data' : d , 'signature' : s , 'files' : [f1 .id , f3 .id ]}
1132
+ r = sogs_put (client , f'/room/{ room .token } /message/{ post_id } ' , post_info , user )
1133
+ assert r .status_code == 200
1134
+ f3 = File (id = f3 .id )
1135
+ assert f3 .expiry == from_now .days (15 )
1130
1136
1131
1137
# - make sure the first post associated message hasn't changed (i.e. no stealing owned uploads)
1132
1138
f1a = File (id = f1 .id )
@@ -1148,6 +1154,8 @@ def test_owned_files(client, room, user, admin):
1148
1154
# - make a post referencing the room image ID
1149
1155
d , s = (utils .encode_base64 (x ) for x in (b"post xyz" , pad64 ("z" )))
1150
1156
post_info = {'data' : d , 'signature' : s , 'files' : [room_img ]}
1157
+ r = sogs_put (client , f'/room/{ room .token } /message/{ post_id } ' , post_info , user )
1158
+ assert r .status_code == 200
1151
1159
1152
1160
# - verify that the pinned image expiry and message are still both NULL
1153
1161
f_room = File (id = f_room .id )
@@ -1165,14 +1173,36 @@ def test_owned_files(client, room, user, admin):
1165
1173
1166
1174
from sogs .cleanup import cleanup
1167
1175
1168
- assert cleanup () == (2 , 0 , 0 , 0 , 0 )
1176
+ # Cleanup should remove 3 attachments: the two originals plus the one we added via an edit:
1177
+ assert cleanup () == (3 , 0 , 0 , 0 , 0 )
1169
1178
1170
1179
with pytest .raises (sogs .model .exc .NoSuchFile ):
1171
1180
f1 = File (id = f1 .id )
1172
1181
with pytest .raises (sogs .model .exc .NoSuchFile ):
1173
1182
f2 = File (id = f2 .id )
1174
1183
1175
1184
1185
+ def test_no_file_crosspost (client , room , room2 , user , global_admin ):
1186
+ # Disallow cross-room references (i.e. a post attaching a file uploaded to another room)
1187
+ filedata , headers = _make_file_upload ('room2-file.jpg' )
1188
+ r = sogs_post_raw (client , f'/room/{ room2 .token } /file' , filedata , user , extra_headers = headers )
1189
+ assert r .status_code == 201
1190
+ f = File (id = r .json ['id' ])
1191
+ d , s = (utils .encode_base64 (x ) for x in (b"room1 post" , pad64 ("sig123" )))
1192
+ post_info = {'data' : d , 'signature' : s , 'files' : [f .id ]}
1193
+ r = sogs_post (client , f'/room/{ room .token } /message' , post_info , user )
1194
+ assert r .status_code == 201
1195
+
1196
+ f = File (id = f .id )
1197
+ # The file isn't for a post in room 1, so shouldn't have been associated:
1198
+ assert f .post_id is None
1199
+ assert f .expiry == from_now .hours (1 )
1200
+
1201
+ # Disallow setting the room image to some foreign room's upload
1202
+ r = sogs_put (client , f'/room/{ room .token } ' , {'image' : f .id }, global_admin )
1203
+ assert r .status_code == 406
1204
+
1205
+
1176
1206
def _make_dummy_post (room , user ):
1177
1207
msg = room .add_post (user , b'data' , b'a' * 64 )
1178
1208
return msg .get ('id' )
0 commit comments