133728e barerepo 1mo
1
package repocfg
133728e barerepo 1mo
3
import "testing"
133728e barerepo 1mo
5
// The rule that matters most: a repository is closed unless it says otherwise.
133728e barerepo 1mo
6
func TestVisibility(t *testing.T) {
133728e barerepo 1mo
7
cases := map[string]bool{
133728e barerepo 1mo
8
"public": true,
133728e barerepo 1mo
9
"Public": true,
133728e barerepo 1mo
10
"private": false,
133728e barerepo 1mo
11
"": false, // no config file at all, the push-to-create case
133728e barerepo 1mo
12
"publik": false, // a typo must not publish anyone's code
133728e barerepo 1mo
13
"true": false,
133728e barerepo 1mo
14
"open": false,
133728e barerepo 1mo
16
for v, want := range cases {
133728e barerepo 1mo
17
c := Default()
133728e barerepo 1mo
18
c.Repo.Visibility = v
133728e barerepo 1mo
19
if got := c.Public(); got != want {
133728e barerepo 1mo
20
t.Errorf("visibility %q: Public() = %v, want %v", v, got, want)
133728e barerepo 1mo
25
func TestAccess(t *testing.T) {
133728e barerepo 1mo
26
c := Default()
133728e barerepo 1mo
27
c.Access.Push = []string{"lisa"}
133728e barerepo 1mo
29
// Private: owner and pushers only, and no anonymous reader.
133728e barerepo 1mo
30
for user, want := range map[string]bool{"john": true, "lisa": true, "mark": false, "": false} {
133728e barerepo 1mo
31
if got := c.MayRead("john", user); got != want {
133728e barerepo 1mo
32
t.Errorf("private MayRead(%q) = %v, want %v", user, got, want)
133728e barerepo 1mo
35
// Public: everyone reads, including anonymously.
133728e barerepo 1mo
36
c.Repo.Visibility = "public"
133728e barerepo 1mo
37
for user, want := range map[string]bool{"john": true, "mark": true, "": true} {
133728e barerepo 1mo
38
if got := c.MayRead("john", user); got != want {
133728e barerepo 1mo
39
t.Errorf("public MayRead(%q) = %v, want %v", user, got, want)
133728e barerepo 1mo
42
// Push is the owner plus the list, and the owner is never in the list.
133728e barerepo 1mo
43
for user, want := range map[string]bool{"john": true, "lisa": true, "mark": false, "": false} {
133728e barerepo 1mo
44
if got := c.MayPush("john", user); got != want {
133728e barerepo 1mo
45
t.Errorf("MayPush(%q) = %v, want %v", user, got, want)
133728e barerepo 1mo
48
// Rule 5: anyone with an account may propose, without being granted it.
133728e barerepo 1mo
49
if !c.MayPropose("john", "mark") {
133728e barerepo 1mo
50
t.Error("MayPropose denied a signed-in stranger on a public repository")
133728e barerepo 1mo
52
if c.MayPropose("john", "") {
133728e barerepo 1mo
53
t.Error("MayPropose allowed an anonymous pusher")
133728e barerepo 1mo
55
c.Proposals.AcceptFrom = "push"
133728e barerepo 1mo
56
if c.MayPropose("john", "mark") {
133728e barerepo 1mo
57
t.Error("accept_from = push still accepted a stranger")
133728e barerepo 1mo
61
// Force-push and deletion are free everywhere except the default branch.
133728e barerepo 1mo
62
func TestForcePushAndDelete(t *testing.T) {
133728e barerepo 1mo
63
c := Default()
133728e barerepo 1mo
64
if !c.ForcePushAllowed("topic", "master") {
133728e barerepo 1mo
65
t.Error("force-push to a topic branch was refused")
133728e barerepo 1mo
67
if c.ForcePushAllowed("master", "master") {
133728e barerepo 1mo
68
t.Error("force-push to the default branch was allowed by default")
133728e barerepo 1mo
70
c.Access.AllowForcePush = []string{"master"}
133728e barerepo 1mo
71
if !c.ForcePushAllowed("master", "master") {
133728e barerepo 1mo
72
t.Error("allow_force_push did not take effect")
133728e barerepo 1mo
74
if c.DeleteAllowed("master", "master") {
133728e barerepo 1mo
75
t.Error("the default branch could be deleted")
133728e barerepo 1mo
77
if !c.DeleteAllowed("topic", "master") {
133728e barerepo 1mo
78
t.Error("a topic branch could not be deleted")
133728e barerepo 1mo
82
// An archived repository is read-only except for the owner, who pushes the line that undoes it.
133728e barerepo 1mo
83
func TestArchived(t *testing.T) {
133728e barerepo 1mo
84
c := Default()
133728e barerepo 1mo
85
c.Repo.Archived = true
133728e barerepo 1mo
86
c.Access.Push = []string{"lisa"}
133728e barerepo 1mo
87
if !c.MayPush("john", "john") {
133728e barerepo 1mo
88
t.Error("the owner could not push to an archived repository, so it could never be unarchived")
133728e barerepo 1mo
90
if c.MayPush("john", "lisa") {
133728e barerepo 1mo
91
t.Error("a collaborator could push to an archived repository")
133728e barerepo 1mo
93
if c.MayPropose("john", "mark") {
133728e barerepo 1mo
94
t.Error("an archived repository accepted a proposal")