From 9a2b46577bc9dd1736bbc866db38ccc4bbe3f9dd Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 21 Oct 2022 14:08:08 +0000 Subject: [PATCH 01/14] Add link to user profile in markdown mention only if user exists Signed-off-by: Yarden Shoham --- modules/markup/html.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index a5606dbb516ad..3f853349ec2bc 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -14,6 +14,8 @@ import ( "strings" "sync" + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/emoji" "code.gitea.io/gitea/modules/git" @@ -603,8 +605,20 @@ func mentionProcessor(ctx *RenderContext, node *html.Node) { start = loc.End continue } - replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, mention[1:]), mention, "mention")) - node = node.NextSibling.NextSibling + mentionedUsername := mention[1:] + + // Only link if the user actually exists + userExists, err := user.IsUserExist(db.DefaultContext, 0, mentionedUsername) + if err != nil { + log.Error("Failed to validate user in mention %v exists, assuming it does", mention) + userExists = true + } + if userExists { + replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, mentionedUsername), mention, "mention")) + node = node.NextSibling.NextSibling + } else { + node = node.NextSibling + } start = 0 } } From 27dd53427d4ec0162c9c1153bdc3ef866740a3a6 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 21 Oct 2022 18:37:50 +0000 Subject: [PATCH 02/14] Trigger build From 6fb9af9c5a62450b1841b2455ce248ae0c0fa374 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 21 Oct 2022 20:47:23 +0000 Subject: [PATCH 03/14] Add user to tests --- models/fixtures/user.yml | 37 ++++++++++++++++++++++++ modules/markup/markdown/markdown_test.go | 10 +++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/models/fixtures/user.yml b/models/fixtures/user.yml index 0e3348e146a82..b332b9ff36f47 100644 --- a/models/fixtures/user.yml +++ b/models/fixtures/user.yml @@ -1220,3 +1220,40 @@ repo_admin_change_team_access: false theme: "" keep_activity_private: false + +- + id: 34 + lower_name: r-lyeh + name: r-lyeh + full_name: user34 + email: user34@example.com + keep_email_private: true + email_notifications_preference: enabled + passwd: a3d5fcd92bae586c2e3dbe72daea7a0d27833a8d0227aa1704f4bbd775c1f3b03535b76dd93b0d4d8d22a519dca47df1547b + passwd_hash_algo: argon2 + must_change_password: false + login_source: 0 + login_name: user34 + type: 0 + salt: ZogKvWdyEx + max_repo_creation: -1 + is_active: true + is_admin: false + is_restricted: false + allow_git_hook: false + allow_import_local: false + allow_create_organization: true + prohibit_login: false + avatar: avatar34 + avatar_email: user34@example.com + use_custom_avatar: false + num_followers: 0 + num_following: 0 + num_stars: 0 + num_repos: 0 + num_teams: 0 + num_members: 0 + visibility: 0 + repo_admin_change_team_access: false + theme: "" + keep_activity_private: false \ No newline at end of file diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 12c6288c24d12..8077b679e9196 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -6,10 +6,11 @@ package markdown_test import ( "context" - "os" + "path/filepath" "strings" "testing" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" @@ -38,7 +39,10 @@ func TestMain(m *testing.M) { if err := git.InitSimple(context.Background()); err != nil { log.Fatal("git init failed, err: %v", err) } - os.Exit(m.Run()) + unittest.MainTest(m, &unittest.TestOptions{ + GiteaRootPath: filepath.Join("..", "..", ".."), + FixtureFiles: []string{"user.yml"}, + }) } func TestRender_StandardLinks(t *testing.T) { @@ -300,6 +304,8 @@ func TestTotal_RenderWiki(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL + assert.NoError(t, unittest.PrepareTestDatabase()) + answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/")) for i := 0; i < len(sameCases); i++ { From 459ce5b2f4b973269bacf2b7724ab60560812902 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 21 Oct 2022 21:03:26 +0000 Subject: [PATCH 04/14] fix test --- routers/api/v1/misc/main_test.go | 19 +++++++++++++++++++ routers/api/v1/misc/markdown_test.go | 3 +++ 2 files changed, 22 insertions(+) create mode 100644 routers/api/v1/misc/main_test.go diff --git a/routers/api/v1/misc/main_test.go b/routers/api/v1/misc/main_test.go new file mode 100644 index 0000000000000..e08538b70f089 --- /dev/null +++ b/routers/api/v1/misc/main_test.go @@ -0,0 +1,19 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package misc + +import ( + "path/filepath" + "testing" + + "code.gitea.io/gitea/models/unittest" +) + +func TestMain(m *testing.M) { + unittest.MainTest(m, &unittest.TestOptions{ + GiteaRootPath: filepath.Join("..", "..", "..", ".."), + FixtureFiles: []string{"user.yml"}, + }) +} diff --git a/routers/api/v1/misc/markdown_test.go b/routers/api/v1/misc/markdown_test.go index 7809fa5cc72a0..78039a76c44f7 100644 --- a/routers/api/v1/misc/markdown_test.go +++ b/routers/api/v1/misc/markdown_test.go @@ -12,6 +12,7 @@ import ( "strings" "testing" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -51,6 +52,8 @@ func wrap(ctx *context.Context) *context.APIContext { func TestAPI_RenderGFM(t *testing.T) { setting.AppURL = AppURL + assert.NoError(t, unittest.PrepareTestDatabase()) + options := api.MarkdownOption{ Mode: "gfm", Text: "", From a23d5a0823df5b549d7a7b21081930cf8d7440b1 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sat, 22 Oct 2022 08:07:07 +0000 Subject: [PATCH 05/14] Fix TestSearchUsers Signed-off-by: Yarden Shoham --- models/user/user_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/user/user_test.go b/models/user/user_test.go index 5f2ac0a60c17d..014bb784f4c61 100644 --- a/models/user/user_test.go +++ b/models/user/user_test.go @@ -96,13 +96,13 @@ func TestSearchUsers(t *testing.T) { } testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}}, - []int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32}) + []int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34}) testUserSuccess(&user_model.SearchUserOptions{ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolFalse}, []int64{9}) testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, - []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32}) + []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34}) testUserSuccess(&user_model.SearchUserOptions{Keyword: "user1", OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, []int64{1, 10, 11, 12, 13, 14, 15, 16, 18}) From d2b211ee553149c648d74e090a18af18d36e9afb Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sat, 22 Oct 2022 09:32:22 +0000 Subject: [PATCH 06/14] Drop db dependency Signed-off-by: Yarden Shoham --- modules/markup/html.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index 3f853349ec2bc..6defcc90cbc0f 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -14,7 +14,6 @@ import ( "strings" "sync" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/emoji" @@ -608,7 +607,7 @@ func mentionProcessor(ctx *RenderContext, node *html.Node) { mentionedUsername := mention[1:] // Only link if the user actually exists - userExists, err := user.IsUserExist(db.DefaultContext, 0, mentionedUsername) + userExists, err := user.IsUserExist(ctx.Ctx, 0, mentionedUsername) if err != nil { log.Error("Failed to validate user in mention %v exists, assuming it does", mention) userExists = true From aab51ec3adab5ecc012dee4a35128058c63736c6 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sat, 22 Oct 2022 09:47:53 +0000 Subject: [PATCH 07/14] Fix test Signed-off-by: Yarden Shoham --- tests/integration/setting_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/setting_test.go b/tests/integration/setting_test.go index 6273545c23623..dc81febf2746c 100644 --- a/tests/integration/setting_test.go +++ b/tests/integration/setting_test.go @@ -26,7 +26,7 @@ func TestSettingShowUserEmailExplore(t *testing.T) { htmlDoc := NewHTMLParser(t, resp.Body) assert.Contains(t, htmlDoc.doc.Find(".ui.user.list").Text(), - "user4@example.com", + "user10@example.com", ) setting.UI.ShowUserEmail = false @@ -36,7 +36,7 @@ func TestSettingShowUserEmailExplore(t *testing.T) { htmlDoc = NewHTMLParser(t, resp.Body) assert.NotContains(t, htmlDoc.doc.Find(".ui.user.list").Text(), - "user4@example.com", + "user10@example.com", ) setting.UI.ShowUserEmail = showUserEmail From f486a5f56bb23911f3d348545697a7d7e70cfd72 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 22 Oct 2022 18:06:37 +0800 Subject: [PATCH 08/14] refactor --- contrib/pr/checkout.go | 3 +- models/fixtures/user.yml | 37 ------------------------ models/user/user_test.go | 4 +-- modules/markup/html.go | 9 +----- modules/markup/markdown/markdown_test.go | 10 ++----- modules/markup/renderer.go | 12 +++++++- routers/api/v1/misc/main_test.go | 19 ------------ routers/init.go | 3 +- services/markup/processorhelper.go | 24 +++++++++++++++ 9 files changed, 44 insertions(+), 77 deletions(-) delete mode 100644 routers/api/v1/misc/main_test.go create mode 100644 services/markup/processorhelper.go diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 09510ac2c5756..686a3ddffaac2 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -33,6 +33,7 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers" + markup_service "code.gitea.io/gitea/services/markup" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/config" @@ -112,7 +113,7 @@ func runPR() { log.Printf("[PR] Setting up router\n") // routers.GlobalInit() external.RegisterRenderers() - markup.Init() + markup.Init(markup_service.ProcessorHelper()) c := routers.NormalRoutes(graceful.GetManager().HammerContext()) log.Printf("[PR] Ready for testing !\n") diff --git a/models/fixtures/user.yml b/models/fixtures/user.yml index b332b9ff36f47..0e3348e146a82 100644 --- a/models/fixtures/user.yml +++ b/models/fixtures/user.yml @@ -1220,40 +1220,3 @@ repo_admin_change_team_access: false theme: "" keep_activity_private: false - -- - id: 34 - lower_name: r-lyeh - name: r-lyeh - full_name: user34 - email: user34@example.com - keep_email_private: true - email_notifications_preference: enabled - passwd: a3d5fcd92bae586c2e3dbe72daea7a0d27833a8d0227aa1704f4bbd775c1f3b03535b76dd93b0d4d8d22a519dca47df1547b - passwd_hash_algo: argon2 - must_change_password: false - login_source: 0 - login_name: user34 - type: 0 - salt: ZogKvWdyEx - max_repo_creation: -1 - is_active: true - is_admin: false - is_restricted: false - allow_git_hook: false - allow_import_local: false - allow_create_organization: true - prohibit_login: false - avatar: avatar34 - avatar_email: user34@example.com - use_custom_avatar: false - num_followers: 0 - num_following: 0 - num_stars: 0 - num_repos: 0 - num_teams: 0 - num_members: 0 - visibility: 0 - repo_admin_change_team_access: false - theme: "" - keep_activity_private: false \ No newline at end of file diff --git a/models/user/user_test.go b/models/user/user_test.go index 014bb784f4c61..5f2ac0a60c17d 100644 --- a/models/user/user_test.go +++ b/models/user/user_test.go @@ -96,13 +96,13 @@ func TestSearchUsers(t *testing.T) { } testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}}, - []int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34}) + []int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32}) testUserSuccess(&user_model.SearchUserOptions{ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolFalse}, []int64{9}) testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, - []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34}) + []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32}) testUserSuccess(&user_model.SearchUserOptions{Keyword: "user1", OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, []int64{1, 10, 11, 12, 13, 14, 15, 16, 18}) diff --git a/modules/markup/html.go b/modules/markup/html.go index 6defcc90cbc0f..ae00c3905fe8b 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -14,7 +14,6 @@ import ( "strings" "sync" - "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/emoji" "code.gitea.io/gitea/modules/git" @@ -606,13 +605,7 @@ func mentionProcessor(ctx *RenderContext, node *html.Node) { } mentionedUsername := mention[1:] - // Only link if the user actually exists - userExists, err := user.IsUserExist(ctx.Ctx, 0, mentionedUsername) - if err != nil { - log.Error("Failed to validate user in mention %v exists, assuming it does", mention) - userExists = true - } - if userExists { + if processorHelper.IsUsernameMentionable != nil && processorHelper.IsUsernameMentionable(ctx.Ctx, mentionedUsername) { replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, mentionedUsername), mention, "mention")) node = node.NextSibling.NextSibling } else { diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 8077b679e9196..12c6288c24d12 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -6,11 +6,10 @@ package markdown_test import ( "context" - "path/filepath" + "os" "strings" "testing" - "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" @@ -39,10 +38,7 @@ func TestMain(m *testing.M) { if err := git.InitSimple(context.Background()); err != nil { log.Fatal("git init failed, err: %v", err) } - unittest.MainTest(m, &unittest.TestOptions{ - GiteaRootPath: filepath.Join("..", "..", ".."), - FixtureFiles: []string{"user.yml"}, - }) + os.Exit(m.Run()) } func TestRender_StandardLinks(t *testing.T) { @@ -304,8 +300,6 @@ func TestTotal_RenderWiki(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL - assert.NoError(t, unittest.PrepareTestDatabase()) - answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/")) for i := 0; i < len(sameCases); i++ { diff --git a/modules/markup/renderer.go b/modules/markup/renderer.go index 5f69dc72354f0..b3289cb3c3b04 100644 --- a/modules/markup/renderer.go +++ b/modules/markup/renderer.go @@ -19,8 +19,18 @@ import ( "code.gitea.io/gitea/modules/setting" ) +type ProcessorHelper struct { + IsUsernameMentionable func(ctx context.Context, username string) bool +} + +var processorHelper ProcessorHelper + // Init initialize regexps for markdown parsing -func Init() { +func Init(ph *ProcessorHelper) { + if ph != nil { + processorHelper = *ph + } + NewSanitizer() if len(setting.Markdown.CustomURLSchemes) > 0 { CustomLinkURLSchemes(setting.Markdown.CustomURLSchemes) diff --git a/routers/api/v1/misc/main_test.go b/routers/api/v1/misc/main_test.go deleted file mode 100644 index e08538b70f089..0000000000000 --- a/routers/api/v1/misc/main_test.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2022 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package misc - -import ( - "path/filepath" - "testing" - - "code.gitea.io/gitea/models/unittest" -) - -func TestMain(m *testing.M) { - unittest.MainTest(m, &unittest.TestOptions{ - GiteaRootPath: filepath.Join("..", "..", "..", ".."), - FixtureFiles: []string{"user.yml"}, - }) -} diff --git a/routers/init.go b/routers/init.go index 0f2e993413af8..9045437f872b5 100644 --- a/routers/init.go +++ b/routers/init.go @@ -41,6 +41,7 @@ import ( "code.gitea.io/gitea/services/automerge" "code.gitea.io/gitea/services/cron" "code.gitea.io/gitea/services/mailer" + markup_service "code.gitea.io/gitea/services/markup" repo_migrations "code.gitea.io/gitea/services/migrations" mirror_service "code.gitea.io/gitea/services/mirror" pull_service "code.gitea.io/gitea/services/pull" @@ -123,7 +124,7 @@ func GlobalInitInstalled(ctx context.Context) { highlight.NewContext() external.RegisterRenderers() - markup.Init() + markup.Init(markup_service.ProcessorHelper()) if setting.EnableSQLite3 { log.Info("SQLite3 support is enabled") diff --git a/services/markup/processorhelper.go b/services/markup/processorhelper.go new file mode 100644 index 0000000000000..60e4a933db36e --- /dev/null +++ b/services/markup/processorhelper.go @@ -0,0 +1,24 @@ +package markup + +import ( + "context" + + "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/markup" +) + +func ProcessorHelper() *markup.ProcessorHelper { + return &markup.ProcessorHelper{ + IsUsernameMentionable: func(ctx context.Context, username string) bool { + // here, you could also try to cast the ctx to a modules/context.Context + // Only link if the user actually exists + userExists, err := user.IsUserExist(ctx, 0, username) + if err != nil { + log.Error("Failed to validate user in mention %q exists, assuming it does", username) + userExists = true + } + return userExists + }, + } +} From 2368ec8a168492631c8398010e366a950c4d83ea Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sat, 22 Oct 2022 10:49:22 +0000 Subject: [PATCH 09/14] Try to fix tests Signed-off-by: Yarden Shoham --- models/fixtures/user.yml | 37 ++++++++++++++++++++++++ models/user/user_test.go | 4 +-- modules/markup/markdown/markdown_test.go | 13 +++++---- routers/api/v1/misc/main_test.go | 19 ++++++++++++ 4 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 routers/api/v1/misc/main_test.go diff --git a/models/fixtures/user.yml b/models/fixtures/user.yml index 0e3348e146a82..d043c506c9632 100644 --- a/models/fixtures/user.yml +++ b/models/fixtures/user.yml @@ -1220,3 +1220,40 @@ repo_admin_change_team_access: false theme: "" keep_activity_private: false + +- + id: 34 + lower_name: r-lyeh + name: r-lyeh + full_name: user34 + email: user34@example.com + keep_email_private: true + email_notifications_preference: enabled + passwd: a3d5fcd92bae586c2e3dbe72daea7a0d27833a8d0227aa1704f4bbd775c1f3b03535b76dd93b0d4d8d22a519dca47df1547b + passwd_hash_algo: argon2 + must_change_password: false + login_source: 0 + login_name: user34 + type: 0 + salt: ZogKvWdyEx + max_repo_creation: -1 + is_active: true + is_admin: false + is_restricted: false + allow_git_hook: false + allow_import_local: false + allow_create_organization: true + prohibit_login: false + avatar: avatar34 + avatar_email: user34@example.com + use_custom_avatar: false + num_followers: 0 + num_following: 0 + num_stars: 0 + num_repos: 0 + num_teams: 0 + num_members: 0 + visibility: 0 + repo_admin_change_team_access: false + theme: "" + keep_activity_private: false \ No newline at end of file diff --git a/models/user/user_test.go b/models/user/user_test.go index 5f2ac0a60c17d..014bb784f4c61 100644 --- a/models/user/user_test.go +++ b/models/user/user_test.go @@ -96,13 +96,13 @@ func TestSearchUsers(t *testing.T) { } testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}}, - []int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32}) + []int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34}) testUserSuccess(&user_model.SearchUserOptions{ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolFalse}, []int64{9}) testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, - []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32}) + []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34}) testUserSuccess(&user_model.SearchUserOptions{Keyword: "user1", OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, []int64{1, 10, 11, 12, 13, 14, 15, 16, 18}) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 12c6288c24d12..1b2409ac383ad 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -5,11 +5,11 @@ package markdown_test import ( - "context" - "os" + "path/filepath" "strings" "testing" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" @@ -35,10 +35,10 @@ var localMetas = map[string]string{ func TestMain(m *testing.M) { setting.LoadAllowEmpty() - if err := git.InitSimple(context.Background()); err != nil { - log.Fatal("git init failed, err: %v", err) - } - os.Exit(m.Run()) + unittest.MainTest(m, &unittest.TestOptions{ + GiteaRootPath: filepath.Join("..", "..", ".."), + FixtureFiles: []string{"user.yml"}, + }) } func TestRender_StandardLinks(t *testing.T) { @@ -299,6 +299,7 @@ This PR has been generated by [Renovate Bot](https://github.com/renovatebot/reno func TestTotal_RenderWiki(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL + assert.NoError(t, unittest.PrepareTestDatabase()) answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/")) diff --git a/routers/api/v1/misc/main_test.go b/routers/api/v1/misc/main_test.go new file mode 100644 index 0000000000000..e08538b70f089 --- /dev/null +++ b/routers/api/v1/misc/main_test.go @@ -0,0 +1,19 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package misc + +import ( + "path/filepath" + "testing" + + "code.gitea.io/gitea/models/unittest" +) + +func TestMain(m *testing.M) { + unittest.MainTest(m, &unittest.TestOptions{ + GiteaRootPath: filepath.Join("..", "..", "..", ".."), + FixtureFiles: []string{"user.yml"}, + }) +} From 282869b21e17c028ab1950e6914082d00606114c Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sat, 22 Oct 2022 10:55:29 +0000 Subject: [PATCH 10/14] Update comment Signed-off-by: Yarden Shoham --- services/markup/processorhelper.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/markup/processorhelper.go b/services/markup/processorhelper.go index 60e4a933db36e..d802e15bc324e 100644 --- a/services/markup/processorhelper.go +++ b/services/markup/processorhelper.go @@ -11,7 +11,8 @@ import ( func ProcessorHelper() *markup.ProcessorHelper { return &markup.ProcessorHelper{ IsUsernameMentionable: func(ctx context.Context, username string) bool { - // here, you could also try to cast the ctx to a modules/context.Context + // TODO: cast ctx to modules/context.Context and use IsUserVisibleToViewer + // Only link if the user actually exists userExists, err := user.IsUserExist(ctx, 0, username) if err != nil { From 30c9ab06f9911f97461270d2bedda47a37f92591 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sat, 22 Oct 2022 11:25:31 +0000 Subject: [PATCH 11/14] Adjust tests Signed-off-by: Yarden Shoham --- models/fixtures/user.yml | 37 ------------------------ models/user/user_test.go | 4 +-- modules/markup/markdown/markdown_test.go | 8 ++--- routers/api/v1/misc/markdown_test.go | 4 +-- tests/integration/setting_test.go | 4 +-- 5 files changed, 10 insertions(+), 47 deletions(-) diff --git a/models/fixtures/user.yml b/models/fixtures/user.yml index d043c506c9632..0e3348e146a82 100644 --- a/models/fixtures/user.yml +++ b/models/fixtures/user.yml @@ -1220,40 +1220,3 @@ repo_admin_change_team_access: false theme: "" keep_activity_private: false - -- - id: 34 - lower_name: r-lyeh - name: r-lyeh - full_name: user34 - email: user34@example.com - keep_email_private: true - email_notifications_preference: enabled - passwd: a3d5fcd92bae586c2e3dbe72daea7a0d27833a8d0227aa1704f4bbd775c1f3b03535b76dd93b0d4d8d22a519dca47df1547b - passwd_hash_algo: argon2 - must_change_password: false - login_source: 0 - login_name: user34 - type: 0 - salt: ZogKvWdyEx - max_repo_creation: -1 - is_active: true - is_admin: false - is_restricted: false - allow_git_hook: false - allow_import_local: false - allow_create_organization: true - prohibit_login: false - avatar: avatar34 - avatar_email: user34@example.com - use_custom_avatar: false - num_followers: 0 - num_following: 0 - num_stars: 0 - num_repos: 0 - num_teams: 0 - num_members: 0 - visibility: 0 - repo_admin_change_team_access: false - theme: "" - keep_activity_private: false \ No newline at end of file diff --git a/models/user/user_test.go b/models/user/user_test.go index 014bb784f4c61..5f2ac0a60c17d 100644 --- a/models/user/user_test.go +++ b/models/user/user_test.go @@ -96,13 +96,13 @@ func TestSearchUsers(t *testing.T) { } testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}}, - []int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34}) + []int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32}) testUserSuccess(&user_model.SearchUserOptions{ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolFalse}, []int64{9}) testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, - []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34}) + []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32}) testUserSuccess(&user_model.SearchUserOptions{Keyword: "user1", OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, []int64{1, 10, 11, 12, 13, 14, 15, 16, 18}) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 1b2409ac383ad..2880a36409988 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -144,8 +144,8 @@ func testAnswers(baseURLContent, baseURLImages string) []string {

See commit 65f1bf27bc

Ideas and codes

    -
  • Bezier widget (by @r-lyeh) ocornut/imgui#786
  • -
  • Bezier widget (by @r-lyeh) #786
  • +
  • Bezier widget (by @user10) ocornut/imgui#786
  • +
  • Bezier widget (by @user10) #786
  • Node graph editors https://github.com/ocornut/imgui/issues/306
  • Memory Editor
  • Plot var helper
  • @@ -229,8 +229,8 @@ See commit 65f1bf27bc Ideas and codes -- Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786 -- Bezier widget (by @r-lyeh) ` + AppURL + `gogits/gogs/issues/786 +- Bezier widget (by @user10) ` + AppURL + `ocornut/imgui/issues/786 +- Bezier widget (by @user10) ` + AppURL + `gogits/gogs/issues/786 - Node graph editors https://github.com/ocornut/imgui/issues/306 - [[Memory Editor|memory_editor_example]] - [[Plot var helper|plot_var_example]]`, diff --git a/routers/api/v1/misc/markdown_test.go b/routers/api/v1/misc/markdown_test.go index 78039a76c44f7..f8ce06a147378 100644 --- a/routers/api/v1/misc/markdown_test.go +++ b/routers/api/v1/misc/markdown_test.go @@ -73,13 +73,13 @@ func TestAPI_RenderGFM(t *testing.T) { `Wiki! Enjoy :) - [[Links, Language bindings, Engine bindings|Links]] - [[Tips]] -- Bezier widget (by @r-lyeh) https://github.com/ocornut/imgui/issues/786`, +- Bezier widget (by @user10) https://github.com/ocornut/imgui/issues/786`, // rendered `

    Wiki! Enjoy :)

    `, // wine-staging wiki home extract: special wiki syntax, images diff --git a/tests/integration/setting_test.go b/tests/integration/setting_test.go index dc81febf2746c..6273545c23623 100644 --- a/tests/integration/setting_test.go +++ b/tests/integration/setting_test.go @@ -26,7 +26,7 @@ func TestSettingShowUserEmailExplore(t *testing.T) { htmlDoc := NewHTMLParser(t, resp.Body) assert.Contains(t, htmlDoc.doc.Find(".ui.user.list").Text(), - "user10@example.com", + "user4@example.com", ) setting.UI.ShowUserEmail = false @@ -36,7 +36,7 @@ func TestSettingShowUserEmailExplore(t *testing.T) { htmlDoc = NewHTMLParser(t, resp.Body) assert.NotContains(t, htmlDoc.doc.Find(".ui.user.list").Text(), - "user10@example.com", + "user4@example.com", ) setting.UI.ShowUserEmail = showUserEmail From 2f7075d318cbe2fd46f0e14fbbb73efddc8361a1 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sat, 22 Oct 2022 11:40:47 +0000 Subject: [PATCH 12/14] Drop DB prep in tests --- modules/markup/markdown/markdown_test.go | 13 ++++++------- routers/api/v1/misc/main_test.go | 19 ------------------- 2 files changed, 6 insertions(+), 26 deletions(-) delete mode 100644 routers/api/v1/misc/main_test.go diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 2880a36409988..d67a5280f0f5a 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -5,11 +5,11 @@ package markdown_test import ( - "path/filepath" + "context" + "os" "strings" "testing" - "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" @@ -35,10 +35,10 @@ var localMetas = map[string]string{ func TestMain(m *testing.M) { setting.LoadAllowEmpty() - unittest.MainTest(m, &unittest.TestOptions{ - GiteaRootPath: filepath.Join("..", "..", ".."), - FixtureFiles: []string{"user.yml"}, - }) + if err := git.InitSimple(context.Background()); err != nil { + log.Fatal("git init failed, err: %v", err) + } + os.Exit(m.Run()) } func TestRender_StandardLinks(t *testing.T) { @@ -299,7 +299,6 @@ This PR has been generated by [Renovate Bot](https://github.com/renovatebot/reno func TestTotal_RenderWiki(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL - assert.NoError(t, unittest.PrepareTestDatabase()) answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/")) diff --git a/routers/api/v1/misc/main_test.go b/routers/api/v1/misc/main_test.go deleted file mode 100644 index e08538b70f089..0000000000000 --- a/routers/api/v1/misc/main_test.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2022 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package misc - -import ( - "path/filepath" - "testing" - - "code.gitea.io/gitea/models/unittest" -) - -func TestMain(m *testing.M) { - unittest.MainTest(m, &unittest.TestOptions{ - GiteaRootPath: filepath.Join("..", "..", "..", ".."), - FixtureFiles: []string{"user.yml"}, - }) -} From 42e9e96dcbabe4f0beaff693c337208f299efd77 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 22 Oct 2022 19:40:09 +0800 Subject: [PATCH 13/14] fix tests --- modules/markup/markdown/markdown_test.go | 13 +++++++++---- routers/api/v1/misc/markdown_test.go | 14 +++++++++----- services/markup/main_test.go | 19 +++++++++++++++++++ services/markup/processorhelper_test.go | 16 ++++++++++++++++ 4 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 services/markup/main_test.go create mode 100644 services/markup/processorhelper_test.go diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index d67a5280f0f5a..fbb741d1cd8a7 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -38,6 +38,11 @@ func TestMain(m *testing.M) { if err := git.InitSimple(context.Background()); err != nil { log.Fatal("git init failed, err: %v", err) } + markup.Init(&markup.ProcessorHelper{ + IsUsernameMentionable: func(ctx context.Context, username string) bool { + return username == "r-lyeh" + }, + }) os.Exit(m.Run()) } @@ -144,8 +149,8 @@ func testAnswers(baseURLContent, baseURLImages string) []string {

    See commit 65f1bf27bc

    Ideas and codes

      -
    • Bezier widget (by @user10) ocornut/imgui#786
    • -
    • Bezier widget (by @user10) #786
    • +
    • Bezier widget (by @r-lyeh) ocornut/imgui#786
    • +
    • Bezier widget (by @r-lyeh) #786
    • Node graph editors https://github.com/ocornut/imgui/issues/306
    • Memory Editor
    • Plot var helper
    • @@ -229,8 +234,8 @@ See commit 65f1bf27bc Ideas and codes -- Bezier widget (by @user10) ` + AppURL + `ocornut/imgui/issues/786 -- Bezier widget (by @user10) ` + AppURL + `gogits/gogs/issues/786 +- Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786 +- Bezier widget (by @r-lyeh) ` + AppURL + `gogits/gogs/issues/786 - Node graph editors https://github.com/ocornut/imgui/issues/306 - [[Memory Editor|memory_editor_example]] - [[Plot var helper|plot_var_example]]`, diff --git a/routers/api/v1/misc/markdown_test.go b/routers/api/v1/misc/markdown_test.go index f8ce06a147378..65ce06027802c 100644 --- a/routers/api/v1/misc/markdown_test.go +++ b/routers/api/v1/misc/markdown_test.go @@ -5,6 +5,7 @@ package misc import ( + go_context "context" "io" "net/http" "net/http/httptest" @@ -12,8 +13,8 @@ import ( "strings" "testing" - "code.gitea.io/gitea/models/unittest" "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/templates" @@ -51,8 +52,11 @@ func wrap(ctx *context.Context) *context.APIContext { func TestAPI_RenderGFM(t *testing.T) { setting.AppURL = AppURL - - assert.NoError(t, unittest.PrepareTestDatabase()) + markup.Init(&markup.ProcessorHelper{ + IsUsernameMentionable: func(ctx go_context.Context, username string) bool { + return username == "r-lyeh" + }, + }) options := api.MarkdownOption{ Mode: "gfm", @@ -73,13 +77,13 @@ func TestAPI_RenderGFM(t *testing.T) { `Wiki! Enjoy :) - [[Links, Language bindings, Engine bindings|Links]] - [[Tips]] -- Bezier widget (by @user10) https://github.com/ocornut/imgui/issues/786`, +- Bezier widget (by @r-lyeh) https://github.com/ocornut/imgui/issues/786`, // rendered `

      Wiki! Enjoy :)

      `, // wine-staging wiki home extract: special wiki syntax, images diff --git a/services/markup/main_test.go b/services/markup/main_test.go new file mode 100644 index 0000000000000..8efd08e69d64e --- /dev/null +++ b/services/markup/main_test.go @@ -0,0 +1,19 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package markup + +import ( + "path/filepath" + "testing" + + "code.gitea.io/gitea/models/unittest" +) + +func TestMain(m *testing.M) { + unittest.MainTest(m, &unittest.TestOptions{ + GiteaRootPath: filepath.Join("..", ".."), + FixtureFiles: []string{"user.yml"}, + }) +} diff --git a/services/markup/processorhelper_test.go b/services/markup/processorhelper_test.go new file mode 100644 index 0000000000000..17ff67ef948c9 --- /dev/null +++ b/services/markup/processorhelper_test.go @@ -0,0 +1,16 @@ +package markup + +import ( + "context" + "testing" + + "code.gitea.io/gitea/models/unittest" + + "github.com/stretchr/testify/assert" +) + +func TestProcessorHelper(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + assert.True(t, ProcessorHelper().IsUsernameMentionable(context.Background(), "user10")) + assert.False(t, ProcessorHelper().IsUsernameMentionable(context.Background(), "no-such-user")) +} From d08bc563975ddb2294a1dddd4dc73ed2773adbaf Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 22 Oct 2022 19:50:57 +0800 Subject: [PATCH 14/14] oops, fix the lint --- services/markup/processorhelper.go | 4 ++++ services/markup/processorhelper_test.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/services/markup/processorhelper.go b/services/markup/processorhelper.go index d802e15bc324e..2b1cac2a5b886 100644 --- a/services/markup/processorhelper.go +++ b/services/markup/processorhelper.go @@ -1,3 +1,7 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package markup import ( diff --git a/services/markup/processorhelper_test.go b/services/markup/processorhelper_test.go index 17ff67ef948c9..386465bc9189c 100644 --- a/services/markup/processorhelper_test.go +++ b/services/markup/processorhelper_test.go @@ -1,3 +1,7 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package markup import (