def test_push_on_locked_repo_by_other_user_hg(self, webserver):
        #clone some temp
        DEST = _get_tmp_dir()
        clone_url = webserver.repo_url(HG_REPO)
        stdout, stderr = Command(TESTS_TMP_PATH).execute(
            'hg clone', clone_url, DEST)

        #lock repo
        r = Repository.get_by_repo_name(HG_REPO)
        # let this user actually push !
        RepoModel().grant_user_permission(repo=r,
                                          user=TEST_USER_REGULAR_LOGIN,
                                          perm='repository.write')
        Session().commit()
        Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)

        #push fails repo is locked by other user !
        stdout, stderr = _add_files_and_push(webserver,
                                             'hg',
                                             DEST,
                                             username=TEST_USER_REGULAR_LOGIN,
                                             password=TEST_USER_REGULAR_PASS,
                                             ignoreReturnCode=True)
        msg = (
            """abort: HTTP Error 423: Repository `%s` locked by user `%s`""" %
            (HG_REPO, TEST_USER_ADMIN_LOGIN))
        assert msg in stderr
    def test_push_on_locked_repo_by_other_user_git(self, webserver):
        # Note: Git hooks must be executable on unix. This test will thus fail
        # for example on Linux if /tmp is mounted noexec.

        #clone some temp
        DEST = _get_tmp_dir()
        clone_url = webserver.repo_url(GIT_REPO)
        stdout, stderr = Command(TESTS_TMP_PATH).execute('git clone', clone_url, DEST)

        #lock repo
        r = Repository.get_by_repo_name(GIT_REPO)
        # let this user actually push !
        RepoModel().grant_user_permission(repo=r, user=TEST_USER_REGULAR_LOGIN,
                                          perm='repository.write')
        Session().commit()
        Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)

        #push fails repo is locked by other user !
        stdout, stderr = _add_files_and_push(webserver, 'git', DEST,
                                             username=TEST_USER_REGULAR_LOGIN,
                                             password=TEST_USER_REGULAR_PASS,
                                             ignoreReturnCode=True)
        err = 'Repository `%s` locked by user `%s`' % (GIT_REPO, TEST_USER_ADMIN_LOGIN)
        assert err in stderr

        #TODO: fix this somehow later on Git, Git is stupid and even if we throw
        #back 423 to it, it makes ANOTHER request and we fail there with 405 :/

        msg = ("""abort: HTTP Error 423: Repository `%s` locked by user `%s`"""
                % (GIT_REPO, TEST_USER_ADMIN_LOGIN))
Ejemplo n.º 3
0
def log_pull_action(ui, repo, **kwargs):
    """
    Logs user last pull action

    :param ui:
    :param repo:
    """
    ex = _extract_extras()

    user = User.get_by_username(ex.username)
    action = 'pull'
    action_logger(user, action, ex.repository, ex.ip, commit=True)
    # extension hook call
    from kallithea import EXTENSIONS
    callback = getattr(EXTENSIONS, 'PULL_HOOK', None)
    if callable(callback):
        kw = {}
        kw.update(ex)
        callback(**kw)

    if ex.make_lock is not None and ex.make_lock:
        Repository.lock(Repository.get_by_repo_name(ex.repository), user.user_id)
        #msg = 'Made lock on repo `%s`' % repository
        #sys.stdout.write(msg)

    if ex.locked_by[0]:
        locked_by = User.get(ex.locked_by[0]).username
        _http_ret = HTTPLockedRC(ex.repository, locked_by)
        if str(_http_ret.code).startswith('2'):
            #2xx Codes don't raise exceptions
            sys.stdout.write(_http_ret.title)
    return 0
Ejemplo n.º 4
0
    def test_push_on_locked_repo_by_other_user_git(self):
        #clone some temp
        DEST = _get_tmp_dir()
        clone_url = _construct_url(GIT_REPO, dest=DEST)
        stdout, stderr = Command('/tmp').execute('git clone', clone_url)

        #lock repo
        r = Repository.get_by_repo_name(GIT_REPO)
        # let this user actually push !
        RepoModel().grant_user_permission(repo=r, user=TEST_USER_REGULAR_LOGIN,
                                          perm='repository.write')
        Session().commit()
        Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)

        #push fails repo is locked by other user !
        stdout, stderr = _add_files_and_push('git', DEST,
                                             user=TEST_USER_REGULAR_LOGIN,
                                             passwd=TEST_USER_REGULAR_PASS)
        err = 'Repository `%s` locked by user `%s`' % (GIT_REPO, TEST_USER_ADMIN_LOGIN)
        assert err in stderr

        #TODO: fix this somehow later on Git, Git is stupid and even if we throw
        #back 423 to it, it makes ANOTHER request and we fail there with 405 :/

        msg = ("""abort: HTTP Error 423: Repository `%s` locked by user `%s`"""
                % (GIT_REPO, TEST_USER_ADMIN_LOGIN))
    def test_push_on_locked_repo_by_other_user_git(self):
        #clone some temp
        DEST = _get_tmp_dir()
        clone_url = _construct_url(GIT_REPO, dest=DEST)
        stdout, stderr = Command('/tmp').execute('git clone', clone_url)

        #lock repo
        r = Repository.get_by_repo_name(GIT_REPO)
        # let this user actually push !
        RepoModel().grant_user_permission(repo=r, user=TEST_USER_REGULAR_LOGIN,
                                          perm='repository.write')
        Session().commit()
        Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)

        #push fails repo is locked by other user !
        stdout, stderr = _add_files_and_push('git', DEST,
                                             user=TEST_USER_REGULAR_LOGIN,
                                             passwd=TEST_USER_REGULAR_PASS)
        err = 'Repository `%s` locked by user `%s`' % (GIT_REPO, TEST_USER_ADMIN_LOGIN)
        assert err in stderr

        #TODO: fix this somehow later on Git, Git is stupid and even if we throw
        #back 423 to it, it makes ANOTHER request and we fail there with 405 :/

        msg = ("""abort: HTTP Error 423: Repository `%s` locked by user `%s`"""
                % (GIT_REPO, TEST_USER_ADMIN_LOGIN))
Ejemplo n.º 6
0
def log_pull_action(ui, repo, **kwargs):
    """
    Logs user last pull action

    :param ui:
    :param repo:
    """
    ex = _extract_extras()

    user = User.get_by_username(ex.username)
    action = 'pull'
    action_logger(user, action, ex.repository, ex.ip, commit=True)
    # extension hook call
    from kallithea import EXTENSIONS
    callback = getattr(EXTENSIONS, 'PULL_HOOK', None)
    if callable(callback):
        kw = {}
        kw.update(ex)
        callback(**kw)

    if ex.make_lock is not None and ex.make_lock:
        Repository.lock(Repository.get_by_repo_name(ex.repository),
                        user.user_id)
        #msg = 'Made lock on repo `%s`' % repository
        #ui.status(msg)

    if ex.locked_by[0]:
        locked_by = User.get(ex.locked_by[0]).username
        _http_ret = HTTPLockedRC(ex.repository, locked_by)
        if str(_http_ret.code).startswith('2'):
            #2xx Codes don't raise exceptions
            ui.status(safe_str(_http_ret.title))
    return 0
Ejemplo n.º 7
0
 def test_clone_after_repo_was_locked_git(self):
     #lock repo
     r = Repository.get_by_repo_name(GIT_REPO)
     Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)
     #pull fails since repo is locked
     clone_url = _construct_url(GIT_REPO)
     stdout, stderr = Command('/tmp').execute('git clone', clone_url)
     msg = ("""The requested URL returned error: 423""")
     assert msg in stderr
 def test_clone_after_repo_was_locked_git(self):
     #lock repo
     r = Repository.get_by_repo_name(GIT_REPO)
     Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)
     #pull fails since repo is locked
     clone_url = _construct_url(GIT_REPO)
     stdout, stderr = Command('/tmp').execute('git clone', clone_url)
     msg = ("""The requested URL returned error: 423""")
     assert msg in stderr
 def test_clone_after_repo_was_locked_git(self, webserver):
     #lock repo
     r = Repository.get_by_repo_name(GIT_REPO)
     Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)
     #pull fails since repo is locked
     clone_url = webserver.repo_url(GIT_REPO)
     stdout, stderr = Command(TESTS_TMP_PATH).execute('git clone', clone_url, _get_tmp_dir(), ignoreReturnCode=True)
     msg = ("""The requested URL returned error: 423""")
     assert msg in stderr
 def test_clone_after_repo_was_locked_hg(self):
     #lock repo
     r = Repository.get_by_repo_name(HG_REPO)
     Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)
     #pull fails since repo is locked
     clone_url = _construct_url(HG_REPO)
     stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
     msg = ("""abort: HTTP Error 423: Repository `%s` locked by user `%s`"""
             % (HG_REPO, TEST_USER_ADMIN_LOGIN))
     assert msg in stderr
Ejemplo n.º 11
0
 def test_clone_after_repo_was_locked_hg(self, webserver):
     #lock repo
     r = Repository.get_by_repo_name(HG_REPO)
     Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)
     #pull fails since repo is locked
     clone_url = webserver.repo_url(HG_REPO)
     stdout, stderr = Command(TESTS_TMP_PATH).execute('hg clone', clone_url, _get_tmp_dir(), ignoreReturnCode=True)
     msg = ("""abort: HTTP Error 423: Repository `%s` locked by user `%s`"""
             % (HG_REPO, TEST_USER_ADMIN_LOGIN))
     assert msg in stderr
Ejemplo n.º 12
0
 def test_clone_after_repo_was_locked_hg(self):
     #lock repo
     r = Repository.get_by_repo_name(HG_REPO)
     Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)
     #pull fails since repo is locked
     clone_url = _construct_url(HG_REPO)
     stdout, stderr = Command('/tmp').execute('hg clone', clone_url)
     msg = ("""abort: HTTP Error 423: Repository `%s` locked by user `%s`"""
             % (HG_REPO, TEST_USER_ADMIN_LOGIN))
     assert msg in stderr
Ejemplo n.º 13
0
 def test_clone_after_repo_was_locked_git(self, webserver):
     #lock repo
     r = Repository.get_by_repo_name(GIT_REPO)
     Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)
     #pull fails since repo is locked
     clone_url = webserver.repo_url(GIT_REPO)
     stdout, stderr = Command(TESTS_TMP_PATH).execute('git clone',
                                                      clone_url,
                                                      _get_tmp_dir(),
                                                      ignoreReturnCode=True)
     msg = ("""The requested URL returned error: 423""")
     assert msg in stderr
Ejemplo n.º 14
0
 def test_clone_after_repo_was_locked_hg(self, webserver):
     #lock repo
     r = Repository.get_by_repo_name(HG_REPO)
     Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)
     #pull fails since repo is locked
     clone_url = webserver.repo_url(HG_REPO)
     stdout, stderr = Command(TESTS_TMP_PATH).execute('hg clone',
                                                      clone_url,
                                                      _get_tmp_dir(),
                                                      ignoreReturnCode=True)
     msg = (
         """abort: HTTP Error 423: Repository `%s` locked by user `%s`""" %
         (HG_REPO, TEST_USER_ADMIN_LOGIN))
     assert msg in stderr
Ejemplo n.º 15
0
    def toggle_locking(self, repo_name):
        try:
            repo = Repository.get_by_repo_name(repo_name)

            if repo.enable_locking:
                if repo.locked[0]:
                    Repository.unlock(repo)
                    h.flash(_('Repository has been unlocked'), category='success')
                else:
                    Repository.lock(repo, request.authuser.user_id)
                    h.flash(_('Repository has been locked'), category='success')

        except Exception as e:
            log.error(traceback.format_exc())
            h.flash(_('An error occurred during unlocking'),
                    category='error')
        raise HTTPFound(location=url('summary_home', repo_name=repo_name))
Ejemplo n.º 16
0
    def edit_advanced_locking(self, repo_name):
        """
        Unlock repository when it is locked !

        :param repo_name:
        """
        try:
            repo = Repository.get_by_repo_name(repo_name)
            if request.POST.get('set_lock'):
                Repository.lock(repo, c.authuser.user_id)
                h.flash(_('Locked repository'), category='success')
            elif request.POST.get('set_unlock'):
                Repository.unlock(repo)
                h.flash(_('Unlocked repository'), category='success')
        except Exception, e:
            log.error(traceback.format_exc())
            h.flash(_('An error occurred during unlocking'), category='error')
Ejemplo n.º 17
0
    def edit_advanced_locking(self, repo_name):
        """
        Unlock repository when it is locked !

        :param repo_name:
        """
        try:
            repo = Repository.get_by_repo_name(repo_name)
            if request.POST.get('set_lock'):
                Repository.lock(repo, c.authuser.user_id)
                h.flash(_('Locked repository'), category='success')
            elif request.POST.get('set_unlock'):
                Repository.unlock(repo)
                h.flash(_('Unlocked repository'), category='success')
        except Exception, e:
            log.error(traceback.format_exc())
            h.flash(_('An error occurred during unlocking'),
                    category='error')
Ejemplo n.º 18
0
    def toggle_locking(self, repo_name):
        try:
            repo = Repository.get_by_repo_name(repo_name)

            if repo.enable_locking:
                if repo.locked[0]:
                    Repository.unlock(repo)
                    h.flash(_('Repository has been unlocked'),
                            category='success')
                else:
                    Repository.lock(repo, request.authuser.user_id)
                    h.flash(_('Repository has been locked'),
                            category='success')

        except Exception as e:
            log.error(traceback.format_exc())
            h.flash(_('An error occurred during unlocking'), category='error')
        raise HTTPFound(location=url('summary_home', repo_name=repo_name))
Ejemplo n.º 19
0
    def edit_advanced_locking(self, repo_name):
        """
        Unlock repository when it is locked !

        :param repo_name:
        """
        try:
            repo = Repository.get_by_repo_name(repo_name)
            if request.POST.get('set_lock'):
                Repository.lock(repo, request.authuser.user_id)
                h.flash(_('Repository has been locked'), category='success')
            elif request.POST.get('set_unlock'):
                Repository.unlock(repo)
                h.flash(_('Repository has been unlocked'), category='success')
        except Exception as e:
            log.error(traceback.format_exc())
            h.flash(_('An error occurred during unlocking'), category='error')
        raise HTTPFound(
            location=url('edit_repo_advanced', repo_name=repo_name))
Ejemplo n.º 20
0
    def edit_advanced_locking(self, repo_name):
        """
        Unlock repository when it is locked !

        :param repo_name:
        """
        try:
            repo = Repository.get_by_repo_name(repo_name)
            if request.POST.get('set_lock'):
                Repository.lock(repo, request.authuser.user_id)
                h.flash(_('Repository has been locked'), category='success')
            elif request.POST.get('set_unlock'):
                Repository.unlock(repo)
                h.flash(_('Repository has been unlocked'), category='success')
        except Exception as e:
            log.error(traceback.format_exc())
            h.flash(_('An error occurred during unlocking'),
                    category='error')
        raise HTTPFound(location=url('edit_repo_advanced', repo_name=repo_name))
    def test_push_on_locked_repo_by_other_user_hg(self):
        #clone some temp
        DEST = _get_tmp_dir()
        clone_url = _construct_url(HG_REPO, dest=DEST)
        stdout, stderr = Command('/tmp').execute('hg clone', clone_url)

        #lock repo
        r = Repository.get_by_repo_name(HG_REPO)
        # let this user actually push !
        RepoModel().grant_user_permission(repo=r, user=TEST_USER_REGULAR_LOGIN,
                                          perm='repository.write')
        Session().commit()
        Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)

        #push fails repo is locked by other user !
        stdout, stderr = _add_files_and_push('hg', DEST,
                                             user=TEST_USER_REGULAR_LOGIN,
                                             passwd=TEST_USER_REGULAR_PASS)
        msg = ("""abort: HTTP Error 423: Repository `%s` locked by user `%s`"""
                % (HG_REPO, TEST_USER_ADMIN_LOGIN))
        assert msg in stderr
Ejemplo n.º 22
0
    def toggle_locking(self, repo_name):
        """
        Toggle locking of repository by simple GET call to url

        :param repo_name:
        """

        try:
            repo = Repository.get_by_repo_name(repo_name)

            if repo.enable_locking:
                if repo.locked[0]:
                    Repository.unlock(repo)
                    action = _('Unlocked')
                else:
                    Repository.lock(repo, c.authuser.user_id)
                    action = _('Locked')

                h.flash(_('Repository has been %s') % action,
                        category='success')
        except Exception, e:
            log.error(traceback.format_exc())
            h.flash(_('An error occurred during unlocking'), category='error')
Ejemplo n.º 23
0
    def toggle_locking(self, repo_name):
        """
        Toggle locking of repository by simple GET call to url

        :param repo_name:
        """

        try:
            repo = Repository.get_by_repo_name(repo_name)

            if repo.enable_locking:
                if repo.locked[0]:
                    Repository.unlock(repo)
                    action = _('Unlocked')
                else:
                    Repository.lock(repo, c.authuser.user_id)
                    action = _('Locked')

                h.flash(_('Repository has been %s') % action,
                        category='success')
        except Exception, e:
            log.error(traceback.format_exc())
            h.flash(_('An error occurred during unlocking'),
                    category='error')
Ejemplo n.º 24
0
    def test_push_on_locked_repo_by_other_user_git(self, webserver):
        # Note: Git hooks must be executable on unix. This test will thus fail
        # for example on Linux if /tmp is mounted noexec.

        #clone some temp
        DEST = _get_tmp_dir()
        clone_url = webserver.repo_url(GIT_REPO)
        stdout, stderr = Command(TESTS_TMP_PATH).execute(
            'git clone', clone_url, DEST)

        #lock repo
        r = Repository.get_by_repo_name(GIT_REPO)
        # let this user actually push !
        RepoModel().grant_user_permission(repo=r,
                                          user=TEST_USER_REGULAR_LOGIN,
                                          perm='repository.write')
        Session().commit()
        Repository.lock(r, User.get_by_username(TEST_USER_ADMIN_LOGIN).user_id)

        #push fails repo is locked by other user !
        stdout, stderr = _add_files_and_push(webserver,
                                             'git',
                                             DEST,
                                             username=TEST_USER_REGULAR_LOGIN,
                                             password=TEST_USER_REGULAR_PASS,
                                             ignoreReturnCode=True)
        err = 'Repository `%s` locked by user `%s`' % (GIT_REPO,
                                                       TEST_USER_ADMIN_LOGIN)
        assert err in stderr

        #TODO: fix this somehow later on Git, Git is stupid and even if we throw
        #back 423 to it, it makes ANOTHER request and we fail there with 405 :/

        msg = (
            """abort: HTTP Error 423: Repository `%s` locked by user `%s`""" %
            (GIT_REPO, TEST_USER_ADMIN_LOGIN))