From 0bc12d658ae00324ac8a3d88d3d41261ab31cfa2 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Wed, 13 May 2026 08:15:10 +0200 Subject: [PATCH] forgery: refactor user_profile_test --- tests/integration/user_profile_test.go | 107 +++++++++---------------- 1 file changed, 38 insertions(+), 69 deletions(-) diff --git a/tests/integration/user_profile_test.go b/tests/integration/user_profile_test.go index 10f3ce6be6..12ee58a0d6 100644 --- a/tests/integration/user_profile_test.go +++ b/tests/integration/user_profile_test.go @@ -9,14 +9,12 @@ import ( "strings" "testing" - "forgejo.org/models/unittest" - user_model "forgejo.org/models/user" "forgejo.org/modules/git" "forgejo.org/modules/setting" "forgejo.org/modules/test" repo_service "forgejo.org/services/repository" - files_service "forgejo.org/services/repository/files" "forgejo.org/tests" + "forgejo.org/tests/forgery" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -24,36 +22,23 @@ import ( func TestUserProfile(t *testing.T) { onApplicationRun(t, func(t *testing.T, u *url.URL) { + user := forgery.CreateUser(t, nil) checkReadme := func(t *testing.T, title, readmeFilename string, expectedCount int) { t.Run(title, func(t *testing.T) { defer tests.PrintCurrentTest(t)() // Prepare the test repository - user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) - - var ops []*files_service.ChangeRepoFile - op := "create" - if readmeFilename != "README.md" { - ops = append(ops, &files_service.ChangeRepoFile{ - Operation: "delete", - TreePath: "README.md", - }) - } else { - op = "update" - } + files := forgery.MapFS{} if readmeFilename != "" { - ops = append(ops, &files_service.ChangeRepoFile{ - Operation: op, - TreePath: readmeFilename, - ContentReader: strings.NewReader("# Hi!\n"), - }) + files[readmeFilename] = forgery.MapFile("# Hi!\n") } - - _, _, f := tests.CreateDeclarativeRepo(t, user2, ".profile", nil, nil, ops) - defer f() + _ = forgery.CreateRepository(t, user, &forgery.CreateRepositoryOptions{ + Name: ".profile", + Files: files, + }) // Perform the test - req := NewRequest(t, "GET", "/user2") + req := NewRequest(t, "GET", "/"+user.Name) resp := MakeRequest(t, req, http.StatusOK) doc := NewHTMLParser(t, resp.Body) @@ -80,26 +65,21 @@ func TestUserProfile(t *testing.T) { t.Run("readme-size", func(t *testing.T) { defer tests.PrintCurrentTest(t)() - // Prepare the test repository - user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) - - _, _, f := tests.CreateDeclarativeRepo(t, user2, ".profile", nil, nil, []*files_service.ChangeRepoFile{ - { - Operation: "update", - TreePath: "README.md", - ContentReader: strings.NewReader(`## Lorem ipsum + _ = forgery.CreateRepository(t, user, &forgery.CreateRepositoryOptions{ + Name: ".profile", + Files: forgery.MapFS{ + "README.md": forgery.MapFile(`## Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ## Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum`), }, }) - defer f() t.Run("full", func(t *testing.T) { defer tests.PrintCurrentTest(t)() defer test.MockVariableValue(&setting.UI.MaxDisplayFileSize, 500)() - req := NewRequest(t, "GET", "/user2") + req := NewRequest(t, "GET", "/"+user.Name) resp := MakeRequest(t, req, http.StatusOK) assert.Contains(t, resp.Body.String(), "Ut enim ad minim veniam") assert.Contains(t, resp.Body.String(), "mollit anim id est laborum") @@ -109,7 +89,7 @@ quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequa defer tests.PrintCurrentTest(t)() defer test.MockVariableValue(&setting.UI.MaxDisplayFileSize, 146)() - req := NewRequest(t, "GET", "/user2") + req := NewRequest(t, "GET", "/"+user.Name) resp := MakeRequest(t, req, http.StatusOK) assert.Contains(t, resp.Body.String(), "Ut enim ad minim") assert.NotContains(t, resp.Body.String(), "veniam") @@ -119,49 +99,46 @@ quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequa t.Run("forked-profile-repo", func(t *testing.T) { defer tests.PrintCurrentTest(t)() - // Create users - user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) - user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}) + // Create forking user + forkingUser := forgery.CreateUser(t, nil) - // Create original .profile repository for user2 - originalRepo, _, f1 := tests.CreateDeclarativeRepo(t, user2, ".profile", nil, nil, []*files_service.ChangeRepoFile{ - { - Operation: "update", - TreePath: "README.md", - ContentReader: strings.NewReader("# Original Profile Content\nThis should show up on user2 profile."), + // Create original .profile repository + originalRepo := forgery.CreateRepository(t, user, &forgery.CreateRepositoryOptions{ + Name: ".profile", + Files: forgery.MapFS{ + "README.md": forgery.MapFile("# Original Profile Content\nThis should show up on user profile."), }, }) - defer f1() - // Fork the .profile repository to user4 - forkedRepo, err := repo_service.ForkRepositoryAndUpdates(git.DefaultContext, user2, user4, repo_service.ForkRepoOptions{ + // Fork the .profile repository to forking user + forkedRepo, err := repo_service.ForkRepositoryAndUpdates(git.DefaultContext, user, forkingUser, repo_service.ForkRepoOptions{ BaseRepo: originalRepo, Name: ".profile", }) require.NoError(t, err) - // Verify that user2's profile shows the original content - req := NewRequest(t, "GET", "/user2") + // Verify that the original user's profile shows the original content + req := NewRequest(t, "GET", "/"+user.Name) resp := MakeRequest(t, req, http.StatusOK) // Check if the content appears in the response body bodyStr := resp.Body.String() if strings.Contains(bodyStr, "Original Profile Content") { // Original profile is working correctly - assert.Contains(t, bodyStr, "This should show up on user2 profile", "Original profile should render content") + assert.Contains(t, bodyStr, "This should show up on user profile", "Original profile should render content") } - // Verify that user4's profile does NOT show the forked content + // Verify that forking user's profile does NOT show the forked content // Since it's a fork, it should not render as a profile page (this is the main test) - req = NewRequest(t, "GET", "/user4") + req = NewRequest(t, "GET", "/"+forkingUser.Name) resp = MakeRequest(t, req, http.StatusOK) bodyStr = resp.Body.String() // The main assertion: forked .profile content should NOT appear on user profile assert.NotContains(t, bodyStr, "Original Profile Content", "Forked .profile repo should NOT render profile content") - assert.NotContains(t, bodyStr, "This should show up on user2 profile", "Forked .profile repo should NOT render profile content") + assert.NotContains(t, bodyStr, "This should show up on user profile", "Forked .profile repo should NOT render profile content") // Ensure the forked repository still exists and is accessible directly - req = NewRequest(t, "GET", "/user4/.profile") + req = NewRequest(t, "GET", "/"+forkingUser.Name+"/.profile") resp = MakeRequest(t, req, http.StatusOK) // The repository page should show the content (since it's the same as original) assert.Contains(t, resp.Body.String(), "Original Profile Content", "Forked repo should still be accessible") @@ -174,25 +151,17 @@ quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequa t.Run("private-profile-repo", func(t *testing.T) { defer tests.PrintCurrentTest(t)() - user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) - // Create a private .profile repository - profileRepo, _, f := tests.CreateDeclarativeRepo(t, user2, ".profile", nil, nil, []*files_service.ChangeRepoFile{ - { - Operation: "update", - TreePath: "README.md", - ContentReader: strings.NewReader("# Private Profile Content\nThis should NOT show up on user profile."), + profileRepo := forgery.CreateRepository(t, user, &forgery.CreateRepositoryOptions{ + Name: ".profile", + IsPrivate: true, + Files: forgery.MapFS{ + "README.md": forgery.MapFile("# Private Profile Content\nThis should NOT show up on user profile."), }, }) - defer f() - // Make the repository private - profileRepo.IsPrivate = true - err := repo_service.UpdateRepository(git.DefaultContext, profileRepo, true) - require.NoError(t, err) - - // Verify that user2's profile does NOT show the private content - req := NewRequest(t, "GET", "/user2") + // Verify that user's profile does NOT show the private content + req := NewRequest(t, "GET", "/"+user.Name) resp := MakeRequest(t, req, http.StatusOK) bodyStr := resp.Body.String()