Skip to content

[TESTS] auth LinkAccount test coverage #25663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions routers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ func InitWebInstalled(ctx context.Context) {
}

// NormalRoutes represents non install routes
func NormalRoutes() *web.Route {
func NormalRoutes(middlewares ...any) *web.Route {
_ = templates.HTMLRenderer()
r := web.NewRoute()
r.Use(common.ProtocolMiddlewares()...)

r.Mount("/", web_routers.Routes())
r.Mount("/", web_routers.Routes(middlewares...))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can understand the logic by reading this PR, but, it will be a problem for future developers: why the "middlewares" appear here? Why it only applies to "/" ? How it co-work with others?

TBH, I do not think it is a good design.

r.Mount("/api/v1", apiv1.Routes())
r.Mount("/api/internal", private.Routes())

Expand Down
3 changes: 2 additions & 1 deletion routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func ctxDataSet(args ...any) func(ctx *context.Context) {
}

// Routes returns all web routes
func Routes() *web.Route {
func Routes(middlewares ...any) *web.Route {
Copy link
Contributor

@wxiaoguang wxiaoguang Jul 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

middlewares ...any introduces strange smell to the code base, no comment, and it might be abused in the future. And it's unclear what's the proper order for these middlewares, how they co-work with other middlewares.

routes := web.NewRoute()

routes.Head("/", misc.DummyOK) // for health check - doesn't need to be passed through gzip handler
Expand Down Expand Up @@ -161,6 +161,7 @@ func Routes() *web.Route {
mid = append(mid, user.GetNotificationCount)
mid = append(mid, repo.GetActiveStopwatch)
mid = append(mid, goGet)
mid = append(mid, middlewares...)

others := web.NewRoute()
others.Use(mid...)
Expand Down
13 changes: 6 additions & 7 deletions tests/integration/api_activitypub_person_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ 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"
)

func TestActivityPubPerson(t *testing.T) {
setting.Federation.Enabled = true
c = routers.NormalRoutes()
setNormalRoutes()
defer func() {
setting.Federation.Enabled = false
c = routers.NormalRoutes()
setNormalRoutes()
}()

onGiteaRun(t, func(*testing.T, *url.URL) {
Expand Down Expand Up @@ -60,10 +59,10 @@ func TestActivityPubPerson(t *testing.T) {

func TestActivityPubMissingPerson(t *testing.T) {
setting.Federation.Enabled = true
c = routers.NormalRoutes()
setNormalRoutes()
defer func() {
setting.Federation.Enabled = false
c = routers.NormalRoutes()
setNormalRoutes()
}()

onGiteaRun(t, func(*testing.T, *url.URL) {
Expand All @@ -75,10 +74,10 @@ func TestActivityPubMissingPerson(t *testing.T) {

func TestActivityPubPersonInbox(t *testing.T) {
setting.Federation.Enabled = true
c = routers.NormalRoutes()
setNormalRoutes()
defer func() {
setting.Federation.Enabled = false
c = routers.NormalRoutes()
setNormalRoutes()
}()

srv := httptest.NewServer(c)
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/api_nodeinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import (

"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()
setNormalRoutes()
defer func() {
setting.Federation.Enabled = false
c = routers.NormalRoutes()
setNormalRoutes()
}()

onGiteaRun(t, func(*testing.T, *url.URL) {
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/create_no_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,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"
Expand Down Expand Up @@ -56,7 +55,7 @@ func TestSessionFileCreation(t *testing.T) {
oldSessionConfig := setting.SessionConfig.ProviderConfig
defer func() {
setting.SessionConfig.ProviderConfig = oldSessionConfig
c = routers.NormalRoutes()
setNormalRoutes()
}()

var config session.Options
Expand All @@ -75,7 +74,7 @@ func TestSessionFileCreation(t *testing.T) {

setting.SessionConfig.ProviderConfig = string(newConfigBytes)

c = routers.NormalRoutes()
setNormalRoutes()

t.Run("NoSessionOnViewIssue", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
Expand Down
34 changes: 31 additions & 3 deletions tests/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -33,14 +34,27 @@ 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"
"github.com/stretchr/testify/assert"
"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(middlewareHook)
}

type NilResponseRecorder struct {
httptest.ResponseRecorder
Expand Down Expand Up @@ -87,8 +101,7 @@ func TestMain(m *testing.M) {
defer cancel()

tests.InitTest(true)
c = routers.NormalRoutes()

setNormalRoutes()
// integration test settings...
if setting.CfgProvider != nil {
testingCfg := setting.CfgProvider.Section("integration-tests")
Expand Down Expand Up @@ -228,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))
}
Comment on lines +254 to +256
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is no need to do so. PrepareTestEnv will reset all test data

}

func loginUser(t testing.TB, userName string) *TestSession {
t.Helper()

Expand Down
63 changes: 63 additions & 0 deletions tests/integration/linkaccount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2023 The Gitea 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 := "[email protected]"
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)
}
}