-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[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
Changes from all commits
c722fc9
a60c98d
eb6f451
a9838b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
routes := web.NewRoute() | ||
|
||
routes.Head("/", misc.DummyOK) // for health check - doesn't need to be passed through gzip handler | ||
|
@@ -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...) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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 | ||
|
@@ -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") | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is no need to do so. |
||
} | ||
|
||
func loginUser(t testing.TB, userName string) *TestSession { | ||
t.Helper() | ||
|
||
|
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) | ||
} | ||
} |
There was a problem hiding this comment.
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.