diff options
| author | Max Resnick <max@ofmax.li> | 2022-12-26 08:37:55 -0800 |
|---|---|---|
| committer | Max Resnick <max@ofmax.li> | 2022-12-26 08:37:55 -0800 |
| commit | 0ac7b93645b169c55f9c50423fab9d4a402e9918 (patch) | |
| tree | d4ec51e99e073d294c0bee030af29b38212f8146 /internal | |
| parent | ec633ec4c55f0098535d6a438e3bc4c3786ad486 (diff) | |
| download | go-git-server-0ac7b93645b169c55f9c50423fab9d4a402e9918.tar.gz | |
update tests for latest refactor
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/admin/model.go | 6 | ||||
| -rw-r--r-- | internal/admin/model_test.go | 47 | ||||
| -rw-r--r-- | internal/authz/middleware.go | 2 | ||||
| -rw-r--r-- | internal/authz/middleware_test.go | 21 | ||||
| -rw-r--r-- | internal/git/handler_test.go | 2 |
5 files changed, 55 insertions, 23 deletions
diff --git a/internal/admin/model.go b/internal/admin/model.go index c0fb888..1b3da00 100644 --- a/internal/admin/model.go +++ b/internal/admin/model.go @@ -147,10 +147,9 @@ func (r *GitRepo) ReconcileRepo(basePath string) { } // ConfigureExport setup repo for sharing and configure web settings -func (r *GitRepo) ConfigureExport(basePath string) { +func (r *GitRepo) ConfigureExport(repoBase string) { // do nothing on public repos - repoBase := fmt.Sprintf("%s.git", r.Name) - okExport := filepath.Join(repoBase, r.Name, GitExportMagic) + okExport := filepath.Join(repoBase, GitExportMagic) _, err := os.Stat(okExport) // Not public but the export setting is setting exists if !r.Public && err == nil { @@ -167,7 +166,6 @@ func (r *GitRepo) ConfigureExport(basePath string) { defer f.Close() if err != nil { log.Fatalf("git-daemon-export-ok coudln't be created %s", err) - return } } diff --git a/internal/admin/model_test.go b/internal/admin/model_test.go index 4ec1177..e8f6e33 100644 --- a/internal/admin/model_test.go +++ b/internal/admin/model_test.go @@ -1,7 +1,9 @@ package admin import ( + "errors" "fmt" + "io/fs" "os" "path/filepath" "testing" @@ -74,17 +76,21 @@ func TestCasbinPolicies(t *testing.T) { 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, "testrepo.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() + repo := &GitRepo{ + Public: true, + Name: "testrepo", + } + t.Run("test add gitweb section and remove it", 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", @@ -93,6 +99,9 @@ func TestConfigReconcile(t *testing.T) { } 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`") } @@ -113,4 +122,22 @@ func TestConfigReconcile(t *testing.T) { 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, GitExportMagic) + 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) + } + // 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") + } + }) } diff --git a/internal/authz/middleware.go b/internal/authz/middleware.go index 41672f2..f01f262 100644 --- a/internal/authz/middleware.go +++ b/internal/authz/middleware.go @@ -40,7 +40,7 @@ func Authentication(authMap TokenMap, next http.Handler) http.Handler { } // Authorization middleware to enforce authoirzation of all requests. -func Authorization(adminSvc *admin.Service, next http.Handler) http.Handler { +func Authorization(adminSvc *admin.Servicer, next http.Handler) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { ctx := req.Context() urn := ctx.Value("urn") diff --git a/internal/authz/middleware_test.go b/internal/authz/middleware_test.go index 8dbc30f..5795b3f 100644 --- a/internal/authz/middleware_test.go +++ b/internal/authz/middleware_test.go @@ -7,11 +7,14 @@ import ( "net/http/httptest" "testing" + "git.ofmax.li/go-git-server/internal/admin" "github.com/casbin/casbin/v2" ) -func junkTestHandler(rw http.ResponseWriter, req *http.Request) { - rw.WriteHeader(http.StatusOK) +func junkTestHandler() http.HandlerFunc { + return func(rw http.ResponseWriter, req *http.Request) { + rw.WriteHeader(http.StatusOK) + } } func TestAuthentication(t *testing.T) { @@ -28,7 +31,7 @@ func TestAuthentication(t *testing.T) { token string tm TokenMap statusCode int - handler func(http.ResponseWriter, *http.Request) + handler http.HandlerFunc }{ { username: okUserName, @@ -51,7 +54,7 @@ func TestAuthentication(t *testing.T) { tm: tm, statusCode: http.StatusForbidden, description: "Bad usename", - handler: junkTestHandler, + handler: junkTestHandler(), }, { username: okUserName, @@ -59,7 +62,7 @@ func TestAuthentication(t *testing.T) { tm: tm, statusCode: http.StatusForbidden, description: "Bad token", - handler: junkTestHandler, + handler: junkTestHandler(), }, } @@ -82,7 +85,7 @@ func TestAuthentication(t *testing.T) { func TestAuthorization(t *testing.T) { t.Log("Starting authorization tests") baseURL := "http://test" - enf, err := casbin.NewEnforcer("../../auth_model.ini", "../../testpolicy.csv") + enf, err := casbin.NewSyncedEnforcer("../../auth_model.ini", "../../testpolicy.csv") if err != nil { t.Fatalf("Failed to load policies\n%s", err) } @@ -105,9 +108,13 @@ func TestAuthorization(t *testing.T) { description: "an unautorized action should yield a 403", }, } + svcr := &admin.Servicer{ + enf, + &admin.ServerRepos{}, + } for _, tc := range cases { t.Logf("test case: %s", tc.description) - authHandler := Authorization(enf, junkTestHandler) + authHandler := Authorization(svcr, junkTestHandler()) recorder := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, tc.url, nil) ctx := req.Context() diff --git a/internal/git/handler_test.go b/internal/git/handler_test.go index 88267b9..f9b4cd7 100644 --- a/internal/git/handler_test.go +++ b/internal/git/handler_test.go @@ -10,5 +10,5 @@ func TestGitHandler(t *testing.T) { if err != nil { t.Fatalf("Couldn't create a temp directory for tests: %s", err) } - _ := GitHttpBackendHandler(dir, "git http-backend") + _ = GitHttpBackendHandler(dir, "git http-backend") } |