ソースを参照

add endpoint to enable/disable workspace notifications for user

Guénaël Muller 6 年 前
コミット
fb794b84bf
共有2 個のファイルを変更した502 個の追加1 個の削除を含む
  1. 439 1
      backend/tracim_backend/tests/functional/test_user.py
  2. 63 0
      backend/tracim_backend/views/core_api/user_controller.py

+ 439 - 1
backend/tracim_backend/tests/functional/test_user.py ファイルの表示

@@ -2382,6 +2382,444 @@ class TestUserSetWorkspaceAsRead(FunctionalTest):
2382 2382
         )
2383 2383
 
2384 2384
 
2385
+class TestUserEnableWorkspaceNotification(FunctionalTest):
2386
+    """
2387
+    Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/notify
2388
+    """
2389
+    def test_api_enable_user_workspace_notification__ok__200__admin(self):
2390
+        # init DB
2391
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
2392
+        admin = dbsession.query(models.User) \
2393
+            .filter(models.User.email == 'admin@admin.admin') \
2394
+            .one()
2395
+        workspace_api = WorkspaceApi(
2396
+            current_user=admin,
2397
+            session=dbsession,
2398
+            config=self.app_config
2399
+
2400
+        )
2401
+        workspace = WorkspaceApi(
2402
+            current_user=admin,
2403
+            session=dbsession,
2404
+            config=self.app_config,
2405
+        ).create_workspace(
2406
+            'test workspace',
2407
+            save_now=True
2408
+        )
2409
+        uapi = UserApi(
2410
+            current_user=admin,
2411
+            session=dbsession,
2412
+            config=self.app_config,
2413
+        )
2414
+        gapi = GroupApi(
2415
+            current_user=admin,
2416
+            session=dbsession,
2417
+            config=self.app_config,
2418
+        )
2419
+        groups = [gapi.get_one_with_name('users')]
2420
+        test_user = uapi.create_user(
2421
+            email='test@test.test',
2422
+            password='pass',
2423
+            name='bob',
2424
+            groups=groups,
2425
+            timezone='Europe/Paris',
2426
+            lang='fr',
2427
+            do_save=True,
2428
+            do_notify=False,
2429
+        )
2430
+        rapi = RoleApi(
2431
+            current_user=admin,
2432
+            session=dbsession,
2433
+            config=self.app_config,
2434
+        )
2435
+        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, with_notif=False)  # nopep8
2436
+        transaction.commit()
2437
+        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
2438
+        assert role.do_notify is False
2439
+        self.testapp.authorization = (
2440
+            'Basic',
2441
+            (
2442
+                'admin@admin.admin',
2443
+                'admin@admin.admin'
2444
+            )
2445
+        )
2446
+        self.testapp.put_json('/api/v2/users/{user_id}/workspaces/{workspace_id}/notify'.format(  # nopep8
2447
+            user_id=test_user.user_id,
2448
+            workspace_id=workspace.workspace_id
2449
+        ), status=204)
2450
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
2451
+        rapi = RoleApi(
2452
+            current_user=admin,
2453
+            session=dbsession,
2454
+            config=self.app_config,
2455
+        )
2456
+        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
2457
+        assert role.do_notify is True
2458
+
2459
+    def test_api_enable_user_workspace_notification__ok__200__user_itself(self):
2460
+        # init DB
2461
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
2462
+        admin = dbsession.query(models.User) \
2463
+            .filter(models.User.email == 'admin@admin.admin') \
2464
+            .one()
2465
+        workspace_api = WorkspaceApi(
2466
+            current_user=admin,
2467
+            session=dbsession,
2468
+            config=self.app_config
2469
+
2470
+        )
2471
+        workspace = WorkspaceApi(
2472
+            current_user=admin,
2473
+            session=dbsession,
2474
+            config=self.app_config,
2475
+        ).create_workspace(
2476
+            'test workspace',
2477
+            save_now=True
2478
+        )
2479
+        uapi = UserApi(
2480
+            current_user=admin,
2481
+            session=dbsession,
2482
+            config=self.app_config,
2483
+        )
2484
+        gapi = GroupApi(
2485
+            current_user=admin,
2486
+            session=dbsession,
2487
+            config=self.app_config,
2488
+        )
2489
+        groups = [gapi.get_one_with_name('users')]
2490
+        test_user = uapi.create_user(
2491
+            email='test@test.test',
2492
+            password='pass',
2493
+            name='bob',
2494
+            groups=groups,
2495
+            timezone='Europe/Paris',
2496
+            lang='fr',
2497
+            do_save=True,
2498
+            do_notify=False,
2499
+        )
2500
+        rapi = RoleApi(
2501
+            current_user=admin,
2502
+            session=dbsession,
2503
+            config=self.app_config,
2504
+        )
2505
+        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, with_notif=False)  # nopep8
2506
+        transaction.commit()
2507
+        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
2508
+        assert role.do_notify is False
2509
+        self.testapp.authorization = (
2510
+            'Basic',
2511
+            (
2512
+                'test@test.test',
2513
+                'pass',
2514
+            )
2515
+        )
2516
+        self.testapp.put_json('/api/v2/users/{user_id}/workspaces/{workspace_id}/notify'.format(  # nopep8
2517
+            user_id=test_user.user_id,
2518
+            workspace_id=workspace.workspace_id
2519
+        ), status=204)
2520
+
2521
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
2522
+        rapi = RoleApi(
2523
+            current_user=admin,
2524
+            session=dbsession,
2525
+            config=self.app_config,
2526
+        )
2527
+        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
2528
+        assert role.do_notify is True
2529
+
2530
+    def test_api_enable_user_workspace_notification__err__403__other_user(self):
2531
+        # init DB
2532
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
2533
+        admin = dbsession.query(models.User) \
2534
+            .filter(models.User.email == 'admin@admin.admin') \
2535
+            .one()
2536
+        workspace_api = WorkspaceApi(
2537
+            current_user=admin,
2538
+            session=dbsession,
2539
+            config=self.app_config
2540
+
2541
+        )
2542
+        workspace = WorkspaceApi(
2543
+            current_user=admin,
2544
+            session=dbsession,
2545
+            config=self.app_config,
2546
+        ).create_workspace(
2547
+            'test workspace',
2548
+            save_now=True
2549
+        )
2550
+        uapi = UserApi(
2551
+            current_user=admin,
2552
+            session=dbsession,
2553
+            config=self.app_config,
2554
+        )
2555
+        gapi = GroupApi(
2556
+            current_user=admin,
2557
+            session=dbsession,
2558
+            config=self.app_config,
2559
+        )
2560
+        groups = [gapi.get_one_with_name('users')]
2561
+        test_user = uapi.create_user(
2562
+            email='test@test.test',
2563
+            password='pass',
2564
+            name='bob',
2565
+            groups=groups,
2566
+            timezone='Europe/Paris',
2567
+            lang='fr',
2568
+            do_save=True,
2569
+            do_notify=False,
2570
+        )
2571
+        test_user2 = uapi.create_user(
2572
+            email='test2@test2.test2',
2573
+            password='pass',
2574
+            name='boby',
2575
+            groups=groups,
2576
+            timezone='Europe/Paris',
2577
+            lang='fr',
2578
+            do_save=True,
2579
+            do_notify=False,
2580
+        )
2581
+        rapi = RoleApi(
2582
+            current_user=admin,
2583
+            session=dbsession,
2584
+            config=self.app_config,
2585
+        )
2586
+        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, with_notif=False)  # nopep8
2587
+        rapi.create_one(test_user2, workspace, UserRoleInWorkspace.READER, with_notif=False)  # nopep8
2588
+        transaction.commit()
2589
+        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
2590
+        assert role.do_notify is False
2591
+        self.testapp.authorization = (
2592
+            'Basic',
2593
+            (
2594
+                'test2@test2.test2',
2595
+                'pass',
2596
+            )
2597
+        )
2598
+        self.testapp.put_json('/api/v2/users/{user_id}/workspaces/{workspace_id}/notify'.format(  # nopep8
2599
+            user_id=test_user.user_id,
2600
+            workspace_id=workspace.workspace_id
2601
+        ), status=403)
2602
+
2603
+
2604
+class TestUserDisableWorkspaceNotification(FunctionalTest):
2605
+    """
2606
+    Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/unnotify
2607
+    """
2608
+    def test_api_disable_user_workspace_notification__ok__200__admin(self):
2609
+        # init DB
2610
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
2611
+        admin = dbsession.query(models.User) \
2612
+            .filter(models.User.email == 'admin@admin.admin') \
2613
+            .one()
2614
+        workspace_api = WorkspaceApi(
2615
+            current_user=admin,
2616
+            session=dbsession,
2617
+            config=self.app_config
2618
+
2619
+        )
2620
+        workspace = WorkspaceApi(
2621
+            current_user=admin,
2622
+            session=dbsession,
2623
+            config=self.app_config,
2624
+        ).create_workspace(
2625
+            'test workspace',
2626
+            save_now=True
2627
+        )
2628
+        uapi = UserApi(
2629
+            current_user=admin,
2630
+            session=dbsession,
2631
+            config=self.app_config,
2632
+        )
2633
+        gapi = GroupApi(
2634
+            current_user=admin,
2635
+            session=dbsession,
2636
+            config=self.app_config,
2637
+        )
2638
+        groups = [gapi.get_one_with_name('users')]
2639
+        test_user = uapi.create_user(
2640
+            email='test@test.test',
2641
+            password='pass',
2642
+            name='bob',
2643
+            groups=groups,
2644
+            timezone='Europe/Paris',
2645
+            lang='fr',
2646
+            do_save=True,
2647
+            do_notify=True,
2648
+        )
2649
+        rapi = RoleApi(
2650
+            current_user=admin,
2651
+            session=dbsession,
2652
+            config=self.app_config,
2653
+        )
2654
+        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, with_notif=True)  # nopep8
2655
+        transaction.commit()
2656
+        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
2657
+        assert role.do_notify is True
2658
+        self.testapp.authorization = (
2659
+            'Basic',
2660
+            (
2661
+                'admin@admin.admin',
2662
+                'admin@admin.admin'
2663
+            )
2664
+        )
2665
+        self.testapp.put_json('/api/v2/users/{user_id}/workspaces/{workspace_id}/unnotify'.format(  # nopep8
2666
+            user_id=test_user.user_id,
2667
+            workspace_id=workspace.workspace_id
2668
+        ), status=204)
2669
+
2670
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
2671
+        rapi = RoleApi(
2672
+            current_user=admin,
2673
+            session=dbsession,
2674
+            config=self.app_config,
2675
+        )
2676
+        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
2677
+        assert role.do_notify is False
2678
+
2679
+    def test_api_enable_user_workspace_notification__ok__200__user_itself(self):
2680
+        # init DB
2681
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
2682
+        admin = dbsession.query(models.User) \
2683
+            .filter(models.User.email == 'admin@admin.admin') \
2684
+            .one()
2685
+        workspace_api = WorkspaceApi(
2686
+            current_user=admin,
2687
+            session=dbsession,
2688
+            config=self.app_config
2689
+
2690
+        )
2691
+        workspace = WorkspaceApi(
2692
+            current_user=admin,
2693
+            session=dbsession,
2694
+            config=self.app_config,
2695
+        ).create_workspace(
2696
+            'test workspace',
2697
+            save_now=True
2698
+        )
2699
+        uapi = UserApi(
2700
+            current_user=admin,
2701
+            session=dbsession,
2702
+            config=self.app_config,
2703
+        )
2704
+        gapi = GroupApi(
2705
+            current_user=admin,
2706
+            session=dbsession,
2707
+            config=self.app_config,
2708
+        )
2709
+        groups = [gapi.get_one_with_name('users')]
2710
+        test_user = uapi.create_user(
2711
+            email='test@test.test',
2712
+            password='pass',
2713
+            name='bob',
2714
+            groups=groups,
2715
+            timezone='Europe/Paris',
2716
+            lang='fr',
2717
+            do_save=True,
2718
+            do_notify=False,
2719
+        )
2720
+        rapi = RoleApi(
2721
+            current_user=admin,
2722
+            session=dbsession,
2723
+            config=self.app_config,
2724
+        )
2725
+        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, with_notif=True)  # nopep8
2726
+        transaction.commit()
2727
+        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
2728
+        assert role.do_notify is True
2729
+        self.testapp.authorization = (
2730
+            'Basic',
2731
+            (
2732
+                'test@test.test',
2733
+                'pass',
2734
+            )
2735
+        )
2736
+        self.testapp.put_json('/api/v2/users/{user_id}/workspaces/{workspace_id}/unnotify'.format(  # nopep8
2737
+            user_id=test_user.user_id,
2738
+            workspace_id=workspace.workspace_id
2739
+        ), status=204)
2740
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
2741
+        rapi = RoleApi(
2742
+            current_user=admin,
2743
+            session=dbsession,
2744
+            config=self.app_config,
2745
+        )
2746
+        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
2747
+        assert role.do_notify is False
2748
+
2749
+    def test_api_disable_user_workspace_notification__err__403__other_user(self):
2750
+        # init DB
2751
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
2752
+        admin = dbsession.query(models.User) \
2753
+            .filter(models.User.email == 'admin@admin.admin') \
2754
+            .one()
2755
+        workspace_api = WorkspaceApi(
2756
+            current_user=admin,
2757
+            session=dbsession,
2758
+            config=self.app_config
2759
+
2760
+        )
2761
+        workspace = WorkspaceApi(
2762
+            current_user=admin,
2763
+            session=dbsession,
2764
+            config=self.app_config,
2765
+        ).create_workspace(
2766
+            'test workspace',
2767
+            save_now=True
2768
+        )
2769
+        uapi = UserApi(
2770
+            current_user=admin,
2771
+            session=dbsession,
2772
+            config=self.app_config,
2773
+        )
2774
+        gapi = GroupApi(
2775
+            current_user=admin,
2776
+            session=dbsession,
2777
+            config=self.app_config,
2778
+        )
2779
+        groups = [gapi.get_one_with_name('users')]
2780
+        test_user = uapi.create_user(
2781
+            email='test@test.test',
2782
+            password='pass',
2783
+            name='bob',
2784
+            groups=groups,
2785
+            timezone='Europe/Paris',
2786
+            lang='fr',
2787
+            do_save=True,
2788
+            do_notify=False,
2789
+        )
2790
+        test_user2 = uapi.create_user(
2791
+            email='test2@test2.test2',
2792
+            password='pass',
2793
+            name='boby',
2794
+            groups=groups,
2795
+            timezone='Europe/Paris',
2796
+            lang='fr',
2797
+            do_save=True,
2798
+            do_notify=False,
2799
+        )
2800
+        rapi = RoleApi(
2801
+            current_user=admin,
2802
+            session=dbsession,
2803
+            config=self.app_config,
2804
+        )
2805
+        rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, with_notif=True)  # nopep8
2806
+        rapi.create_one(test_user2, workspace, UserRoleInWorkspace.READER, with_notif=False)  # nopep8
2807
+        transaction.commit()
2808
+        role = rapi.get_one(test_user.user_id, workspace.workspace_id)
2809
+        assert role.do_notify is True
2810
+        self.testapp.authorization = (
2811
+            'Basic',
2812
+            (
2813
+                'test2@test2.test2',
2814
+                'pass',
2815
+            )
2816
+        )
2817
+        self.testapp.put_json('/api/v2/users/{user_id}/workspaces/{workspace_id}/unnotify'.format(  # nopep8
2818
+            user_id=test_user.user_id,
2819
+            workspace_id=workspace.workspace_id
2820
+        ), status=403)
2821
+
2822
+
2385 2823
 class TestUserWorkspaceEndpoint(FunctionalTest):
2386 2824
     """
2387 2825
     Tests for /api/v2/users/{user_id}/workspaces
@@ -4235,7 +4673,7 @@ class TestSetUserInfoEndpoint(FunctionalTest):
4235 4673
         params = {
4236 4674
             'public_name': 'updated',
4237 4675
             'timezone': 'Europe/London',
4238
-            'lang' : 'en',
4676
+            'lang': 'en',
4239 4677
         }
4240 4678
         self.testapp.put_json(
4241 4679
             '/api/v2/users/{}'.format(user_id),

+ 63 - 0
backend/tracim_backend/views/core_api/user_controller.py ファイルの表示

@@ -1,4 +1,5 @@
1 1
 from pyramid.config import Configurator
2
+from tracim_backend.lib.core.userworkspace import RoleApi
2 3
 from tracim_backend.lib.utils.utils import password_generator
3 4
 
4 5
 try:  # Python 3.5+
@@ -458,6 +459,60 @@ class UserController(Controller):
458 459
         api.mark_read__workspace(request.current_workspace)
459 460
         return
460 461
 
462
+    @hapic.with_api_doc(tags=[SWAGGER_TAG__USER_ENDPOINTS])
463
+    @require_same_user_or_profile(Group.TIM_ADMIN)
464
+    @hapic.input_path(UserWorkspaceIdPathSchema())
465
+    @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.NO_CONTENT)  # nopep8
466
+    def enable_workspace_notification(self, context, request: TracimRequest, hapic_data=None):  # nopep8
467
+        """
468
+        enable workspace notification
469
+        """
470
+        app_config = request.registry.settings['CFG']
471
+        api = ContentApi(
472
+            current_user=request.candidate_user,  # User
473
+            session=request.dbsession,
474
+            config=app_config,
475
+        )
476
+        wapi = WorkspaceApi(
477
+            current_user=request.candidate_user,  # User
478
+            session=request.dbsession,
479
+            config=app_config,
480
+        )
481
+        workspace = wapi.get_one(hapic_data.path.workspace_id)
482
+        wapi.enable_notifications(request.candidate_user, workspace)
483
+        rapi = RoleApi(
484
+            current_user=request.candidate_user,  # User
485
+            session=request.dbsession,
486
+            config=app_config,
487
+        )
488
+        role = rapi.get_one(request.candidate_user.user_id, workspace.workspace_id)
489
+        wapi.save(workspace)
490
+        return
491
+
492
+    @hapic.with_api_doc(tags=[SWAGGER_TAG__USER_ENDPOINTS])
493
+    @require_same_user_or_profile(Group.TIM_ADMIN)
494
+    @hapic.input_path(UserWorkspaceIdPathSchema())
495
+    @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.NO_CONTENT)  # nopep8
496
+    def disable_workspace_notification(self, context, request: TracimRequest, hapic_data=None):  # nopep8
497
+        """
498
+        disable workspace notification
499
+        """
500
+        app_config = request.registry.settings['CFG']
501
+        api = ContentApi(
502
+            current_user=request.candidate_user,  # User
503
+            session=request.dbsession,
504
+            config=app_config,
505
+        )
506
+        wapi = WorkspaceApi(
507
+            current_user=request.candidate_user,  # User
508
+            session=request.dbsession,
509
+            config=app_config,
510
+        )
511
+        workspace = wapi.get_one(hapic_data.path.workspace_id)
512
+        wapi.disable_notifications(request.candidate_user, workspace)
513
+        wapi.save(workspace)
514
+        return
515
+
461 516
     def bind(self, configurator: Configurator) -> None:
462 517
         """
463 518
         Create all routes and views using pyramid configurator
@@ -532,3 +587,11 @@ class UserController(Controller):
532 587
         # set workspace as read
533 588
         configurator.add_route('read_workspace', '/users/{user_id}/workspaces/{workspace_id}/read', request_method='PUT')  # nopep8
534 589
         configurator.add_view(self.set_workspace_as_read, route_name='read_workspace')  # nopep8
590
+
591
+        # enable workspace notification
592
+        configurator.add_route('enable_workspace_notification', '/users/{user_id}/workspaces/{workspace_id}/notify', request_method='PUT')  # nopep8
593
+        configurator.add_view(self.enable_workspace_notification, route_name='enable_workspace_notification')  # nopep8
594
+
595
+        # enable workspace notification
596
+        configurator.add_route('disable_workspace_notification', '/users/{user_id}/workspaces/{workspace_id}/unnotify', request_method='PUT')  # nopep8
597
+        configurator.add_view(self.disable_workspace_notification, route_name='disable_workspace_notification')  # nopep8