aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/model_test.go
blob: 7f816f53144ac7e2455ad154bf6cd7d2881491f1 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package admin

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"io/fs"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"github.com/go-git/go-billy/v5/osfs"
	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/storage/filesystem"
	"gopkg.in/ini.v1"
)

func TestCasbinPolicies(t *testing.T) {
	roleName := "myrole"
	repoName := "myrepo"
	pRO := &Permission{
		Role: roleName,
		Mode: 0,
	}
	pW := &Permission{
		Role: "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-receive-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 TestLoadServerConfig(t *testing.T) {
	t.Run("testing server config from file", func(t *testing.T) {
		localDir := t.TempDir()
		// TODO Refactor next touch
		localFile := filepath.Join(localDir, "stuff.yaml")
		srcFile, err := os.Open("../../gitserver.yaml")
		if err != nil {
			t.Fatalf("Error opening base config %s", err)
		}
		defer srcFile.Close()

		// dest
		destFile, err := os.OpenFile(localFile, os.O_RDWR|os.O_CREATE, 0755)
		if err != nil {
			t.Fatalf("failed to open destination in git repo %s", err)
		}
		defer destFile.Close()

		// copy
		if _, err := io.Copy(destFile, srcFile); err != nil {
			t.Fatalf("Error copying file %s", err)
		}

		// end copy file
		loadedFile, err := loadServerConfig(false, localDir, "stuff.yaml")
		if err != nil {
			t.Fatal(err)
		}
		if len(loadedFile.Repos) != 2 {
			t.Fatalf("expected to find 2 repos found %d", len(loadedFile.Repos))
		}
	})

	t.Run("testing server config from git", func(t *testing.T) {

	})
}

func TestLocalFile(t *testing.T) {
	localDir := t.TempDir()
	localFile := filepath.Join(localDir, "stuff.yaml")
	os.WriteFile(localFile, []byte("stuff"), 0750)
	loadedFile, err := loadLocalFile(localFile)
	if err != nil {
		t.Fatal(err)
	}

	if !bytes.Contains(loadedFile, []byte("stuff")) {
		t.Fatal("failed to find expected contents in localfile")
	}
	_, err = loadLocalFile("dne.txt")
	if err == nil {
		t.Fatal("Expected to find and error and didn't")
	}
}

func TestMgmtGitConfig(t *testing.T) {
	// setup tempdir
	gitDir := t.TempDir()
	// init git repo
	gitFs := osfs.New(gitDir)
	strg := filesystem.NewStorage(gitFs, nil)
	repo, _ := git.Init(strg, gitFs)
	// add file

	// src
	srcFile, err := os.Open("../../gitserver.yaml")
	if err != nil {
		t.Fatalf("Error opening base config %s", err)
	}
	defer srcFile.Close()

	// file name
	// fileToCommit := fs.Join(gitDir, "gitserver.yaml")
	fileToCommit := "gitserver.yaml"

	// dest
	destFile, err := gitFs.OpenFile(fileToCommit, os.O_RDWR|os.O_CREATE, 0755)
	if err != nil {
		t.Fatalf("failed to open destination in git repo %s", err)
	}
	defer destFile.Close()

	// copy
	if _, err := io.Copy(destFile, srcFile); err != nil {
		t.Fatalf("Error copying file %s", err)
	}
	// commit
	wt, err := repo.Worktree()
	if err != nil {
		t.Fatal(err)
	}
	wt.Add(fileToCommit)
	_, err = wt.Commit(fileToCommit, &git.CommitOptions{})
	if err != nil {
		t.Fatalf("Error creating commit %s", err)
	}

	// run load func
	content, err := loadFromGit(gitDir, "gitserver.yaml")
	if err != nil {
		t.Fatal(err)
	}

	// "go-git-server"
	if !bytes.Contains(content, []byte("go-git-server")) {
		t.Fatal("config missing expected")
	}

	// check couldnt clone err
	_, err = loadFromGit("/dne/bar", "gitserver.yaml")
	if err == nil {
		t.Fatal("expected an cloning repo didn't find one")
	}
	// check couldnt open file err
	_, err = loadFromGit(gitDir, "dne.yaml")
	if err == nil {
		t.Fatal("expected an error opening config file didn't find one")
	}
	// TODO run via serverLoadConfig
}

func TestConfigReconcile(t *testing.T) {
	tempDir := t.TempDir()
	defer os.RemoveAll(tempDir)
	// make "fake" repo
	testRepo := filepath.Join(tempDir, "testrepo.git")
	testConf := filepath.Join(testRepo, "config")
	os.Mkdir(testRepo, 0750)
	f, err := os.Create(filepath.Join(testRepo, "config"))
	if err != nil {
		t.Fatalf("couldn't create testdir, %s", err)
	}
	f.Close()
	repo := &GitRepo{
		Name: "testrepo",
	}
	t.Run("test add gitweb section and remove it", func(t *testing.T) {
		// make "fake" repo
		gw := &GitWeb{
			"owner",
			"description",
			"category",
			"url",
		}
		gw.ReconcileGitConf(testRepo)
		cfg, err := ini.Load(testConf)
		if err != nil {
			t.Fatalf("an error occured loading config %s", err)
		}
		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`")
		}
	})
	t.Run("test magic export file is created", func(t *testing.T) {
		exportPath := filepath.Join(testRepo, GitWebExportMagic)
		repo.ConfigureExport(testRepo)
		_, err := os.Stat(exportPath)
		if errors.Is(err, fs.ErrNotExist) {
			t.Fatal("expected export file to exist, but does not exist")
		}
		if err != nil {
			t.Fatalf("encountered an error %s", err)
		}
	})
}

func TestRepoReconcile(t *testing.T) {
	tempDir := t.TempDir()
	print(tempDir)
	// defer os.RemoveAll(tempDir)
	repo := &GitRepo{
		Name: "TestMeRepo",
		GitWebConfig: &GitWeb{
			"owner",
			"description",
			"category",
			"url",
		},
	}
	repoPath := filepath.Join(tempDir, fmt.Sprintf("%s.git", repo.Name))
	repo.ReconcileRepo(tempDir)
	if _, err := os.Stat(repoPath); errors.Is(err, fs.ErrNotExist) {
		t.Fatal("expected repo to be created, but does not exist")
	}
	defaultFile := []byte(`
[core]      
bare = true 
`)
	// write the base config to repo
	tempConfigFile := filepath.Join(repoPath, "config")
	ioutil.WriteFile(tempConfigFile, defaultFile, 0644)
	// re-reconcile
	repo.ReconcileRepo(tempDir)
	content, err := ioutil.ReadFile(tempConfigFile)
	if err != nil {
		t.Fatal(err)
	}
	// check if description is in the file
	if !strings.Contains(string(content), "description") {
		t.Fatal("expected to find 'description' in config, didn't found", string(content))
	}
	gitExportMagicPath := filepath.Join(tempDir, fmt.Sprintf("%s.git", repo.Name), GitWebExportMagic)
	if _, err := os.Stat(gitExportMagicPath); errors.Is(err, fs.ErrNotExist) {
		t.Fatal("expected git export magic to be created, but does not exist")
	}

}