mirror of
https://github.com/go-gitea/gitea
synced 2025-07-03 09:07:19 +00:00
Refactor web package and context package (#25298)
1. The "web" package shouldn't depends on "modules/context" package, instead, let each "web context" register themselves to the "web" package. 2. The old Init/Free doesn't make sense, so simplify it * The ctx in "Init(ctx)" is never used, and shouldn't be used that way * The "Free" is never called and shouldn't be called because the SSPI instance is shared --------- Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
@ -64,7 +64,6 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
gocontext "context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
@ -705,7 +704,7 @@ func buildAuthGroup() *auth.Group {
|
||||
}
|
||||
|
||||
// Routes registers all v1 APIs routes to web application.
|
||||
func Routes(ctx gocontext.Context) *web.Route {
|
||||
func Routes() *web.Route {
|
||||
m := web.NewRoute()
|
||||
|
||||
m.Use(securityHeaders())
|
||||
@ -722,13 +721,8 @@ func Routes(ctx gocontext.Context) *web.Route {
|
||||
}
|
||||
m.Use(context.APIContexter())
|
||||
|
||||
group := buildAuthGroup()
|
||||
if err := group.Init(ctx); err != nil {
|
||||
log.Error("Could not initialize '%s' auth method, error: %s", group.Name(), err)
|
||||
}
|
||||
|
||||
// Get user from session if logged in.
|
||||
m.Use(auth.APIAuth(group))
|
||||
m.Use(auth.APIAuth(buildAuthGroup()))
|
||||
|
||||
m.Use(auth.VerifyAuthWithOptionsAPI(&auth.VerifyOptions{
|
||||
SignInRequired: setting.Service.RequireSignInView,
|
||||
|
@ -7,18 +7,14 @@ import (
|
||||
go_context "context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/markup"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/test"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
"code.gitea.io/gitea/modules/web/middleware"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@ -29,34 +25,16 @@ const (
|
||||
AppSubURL = AppURL + Repo + "/"
|
||||
)
|
||||
|
||||
func createAPIContext(req *http.Request) (*context.APIContext, *httptest.ResponseRecorder) {
|
||||
resp := httptest.NewRecorder()
|
||||
base, baseCleanUp := context.NewBaseContext(resp, req)
|
||||
base.Data = middleware.ContextData{}
|
||||
c := &context.APIContext{Base: base}
|
||||
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later
|
||||
|
||||
return c, resp
|
||||
}
|
||||
|
||||
func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, responseCode int) {
|
||||
setting.AppURL = AppURL
|
||||
|
||||
options := api.MarkupOption{
|
||||
Mode: mode,
|
||||
Text: "",
|
||||
Text: text,
|
||||
Context: Repo,
|
||||
Wiki: true,
|
||||
FilePath: filePath,
|
||||
}
|
||||
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markup"))
|
||||
req := &http.Request{
|
||||
Method: "POST",
|
||||
URL: requrl,
|
||||
}
|
||||
ctx, resp := createAPIContext(req)
|
||||
|
||||
options.Text = text
|
||||
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markup")
|
||||
web.SetForm(ctx, &options)
|
||||
Markup(ctx)
|
||||
assert.Equal(t, responseBody, resp.Body.String())
|
||||
@ -66,21 +44,13 @@ func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, r
|
||||
|
||||
func testRenderMarkdown(t *testing.T, mode, text, responseBody string, responseCode int) {
|
||||
setting.AppURL = AppURL
|
||||
|
||||
options := api.MarkdownOption{
|
||||
Mode: mode,
|
||||
Text: "",
|
||||
Text: text,
|
||||
Context: Repo,
|
||||
Wiki: true,
|
||||
}
|
||||
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
|
||||
req := &http.Request{
|
||||
Method: "POST",
|
||||
URL: requrl,
|
||||
}
|
||||
ctx, resp := createAPIContext(req)
|
||||
|
||||
options.Text = text
|
||||
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
|
||||
web.SetForm(ctx, &options)
|
||||
Markdown(ctx)
|
||||
assert.Equal(t, responseBody, resp.Body.String())
|
||||
@ -187,19 +157,12 @@ var simpleCases = []string{
|
||||
|
||||
func TestAPI_RenderSimple(t *testing.T) {
|
||||
setting.AppURL = AppURL
|
||||
|
||||
options := api.MarkdownOption{
|
||||
Mode: "markdown",
|
||||
Text: "",
|
||||
Context: Repo,
|
||||
}
|
||||
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
|
||||
req := &http.Request{
|
||||
Method: "POST",
|
||||
URL: requrl,
|
||||
}
|
||||
ctx, resp := createAPIContext(req)
|
||||
|
||||
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
|
||||
for i := 0; i < len(simpleCases); i += 2 {
|
||||
options.Text = simpleCases[i]
|
||||
web.SetForm(ctx, &options)
|
||||
@ -211,14 +174,7 @@ func TestAPI_RenderSimple(t *testing.T) {
|
||||
|
||||
func TestAPI_RenderRaw(t *testing.T) {
|
||||
setting.AppURL = AppURL
|
||||
|
||||
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
|
||||
req := &http.Request{
|
||||
Method: "POST",
|
||||
URL: requrl,
|
||||
}
|
||||
ctx, resp := createAPIContext(req)
|
||||
|
||||
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
|
||||
for i := 0; i < len(simpleCases); i += 2 {
|
||||
ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i]))
|
||||
MarkdownRaw(ctx)
|
||||
|
@ -17,7 +17,7 @@ import (
|
||||
func TestTestHook(t *testing.T) {
|
||||
unittest.PrepareTestEnv(t)
|
||||
|
||||
ctx := test.MockAPIContext(t, "user2/repo1/wiki/_pages")
|
||||
ctx, _ := test.MockAPIContext(t, "user2/repo1/wiki/_pages")
|
||||
ctx.SetParams(":id", "1")
|
||||
test.LoadRepo(t, ctx, 1)
|
||||
test.LoadRepoCommit(t, ctx)
|
||||
|
@ -19,7 +19,7 @@ import (
|
||||
func TestRepoEdit(t *testing.T) {
|
||||
unittest.PrepareTestEnv(t)
|
||||
|
||||
ctx := test.MockAPIContext(t, "user2/repo1")
|
||||
ctx, _ := test.MockAPIContext(t, "user2/repo1")
|
||||
test.LoadRepo(t, ctx, 1)
|
||||
test.LoadUser(t, ctx, 2)
|
||||
ctx.Repo.Owner = ctx.Doer
|
||||
@ -65,7 +65,7 @@ func TestRepoEdit(t *testing.T) {
|
||||
func TestRepoEditNameChange(t *testing.T) {
|
||||
unittest.PrepareTestEnv(t)
|
||||
|
||||
ctx := test.MockAPIContext(t, "user2/repo1")
|
||||
ctx, _ := test.MockAPIContext(t, "user2/repo1")
|
||||
test.LoadRepo(t, ctx, 1)
|
||||
test.LoadUser(t, ctx, 2)
|
||||
ctx.Repo.Owner = ctx.Doer
|
||||
|
Reference in New Issue
Block a user