def test_push_inits_no_stdout_spam(): # git init has a tendency to spew to stdout, and that confuses # e.g. a git push tmp = util.maketemp() cfg = RawConfigParser() cfg.add_section('gitosis') repositories = os.path.join(tmp, 'repositories') os.mkdir(repositories) cfg.set('gitosis', 'repositories', repositories) generated = os.path.join(tmp, 'generated') os.mkdir(generated) cfg.set('gitosis', 'generate-files-in', generated) cfg.add_section('group foo') cfg.set('group foo', 'members', 'jdoe') cfg.set('group foo', 'writable', 'foo') old_stdout = os.dup(1) try: new_stdout = os.tmpfile() os.dup2(new_stdout.fileno(), 1) serve.serve( cfg=cfg, user='******', command="git-receive-pack 'foo'", ) finally: os.dup2(old_stdout, 1) os.close(old_stdout) new_stdout.seek(0) got = new_stdout.read() new_stdout.close() eq(got, '') eq(os.listdir(repositories), ['foo.git']) assert os.path.isfile(os.path.join(repositories, 'foo.git', 'HEAD'))
def test_no_notListed(): cfg = RawConfigParser() cfg.add_section('group hackers') cfg.set('group hackers', 'members', 'wsmith') gen = group.getMembership(config=cfg, user='******') eq(gen.next(), 'all') assert_raises(StopIteration, gen.next)
def test_typo_writeable(): tmp = util.maketemp() repository.init(os.path.join(tmp, 'foo.git')) cfg = RawConfigParser() cfg.add_section('gitosis') cfg.set('gitosis', 'repositories', tmp) cfg.add_section('group foo') cfg.set('group foo', 'members', 'jdoe') cfg.set('group foo', 'writeable', 'foo') log = logging.getLogger('gitosis.serve') buf = StringIO() handler = logging.StreamHandler(buf) log.addHandler(handler) try: got = serve.serve( cfg=cfg, user='******', command="git-receive-pack 'foo'", ) finally: log.removeHandler(handler) eq(got, "git-receive-pack '%s/foo.git'" % tmp) handler.flush() eq( buf.getvalue(), """Repository 'foo' config has typo "writeable", should be "writable" """)
def test_push_inits_subdir_parent_exists(): tmp = util.maketemp() cfg = RawConfigParser() cfg.add_section('gitosis') repositories = os.path.join(tmp, 'repositories') os.mkdir(repositories) foo = os.path.join(repositories, 'foo') # silly mode on purpose; not to be touched os.mkdir(foo, 0751) cfg.set('gitosis', 'repositories', repositories) generated = os.path.join(tmp, 'generated') os.mkdir(generated) cfg.set('gitosis', 'generate-files-in', generated) cfg.add_section('group foo') cfg.set('group foo', 'members', 'jdoe') cfg.set('group foo', 'writable', 'foo/bar') serve.serve( cfg=cfg, user='******', command="git-receive-pack 'foo/bar.git'", ) eq(os.listdir(repositories), ['foo']) util.check_mode(foo, 0751, is_dir=True) eq(os.listdir(foo), ['bar.git']) assert os.path.isfile(os.path.join(repositories, 'foo', 'bar.git', 'HEAD'))
def test_write_yes_map(): cfg = GitosisRawConfigParser() cfg.add_section("group fooers") cfg.set("group fooers", "members", "jdoe") cfg.set("group fooers", "map writable foo/bar", "quux/thud") assert access.allowed(config=cfg, user="******", mode="writable", path="foo/bar") == ("repositories", "quux/thud")
def test_write_no_simple_with_readonly(): cfg = GitosisRawConfigParser() cfg.add_section("group fooers") cfg.set("group fooers", "members", "jdoe") cfg.set("group fooers", "readonly", "foo/bar") assert access.allowed(cfg, user="******", mode="writable", path="foo/bar") is None
def test_read_yes_map_with_writable(): cfg = GitosisRawConfigParser() cfg.add_section("group fooers") cfg.set("group fooers", "members", "jdoe") cfg.set("group fooers", "map writable foo/bar", "quux/thud") assert access.allowed(cfg, user="******", mode="readonly", path="foo/bar") is None
def test_read_yes_all(): cfg = GitosisRawConfigParser() cfg.add_section("group fooers") cfg.set("group fooers", "members", "@all") cfg.set("group fooers", "readonly", "foo/bar") assert access.allowed(cfg, user="******", mode="readonly", path="foo/bar") == ("repositories", "foo/bar")
def test_base_global_unset(): cfg = GitosisRawConfigParser() cfg.add_section("gitosis") cfg.add_section("group fooers") cfg.set("group fooers", "members", "jdoe") cfg.set("group fooers", "readonly", "foo xyzzy bar") assert access.allowed(cfg, user="******", mode="readonly", path="xyzzy") == ("repositories", "xyzzy")
def test_dotgit(): # a .git extension is always allowed to be added cfg = GitosisRawConfigParser() cfg.add_section("group fooers") cfg.set("group fooers", "members", "jdoe") cfg.set("group fooers", "writable", "foo/bar") assert access.allowed(cfg, user="******", mode="writable", path="foo/bar.git") == ("repositories", "foo/bar")
def test_base_local(): cfg = GitosisRawConfigParser() cfg.add_section("group fooers") cfg.set("group fooers", "repositories", "some/relative/path") cfg.set("group fooers", "members", "jdoe") cfg.set("group fooers", "map writable foo/bar", "baz/quux/thud") assert access.allowed(cfg, user="******", mode="writable", path="foo/bar") == ("some/relative/path", "baz/quux/thud")
def test_no_recurse_loop(): cfg = RawConfigParser() cfg.add_section('group hackers') cfg.set('group hackers', 'members', '@smackers') cfg.add_section('group smackers') cfg.set('group smackers', 'members', '@hackers') gen = group.getMembership(config=cfg, user='******') eq(gen.next(), 'all') assert_raises(StopIteration, gen.next)
def test_base_global_absolute(): cfg = GitosisRawConfigParser() cfg.add_section("gitosis") cfg.set("gitosis", "repositories", "/a/leading/path") cfg.add_section("group fooers") cfg.set("group fooers", "members", "jdoe") cfg.set("group fooers", "map writable foo/bar", "baz/quux/thud") assert access.allowed(cfg, user="******", mode="writable", path="foo/bar") == ("/a/leading/path", "baz/quux/thud")
def test_projectsList_repoDenied(): cfg = RawConfigParser() cfg.add_section("repo foo/bar") got = StringIO() gitweb.generate_project_list_fp(config=cfg, fp=got) eq( got.getvalue(), """\ """, )
def test_owner_full_access(): cfg = GitosisRawConfigParser() cfg.add_section("repo foo/bar") cfg.set("repo foo/bar", "owner", "jdoe") assert access.allowed(cfg, user="******", mode="writable", path="foo/bar") == ("repositories", "foo/bar") assert access.allowed(cfg, user="******", mode="readable", path="foo/bar") == ("repositories", "foo/bar")
def test_description_repo_missing_parent(): # configured but not created yet; before first push tmp = maketemp() path = os.path.join(tmp, "foo/bar.git") cfg = RawConfigParser() cfg.add_section("gitosis") cfg.set("gitosis", "repositories", tmp) cfg.add_section("repo foo") cfg.set("repo foo", "description", "foodesc") gitweb.set_descriptions(config=cfg) assert not os.path.exists(os.path.join(tmp, "foo"))
def test_yes_recurse_one_ordering(): cfg = RawConfigParser() cfg.add_section('group smackers') cfg.set('group smackers', 'members', 'danny jdoe') cfg.add_section('group hackers') cfg.set('group hackers', 'members', 'wsmith @smackers') gen = group.getMembership(config=cfg, user='******') eq(gen.next(), 'smackers') eq(gen.next(), 'hackers') eq(gen.next(), 'all') assert_raises(StopIteration, gen.next)
def test_projectsList_noOwner(): cfg = RawConfigParser() cfg.add_section("repo foo/bar") cfg.set("repo foo/bar", "gitweb", "yes") got = StringIO() gitweb.generate_project_list_fp(config=cfg, fp=got) eq( got.getvalue(), """\ foo%2Fbar """, )
def test_description_not_set(): tmp = maketemp() path = os.path.join(tmp, "foo.git") mkdir(path) writeFile(os.path.join(path, "description"), "i was here first\n") cfg = RawConfigParser() cfg.add_section("gitosis") cfg.set("gitosis", "repositories", tmp) cfg.add_section("repo foo") gitweb.set_descriptions(config=cfg) got = readFile(os.path.join(path, "description")) eq(got, "i was here first\n")
def test_description_default(): tmp = maketemp() path = os.path.join(tmp, "foo.git") mkdir(path) writeFile(os.path.join(path, "description"), "Unnamed repository; edit this file to name it for gitweb.\n") cfg = RawConfigParser() cfg.add_section("gitosis") cfg.set("gitosis", "repositories", tmp) cfg.add_section("repo foo") cfg.set("repo foo", "description", "foodesc") gitweb.set_descriptions(config=cfg) got = readFile(os.path.join(path, "description")) eq(got, "foodesc\n")
def test_bad_forbiddenCommand_write_readAccess_space(): cfg = RawConfigParser() cfg.add_section('group foo') cfg.set('group foo', 'members', 'jdoe') cfg.set('group foo', 'readonly', 'foo') e = assert_raises( serve.WriteAccessDenied, serve.serve, cfg=cfg, user='******', command="git receive-pack 'foo'", ) eq(str(e), 'Repository write access denied') assert isinstance(e, serve.AccessDenied) assert isinstance(e, serve.ServingError)
def test_simple_read_leading_slash(): tmp = util.maketemp() repository.init(os.path.join(tmp, 'foo.git')) cfg = RawConfigParser() cfg.add_section('gitosis') cfg.set('gitosis', 'repositories', tmp) cfg.add_section('group foo') cfg.set('group foo', 'members', 'jdoe') cfg.set('group foo', 'readonly', 'foo') got = serve.serve( cfg=cfg, user='******', command="git-upload-pack '/foo'", ) eq(got, "git-upload-pack '%s/foo.git'" % tmp)
def test_simple_write_space(): tmp = util.maketemp() repository.init(os.path.join(tmp, 'foo.git')) cfg = RawConfigParser() cfg.add_section('gitosis') cfg.set('gitosis', 'repositories', tmp) cfg.add_section('group foo') cfg.set('group foo', 'members', 'jdoe') cfg.set('group foo', 'writable', 'foo') got = serve.serve( cfg=cfg, user='******', command="git receive-pack 'foo'", ) eq(got, "git receive-pack '%s/foo.git'" % tmp)
def test_projectsList_reallyEndsWithGit(): tmp = maketemp() path = os.path.join(tmp, "foo.git") mkdir(path) cfg = RawConfigParser() cfg.add_section("gitosis") cfg.set("gitosis", "repositories", tmp) cfg.add_section("repo foo") cfg.set("repo foo", "gitweb", "yes") got = StringIO() gitweb.generate_project_list_fp(config=cfg, fp=got) eq( got.getvalue(), """\ foo.git """, )
def test_projectsList_path(): tmp = maketemp() path = os.path.join(tmp, "foo.git") mkdir(path) cfg = RawConfigParser() cfg.add_section("gitosis") cfg.set("gitosis", "repositories", tmp) cfg.add_section("repo foo") cfg.set("repo foo", "gitweb", "yes") projects_list = os.path.join(tmp, "projects.list") gitweb.generate_project_list(config=cfg, path=projects_list) got = readFile(projects_list) eq( got, """\ foo.git """, )
def test_absolute(): # as the only convenient way to use non-standard SSH ports with # git is via the ssh://user@host:port/path syntax, and that syntax # forces absolute urls, just force convert absolute paths to # relative paths; you'll never really want absolute paths via # gitosis, anyway. tmp = util.maketemp() repository.init(os.path.join(tmp, 'foo.git')) cfg = RawConfigParser() cfg.add_section('gitosis') cfg.set('gitosis', 'repositories', tmp) cfg.add_section('group foo') cfg.set('group foo', 'members', 'jdoe') cfg.set('group foo', 'readonly', 'foo') got = serve.serve( cfg=cfg, user='******', command="git-upload-pack '/foo'", ) eq(got, "git-upload-pack '%s/foo.git'" % tmp)
def test_push_inits_updates_projects_list(): tmp = util.maketemp() cfg = RawConfigParser() cfg.add_section('gitosis') repositories = os.path.join(tmp, 'repositories') os.mkdir(repositories) cfg.set('gitosis', 'repositories', repositories) generated = os.path.join(tmp, 'generated') os.mkdir(generated) cfg.set('gitosis', 'generate-files-in', generated) cfg.add_section('group foo') cfg.set('group foo', 'members', 'jdoe') cfg.set('group foo', 'writable', 'foo') cfg.add_section('repo foo') cfg.set('repo foo', 'gitweb', 'yes') os.mkdir(os.path.join(repositories, 'gitosis-admin.git')) serve.serve( cfg=cfg, user='******', command="git-receive-pack 'foo'", ) eq( sorted(os.listdir(repositories)), sorted(['foo.git', 'gitosis-admin.git']), ) path = os.path.join(generated, 'projects.list') assert os.path.exists(path) got = util.readFile(path) eq(got, 'foo.git\n')
def test_push_inits_sets_description(): tmp = util.maketemp() cfg = RawConfigParser() cfg.add_section('gitosis') repositories = os.path.join(tmp, 'repositories') os.mkdir(repositories) cfg.set('gitosis', 'repositories', repositories) generated = os.path.join(tmp, 'generated') os.mkdir(generated) cfg.set('gitosis', 'generate-files-in', generated) cfg.add_section('group foo') cfg.set('group foo', 'members', 'jdoe') cfg.set('group foo', 'writable', 'foo') cfg.add_section('repo foo') cfg.set('repo foo', 'description', 'foodesc') serve.serve( cfg=cfg, user='******', command="git-receive-pack 'foo'", ) eq(os.listdir(repositories), ['foo.git']) path = os.path.join(repositories, 'foo.git', 'description') assert os.path.exists(path) got = util.readFile(path) eq(got, 'foodesc\n')
def test_push_inits_if_needed_haveExtension(): # a push to a non-existent repository (but where config authorizes # you to do that) will create the repository on the fly tmp = util.maketemp() cfg = RawConfigParser() cfg.add_section('gitosis') repositories = os.path.join(tmp, 'repositories') os.mkdir(repositories) cfg.set('gitosis', 'repositories', repositories) generated = os.path.join(tmp, 'generated') os.mkdir(generated) cfg.set('gitosis', 'generate-files-in', generated) cfg.add_section('group foo') cfg.set('group foo', 'members', 'jdoe') cfg.set('group foo', 'writable', 'foo') serve.serve( cfg=cfg, user='******', command="git-receive-pack 'foo.git'", ) eq(os.listdir(repositories), ['foo.git']) assert os.path.isfile(os.path.join(repositories, 'foo.git', 'HEAD'))
def test_push_inits_if_needed_existsWithExtension(): tmp = util.maketemp() cfg = RawConfigParser() cfg.add_section('gitosis') repositories = os.path.join(tmp, 'repositories') os.mkdir(repositories) cfg.set('gitosis', 'repositories', repositories) cfg.add_section('group foo') cfg.set('group foo', 'members', 'jdoe') cfg.set('group foo', 'writable', 'foo') os.mkdir(os.path.join(repositories, 'foo.git')) serve.serve( cfg=cfg, user='******', command="git-receive-pack 'foo'", ) eq(os.listdir(repositories), ['foo.git']) # it should *not* have HEAD here as we just mkdirred it and didn't # create it properly, and the mock repo didn't have anything in # it.. having HEAD implies serve ran git init, which is supposed # to be unnecessary here eq(os.listdir(os.path.join(repositories, 'foo.git')), [])