@@ -124,8 +124,8 @@ def test_api_documents_move_authenticated_target_roles_mocked(
124
124
target_role , target_parent_role , position
125
125
):
126
126
"""
127
- Authenticated users with insufficient permissions on the target document (or its
128
- parent depending on the position chosen), should not be allowed to move documents.
127
+ Only authenticated users with sufficient permissions on the target document (or its
128
+ parent depending on the position chosen), should be allowed to move documents.
129
129
"""
130
130
131
131
user = factories .UserFactory ()
@@ -208,6 +208,107 @@ def test_api_documents_move_authenticated_target_roles_mocked(
208
208
assert document .is_root () is True
209
209
210
210
211
+ def test_api_documents_move_authenticated_no_owner_user_and_team ():
212
+ """
213
+ Moving a document with no owner to the root of the tree should automatically declare
214
+ the owner of the prevous root of the document as owner of the document itself.
215
+ """
216
+ user = factories .UserFactory ()
217
+ client = APIClient ()
218
+ client .force_login (user )
219
+
220
+ parent_owner = factories .UserFactory ()
221
+ parent = factories .DocumentFactory (
222
+ users = [(parent_owner , "owner" )], teams = [("lasuite" , "owner" )]
223
+ )
224
+ # A document with no owner
225
+ document = factories .DocumentFactory (parent = parent , users = [(user , "administrator" )])
226
+ child = factories .DocumentFactory (parent = document )
227
+ target = factories .DocumentFactory ()
228
+
229
+ response = client .post (
230
+ f"/api/v1.0/documents/{ document .id !s} /move/" ,
231
+ data = {"target_document_id" : str (target .id ), "position" : "first-sibling" },
232
+ )
233
+
234
+ assert response .status_code == 200
235
+ assert response .json () == {"message" : "Document moved successfully." }
236
+ assert list (target .get_siblings ()) == [document , parent , target ]
237
+
238
+ document .refresh_from_db ()
239
+ assert list (document .get_children ()) == [child ]
240
+ assert document .accesses .count () == 3
241
+ assert document .accesses .get (user__isnull = False , role = "owner" ).user == parent_owner
242
+ assert document .accesses .get (user__isnull = True , role = "owner" ).team == "lasuite"
243
+ assert document .accesses .get (role = "administrator" ).user == user
244
+
245
+
246
+ def test_api_documents_move_authenticated_no_owner_same_user ():
247
+ """
248
+ Moving a document should not fail if the user moving a document with no owner was
249
+ at the same time owner of the previous root and has a role on the document being moved.
250
+ """
251
+ user = factories .UserFactory ()
252
+ client = APIClient ()
253
+ client .force_login (user )
254
+
255
+ parent = factories .DocumentFactory (
256
+ users = [(user , "owner" )], teams = [("lasuite" , "owner" )]
257
+ )
258
+ # A document with no owner
259
+ document = factories .DocumentFactory (parent = parent , users = [(user , "reader" )])
260
+ child = factories .DocumentFactory (parent = document )
261
+ target = factories .DocumentFactory ()
262
+
263
+ response = client .post (
264
+ f"/api/v1.0/documents/{ document .id !s} /move/" ,
265
+ data = {"target_document_id" : str (target .id ), "position" : "first-sibling" },
266
+ )
267
+
268
+ assert response .status_code == 200
269
+ assert response .json () == {"message" : "Document moved successfully." }
270
+ assert list (target .get_siblings ()) == [document , parent , target ]
271
+
272
+ document .refresh_from_db ()
273
+ assert list (document .get_children ()) == [child ]
274
+ assert document .accesses .count () == 2
275
+ assert document .accesses .get (user__isnull = False , role = "owner" ).user == user
276
+ assert document .accesses .get (user__isnull = True , role = "owner" ).team == "lasuite"
277
+
278
+
279
+ def test_api_documents_move_authenticated_no_owner_same_team ():
280
+ """
281
+ Moving a document should not fail if the team that is owner of the document root was
282
+ already declared on the document with a different role.
283
+ """
284
+ user = factories .UserFactory ()
285
+ client = APIClient ()
286
+ client .force_login (user )
287
+
288
+ parent = factories .DocumentFactory (teams = [("lasuite" , "owner" )])
289
+ # A document with no owner but same team
290
+ document = factories .DocumentFactory (
291
+ parent = parent , users = [(user , "administrator" )], teams = [("lasuite" , "reader" )]
292
+ )
293
+ child = factories .DocumentFactory (parent = document )
294
+ target = factories .DocumentFactory ()
295
+
296
+ response = client .post (
297
+ f"/api/v1.0/documents/{ document .id !s} /move/" ,
298
+ data = {"target_document_id" : str (target .id ), "position" : "first-sibling" },
299
+ )
300
+
301
+ assert response .status_code == 200
302
+ assert response .json () == {"message" : "Document moved successfully." }
303
+ assert list (target .get_siblings ()) == [document , parent , target ]
304
+
305
+ document .refresh_from_db ()
306
+ assert list (document .get_children ()) == [child ]
307
+ assert document .accesses .count () == 2
308
+ assert document .accesses .get (user__isnull = False , role = "administrator" ).user == user
309
+ assert document .accesses .get (user__isnull = True , role = "owner" ).team == "lasuite"
310
+
311
+
211
312
def test_api_documents_move_authenticated_deleted_document ():
212
313
"""
213
314
It should not be possible to move a deleted document or its descendants, even
0 commit comments