aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/model_test.go
blob: 4ec1177dd62c9ebd4a88dcab6eebde2d253c7b87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package admin

import (
	"fmt"
	"os"
	"path/filepath"
	"testing"

	"gopkg.in/ini.v1"
)

func TestCasbinPolicies(t *testing.T) {
	roleName := "mr:role"
	repoName := "myrepo"
	pRO := &Permission{
		Role: roleName,
		Mode: 0,
	}
	pW := &Permission{
		Role: "my:admin",
		Mode: 1,
	}

	t.Run("test read only policies", func(t *testing.T) {
		roPolicies := readOnlyPaths(roleName, repoName)
		for _, v := range roPolicies {
			if v[0] != roleName {
				t.Fatalf("Missing rolename in policy %s %s", v[0], v[1])
			}
		}
		if roPolicies[0][1] != fmt.Sprintf("/%s/info/refs", repoName) {
			t.Fatal("missing info/refs policy")
		}
		if roPolicies[1][1] != fmt.Sprintf("/%s/git-upload-pack", repoName) {
			t.Fatal("missing git-upload-pack policy")
		}
		if roPolicies[0][2] != "GET" {
			t.Fatal("missing info/refs policy")
		}
		if roPolicies[1][2] != "POST" {
			t.Fatal("missing git-upload-pack policy")
		}
	})
	t.Run("testing write policies", func(t *testing.T) {
		wPolicies := writePaths(roleName, repoName)
		if wPolicies[0][0] != roleName {
			t.Fatal("Role name doesn't match")
		}
		if wPolicies[0][1] != fmt.Sprintf("/%s/git-recieve-pack", repoName) {
			t.Fatal("Policy missing write path")
		}
	})

	t.Run("testing mode build policies", func(t *testing.T) {
		rOPolicy := pRO.Policy(roleName)
		wPolicy := pW.Policy(roleName)
		if len(rOPolicy) != 2 {
			t.Fatal("Didn't provide correct number of read policies")
		}
		if len(wPolicy) != 3 {
			t.Fatal("Didn't provide correct number of write policies")
		}
	})
	t.Run("testing repo level policies", func(t *testing.T) {
		repo := &GitRepo{
			Permissions: []*Permission{pRO, pW},
		}
		policies := repo.CasbinPolicies()
		if len(policies) != 5 {
			t.Fatal("Repo was expected to have 5 policies generated")
		}
	})
}

func TestConfigReconcile(t *testing.T) {
	tempDir := t.TempDir()

	t.Run("test add gitweb section", func(t *testing.T) {
		// make "fake" repo
		testRepo := filepath.Join(tempDir, "testadd.git")
		testConf := filepath.Join(testRepo, "conf")
		os.Mkdir(testRepo, 0750)
		f, err := os.Create(filepath.Join(testRepo, "conf"))
		if err != nil {
			t.Fatalf("couldn't create testdir, %s", err)
		}
		f.Close()
		gw := &GitWeb{
			"owner",
			"description",
			"category",
			"url",
		}
		gw.ReconcileGitConf(testRepo)
		cfg, err := ini.Load(testConf)
		if !cfg.HasSection("gitweb") {
			t.Fatalf("reconciler conf didn't have a section `gitweb`")
		}
		section := cfg.Section("gitweb")

		for _, v := range []string{"owner", "description", "category", "url"} {
			val := section.Key(v).Value()
			if val != v {
				t.Fatalf("expected %s found %s", v, val)
			}
		}

		// flip public repo status
		emptyGitWeb := &GitWeb{}
		emptyGitWeb.ReconcileGitConf(testRepo)
		emptyCfg, err := ini.Load(testConf)
		if emptyCfg.HasSection("gitweb") {
			t.Fatalf("reconciler conf didn't remove section `gitweb`")
		}
	})
}