@@ -13,52 +13,17 @@ import (
1313
1414 auth_model "code.gitea.io/gitea/models/auth"
1515 "code.gitea.io/gitea/modules/git"
16- api "code.gitea.io/gitea/modules/structs"
17- "code.gitea.io/gitea/modules/util"
16+ "code.gitea.io/gitea/modules/git/gitcmd"
1817 "code.gitea.io/gitea/tests"
1918
2019 "github.com/PuerkitoBio/goquery"
2120 "github.com/stretchr/testify/assert"
21+ "github.com/stretchr/testify/require"
2222)
2323
24- func assertFileExist (t * testing.T , p string ) {
25- exist , err := util .IsExist (p )
26- assert .NoError (t , err )
27- assert .True (t , exist )
28- }
29-
30- func assertFileEqual (t * testing.T , p string , content []byte ) {
31- bs , err := os .ReadFile (p )
32- assert .NoError (t , err )
33- assert .Equal (t , content , bs )
34- }
35-
36- func TestRepoCloneWiki (t * testing.T ) {
37- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
38- defer tests .PrepareTestEnv (t )()
39-
40- dstPath := t .TempDir ()
41-
42- r := u .String () + "user2/repo1.wiki.git"
43- u , _ = url .Parse (r )
44- u .User = url .UserPassword ("user2" , userPassword )
45- t .Run ("Clone" , func (t * testing.T ) {
46- assert .NoError (t , git .Clone (t .Context (), u .String (), dstPath , git.CloneRepoOptions {}))
47- assertFileEqual (t , filepath .Join (dstPath , "Home.md" ), []byte ("# Home page\n \n This is the home page!\n " ))
48- assertFileExist (t , filepath .Join (dstPath , "Page-With-Image.md" ))
49- assertFileExist (t , filepath .Join (dstPath , "Page-With-Spaced-Name.md" ))
50- assertFileExist (t , filepath .Join (dstPath , "images" ))
51- assertFileExist (t , filepath .Join (dstPath , "files/Non-Renderable-File.zip" ))
52- assertFileExist (t , filepath .Join (dstPath , "jpeg.jpg" ))
53- })
54- })
55- }
56-
57- func Test_RepoWikiPages (t * testing.T ) {
24+ func TestRepoWikiPages (t * testing.T ) {
5825 defer tests .PrepareTestEnv (t )()
59-
60- url := "/user2/repo1/wiki/?action=_pages"
61- req := NewRequest (t , "GET" , url )
26+ req := NewRequest (t , "GET" , "/user2/repo1/wiki/?action=_pages" )
6227 resp := MakeRequest (t , req , http .StatusOK )
6328
6429 doc := NewHTMLParser (t , resp .Body )
@@ -74,45 +39,72 @@ func Test_RepoWikiPages(t *testing.T) {
7439 })
7540}
7641
77- func Test_WikiClone (t * testing.T ) {
42+ func testRepoWikiCloneHTTP (t * testing.T , u * url.URL ) {
43+ // When proc-receive support is enabled globally, the HTTP receive-pack pre-check
44+ // must still require write access for wiki repositories. Exercise this with a
45+ // normal wiki push because the regression is about the pre-check, not agit refs.
46+ require .True (t , git .DefaultFeatures ().SupportProcReceive ) // modern git should all support proc-receive
47+
48+ wikiURL := * u
49+ wikiURL .Path = "/user2/repo1.wiki.git"
50+
51+ dstLocalPath := t .TempDir ()
52+
53+ // reader can clone
54+ wikiURL .User = url .UserPassword ("user20" , userPassword )
55+ require .NoError (t , git .Clone (t .Context (), wikiURL .String (), dstLocalPath , git.CloneRepoOptions {}))
56+ _ , _ , runErr := gitcmd .NewCommand ("fast-import" ).WithDir (dstLocalPath ).WithStdinBytes ([]byte (`commit refs/heads/master
57+ committer unauthorized-user <user20@example.com> 1714310400 +0000
58+ data <<EOM
59+ dummy-message
60+ EOM
61+ from refs/heads/master^0
62+ M 100644 inline Home.md
63+ data <<EOF
64+ changed-content
65+ EOF
66+ ` )).RunStdString (t .Context ())
67+ require .NoError (t , runErr )
68+
69+ content , err := os .ReadFile (filepath .Join (dstLocalPath , "Home.md" ))
70+ assert .NoError (t , err )
71+ assert .Equal (t , "# Home page\n \n This is the home page!\n " , string (content ))
72+
73+ // reader can't push
74+ _ , _ , runErr = gitcmd .NewCommand ("push" , "origin" , "refs/heads/master" ).WithDir (dstLocalPath ).RunStdString (t .Context ())
75+ assert .True (t , gitcmd .StderrContains (runErr , "remote: Repository not found\n " ))
76+ req := NewRequest (t , "GET" , "/user2/repo1/wiki/raw/Home.md" )
77+ resp := MakeRequest (t , req , http .StatusOK )
78+ assert .Contains (t , resp .Body .String (), "This is the home page!" )
79+
80+ // owner can push
81+ wikiURL .User = url .UserPassword ("user2" , userPassword )
82+ _ , _ , runErr = gitcmd .NewCommand ("remote" , "add" , "origin-owner" ).AddDynamicArguments (wikiURL .String ()).WithDir (dstLocalPath ).RunStdString (t .Context ())
83+ require .NoError (t , runErr )
84+ _ , _ , runErr = gitcmd .NewCommand ("push" , "origin-owner" , "refs/heads/master" ).WithDir (dstLocalPath ).RunStdString (t .Context ())
85+ assert .NoError (t , runErr )
86+ req = NewRequest (t , "GET" , "/user2/repo1/wiki/raw/Home.md" )
87+ resp = MakeRequest (t , req , http .StatusOK )
88+ assert .Equal (t , "changed-content" , strings .TrimSpace (resp .Body .String ()))
89+ }
90+
91+ func testRepoWikiCloneSSH (t * testing.T , u * url.URL ) {
92+ dstLocalPath := t .TempDir ()
93+ baseAPITestContext := NewAPITestContext (t , "user2" , "repo1" , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
94+ sshURL := createSSHUrl ("/user2/repo1.wiki.git" , u )
95+
96+ withKeyFile (t , "my-testing-key" , func (keyFile string ) {
97+ t .Run ("CreateUserKey" , doAPICreateUserKey (baseAPITestContext , "test-key" , keyFile ))
98+ assert .NoError (t , git .Clone (t .Context (), sshURL .String (), dstLocalPath , git.CloneRepoOptions {}))
99+ content , err := os .ReadFile (filepath .Join (dstLocalPath , "Home.md" ))
100+ assert .NoError (t , err )
101+ assert .Equal (t , "# Home page\n \n This is the home page!\n " , string (content ))
102+ })
103+ }
104+
105+ func TestRepoWikiClonePush (t * testing.T ) {
78106 onGiteaRun (t , func (t * testing.T , u * url.URL ) {
79- username := "user2"
80- reponame := "repo1"
81- wikiPath := username + "/" + reponame + ".wiki.git"
82- keyname := "my-testing-key"
83- baseAPITestContext := NewAPITestContext (t , username , "repo1" , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
84-
85- u .Path = wikiPath
86-
87- t .Run ("Clone HTTP" , func (t * testing.T ) {
88- defer tests .PrintCurrentTest (t )()
89-
90- dstLocalPath := t .TempDir ()
91- assert .NoError (t , git .Clone (t .Context (), u .String (), dstLocalPath , git.CloneRepoOptions {}))
92- content , err := os .ReadFile (filepath .Join (dstLocalPath , "Home.md" ))
93- assert .NoError (t , err )
94- assert .Equal (t , "# Home page\n \n This is the home page!\n " , string (content ))
95- })
96-
97- t .Run ("Clone SSH" , func (t * testing.T ) {
98- defer tests .PrintCurrentTest (t )()
99-
100- dstLocalPath := t .TempDir ()
101- sshURL := createSSHUrl (wikiPath , u )
102-
103- withKeyFile (t , keyname , func (keyFile string ) {
104- var keyID int64
105- t .Run ("CreateUserKey" , doAPICreateUserKey (baseAPITestContext , "test-key" , keyFile , func (t * testing.T , key api.PublicKey ) {
106- keyID = key .ID
107- }))
108- assert .NotZero (t , keyID )
109-
110- // Setup clone folder
111- assert .NoError (t , git .Clone (t .Context (), sshURL .String (), dstLocalPath , git.CloneRepoOptions {}))
112- content , err := os .ReadFile (filepath .Join (dstLocalPath , "Home.md" ))
113- assert .NoError (t , err )
114- assert .Equal (t , "# Home page\n \n This is the home page!\n " , string (content ))
115- })
116- })
107+ t .Run ("SSH" , func (t * testing.T ) { testRepoWikiCloneSSH (t , u ) })
108+ t .Run ("HTTP" , func (t * testing.T ) { testRepoWikiCloneHTTP (t , u ) })
117109 })
118110}
0 commit comments