def test_api__try_whoami_enpoint__err_401__user_is_not_active(self):
        dbsession = get_tm_session(self.session_factory, transaction.manager)
        admin = dbsession.query(models.User) \
            .filter(models.User.email == '*****@*****.**') \
            .one()
        uapi = UserApi(
            current_user=admin,
            session=dbsession,
            config=self.app_config,
        )
        gapi = GroupApi(
            current_user=admin,
            session=dbsession,
            config=self.app_config,
        )
        groups = [gapi.get_one_with_name('users')]
        test_user = uapi.create_user(
            email='*****@*****.**',
            password='******',
            name='bob',
            groups=groups,
            timezone='Europe/Paris',
            do_save=True,
            do_notify=False,
        )
        uapi.save(test_user)
        uapi.disable(test_user)
        transaction.commit()
        self.testapp.authorization = ('Basic', ('*****@*****.**', 'pass'))

        res = self.testapp.get('/api/v2/sessions/whoami', status=401)
 def test_unit__get_one_with_name__err__group_not_exist(self) -> None:
     """
     get one group by name who does not exist
     """
     api = GroupApi(
         current_user=None,
         session=self.session,
         config=self.app_config,
     )
     with pytest.raises(GroupDoesNotExist):
         group = api.get_one_with_name('unknown_group')
 def test_unit__get_one_group_with_name__nominal_case(self) -> None:
     """
     get one group by name
     """
     api = GroupApi(
         current_user=None,
         session=self.session,
         config=self.app_config,
     )
     group = api.get_one_with_name('administrators')
     assert group.group_id == 3
     assert group.group_name == 'administrators'
 def set_profile(self, context, request: TracimRequest, hapic_data=None):
     """
     set user profile
     """
     app_config = request.registry.settings['CFG']
     uapi = UserApi(
         current_user=request.current_user,  # User
         session=request.dbsession,
         config=app_config,
     )
     gapi = GroupApi(
         current_user=request.current_user,  # User
         session=request.dbsession,
         config=app_config,
     )
     groups = [gapi.get_one_with_name(hapic_data.body.profile)]
     uapi.update(
         user=request.candidate_user,
         groups=groups,
         do_save=True,
     )
     return
    def test_api__try_login_enpoint__err_401__user_not_activated(self):
        dbsession = get_tm_session(self.session_factory, transaction.manager)
        admin = dbsession.query(models.User) \
            .filter(models.User.email == '*****@*****.**') \
            .one()
        uapi = UserApi(
            current_user=admin,
            session=dbsession,
            config=self.app_config,
        )
        gapi = GroupApi(
            current_user=admin,
            session=dbsession,
            config=self.app_config,
        )
        groups = [gapi.get_one_with_name('users')]
        test_user = uapi.create_user(
            email='*****@*****.**',
            password='******',
            name='bob',
            groups=groups,
            timezone='Europe/Paris',
            do_save=True,
            do_notify=False,
        )
        uapi.save(test_user)
        uapi.disable(test_user)
        transaction.commit()

        params = {
            'email': '*****@*****.**',
            'password': '******',
        }
        res = self.testapp.post_json(
            '/api/v2/sessions/login',
            params=params,
            status=403,
        )
 def create_user(self, context, request: TracimRequest, hapic_data=None):
     """
     Create new user
     """
     app_config = request.registry.settings['CFG']
     uapi = UserApi(
         current_user=request.current_user,  # User
         session=request.dbsession,
         config=app_config,
     )
     gapi = GroupApi(
         current_user=request.current_user,  # User
         session=request.dbsession,
         config=app_config,
     )
     groups = [gapi.get_one_with_name(hapic_data.body.profile)]
     user = uapi.create_user(email=hapic_data.body.email,
                             password=hapic_data.body.password,
                             timezone=hapic_data.body.timezone,
                             name=hapic_data.body.public_name,
                             do_notify=hapic_data.body.email_notification,
                             groups=groups,
                             do_save=True)
     return uapi.get_user_with_context(user)
 def test_unit__authenticate_user___err__user_not_active(self):
     api = UserApi(
         current_user=None,
         session=self.session,
         config=self.config,
     )
     gapi = GroupApi(
         current_user=None,
         session=self.session,
         config=self.config,
     )
     groups = [gapi.get_one_with_name('users')]
     user = api.create_user(
         email='*****@*****.**',
         password='******',
         name='bob',
         groups=groups,
         timezone='Europe/Paris',
         do_save=True,
         do_notify=False,
     )
     api.disable(user)
     with pytest.raises(UserNotActive):
         api.authenticate_user('*****@*****.**', '*****@*****.**')