loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: load 2fa status for user search when needed (#6727)

- Don't make an extra database call to gather the 2FA status of the users returned from the search. Only load it for the admin's user list page.
- Integration testing added.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6727
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>

authored by

Gusted
Gusted
and committed by
Gusted
59dfab2d 4cc0320e

+33 -7
+1
models/user/search.go
··· 40 40 IsProhibitLogin optional.Option[bool] 41 41 IncludeReserved bool 42 42 43 + Load2FAStatus bool 43 44 ExtraParamStrings map[string]string 44 45 } 45 46
+1
routers/web/admin/users.go
··· 83 83 IsTwoFactorEnabled: util.OptionalBoolParse(statusFilterMap["is_2fa_enabled"]), 84 84 IsProhibitLogin: util.OptionalBoolParse(statusFilterMap["is_prohibit_login"]), 85 85 IncludeReserved: true, // administrator needs to list all accounts include reserved, bot, remote ones 86 + Load2FAStatus: true, 86 87 ExtraParamStrings: extraParamStrings, 87 88 }, tplUsers) 88 89 }
+3 -1
routers/web/explore/user.go
··· 114 114 ctx.Data["Keyword"] = opts.Keyword 115 115 ctx.Data["Total"] = count 116 116 ctx.Data["Users"] = users 117 - ctx.Data["UsersTwoFaStatus"] = user_model.UserList(users).GetTwoFaStatus(ctx) 117 + if opts.Load2FAStatus { 118 + ctx.Data["UsersTwoFaStatus"] = user_model.UserList(users).GetTwoFaStatus(ctx) 119 + } 118 120 ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail 119 121 ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled 120 122
+28 -6
tests/integration/admin_user_test.go
··· 26 26 func TestAdminViewUsers(t *testing.T) { 27 27 defer tests.PrepareTestEnv(t)() 28 28 29 - session := loginUser(t, "user1") 30 - req := NewRequest(t, "GET", "/admin/users") 31 - session.MakeRequest(t, req, http.StatusOK) 29 + t.Run("Admin user", func(t *testing.T) { 30 + defer tests.PrintCurrentTest(t)() 31 + 32 + session := loginUser(t, "user1") 33 + req := NewRequest(t, "GET", "/admin/users") 34 + session.MakeRequest(t, req, http.StatusOK) 35 + 36 + req = NewRequest(t, "GET", "/admin/users?status_filter[is_2fa_enabled]=1") 37 + resp := session.MakeRequest(t, req, http.StatusOK) 38 + htmlDoc := NewHTMLParser(t, resp.Body) 32 39 33 - session = loginUser(t, "user2") 34 - req = NewRequest(t, "GET", "/admin/users") 35 - session.MakeRequest(t, req, http.StatusForbidden) 40 + // 6th column is the 2FA column. 41 + htmlDoc.AssertElement(t, ".admin-setting-content table tbody tr td:nth-child(6) .octicon-check", true) 42 + }) 43 + 44 + t.Run("Normal user", func(t *testing.T) { 45 + defer tests.PrintCurrentTest(t)() 46 + 47 + session := loginUser(t, "user2") 48 + req := NewRequest(t, "GET", "/admin/users") 49 + session.MakeRequest(t, req, http.StatusForbidden) 50 + }) 51 + 52 + t.Run("Anonymous user", func(t *testing.T) { 53 + defer tests.PrintCurrentTest(t)() 54 + 55 + req := NewRequest(t, "GET", "/admin/users") 56 + MakeRequest(t, req, http.StatusSeeOther) 57 + }) 36 58 } 37 59 38 60 func TestAdminViewUser(t *testing.T) {