Skip to content

Commit

Permalink
fine tune
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed May 18, 2023
1 parent 0ee21bf commit 2de3d6a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions modules/context/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type packageAssignmentCtx struct {
ContextUser *user_model.User
}

// PackageAssignmentWeb returns a middleware to handle Context.Package assignment
func PackageAssignmentWeb() func(ctx *Context) {
// PackageAssignment returns a middleware to handle Context.Package assignment
func PackageAssignment() func(ctx *Context) {
return func(ctx *Context) {
errorFn := func(status int, title string, obj interface{}) {
err, ok := obj.(error)
Expand Down
26 changes: 13 additions & 13 deletions modules/test/context_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ import (
"github.com/stretchr/testify/assert"
)

type mockRender struct{}

func (tr *mockRender) TemplateLookup(tmpl string) (templates.TemplateExecutor, error) {
return nil, nil
}

func (tr *mockRender) HTML(w io.Writer, status int, _ string, _ interface{}) error {
if resp, ok := w.(http.ResponseWriter); ok {
resp.WriteHeader(status)
}
return nil
}

// MockContext mock context for unit tests
// TODO: move this function to other packages, because it depends on "models" package
func MockContext(t *testing.T, path string) *context.Context {
Expand Down Expand Up @@ -158,3 +145,16 @@ func LoadGitRepo(t *testing.T, ctx *context.Context) {
ctx.Repo.GitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath())
assert.NoError(t, err)
}

type mockRender struct{}

func (tr *mockRender) TemplateLookup(tmpl string) (templates.TemplateExecutor, error) {
return nil, nil
}

func (tr *mockRender) HTML(w io.Writer, status int, _ string, _ interface{}) error {
if resp, ok := w.(http.ResponseWriter); ok {
resp.WriteHeader(status)
}
return nil
}
4 changes: 2 additions & 2 deletions routers/api/packages/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func CommonRoutes(ctx gocontext.Context) *web.Route {
})
})
}, reqPackageAccess(perm.AccessModeRead))
}, context_service.UserAssignmentWeb(), context.PackageAssignmentWeb())
}, context_service.UserAssignmentWeb(), context.PackageAssignment())

return r
}
Expand Down Expand Up @@ -730,7 +730,7 @@ func ContainerRoutes(ctx gocontext.Context) *web.Route {

ctx.Status(http.StatusNotFound)
})
}, container.ReqContainerAccess, context_service.UserAssignmentWeb(), context.PackageAssignmentWeb(), reqPackageAccess(perm.AccessModeRead))
}, container.ReqContainerAccess, context_service.UserAssignmentWeb(), context.PackageAssignment(), reqPackageAccess(perm.AccessModeRead))

return r
}
2 changes: 1 addition & 1 deletion routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ func registerRoutes(m *web.Route) {
}, reqPackageAccess(perm.AccessModeWrite))
})
})
}, context.PackageAssignmentWeb(), reqPackageAccess(perm.AccessModeRead))
}, context.PackageAssignment(), reqPackageAccess(perm.AccessModeRead))
}

m.Group("/projects", func() {
Expand Down
1 change: 1 addition & 0 deletions services/auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func APIAuth(authMethod Method) func(*context.APIContext) {
ar, err := authShared(ctx.Base, nil, authMethod)
if err != nil {
ctx.Error(http.StatusUnauthorized, "APIAuth", err)
return
}
ctx.Doer = ar.Doer
ctx.IsSigned = ar.Doer != nil
Expand Down

0 comments on commit 2de3d6a

Please sign in to comment.