From 6623630d1070c6c0f195197ad769b2aed15a50b9 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Mon, 5 Jun 2023 11:04:47 +0200 Subject: [PATCH 1/4] [TESTS] testMiddlewareHook, dependency injection in integration tests --- modules/web/route.go | 4 ++++ routers/web/web.go | 4 ++++ .../integration/api_activitypub_person_test.go | 13 ++++++------- tests/integration/api_nodeinfo_test.go | 6 ++---- tests/integration/create_no_session_test.go | 6 ++---- tests/integration/integration_test.go | 17 ++++++++++++++--- 6 files changed, 32 insertions(+), 18 deletions(-) diff --git a/modules/web/route.go b/modules/web/route.go index d801f1025c..3a7e690659 100644 --- a/modules/web/route.go +++ b/modules/web/route.go @@ -14,6 +14,10 @@ import ( chi "github.com/go-chi/chi/v5" ) +type KeyTestMiddlewareHookType string + +var KeyTestMiddlewareHook = KeyTestMiddlewareHookType("testMiddlewareHook") + // Bind binding an obj to a handler func Bind[T any](_ T) any { return func(ctx *context.Context) { diff --git a/routers/web/web.go b/routers/web/web.go index 037e3075c5..786cfcc1e7 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -167,6 +167,10 @@ func Routes(ctx gocontext.Context) *web.Route { mid = append(mid, user.GetNotificationCount) mid = append(mid, repo.GetActiveStopwatch) mid = append(mid, goGet) + middlewareHook := ctx.Value(web.KeyTestMiddlewareHook) + if middlewareHook != nil { + mid = append(mid, middlewareHook) + } others := web.NewRoute() others.Use(mid...) diff --git a/tests/integration/api_activitypub_person_test.go b/tests/integration/api_activitypub_person_test.go index 301cfba172..140cf0f034 100644 --- a/tests/integration/api_activitypub_person_test.go +++ b/tests/integration/api_activitypub_person_test.go @@ -14,7 +14,6 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/routers" ap "github.com/go-ap/activitypub" "github.com/stretchr/testify/assert" @@ -22,10 +21,10 @@ import ( func TestActivityPubPerson(t *testing.T) { setting.Federation.Enabled = true - c = routers.NormalRoutes(context.TODO()) + setNormalRoutes() defer func() { setting.Federation.Enabled = false - c = routers.NormalRoutes(context.TODO()) + setNormalRoutes() }() onGiteaRun(t, func(*testing.T, *url.URL) { @@ -60,10 +59,10 @@ func TestActivityPubPerson(t *testing.T) { func TestActivityPubMissingPerson(t *testing.T) { setting.Federation.Enabled = true - c = routers.NormalRoutes(context.TODO()) + setNormalRoutes() defer func() { setting.Federation.Enabled = false - c = routers.NormalRoutes(context.TODO()) + setNormalRoutes() }() onGiteaRun(t, func(*testing.T, *url.URL) { @@ -75,10 +74,10 @@ func TestActivityPubMissingPerson(t *testing.T) { func TestActivityPubPersonInbox(t *testing.T) { setting.Federation.Enabled = true - c = routers.NormalRoutes(context.TODO()) + setNormalRoutes() defer func() { setting.Federation.Enabled = false - c = routers.NormalRoutes(context.TODO()) + setNormalRoutes() }() srv := httptest.NewServer(c) diff --git a/tests/integration/api_nodeinfo_test.go b/tests/integration/api_nodeinfo_test.go index bc2f11a7f2..3052d95be0 100644 --- a/tests/integration/api_nodeinfo_test.go +++ b/tests/integration/api_nodeinfo_test.go @@ -4,24 +4,22 @@ package integration import ( - "context" "net/http" "net/url" "testing" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - "code.gitea.io/gitea/routers" "github.com/stretchr/testify/assert" ) func TestNodeinfo(t *testing.T) { setting.Federation.Enabled = true - c = routers.NormalRoutes(context.TODO()) + setNormalRoutes() defer func() { setting.Federation.Enabled = false - c = routers.NormalRoutes(context.TODO()) + setNormalRoutes() }() onGiteaRun(t, func(*testing.T, *url.URL) { diff --git a/tests/integration/create_no_session_test.go b/tests/integration/create_no_session_test.go index 9a96ed61fa..81e02e3c2c 100644 --- a/tests/integration/create_no_session_test.go +++ b/tests/integration/create_no_session_test.go @@ -4,7 +4,6 @@ package integration import ( - "context" "net/http" "net/http/httptest" "os" @@ -13,7 +12,6 @@ import ( "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/routers" "code.gitea.io/gitea/tests" "gitea.com/go-chi/session" @@ -57,7 +55,7 @@ func TestSessionFileCreation(t *testing.T) { oldSessionConfig := setting.SessionConfig.ProviderConfig defer func() { setting.SessionConfig.ProviderConfig = oldSessionConfig - c = routers.NormalRoutes(context.TODO()) + setNormalRoutes() }() var config session.Options @@ -76,7 +74,7 @@ func TestSessionFileCreation(t *testing.T) { setting.SessionConfig.ProviderConfig = string(newConfigBytes) - c = routers.NormalRoutes(context.TODO()) + setNormalRoutes() t.Run("NoSessionOnViewIssue", func(t *testing.T) { defer tests.PrintCurrentTest(t)() diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 27918a9ccc..4d9ecf3faf 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -40,7 +40,19 @@ import ( "github.com/xeipuuv/gojsonschema" ) -var c *web.Route +var ( + c *web.Route + testMiddlewareHook func(*gitea_context.Context) +) + +func setNormalRoutes() { + middlewareHook := func(ctx *gitea_context.Context) { + if testMiddlewareHook != nil { + testMiddlewareHook(ctx) + } + } + c = routers.NormalRoutes(context.WithValue(context.Background(), web.KeyTestMiddlewareHook, middlewareHook)) +} type NilResponseRecorder struct { httptest.ResponseRecorder @@ -87,8 +99,7 @@ func TestMain(m *testing.M) { defer cancel() tests.InitTest(true) - c = routers.NormalRoutes(context.TODO()) - + setNormalRoutes() // integration test settings... if setting.CfgProvider != nil { testingCfg := setting.CfgProvider.Section("integration-tests") From c1d14c5fffeb823385b2984cfcdb3e195bfb151d Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Mon, 5 Jun 2023 11:43:31 +0200 Subject: [PATCH 2/4] [TESTS] createUser via the user model helper for integration tests --- tests/integration/integration_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 4d9ecf3faf..7cc7a45018 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -24,6 +24,7 @@ import ( "code.gitea.io/gitea/models/auth" "code.gitea.io/gitea/models/unittest" + user_model "code.gitea.io/gitea/models/user" gitea_context "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/json" @@ -33,6 +34,7 @@ import ( "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers" + user_service "code.gitea.io/gitea/services/user" "code.gitea.io/gitea/tests" "github.com/PuerkitoBio/goquery" @@ -239,6 +241,21 @@ func getUserToken(t testing.TB, userName string, scope ...auth.AccessTokenScope) return getTokenForLoggedInUser(t, loginUser(t, userName), scope...) } +func createUser(t testing.TB, userName, email, password string) func() { + u := &user_model.User{ + Name: userName, + Email: email, + Passwd: password, + MustChangePassword: false, + LoginType: auth.Plain, + } + + assert.NoError(t, user_model.CreateUser(u, &user_model.CreateUserOverwriteOptions{})) + return func() { + assert.NoError(t, user_service.DeleteUser(context.Background(), u, true)) + } +} + func loginUser(t testing.TB, userName string) *TestSession { t.Helper() From e11dcc60f291f1b882a993f60f8381fe4561d6d0 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Mon, 5 Jun 2023 11:29:07 +0200 Subject: [PATCH 3/4] [TESTS] auth LinkAccount test coverage --- tests/integration/linkaccount_test.go | 63 +++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/integration/linkaccount_test.go diff --git a/tests/integration/linkaccount_test.go b/tests/integration/linkaccount_test.go new file mode 100644 index 0000000000..48b1ac3650 --- /dev/null +++ b/tests/integration/linkaccount_test.go @@ -0,0 +1,63 @@ +// Copyright 2023 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package integration + +import ( + "net/http" + "testing" + + gitea_context "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/tests" + + "github.com/markbates/goth" + "github.com/stretchr/testify/assert" +) + +func TestLinkAccountChoose(t *testing.T) { + defer tests.PrepareTestEnv(t)() + username := "linkaccountuser" + email := "linkaccountuser@example.com" + password := "linkaccountuser" + defer createUser(t, username, email, password)() + + defer func() { + testMiddlewareHook = nil + }() + + for _, testCase := range []struct { + gothUser goth.User + signupTab string + signinTab string + }{ + { + gothUser: goth.User{}, + signupTab: "item active", + signinTab: "item ", + }, + { + gothUser: goth.User{ + Email: email, + }, + signupTab: "item ", + signinTab: "item active", + }, + } { + testMiddlewareHook = func(ctx *gitea_context.Context) { + ctx.Session.Set("linkAccountGothUser", testCase.gothUser) + } + + req := NewRequest(t, "GET", "/user/link_account") + resp := MakeRequest(t, req, http.StatusOK) + assert.Equal(t, resp.Code, http.StatusOK, resp.Body) + doc := NewHTMLParser(t, resp.Body) + + class, exists := doc.Find(".new-menu-inner .item[data-tab=\"auth-link-signup-tab\"]").Attr("class") + assert.True(t, exists, resp.Body) + assert.Equal(t, testCase.signupTab, class) + + class, exists = doc.Find(".new-menu-inner .item[data-tab=\"auth-link-signin-tab\"]").Attr("class") + assert.True(t, exists) + assert.Equal(t, testCase.signinTab, class) + } +} From 34212791eef2031ef09ea118a2ee5b98082174dc Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Mon, 5 Jun 2023 18:42:17 +0200 Subject: [PATCH 4/4] use backticks to avoid backslash --- tests/integration/linkaccount_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/linkaccount_test.go b/tests/integration/linkaccount_test.go index 48b1ac3650..e7b9f9c261 100644 --- a/tests/integration/linkaccount_test.go +++ b/tests/integration/linkaccount_test.go @@ -52,11 +52,11 @@ func TestLinkAccountChoose(t *testing.T) { assert.Equal(t, resp.Code, http.StatusOK, resp.Body) doc := NewHTMLParser(t, resp.Body) - class, exists := doc.Find(".new-menu-inner .item[data-tab=\"auth-link-signup-tab\"]").Attr("class") + class, exists := doc.Find(`.new-menu-inner .item[data-tab="auth-link-signup-tab"]`).Attr("class") assert.True(t, exists, resp.Body) assert.Equal(t, testCase.signupTab, class) - class, exists = doc.Find(".new-menu-inner .item[data-tab=\"auth-link-signin-tab\"]").Attr("class") + class, exists = doc.Find(`.new-menu-inner .item[data-tab="auth-link-signin-tab"]`).Attr("class") assert.True(t, exists) assert.Equal(t, testCase.signinTab, class) }