Skip to content
Snippets Groups Projects
Commit 20ae20af9e51 authored by Georges Racinet's avatar Georges Racinet
Browse files

Test for the check_publish hook

This had to be rewritten after the hook itself has been moved
from hg-git to here. It's a good occasion to do it with
our test helpers.

We now have 100% coverage for this hooks submodule
parent dc2c6b29d523
No related branches found
No related tags found
No related merge requests found
Pipeline #
# Python package
from __future__ import absolute_import
import pytest
from mercurial import (
error,
phases,
)
from heptapod.testhelpers import (
LocalRepoWrapper,
)
def init_repo(basedir):
return LocalRepoWrapper.init(
basedir,
config=dict(
phases={'publish': 'no'},
hooks={'pretxnclose.check_publish':
'python:heptapod.hooks.check_publish.check_publish'
},
web={'allow-publish': 'maintainer',
'allow-push': '*',
},
))
def switch_user(wrapper, user):
wrapper.repo.ui.environ['REMOTE_USER'] = user
def test_draft_publish(tmpdir):
wrapper = init_repo(tmpdir)
switch_user(wrapper, 'someone')
ctx = wrapper.write_commit('foo', content='Foo', return_ctx=True)
assert ctx.phase() == phases.draft
with pytest.raises(error.HookAbort) as exc_info:
wrapper.set_phase('public', [ctx.hex()])
assert 'pretxnclose.check_publish' in exc_info.value.args[0]
switch_user(wrapper, 'maintainer')
wrapper.set_phase('public', [ctx.hex()])
assert ctx.phase() == phases.public
def test_wrong_hook(tmpdir):
wrapper = init_repo(tmpdir)
ui = wrapper.repo.ui
pretxn = 'pretxnclose.check_publish'
hookdef = ui.config('hooks', pretxn)
ui.setconfig('hooks', pretxn, '')
ui.setconfig('hooks', 'precommit.check_publish', hookdef)
# precommit because that one does not swallow exceptions other
# than abort
with pytest.raises(error.ProgrammingError) as exc_info:
wrapper.write_commit('foo')
assert 'precommit' in exc_info.value.args[0]
$ cat > fakeremoteuser.py << EOF
> import os
> from mercurial.hgweb import hgweb_mod
> from mercurial import wireprotov1server
> class testenvhgweb(hgweb_mod.hgweb):
> def __call__(self, env, respond):
> # Allow REMOTE_USER to define authenticated user.
> if r'REMOTE_USER' in os.environ:
> env[r'REMOTE_USER'] = os.environ[r'REMOTE_USER']
> # Allow REQUEST_METHOD to override HTTP method
> if r'REQUEST_METHOD' in os.environ:
> env[r'REQUEST_METHOD'] = os.environ[r'REQUEST_METHOD']
> return super(testenvhgweb, self).__call__(env, respond)
> hgweb_mod.hgweb = testenvhgweb
>
> @wireprotov1server.wireprotocommand(b'customreadnoperm')
> def customread(repo, proto):
> return b'read-only command no defined permissions\n'
> @wireprotov1server.wireprotocommand(b'customwritenoperm')
> def customwritenoperm(repo, proto):
> return b'write command no defined permissions\n'
> @wireprotov1server.wireprotocommand(b'customreadwithperm', permission=b'pull')
> def customreadwithperm(repo, proto):
> return b'read-only command w/ defined permissions\n'
> @wireprotov1server.wireprotocommand(b'customwritewithperm', permission=b'push')
> def customwritewithperm(repo, proto):
> return b'write command w/ defined permissions\n'
> EOF
$ hg init server
$ cat >> server/.hg/hgrc << EOF
> [extensions]
> fakeremoteuser=$TESTTMP/fakeremoteuser.py
>
> [phases]
> publish=no
>
> [web]
> push_ssl = false
> allow-push = *
> allow-publish = master
>
> [hooks]
> pretxnclose.check_publish=python:heptapod.hooks.check_publish.check_publish
> EOF
$ cd server
$ hg debugbuilddag .+1
$ hg phase --public -r tip
$ REMOTE_USER=dev hg serve -n test -p $HGPORT -d --pid-file=$TESTTMP/server-hg1.pid -A $TESTTMP/access1.log -E $TESTTMP/errors1.log
$ cat $TESTTMP/server-hg1.pid >> $DAEMON_PIDS
$ REMOTE_USER=master hg serve -n test -p $HGPORT2 -d --pid-file=$TESTTMP/server-hg2.pid -A $TESTTMP/access2.log -E $TESTTMP/errors2.log
$ cat $TESTTMP/server-hg2.pid >> $DAEMON_PIDS
$ cd ..
$ hg clone http://localhost:$HGPORT/ client-dev
requesting all changes
adding changesets
adding manifests
adding file changes
added 2 changesets with 0 changes to 0 files
new changesets 1ea73414a91b:66f7d451a68b
updating to branch default
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ hg clone http://localhost:$HGPORT2/ client-master
requesting all changes
adding changesets
adding manifests
adding file changes
added 2 changesets with 0 changes to 0 files
new changesets 1ea73414a91b:66f7d451a68b
updating to branch default
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ cd client-dev
$ echo draft > draft
$ hg add draft
$ hg commit -m draft
$ hg push -fr .
pushing to http://localhost:$HGPORT/
searching for changes
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: added 1 changesets with 1 changes to 1 files
$ cat ../errors1.log
$ hg push -fr . --publish
pushing to http://localhost:$HGPORT/
searching for changes
no changes found
remote: user "dev" is not authorised to publish changesets: ceda9cbb053e
remote: pretxnclose.check_publish hook failed
abort: push failed on remote
[255]
$ cat ../errors1.log
$ hg up ".^"
0 files updated, 0 files merged, 1 files removed, 0 files unresolved
$ echo public > public
$ hg add public
$ hg commit -m public
created new head
$ hg phase --public --rev .
$ hg push -fr .
pushing to http://localhost:$HGPORT/
searching for changes
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: user "dev" is not authorised to publish changesets: c2684d781b2f
remote: transaction abort!
remote: rollback completed
remote: pretxnclose.check_publish hook failed
abort: push failed on remote
[255]
$ cd ..
$ cd client-master
$ echo draft2 > draft2
$ hg add draft2
$ hg commit -m draft
$ hg push -fr .
pushing to http://localhost:$HGPORT2/
searching for changes
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: added 1 changesets with 1 changes to 1 files (+1 heads)
$ hg push -fr . --publish
pushing to http://localhost:$HGPORT2/
searching for changes
no changes found
[1]
$ hg up ".^"
0 files updated, 0 files merged, 1 files removed, 0 files unresolved
$ echo public > public2
$ hg add public2
$ hg commit -m public2
created new head
$ hg phase --public --rev .
$ hg push -fr .
pushing to http://localhost:$HGPORT2/
searching for changes
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: added 1 changesets with 1 changes to 1 files (+1 heads)
$ cd ..
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment