Browse Source

add delete/undelete user endpoints + nominal case tests

Guénaël Muller 6 years ago
parent
commit
e8890eee35

+ 16 - 1
backend/tracim_backend/lib/core/user.py View File

@@ -34,13 +34,18 @@ class UserApi(object):
34 34
             current_user: typing.Optional[User],
35 35
             session: Session,
36 36
             config: CFG,
37
+            show_deleted: bool = False,
37 38
     ) -> None:
38 39
         self._session = session
39 40
         self._user = current_user
40 41
         self._config = config
42
+        self._show_deleted = show_deleted
41 43
 
42 44
     def _base_query(self):
43
-        return self._session.query(User)
45
+        query = self._session.query(User)
46
+        if not self._show_deleted:
47
+            query = query.filter(User.is_deleted == False)
48
+        return query
44 49
 
45 50
     def get_user_with_context(self, user: User) -> UserInContext:
46 51
         """
@@ -382,6 +387,16 @@ class UserApi(object):
382 387
         if do_save:
383 388
             self.save(user)
384 389
 
390
+    def delete(self, user: User, do_save=False):
391
+        user.is_deleted = True
392
+        if do_save:
393
+            self.save(user)
394
+
395
+    def undelete(self, user: User, do_save=False):
396
+        user.is_deleted = False
397
+        if do_save:
398
+            self.save(user)
399
+
385 400
     def save(self, user: User):
386 401
         self._session.flush()
387 402
 

+ 1 - 1
backend/tracim_backend/lib/utils/request.py View File

@@ -293,7 +293,7 @@ class TracimRequest(Request):
293 293
         :return: user found from header/body
294 294
         """
295 295
         app_config = request.registry.settings['CFG']
296
-        uapi = UserApi(None, session=request.dbsession, config=app_config)
296
+        uapi = UserApi(None, show_deleted=True, session=request.dbsession, config=app_config)
297 297
         login = ''
298 298
         try:
299 299
             login = None

+ 46 - 0
backend/tracim_backend/tests/functional/test_user.py View File

@@ -2636,6 +2636,52 @@ class TestUserEndpoint(FunctionalTest):
2636 2636
             status=403
2637 2637
         )
2638 2638
 
2639
+    def test_api_delete_user__ok_200__admin(self):
2640
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
2641
+        admin = dbsession.query(models.User) \
2642
+            .filter(models.User.email == 'admin@admin.admin') \
2643
+            .one()
2644
+        uapi = UserApi(
2645
+            current_user=admin,
2646
+            session=dbsession,
2647
+            config=self.app_config,
2648
+        )
2649
+        gapi = GroupApi(
2650
+            current_user=admin,
2651
+            session=dbsession,
2652
+            config=self.app_config,
2653
+        )
2654
+        groups = [gapi.get_one_with_name('users')]
2655
+        test_user = uapi.create_user(
2656
+            email='test@test.test',
2657
+            password='pass',
2658
+            name='bob',
2659
+            groups=groups,
2660
+            timezone='Europe/Paris',
2661
+            do_save=True,
2662
+            do_notify=False,
2663
+        )
2664
+        uapi.save(test_user)
2665
+        transaction.commit()
2666
+        user_id = int(test_user.user_id)
2667
+
2668
+        self.testapp.authorization = (
2669
+            'Basic',
2670
+            (
2671
+                'admin@admin.admin',
2672
+                'admin@admin.admin'
2673
+            )
2674
+        )
2675
+        self.testapp.put(
2676
+            '/api/v2/users/{}/delete'.format(user_id),
2677
+            status=204
2678
+        )
2679
+        res = self.testapp.get(
2680
+            '/api/v2/users/{}'.format(user_id),
2681
+            status=200
2682
+        ).json_body
2683
+        assert res['is_deleted'] is True
2684
+
2639 2685
 
2640 2686
 class TestUsersEndpoint(FunctionalTest):
2641 2687
     # -*- coding: utf-8 -*-

+ 43 - 0
backend/tracim_backend/views/core_api/user_controller.py View File

@@ -244,6 +244,41 @@ class UserController(Controller):
244 244
     @require_profile(Group.TIM_ADMIN)
245 245
     @hapic.input_path(UserIdPathSchema())
246 246
     @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.NO_CONTENT)  # nopep8
247
+    def delete_user(self, context, request: TracimRequest, hapic_data=None):
248
+        """
249
+        delete user
250
+        """
251
+        app_config = request.registry.settings['CFG']
252
+        uapi = UserApi(
253
+            current_user=request.current_user,  # User
254
+            session=request.dbsession,
255
+            config=app_config,
256
+        )
257
+        uapi.delete(user=request.candidate_user, do_save=True)
258
+        return
259
+
260
+    @hapic.with_api_doc(tags=[SWAGGER_TAG__USER_ENDPOINTS])
261
+    @require_profile(Group.TIM_ADMIN)
262
+    @hapic.input_path(UserIdPathSchema())
263
+    @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.NO_CONTENT)  # nopep8
264
+    def undelete_user(self, context, request: TracimRequest, hapic_data=None):
265
+        """
266
+        undelete user
267
+        """
268
+        app_config = request.registry.settings['CFG']
269
+        uapi = UserApi(
270
+            current_user=request.current_user,  # User
271
+            session=request.dbsession,
272
+            config=app_config,
273
+            show_deleted=True,
274
+        )
275
+        uapi.undelete(user=request.candidate_user, do_save=True)
276
+        return
277
+
278
+    @hapic.with_api_doc(tags=[SWAGGER_TAG__USER_ENDPOINTS])
279
+    @require_profile(Group.TIM_ADMIN)
280
+    @hapic.input_path(UserIdPathSchema())
281
+    @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.NO_CONTENT)  # nopep8
247 282
     def disable_user(self, context, request: TracimRequest, hapic_data=None):
248 283
         """
249 284
         disable user
@@ -464,6 +499,14 @@ class UserController(Controller):
464 499
         configurator.add_route('disable_user', '/users/{user_id}/disable', request_method='PUT')  # nopep8
465 500
         configurator.add_view(self.disable_user, route_name='disable_user')
466 501
 
502
+        # delete user
503
+        configurator.add_route('delete_user', '/users/{user_id}/delete', request_method='PUT')  # nopep8
504
+        configurator.add_view(self.delete_user, route_name='delete_user')
505
+
506
+        # undelete user
507
+        configurator.add_route('undelete_user', '/users/{user_id}/undelete', request_method='PUT')  # nopep8
508
+        configurator.add_view(self.undelete_user, route_name='undelete_user')
509
+
467 510
         # set user profile
468 511
         configurator.add_route('set_user_profile', '/users/{user_id}/profile', request_method='PUT')  # nopep8
469 512
         configurator.add_view(self.set_profile, route_name='set_user_profile')