@@ -105,7 +105,7 @@ def test_api_document_accesses_create_authenticated_reader_or_editor(
105
105
106
106
@pytest .mark .parametrize ("depth" , [1 , 2 , 3 ])
107
107
@pytest .mark .parametrize ("via" , VIA )
108
- def test_api_document_accesses_create_authenticated_administrator (
108
+ def test_api_document_accesses_create_authenticated_administrator_share_to_user (
109
109
via , depth , mock_user_teams
110
110
):
111
111
"""
@@ -195,7 +195,90 @@ def test_api_document_accesses_create_authenticated_administrator(
195
195
196
196
@pytest .mark .parametrize ("depth" , [1 , 2 , 3 ])
197
197
@pytest .mark .parametrize ("via" , VIA )
198
- def test_api_document_accesses_create_authenticated_owner (via , depth , mock_user_teams ):
198
+ def test_api_document_accesses_create_authenticated_administrator_share_to_team (
199
+ via , depth , mock_user_teams
200
+ ):
201
+ """
202
+ Administrators of a document (direct or by heritage) should be able to create
203
+ document accesses except for the "owner" role.
204
+ An email should be sent to the accesses to notify them of the adding.
205
+ """
206
+ user = factories .UserFactory (with_owned_document = True )
207
+ client = APIClient ()
208
+ client .force_login (user )
209
+
210
+ documents = []
211
+ for i in range (depth ):
212
+ parent = documents [i - 1 ] if i > 0 else None
213
+ documents .append (factories .DocumentFactory (parent = parent ))
214
+
215
+ if via == USER :
216
+ factories .UserDocumentAccessFactory (
217
+ document = documents [0 ], user = user , role = "administrator"
218
+ )
219
+ elif via == TEAM :
220
+ mock_user_teams .return_value = ["lasuite" , "unknown" ]
221
+ factories .TeamDocumentAccessFactory (
222
+ document = documents [0 ], team = "lasuite" , role = "administrator"
223
+ )
224
+
225
+ other_user = factories .UserFactory (language = "en-us" )
226
+ document = documents [- 1 ]
227
+ response = client .post (
228
+ f"/api/v1.0/documents/{ document .id !s} /accesses/" ,
229
+ {
230
+ "team" : "new-team" ,
231
+ "role" : "owner" ,
232
+ },
233
+ format = "json" ,
234
+ )
235
+
236
+ assert response .status_code == 403
237
+ assert response .json () == {
238
+ "detail" : "Only owners of a document can assign other users as owners."
239
+ }
240
+
241
+ # It should be allowed to create a lower access
242
+ role = random .choice (
243
+ [role [0 ] for role in models .RoleChoices .choices if role [0 ] != "owner" ]
244
+ )
245
+
246
+ assert len (mail .outbox ) == 0
247
+
248
+ response = client .post (
249
+ f"/api/v1.0/documents/{ document .id !s} /accesses/" ,
250
+ {
251
+ "team" : "new-team" ,
252
+ "role" : role ,
253
+ },
254
+ format = "json" ,
255
+ )
256
+
257
+ assert response .status_code == 201
258
+ assert models .DocumentAccess .objects .filter (team = "new-team" ).count () == 1
259
+ new_document_access = models .DocumentAccess .objects .filter (team = "new-team" ).get ()
260
+ other_user = serializers .UserSerializer (instance = other_user ).data
261
+ assert response .json () == {
262
+ "abilities" : new_document_access .get_abilities (user ),
263
+ "document" : {
264
+ "id" : str (new_document_access .document_id ),
265
+ "depth" : new_document_access .document .depth ,
266
+ "path" : new_document_access .document .path ,
267
+ },
268
+ "id" : str (new_document_access .id ),
269
+ "user" : None ,
270
+ "team" : "new-team" ,
271
+ "role" : role ,
272
+ "max_ancestors_role" : None ,
273
+ }
274
+ assert len (mail .outbox ) == 0
275
+
276
+
277
+ @pytest .mark .parametrize ("depth" , [1 , 2 , 3 ])
278
+ @pytest .mark .parametrize ("via" , VIA )
279
+ def test_api_document_accesses_create_authenticated_owner_share_to_user (
280
+ via , depth , mock_user_teams
281
+ ):
199
282
"""
200
283
Owners of a document (direct or by heritage) should be able to create document accesses
201
284
whatever the role. An email should be sent to the accesses to notify them of the adding.
@@ -264,6 +347,70 @@ def test_api_document_accesses_create_authenticated_owner(via, depth, mock_user_
264
347
assert "docs/" + str (document .id ) + "/" in email_content
265
348
266
349
350
+ @pytest .mark .parametrize ("depth" , [1 , 2 , 3 ])
351
+ @pytest .mark .parametrize ("via" , VIA )
352
+ def test_api_document_accesses_create_authenticated_owner_share_to_team (
353
+ via , depth , mock_user_teams
354
+ ):
355
+ """
356
+ Owners of a document (direct or by heritage) should be able to create document accesses
357
+ whatever the role. An email should be sent to the accesses to notify them of the adding.
358
+ """
359
+ user = factories .UserFactory ()
360
+
361
+ client = APIClient ()
362
+ client .force_login (user )
363
+
364
+ documents = []
365
+ for i in range (depth ):
366
+ parent = documents [i - 1 ] if i > 0 else None
367
+ documents .append (factories .DocumentFactory (parent = parent ))
368
+
369
+ if via == USER :
370
+ factories .UserDocumentAccessFactory (
371
+ document = documents [0 ], user = user , role = "owner"
372
+ )
373
+ elif via == TEAM :
374
+ mock_user_teams .return_value = ["lasuite" , "unknown" ]
375
+ factories .TeamDocumentAccessFactory (
376
+ document = documents [0 ], team = "lasuite" , role = "owner"
377
+ )
378
+
379
+ other_user = factories .UserFactory (language = "en-us" )
380
+ document = documents [- 1 ]
381
+ role = random .choice ([role [0 ] for role in models .RoleChoices .choices ])
382
+
383
+ assert len (mail .outbox ) == 0
384
+
385
+ response = client .post (
386
+ f"/api/v1.0/documents/{ document .id !s} /accesses/" ,
387
+ {
388
+ "team" : "new-team" ,
389
+ "role" : role ,
390
+ },
391
+ format = "json" ,
392
+ )
393
+
394
+ assert response .status_code == 201
395
+ assert models .DocumentAccess .objects .filter (team = "new-team" ).count () == 1
396
+ new_document_access = models .DocumentAccess .objects .filter (team = "new-team" ).get ()
397
+ other_user = serializers .UserSerializer (instance = other_user ).data
398
+ assert response .json () == {
399
+ "document" : {
400
+ "id" : str (new_document_access .document_id ),
401
+ "path" : new_document_access .document .path ,
402
+ "depth" : new_document_access .document .depth ,
403
+ },
404
+ "id" : str (new_document_access .id ),
405
+ "user" : None ,
406
+ "team" : "new-team" ,
407
+ "role" : role ,
408
+ "max_ancestors_role" : None ,
409
+ "abilities" : new_document_access .get_abilities (user ),
410
+ }
411
+ assert len (mail .outbox ) == 0
412
+
413
+
267
414
@pytest .mark .parametrize ("via" , VIA )
268
415
def test_api_document_accesses_create_email_in_receivers_language (via , mock_user_teams ):
269
416
"""
0 commit comments