package admin import ( "io" "log" "os" "path/filepath" "testing" ) var ( updatedServerConfig = []byte(` --- name: "go-git-server" version: "v1alpha1" basepath: ./repos repos: - name: mgmt permissions: - role: admin mode: 1 - name: testmerepo git_web_config: owner: grumps description: >- A wrapper to git http-backend providing authentcation and authorization inspired by gitolite. permissions: - role: maintainers mode: 1 - name: thisismynewrepo git_web_config: owner: grumps description: >- A wrapper to git http-backend providing authentcation and authorization inspired by gitolite. permissions: - role: maintainers mode: 1 `) ) func tempModelPolicyConfig(t *testing.T, tempDir, tempRepoDir string) (string, string, string) { // auth model destModelFile := filepath.Join(tempDir, "auth_model.ini") srcModelFile := "../../auth_model.ini" copyFile(t, srcModelFile, destModelFile) // end auth model // policy destPolicyFile := filepath.Join(tempDir, "testpolicy.csv") srcPolicyFile := "../../tests/testpolicy.csv" copyFile(t, srcPolicyFile, destPolicyFile) // end policy // config destConfigFile := filepath.Join(tempRepoDir, "gitserver.yaml") srcConfigFile := "../../gitserver.yaml" copyFile(t, srcConfigFile, destConfigFile) // end config return destModelFile, destPolicyFile, destConfigFile } func copyFile(t *testing.T, srcFilePath, destPath string) { srcFile, err := os.Open(srcFilePath) if err != nil { t.Fatalf("Error opening base config %s", err) } defer srcFile.Close() // dest destFile, err := os.OpenFile(destPath, 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) } } func TestInitServer(t *testing.T) { tempDir := t.TempDir() tempRepoDir := t.TempDir() destModelFile, destPolicyFile, destConfigFile := tempModelPolicyConfig(t, tempDir, tempRepoDir) t.Run("test reload config success", func(t *testing.T) { svc, _ := NewService(destModelFile, destPolicyFile, filepath.Join(tempRepoDir, "gitserver.yaml"), tempRepoDir, false) //nolint:gosec err := os.WriteFile(destConfigFile, updatedServerConfig, 0500) if err != nil { t.Fatal(err) } // stuff svc.Reload() // check policy file to make sure it was saved expectedPolicies := [][]string{ {"role:maintainers", "/thisismynewrepo/info/refs", "GET"}, {"role:maintainers", "/thisismynewrepo/git-upload-pack", "POST"}, {"role:maintainers", "/thisismynewrepo/git-receive-pack", "POST"}, } for _, policy := range expectedPolicies { if !svc.HasPolicy(policy[0], policy[1], policy[2]) { t.Log("policy not found", policy) t.Fail() } } }) t.Run("test reload config err", func(t *testing.T) { svc, _ := NewService(destModelFile, destPolicyFile, // TODO set abs path filepath.Join(tempRepoDir, "gitserver.yaml"), tempRepoDir, false) notAGoodConfig := []byte("this is not valid yaml") //nolint:gosec err := os.WriteFile(destConfigFile, notAGoodConfig, 0500) if err != nil { t.Fatal(err) } // stuff svc.Reload() // check policy file to make sure it wasn't saved expectedPolicies := [][]string{ {"role:admin", "/mgmt/info/refs", "GET"}, {"role:admin", "/mgmt/git-upload-pack", "POST"}, {"role:admin", "/mgmt/git-receive-pack", "POST"}, } for _, policy := range expectedPolicies { if !svc.HasPolicy(policy[0], policy[1], policy[2]) { t.Log("policy not found", policy) t.Fail() } } }) t.Run("test an unitialized server config", func(t *testing.T) { tempRepoDir := t.TempDir() svc, _ := NewService(destModelFile, destPolicyFile, // TODO set abs path "gitserver.yaml", tempRepoDir, true) if svc.Conf.Name != "default config" { log.Fatalf("found %s expected 'default config'", svc.Conf.Name) t.Fail() } }) }