diff options
| author | Max Resnick <max@ofmax.li> | 2023-04-02 08:52:52 -0700 |
|---|---|---|
| committer | Max Resnick <max@ofmax.li> | 2023-05-26 09:47:59 -0700 |
| commit | 480ce38f8c01de00adf6de651b8c2c57cd2148be (patch) | |
| tree | 85bd66986cb3f0ad501549e46e649073775e8077 /internal/admin/model_test.go | |
| parent | 9e04be2ca3d8980ebc8ec791d005ba77382fb1fa (diff) | |
| download | go-git-server-480ce38f8c01de00adf6de651b8c2c57cd2148be.tar.gz | |
Adds a lot of tests adds middleware hook
Diffstat (limited to 'internal/admin/model_test.go')
| -rw-r--r-- | internal/admin/model_test.go | 164 |
1 files changed, 134 insertions, 30 deletions
diff --git a/internal/admin/model_test.go b/internal/admin/model_test.go index 79e3cb5..7f816f5 100644 --- a/internal/admin/model_test.go +++ b/internal/admin/model_test.go @@ -1,8 +1,10 @@ package admin import ( + "bytes" "errors" "fmt" + "io" "io/fs" "io/ioutil" "os" @@ -10,18 +12,21 @@ import ( "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 := "mr:role" + roleName := "myrole" repoName := "myrepo" pRO := &Permission{ Role: roleName, Mode: 0, } pW := &Permission{ - Role: "my:admin", + Role: "admin", Mode: 1, } @@ -50,7 +55,7 @@ func TestCasbinPolicies(t *testing.T) { if wPolicies[0][0] != roleName { t.Fatal("Role name doesn't match") } - if wPolicies[0][1] != fmt.Sprintf("/%s/git-recieve-pack", repoName) { + if wPolicies[0][1] != fmt.Sprintf("/%s/git-receive-pack", repoName) { t.Fatal("Policy missing write path") } }) @@ -76,6 +81,128 @@ func TestCasbinPolicies(t *testing.T) { }) } +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) @@ -89,8 +216,7 @@ func TestConfigReconcile(t *testing.T) { } f.Close() repo := &GitRepo{ - Public: true, - Name: "testrepo", + Name: "testrepo", } t.Run("test add gitweb section and remove it", func(t *testing.T) { // make "fake" repo @@ -126,7 +252,7 @@ func TestConfigReconcile(t *testing.T) { } }) t.Run("test magic export file is created", func(t *testing.T) { - exportPath := filepath.Join(testRepo, GitExportMagic) + exportPath := filepath.Join(testRepo, GitWebExportMagic) repo.ConfigureExport(testRepo) _, err := os.Stat(exportPath) if errors.Is(err, fs.ErrNotExist) { @@ -135,13 +261,6 @@ func TestConfigReconcile(t *testing.T) { if err != nil { t.Fatalf("encountered an error %s", err) } - // copy repo - pvtRepo := repo - pvtRepo.Public = false - pvtRepo.ConfigureExport(testRepo) - if _, err := os.Stat(exportPath); err == nil { - t.Fatal("expected export file exist, but does not exist") - } }) } @@ -150,8 +269,7 @@ func TestRepoReconcile(t *testing.T) { print(tempDir) // defer os.RemoveAll(tempDir) repo := &GitRepo{ - Public: true, - Name: "testrepo", + Name: "TestMeRepo", GitWebConfig: &GitWeb{ "owner", "description", @@ -181,23 +299,9 @@ bare = true 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), GitExportMagic) + 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") } - // Test that repo is switched back to private - repo.Public = false - // re-write the base config to repo - ioutil.WriteFile(tempConfigFile, defaultFile, 0644) - // re-reconcile - repo.ReconcileRepo(tempDir) - // check if description is *NOT* in the file - if !strings.Contains(string(content), "description") { - t.Fatal("expected to *NOT* find 'description' in config, didn't found", string(content)) - } - // make sure export is removed - if _, err := os.Stat(gitExportMagicPath); !errors.Is(err, fs.ErrNotExist) { - t.Fatal("expected git export magic to not exist, but *does* exist") - } } |