Browse Source

add few tests for read_status endpoint

Guénaël Muller 6 years ago
parent
commit
f9c0e00919
1 changed files with 192 additions and 0 deletions
  1. 192 0
      tracim/tests/functional/test_user.py

+ 192 - 0
tracim/tests/functional/test_user.py View File

@@ -4,6 +4,7 @@ Tests for /api/v2/users subpath endpoints.
4 4
 """
5 5
 from time import sleep
6 6
 
7
+import pytest
7 8
 import transaction
8 9
 
9 10
 from tracim import models
@@ -240,6 +241,197 @@ class TestUserRecentlyActiveContentEndpoint(FunctionalTest):
240 241
         assert res[1]['content_id'] == secondly_created_but_not_updated.content_id
241 242
 
242 243
 
244
+class TestUserReadStatusEndpoint(FunctionalTest):
245
+
246
+    def test_api__get_read_status__ok__200__all(self):
247
+
248
+        # init DB
249
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
250
+        admin = dbsession.query(models.User) \
251
+            .filter(models.User.email == 'admin@admin.admin') \
252
+            .one()
253
+        workspace_api = WorkspaceApi(
254
+            current_user=admin,
255
+            session=dbsession,
256
+            config=self.app_config
257
+
258
+        )
259
+        workspace = WorkspaceApi(
260
+            current_user=admin,
261
+            session=dbsession,
262
+            config=self.app_config,
263
+        ).create_workspace(
264
+            'test workspace',
265
+            save_now=True
266
+        )
267
+        workspace2 = WorkspaceApi(
268
+            current_user=admin,
269
+            session=dbsession,
270
+            config=self.app_config,
271
+        ).create_workspace(
272
+            'test workspace2',
273
+            save_now=True
274
+        )
275
+
276
+        api = ContentApi(
277
+            current_user=admin,
278
+            session=dbsession,
279
+            config=self.app_config,
280
+        )
281
+        main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True)  # nopep8
282
+        main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True)  # nopep8
283
+        # creation order test
284
+        firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
285
+        secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
286
+        # update order test
287
+        firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True)  # nopep8
288
+        secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
289
+        with new_revision(
290
+            session=dbsession,
291
+            tm=transaction.manager,
292
+            content=firstly_created_but_recently_updated,
293
+        ):
294
+            firstly_created_but_recently_updated.description = 'Just an update'
295
+        api.save(firstly_created_but_recently_updated)
296
+        # comment change order
297
+        firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
298
+        secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
299
+        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
300
+        content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True)  # nopep8
301
+        dbsession.flush()
302
+        transaction.commit()
303
+
304
+        self.testapp.authorization = (
305
+            'Basic',
306
+            (
307
+                'admin@admin.admin',
308
+                'admin@admin.admin'
309
+            )
310
+        )
311
+        res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
312
+        res = res.json_body
313
+        assert len(res) == 7
314
+        for elem in res:
315
+            assert isinstance(elem['content_id'], int)
316
+            assert isinstance(elem['read_by_user'], bool)
317
+        # comment is newest than page2
318
+        assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
319
+        assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
320
+        # last updated content is newer than other one despite creation
321
+        # of the other is more recent
322
+        assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
323
+        assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
324
+        # creation order is inverted here as last created is last active
325
+        assert res[4]['content_id'] == secondly_created.content_id
326
+        assert res[5]['content_id'] == firstly_created.content_id
327
+        # folder subcontent modification does not change folder order
328
+        assert res[6]['content_id'] == main_folder.content_id
329
+
330
+    @pytest.mark.xfail(reason='List of item in path bug need to be fixed')
331
+    def test_api__get_read_status__ok__200__nominal_case(self):
332
+
333
+        # init DB
334
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
335
+        admin = dbsession.query(models.User) \
336
+            .filter(models.User.email == 'admin@admin.admin') \
337
+            .one()
338
+        workspace_api = WorkspaceApi(
339
+            current_user=admin,
340
+            session=dbsession,
341
+            config=self.app_config
342
+
343
+        )
344
+        workspace = WorkspaceApi(
345
+            current_user=admin,
346
+            session=dbsession,
347
+            config=self.app_config,
348
+        ).create_workspace(
349
+            'test workspace',
350
+            save_now=True
351
+        )
352
+        workspace2 = WorkspaceApi(
353
+            current_user=admin,
354
+            session=dbsession,
355
+            config=self.app_config,
356
+        ).create_workspace(
357
+            'test workspace2',
358
+            save_now=True
359
+        )
360
+
361
+        api = ContentApi(
362
+            current_user=admin,
363
+            session=dbsession,
364
+            config=self.app_config,
365
+        )
366
+        main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True)  # nopep8
367
+        main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True)  # nopep8
368
+        # creation order test
369
+        firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True)  # nopep8
370
+        secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True)  # nopep8
371
+        # update order test
372
+        firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True)  # nopep8
373
+        secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True)  # nopep8
374
+        with new_revision(
375
+            session=dbsession,
376
+            tm=transaction.manager,
377
+            content=firstly_created_but_recently_updated,
378
+        ):
379
+            firstly_created_but_recently_updated.description = 'Just an update'
380
+        api.save(firstly_created_but_recently_updated)
381
+        # comment change order
382
+        firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True)  # nopep8
383
+        secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True)  # nopep8
384
+        comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True)  # nopep8
385
+        content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True)  # nopep8
386
+        dbsession.flush()
387
+        transaction.commit()
388
+
389
+        self.testapp.authorization = (
390
+            'Basic',
391
+            (
392
+                'admin@admin.admin',
393
+                'admin@admin.admin'
394
+            )
395
+        )
396
+        selected_contents_id = [
397
+            firstly_created_but_recently_commented.content_id,
398
+            firstly_created_but_recently_updated.content_id,
399
+            firstly_created.content_id,
400
+            main_folder.content_id,
401
+        ]
402
+
403
+        content_id_str = '[{0},{1},{2},{3}]'.format(
404
+            selected_contents_id[0],
405
+            selected_contents_id[1],
406
+            selected_contents_id[2],
407
+            selected_contents_id[3],
408
+        )
409
+        params = {
410
+            'contents_ids': content_id_str
411
+        }
412
+        res = self.testapp.get(
413
+            '/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id),  # nopep8
414
+            status=200,
415
+            params=params
416
+        )
417
+        res = res.json_body
418
+        assert len(res) == 4
419
+        for elem in res:
420
+            assert isinstance(elem['content_id'], int)
421
+            assert isinstance(elem['read_by_user'], bool)
422
+        # comment is newest than page2
423
+        assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
424
+        assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
425
+        # last updated content is newer than other one despite creation
426
+        # of the other is more recent
427
+        assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
428
+        assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
429
+        # creation order is inverted here as last created is last active
430
+        assert res[4]['content_id'] == secondly_created.content_id
431
+        assert res[5]['content_id'] == firstly_created.content_id
432
+        # folder subcontent modification does not change folder order
433
+        assert res[6]['content_id'] == main_folder.content_id
434
+
243 435
 class TestUserWorkspaceEndpoint(FunctionalTest):
244 436
     """
245 437
     Tests for /api/v2/users/{user_id}/workspaces